Merge pull request #1227 from karniwl/feat/api-gateway-v2-api

main
William BEUIL 2021-11-24 15:54:22 +01:00 committed by GitHub
commit df06e293f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 177851 additions and 0 deletions

View File

@ -159,6 +159,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{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 vpc link", dirName: "api_gateway_vpc_link", wantErr: false},
{name: "Api Gateway V2 Api", dirName: "apigatewayv2_api", wantErr: false},
{name: "Api Gateway request validator", dirName: "api_gateway_request_validator", wantErr: false},
{name: "Api Gateway rest api policy", dirName: "api_gateway_rest_api_policy", wantErr: false},
{name: "Api Gateway base path mapping", dirName: "api_gateway_base_path_mapping", wantErr: false},

View File

@ -0,0 +1,19 @@
[
{
"Id": "f5vdrg12tk",
"Type": "aws_apigatewayv2_api",
"Attrs": {
"api_endpoint": "wss://f5vdrg12tk.execute-api.us-east-2.amazonaws.com",
"api_key_selection_expression": "$request.header.x-api-key",
"arn": "arn:aws:apigateway:us-east-2::/apis/f5vdrg12tk",
"description": "",
"disable_execute_api_endpoint": false,
"execution_arn": "arn:aws:execute-api:us-east-2:070182406464:f5vdrg12tk",
"id": "f5vdrg12tk",
"name": "example-websocket-api",
"protocol_type": "WEBSOCKET",
"route_selection_expression": "$request.body.action",
"version": ""
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
{
"version": 4,
"terraform_version": "1.0.11",
"serial": 1,
"lineage": "b6adb9f7-8e1a-eaea-5c52-c50d6851fa6e",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_apigatewayv2_api",
"name": "example",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"api_endpoint": "wss://f5vdrg12tk.execute-api.us-east-2.amazonaws.com",
"api_key_selection_expression": "$request.header.x-api-key",
"arn": "arn:aws:apigateway:us-east-2::/apis/f5vdrg12tk",
"body": null,
"cors_configuration": [],
"credentials_arn": null,
"description": "",
"disable_execute_api_endpoint": false,
"execution_arn": "arn:aws:execute-api:us-east-2:070182406464:f5vdrg12tk",
"fail_on_warnings": null,
"id": "f5vdrg12tk",
"name": "example-websocket-api",
"protocol_type": "WEBSOCKET",
"route_key": null,
"route_selection_expression": "$request.body.action",
"tags": null,
"tags_all": {},
"target": null,
"version": ""
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
]
}

View File

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

View File

@ -51,6 +51,7 @@ func Init(version string, alerter *alerter.Alerter,
cloudformationRepository := repository.NewCloudformationRepository(provider.session, repositoryCache)
apigatewayRepository := repository.NewApiGatewayRepository(provider.session, repositoryCache)
appAutoScalingRepository := repository.NewAppAutoScalingRepository(provider.session, repositoryCache)
apigatewayv2Repository := repository.NewApiGatewayV2Repository(provider.session, repositoryCache)
deserializer := resource.NewDeserializer(factory)
providerLibrary.AddProvider(terraform.AWS, provider)
@ -201,6 +202,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewApiGatewayIntegrationEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayIntegrationResponseEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2ApiEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewAppAutoscalingTargetEnumerator(appAutoScalingRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, common.NewGenericDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, provider, deserializer))

View File

@ -0,0 +1,42 @@
package repository
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
)
type ApiGatewayV2Repository interface {
ListAllApis() ([]*apigatewayv2.Api, error)
}
type apigatewayv2Repository struct {
client apigatewayv2iface.ApiGatewayV2API
cache cache.Cache
}
func NewApiGatewayV2Repository(session *session.Session, c cache.Cache) *apigatewayv2Repository {
return &apigatewayv2Repository{
apigatewayv2.New(session),
c,
}
}
func (r *apigatewayv2Repository) ListAllApis() ([]*apigatewayv2.Api, error) {
cacheKey := "apigatewayv2ListAllApis"
v := r.cache.Get(cacheKey)
if v != nil {
return v.([]*apigatewayv2.Api), nil
}
input := apigatewayv2.GetApisInput{}
resources, err := r.client.GetApis(&input)
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}

View File

@ -0,0 +1,88 @@
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
awstest "github.com/cloudskiff/driftctl/test/aws"
"github.com/pkg/errors"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_apigatewayv2Repository_ListAllApis(t *testing.T) {
apis := []*apigatewayv2.Api{
{ApiId: aws.String("api1")},
{ApiId: aws.String("api2")},
{ApiId: aws.String("api3")},
{ApiId: aws.String("api4")},
{ApiId: aws.String("api5")},
{ApiId: aws.String("api6")},
}
remoteError := errors.New("remote error")
tests := []struct {
name string
mocks func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache)
want []*apigatewayv2.Api
wantErr error
}{
{
name: "list multiple apis",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
client.On("GetApis",
&apigatewayv2.GetApisInput{}).Return(&apigatewayv2.GetApisOutput{Items: apis}, nil).Once()
store.On("Get", "apigatewayv2ListAllApis").Return(nil).Times(1)
store.On("Put", "apigatewayv2ListAllApis", apis).Return(false).Times(1)
},
want: apis,
},
{
name: "should hit cache",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
store.On("Get", "apigatewayv2ListAllApis").Return(apis).Times(1)
},
want: apis,
},
{
name: "should return remote error",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
client.On("GetApis",
&apigatewayv2.GetApisInput{}).Return(nil, remoteError).Once()
store.On("Get", "apigatewayv2ListAllApis").Return(nil).Times(1)
},
wantErr: remoteError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cache.MockCache{}
client := &awstest.MockFakeApiGatewayV2{}
tt.mocks(client, store)
r := &apigatewayv2Repository{
client: client,
cache: store,
}
got, err := r.ListAllApis()
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

@ -0,0 +1,36 @@
// Code generated by mockery v2.9.4. DO NOT EDIT.
package repository
import (
apigatewayv2 "github.com/aws/aws-sdk-go/service/apigatewayv2"
mock "github.com/stretchr/testify/mock"
)
// MockApiGatewayV2Repository is an autogenerated mock type for the ApiGatewayV2Repository type
type MockApiGatewayV2Repository struct {
mock.Mock
}
// ListAllApis provides a mock function with given fields:
func (_m *MockApiGatewayV2Repository) ListAllApis() ([]*apigatewayv2.Api, error) {
ret := _m.Called()
var r0 []*apigatewayv2.Api
if rf, ok := ret.Get(0).(func() []*apigatewayv2.Api); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.Api)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -0,0 +1,101 @@
package remote
import (
"testing"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/filter"
"github.com/cloudskiff/driftctl/pkg/remote/alerts"
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/common"
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
testresource "github.com/cloudskiff/driftctl/test/resource"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestApiGatewayV2Api(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",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllApis").Return([]*apigatewayv2.Api{
{ApiId: awssdk.String("f5vdrg12tk")},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, got[0].ResourceId(), "f5vdrg12tk")
assert.Equal(t, got[0].ResourceType(), resourceaws.AwsApiGatewayV2ApiResourceType)
},
},
{
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.AwsApiGatewayV2ApiResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2ApiResourceType, resourceaws.AwsApiGatewayV2ApiResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingError(dummyError, 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.NewApiGatewayV2ApiEnumerator(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 AwsApiGatewayV2ApiResourceType = "aws_apigatewayv2_api"

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

@ -31,6 +31,7 @@ func TestAWS_Metadata_Flags(t *testing.T) {
AwsApiGatewayRestApiPolicyResourceType: {},
AwsApiGatewayStageResourceType: {},
AwsApiGatewayVpcLinkResourceType: {},
AwsApiGatewayV2ApiResourceType: {},
AwsAppAutoscalingPolicyResourceType: {resource.FlagDeepMode},
AwsAppAutoscalingScheduledActionResourceType: {},
AwsAppAutoscalingTargetResourceType: {resource.FlagDeepMode},

View File

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

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,15 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.19.0"
}
}
resource "aws_apigatewayv2_api" "example" {
name = "example-websocket-api"
protocol_type = "WEBSOCKET"
route_selection_expression = "$request.body.action"
}

View File

@ -139,6 +139,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
}},
"aws_appautoscaling_policy": {},
"aws_appautoscaling_scheduled_action": {},
"aws_apigatewayv2_api": {},
"github_branch_protection": {},
"github_membership": {},

9
test/aws/apigatewayv2.go Normal file
View File

@ -0,0 +1,9 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
)
type FakeApiGatewayV2 interface {
apigatewayv2iface.ApiGatewayV2API
}

File diff suppressed because it is too large Load Diff