refactor: split iam access key supplier

main
sundowndev 2021-07-12 11:50:10 +02:00
parent 8c7ad6628c
commit 169f1c37a5
15 changed files with 219 additions and 245263 deletions

View File

@ -0,0 +1,38 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type IamAccessKeyDetailsFetcher struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
}
func NewIamAccessKeyDetailsFetcher(provider terraform.ResourceReader, deserializer *resource.Deserializer) *IamAccessKeyDetailsFetcher {
return &IamAccessKeyDetailsFetcher{
reader: provider,
deserializer: deserializer,
}
}
func (r *IamAccessKeyDetailsFetcher) ReadDetails(res resource.Resource) (resource.Resource, error) {
ctyVal, err := r.reader.ReadResource(terraform.ReadResourceArgs{
Ty: aws.AwsIamAccessKeyResourceType,
ID: res.TerraformId(),
Attributes: map[string]string{
"user": *res.Attributes().GetString("user"),
},
})
if err != nil {
return nil, err
}
deserializedRes, err := r.deserializer.DeserializeOne(aws.AwsIamAccessKeyResourceType, *ctyVal)
if err != nil {
return nil, err
}
return deserializedRes, nil
}

View File

@ -0,0 +1,52 @@
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"
)
type IamAccessKeyEnumerator struct {
repository repository.IAMRepository
factory resource.ResourceFactory
}
func NewIamAccessKeyEnumerator(repository repository.IAMRepository, factory resource.ResourceFactory) *IamAccessKeyEnumerator {
return &IamAccessKeyEnumerator{
repository,
factory,
}
}
func (e *IamAccessKeyEnumerator) SupportedType() resource.ResourceType {
return resourceaws.AwsIamAccessKeyResourceType
}
func (e *IamAccessKeyEnumerator) Enumerate() ([]resource.Resource, error) {
users, err := e.repository.ListAllUsers()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, string(e.SupportedType()), resourceaws.AwsIamUserResourceType)
}
keys, err := e.repository.ListAllAccessKeys(users)
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamAccessKeyResourceType)
}
results := make([]resource.Resource, 0)
for _, key := range keys {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*key.AccessKeyId,
map[string]interface{}{
"user": *key.UserName,
},
),
)
}
return results, nil
}

View File

