-
Notifications
You must be signed in to change notification settings - Fork 61
OSAC-365: canonicalize CIDR values on VirtualNetwork and Subnet Create #687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2080022
04c5f80
b64bf27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| Copyright (c) 2026 Red Hat Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
| language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package servers | ||
|
|
||
| import ( | ||
| "net/netip" | ||
|
|
||
| grpccodes "google.golang.org/grpc/codes" | ||
| grpcstatus "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| const ( | ||
| cidrIPv4 = "IPv4" | ||
| cidrIPv6 = "IPv6" | ||
| ) | ||
|
|
||
| // parseAndValidateCIDR parses cidrStr, validates it matches the expected IP version, | ||
| // and returns the canonical CIDR notation. | ||
| func parseAndValidateCIDR(cidrStr string, ipVersion string) (string, error) { | ||
| prefix, err := netip.ParsePrefix(cidrStr) | ||
| if err != nil { | ||
| return "", grpcstatus.Errorf(grpccodes.InvalidArgument, | ||
| "invalid %s CIDR format '%s': %v", ipVersion, cidrStr, err) | ||
| } | ||
|
|
||
| isIPv4 := prefix.Addr().Is4() | ||
| if ipVersion == cidrIPv4 && !isIPv4 { | ||
| return "", grpcstatus.Errorf(grpccodes.InvalidArgument, | ||
| "field 'ipv4_cidr' contains IPv6 address: %s", cidrStr) | ||
| } | ||
| if ipVersion == cidrIPv6 && isIPv4 { | ||
| return "", grpcstatus.Errorf(grpccodes.InvalidArgument, | ||
| "field 'ipv6_cidr' contains IPv4 address: %s", cidrStr) | ||
| } | ||
|
|
||
| return prefix.Masked().String(), nil | ||
| } | ||
|
|
||
| // validateCIDR validates a CIDR string and checks if it matches the expected IP version. | ||
| func validateCIDR(cidrStr string, ipVersion string) error { | ||
| _, err := parseAndValidateCIDR(cidrStr, ipVersion) | ||
| return err | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,16 +228,26 @@ func (s *PrivateSubnetsServer) validateSubnet(ctx context.Context, | |
|
|
||
| // SUB-VAL-01: Validate IPv4 CIDR format | ||
| if spec.GetIpv4Cidr() != "" { | ||
| if err := validateCIDR(spec.GetIpv4Cidr(), "IPv4"); err != nil { | ||
| canonical, err := parseAndValidateCIDR(spec.GetIpv4Cidr(), cidrIPv4) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Canonicalize on Create only; Update must not rewrite immutable CIDR fields (SUB-VAL-11+). | ||
| if existingSubnet == nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is done to facilitate further validation. But, it's not clear why and why it depends on existingSubnet being
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comments on both paths.
We can do Update canonicalization, but it’s a product decision about migrating existing data... we’d want agreed Update/migration rules, tests for legacy rows, and a quick check that anything downstream wouldn’t break if the stored CIDR text changed but the network didn’t. |
||
| spec.SetIpv4Cidr(canonical) | ||
| } | ||
| } | ||
|
|
||
| // SUB-VAL-02: Validate IPv6 CIDR format | ||
| if spec.GetIpv6Cidr() != "" { | ||
| if err := validateCIDR(spec.GetIpv6Cidr(), "IPv6"); err != nil { | ||
| canonical, err := parseAndValidateCIDR(spec.GetIpv6Cidr(), cidrIPv6) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Canonicalize on Create only; Update must not rewrite immutable CIDR fields (SUB-VAL-11+). | ||
| if existingSubnet == nil { | ||
| spec.SetIpv6Cidr(canonical) | ||
| } | ||
| } | ||
|
|
||
| // SUB-VAL-04, SUB-VAL-05, SUB-VAL-06, SUB-VAL-07, SUB-VAL-08: Validate parent VirtualNetwork | ||
|
|
@@ -331,7 +341,7 @@ func (s *PrivateSubnetsServer) validateVirtualNetworkReference(ctx context.Conte | |
| "subnet has IPv4 CIDR but parent VirtualNetwork does not support IPv4") | ||
| } | ||
| // SUB-VAL-06: Validate IPv4 CIDR is subset of parent | ||
| if err := validateCIDRSubset(spec.GetIpv4Cidr(), parentSpec.GetIpv4Cidr(), "IPv4"); err != nil { | ||
| if err := validateCIDRSubset(spec.GetIpv4Cidr(), parentSpec.GetIpv4Cidr(), cidrIPv4); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
@@ -343,7 +353,7 @@ func (s *PrivateSubnetsServer) validateVirtualNetworkReference(ctx context.Conte | |
| "subnet has IPv6 CIDR but parent VirtualNetwork does not support IPv6") | ||
| } | ||
| // SUB-VAL-06: Validate IPv6 CIDR is subset of parent | ||
| if err := validateCIDRSubset(spec.GetIpv6Cidr(), parentSpec.GetIpv6Cidr(), "IPv6"); err != nil { | ||
| if err := validateCIDRSubset(spec.GetIpv6Cidr(), parentSpec.GetIpv6Cidr(), cidrIPv6); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method used anywhere? I couldn't find it in this PR, but maybe somewhere else
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes... still used by SG rule validation:
fulfillment-service/internal/servers/private_security_groups_server.go
Lines 344 to 354 in 8a26dc2
VN/Subnet use
parseAndValidateCIDRto canonicalize on Create; SG keepsvalidateCIDR(validate only) per OSAC-365 scope:fulfillment-service/internal/servers/cidr_validation.go
Lines 45 to 49 in 8a26dc2
If you want SG (or
PublicIPPoolspec.cidrs[]) canonicalized here too, say the word... otherwise I’d track those as follow-ups.