Merge pull request #1316 from snyk/feat/add_aws_apigatewayv2_model

Add aws_apigatewayv2_model
main
Elie 2022-01-12 15:29:05 +01:00 committed by GitHub
commit a97b448f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 172170 additions and 24 deletions

View File

@ -173,6 +173,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{name: "Api Gateway integration response", dirName: "aws_api_gateway_integration_response", wantErr: false},
{name: "Api Gateway V2 authorizer", dirName: "aws_apigatewayv2_authorizer", wantErr: false},
{name: "Api Gateway V2 integration", dirName: "aws_apigatewayv2_integration", wantErr: false},
{name: "Api Gateway V2 model", dirName: "aws_apigatewayv2_model", wantErr: false},
{name: "AppAutoScaling Targets", dirName: "aws_appautoscaling_target", wantErr: false},
{name: "network acl", dirName: "aws_network_acl", wantErr: false},
{name: "network acl rule", dirName: "aws_network_acl_rule", wantErr: false},

View File

@ -0,0 +1,14 @@
[
{
"Id": "vdw6up",
"Type": "aws_apigatewayv2_model",
"Attrs": {
"api_id": "ci51xtkpsg",
"content_type": "application/json",
"description": "",
"id": "vdw6up",
"name": "example",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"title\": \"ExampleModel\",\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" }\n }\n}\n"
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,31 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.47.0"
}
}
resource "aws_apigatewayv2_api" "example" {
name = "example-http-api"
protocol_type = "WEBSOCKET"
}
resource "aws_apigatewayv2_model" "example" {
api_id = aws_apigatewayv2_api.example.id
content_type = "application/json"
name = "example"
schema = <<EOF
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ExampleModel",
"type": "object",
"properties": {
"id": { "type": "string" }
}
}
EOF
}

View File

@ -0,0 +1,33 @@
{
"version": 4,
"terraform_version": "1.1.0",
"serial": 120,
"lineage": "0738cef4-9d69-9ccc-aebd-1177cafa0aa9",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_apigatewayv2_model",
"name": "example",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"api_id": "ci51xtkpsg",
"content_type": "application/json",
"description": "",
"id": "vdw6up",
"name": "example",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"title\": \"ExampleModel\",\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" }\n }\n}\n"
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_apigatewayv2_api.example"
]
}
]
}
]
}

View File

@ -0,0 +1,52 @@
package aws
import (
"github.com/snyk/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/pkg/remote/error"
"github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/pkg/resource/aws"
)
type ApiGatewayV2ModelEnumerator struct {
repository repository.ApiGatewayV2Repository
factory resource.ResourceFactory
}
func NewApiGatewayV2ModelEnumerator(repo repository.ApiGatewayV2Repository, factory resource.ResourceFactory) *ApiGatewayV2ModelEnumerator {
return &ApiGatewayV2ModelEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ApiGatewayV2ModelEnumerator) SupportedType() resource.ResourceType {
return aws.AwsApiGatewayV2ModelResourceType
}
func (e *ApiGatewayV2ModelEnumerator) Enumerate() ([]*resource.Resource, error) {
apis, err := e.repository.ListAllApis()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsApiGatewayV2ApiResourceType)
}
var results []*resource.Resource
for _, api := range apis {
models, err := e.repository.ListAllApiModels(*api.ApiId)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, model := range models {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*model.ModelId,
map[string]interface{}{
"name": *model.Name,
},
),
)
}
}
return results, err
}

View File

@ -208,6 +208,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewApiGatewayV2VpcLinkEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2AuthorizerEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2IntegrationEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2ModelEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewAppAutoscalingTargetEnumerator(appAutoScalingRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, common.NewGenericDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, provider, deserializer))

View File

