-
Notifications
You must be signed in to change notification settings - Fork 0
504 lint overlap subnets #700
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
639186b
adjusting interface to new lint, writing skeleton of new lint
ShiriMoran e57d67a
begin impl check()
ShiriMoran 403c01b
implemented check() and string()
ShiriMoran 4abb266
adjusted string() fundtions to multiple prints
ShiriMoran 1dd1684
update existing tests with the new linter
ShiriMoran 3ce0570
wording improvements
ShiriMoran b45a727
added test
ShiriMoran a6dde3d
improved headers
ShiriMoran 1b1c014
lint
ShiriMoran 328be34
typo in doc fix
ShiriMoran 5f902b4
implement toJson
ShiriMoran 79fc879
Merge branch 'main' into 504_lint_overlap_subnets
ShiriMoran 9735760
Merge branch 'main' into 504_lint_overlap_subnets
ShiriMoran c00d7e3
Merge remote-tracking branch 'origin' into 504_lint_overlap_subnets
ShiriMoran 1a6ee35
Merge remote-tracking branch 'refs/remotes/origin/504_lint_overlap_su…
ShiriMoran 3c6fed4
subnets of the same VPC can not (or at least should not) overlap
ShiriMoran eaf273f
no longer needed
ShiriMoran d12d1f6
added a test in which the overlapping is in part of the range
ShiriMoran 2e0fc61
skeleton
ShiriMoran 71a9e0e
CR
ShiriMoran 7724c81
CR
ShiriMoran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5,682 changes: 5,682 additions & 0 deletions
5,682
pkg/ibmvpc/examples/input/input_tgw_larger_example_partly_overlap.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
"Firewall rules implying different connectivity for different endpoints within a subnet" issues: | ||
------------------------------------------------------------------------------------------------ | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
In VPC test-vpc1-ky, network ACL acl3-ky rule's indexed 1 splits subnet subnet3-ky (10.240.30.0/24). Splitting rule details: index: 1, direction: outbound , src: 10.240.30.0/31 , dst: 10.240.20.0/24, conn: all, action: allow | ||
In VPC test-vpc1-ky, network ACL acl3-ky rule's indexed 3 splits subnet subnet3-ky (10.240.30.0/24). Splitting rule details: index: 3, direction: inbound , src: 10.240.20.0/24 , dst: 10.240.30.0/31, conn: all, action: allow | ||
________________________________________________________________________________________________________________________________________________________________________________________________________ | ||
|
||
no lint "Overlapping CIDR ranges between different subnets" issues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
"Firewall rules implying different connectivity for different endpoints within a subnet" issues: | ||
------------------------------------------------------------------------------------------------ | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
In VPC test-vpc1-ky, security group sg1-ky rule's indexed 1 splits subnet subnet1-ky (10.240.10.0/24). Splitting rule details: index: 1, direction: inbound, conns: protocol: all, remote: sg1-ky (10.240.10.4/32), local: 0.0.0.0/0 | ||
In VPC test-vpc1-ky, security group sg1-ky rule's indexed 3 splits subnets subnet2-ky (10.240.20.0/24), subnet3-ky (10.240.30.0/24). Splitting rule details: index: 3, direction: inbound, conns: protocol: all, remote: sg2-ky (10.240.20.4/32,10.240.30.4/32), local: 0.0.0.0/0 | ||
In VPC test-vpc1-ky, security group sg1-ky rule's indexed 4 splits subnet subnet3-ky (10.240.30.0/24). Splitting rule details: index: 4, direction: inbound, conns: protocol: all, remote: sg3-ky (10.240.30.5/32,10.240.30.6/32), local: 0.0.0.0/0 | ||
In VPC test-vpc1-ky, security group sg2-ky rule's indexed 4 splits subnet subnet1-ky (10.240.10.0/24). Splitting rule details: index: 4, direction: inbound, conns: protocol: all, remote: sg1-ky (10.240.10.4/32), local: 0.0.0.0/0 | ||
In VPC test-vpc1-ky, security group sg2-ky rule's indexed 6 splits subnets subnet2-ky (10.240.20.0/24), subnet3-ky (10.240.30.0/24). Splitting rule details: index: 6, direction: outbound, conns: protocol: tcp, dstPorts: 1-65535, remote: sg2-ky (10.240.20.4/32,10.240.30.4/32), local: 0.0.0.0/0 | ||
In VPC test-vpc1-ky, security group sg2-ky rule's indexed 7 splits subnets subnet2-ky (10.240.20.0/24), subnet3-ky (10.240.30.0/24). Splitting rule details: index: 7, direction: inbound, conns: protocol: tcp, dstPorts: 1-65535, remote: sg2-ky (10.240.20.4/32,10.240.30.4/32), local: 0.0.0.0/0 | ||
________________________________________________________________________________________________________________________________________________________________________________________________________ | ||
|
||
no lint "Overlapping CIDR ranges between different subnets" issues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
"Overlapping CIDR ranges between different subnets" issues: | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
VPC test-vpc2-ky's subnet subnet21-ky of cidr 10.240.64.0/24 and VPC zn-vpc2's subnet zn-vpc2-net1 of cidr 10.240.64.0/24 overlap in the entire subnets' CIDR range | ||
________________________________________________________________________________________________________________________________________________________________________________________________________ | ||
|
||
no lint "Firewall rules implying different connectivity for different endpoints within a subnet" issues |
6 changes: 6 additions & 0 deletions
6
pkg/ibmvpc/examples/out/lint_out/multivpc_partly_overlap_Lint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"Overlapping CIDR ranges between different subnets" issues: | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
VPC test-vpc2-ky's subnet subnet21-ky of cidr 10.240.64.0/28 and VPC zn-vpc2's subnet zn-vpc2-net1 of cidr 10.240.64.0/24 overlap in 10.240.64.0/28 | ||
________________________________________________________________________________________________________________________________________________________________________________________________________ | ||
|
||
no lint "Firewall rules implying different connectivity for different endpoints within a subnet" issues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2023- IBM Inc. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package linter | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/np-guard/models/pkg/ipblock" | ||
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel" | ||
) | ||
|
||
const overlappingSubnetsName = "overlapping-subnets" | ||
|
||
// overlapSubnets: overlapping subnet ranges (relevant mostly for the multiple VPCs use case) | ||
type overlappingSubnetsLint struct { | ||
basicLinter | ||
} | ||
|
||
// a couple of overlapping subnets | ||
type overlapSubnets struct { | ||
overlapSubnets [2]vpcmodel.Subnet | ||
overlapIPBlocks *ipblock.IPBlock | ||
} | ||
|
||
// ///////////////////////////////////////////////////////// | ||
// lint interface implementation for overlapSubnets | ||
// //////////////////////////////////////////////////////// | ||
func (lint *overlappingSubnetsLint) lintName() string { | ||
return overlappingSubnetsName | ||
} | ||
|
||
func (lint *overlappingSubnetsLint) lintDescription() string { | ||
return "Overlapping CIDR ranges between different subnets" | ||
} | ||
|
||
func (lint *overlappingSubnetsLint) check() error { | ||
allSubnets := []vpcmodel.Subnet{} | ||
for _, config := range lint.configs { | ||
if config.IsMultipleVPCsConfig { | ||
continue | ||
} | ||
allSubnets = append(allSubnets, config.Subnets...) | ||
} | ||
for i, subnet1 := range allSubnets { | ||
subnet1IPBlock, err1 := ipblock.FromCidr(subnet1.CIDR()) | ||
if err1 != nil { | ||
return err1 | ||
} | ||
for _, subnet2 := range allSubnets[i+1:] { | ||
subnet2IPBlock, err2 := ipblock.FromCidr(subnet2.CIDR()) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
intersectIPBlock := subnet1IPBlock.Intersect(subnet2IPBlock) | ||
if !intersectIPBlock.IsEmpty() { | ||
// to make the content of the overlapSubnets struct deterministic | ||
if subnetStr(subnet1) > subnetStr(subnet2) { | ||
adisos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
subnet1, subnet2 = subnet2, subnet1 | ||
} | ||
lint.addFinding(&overlapSubnets{overlapSubnets: [2]vpcmodel.Subnet{subnet1, subnet2}, overlapIPBlocks: intersectIPBlock}) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
/////////////////////////////////////////////////////////// | ||
// finding interface implementation for overlapSubnets | ||
////////////////////////////////////////////////////////// | ||
|
||
func (finding *overlapSubnets) vpc() []string { | ||
return []string{finding.overlapSubnets[0].VPC().Name(), finding.overlapSubnets[1].VPC().Name()} | ||
} | ||
|
||
func (finding *overlapSubnets) string() string { | ||
subnet1 := finding.overlapSubnets[0] | ||
subnet2 := finding.overlapSubnets[1] | ||
overlapStr := "" | ||
if finding.overlapIPBlocks.String() == subnet1.CIDR() && subnet1.CIDR() == subnet2.CIDR() { | ||
overlapStr = " overlap in the entire subnets' CIDR range" | ||
} else { | ||
overlapStr = fmt.Sprintf(" overlap in %s", finding.overlapIPBlocks.String()) | ||
} | ||
adisos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return fmt.Sprintf("VPC %s's %s and VPC %s's %s ", subnet1.VPC().Name(), subnetStr(subnet1), | ||
subnet2.VPC().Name(), subnetStr(subnet2)) + overlapStr | ||
} | ||
|
||
func subnetStr(subnet vpcmodel.Subnet) string { | ||
return fmt.Sprintf("subnet %s of cidr %s", subnet.Name(), subnet.CIDR()) | ||
} | ||
|
||
// for json: details of overlapping subnets | ||
type overlapSubnetsJSON struct { | ||
OverlapSubnets []subnetJSON `json:"couple_overlap_subnets"` | ||
OverlapCidr string `json:"overlap_cidr"` | ||
} | ||
|
||
type subnetJSON struct { | ||
Name string `json:"name"` | ||
CIDR string `json:"cidr"` | ||
VpcName string `json:"vpc_name,omitempty"` | ||
} | ||
|
||
func (finding *overlapSubnets) toJSON() any { | ||
overlapsSubnetsJSON := make([]subnetJSON, 2) | ||
for i, overlapSubnet := range finding.overlapSubnets { | ||
overlapsSubnetsJSON[i] = subnetJSON{Name: overlapSubnet.Name(), VpcName: overlapSubnet.VPC().Name(), CIDR: overlapSubnet.CIDR()} | ||
} | ||
res := overlapSubnetsJSON{OverlapSubnets: overlapsSubnetsJSON, OverlapCidr: finding.overlapIPBlocks.String()} | ||
return res | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.