2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-21 11:46:12 +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-21 11:46:12 +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 TestIamUserPolicySupplier_Resources(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2021-05-21 11:46:12 +00:00
|
|
|
mocks func(repo *repository.MockIAMRepository)
|
2020-12-09 15:31:34 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
2021-05-21 11:46:12 +00:00
|
|
|
test: "no iam user policy",
|
2020-12-09 15:31:34 +00:00
|
|
|
dirName: "iam_user_policy_empty",
|
2021-05-21 11:46:12 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
users := []*iam.User{
|
|
|
|
{
|
|
|
|
UserName: aws.String("loadbalancer"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
repo.On("ListAllUsers").Return(users, nil)
|
|
|
|
repo.On("ListAllUserPolicies", users).Return([]string{}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "iam multiples users multiple policies",
|
|
|
|
dirName: "iam_user_policy_multiple",
|
2021-05-21 11:46:12 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
users := []*iam.User{
|
|
|
|
{
|
2020-12-09 15:31:34 +00:00
|
|
|
UserName: aws.String("loadbalancer"),
|
|
|
|
},
|
2021-05-21 11:46:12 +00:00
|
|
|
{
|
2020-12-09 15:31:34 +00:00
|
|
|
UserName: aws.String("loadbalancer2"),
|
|
|
|
},
|
2021-05-21 11:46:12 +00:00
|
|
|
{
|
2020-12-09 15:31:34 +00:00
|
|
|
UserName: aws.String("loadbalancer3"),
|
|
|
|
},
|
2021-05-21 11:46:12 +00:00
|
|
|
}
|
|
|
|
repo.On("ListAllUsers").Return(users, nil)
|
|
|
|
repo.On("ListAllUserPolicies", users).Once().Return([]string{
|
|
|
|
*aws.String("loadbalancer:test"),
|
|
|
|
*aws.String("loadbalancer:test2"),
|
|
|
|
*aws.String("loadbalancer:test3"),
|
|
|
|
*aws.String("loadbalancer:test4"),
|
|
|
|
*aws.String("loadbalancer2:test2"),
|
|
|
|
*aws.String("loadbalancer2:test22"),
|
|
|
|
*aws.String("loadbalancer2:test23"),
|
|
|
|
*aws.String("loadbalancer2:test24"),
|
|
|
|
*aws.String("loadbalancer3:test3"),
|
|
|
|
*aws.String("loadbalancer3:test32"),
|
|
|
|
*aws.String("loadbalancer3:test33"),
|
|
|
|
*aws.String("loadbalancer3:test34"),
|
|
|
|
}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
2021-05-21 11:46:12 +00:00
|
|
|
test: "cannot list user",
|
2021-01-20 13:01:57 +00:00
|
|
|
dirName: "iam_user_policy_empty",
|
2021-05-21 11:46:12 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllUsers").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamUserPolicyResourceType, resourceaws.AwsIamUserResourceType),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "cannot list user policy",
|
|
|
|
dirName: "iam_user_policy_empty",
|
2021-05-21 11:46:12 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllUsers").Once().Return([]*iam.User{}, nil)
|
|
|
|
repo.On("ListAllUserPolicies", mock.Anything).Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamUserPolicyResourceType),
|
|
|
|
},
|
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(NewIamUserPolicySupplier(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-21 11:46:12 +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 := &IamUserPolicySupplier{
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|