From dc5a8413f65d87f296ec45485430f3ba077ba45e Mon Sep 17 00:00:00 2001 From: nikolayzakharov Date: Mon, 31 Aug 2026 07:13:33 +0200 Subject: [PATCH] fix: normalize CIDRs and keep SSH rules without an ip range The bastion reconcile compared allowed CIDRs as raw strings, so a prefix with host bits set never matched the stored rule. The revoke loop also deleted each SSH rule that has no ip range. Signed-off-by: Jan Larwig --- .../services/securitygroup/reconcile_test.go | 173 ++++++++++++++++++ cloud/services/securitygroup/rules.go | 9 +- 2 files changed, 180 insertions(+), 2 deletions(-) diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 3a01ee7..f84b194 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -920,3 +920,176 @@ func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { t.Fatalf("deletedSGIDs = %v, want [%s %s] (reverse order: node-ssh then bastion-ssh)", deletedSGIDs, testSecurityGroupID, testRemoteSGID) } } + +// TestServiceReconcileToleratesNonCanonicalCIDR guards against CIDRs +// given or returned in a non-canonical form (e.g. host bits set or +// uncompressed IPv6) failing to match, deduplicate, or normalize on create. +func TestServiceReconcileToleratesNonCanonicalCIDR(t *testing.T) { + const ( + existingRuleID = "88888888-8888-4888-8888-888888888888" + canonicalCIDR = "203.0.113.0/24" + ) + + newHandler := func( + t *testing.T, + existingRules []any, + createdRules *[]map[string]any, + deletedRuleIDs *[]string, + ) http.Handler { + t.Helper() + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testSecurityGroupID, + "name": "bastion-ssh", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/rules"): + writeJSON(t, w, map[string]any{"items": existingRules}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/rules"): + *createdRules = append(*createdRules, readJSON(t, r)) + writeJSON(t, w, map[string]any{"id": testRuleID2, "direction": "ingress"}) + case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): + parts := strings.Split(strings.TrimSuffix(path, "/"), "/") + *deletedRuleIDs = append(*deletedRuleIDs, parts[len(parts)-1]) + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + }) + } + + t.Run("matches canonical rule from API when desired CIDR has host bits set", func(t *testing.T) { + var ( + createdRules []map[string]any + deletedRuleIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{{ + Name: "bastion-ssh", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "203.0.113.5/24", + }}, + }} + existing := []any{ + map[string]any{ + "id": existingRuleID, + "direction": "ingress", + "ipRange": canonicalCIDR, + "portRange": map[string]any{"min": 22, "max": 22}, + "protocol": map[string]any{"name": "tcp"}, + }, + } + + svc := newTestService(t, specs, newHandler(t, existing, &createdRules, &deletedRuleIDs)) + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if len(createdRules) != 0 { + t.Fatalf("created rules = %v, want none", createdRules) + } + if len(deletedRuleIDs) != 0 { + t.Fatalf("deleted rules = %v, want none", deletedRuleIDs) + } + }) + + t.Run("matches non-canonical IPv4 and IPv6 rules returned by API", func(t *testing.T) { + var ( + createdRules []map[string]any + deletedRuleIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{{ + Name: "bastion-ssh", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: canonicalCIDR, + }, + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "2001:db8::/64", + }, + }, + }} + existing := []any{ + map[string]any{ + "id": existingRuleID, + "direction": "ingress", + "ipRange": "203.0.113.5/24", + "portRange": map[string]any{"min": 22, "max": 22}, + "protocol": map[string]any{"name": "tcp"}, + }, + map[string]any{ + "id": testRuleID1, + "direction": "ingress", + "ipRange": "2001:0db8:0000:0000::1/64", + "portRange": map[string]any{"min": 22, "max": 22}, + "protocol": map[string]any{"name": "tcp"}, + }, + } + + svc := newTestService(t, specs, newHandler(t, existing, &createdRules, &deletedRuleIDs)) + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if len(createdRules) != 0 { + t.Fatalf("created rules = %v, want none", createdRules) + } + if len(deletedRuleIDs) != 0 { + t.Fatalf("deleted rules = %v, want none", deletedRuleIDs) + } + }) + + t.Run("creates canonical CIDR and deduplicates equivalent non-canonical CIDRs", func(t *testing.T) { + var ( + createdRules []map[string]any + deletedRuleIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{{ + Name: "bastion-ssh", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "203.0.113.5/24", + }, + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "203.0.113.9/24", + }, + }, + }} + + svc := newTestService(t, specs, newHandler(t, []any{}, &createdRules, &deletedRuleIDs)) + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if len(createdRules) != 1 || createdRules[0]["ipRange"] != canonicalCIDR { + t.Fatalf("created rules = %#v, want single rule for %s", createdRules, canonicalCIDR) + } + if len(deletedRuleIDs) != 0 { + t.Fatalf("deleted rules = %v, want none", deletedRuleIDs) + } + }) +} diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 73514f4..48a86ca 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -157,6 +157,11 @@ func (p Protocol) normalize() Protocol { func (r Rule) normalize() Rule { r.Protocol = r.Protocol.normalize() + if r.IPRange != "" { + if prefix, err := netip.ParsePrefix(r.IPRange); err == nil { + r.IPRange = prefix.Masked().String() + } + } return r } @@ -196,7 +201,7 @@ func hasRule(rules []iaas.SecurityGroupRule, desired Rule) bool { func ruleFromSDK(in iaas.SecurityGroupRule) Rule { var ruleProtocol Protocol if protocol, ok := in.GetProtocolOk(); ok { - ruleProtocol = Protocol(protocol.GetName()).normalize() + ruleProtocol = Protocol(protocol.GetName()) } var portMin, portMax int64 if portRange, ok := in.GetPortRangeOk(); ok { @@ -210,5 +215,5 @@ func ruleFromSDK(in iaas.SecurityGroupRule) Rule { PortMax: portMax, IPRange: in.GetIpRange(), RemoteSecurityGroupID: in.GetRemoteSecurityGroupId(), - } + }.normalize() }