driftctl/enumeration/remote/aws_lambda_scanner_test.go

265 lines
9.2 KiB
Go
Raw Normal View History

2021-07-08 17:14:23 +00:00
package remote
import (
"testing"
"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"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
2022-07-06 08:44:42 +00:00
"github.com/snyk/driftctl/enumeration/remote/common"
remoteerr "github.com/snyk/driftctl/enumeration/remote/error"
terraform3 "github.com/snyk/driftctl/enumeration/terraform"
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"
"github.com/pkg/errors"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
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"
"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
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",
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",
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",
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",
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
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)
factory := terraform3.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}
providerLibrary := terraform3.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{}
fakeRepo := &repository.MockLambdaRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
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()
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
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
})
}
}
func TestScanLambdaEventSourceMapping(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(*repository.MockLambdaRepository, *mocks.AlerterInterface)
err error
}{
{
test: "no EventSourceMapping",
2021-12-14 15:35:35 +00:00
dirName: "aws_lambda_source_mapping_empty",
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
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",
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
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",
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
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",
mocks: func(repo *repository.MockLambdaRepository, alerter *mocks.AlerterInterface) {
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()
},
err: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
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}
providerLibrary := terraform3.NewProviderLibrary()
2022-07-06 08:44:42 +00:00
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
2021-08-20 13:15:51 +00:00
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockLambdaRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
var repo repository.LambdaRepository = 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.NewLambdaRepository(session, cache.New(0))
}
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))
testFilter := &enumeration.MockFilter{}
2021-07-26 09:55:59 +00:00
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
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)
})
}
}