@@ -43,14 +43,15 @@ func (r *AwsSecurityGroupRuleInvalidCidrBlockRule) Link() string {
4343
4444// Check checks whether ...
4545func (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
146138func 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}
0 commit comments