Add aws_api_gateway_request_validator resource

main
William Beuil 2021-10-13 12:49:54 +02:00
parent 9e009ea590
commit f321a1df3b
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
16 changed files with 172232 additions and 5 deletions

View File

@ -157,6 +157,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{name: "Api Gateway resource", dirName: "api_gateway_resource", wantErr: false}, {name: "Api Gateway resource", dirName: "api_gateway_resource", wantErr: false},
{name: "Api Gateway domain name", dirName: "api_gateway_domain_name", wantErr: false}, {name: "Api Gateway domain name", dirName: "api_gateway_domain_name", wantErr: false},
{name: "Api Gateway vpc link", dirName: "api_gateway_vpc_link", wantErr: false}, {name: "Api Gateway vpc link", dirName: "api_gateway_vpc_link", wantErr: false},
{name: "Api Gateway request validator", dirName: "api_gateway_request_validator", wantErr: false},
{name: "AppAutoScaling Targets", dirName: "aws_appautoscaling_target", wantErr: false}, {name: "AppAutoScaling Targets", dirName: "aws_appautoscaling_target", wantErr: false},
{name: "network acl", dirName: "aws_network_acl", wantErr: false}, {name: "network acl", dirName: "aws_network_acl", wantErr: false},
{name: "network acl rule", dirName: "aws_network_acl_rule", wantErr: false}, {name: "network acl rule", dirName: "aws_network_acl_rule", wantErr: false},

View File

@ -0,0 +1,24 @@
[
{
"Id": "tak8bq",
"Type": "aws_api_gateway_request_validator",
"Attrs": {
"id": "tak8bq",
"name": "bar",
"rest_api_id": "zetudukz30",
"validate_request_body": true,
"validate_request_parameters": true
}
},
{
"Id": "ywlcuf",
"Type": "aws_api_gateway_request_validator",
"Attrs": {
"id": "ywlcuf",
"name": "foo",
"rest_api_id": "vryjzimtj1",
"validate_request_body": true,
"validate_request_parameters": true
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
{
"version": 4,
"terraform_version": "1.0.8",
"serial": 257,
"lineage": "85f5bee6-139e-8db2-ae5d-82aa82f62611",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_api_gateway_request_validator",
"name": "bar",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "tak8bq",
"name": "bar",
"rest_api_id": "zetudukz30",
"validate_request_body": true,
"validate_request_parameters": true
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_api_gateway_rest_api.bar"
]
}
]
},
{
"mode": "managed",
"type": "aws_api_gateway_request_validator",
"name": "foo",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "ywlcuf",
"name": "foo",
"rest_api_id": "vryjzimtj1",
"validate_request_body": true,
"validate_request_parameters": true
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_api_gateway_rest_api.foo"
]
}
]
}
]
}

View File

