2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-05-04 16:37:45 +00:00
|
|
|
awstest "github.com/cloudskiff/driftctl/test/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
2021-01-20 13:01:57 +00:00
|
|
|
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
|
2021-01-15 11:44:13 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-01-20 13:01:57 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
|
|
|
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/cloudskiff/driftctl/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIamRolePolicySupplier_Resources(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks func(client *awstest.MockFakeIAM)
|
2020-12-09 15:31:34 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "multiples roles without any inline policies",
|
|
|
|
dirName: "iam_role_policy_empty",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeIAM) {
|
2020-12-09 15:31:34 +00:00
|
|
|
client.On("ListRolesPages",
|
|
|
|
&iam.ListRolesInput{},
|
|
|
|
mock.MatchedBy(func(callback func(res *iam.ListRolesOutput, lastPage bool) bool) bool {
|
|
|
|
callback(&iam.ListRolesOutput{Roles: []*iam.Role{
|
|
|
|
{
|
|
|
|
RoleName: aws.String("test_role_0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RoleName: aws.String("test_role_1"),
|
|
|
|
},
|
|
|
|
}}, true)
|
|
|
|
return true
|
|
|
|
})).Return(nil)
|
|
|
|
client.On("ListRolePoliciesPages",
|
|
|
|
&iam.ListRolePoliciesInput{
|
|
|
|
RoleName: aws.String("test_role_0"),
|
|
|
|
},
|
|
|
|
mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
client.On("ListRolePoliciesPages",
|
|
|
|
&iam.ListRolePoliciesInput{
|
|
|
|
RoleName: aws.String("test_role_1"),
|
|
|
|
},
|
|
|
|
mock.Anything,
|
|
|
|
).Return(nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "iam multiples roles with inline policies",
|
|
|
|
dirName: "iam_role_policy_multiple",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeIAM) {
|
2020-12-09 15:31:34 +00:00
|
|
|
client.On("ListRolesPages",
|
|
|
|
&iam.ListRolesInput{},
|
|
|
|
mock.MatchedBy(func(callback func(res *iam.ListRolesOutput, lastPage bool) bool) bool {
|
|
|
|
callback(&iam.ListRolesOutput{Roles: []*iam.Role{
|
|
|
|
{
|
|
|
|
RoleName: aws.String("test_role_0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RoleName: aws.String("test_role_1"),
|
|
|
|
},
|
|
|
|
}}, true)
|
|
|
|
return true
|
|
|
|
})).Once().Return(nil)
|
|
|
|
firstMockCalled := false
|
|
|
|
client.On("ListRolePoliciesPages",
|
|
|
|
&iam.ListRolePoliciesInput{
|
|
|
|
RoleName: aws.String("test_role_0"),
|
|
|
|
},
|
|
|
|
mock.MatchedBy(func(callback func(res *iam.ListRolePoliciesOutput, lastPage bool) bool) bool {
|
|
|
|
if firstMockCalled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
callback(&iam.ListRolePoliciesOutput{
|
|
|
|
PolicyNames: []*string{
|
|
|
|
aws.String("policy-role0-0"),
|
|
|
|
aws.String("policy-role0-1"),
|
|
|
|
},
|
|
|
|
}, false)
|
|
|
|
callback(&iam.ListRolePoliciesOutput{
|
|
|
|
PolicyNames: []*string{
|
|
|
|
aws.String("policy-role0-2"),
|
|
|
|
},
|
|
|
|
}, true)
|
|
|
|
firstMockCalled = true
|
|
|
|
return true
|
|
|
|
})).Once().Return(nil)
|
|
|
|
client.On("ListRolePoliciesPages",
|
|
|
|
&iam.ListRolePoliciesInput{
|
|
|
|
RoleName: aws.String("test_role_1"),
|
|
|
|
},
|
|
|
|
mock.MatchedBy(func(callback func(res *iam.ListRolePoliciesOutput, lastPage bool) bool) bool {
|
|
|
|
callback(&iam.ListRolePoliciesOutput{
|
|
|
|
PolicyNames: []*string{
|
|
|
|
aws.String("policy-role1-0"),
|
|
|
|
aws.String("policy-role1-1"),
|
|
|
|
},
|
|
|
|
}, false)
|
|
|
|
callback(&iam.ListRolePoliciesOutput{
|
|
|
|
PolicyNames: []*string{
|
|
|
|
aws.String("policy-role1-2"),
|
|
|
|
},
|
|
|
|
}, true)
|
|
|
|
return true
|
|
|
|
})).Once().Return(nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
|
|
|
test: "Cannot list roles",
|
|
|
|
dirName: "iam_role_policy_empty",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeIAM) {
|
2021-01-20 13:01:57 +00:00
|
|
|
client.On("ListRolesPages",
|
|
|
|
&iam.ListRolesInput{},
|
|
|
|
mock.MatchedBy(func(callback func(res *iam.ListRolesOutput, lastPage bool) bool) bool {
|
|
|
|
return true
|
|
|
|
})).Return(awserr.NewRequestFailure(nil, 403, ""))
|
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamRolePolicyResourceType, resourceaws.AwsIamRoleResourceType),
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
2021-01-22 17:06:17 +00:00
|
|
|
|
|
|
|
providerLibrary := terraform.NewProviderLibrary()
|
|
|
|
supplierLibrary := resource.NewSupplierLibrary()
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
|
|
resourceaws.InitResourcesMetadata(repo)
|
|
|
|
factory := terraform.NewTerraformResourceFactory(repo)
|
|
|
|
|
|
|
|
deserializer := resource.NewDeserializer(factory)
|
2020-12-09 15:31:34 +00:00
|
|
|
if shouldUpdate {
|
2021-02-09 13:56:11 +00:00
|
|
|
provider, err := InitTestAwsProvider(providerLibrary)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
supplierLibrary.AddSupplier(NewIamRolePolicySupplier(provider, deserializer))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
2021-05-04 16:37:45 +00:00
|
|
|
fakeIam := awstest.MockFakeIAM{}
|
2020-12-09 15:31:34 +00:00
|
|
|
c.mocks(&fakeIam)
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
2020-12-09 15:31:34 +00:00
|
|
|
s := &IamRolePolicySupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
|
|
|
&fakeIam,
|
2021-01-15 11:44:13 +00:00
|
|
|
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
got, err := s.Resources()
|
2021-01-20 13:01:57 +00:00
|
|
|
assert.Equal(tt, c.err, err)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
mock.AssertExpectationsForObjects(tt)
|
|
|
|
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|