Skip to content

Commit

Permalink
feat(AWS API Gateway): Support customerId in API keys (#7786)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcampionpae committed May 28, 2020
1 parent d43241e commit c6894b5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/providers/aws/events/apigateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ provider:
value: myThirdKeyValue
- value: myFourthKeyValue # let cloudformation name the key (recommended when setting api key value)
description: Api key description # Optional
customerId: A string that will be set as the customerID for the key # Optional
usagePlan:
quota:
limit: 5000
Expand Down
3 changes: 3 additions & 0 deletions docs/providers/aws/guide/serverless.yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ provider:
endpointType: regional # Optional endpoint configuration for API Gateway REST API. Default is Edge.
apiKeys: # List of API keys to be used by your service API Gateway REST API
- myFirstKey
value: myFirstKeyValue
description: myFirstKeyDescription
customerId: myFirstKeyCustomerId
- ${opt:stage}-myFirstKey
- ${env:MY_API_KEY} # you can hide it in a serverless variable
apiGateway: # Optional API Gateway global config
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/info/getApiKeyValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {
.then(apiKeys => {
if (apiKeys && apiKeys.length) {
info.apiKeys = _.map(apiKeys, apiKey =>
_.pick(apiKey, ['name', 'value', 'description'])
_.pick(apiKey, ['name', 'value', 'description', 'customerId'])
);
}
return BbPromise.resolve();
Expand Down
2 changes: 2 additions & 0 deletions lib/plugins/aws/info/getApiKeyValues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('#getApiKeyValues()', () => {
value: 'valueForKeyBar',
name: 'bar',
description: 'bar description',
customerId: 'bar customer id',
});

const expectedGatheredDataObj = {
Expand All @@ -69,6 +70,7 @@ describe('#getApiKeyValues()', () => {
value: 'valueForKeyFoo',
},
{
customerId: 'bar customer id',
description: 'bar description',
name: 'bar',
value: 'valueForKeyBar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ function createApiKeyResource(that, apiKey) {
const name = _.isString(apiKey) ? apiKey : apiKey.name;
const value = _.isObject(apiKey) && apiKey.value ? apiKey.value : undefined;
const description = _.isObject(apiKey) ? apiKey.description : undefined;
const customerId = _.isObject(apiKey) ? apiKey.customerId : undefined;
const resourceTemplate = {
Type: 'AWS::ApiGateway::ApiKey',
Properties: {
Enabled: true,
Name: name,
Value: value,
Description: description,
CustomerId: customerId,
StageKeys: [
{
RestApiId: that.provider.getApiGatewayRestApiId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,45 @@ describe('#compileApiKeys()', () => {
{ name: '2345678901' },
{ value: 'valueForKeyWithoutName', description: 'Api key description' },
{ name: '3456789012', value: 'valueForKey3456789012' },
{
name: '9876543211',
value: 'valueForKey9876543211',
customerId: 'customerid98765',
},
];

return awsCompileApigEvents.compileApiKeys().then(() => {
const expectedApiKeys = [
{ name: '1234567890', value: undefined, description: undefined },
{ name: '2345678901', value: undefined, description: undefined },
{ name: undefined, value: 'valueForKeyWithoutName', description: 'Api key description' },
{ name: '3456789012', value: 'valueForKey3456789012', description: undefined },
{
name: '1234567890',
value: undefined,
description: undefined,
customerId: undefined,
},
{
name: '2345678901',
value: undefined,
description: undefined,
customerId: undefined,
},
{
name: undefined,
value: 'valueForKeyWithoutName',
description: 'Api key description',
customerId: undefined,
},
{
name: '3456789012',
value: 'valueForKey3456789012',
description: undefined,
customerId: undefined,
},
{
name: '9876543211',
value: 'valueForKey9876543211',
description: undefined,
customerId: 'customerid98765',
},
];

_.forEach(expectedApiKeys, (apiKey, index) => {
Expand Down Expand Up @@ -68,6 +99,12 @@ describe('#compileApiKeys()', () => {
].Properties.Description
).to.equal(apiKey.description);

expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources[
awsCompileApigEvents.provider.naming.getApiKeyLogicalId(index + 1)
].Properties.CustomerId
).to.equal(apiKey.customerId);

expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources[
awsCompileApigEvents.provider.naming.getApiKeyLogicalId(index + 1)
Expand Down Expand Up @@ -103,7 +140,10 @@ describe('#compileApiKeys()', () => {
free: [
'1234567890',
{ name: '2345678901' },
{ value: 'valueForKeyWithoutName', description: 'Api key description' },
{
value: 'valueForKeyWithoutName',
description: 'Api key description',
},
{ name: '3456789012', value: 'valueForKey3456789012' },
],
},
Expand All @@ -120,7 +160,11 @@ describe('#compileApiKeys()', () => {
value: 'valueForKeyWithoutName',
description: 'Api key description',
},
{ name: '3456789012', value: 'valueForKey3456789012', description: undefined },
{
name: '3456789012',
value: 'valueForKey3456789012',
description: undefined,
},
],
paid: [
{ name: '0987654321', value: undefined, description: undefined },
Expand Down

0 comments on commit c6894b5

Please sign in to comment.