Skip to content

Commit

Permalink
Merge pull request #98 from wata727/aws_route_invalid_network_interface
Browse files Browse the repository at this point in the history
Add AwsRouteInvalidNetworkInterfaceDetector
  • Loading branch information
wata727 authored Apr 9, 2017
2 parents 6dbf052 + cd2492e commit 95ef70e
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type ResponseCache struct {
DescribeEgressOnlyInternetGatewaysOutput *ec2.DescribeEgressOnlyInternetGatewaysOutput
DescribeNatGatewaysOutput *ec2.DescribeNatGatewaysOutput
DescribeVpcPeeringConnectionsOutput *ec2.DescribeVpcPeeringConnectionsOutput
DescribeNetworkInterfacesOutput *ec2.DescribeNetworkInterfacesOutput
ListInstanceProfilesOutput *iam.ListInstanceProfilesOutput
DescribeDBSubnetGroupsOutput *rds.DescribeDBSubnetGroupsOutput
DescribeDBParameterGroupsOutput *rds.DescribeDBParameterGroupsOutput
Expand Down Expand Up @@ -235,6 +236,17 @@ func (c AwsClient) DescribeVpcPeeringConnections() (*ec2.DescribeVpcPeeringConne
return c.Cache.DescribeVpcPeeringConnectionsOutput, nil
}

func (c AwsClient) DescribeNetworkInterfaces() (*ec2.DescribeNetworkInterfacesOutput, error) {
if c.Cache.DescribeNetworkInterfacesOutput == nil {
resp, err := c.Ec2.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{})
if err != nil {
return nil, err
}
c.Cache.DescribeNetworkInterfacesOutput = resp
}
return c.Cache.DescribeNetworkInterfacesOutput, nil
}

func (c *AwsClient) ListInstanceProfiles() (*iam.ListInstanceProfilesOutput, error) {
if c.Cache.ListInstanceProfilesOutput == nil {
resp, err := c.Iam.ListInstanceProfiles(&iam.ListInstanceProfilesInput{})
Expand Down
62 changes: 62 additions & 0 deletions detector/aws_route_invalid_network_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package detector

import (
"fmt"

"github.com/hashicorp/hcl/hcl/ast"
"github.com/wata727/tflint/issue"
)

type AwsRouteInvalidNetworkInterfaceDetector struct {
*Detector
IssueType string
Target string
DeepCheck bool
networkInterfaces map[string]bool
}

func (d *Detector) CreateAwsRouteInvalidNetworkInterfaceDetector() *AwsRouteInvalidNetworkInterfaceDetector {
return &AwsRouteInvalidNetworkInterfaceDetector{
Detector: d,
IssueType: issue.ERROR,
Target: "aws_route",
DeepCheck: true,
networkInterfaces: map[string]bool{},
}
}

func (d *AwsRouteInvalidNetworkInterfaceDetector) PreProcess() {
resp, err := d.AwsClient.DescribeNetworkInterfaces()
if err != nil {
d.Logger.Error(err)
d.Error = true
return
}

for _, networkInterface := range resp.NetworkInterfaces {
d.networkInterfaces[*networkInterface.NetworkInterfaceId] = true
}
}

func (d *AwsRouteInvalidNetworkInterfaceDetector) Detect(file string, item *ast.ObjectItem, issues *[]*issue.Issue) {
networkInterfaceToken, err := hclLiteralToken(item, "network_interface_id")
if err != nil {
d.Logger.Error(err)
return
}
networkInterface, err := d.evalToString(networkInterfaceToken.Text)
if err != nil {
d.Logger.Error(err)
return
}

if !d.networkInterfaces[networkInterface] {
issue := &issue.Issue{
Type: d.IssueType,
Message: fmt.Sprintf("\"%s\" is invalid network interface ID.", networkInterface),
Line: networkInterfaceToken.Pos.Line,
File: file,
}
*issues = append(*issues, issue)
}
}
94 changes: 94 additions & 0 deletions detector/aws_route_invalid_network_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package detector

import (
"reflect"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/mock/gomock"
"github.com/k0kubun/pp"
"github.com/wata727/tflint/config"
"github.com/wata727/tflint/issue"
"github.com/wata727/tflint/mock"
)

func TestDetectAwsRouteInvalidNetworkInterface(t *testing.T) {
cases := []struct {
Name string
Src string
Response []*ec2.NetworkInterface
Issues []*issue.Issue
}{
{
Name: "network interface id is invalid",
Src: `
resource "aws_route" "foo" {
network_interface_id = "eni-1234abcd"
}`,
Response: []*ec2.NetworkInterface{
{
NetworkInterfaceId: aws.String("eni-5678abcd"),
},
{
NetworkInterfaceId: aws.String("eni-abcd1234"),
},
},
Issues: []*issue.Issue{
{
Type: "ERROR",
Message: "\"eni-1234abcd\" is invalid network interface ID.",
Line: 3,
File: "test.tf",
},
},
},
{
Name: "network interfaec id is valid",
Src: `
resource "aws_route" "foo" {
network_interface_id = "eni-1234abcd"
}`,
Response: []*ec2.NetworkInterface{
{
NetworkInterfaceId: aws.String("eni-1234abcd"),
},
{
NetworkInterfaceId: aws.String("eni-abcd1234"),
},
},
Issues: []*issue.Issue{},
},
}

for _, tc := range cases {
c := config.Init()
c.DeepCheck = true

awsClient := c.NewAwsClient()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ec2mock := mock.NewMockEC2API(ctrl)
ec2mock.EXPECT().DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{}).Return(&ec2.DescribeNetworkInterfacesOutput{
NetworkInterfaces: tc.Response,
}, nil)
awsClient.Ec2 = ec2mock

var issues = []*issue.Issue{}
err := TestDetectByCreatorName(
"CreateAwsRouteInvalidNetworkInterfaceDetector",
tc.Src,
"",
c,
awsClient,
&issues,
)
if err != nil {
t.Fatalf("\nERROR: %s", err)
}

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("\nBad: %s\nExpected: %s\n\ntestcase: %s", pp.Sprint(issues), pp.Sprint(tc.Issues), tc.Name)
}
}
}
1 change: 1 addition & 0 deletions detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var detectors = map[string]string{
"aws_route_invalid_nat_gateway": "CreateAwsRouteInvalidNatGatewayDetector",
"aws_route_invalid_vpc_peering_connection": "CreateAwsRouteInvalidVpcPeeringConnectionDetector",
"aws_route_invalid_instance": "CreateAwsRouteInvalidInstanceDetector",
"aws_route_invalid_network_interface": "CreateAwsRouteInvalidNetworkInterfaceDetector",
}

func NewDetector(templates map[string]*ast.File, state *state.TFState, tfvars []*ast.File, c *config.Config) (*Detector, error) {
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Report these issues if you have specified invalid resource ID, name, etc. All is
- aws_route_invalid_nat_gateway
- aws_route_invalid_vpc_peering_connection
- aws_route_invalid_instance
- aws_route_invalid_network_interface

### Duplicate Resource Issue
Report these issues if you have specified resource ID, name, etc that already existed and must be unique. All issues are reported as ERROR. These issues are reported when enabled deep check. For example, it happens when resources with the same name is already created. Please check the actual resources again.
Expand Down

0 comments on commit 95ef70e

Please sign in to comment.