Fix discovery blocklist for configured subnets (#1616)

This commit is contained in:
courtmanr@gmail.com 2026-07-30 00:18:48 +01:00
parent 41978482e7
commit 94c908419b
2 changed files with 43 additions and 4 deletions

View file

@ -677,6 +677,7 @@ func (s *Scanner) resolveProfile(subnet string) (*envdetect.EnvironmentProfile,
Type: envdetect.Unknown,
Confidence: 1.0,
Policy: s.policy,
IPBlocklist: append([]net.IP(nil), s.profile.IPBlocklist...),
Warnings: []string{"Manual subnet override applied"},
Metadata: map[string]string{
"manual_subnets": strings.Join(renderedSubnets, ","),
@ -827,6 +828,9 @@ func cloneProfile(profile *envdetect.EnvironmentProfile) *envdetect.EnvironmentP
if profile.ExtraTargets != nil {
cloned.ExtraTargets = append([]net.IP(nil), profile.ExtraTargets...)
}
if profile.IPBlocklist != nil {
cloned.IPBlocklist = append([]net.IP(nil), profile.IPBlocklist...)
}
if profile.Phases != nil {
cloned.Phases = make([]envdetect.SubnetPhase, len(profile.Phases))
for i, phase := range profile.Phases {

View file

@ -1160,6 +1160,23 @@ func TestCloneProfile(t *testing.T) {
}
})
t.Run("IP blocklist is deep copied", func(t *testing.T) {
input := &envdetect.EnvironmentProfile{
Type: envdetect.Native,
IPBlocklist: []net.IP{net.ParseIP("192.168.1.10")},
}
result := cloneProfile(input)
if len(result.IPBlocklist) != 1 || result.IPBlocklist[0].String() != "192.168.1.10" {
t.Fatalf("IPBlocklist = %v, want [192.168.1.10]", result.IPBlocklist)
}
result.IPBlocklist[0] = net.ParseIP("10.0.0.10")
if input.IPBlocklist[0].String() != "192.168.1.10" {
t.Errorf("original IPBlocklist was modified: got %v", input.IPBlocklist[0])
}
})
t.Run("phases are deep copied", func(t *testing.T) {
_, subnet, _ := net.ParseCIDR("172.17.0.0/16")
input := &envdetect.EnvironmentProfile{
@ -1216,3 +1233,21 @@ func TestCloneProfile(t *testing.T) {
}
})
}
func TestResolveProfileManualSubnetPreservesIPBlocklist(t *testing.T) {
t.Parallel()
scanner := NewScannerWithProfile(&envdetect.EnvironmentProfile{
Type: envdetect.Native,
Policy: envdetect.DefaultScanPolicy(),
IPBlocklist: []net.IP{net.ParseIP("10.0.1.10")},
})
profile, err := scanner.resolveProfile("10.0.1.0/24")
if err != nil {
t.Fatalf("resolveProfile returned error: %v", err)
}
if len(profile.IPBlocklist) != 1 || profile.IPBlocklist[0].String() != "10.0.1.10" {
t.Fatalf("manual profile IPBlocklist = %v, want [10.0.1.10]", profile.IPBlocklist)
}
}