Skip to content

Commit 49fca92

Browse files
authored
Merge pull request #29 from wata727/aws_elasticache_cluster_invalid_security_group
add invalid security group detector for ElastiCache
2 parents d47313c + b73af9a commit 49fca92

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package detector
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/service/ec2"
7+
"github.com/hashicorp/hcl/hcl/token"
8+
"github.com/wata727/tflint/issue"
9+
)
10+
11+
type AwsElastiCacheClusterInvalidSecurityGroupDetector struct {
12+
*Detector
13+
}
14+
15+
func (d *Detector) CreateAwsElastiCacheClusterInvalidSecurityGroupDetector() *AwsElastiCacheClusterInvalidSecurityGroupDetector {
16+
return &AwsElastiCacheClusterInvalidSecurityGroupDetector{d}
17+
}
18+
19+
func (d *AwsElastiCacheClusterInvalidSecurityGroupDetector) Detect(issues *[]*issue.Issue) {
20+
if !d.isDeepCheck("resource", "aws_elasticache_cluster") {
21+
return
22+
}
23+
24+
validSecurityGroups := map[string]bool{}
25+
if d.ResponseCache.DescribeSecurityGroupsOutput == nil {
26+
resp, err := d.AwsClient.Ec2.DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{})
27+
if err != nil {
28+
d.Logger.Error(err)
29+
d.Error = true
30+
}
31+
d.ResponseCache.DescribeSecurityGroupsOutput = resp
32+
}
33+
for _, securityGroup := range d.ResponseCache.DescribeSecurityGroupsOutput.SecurityGroups {
34+
validSecurityGroups[*securityGroup.GroupId] = true
35+
}
36+
37+
for filename, list := range d.ListMap {
38+
for _, item := range list.Filter("resource", "aws_elasticache_cluster").Items {
39+
var varToken token.Token
40+
var securityGroupTokens []token.Token
41+
var err error
42+
if varToken, err = hclLiteralToken(item, "security_group_ids"); err == nil {
43+
securityGroupTokens, err = d.evalToStringTokens(varToken)
44+
if err != nil {
45+
d.Logger.Error(err)
46+
continue
47+
}
48+
} else {
49+
d.Logger.Error(err)
50+
securityGroupTokens, err = hclLiteralListToken(item, "security_group_ids")
51+
if err != nil {
52+
d.Logger.Error(err)
53+
continue
54+
}
55+
}
56+
57+
for _, securityGroupToken := range securityGroupTokens {
58+
securityGroup, err := d.evalToString(securityGroupToken.Text)
59+
if err != nil {
60+
d.Logger.Error(err)
61+
continue
62+
}
63+
64+
if !validSecurityGroups[securityGroup] {
65+
issue := &issue.Issue{
66+
Type: "ERROR",
67+
Message: fmt.Sprintf("\"%s\" is invalid security group.", securityGroup),
68+
Line: securityGroupToken.Pos.Line,
69+
File: filename,
70+
}
71+
*issues = append(*issues, issue)
72+
}
73+
}
74+
}
75+
}
76+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package detector
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/service/ec2"
9+
"github.com/golang/mock/gomock"
10+
"github.com/wata727/tflint/awsmock"
11+
"github.com/wata727/tflint/config"
12+
"github.com/wata727/tflint/issue"
13+
)
14+
15+
func TestDetectAwsElastiCacheClusterInvalidSecurityGroup(t *testing.T) {
16+
cases := []struct {
17+
Name string
18+
Src string
19+
Response []*ec2.SecurityGroup
20+
Issues []*issue.Issue
21+
}{
22+
{
23+
Name: "security group is invalid",
24+
Src: `
25+
resource "aws_elasticache_cluster" "redis" {
26+
security_group_ids = [
27+
"sg-1234abcd",
28+
"sg-abcd1234",
29+
]
30+
}`,
31+
Response: []*ec2.SecurityGroup{
32+
&ec2.SecurityGroup{
33+
GroupId: aws.String("sg-12345678"),
34+
},
35+
&ec2.SecurityGroup{
36+
GroupId: aws.String("sg-abcdefgh"),
37+
},
38+
},
39+
Issues: []*issue.Issue{
40+
&issue.Issue{
41+
Type: "ERROR",
42+
Message: "\"sg-1234abcd\" is invalid security group.",
43+
Line: 4,
44+
File: "test.tf",
45+
},
46+
&issue.Issue{
47+
Type: "ERROR",
48+
Message: "\"sg-abcd1234\" is invalid security group.",
49+
Line: 5,
50+
File: "test.tf",
51+
},
52+
},
53+
},
54+
{
55+
Name: "security group is valid",
56+
Src: `
57+
resource "aws_elasticache_cluster" "redis" {
58+
security_group_ids = [
59+
"sg-1234abcd",
60+
"sg-abcd1234",
61+
]
62+
}`,
63+
Response: []*ec2.SecurityGroup{
64+
&ec2.SecurityGroup{
65+
GroupId: aws.String("sg-1234abcd"),
66+
},
67+
&ec2.SecurityGroup{
68+
GroupId: aws.String("sg-abcd1234"),
69+
},
70+
},
71+
Issues: []*issue.Issue{},
72+
},
73+
}
74+
75+
for _, tc := range cases {
76+
c := config.Init()
77+
c.DeepCheck = true
78+
79+
awsClient := c.NewAwsClient()
80+
ctrl := gomock.NewController(t)
81+
defer ctrl.Finish()
82+
ec2mock := awsmock.NewMockEC2API(ctrl)
83+
ec2mock.EXPECT().DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{}).Return(&ec2.DescribeSecurityGroupsOutput{
84+
SecurityGroups: tc.Response,
85+
}, nil)
86+
awsClient.Ec2 = ec2mock
87+
88+
var issues = []*issue.Issue{}
89+
TestDetectByCreatorName(
90+
"CreateAwsElastiCacheClusterInvalidSecurityGroupDetector",
91+
tc.Src,
92+
c,
93+
awsClient,
94+
&issues,
95+
)
96+
97+
if !reflect.DeepEqual(issues, tc.Issues) {
98+
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
99+
}
100+
}
101+
}

detector/detector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var detectors = map[string]string{
4646
"aws_elasticache_cluster_default_parameter_group": "CreateAwsElastiCacheClusterDefaultParameterGroupDetector",
4747
"aws_elasticache_cluster_invalid_parameter_group": "CreateAwsElastiCacheClusterInvalidParameterGroupDetector",
4848
"aws_elasticache_cluster_invalid_subnet_group": "CreateAwsElastiCacheClusterInvalidSubnetGroupDetector",
49+
"aws_elasticache_cluster_invalid_security_group": "CreateAwsElastiCacheClusterInvalidSecurityGroupDetector",
4950
}
5051

5152
func NewDetector(listMap map[string]*ast.ObjectList, c *config.Config) (*Detector, error) {

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ If you have enabled deep check, you can check if nonexistent values ​​are no
4545
- AWS ElastiCache Cluster
4646
- aws_elasticache_cluster_invalid_parameter_group
4747
- aws_elasticache_cluster_invalid_subnet_group
48+
- aws_elasticache_cluster_invalid_security_group

0 commit comments

Comments
 (0)