Issue 165: Delete hidden dependency for iam_policy_supplier

main
Louis TOUSSAINT 2021-05-19 17:09:11 +02:00
parent f81cfb9eaf
commit 414fecb9d1
2 changed files with 28 additions and 56 deletions

View File

@ -1,9 +1,9 @@
package aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
@ -18,7 +18,7 @@ import (
type IamPolicySupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
client iamiface.IAMAPI
client repository.IAMRepository
runner *terraform.ParallelResourceReader
}
@ -26,13 +26,13 @@ func NewIamPolicySupplier(provider *AWSTerraformProvider, deserializer *resource
return &IamPolicySupplier{
provider,
deserializer,
iam.New(provider.session),
repository.NewIAMClient(provider.session),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *IamPolicySupplier) Resources() ([]resource.Resource, error) {
policies, err := listIamPolicies(s.client)
policies, err := s.client.ListAllPolicies()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamPolicyResourceType)
}
@ -41,7 +41,7 @@ func (s *IamPolicySupplier) Resources() ([]resource.Resource, error) {
for _, policy := range policies {
u := *policy
s.runner.Run(func() (cty.Value, error) {
return s.readRes(&u)
return s.readPolicy(&u)
})
}
results, err = s.runner.Wait()
@ -52,7 +52,7 @@ func (s *IamPolicySupplier) Resources() ([]resource.Resource, error) {
return s.deserializer.Deserialize(resourceaws.AwsIamPolicyResourceType, results)
}
func (s *IamPolicySupplier) readRes(resource *iam.Policy) (cty.Value, error) {
func (s *IamPolicySupplier) readPolicy(resource *iam.Policy) (cty.Value, error) {
res, err := s.reader.ReadResource(
terraform.ReadResourceArgs{
Ty: resourceaws.AwsIamPolicyResourceType,
@ -66,18 +66,3 @@ func (s *IamPolicySupplier) readRes(resource *iam.Policy) (cty.Value, error) {
return *res, nil
}
func listIamPolicies(client iamiface.IAMAPI) ([]*iam.Policy, error) {
var resources []*iam.Policy
input := &iam.ListPoliciesInput{
Scope: aws.String(iam.PolicyScopeTypeLocal),
}
err := client.ListPoliciesPages(input, func(res *iam.ListPoliciesOutput, lastPage bool) bool {
resources = append(resources, res.Policies...)
return !lastPage
})
if err != nil {
return nil, err
}
return resources, nil
}

View File

@ -4,6 +4,7 @@ import (
"context"
"testing"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
awstest "github.com/cloudskiff/driftctl/test/aws"
testresource "github.com/cloudskiff/driftctl/test/resource"
@ -18,10 +19,11 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
@ -33,55 +35,40 @@ func TestIamPolicySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *awstest.MockFakeIAM)
mocks func(repo *repository.MockIAMRepository)
err error
}{
{
test: "no iam custom policies",
dirName: "iam_policy_empty",
mocks: func(client *awstest.MockFakeIAM) {
client.On(
"ListPoliciesPages",
&iam.ListPoliciesInput{Scope: aws.String("Local")},
mock.Anything,
).Once().Return(nil)
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllPolicies").Once().Return([]*iam.Policy{}, nil)
},
err: nil,
},
{
test: "iam multiples custom policies",
dirName: "iam_policy_multiple",
mocks: func(client *awstest.MockFakeIAM) {
client.On("ListPoliciesPages",
&iam.ListPoliciesInput{Scope: aws.String(iam.PolicyScopeTypeLocal)},
mock.MatchedBy(func(callback func(res *iam.ListPoliciesOutput, lastPage bool) bool) bool {
callback(&iam.ListPoliciesOutput{Policies: []*iam.Policy{
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-0"),
},
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-1"),
},
}}, false)
callback(&iam.ListPoliciesOutput{Policies: []*iam.Policy{
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-2"),
},
}}, true)
return true
})).Once().Return(nil)
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllPolicies").Once().Return([]*iam.Policy{
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-0"),
},
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-1"),
},
{
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-2"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list iam custom policies",
dirName: "iam_policy_empty",
mocks: func(client *awstest.MockFakeIAM) {
client.On(
"ListPoliciesPages",
&iam.ListPoliciesInput{Scope: aws.String("Local")},
mock.Anything,
).Once().Return(awserr.NewRequestFailure(nil, 403, ""))
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllPolicies").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamPolicyResourceType),
},
@ -106,7 +93,7 @@ func TestIamPolicySupplier_Resources(t *testing.T) {
}
t.Run(c.test, func(tt *testing.T) {
fakeIam := awstest.MockFakeIAM{}
fakeIam := repository.MockIAMRepository{}
c.mocks(&fakeIam)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)