2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVPCSecurityGroupRuleSanitizer(t *testing.T) {
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
factory := &terraform.MockResourceFactory{}
|
2021-05-10 16:02:57 +00:00
|
|
|
factory.On("CreateAbstractResource", aws.AwsSecurityGroupRuleResourceType, "sgrule-1175318309", mock.Anything).Times(1).Return(
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-1175318309",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
2021-05-24 15:19:06 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-10 16:02:57 +00:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
factory.On("CreateAbstractResource", aws.AwsSecurityGroupRuleResourceType, "sgrule-2582518759", mock.Anything).Times(1).Return(
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-2582518759",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
2021-05-24 15:19:06 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-10 16:02:57 +00:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
factory.On("CreateAbstractResource", aws.AwsSecurityGroupRuleResourceType, "sgrule-2165103420", mock.Anything).Times(1).Return(
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-2165103420",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
2021-05-24 15:19:06 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-10 16:02:57 +00:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
factory.On("CreateAbstractResource", aws.AwsSecurityGroupRuleResourceType, "sgrule-350400929", mock.Anything).Times(1).Return(
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-350400929",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
2021-05-24 15:19:06 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-10 16:02:57 +00:00
|
|
|
}, nil)
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
middleware := NewVPCSecurityGroupRuleSanitizer(factory)
|
2020-12-09 15:31:34 +00:00
|
|
|
var remoteResources []resource.Resource
|
|
|
|
stateResources := []resource.Resource{
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "sg-test",
|
2021-05-10 16:02:57 +00:00
|
|
|
Type: aws.AwsSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sg-test",
|
|
|
|
"name": "test",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-3970541193",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3970541193",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
2021-05-10 16:02:57 +00:00
|
|
|
"self": true,
|
|
|
|
"source_security_group_id": "sg-0254c038e32f25530",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-845917806",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-845917806",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0cc8b3c3c2851705a",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-294318973",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-294318973",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"1.2.0.0/16", "5.6.7.0/24"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-2471889226",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2471889226",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"prefix_list_id": []interface{}{"pl-abb451c2"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "sgrule-3587309474",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3587309474",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
|
|
|
"prefix_list_id": []interface{}{"sg-9e0204ff"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err := middleware.Execute(&remoteResources, &stateResources)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(stateResources) != 8 {
|
|
|
|
t.Error("Some security group rules were not split")
|
|
|
|
}
|
|
|
|
}
|