Skip to content

Commit

Permalink
Merge pull request #1381 from snyk/cherry-pick-b0cda12d1c438940ae6e88…
Browse files Browse the repository at this point in the history
…95f84915d138953569

Cherry-pick: fix: panic in apigatewayv2_api_mapping enumerator
  • Loading branch information
sundowndev-snyk authored Mar 11, 2022
2 parents 856df94 + 9c791dd commit d08cd70
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
14 changes: 10 additions & 4 deletions pkg/remote/aws/apigatewayv2_mapping_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ func (e *ApiGatewayV2MappingEnumerator) Enumerate() ([]*resource.Resource, error
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, mapping := range mappings {
attrs := make(map[string]interface{})

if mapping.ApiId != nil {
attrs["api_id"] = *mapping.ApiId
}
if mapping.Stage != nil {
attrs["stage"] = *mapping.Stage
}

results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*mapping.ApiMappingId,
map[string]interface{}{
"stage": *mapping.Stage,
"api_id": *mapping.ApiId,
},
attrs,
),
)
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/remote/aws_apigatewayv2_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,34 @@ func TestApiGatewayV2Mapping(t *testing.T) {
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceaws.AwsApiGatewayV2MappingResourceType),
},
{
test: "returning mapping with invalid attributes",
mocks: func(repositoryV1 *repository.MockApiGatewayRepository, repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repositoryV1.On("ListAllDomainNames").Return([]*apigateway.DomainName{
{DomainName: awssdk.String("example.com")},
}, nil)
repository.On("ListAllApiMappings", "example.com").
Return([]*apigatewayv2.ApiMapping{
{
ApiMappingId: awssdk.String("barfoo"),
},
{
Stage: awssdk.String("a-stage"),
ApiId: awssdk.String("foobar"),
ApiMappingId: awssdk.String("foobar"),
},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)

assert.Equal(t, "barfoo", got[0].ResourceId())
assert.Equal(t, resourceaws.AwsApiGatewayV2MappingResourceType, got[0].ResourceType())

assert.Equal(t, "foobar", got[1].ResourceId())
assert.Equal(t, resourceaws.AwsApiGatewayV2MappingResourceType, got[1].ResourceType())
},
},
}

providerVersion := "3.19.0"
Expand Down
12 changes: 9 additions & 3 deletions pkg/resource/aws/aws_apigatewayv2_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ func initAwsApiGatewayV2MappingMetaData(resourceSchemaRepository resource.Schema
resourceSchemaRepository.SetHumanReadableAttributesFunc(
AwsApiGatewayV2MappingResourceType,
func(res *resource.Resource) map[string]string {
return map[string]string{
"Api": *res.Attributes().GetString("api_id"),
"Stage": *res.Attributes().GetString("stage"),
attrs := make(map[string]string)

if v := res.Attributes().GetString("api_id"); v != nil {
attrs["Api"] = *v
}
if v := res.Attributes().GetString("stage"); v != nil {
attrs["Stage"] = *v
}

return attrs
},
)
}

0 comments on commit d08cd70

Please sign in to comment.