2021-07-08 17:14:23 +00:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration"
|
|
|
|
"github.com/snyk/driftctl/enumeration/remote/alerts"
|
2022-07-06 14:36:32 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/aws"
|
2022-07-06 08:39:33 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/cache"
|
2022-07-06 08:44:42 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/common"
|
2022-06-28 07:23:29 +00:00
|
|
|
remoteerr "github.com/snyk/driftctl/enumeration/remote/error"
|
2022-07-06 14:38:03 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/terraform"
|
2022-06-28 07:23:29 +00:00
|
|
|
|
2021-07-08 17:14:23 +00:00
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
2021-09-06 15:00:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/mocks"
|
2022-06-28 07:23:29 +00:00
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
testresource "github.com/snyk/driftctl/test/resource"
|
|
|
|
terraform2 "github.com/snyk/driftctl/test/terraform"
|
2021-07-08 17:14:23 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
resourceaws "github.com/snyk/driftctl/enumeration/resource/aws"
|
2021-07-08 17:14:23 +00:00
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/test"
|
|
|
|
"github.com/snyk/driftctl/test/goldenfile"
|
2021-07-08 17:14:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScanLambdaFunction(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks func(*repository.MockLambdaRepository, *mocks.AlerterInterface)
|
2021-07-08 17:14:23 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no lambda functions",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_function_empty",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:14:23 +00:00
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "with lambda functions",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_function_multiple",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:14:23 +00:00
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{
|
|
|
|
{
|
|
|
|
FunctionName: awssdk.String("foo"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FunctionName: awssdk.String("bar"),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "One lambda with signing",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_function_signed",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:14:23 +00:00
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{
|
|
|
|
{
|
|
|
|
FunctionName: awssdk.String("foo"),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "cannot list lambda functions",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_function_empty",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-09-06 15:00:38 +00:00
|
|
|
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
|
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{}, awsError)
|
2021-08-20 13:15:51 +00:00
|
|
|
|
2022-07-06 08:44:42 +00:00
|
|
|
alerter.On("SendAlert", resourceaws.AwsLambdaFunctionResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsLambdaFunctionResourceType, resourceaws.AwsLambdaFunctionResourceType), alerts.EnumerationPhase)).Return()
|
2021-07-08 17:14:23 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
|
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
2022-07-06 14:38:03 +00:00
|
|
|
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
2021-07-08 17:14:23 +00:00
|
|
|
deserializer := resource.NewDeserializer(factory)
|
|
|
|
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
|
|
|
|
|
|
|
session := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
scanOptions := ScannerOptions{Deep: true}
|
2022-07-06 14:38:03 +00:00
|
|
|
providerLibrary := terraform.NewProviderLibrary()
|
2022-07-06 08:44:42 +00:00
|
|
|
remoteLibrary := common.NewRemoteLibrary()
|
2021-07-08 17:14:23 +00:00
|
|
|
|
|
|
|
// Initialize mocks
|
2021-08-20 13:15:51 +00:00
|
|
|
alerter := &mocks.AlerterInterface{}
|
2022-07-06 08:39:33 +00:00
|
|
|
fakeRepo := &repository.MockLambdaRepository{}
|
2021-08-20 13:15:51 +00:00
|
|
|
c.mocks(fakeRepo, alerter)
|
|
|
|
|
2022-07-06 08:39:33 +00:00
|
|
|
var repo repository.LambdaRepository = fakeRepo
|
2021-07-08 17:14:23 +00:00
|
|
|
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()
|
2022-07-06 08:39:33 +00:00
|
|
|
repo = repository.NewLambdaRepository(session, cache.New(0))
|
2021-07-08 17:14:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:36:32 +00:00
|
|
|
remoteLibrary.AddEnumerator(aws.NewLambdaFunctionEnumerator(repo, factory))
|
2022-07-06 08:44:42 +00:00
|
|
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsLambdaFunctionResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsLambdaFunctionResourceType, provider, deserializer))
|
2021-07-08 17:14:23 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
testFilter := &enumeration.MockFilter{}
|
2021-07-26 09:55:59 +00:00
|
|
|
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
|
|
|
|
|
|
|
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
2021-07-08 17:14:23 +00:00
|
|
|
got, err := s.Resources()
|
|
|
|
assert.Equal(tt, c.err, err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test.TestAgainstGoldenFile(got, resourceaws.AwsLambdaFunctionResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
2021-08-20 13:15:51 +00:00
|
|
|
alerter.AssertExpectations(tt)
|
|
|
|
fakeRepo.AssertExpectations(tt)
|
2021-07-08 17:14:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 17:27:38 +00:00
|
|
|
|
|
|
|
func TestScanLambdaEventSourceMapping(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks func(*repository.MockLambdaRepository, *mocks.AlerterInterface)
|
2021-07-08 17:27:38 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no EventSourceMapping",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_source_mapping_empty",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:27:38 +00:00
|
|
|
repo.On("ListAllLambdaEventSourceMappings").Return([]*lambda.EventSourceMappingConfiguration{}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "with 2 sqs EventSourceMapping",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_source_mapping_sqs_multiple",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:27:38 +00:00
|
|
|
repo.On("ListAllLambdaEventSourceMappings").Return([]*lambda.EventSourceMappingConfiguration{
|
|
|
|
{
|
|
|
|
UUID: awssdk.String("13ff66f8-37eb-4ad6-a0a8-594fea72df4f"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UUID: awssdk.String("4ad7e2b3-79e9-4713-9d9d-5af2c01d9058"),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "with dynamo EventSourceMapping",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_source_mapping_dynamo_multiple",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-07-08 17:27:38 +00:00
|
|
|
repo.On("ListAllLambdaEventSourceMappings").Return([]*lambda.EventSourceMappingConfiguration{
|
|
|
|
{
|
|
|
|
UUID: awssdk.String("1aa9c4a0-060b-41c1-a9ae-dc304ebcdb00"),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "cannot list lambda functions",
|
2021-12-14 15:35:35 +00:00
|
|
|
dirName: "aws_lambda_function_empty",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
|
2021-09-06 15:00:38 +00:00
|
|
|
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
|
|
|
|
repo.On("ListAllLambdaEventSourceMappings").Return([]*lambda.EventSourceMappingConfiguration{}, awsError)
|
2021-08-20 13:15:51 +00:00
|
|
|
|
2022-07-06 08:44:42 +00:00
|
|
|
alerter.On("SendAlert", resourceaws.AwsLambdaEventSourceMappingResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsLambdaEventSourceMappingResourceType, resourceaws.AwsLambdaEventSourceMappingResourceType), alerts.EnumerationPhase)).Return()
|
2021-07-08 17:27:38 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
|
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
2022-07-06 14:38:03 +00:00
|
|
|
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
2021-07-08 17:27:38 +00:00
|
|
|
deserializer := resource.NewDeserializer(factory)
|
|
|
|
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
|
|
|
|
|
|
|
session := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
scanOptions := ScannerOptions{Deep: true}
|
2022-07-06 14:38:03 +00:00
|
|
|
providerLibrary := terraform.NewProviderLibrary()
|
2022-07-06 08:44:42 +00:00
|
|
|
remoteLibrary := common.NewRemoteLibrary()
|
2021-07-08 17:27:38 +00:00
|
|
|
|
|
|
|
// Initialize mocks
|
2021-08-20 13:15:51 +00:00
|
|
|
alerter := &mocks.AlerterInterface{}
|
2022-07-06 08:39:33 +00:00
|
|
|
fakeRepo := &repository.MockLambdaRepository{}
|
2021-08-20 13:15:51 +00:00
|
|
|
c.mocks(fakeRepo, alerter)
|
|
|
|
|
2022-07-06 08:39:33 +00:00
|
|
|
var repo repository.LambdaRepository = fakeRepo
|
2021-07-08 17:27:38 +00:00
|
|
|
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()
|
2022-07-06 08:39:33 +00:00
|
|
|
repo = repository.NewLambdaRepository(session, cache.New(0))
|
2021-07-08 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:36:32 +00:00
|
|
|
remoteLibrary.AddEnumerator(aws.NewLambdaEventSourceMappingEnumerator(repo, factory))
|
2022-07-06 08:44:42 +00:00
|
|
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsLambdaEventSourceMappingResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsLambdaEventSourceMappingResourceType, provider, deserializer))
|
2021-07-08 17:27:38 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
testFilter := &enumeration.MockFilter{}
|
2021-07-26 09:55:59 +00:00
|
|
|
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
|
|
|
|
|
|
|
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
2021-07-08 17:27:38 +00:00
|
|
|
got, err := s.Resources()
|
|
|
|
assert.Equal(tt, c.err, err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test.TestAgainstGoldenFile(got, resourceaws.AwsLambdaEventSourceMappingResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
2021-08-20 13:15:51 +00:00
|
|
|
alerter.AssertExpectations(tt)
|
|
|
|
fakeRepo.AssertExpectations(tt)
|
2021-07-08 17:27:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|