Skip to content

Commit f7f16fe

Browse files
author
Marina Limeira
committed
Rules now working
1 parent 03e5cab commit f7f16fe

File tree

3 files changed

+91
-88
lines changed

3 files changed

+91
-88
lines changed

rules/aws_instance_example_type_test.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

rules/aws_security_group_rule_invalid_cidr_block.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Link() string {
4343

4444
// Check checks whether ...
4545
func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) error {
46-
// This rule is an example to get a top-level resource attribute.
4746
resources, err := runner.GetResourceContent("aws_security_group_rule", &hclext.BodySchema{
4847
Attributes: []hclext.AttributeSchema{
49-
{Name: "cidr_blocks"},
50-
{Name: "ipv6_cidr_blocks"},
48+
// Required attributes
5149
{Name: "from_port"},
5250
{Name: "to_port"},
5351
{Name: "type"},
52+
// Optional attributes
53+
{Name: "cidr_blocks"},
54+
{Name: "ipv6_cidr_blocks"},
5455
},
5556
}, nil)
5657
if err != nil {
@@ -62,6 +63,7 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
6263

6364
for _, resource := range resources.Blocks {
6465
typeAttribute, exists := resource.Body.Attributes["type"]
66+
// well this cant not exist
6567
if !exists {
6668
continue
6769
}
@@ -75,23 +77,20 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
7577
continue
7678
}
7779

80+
// A Port value of ALL
7881
fromPortAttribute, exists := resource.Body.Attributes["from_port"]
7982
if !exists {
8083
continue
8184
}
8285

86+
var fromPort int
87+
err = runner.EvaluateExpr(fromPortAttribute.Expr, &fromPort, nil)
88+
8389
toPortAttribute, exists := resource.Body.Attributes["to_port"]
8490
if !exists {
8591
continue
8692
}
8793

88-
// TODO what means that each field doesnt exist?
89-
// Case 1:
90-
// From 0, to 0.
91-
92-
var fromPort int
93-
err = runner.EvaluateExpr(fromPortAttribute.Expr, &fromPort, nil)
94-
9594
var toPort int
9695
err = runner.EvaluateExpr(toPortAttribute.Expr, &toPort, nil)
9796

@@ -102,39 +101,32 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
102101
continue
103102
}
104103

105-
ipv6CidrBlocksAttribute, exists := resource.Body.Attributes["ipv6_cidr_blocks"]
104+
cidrBlocksAttribute, exists := resource.Body.Attributes["cidr_blocks"]
106105
if exists {
107-
var ipv6CidrBlocks []string
108-
err = runner.EvaluateExpr(ipv6CidrBlocksAttribute.Expr, &ipv6CidrBlocks, nil)
109-
110-
//err = runner.EnsureNoError(err, func() error {
111-
if doesCidrBlocksContainAll(ipv6CidrBlocks) {
106+
var cidrBlocks []string
107+
err = runner.EvaluateExpr(cidrBlocksAttribute.Expr, &cidrBlocks, nil)
108+
if doesIpv4CidrBlocksAllowAll(cidrBlocks) {
112109
return runner.EmitIssue(
113110
r,
114-
fmt.Sprintf("cidr_blocks are %v", ipv6CidrBlocks),
115-
ipv6CidrBlocksAttribute.Expr.Range(),
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(),
116113
)
117-
} //)
114+
}
118115

119116
}
120117

121-
cidrBlocksAttribute, exists := resource.Body.Attributes["ipv6_cidr_blocks"]
118+
ipv6CidrBlocksAttribute, exists := resource.Body.Attributes["ipv6_cidr_blocks"]
122119
if exists {
123-
var cidrBlocks []string
124-
err = runner.EvaluateExpr(cidrBlocksAttribute.Expr, &cidrBlocks, nil)
125-
126-
//err = runner.EnsureNoError(err, func() error {
127-
if doesCidrBlocksContainAll(cidrBlocks) {
128-
120+
var ipv6CidrBlocks []string
121+
err = runner.EvaluateExpr(ipv6CidrBlocksAttribute.Expr, &ipv6CidrBlocks, nil)
122+
if doesIpv6CidrBlocksAllowAll(ipv6CidrBlocks) {
129123
return runner.EmitIssue(
130124
r,
131-
fmt.Sprintf("cidr_blocks are %v", cidrBlocks),
132-
cidrBlocksAttribute.Expr.Range(),
125+
"ipv6_cidr_blocks can not contain '::/0' when allowing 'ingress' access to ports 22 and/or 3389",
126+
ipv6CidrBlocksAttribute.Expr.Range(),
133127
)
134128
}
135-
//})
136129
}
137-
138130
if err != nil {
139131
return err
140132
}
@@ -144,6 +136,7 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Check(runner tflint.Runner) e
144136
}
145137

146138
func doesPortRangeContainsRemoteAccess(fromPort int, toPort int) bool {
139+
// TODO add case for 0, 0
147140
remoteAccessPorts := []int{22, 3389}
148141

149142
for _, port := range remoteAccessPorts {
@@ -157,7 +150,20 @@ func doesPortRangeContainsRemoteAccess(fromPort int, toPort int) bool {
157150
return false
158151
}
159152

160-
func doesCidrBlocksContainAll(cidrBlocks []string) bool {
161-
return true
153+
func doesIpv4CidrBlocksAllowAll(cidrBlocks []string) bool {
154+
for _, cidrBlock := range cidrBlocks {
155+
if cidrBlock == "0.0.0.0/0" {
156+
return true
157+
}
158+
}
159+
return false
160+
}
162161

162+
func doesIpv6CidrBlocksAllowAll(ipv6CidrBlocks []string) bool {
163+
for _, cidrBlock := range ipv6CidrBlocks {
164+
if cidrBlock == "::/0" {
165+
return true
166+
}
167+
}
168+
return false
163169
}
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,65 @@
11
package rules
22

33
import (
4-
"github.com/terraform-linters/tflint-plugin-sdk/helper"
54
"testing"
6-
)
75

8-
func TestDoesPortRangeContainsRemoteAccess(t *testing.T) {
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
99

10-
_ := []struct {
10+
func Test_AwsSecurityGroupRuleInvalidCidrBlock(t *testing.T) {
11+
tests := []struct {
1112
Name string
1213
Content string
1314
Expected helper.Issues
1415
}{
1516
{
16-
Name: "issue found",
17-
Content: ""}}
17+
Name: "Find issues 0.0.0.0/0 allows ingress access through 22",
18+
Content: `
19+
resource "aws_security_group_rule" "rule" {
20+
from_port = 10
21+
to_port = 300
22+
protocol = "tcp"
23+
type = "ingress"
24+
cidr_blocks = ["0.0.0.0/0", "10.0.0.0/16"]
25+
}`,
26+
Expected: helper.Issues{
27+
{
28+
Rule: NewAwsSecurityGroupRuleInvalidCidrBlockRule(),
29+
Message: "cidr_blocks can not contain '0.0.0.0/0' when allowing 'ingress' access to ports 22 and/or 3389",
30+
Range: hcl.Range{
31+
Filename: "resource.tf",
32+
Start: hcl.Pos{Line: 7, Column: 23},
33+
End: hcl.Pos{Line: 7, Column: 51},
34+
},
35+
},
36+
},
37+
},
38+
{
39+
Name: "Find no issues 0.0.0.0/0 allows ingress access through 80",
40+
Content: `
41+
resource "aws_security_group_rule" "rule" {
42+
from_port = 80
43+
to_port = 80
44+
protocol = "tcp"
45+
type = "ingress"
46+
cidr_blocks = ["0.0.0.0/0", "10.0.0.0/16"]
47+
}`,
48+
Expected: helper.Issues{},
49+
},
50+
}
51+
52+
rule := NewAwsSecurityGroupRuleInvalidCidrBlockRule()
53+
54+
for _, test := range tests {
55+
t.Run(test.Name, func(t *testing.T) {
56+
runner := helper.TestRunner(t, map[string]string{"resource.tf": test.Content})
57+
58+
if err := rule.Check(runner); err != nil {
59+
t.Fatalf("Unexpected error occurred: %s", err)
60+
}
61+
62+
helper.AssertIssues(t, test.Expected, runner.Issues)
63+
})
64+
}
1865
}

0 commit comments

Comments
 (0)