Merge branch 'main' into refact/route53_health_check_deepmode

main
Elie 2021-07-05 14:37:46 +02:00 committed by GitHub
commit ee1b4d3864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 137 additions and 343678 deletions

View File

@ -86,6 +86,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewKMSKeyEnumerator(kmsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsKmsKeyResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsKeyResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewKMSAliasEnumerator(kmsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsKmsAliasResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsAliasResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewVPCEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsVpcResourceType, common.NewGenericDetailsFetcher(aws.AwsVpcResourceType, provider, deserializer))
@ -126,7 +128,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewDynamoDBTableSupplier(provider, deserializer, dynamoDBRepository))
supplierLibrary.AddSupplier(NewCloudfrontDistributionSupplier(provider, deserializer, cloudfrontRepository))
supplierLibrary.AddSupplier(NewECRRepositorySupplier(provider, deserializer, ecrRepository))
supplierLibrary.AddSupplier(NewKMSAliasSupplier(provider, deserializer, kmsRepository))
supplierLibrary.AddSupplier(NewLambdaEventSourceMappingSupplier(provider, deserializer, lambdaRepository))
err = resourceSchemaRepository.Init(version, provider.Schema())

View File

@ -0,0 +1,46 @@
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 KMSAliasEnumerator struct {
repository repository.KMSRepository
factory resource.ResourceFactory
}
func NewKMSAliasEnumerator(repo repository.KMSRepository, factory resource.ResourceFactory) *KMSAliasEnumerator {
return &KMSAliasEnumerator{
repository: repo,
factory: factory,
}
}
func (e *KMSAliasEnumerator) SupportedType() resource.ResourceType {
return aws.AwsKmsAliasResourceType
}
func (e *KMSAliasEnumerator) Enumerate() ([]resource.Resource, error) {
aliases, err := e.repository.ListAllAliases()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(aliases))
for _, alias := range aliases {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*alias.AliasName,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,63 +0,0 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/kms"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type KMSAliasSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.KMSRepository
runner *terraform.ParallelResourceReader
}
func NewKMSAliasSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.KMSRepository) *KMSAliasSupplier {
return &KMSAliasSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *KMSAliasSupplier) Resources() ([]resource.Resource, error) {
aliases, err := s.repository.ListAllAliases()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsKmsAliasResourceType)
}
for _, alias := range aliases {
alias := alias
s.runner.Run(func() (cty.Value, error) {
return s.readAlias(alias)
})
}
retrieve, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(aws.AwsKmsAliasResourceType, retrieve)
}
func (s *KMSAliasSupplier) readAlias(alias *kms.AliasListEntry) (cty.Value, error) {
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: *alias.AliasName,
Ty: aws.AwsKmsAliasResourceType,
})
if err != nil {
logrus.Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,100 +0,0 @@
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
testresource "github.com/cloudskiff/driftctl/test/resource"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
testmocks "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
func TestKMSAliasSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockKMSRepository)
err error
}{
{
test: "no aliases",
dirName: "kms_alias_empty",
mocks: func(client *repository.MockKMSRepository) {
client.On("ListAllAliases").Return([]*kms.AliasListEntry{}, nil)
},
err: nil,
},
{
test: "multiple aliases",
dirName: "kms_alias_multiple",
mocks: func(client *repository.MockKMSRepository) {
client.On("ListAllAliases").Return([]*kms.AliasListEntry{
{AliasName: aws.String("alias/foo")},
{AliasName: aws.String("alias/bar")},
{AliasName: aws.String("alias/baz20210225124429210500000001")},
}, nil)
},
err: nil,
},
{
test: "cannot list aliases",
dirName: "kms_alias_empty",
mocks: func(client *repository.MockKMSRepository) {
client.On("ListAllAliases").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsKmsAliasResourceType),
},
}
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(NewKMSAliasSupplier(provider, deserializer, repository.NewKMSRepository(provider.session, cache.New(0))))
}
t.Run(c.test, func(tt *testing.T) {
fakeClient := repository.MockKMSRepository{}
c.mocks(&fakeClient)
provider := testmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &KMSAliasSupplier{
provider,
deserializer,
&fakeClient,
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, tt)
})
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -111,3 +111,92 @@ func TestKMSKey(t *testing.T) {
})
}
}
func TestKMSAlias(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockKMSRepository)
wantErr error
}{
{
test: "no aliases",
dirName: "aws_kms_alias_empty",
mocks: func(repository *repository.MockKMSRepository) {
repository.On("ListAllAliases").Return([]*kms.AliasListEntry{}, nil)
},
},
{
test: "multiple aliases",
dirName: "aws_kms_alias_multiple",
mocks: func(repository *repository.MockKMSRepository) {
repository.On("ListAllAliases").Return([]*kms.AliasListEntry{
{AliasName: awssdk.String("alias/foo")},
{AliasName: awssdk.String("alias/bar")},
{AliasName: awssdk.String("alias/baz20210225124429210500000001")},
}, nil)
},
},
{
test: "cannot list aliases",
dirName: "aws_kms_alias_list",
mocks: func(repository *repository.MockKMSRepository) {
repository.On("ListAllAliases").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsKmsAliasResourceType),
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
alerter := &mocks.AlerterInterface{}
for _, c := range tests {
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
fakeRepo := &repository.MockKMSRepository{}
c.mocks(fakeRepo)
var repo repository.KMSRepository = 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.NewKMSRepository(sess, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewKMSAliasEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsKmsAliasResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsKmsAliasResourceType, 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.AwsKmsAliasResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}