commit
2b74785da5
|
@ -0,0 +1,49 @@
|
||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||||
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||||
|
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EC2KeyPairEnumerator struct {
|
||||||
|
repository repository.EC2Repository
|
||||||
|
factory resource.ResourceFactory
|
||||||
|
providerConfig tf.TerraformProviderConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEC2KeyPairEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *EC2KeyPairEnumerator {
|
||||||
|
return &EC2KeyPairEnumerator{
|
||||||
|
repository: repo,
|
||||||
|
factory: factory,
|
||||||
|
providerConfig: providerConfig,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EC2KeyPairEnumerator) SupportedType() resource.ResourceType {
|
||||||
|
return aws.AwsKeyPairResourceType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EC2KeyPairEnumerator) Enumerate() ([]resource.Resource, error) {
|
||||||
|
keyPairs, err := e.repository.ListAllKeyPairs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]resource.Resource, len(keyPairs))
|
||||||
|
|
||||||
|
for _, keyPair := range keyPairs {
|
||||||
|
results = append(
|
||||||
|
results,
|
||||||
|
e.factory.CreateAbstractResource(
|
||||||
|
string(e.SupportedType()),
|
||||||
|
*keyPair.KeyName,
|
||||||
|
map[string]interface{}{},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, err
|
||||||
|
}
|
|
@ -1,64 +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/aws/aws-sdk-go/aws"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/zclconf/go-cty/cty"
|
|
||||||
)
|
|
||||||
|
|
||||||
type EC2KeyPairSupplier struct {
|
|
||||||
reader terraform.ResourceReader
|
|
||||||
deserializer *resource.Deserializer
|
|
||||||
repository repository.EC2Repository
|
|
||||||
runner *terraform.ParallelResourceReader
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEC2KeyPairSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *EC2KeyPairSupplier {
|
|
||||||
return &EC2KeyPairSupplier{
|
|
||||||
provider,
|
|
||||||
deserializer,
|
|
||||||
repository,
|
|
||||||
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *EC2KeyPairSupplier) Resources() ([]resource.Resource, error) {
|
|
||||||
keyPairs, err := s.repository.ListAllKeyPairs()
|
|
||||||
if err != nil {
|
|
||||||
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsKeyPairResourceType)
|
|
||||||
}
|
|
||||||
results := make([]cty.Value, 0)
|
|
||||||
if len(keyPairs) > 0 {
|
|
||||||
for _, kp := range keyPairs {
|
|
||||||
name := aws.StringValue(kp.KeyName)
|
|
||||||
s.runner.Run(func() (cty.Value, error) {
|
|
||||||
return s.readKeyPair(name)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
results, err = s.runner.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s.deserializer.Deserialize(resourceaws.AwsKeyPairResourceType, results)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *EC2KeyPairSupplier) readKeyPair(name string) (cty.Value, error) {
|
|
||||||
resKp, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
||||||
Ty: resourceaws.AwsKeyPairResourceType,
|
|
||||||
ID: name,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logrus.Warnf("Error reading key pair %s: %+v", name, err)
|
|
||||||
return cty.NilVal, err
|
|
||||||
}
|
|
||||||
return *resKp, nil
|
|
||||||
}
|
|
|
@ -1,103 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
|
||||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
|
||||||
|
|
||||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/parallel"
|
|
||||||
|
|
||||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
||||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
||||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
||||||
"github.com/cloudskiff/driftctl/test"
|
|
||||||
"github.com/cloudskiff/driftctl/test/mocks"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestEC2KeyPairSupplier_Resources(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
test string
|
|
||||||
dirName string
|
|
||||||
mock func(mock *repository.MockEC2Repository)
|
|
||||||
err error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
test: "no key pairs",
|
|
||||||
dirName: "ec2_key_pair_empty",
|
|
||||||
mock: func(mock *repository.MockEC2Repository) {
|
|
||||||
mock.On("ListAllKeyPairs").Return([]*ec2.KeyPairInfo{}, nil)
|
|
||||||
},
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: "with key pairs",
|
|
||||||
dirName: "ec2_key_pair_multiple",
|
|
||||||
mock: func(mock *repository.MockEC2Repository) {
|
|
||||||
mock.On("ListAllKeyPairs").Return([]*ec2.KeyPairInfo{
|
|
||||||
{
|
|
||||||
KeyName: aws.String("test"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
KeyName: aws.String("bar"),
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
},
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: "cannot list key pairs",
|
|
||||||
dirName: "ec2_key_pair_empty",
|
|
||||||
mock: func(mock *repository.MockEC2Repository) {
|
|
||||||
mock.On("ListAllKeyPairs").Return([]*ec2.KeyPairInfo{}, awserr.NewRequestFailure(nil, 403, ""))
|
|
||||||
},
|
|
||||||
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsKeyPairResourceType),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
shouldUpdate := tt.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(NewEC2KeyPairSupplier(provider, deserializer, repository.NewEC2Repository(provider.session, cache.New(0))))
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run(tt.test, func(t *testing.T) {
|
|
||||||
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
|
||||||
client := &repository.MockEC2Repository{}
|
|
||||||
tt.mock(client)
|
|
||||||
s := &EC2KeyPairSupplier{
|
|
||||||
provider,
|
|
||||||
deserializer,
|
|
||||||
client,
|
|
||||||
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
|
||||||
}
|
|
||||||
got, err := s.Resources()
|
|
||||||
assert.Equal(t, tt.err, err)
|
|
||||||
|
|
||||||
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -76,6 +76,8 @@ func Init(version string, alerter *alerter.Alerter,
|
||||||
remoteLibrary.AddDetailsFetcher(aws.AwsEipResourceType, common.NewGenericDetailsFetcher(aws.AwsEipResourceType, provider, deserializer))
|
remoteLibrary.AddDetailsFetcher(aws.AwsEipResourceType, common.NewGenericDetailsFetcher(aws.AwsEipResourceType, provider, deserializer))
|
||||||
remoteLibrary.AddEnumerator(NewEC2AmiEnumerator(ec2repository, factory, provider.Config))
|
remoteLibrary.AddEnumerator(NewEC2AmiEnumerator(ec2repository, factory, provider.Config))
|
||||||
remoteLibrary.AddDetailsFetcher(aws.AwsAmiResourceType, common.NewGenericDetailsFetcher(aws.AwsAmiResourceType, provider, deserializer))
|
remoteLibrary.AddDetailsFetcher(aws.AwsAmiResourceType, common.NewGenericDetailsFetcher(aws.AwsAmiResourceType, provider, deserializer))
|
||||||
|
remoteLibrary.AddEnumerator(NewEC2KeyPairEnumerator(ec2repository, factory, provider.Config))
|
||||||
|
remoteLibrary.AddDetailsFetcher(aws.AwsKeyPairResourceType, common.NewGenericDetailsFetcher(aws.AwsKeyPairResourceType, provider, deserializer))
|
||||||
|
|
||||||
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
|
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
|
||||||
supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer))
|
supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer))
|
||||||
|
@ -83,7 +85,6 @@ func Init(version string, alerter *alerter.Alerter,
|
||||||
supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository))
|
supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository))
|
||||||
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))
|
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))
|
||||||
supplierLibrary.AddSupplier(NewEC2InstanceSupplier(provider, deserializer, ec2repository))
|
supplierLibrary.AddSupplier(NewEC2InstanceSupplier(provider, deserializer, ec2repository))
|
||||||
supplierLibrary.AddSupplier(NewEC2KeyPairSupplier(provider, deserializer, ec2repository))
|
|
||||||
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
|
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
|
||||||
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
|
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
|
||||||
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, rdsRepository))
|
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, rdsRepository))
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -387,3 +387,94 @@ func TestEC2Ami(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEC2KeyPair(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
test string
|
||||||
|
dirName string
|
||||||
|
mocks func(repository *repository.MockEC2Repository)
|
||||||
|
wantErr error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
test: "no key pairs",
|
||||||
|
dirName: "aws_ec2_key_pair_empty",
|
||||||
|
mocks: func(repository *repository.MockEC2Repository) {
|
||||||
|
repository.On("ListAllKeyPairs").Return([]*ec2.KeyPairInfo{}, nil)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: "multiple key pairs",
|
||||||
|
dirName: "aws_ec2_key_pair_multiple",
|
||||||
|
mocks: func(repository *repository.MockEC2Repository) {
|
||||||
|
repository.On("ListAllKeyPairs").Return([]*ec2.KeyPairInfo{
|
||||||
|
{KeyName: awssdk.String("test")},
|
||||||
|
{KeyName: awssdk.String("bar")},
|
||||||
|
}, nil)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: "cannot list key pairs",
|
||||||
|
dirName: "aws_ec2_key_pair_list",
|
||||||
|
mocks: func(repository *repository.MockEC2Repository) {
|
||||||
|
repository.On("ListAllKeyPairs").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||||
|
},
|
||||||
|
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsKeyPairResourceType),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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.MockEC2Repository{}
|
||||||
|
c.mocks(fakeRepo)
|
||||||
|
var repo repository.EC2Repository = 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.NewEC2Repository(sess, cache.New(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteLibrary.AddEnumerator(aws.NewEC2KeyPairEnumerator(repo, factory, tf.TerraformProviderConfig{
|
||||||
|
Name: "test",
|
||||||
|
DefaultAlias: "eu-west-3",
|
||||||
|
}))
|
||||||
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsKeyPairResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsKeyPairResourceType, 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.AwsKeyPairResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue