2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-19 15:02:45 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-06-08 09:40:04 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
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"
|
|
|
|
|
2021-05-19 15:02:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
2021-05-27 14:15:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/mocks"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
"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-19 15:02:45 +00:00
|
|
|
mocks func(repo *repository.MockIAMRepository)
|
2020-12-09 15:31:34 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
2021-05-19 15:02:45 +00:00
|
|
|
test: "no iam role policy",
|
2020-12-09 15:31:34 +00:00
|
|
|
dirName: "iam_role_policy_empty",
|
2021-05-19 15:02:45 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
roles := []*iam.Role{
|
|
|
|
{
|
|
|
|
RoleName: aws.String("test_role"),
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-05-19 15:02:45 +00:00
|
|
|
}
|
|
|
|
repo.On("ListAllRoles").Return(roles, nil)
|
|
|
|
repo.On("ListAllRolePolicies", roles).Return([]string{}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
2021-05-19 15:02:45 +00:00
|
|
|
test: "multiples roles with inline policies",
|
2020-12-09 15:31:34 +00:00
|
|
|
dirName: "iam_role_policy_multiple",
|
2021-05-19 15:02:45 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
roles := []*iam.Role{
|
|
|
|
{
|
2020-12-09 15:31:34 +00:00
|
|
|
RoleName: aws.String("test_role_0"),
|
|
|
|
},
|
2021-05-19 15:02:45 +00:00
|
|
|
{
|
2020-12-09 15:31:34 +00:00
|
|
|
RoleName: aws.String("test_role_1"),
|
|
|
|
},
|
2021-05-19 15:02:45 +00:00
|
|
|
}
|
|
|
|
repo.On("ListAllRoles").Return(roles, nil)
|
|
|
|
repo.On("ListAllRolePolicies", roles).Return([]string{
|
|
|
|
*aws.String("test_role_0:policy-role0-0"),
|
|
|
|
*aws.String("test_role_0:policy-role0-1"),
|
|
|
|
*aws.String("test_role_0:policy-role0-2"),
|
|
|
|
*aws.String("test_role_1:policy-role1-0"),
|
|
|
|
*aws.String("test_role_1:policy-role1-1"),
|
|
|
|
*aws.String("test_role_1:policy-role1-2"),
|
|
|
|
}, nil).Once()
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
|
|
|
test: "Cannot list roles",
|
|
|
|
dirName: "iam_role_policy_empty",
|
2021-05-19 15:02:45 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllRoles").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamRolePolicyResourceType, resourceaws.AwsIamRoleResourceType),
|
|
|
|
},
|
2021-05-19 15:02:45 +00:00
|
|
|
{
|
|
|
|
test: "cannot list role policy",
|
|
|
|
dirName: "iam_role_policy_empty",
|
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllRoles").Once().Return([]*iam.Role{}, nil)
|
|
|
|
repo.On("ListAllRolePolicies", mock.Anything).Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamRolePolicyResourceType),
|
|
|
|
},
|
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-06-08 09:40:04 +00:00
|
|
|
supplierLibrary.AddSupplier(NewIamRolePolicySupplier(provider, deserializer, repository.NewIAMRepository(provider.session, cache.New(0))))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
2021-05-19 15:02:45 +00:00
|
|
|
fakeIam := repository.MockIAMRepository{}
|
2020-12-09 15:31:34 +00:00
|
|
|
c.mocks(&fakeIam)
|
|
|
|
|
2021-05-27 14:15:55 +00:00
|
|
|
provider := mocks.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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|