@@ -12,13 +12,20 @@ import (
1212type 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
2021func 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 ...
4552func (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 {
0 commit comments