@ -716,3 +716,100 @@ func TestApiGatewayVpcLink(t *testing.T) {
}) })
} }
} }
func TestApiGatewayRequestValidator(t *testing.T) {
dummyError := errors.New("this is an error")
apis := []*apigateway.RestApi{
{Id: awssdk.String("vryjzimtj1")},
}
tests := []struct {
test string
mocks func(*repository.MockApiGatewayRepository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no api gateway request validators",
mocks: func(repo *repository.MockApiGatewayRepository, alerter *mocks.AlerterInterface) {
repo.On("ListAllRestApis").Return(apis, nil)
repo.On("ListAllRestApiRequestValidators", *apis[0].Id).Return([]*apigateway.UpdateRequestValidatorOutput{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiple api gateway request validators",
mocks: func(repo *repository.MockApiGatewayRepository, alerter *mocks.AlerterInterface) {
repo.On("ListAllRestApis").Return(apis, nil)
repo.On("ListAllRestApiRequestValidators", *apis[0].Id).Return([]*apigateway.UpdateRequestValidatorOutput{
{Id: awssdk.String("ywlcuf")},
{Id: awssdk.String("qmpbs8")},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "ywlcuf")
assert.Equal(t, got[0].ResourceType(), resourceaws.AwsApiGatewayRequestValidatorResourceType)
assert.Equal(t, got[1].ResourceId(), "qmpbs8")
assert.Equal(t, got[1].ResourceType(), resourceaws.AwsApiGatewayRequestValidatorResourceType)
},
},
{
test: "cannot list rest apis",
mocks: func(repo *repository.MockApiGatewayRepository, alerter *mocks.AlerterInterface) {
repo.On("ListAllRestApis").Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayRequestValidatorResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayRequestValidatorResourceType, resourceaws.AwsApiGatewayRestApiResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayRequestValidatorResourceType, resourceaws.AwsApiGatewayRestApiResourceType),
},
{
test: "cannot list api gateway request validators",
mocks: func(repo *repository.MockApiGatewayRepository, alerter *mocks.AlerterInterface) {
repo.On("ListAllRestApis").Return(apis, nil)
repo.On("ListAllRestApiRequestValidators", *apis[0].Id).Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayRequestValidatorResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayRequestValidatorResourceType, resourceaws.AwsApiGatewayRequestValidatorResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceaws.AwsApiGatewayRequestValidatorResourceType),
},
}
providerVersion := "3.19.0"
schemaRepository := testresource.InitFakeSchemaRepository("aws", providerVersion)
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockApiGatewayRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.ApiGatewayRepository = fakeRepo
remoteLibrary.AddEnumerator(aws.NewApiGatewayRequestValidatorEnumerator(repo, factory))
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
c.assertExpected(tt, got)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
})
}
}

View File

@ -0,0 +1,55 @@
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 ApiGatewayRequestValidatorEnumerator struct {
repository repository.ApiGatewayRepository
factory resource.ResourceFactory
}
func NewApiGatewayRequestValidatorEnumerator(repo repository.ApiGatewayRepository, factory resource.ResourceFactory) *ApiGatewayRequestValidatorEnumerator {
return &ApiGatewayRequestValidatorEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ApiGatewayRequestValidatorEnumerator) SupportedType() resource.ResourceType {
return aws.AwsApiGatewayRequestValidatorResourceType
}
func (e *ApiGatewayRequestValidatorEnumerator) Enumerate() ([]*resource.Resource, error) {
apis, err := e.repository.ListAllRestApis()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsApiGatewayRestApiResourceType)
}
results := make([]*resource.Resource, 0)
for _, api := range apis {
a := api
requestValidators, err := e.repository.ListAllRestApiRequestValidators(*a.Id)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, requestValidator := range requestValidators {
r := requestValidator
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*r.Id,
map[string]interface{}{},
),
)
}
}
return results, err
}

View File

@ -190,6 +190,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewApiGatewayResourceEnumerator(apigatewayRepository, factory)) remoteLibrary.AddEnumerator(NewApiGatewayResourceEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayDomainNameEnumerator(apigatewayRepository, factory)) remoteLibrary.AddEnumerator(NewApiGatewayDomainNameEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayVpcLinkEnumerator(apigatewayRepository, factory)) remoteLibrary.AddEnumerator(NewApiGatewayVpcLinkEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayRequestValidatorEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewAppAutoscalingTargetEnumerator(appAutoScalingRepository, factory)) remoteLibrary.AddEnumerator(NewAppAutoscalingTargetEnumerator(appAutoScalingRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, common.NewGenericDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, common.NewGenericDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, provider, deserializer))

View File

