Merge pull request #828 from cloudskiff/issue_728_split_iam_user_policy
Split iam_user_policy_suppliermain
commit
57a0c387ef
|
@ -0,0 +1,50 @@
|
||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||||
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IamUserPolicyEnumerator struct {
|
||||||
|
repository repository.IAMRepository
|
||||||
|
factory resource.ResourceFactory
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIamUserPolicyEnumerator(repo repository.IAMRepository, factory resource.ResourceFactory) *IamUserPolicyEnumerator {
|
||||||
|
return &IamUserPolicyEnumerator{
|
||||||
|
repository: repo,
|
||||||
|
factory: factory,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *IamUserPolicyEnumerator) SupportedType() resource.ResourceType {
|
||||||
|
return aws.AwsIamUserPolicyResourceType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *IamUserPolicyEnumerator) Enumerate() ([]resource.Resource, error) {
|
||||||
|
users, err := e.repository.ListAllUsers()
|
||||||
|
if err != nil {
|
||||||
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, string(e.SupportedType()), aws.AwsIamUserResourceType)
|
||||||
|
}
|
||||||
|
userPolicies, err := e.repository.ListAllUserPolicies(users)
|
||||||
|
if err != nil {
|
||||||
|
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]resource.Resource, len(userPolicies))
|
||||||
|
|
||||||
|
for _, userPolicy := range userPolicies {
|
||||||
|
results = append(
|
||||||
|
results,
|
||||||
|
e.factory.CreateAbstractResource(
|
||||||
|
string(e.SupportedType()),
|
||||||
|
userPolicy,
|
||||||
|
map[string]interface{}{},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, err
|
||||||
|
}
|
|
@ -1,69 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
||||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
||||||
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
||||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/zclconf/go-cty/cty"
|
|
||||||
)
|
|
||||||
|
|
||||||
type IamUserPolicySupplier struct {
|
|
||||||
reader terraform.ResourceReader
|
|
||||||
deserializer *resource.Deserializer
|
|
||||||
repository repository.IAMRepository
|
|
||||||
runner *terraform.ParallelResourceReader
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIamUserPolicySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamUserPolicySupplier {
|
|
||||||
return &IamUserPolicySupplier{
|
|
||||||
provider,
|
|
||||||
deserializer,
|
|
||||||
repository,
|
|
||||||
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *IamUserPolicySupplier) Resources() ([]resource.Resource, error) {
|
|
||||||
users, err := s.repository.ListAllUsers()
|
|
||||||
if err != nil {
|
|
||||||
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamUserPolicyResourceType, resourceaws.AwsIamUserResourceType)
|
|
||||||
}
|
|
||||||
policies, err := s.repository.ListAllUserPolicies(users)
|
|
||||||
if err != nil {
|
|
||||||
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamUserPolicyResourceType)
|
|
||||||
}
|
|
||||||
results := make([]cty.Value, 0)
|
|
||||||
if len(policies) > 0 {
|
|
||||||
for _, policy := range policies {
|
|
||||||
p := policy
|
|
||||||
s.runner.Run(func() (cty.Value, error) {
|
|
||||||
return s.readUserPolicy(p)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
results, err = s.runner.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s.deserializer.Deserialize(resourceaws.AwsIamUserPolicyResourceType, results)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *IamUserPolicySupplier) readUserPolicy(policyName string) (cty.Value, error) {
|
|
||||||
res, err := s.reader.ReadResource(
|
|
||||||
terraform.ReadResourceArgs{
|
|
||||||
Ty: resourceaws.AwsIamUserPolicyResourceType,
|
|
||||||
ID: policyName,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Warnf("Error reading iam user policy %s[%s]: %+v", policyName, resourceaws.AwsIamUserResourceType, err)
|
|
||||||
return cty.NilVal, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return *res, nil
|
|
||||||
}
|
|
|
@ -1,142 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/iam"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
|
||||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
||||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
|
||||||
|
|
||||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
||||||
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/parallel"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
|
|
||||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
|
||||||
"github.com/cloudskiff/driftctl/test/mocks"
|
|
||||||
|
|
||||||
"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
|
|
||||||
mocks func(repo *repository.MockIAMRepository)
|
|
||||||
err error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
test: "no iam user policy",
|
|
||||||
dirName: "iam_user_policy_empty",
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: "iam multiples users multiple policies",
|
|
||||||
dirName: "iam_user_policy_multiple",
|
|
||||||
mocks: func(repo *repository.MockIAMRepository) {
|
|
||||||
users := []*iam.User{
|
|
||||||
{
|
|
||||||
UserName: aws.String("loadbalancer"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
UserName: aws.String("loadbalancer2"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
UserName: aws.String("loadbalancer3"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: "cannot list user",
|
|
||||||
dirName: "iam_user_policy_empty",
|
|
||||||
mocks: func(repo *repository.MockIAMRepository) {
|
|
||||||
repo.On("ListAllUsers").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
|
||||||
},
|
|
||||||
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamUserPolicyResourceType, resourceaws.AwsIamUserResourceType),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: "cannot list user policy",
|
|
||||||
dirName: "iam_user_policy_empty",
|
|
||||||
mocks: func(repo *repository.MockIAMRepository) {
|
|
||||||
repo.On("ListAllUsers").Once().Return([]*iam.User{}, nil)
|
|
||||||
repo.On("ListAllUserPolicies", mock.Anything).Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
|
||||||
},
|
|
||||||
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamUserPolicyResourceType),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, c := range cases {
|
|
||||||
shouldUpdate := c.dirName == *goldenfile.Update
|
|
||||||
|
|
||||||
providerLibrary := terraform.NewProviderLibrary()
|
|
||||||
supplierLibrary := resource.NewSupplierLibrary()
|
|
||||||
|
|
||||||
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
||||||
resourceaws.InitResourcesMetadata(repo)
|
|
||||||
factory := terraform.NewTerraformResourceFactory(repo)
|
|
||||||
|
|
||||||
deserializer := resource.NewDeserializer(factory)
|
|
||||||
if shouldUpdate {
|
|
||||||
provider, err := InitTestAwsProvider(providerLibrary)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
supplierLibrary.AddSupplier(NewIamUserPolicySupplier(provider, deserializer, repository.NewIAMRepository(provider.session, cache.New(0))))
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run(c.test, func(tt *testing.T) {
|
|
||||||
fakeIam := repository.MockIAMRepository{}
|
|
||||||
c.mocks(&fakeIam)
|
|
||||||
|
|
||||||
provider := mocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
|
||||||
s := &IamUserPolicySupplier{
|
|
||||||
provider,
|
|
||||||
deserializer,
|
|
||||||
&fakeIam,
|
|
||||||
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
|
||||||
}
|
|
||||||
got, err := s.Resources()
|
|
||||||
assert.Equal(tt, c.err, err)
|
|
||||||
|
|
||||||
mock.AssertExpectationsForObjects(tt)
|
|
||||||
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, t)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -155,8 +155,9 @@ func Init(version string, alerter *alerter.Alerter,
|
||||||
|
|
||||||
remoteLibrary.AddEnumerator(NewIamUserEnumerator(iamRepository, factory))
|
remoteLibrary.AddEnumerator(NewIamUserEnumerator(iamRepository, factory))
|
||||||
remoteLibrary.AddDetailsFetcher(aws.AwsIamUserResourceType, common.NewGenericDetailsFetcher(aws.AwsIamUserResourceType, provider, deserializer))
|
remoteLibrary.AddDetailsFetcher(aws.AwsIamUserResourceType, common.NewGenericDetailsFetcher(aws.AwsIamUserResourceType, provider, deserializer))
|
||||||
|
remoteLibrary.AddEnumerator(NewIamUserPolicyEnumerator(iamRepository, factory))
|
||||||
|
remoteLibrary.AddDetailsFetcher(aws.AwsIamUserPolicyResourceType, common.NewGenericDetailsFetcher(aws.AwsIamUserPolicyResourceType, provider, deserializer))
|
||||||
|
|
||||||
supplierLibrary.AddSupplier(NewIamUserPolicySupplier(provider, deserializer, iamRepository))
|
|
||||||
supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
|
supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
|
||||||
supplierLibrary.AddSupplier(NewIamAccessKeySupplier(provider, deserializer, iamRepository))
|
supplierLibrary.AddSupplier(NewIamAccessKeySupplier(provider, deserializer, iamRepository))
|
||||||
supplierLibrary.AddSupplier(NewIamRoleSupplier(provider, deserializer, iamRepository))
|
supplierLibrary.AddSupplier(NewIamRoleSupplier(provider, deserializer, iamRepository))
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -121,6 +121,134 @@ func TestIamUser(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIamUserPolicy(t *testing.T) {
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
test string
|
||||||
|
dirName string
|
||||||
|
mocks func(repo *repository.MockIAMRepository)
|
||||||
|
wantErr error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
test: "no iam user policy",
|
||||||
|
dirName: "iam_user_policy_empty",
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
wantErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: "iam multiples users multiple policies",
|
||||||
|
dirName: "iam_user_policy_multiple",
|
||||||
|
mocks: func(repo *repository.MockIAMRepository) {
|
||||||
|
users := []*iam.User{
|
||||||
|
{
|
||||||
|
UserName: aws.String("loadbalancer"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
UserName: aws.String("loadbalancer2"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
UserName: aws.String("loadbalancer3"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
wantErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: "cannot list user",
|
||||||
|
dirName: "iam_user_policy_empty",
|
||||||
|
mocks: func(repo *repository.MockIAMRepository) {
|
||||||
|
repo.On("ListAllUsers").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||||
|
},
|
||||||
|
wantErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: "cannot list user policy",
|
||||||
|
dirName: "iam_user_policy_empty",
|
||||||
|
mocks: func(repo *repository.MockIAMRepository) {
|
||||||
|
repo.On("ListAllUsers").Once().Return([]*iam.User{}, nil)
|
||||||
|
repo.On("ListAllUserPolicies", mock.Anything).Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||||
|
},
|
||||||
|
wantErr: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||||||
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
||||||
|
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||||||
|
deserializer := resource.NewDeserializer(factory)
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.test, func(tt *testing.T) {
|
||||||
|
shouldUpdate := c.dirName == *goldenfile.Update
|
||||||
|
|
||||||
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||||
|
SharedConfigState: session.SharedConfigEnable,
|
||||||
|
}))
|
||||||
|
|
||||||
|
scanOptions := ScannerOptions{Deep: true}
|
||||||
|
providerLibrary := terraform.NewProviderLibrary()
|
||||||
|
remoteLibrary := common.NewRemoteLibrary()
|
||||||
|
|
||||||
|
// Initialize mocks
|
||||||
|
alerter := &mocks.AlerterInterface{}
|
||||||
|
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
|
||||||
|
fakeRepo := &repository.MockIAMRepository{}
|
||||||
|
c.mocks(fakeRepo)
|
||||||
|
var repo repository.IAMRepository = fakeRepo
|
||||||
|
providerVersion := "3.19.0"
|
||||||
|
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
provider := terraform2.NewFakeTerraformProvider(realProvider)
|
||||||
|
provider.WithResponse(c.dirName)
|
||||||
|
|
||||||
|
// Replace mock by real resources if we are in update mode
|
||||||
|
if shouldUpdate {
|
||||||
|
err := realProvider.Init()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
provider.ShouldUpdate()
|
||||||
|
repo = repository.NewIAMRepository(sess, cache.New(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteLibrary.AddEnumerator(remoteaws.NewIamUserPolicyEnumerator(repo, factory))
|
||||||
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsIamUserPolicyResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsIamUserPolicyResourceType, provider, deserializer))
|
||||||
|
|
||||||
|
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
|
||||||
|
got, err := s.Resources()
|
||||||
|
assert.Equal(tt, err, c.wantErr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
test.TestAgainstGoldenFile(got, resourceaws.AwsIamUserPolicyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIamPolicy(t *testing.T) {
|
func TestIamPolicy(t *testing.T) {
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
|
Loading…
Reference in New Issue