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 @@ -87,6 +87,9 @@ func SetForTest(agents []Agent) (restore func()) {
if a.Hostname == "" {
continue
}
if other, exists := idx[a.NodeID]; exists {
panic(fmt.Sprintf("SetForTest: duplicate node_id %d (hostnames %q and %q)", a.NodeID, other, a.Hostname))
}
idx[a.NodeID] = a.Hostname
}
mu.Lock()
Expand Down Expand Up @@ -202,6 +205,9 @@ func Load(raw []byte) error {
if a.Hostname == "" {
continue // empty hostname: missing required field — drop
}
if other, exists := idx[a.NodeID]; exists {
return fmt.Errorf("duplicate node_id %d in trusted-agents list: %q and %q", a.NodeID, other, a.Hostname)
}
idx[a.NodeID] = a.Hostname
}
mu.Lock()
Expand Down
16 changes: 16 additions & 0 deletions zz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func TestLoadEmptyHostnameSkipped(t *testing.T) {
_ = Load(embeddedJSON)
}

func TestLoadDuplicateNodeID(t *testing.T) {
t.Parallel()
err := Load([]byte(`{"agents":[
{"hostname":"a","node_id":1},
{"hostname":"b","node_id":1}
]}`))
if err == nil {
t.Fatal("Load with duplicate node_id must return an error")
}
// Also verify the list wasn't corrupted by the failed load.
if name, ok := IsTrusted(1); ok {
t.Fatalf("IsTrusted(1)=%q after failed Load — list must not be updated", name)
}
_ = Load(embeddedJSON) // restore
}

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