@ -15,6 +15,7 @@ type ApiGatewayV2Repository interface {
ListAllVpcLinks() ([]*apigatewayv2.VpcLink, error)
ListAllApiAuthorizers(string) ([]*apigatewayv2.Authorizer, error)
ListAllApiIntegrations(string) ([]*apigatewayv2.Integration, error)
ListAllApiModels(string) ([]*apigatewayv2.Model, error)
}
type apigatewayv2Repository struct {
@ -114,3 +115,22 @@ func (r *apigatewayv2Repository) ListAllApiIntegrations(apiId string) ([]*apigat
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}
func (r *apigatewayv2Repository) ListAllApiModels(apiId string) ([]*apigatewayv2.Model, error) {
cacheKey := fmt.Sprintf("apigatewayv2ListAllApiModels_api_%s", apiId)
if v := r.cache.Get(cacheKey); v != nil {
return v.([]*apigatewayv2.Model), nil
}
input := apigatewayv2.GetModelsInput{
ApiId: &apiId,
}
resources, err := r.client.GetModels(&input)
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}

View File

@ -1,4 +1,4 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
// Code generated by mockery v2.9.4. DO NOT EDIT.
package repository
@ -35,6 +35,52 @@ func (_m *MockApiGatewayV2Repository) ListAllApiAuthorizers(_a0 string) ([]*apig
return r0, r1
}
// ListAllApiIntegrations provides a mock function with given fields: _a0
func (_m *MockApiGatewayV2Repository) ListAllApiIntegrations(_a0 string) ([]*apigatewayv2.Integration, error) {
ret := _m.Called(_a0)
var r0 []*apigatewayv2.Integration
if rf, ok := ret.Get(0).(func(string) []*apigatewayv2.Integration); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.Integration)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllApiModels provides a mock function with given fields: _a0
func (_m *MockApiGatewayV2Repository) ListAllApiModels(_a0 string) ([]*apigatewayv2.Model, error) {
ret := _m.Called(_a0)
var r0 []*apigatewayv2.Model
if rf, ok := ret.Get(0).(func(string) []*apigatewayv2.Model); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.Model)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllApiRoutes provides a mock function with given fields: apiId
func (_m *MockApiGatewayV2Repository) ListAllApiRoutes(apiId *string) ([]*apigatewayv2.Route, error) {
ret := _m.Called(apiId)
@ -81,29 +127,6 @@ func (_m *MockApiGatewayV2Repository) ListAllApis() ([]*apigatewayv2.Api, error)
return r0, r1
}
// ListAllIntegrations provides a mock function with given fields: _a0
func (_m *MockApiGatewayV2Repository) ListAllApiIntegrations(_a0 string) ([]*apigatewayv2.Integration, error) {
ret := _m.Called(_a0)
var r0 []*apigatewayv2.Integration
if rf, ok := ret.Get(0).(func(string) []*apigatewayv2.Integration); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.Integration)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllVpcLinks provides a mock function with given fields:
func (_m *MockApiGatewayV2Repository) ListAllVpcLinks() ([]*apigatewayv2.VpcLink, error) {
ret := _m.Called()

View File

@ -477,3 +477,114 @@ func TestApiGatewayV2Integration(t *testing.T) {
})
}
}
func TestApiGatewayV2Model(t *testing.T) {
dummyError := errors.New("this is an error")
apis := []*apigatewayv2.Api{
{ApiId: awssdk.String("bmyl5c6huh")},
{ApiId: awssdk.String("blghshbgte")},
}
tests := []struct {
test string
mocks func(*repository.MockApiGatewayV2Repository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no api gateway v2 models",
mocks: func(repo *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repo.On("ListAllApis").Return(apis, nil)
repo.On("ListAllApiModels", *apis[0].ApiId).Return([]*apigatewayv2.Model{}, nil).Once()
repo.On("ListAllApiModels", *apis[1].ApiId).Return([]*apigatewayv2.Model{}, nil).Once()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiple api gateway v2 models",
mocks: func(repo *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repo.On("ListAllApis").Return(apis, nil)
repo.On("ListAllApiModels", *apis[0].ApiId).Return([]*apigatewayv2.Model{
{
ModelId: awssdk.String("vdw6up"),
Name: awssdk.String("model1"),
},
}, nil).Once()
repo.On("ListAllApiModels", *apis[1].ApiId).Return([]*apigatewayv2.Model{
{
ModelId: awssdk.String("bwhebj"),
Name: awssdk.String("model2"),
},
}, nil).Once()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "vdw6up")
assert.Equal(t, got[0].ResourceType(), resourceaws.AwsApiGatewayV2ModelResourceType)
assert.Equal(t, "model1", *got[0].Attributes().GetString("name"))
assert.Equal(t, got[1].ResourceId(), "bwhebj")
assert.Equal(t, got[1].ResourceType(), resourceaws.AwsApiGatewayV2ModelResourceType)
assert.Equal(t, "model2", *got[1].Attributes().GetString("name"))
},
},
{
test: "cannot list apis",
mocks: func(repo *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repo.On("ListAllApis").Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayV2ModelResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2ModelResourceType, resourceaws.AwsApiGatewayV2ApiResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2ModelResourceType, resourceaws.AwsApiGatewayV2ApiResourceType),
},
{
test: "cannot list api gateway v2 model",
mocks: func(repo *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repo.On("ListAllApis").Return(apis, nil)
repo.On("ListAllApiModels", *apis[0].ApiId).Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayV2ModelResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2ModelResourceType, resourceaws.AwsApiGatewayV2ModelResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceaws.AwsApiGatewayV2ModelResourceType),
},
}
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.MockApiGatewayV2Repository{}
c.mocks(fakeRepo, alerter)
var repo repository.ApiGatewayV2Repository = fakeRepo
remoteLibrary.AddEnumerator(aws.NewApiGatewayV2ModelEnumerator(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,16 @@
package aws
import "github.com/snyk/driftctl/pkg/resource"
const AwsApiGatewayV2ModelResourceType = "aws_apigatewayv2_model"
func initAwsApiGatewayV2ModelMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetHumanReadableAttributesFunc(
AwsApiGatewayV2ModelResourceType,
func(res *resource.Resource) map[string]string {
return map[string]string{
"name": *res.Attributes().GetString("name"),
}
},
)
}

View File

@ -0,0 +1,30 @@
package aws_test
import (
"testing"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance"
)
func TestAcc_Aws_ApiGatewayV2Model(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_apigatewayv2_model"},
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(1)
},
},
},
})
}