@ -18,6 +18,7 @@ type ApiGatewayRepository interface {
ListAllRestApiResources(string) ([]*apigateway.Resource, error) ListAllRestApiResources(string) ([]*apigateway.Resource, error)
ListAllDomainNames() ([]*apigateway.DomainName, error) ListAllDomainNames() ([]*apigateway.DomainName, error)
ListAllVpcLinks() ([]*apigateway.UpdateVpcLinkOutput, error) ListAllVpcLinks() ([]*apigateway.UpdateVpcLinkOutput, error)
ListAllRestApiRequestValidators(string) ([]*apigateway.UpdateRequestValidatorOutput, error)
} }
type apigatewayRepository struct { type apigatewayRepository struct {
@ -190,3 +191,21 @@ func (r *apigatewayRepository) ListAllVpcLinks() ([]*apigateway.UpdateVpcLinkOut
r.cache.Put("apigatewayListAllVpcLinks", vpcLinks) r.cache.Put("apigatewayListAllVpcLinks", vpcLinks)
return vpcLinks, nil return vpcLinks, nil
} }
func (r *apigatewayRepository) ListAllRestApiRequestValidators(apiId string) ([]*apigateway.UpdateRequestValidatorOutput, error) {
cacheKey := fmt.Sprintf("apigatewayListAllRestApiRequestValidators_api_%s", apiId)
if v := r.cache.Get(cacheKey); v != nil {
return v.([]*apigateway.UpdateRequestValidatorOutput), nil
}
input := &apigateway.GetRequestValidatorsInput{
RestApiId: &apiId,
}
resources, err := r.client.GetRequestValidators(input)
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}

View File

@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/apigateway" "github.com/aws/aws-sdk-go/service/apigateway"
"github.com/cloudskiff/driftctl/pkg/remote/cache" "github.com/cloudskiff/driftctl/pkg/remote/cache"
awstest "github.com/cloudskiff/driftctl/test/aws" awstest "github.com/cloudskiff/driftctl/test/aws"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
@ -552,3 +553,82 @@ func Test_apigatewayRepository_ListAllVpcLinks(t *testing.T) {
}) })
} }
} }
func Test_apigatewayRepository_ListAllRestApiRequestValidators(t *testing.T) {
api := &apigateway.RestApi{
Id: aws.String("restapi1"),
}
requestValidators := []*apigateway.UpdateRequestValidatorOutput{
{Id: aws.String("reqVal1")},
{Id: aws.String("reqVal2")},
{Id: aws.String("reqVal3")},
{Id: aws.String("reqVal4")},
}
remoteError := errors.New("remote error")
tests := []struct {
name string
mocks func(client *awstest.MockFakeApiGateway, store *cache.MockCache)
want []*apigateway.UpdateRequestValidatorOutput
wantErr error
}{
{
name: "list multiple rest api request validators",
mocks: func(client *awstest.MockFakeApiGateway, store *cache.MockCache) {
client.On("GetRequestValidators",
&apigateway.GetRequestValidatorsInput{
RestApiId: aws.String("restapi1"),
}).Return(&apigateway.GetRequestValidatorsOutput{Items: requestValidators}, nil).Once()
store.On("Get", "apigatewayListAllRestApiRequestValidators_api_restapi1").Return(nil).Times(1)
store.On("Put", "apigatewayListAllRestApiRequestValidators_api_restapi1", requestValidators).Return(false).Times(1)
},
want: requestValidators,
},
{
name: "should hit cache",
mocks: func(client *awstest.MockFakeApiGateway, store *cache.MockCache) {
store.On("Get", "apigatewayListAllRestApiRequestValidators_api_restapi1").Return(requestValidators).Times(1)
},
want: requestValidators,
},
{
name: "should return remote error",
mocks: func(client *awstest.MockFakeApiGateway, store *cache.MockCache) {
client.On("GetRequestValidators",
&apigateway.GetRequestValidatorsInput{
RestApiId: aws.String("restapi1"),
}).Return(nil, remoteError).Once()
store.On("Get", "apigatewayListAllRestApiRequestValidators_api_restapi1").Return(nil).Times(1)
},
wantErr: remoteError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cache.MockCache{}
client := &awstest.MockFakeApiGateway{}
tt.mocks(client, store)
r := &apigatewayRepository{
client: client,
cache: store,
}
got, err := r.ListAllRestApiRequestValidators(*api.Id)
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
store.AssertExpectations(t)
client.AssertExpectations(t)
})
}
}

View File

