Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func SetForTest(agents []Agent) (restore func()) {
if a.NodeID == 0 {
continue
}
if a.Hostname == "" {
continue
}
idx[a.NodeID] = a.Hostname
}
mu.Lock()
Expand Down Expand Up @@ -120,6 +123,9 @@ func Load(raw []byte) error {
if a.NodeID == 0 {
continue // 0 is reserved / would silently match unset fields
}
if a.Hostname == "" {
continue // empty hostname: missing required field — drop
}
idx[a.NodeID] = a.Hostname
}
mu.Lock()
Expand Down
37 changes: 37 additions & 0 deletions zz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ func TestMalformedRejected(t *testing.T) {
}
}

func TestEmptyHostnameSkipped(t *testing.T) {
t.Parallel()

// Entry with non-zero node_id but empty hostname must be dropped,
// so a missing hostname field can't produce an empty-string trust name.
restore := SetForTest([]Agent{
{Hostname: "", NodeID: 7},
{Hostname: "valid", NodeID: 9},
})
defer restore()

if _, ok := IsTrusted(7); ok {
t.Fatal("IsTrusted(7): empty-hostname entry must not match")
}
if _, ok := IsTrusted(9); !ok {
t.Fatal("IsTrusted(9): valid entry should still match")
}
}

func TestLoadEmptyHostnameSkipped(t *testing.T) {
// Not parallel: calls Load() which mutates shared global state.
if err := Load([]byte(`{"agents":[
{"hostname":"","node_id":7},
{"hostname":"valid","node_id":9}
]}`)); err != nil {
t.Fatalf("Load must succeed when skipping empty-hostname entries: %v", err)
}
if _, ok := IsTrusted(7); ok {
t.Fatal("empty-hostname entry must not become trusted after Load")
}
if _, ok := IsTrusted(9); !ok {
t.Fatal("valid entry must still be trusted after Load")
}
// Restore embedded list for downstream tests.
_ = Load(embeddedJSON)
}

func TestAllReturnsCopy(t *testing.T) {
t.Parallel()
restore := SetForTest([]Agent{{Hostname: "a", NodeID: 1}})
Expand Down
Loading