View File

@ -97,6 +97,7 @@ func TestAWS_Metadata_Flags(t *testing.T) {
AwsNetworkACLRuleResourceType: {resource.FlagDeepMode},
AwsLaunchTemplateResourceType: {resource.FlagDeepMode},
AwsLaunchConfigurationResourceType: {},
AwsApiGatewayV2ModelResourceType: {},
}
schemaRepository := testresource.InitFakeSchemaRepository(tf.AWS, "3.19.0")

View File

@ -63,4 +63,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
initAwsAppAutoscalingTargetMetaData(resourceSchemaRepository)
initAwsAppAutoscalingPolicyMetaData(resourceSchemaRepository)
initAwsLaunchTemplateMetaData(resourceSchemaRepository)
initAwsApiGatewayV2ModelMetaData(resourceSchemaRepository)
}

View File

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

View File

@ -0,0 +1,21 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.47.0"
constraints = "3.47.0"
hashes = [
"h1:gXncRh1KtgLNMeb3/bYq5CvGfy8YTR+n6ds1noc5ggc=",
"zh:07bb6bda5b9fdb782dd568a2e85cfe0ab108770e2218f3411e57ed845c58af40",
"zh:0926b161a109e75bdc8691e8a32f568b4cd77a55510cf27573261fb5ba382287",
"zh:0a91adf25a78ad31d547da513db24f493d27592d3675ed291a7698351c30992d",
"zh:0f95f01e3bf0dab306ed86afb1ca00e01ce94ed6696765158d544b1569483b13",
"zh:10466a520c617354ebbee9366267e0878b091a15d49cb97846511e952bd9db90",
"zh:2fc627d3dc5a6df904591c673d640e6d3a697dcc12d1a43cf71066a47314f7c0",
"zh:a85476047ddb359acdc0db5b9cbe0a7e13c4e65289b03f6c93303d0452db450b",
"zh:cbadde98d44e8953cc78487b6788b97cff12632e9fda065bb970b001205662cb",
"zh:db05702323c5fa253d5e067458340b89126738b8f6a9847465ee3e75b0f28320",
"zh:e16cf52ff3b067adb33a75b89c03f9b03e666e2d45adb2ee296ae12b36cd5776",
"zh:fcb8f73f7f5e195e3345d5694b526e0d5e77562d2e7dd468366ee15b1be6b418",
]
}

View File

@ -0,0 +1,31 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.47.0"
}
}
resource "aws_apigatewayv2_api" "example" {
name = "example-http-api"
protocol_type = "WEBSOCKET"
}
resource "aws_apigatewayv2_model" "example" {
api_id = aws_apigatewayv2_api.example.id
content_type = "application/json"
name = "example"
schema = <<EOF
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ExampleModel",
"type": "object",
"properties": {
"id": { "type": "string" }
}
}
EOF
}

View File

@ -149,6 +149,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"aws_apigatewayv2_integration": {},
"aws_launch_template": {},
"aws_launch_configuration": {},
"aws_apigatewayv2_model": {},
"github_branch_protection": {},
"github_membership": {},