@ -1,74 +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 IamAccessKeySupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.IAMRepository
runner *terraform.ParallelResourceReader
}
func NewIamAccessKeySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamAccessKeySupplier {
return &IamAccessKeySupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *IamAccessKeySupplier) Resources() ([]resource.Resource, error) {
users, err := s.repository.ListAllUsers()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamAccessKeyResourceType, resourceaws.AwsIamUserResourceType)
}
keys, err := s.repository.ListAllAccessKeys(users)
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamAccessKeyResourceType)
}
results := make([]cty.Value, 0)
if len(keys) > 0 {
for _, key := range keys {
k := *key
s.runner.Run(func() (cty.Value, error) {
return s.readAccessKey(&k)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
return s.deserializer.Deserialize(resourceaws.AwsIamAccessKeyResourceType, results)
}
func (s *IamAccessKeySupplier) readAccessKey(key *iam.AccessKeyMetadata) (cty.Value, error) {
res, err := s.reader.ReadResource(
terraform.ReadResourceArgs{
Ty: resourceaws.AwsIamAccessKeyResourceType,
ID: *key.AccessKeyId,
Attributes: map[string]string{
"user": *key.UserName,
},
},
)
if err != nil {
logrus.Warnf("Error reading iam access key %s[%s]: %+v", *key.AccessKeyId, resourceaws.AwsIamAccessKeyResourceType, err)
return cty.NilVal, err
}
return *res, nil
}

View File

@ -1,142 +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 TestIamAccessKeySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(repo *repository.MockIAMRepository)
err error
}{
{
test: "no iam access_key",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
users := []*iam.User{
{
UserName: aws.String("test-driftctl"),
},
}
repo.On("ListAllUsers").Return(users, nil)
repo.On("ListAllAccessKeys", users).Return([]*iam.AccessKeyMetadata{}, nil)
},
err: nil,
},
{
test: "iam multiples keys for multiples users",
dirName: "iam_access_key_multiple",
mocks: func(repo *repository.MockIAMRepository) {
users := []*iam.User{
{
UserName: aws.String("test-driftctl"),
},
}
repo.On("ListAllUsers").Return(users, nil)
repo.On("ListAllAccessKeys", users).Return([]*iam.AccessKeyMetadata{
{
AccessKeyId: aws.String("AKIA5QYBVVD223VWU32A"),
UserName: aws.String("test-driftctl"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD2QYI36UZP"),
UserName: aws.String("test-driftctl"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD26EJME25D"),
UserName: aws.String("test-driftctl2"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD2SWDFVVMG"),
UserName: aws.String("test-driftctl2"),
},
}, nil)
},
err: nil,
},
{
test: "Cannot list iam user",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllUsers").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamAccessKeyResourceType, resourceaws.AwsIamUserResourceType),
},
{
test: "Cannot list iam access_key",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllUsers").Once().Return([]*iam.User{}, nil)
repo.On("ListAllAccessKeys", mock.Anything).Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamAccessKeyResourceType),
},
}
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(NewIamAccessKeySupplier(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 := &IamAccessKeySupplier{
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)
})
}
}

View File

@ -159,12 +159,13 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsIamUserPolicyResourceType, common.NewGenericDetailsFetcher(aws.AwsIamUserPolicyResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewIamRoleEnumerator(iamRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsIamRoleResourceType, common.NewGenericDetailsFetcher(aws.AwsIamRoleResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewIamAccessKeyEnumerator(iamRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsIamAccessKeyResourceType, NewIamAccessKeyDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewECRRepositoryEnumerator(ecrRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsEcrRepositoryResourceType, common.NewGenericDetailsFetcher(aws.AwsEcrRepositoryResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewIamAccessKeySupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewIamRolePolicySupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewIamRolePolicyAttachmentSupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -456,3 +456,130 @@ func TestIamRole(t *testing.T) {
})
}
}
func TestIamAccessKey(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(repo *repository.MockIAMRepository)
wantErr error
}{
{
test: "no iam access_key",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
users := []*iam.User{
{
UserName: aws.String("test-driftctl"),
},
}
repo.On("ListAllUsers").Return(users, nil)
repo.On("ListAllAccessKeys", users).Return([]*iam.AccessKeyMetadata{}, nil)
},
wantErr: nil,
},
{
test: "iam multiples keys for multiples users",
dirName: "iam_access_key_multiple",
mocks: func(repo *repository.MockIAMRepository) {
users := []*iam.User{
{
UserName: aws.String("test-driftctl"),
},
}
repo.On("ListAllUsers").Return(users, nil)
repo.On("ListAllAccessKeys", users).Return([]*iam.AccessKeyMetadata{
{
AccessKeyId: aws.String("AKIA5QYBVVD223VWU32A"),
UserName: aws.String("test-driftctl"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD2QYI36UZP"),
UserName: aws.String("test-driftctl"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD26EJME25D"),
UserName: aws.String("test-driftctl2"),
},
{
AccessKeyId: aws.String("AKIA5QYBVVD2SWDFVVMG"),
UserName: aws.String("test-driftctl2"),
},
}, nil)
},
wantErr: nil,
},
{
test: "Cannot list iam user",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllUsers").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: nil,
},
{
test: "Cannot list iam access_key",
dirName: "iam_access_key_empty",
mocks: func(repo *repository.MockIAMRepository) {
repo.On("ListAllUsers").Once().Return([]*iam.User{}, nil)
repo.On("ListAllAccessKeys", 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.NewIamAccessKeyEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsIamAccessKeyResourceType, remoteaws.NewIamAccessKeyDetailsFetcher(provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, c.wantErr, err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsIamAccessKeyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}