Skip to content

Commit d08cd70

Browse files
Merge pull request #1381 from snyk/cherry-pick-b0cda12d1c438940ae6e8895f84915d138953569
Cherry-pick: fix: panic in apigatewayv2_api_mapping enumerator
2 parents 856df94 + 9c791dd commit d08cd70

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

pkg/remote/aws/apigatewayv2_mapping_enumerator.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,21 @@ func (e *ApiGatewayV2MappingEnumerator) Enumerate() ([]*resource.Resource, error
3838
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
3939
}
4040
for _, mapping := range mappings {
41+
attrs := make(map[string]interface{})
42+
43+
if mapping.ApiId != nil {
44+
attrs["api_id"] = *mapping.ApiId
45+
}
46+
if mapping.Stage != nil {
47+
attrs["stage"] = *mapping.Stage
48+
}
49+
4150
results = append(
4251
results,
4352
e.factory.CreateAbstractResource(
4453
string(e.SupportedType()),
4554
*mapping.ApiMappingId,
46-
map[string]interface{}{
47-
"stage": *mapping.Stage,
48-
"api_id": *mapping.ApiId,
49-
},
55+
attrs,
5056
),
5157
)
5258
}

pkg/remote/aws_apigatewayv2_scanner_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,34 @@ func TestApiGatewayV2Mapping(t *testing.T) {
961961
},
962962
wantErr: remoteerr.NewResourceListingError(dummyError, resourceaws.AwsApiGatewayV2MappingResourceType),
963963
},
964+
{
965+
test: "returning mapping with invalid attributes",
966+
mocks: func(repositoryV1 *repository.MockApiGatewayRepository, repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
967+
repositoryV1.On("ListAllDomainNames").Return([]*apigateway.DomainName{
968+
{DomainName: awssdk.String("example.com")},
969+
}, nil)
970+
repository.On("ListAllApiMappings", "example.com").
971+
Return([]*apigatewayv2.ApiMapping{
972+
{
973+
ApiMappingId: awssdk.String("barfoo"),
974+
},
975+
{
976+
Stage: awssdk.String("a-stage"),
977+
ApiId: awssdk.String("foobar"),
978+
ApiMappingId: awssdk.String("foobar"),
979+
},
980+
}, nil)
981+
},
982+
assertExpected: func(t *testing.T, got []*resource.Resource) {
983+
assert.Len(t, got, 2)
984+
985+
assert.Equal(t, "barfoo", got[0].ResourceId())
986+
assert.Equal(t, resourceaws.AwsApiGatewayV2MappingResourceType, got[0].ResourceType())
987+
988+
assert.Equal(t, "foobar", got[1].ResourceId())
989+
assert.Equal(t, resourceaws.AwsApiGatewayV2MappingResourceType, got[1].ResourceType())
990+
},
991+
},
964992
}
965993

966994
providerVersion := "3.19.0"

pkg/resource/aws/aws_apigatewayv2_mapping.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ func initAwsApiGatewayV2MappingMetaData(resourceSchemaRepository resource.Schema
88
resourceSchemaRepository.SetHumanReadableAttributesFunc(
99
AwsApiGatewayV2MappingResourceType,
1010
func(res *resource.Resource) map[string]string {
11-
return map[string]string{
12-
"Api": *res.Attributes().GetString("api_id"),
13-
"Stage": *res.Attributes().GetString("stage"),
11+
attrs := make(map[string]string)
12+
13+
if v := res.Attributes().GetString("api_id"); v != nil {
14+
attrs["Api"] = *v
15+
}
16+
if v := res.Attributes().GetString("stage"); v != nil {
17+
attrs["Stage"] = *v
1418
}
19+
20+
return attrs
1521
},
1622
)
1723
}

0 commit comments

Comments
 (0)