driftctl/pkg/middlewares/aws_defaults_test.go

324 lines
8.2 KiB
Go
Raw Normal View History

package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
2021-04-09 11:07:15 +00:00
func TestAwsDefaults_Execute(t *testing.T) {
tests := []struct {
name string
remoteResources []resource.Resource
resourcesFromState []resource.Resource
expected diff.Changelog
}{
{
"default iam roles when they're not managed by IaC",
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "AWSServiceRoleForSSO",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/aws-service-role/sso.amazonaws.com",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com/",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
2021-04-09 11:07:15 +00:00
Id: "terraform-20210408093258091700000001",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/",
},
2021-04-09 11:07:15 +00:00
},
2021-05-21 14:09:45 +00:00
&resource.AbstractResource{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
},
},
[]resource.Resource{
2021-05-21 14:09:45 +00:00
&resource.AbstractResource{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
2021-04-09 11:07:15 +00:00
Id: "terraform-20210408093258091700000001",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/",
},
2021-04-09 11:07:15 +00:00
},
},
diff.Changelog{
{
Type: "delete",
2021-04-12 15:05:32 +00:00
Path: []string{"0"},
2021-05-07 15:47:53 +00:00
From: &resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com/",
},
},
To: nil,
},
},
},
{
"default iam roles when they're managed by IaC",
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "AWSServiceRoleForSSO",
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/aws-service-role/sso.amazonaws.com/",
"description": "test",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com/",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
2021-04-09 11:07:15 +00:00
Id: "driftctl_assume_role:driftctl_policy.10",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/",
"tags": map[string]string{
"test": "value",
},
},
},
},
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "AWSServiceRoleForSSO",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/aws-service-role/sso.amazonaws.com/",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com/",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "driftctl_assume_role:driftctl_policy.10",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/",
"tags": map[string]string{},
},
},
},
diff.Changelog{
{
Type: diff.DELETE,
2021-05-07 15:47:53 +00:00
Path: []string{"1", "Attrs", "tags", "test"},
From: "value",
To: nil,
},
},
},
{
2021-04-09 11:07:15 +00:00
"ignore default iam role policies when they're not managed by IaC",
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "AWSServiceRoleForSSO",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/aws-service-role/sso.amazonaws.com",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "AWSServiceRoleForSSO",
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "AWSServiceRoleForSSO",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "OrganizationAccountAccessRole",
},
},
2021-05-21 14:09:45 +00:00
&resource.AbstractResource{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
},
},
[]resource.Resource{
2021-05-21 14:09:45 +00:00
&resource.AbstractResource{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
},
},
diff.Changelog{
{
Type: diff.DELETE,
2021-04-12 15:05:32 +00:00
Path: []string{"0"},
2021-05-07 15:47:53 +00:00
From: &resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com",
},
},
To: nil,
},
{
Type: diff.DELETE,
2021-04-12 15:05:32 +00:00
Path: []string{"1"},
2021-05-07 15:47:53 +00:00
From: &resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "OrganizationAccountAccessRole",
},
},
To: nil,
},
},
},
{
2021-04-09 11:07:15 +00:00
"ignore default iam role policies even when they're managed by IaC",
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "custom-role",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/aws-service-role/sso.amazonaws.com",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "driftctl_assume_role:driftctl_policy.10",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "custom-role",
},
},
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole:AdministratorAccess",
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "OrganizationAccountAccessRole",
"name_prefix": nil,
},
},
},
[]resource.Resource{
2021-05-07 15:47:53 +00:00
&resource.AbstractResource{
Id: "OrganizationAccountAccessRole:AdministratorAccess",
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "OrganizationAccountAccessRole",
"name_prefix": "tf-",
},
},
},
diff.Changelog{
{
Type: diff.DELETE,
Path: []string{"0"},
2021-05-07 15:47:53 +00:00
From: &resource.AbstractResource{
Id: "custom-role",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRoleResourceType,
Attrs: &resource.Attributes{
"path": "/not-aws-service-role/sso.amazonaws.com",
},
},
To: nil,
},
{
Type: diff.DELETE,
Path: []string{"1"},
2021-05-07 15:47:53 +00:00
From: &resource.AbstractResource{
Id: "driftctl_assume_role:driftctl_policy.10",
2021-05-07 15:47:53 +00:00
Type: aws.AwsIamRolePolicyResourceType,
Attrs: &resource.Attributes{
"role": "custom-role",
},
},
To: nil,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-04-09 11:07:15 +00:00
m := &AwsDefaults{}
err := m.Execute(&tt.remoteResources, &tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
changelog, err := diff.Diff(tt.remoteResources, tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
diffs, err := diff.Diff(tt.expected, changelog)
if err != nil {
t.Fatal(err)
}
if len(diffs) == 0 {
return
}
for _, change := range diffs {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
})
}
}