@ -104,6 +104,29 @@ func (_m *MockApiGatewayRepository) ListAllRestApiAuthorizers(_a0 string) ([]*ap
return r0, r1 return r0, r1
} }
// ListAllRestApiRequestValidators provides a mock function with given fields: _a0
func (_m *MockApiGatewayRepository) ListAllRestApiRequestValidators(_a0 string) ([]*apigateway.UpdateRequestValidatorOutput, error) {
ret := _m.Called(_a0)
var r0 []*apigateway.UpdateRequestValidatorOutput
if rf, ok := ret.Get(0).(func(string) []*apigateway.UpdateRequestValidatorOutput); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigateway.UpdateRequestValidatorOutput)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllRestApiResources provides a mock function with given fields: _a0 // ListAllRestApiResources provides a mock function with given fields: _a0
func (_m *MockApiGatewayRepository) ListAllRestApiResources(_a0 string) ([]*apigateway.Resource, error) { func (_m *MockApiGatewayRepository) ListAllRestApiResources(_a0 string) ([]*apigateway.Resource, error) {
ret := _m.Called(_a0) ret := _m.Called(_a0)

View File

@ -0,0 +1,3 @@
package aws
const AwsApiGatewayRequestValidatorResourceType = "aws_api_gateway_request_validator"

View File

@ -0,0 +1,30 @@
package aws_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Aws_ApiGatewayRequestValidator(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_api_gateway_request_validator"},
Args: []string{"scan"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(3)
},
},
},
})
}

View File

@ -0,0 +1,2 @@
*
!aws_api_gateway_request_validator

View File

@ -0,0 +1,20 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.19.0"
constraints = "3.19.0"
hashes = [
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",
"zh:703f6da36b1b5f3497baa38fccaa7765fb8a2b6440344e4c97172516b49437dd",
"zh:770855565462abadbbddd98cb357d2f1a8f30f68a358cb37cbd5c072cb15b377",
"zh:8008db43149fe4345301f81e15e6d9ddb47aa5e7a31648f9b290af96ad86e92a",
"zh:8cdd27d375da6dcb7687f1fed126b7c04efce1671066802ee876dbbc9c66ec79",
"zh:be22ae185005690d1a017c1b909e0d80ab567e239b4f06ecacdba85080667c1c",
"zh:d2d02e72dbd80f607636cd6237a6c862897caabc635c7b50c0cb243d11246723",
"zh:d8f125b66a1eda2555c0f9bbdf12036a5f8d073499a22ca9e4812b68067fea31",
"zh:f5a98024c64d5d2973ff15b093725a074c0cb4afde07ef32c542e69f17ac90bc",
]
}

View File

@ -0,0 +1,59 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.19.0"
}
}
resource "aws_api_gateway_rest_api" "foo" {
name = "foo"
description = "This is foo API"
}
resource "aws_api_gateway_request_validator" "foo" {
name = "foo"
rest_api_id = aws_api_gateway_rest_api.foo.id
validate_request_body = true
validate_request_parameters = true
}
resource "aws_api_gateway_request_validator" "baz" {
name = "baz"
rest_api_id = aws_api_gateway_rest_api.foo.id
validate_request_body = false
validate_request_parameters = false
}
resource "aws_api_gateway_rest_api" "bar" {
name = "bar"
description = "This is bar API"
body = jsonencode({
openapi = "3.0.1"
info = {
title = "example"
version = "1.0"
}
paths = {
"/path1" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "HTTP_PROXY"
uri = "https://ip-ranges.amazonaws.com/ip-ranges.json"
}
}
}
}
})
}
resource "aws_api_gateway_request_validator" "bar" {
name = "bar"
rest_api_id = aws_api_gateway_rest_api.bar.id
validate_request_body = true
validate_request_parameters = true
}

View File

@ -114,6 +114,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"aws_api_gateway_resource": {}, "aws_api_gateway_resource": {},
"aws_api_gateway_domain_name": {}, "aws_api_gateway_domain_name": {},
"aws_api_gateway_vpc_link": {}, "aws_api_gateway_vpc_link": {},
"aws_api_gateway_request_validator": {},
"aws_appautoscaling_target": {}, "aws_appautoscaling_target": {},
"aws_rds_cluster_instance": {children: []ResourceType{ "aws_rds_cluster_instance": {children: []ResourceType{
"aws_db_instance", "aws_db_instance",