Issue 729: Split iam user
parent
9cb7a57e28
commit
5021803fa0
|
@ -0,0 +1,47 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
awssdk "github.com/aws/aws-sdk-go/aws"
|
||||
"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 IamUserEnumerator struct {
|
||||
repository repository.IAMRepository
|
||||
factory resource.ResourceFactory
|
||||
}
|
||||
|
||||
func NewIamUserEnumerator(repo repository.IAMRepository, factory resource.ResourceFactory) *IamUserEnumerator {
|
||||
return &IamUserEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *IamUserEnumerator) SupportedType() resource.ResourceType {
|
||||
return aws.AwsIamUserResourceType
|
||||
}
|
||||
|
||||
func (e *IamUserEnumerator) Enumerate() ([]resource.Resource, error) {
|
||||
users, err := e.repository.ListAllUsers()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
results := make([]resource.Resource, len(users))
|
||||
|
||||
for _, user := range users {
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
awssdk.StringValue(user.UserName),
|
||||
map[string]interface{}{},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/service/iam"
|
||||
|
||||
"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 IamUserSupplier struct {
|
||||
reader terraform.ResourceReader
|
||||
deserializer *resource.Deserializer
|
||||
repository repository.IAMRepository
|
||||
runner *terraform.ParallelResourceReader
|
||||
}
|
||||
|
||||
func NewIamUserSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamUserSupplier {
|
||||
return &IamUserSupplier{
|
||||
provider,
|
||||
deserializer,
|
||||
repository,
|
||||
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IamUserSupplier) Resources() ([]resource.Resource, error) {
|
||||
users, err := s.repository.ListAllUsers()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamUserResourceType)
|
||||
}
|
||||
results := make([]cty.Value, 0)
|
||||
if len(users) > 0 {
|
||||
for _, user := range users {
|
||||
u := *user
|
||||
s.runner.Run(func() (cty.Value, error) {
|
||||
return s.readUser(&u)
|
||||
})
|
||||
}
|
||||
results, err = s.runner.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return s.deserializer.Deserialize(resourceaws.AwsIamUserResourceType, results)
|
||||
}
|
||||
|
||||
func (s *IamUserSupplier) readUser(user *iam.User) (cty.Value, error) {
|
||||
res, err := s.reader.ReadResource(
|
||||
terraform.ReadResourceArgs{
|
||||
Ty: resourceaws.AwsIamUserResourceType,
|
||||
ID: *user.UserName,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
logrus.Warnf("Error reading iam user %s[%s]: %+v", *user.UserName, resourceaws.AwsIamUserResourceType, err)
|
||||
return cty.NilVal, err
|
||||
}
|
||||
|
||||
return *res, nil
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"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/cloudskiff/driftctl/test/goldenfile"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/iam"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/cloudskiff/driftctl/test/mocks"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
)
|
||||
|
||||
func TestIamUserSupplier_Resources(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(repo *repository.MockIAMRepository)
|
||||
err error
|
||||
}{
|
||||
{
|
||||
test: "no iam user",
|
||||
dirName: "iam_user_empty",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").Return([]*iam.User{}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "iam multiples users",
|
||||
dirName: "iam_user_multiple",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").Return([]*iam.User{
|
||||
{
|
||||
UserName: aws.String("test-driftctl-0"),
|
||||
},
|
||||
{
|
||||
UserName: aws.String("test-driftctl-1"),
|
||||
},
|
||||
{
|
||||
UserName: aws.String("test-driftctl-2"),
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "cannot list iam user",
|
||||
dirName: "iam_user_empty",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamUserResourceType),
|
||||
},
|
||||
}
|
||||
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(NewIamUserSupplier(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 := &IamUserSupplier{
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -151,7 +151,9 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
remoteLibrary.AddEnumerator(NewLambdaEventSourceMappingEnumerator(lambdaRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsLambdaEventSourceMappingResourceType, common.NewGenericDetailsFetcher(aws.AwsLambdaEventSourceMappingResourceType, provider, deserializer))
|
||||
|
||||
supplierLibrary.AddSupplier(NewIamUserSupplier(provider, deserializer, iamRepository))
|
||||
remoteLibrary.AddEnumerator(NewIamUserEnumerator(iamRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsIamUserResourceType, common.NewGenericDetailsFetcher(aws.AwsIamUserResourceType, provider, deserializer))
|
||||
|
||||
supplierLibrary.AddSupplier(NewIamUserPolicySupplier(provider, deserializer, iamRepository))
|
||||
supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
|
||||
supplierLibrary.AddSupplier(NewIamAccessKeySupplier(provider, deserializer, iamRepository))
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -23,6 +23,104 @@ import (
|
|||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestIamUser(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(repo *repository.MockIAMRepository)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no iam user",
|
||||
dirName: "iam_user_empty",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").Return([]*iam.User{}, nil)
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
test: "iam multiples users",
|
||||
dirName: "iam_user_multiple",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").Return([]*iam.User{
|
||||
{
|
||||
UserName: aws.String("test-driftctl-0"),
|
||||
},
|
||||
{
|
||||
UserName: aws.String("test-driftctl-1"),
|
||||
},
|
||||
{
|
||||
UserName: aws.String("test-driftctl-2"),
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
test: "cannot list iam user",
|
||||
dirName: "iam_user_empty",
|
||||
mocks: func(repo *repository.MockIAMRepository) {
|
||||
repo.On("ListAllUsers").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.NewIamUserEnumerator(repo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(resourceaws.AwsIamUserResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsIamUserResourceType, 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.AwsIamUserResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIamPolicy(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
|
|
Loading…
Reference in New Issue