Merge pull request #1263 from snyk/feat/add_aws_apigatewayv2_stage

Add aws_apigatewayv2_stage
main
Elie 2022-01-12 16:53:16 +01:00 committed by GitHub
commit a00b2634cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 172112 additions and 0 deletions

View File

@ -161,6 +161,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{name: "Api Gateway vpc link", dirName: "aws_api_gateway_vpc_link", wantErr: false},
{name: "Api Gateway V2 Api", dirName: "aws_apigatewayv2_api", wantErr: false},
{name: "Api Gateway V2 Route", dirName: "aws_apigatewayv2_route", wantErr: false},
{name: "Api Gateway V2 stage", dirName: "aws_apigatewayv2_stage", wantErr: false},
{name: "Api Gateway request validator", dirName: "aws_api_gateway_request_validator", wantErr: false},
{name: "Api Gateway rest api policy", dirName: "aws_api_gateway_rest_api_policy", wantErr: false},
{name: "Api Gateway base path mapping", dirName: "aws_api_gateway_base_path_mapping", wantErr: false},

View File

@ -0,0 +1,27 @@
[
{
"Id": "example-stage",
"Type": "aws_apigatewayv2_stage",
"Attrs": {
"api_id": "hn5fxpwfae",
"arn": "arn:aws:apigateway:us-east-1::/apis/hn5fxpwfae/stages/example-stage",
"auto_deploy": false,
"client_certificate_id": "",
"default_route_settings": [
{
"data_trace_enabled": false,
"detailed_metrics_enabled": false,
"logging_level": "",
"throttling_burst_limit": 0,
"throttling_rate_limit": 0
}
],
"deployment_id": "",
"description": "",
"execution_arn": "arn:aws:execute-api:us-east-1:929327065333:hn5fxpwfae/example-stage",
"id": "example-stage",
"invoke_url": "https://hn5fxpwfae.execute-api.us-east-1.amazonaws.com/example-stage",
"name": "example-stage"
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.47.0"
}
}
resource "aws_apigatewayv2_api" "example" {
name = "example-http-api"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_stage" "example" {
api_id = aws_apigatewayv2_api.example.id
name = "example-stage"
}

View File

@ -0,0 +1,51 @@
{
"version": 4,
"terraform_version": "1.1.0",
"serial": 110,
"lineage": "0738cef4-9d69-9ccc-aebd-1177cafa0aa9",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_apigatewayv2_stage",
"name": "example",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"access_log_settings": [],
"api_id": "hn5fxpwfae",
"arn": "arn:aws:apigateway:us-east-1::/apis/hn5fxpwfae/stages/example-stage",
"auto_deploy": false,
"client_certificate_id": "",
"default_route_settings": [
{
"data_trace_enabled": false,
"detailed_metrics_enabled": false,
"logging_level": "",
"throttling_burst_limit": 0,
"throttling_rate_limit": 0
}
],
"deployment_id": "",
"description": "",
"execution_arn": "arn:aws:execute-api:us-east-1:929327065333:hn5fxpwfae/example-stage",
"id": "example-stage",
"invoke_url": "https://hn5fxpwfae.execute-api.us-east-1.amazonaws.com/example-stage",
"name": "example-stage",
"route_settings": [],
"stage_variables": null,
"tags": null,
"tags_all": {}
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_apigatewayv2_api.example"
]
}
]
}
]
}

View File

@ -0,0 +1,54 @@
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 ApiGatewayV2StageEnumerator struct {
repository repository.ApiGatewayV2Repository
factory resource.ResourceFactory
}
func NewApiGatewayV2StageEnumerator(repo repository.ApiGatewayV2Repository, factory resource.ResourceFactory) *ApiGatewayV2StageEnumerator {
return &ApiGatewayV2StageEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ApiGatewayV2StageEnumerator) SupportedType() resource.ResourceType {
return aws.AwsApiGatewayV2StageResourceType
}
func (e *ApiGatewayV2StageEnumerator) Enumerate() ([]*resource.Resource, error) {
apis, err := e.repository.ListAllApis()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsApiGatewayV2ApiResourceType)
}
results := make([]*resource.Resource, 0)
for _, api := range apis {
stages, err := e.repository.ListAllApiStages(*api.ApiId)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, stage := range stages {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*stage.StageName,
map[string]interface{}{},
),
)
}
}
return results, err
}

View File

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

View File

@ -16,6 +16,7 @@ type ApiGatewayV2Repository interface {
ListAllApiAuthorizers(string) ([]*apigatewayv2.Authorizer, error)
ListAllApiIntegrations(string) ([]*apigatewayv2.Integration, error)
ListAllApiModels(string) ([]*apigatewayv2.Model, error)
ListAllApiStages(string) ([]*apigatewayv2.Stage, error)
}
type apigatewayv2Repository struct {
@ -134,3 +135,21 @@ func (r *apigatewayv2Repository) ListAllApiModels(apiId string) ([]*apigatewayv2
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}
func (r *apigatewayv2Repository) ListAllApiStages(apiId string) ([]*apigatewayv2.Stage, error) {
cacheKey := fmt.Sprintf("apigatewayv2ListAllApiStages_api_%s", apiId)
if v := r.cache.Get(cacheKey); v != nil {
return v.([]*apigatewayv2.Stage), nil
}
input := apigatewayv2.GetStagesInput{
ApiId: &apiId,
}
resources, err := r.client.GetStages(&input)
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}

View File

@ -104,6 +104,29 @@ func (_m *MockApiGatewayV2Repository) ListAllApiRoutes(apiId *string) ([]*apigat
return r0, r1
}
// ListAllApiStages provides a mock function with given fields: _a0
func (_m *MockApiGatewayV2Repository) ListAllApiStages(_a0 string) ([]*apigatewayv2.Stage, error) {
ret := _m.Called(_a0)
var r0 []*apigatewayv2.Stage
if rf, ok := ret.Get(0).(func(string) []*apigatewayv2.Stage); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.Stage)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllApis provides a mock function with given fields:
func (_m *MockApiGatewayV2Repository) ListAllApis() ([]*apigatewayv2.Api, error) {
ret := _m.Called()

View File

@ -588,3 +588,86 @@ func TestApiGatewayV2Model(t *testing.T) {
})
}
}
func TestApiGatewayV2Stage(t *testing.T) {
dummyError := errors.New("this is an error")
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 api",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllApis").Return([]*apigatewayv2.Api{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "single api gateway v2 api with a single stage",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllApis").Return([]*apigatewayv2.Api{
{ApiId: awssdk.String("a-gateway")},
}, nil)
repository.On("ListAllApiStages", "a-gateway").
Return([]*apigatewayv2.Stage{{
StageName: awssdk.String("a-stage"),
}}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, "a-stage", got[0].ResourceId())
assert.Equal(t, resourceaws.AwsApiGatewayV2StageResourceType, got[0].ResourceType())
},
},
{
test: "cannot list api gateway v2 apis",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllApis").Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayV2StageResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2ApiResourceType, resourceaws.AwsApiGatewayV2StageResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2StageResourceType, resourceaws.AwsApiGatewayV2ApiResourceType),
},
}
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.NewApiGatewayV2StageEnumerator(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,3 @@
package aws
const AwsApiGatewayV2StageResourceType = "aws_apigatewayv2_stage"

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_ApiGatewayV2Stage(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_apigatewayv2_stage"},
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

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

View File

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

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,19 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.47.0"
}
}
resource "aws_apigatewayv2_api" "example" {
name = "example-http-api"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_stage" "example" {
api_id = aws_apigatewayv2_api.example.id
name = "example-stage"
}

View File

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