Skip to content

Commit bd7da4b

Browse files
author
Marina Limeira
committed
Add more test cases
1 parent f7f16fe commit bd7da4b

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

rules/aws_security_group_rule_invalid_cidr_block.go

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ import (
1212
type AwsSecurityGroupRuleInvalidCidrBlockRule struct {
1313
tflint.DefaultRule
1414

15-
resourceType string
16-
cidrBlocks []string
15+
resourceType string
16+
cidrBlocks []string
17+
remoteAccessPorts []int
1718
}
1819

1920
// NewAwsSecurityGroupRuleInvalidCidrBlockRule returns a new rule
2021
func NewAwsSecurityGroupRuleInvalidCidrBlockRule() *AwsSecurityGroupRuleInvalidCidrBlockRule {
21-
return &AwsSecurityGroupRuleInvalidCidrBlockRule{}
22+
return &AwsSecurityGroupRuleInvalidCidrBlockRule{
23+
resourceType: "aws_security_group_rule",
24+
remoteAccessPorts: []int{22, 3389},
25+
26+
// todo extrair os resource types daqui?
27+
28+
}
2229
}
2330

2431
// Name returns the rule name
@@ -43,7 +50,7 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Link() string {
4350

4451
// Check checks whether ...
4552
func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) error {
46-
resources, err := runner.GetResourceContent("aws_security_group_rule", &hclext.BodySchema{
53+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
4754
Attributes: []hclext.AttributeSchema{
4855
// Required attributes
4956
{Name: "from_port"},
@@ -97,35 +104,38 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
97104
logger.Debug(fmt.Sprintf("Security Group Rule port range is %d to %d", fromPort, toPort))
98105

99106
// No need to check the CIDR blocks if the ports range does not contain 22 or 3389.
100-
if !doesPortRangeContainsRemoteAccess(fromPort, toPort) {
107+
if !doesPortRangeContainsPorts(fromPort, toPort, r.remoteAccessPorts) {
101108
continue
102109
}
103110

104111
cidrBlocksAttribute, exists := resource.Body.Attributes["cidr_blocks"]
105-
if exists {
106-
var cidrBlocks []string
107-
err = runner.EvaluateExpr(cidrBlocksAttribute.Expr, &cidrBlocks, nil)
108-
if doesIpv4CidrBlocksAllowAll(cidrBlocks) {
109-
return runner.EmitIssue(
110-
r,
111-
"cidr_blocks can not contain '0.0.0.0/0' when allowing 'ingress' access to ports 22 and/or 3389",
112-
cidrBlocksAttribute.Expr.Range(),
113-
)
114-
}
112+
if !exists {
113+
continue
114+
}
115115

116+
var cidrBlocks []string
117+
err = runner.EvaluateExpr(cidrBlocksAttribute.Expr, &cidrBlocks, nil)
118+
if doesIpv4CidrBlocksAllowAll(cidrBlocks) {
119+
return runner.EmitIssue(
120+
r,
121+
fmt.Sprintf("cidr_blocks can not contain '0.0.0.0/0' when allowing 'ingress' access to ports %v", r.remoteAccessPorts),
122+
cidrBlocksAttribute.Expr.Range(),
123+
)
116124
}
117125

118126
ipv6CidrBlocksAttribute, exists := resource.Body.Attributes["ipv6_cidr_blocks"]
119-
if exists {
120-
var ipv6CidrBlocks []string
121-
err = runner.EvaluateExpr(ipv6CidrBlocksAttribute.Expr, &ipv6CidrBlocks, nil)
122-
if doesIpv6CidrBlocksAllowAll(ipv6CidrBlocks) {
123-
return runner.EmitIssue(
124-
r,
125-
"ipv6_cidr_blocks can not contain '::/0' when allowing 'ingress' access to ports 22 and/or 3389",
126-
ipv6CidrBlocksAttribute.Expr.Range(),
127-
)
128-
}
127+
if !exists {
128+
continue
129+
}
130+
131+
var ipv6CidrBlocks []string
132+
err = runner.EvaluateExpr(ipv6CidrBlocksAttribute.Expr, &ipv6CidrBlocks, nil)
133+
if doesIpv6CidrBlocksAllowAll(ipv6CidrBlocks) {
134+
return runner.EmitIssue(
135+
r,
136+
fmt.Sprintf("ipv6_cidr_blocks can not contain '::/0' when allowing 'ingress' access to ports %v", r.remoteAccessPorts),
137+
ipv6CidrBlocksAttribute.Expr.Range(),
138+
)
129139
}
130140
if err != nil {
131141
return err
@@ -135,11 +145,8 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
135145
return nil
136146
}
137147

138-
func doesPortRangeContainsRemoteAccess(fromPort int, toPort int) bool {
139-
// TODO add case for 0, 0
140-
remoteAccessPorts := []int{22, 3389}
141-
142-
for _, port := range remoteAccessPorts {
148+
func doesPortRangeContainsPorts(fromPort int, toPort int, ports []int) bool {
149+
for _, port := range ports {
143150
isIncludedInRange := fromPort <= port && port <= toPort
144151
logger.Debug(fmt.Sprintf("%v for %d isIncludedInRange", isIncludedInRange, port))
145152
if isIncludedInRange {

rules/aws_security_group_rule_invalid_cidr_block_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func Test_AwsSecurityGroupRuleInvalidCidrBlock(t *testing.T) {
1414
Expected helper.Issues
1515
}{
1616
{
17-
Name: "Find issues 0.0.0.0/0 allows ingress access through 22",
17+
Name: "Find issues when 0.0.0.0/0 allows ingress access through 22",
1818
Content: `
1919
resource "aws_security_group_rule" "rule" {
2020
from_port = 10
@@ -26,7 +26,7 @@ resource "aws_security_group_rule" "rule" {
2626
Expected: helper.Issues{
2727
{
2828
Rule: NewAwsSecurityGroupRuleInvalidCidrBlockRule(),
29-
Message: "cidr_blocks can not contain '0.0.0.0/0' when allowing 'ingress' access to ports 22 and/or 3389",
29+
Message: "cidr_blocks can not contain '0.0.0.0/0' when allowing 'ingress' access to ports [22 3389]",
3030
Range: hcl.Range{
3131
Filename: "resource.tf",
3232
Start: hcl.Pos{Line: 7, Column: 23},
@@ -35,6 +35,28 @@ resource "aws_security_group_rule" "rule" {
3535
},
3636
},
3737
},
38+
{
39+
Name: "Find issues when ::/0 allows ingress access through 22",
40+
Content: `
41+
resource "aws_security_group_rule" "rule" {
42+
from_port = 10
43+
to_port = 300
44+
protocol = "tcp"
45+
type = "ingress"
46+
ipv6_cidr_blocks = ["::/0"]
47+
}`,
48+
Expected: helper.Issues{
49+
{
50+
Rule: NewAwsSecurityGroupRuleInvalidCidrBlockRule(),
51+
Message: "ipv6_cidr_blocks can not contain '::/0' when allowing 'ingress' access to ports [22 3389]",
52+
Range: hcl.Range{
53+
Filename: "resource.tf",
54+
Start: hcl.Pos{Line: 7, Column: 23},
55+
End: hcl.Pos{Line: 7, Column: 31},
56+
},
57+
},
58+
},
59+
},
3860
{
3961
Name: "Find no issues 0.0.0.0/0 allows ingress access through 80",
4062
Content: `
@@ -43,7 +65,7 @@ resource "aws_security_group_rule" "rule" {
4365
to_port = 80
4466
protocol = "tcp"
4567
type = "ingress"
46-
cidr_blocks = ["0.0.0.0/0", "10.0.0.0/16"]
68+
cidr_blocks = ["0.0.0.0/0"]
4769
}`,
4870
Expected: helper.Issues{},
4971
},

0 commit comments

Comments
 (0)