2021-06-28 09:16:10 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/resource"
|
|
|
|
"github.com/snyk/driftctl/pkg/resource/aws"
|
2021-06-28 09:16:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsDefaultSecurityGroupRule_Execute(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources *[]*resource.Resource
|
|
|
|
resourcesFromState *[]*resource.Resource
|
|
|
|
expected *[]*resource.Resource
|
2021-06-28 09:16:10 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Should ignore default rules if not managed",
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-sg",
|
|
|
|
Type: aws.AwsDefaultSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"source_security_group_id": "default-sg",
|
2021-07-22 10:46:20 +00:00
|
|
|
"self": true,
|
2021-06-28 09:16:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(22),
|
|
|
|
"to_port": float64(22),
|
|
|
|
"protocol": "tcp",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"cidr_blocks": []interface{}{"1.2.3.4/32"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{},
|
|
|
|
expected: &[]*resource.Resource{
|
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-sg",
|
|
|
|
Type: aws.AwsDefaultSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(22),
|
|
|
|
"to_port": float64(22),
|
|
|
|
"protocol": "tcp",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"cidr_blocks": []interface{}{"1.2.3.4/32"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should not ignore default rules if managed",
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-sg",
|
|
|
|
Type: aws.AwsDefaultSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-sg",
|
|
|
|
Type: aws.AwsSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"source_security_group_id": "default-sg",
|
2021-07-22 10:46:20 +00:00
|
|
|
"self": true,
|
2021-06-28 09:16:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(22),
|
|
|
|
"to_port": float64(22),
|
|
|
|
"protocol": "tcp",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"cidr_blocks": []interface{}{"1.2.3.4/32"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"self": false,
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress-2",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-sg",
|
|
|
|
Type: aws.AwsDefaultSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"source_security_group_id": "default-sg",
|
2021-07-22 10:46:20 +00:00
|
|
|
"self": true,
|
2021-06-28 09:16:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
expected: &[]*resource.Resource{
|
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-sg",
|
|
|
|
Type: aws.AwsDefaultSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-sg",
|
|
|
|
Type: aws.AwsSecurityGroupResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"source_security_group_id": "default-sg",
|
2021-07-22 10:46:20 +00:00
|
|
|
"self": true,
|
2021-06-28 09:16:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "default-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-ingress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "ingress",
|
|
|
|
"from_port": float64(22),
|
|
|
|
"to_port": float64(22),
|
|
|
|
"protocol": "tcp",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"cidr_blocks": []interface{}{"1.2.3.4/32"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "dummy-egress",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"self": false,
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-28 09:16:10 +00:00
|
|
|
Id: "default-egress-2",
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "egress",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"protocol": "-1",
|
|
|
|
"security_group_id": "dummy-sg",
|
|
|
|
"self": false,
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
m := AwsDefaultSecurityGroupRule{}
|
|
|
|
if err := m.Execute(tt.remoteResources, tt.resourcesFromState); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.remoteResources, tt.expected) {
|
|
|
|
t.Fatalf("Expected results mismatch")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|