Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigatewayv2): websocket api: api keys #16636

Merged
merged 9 commits into from
Jan 11, 2022

Conversation

alpacamybags118
Copy link
Contributor

@alpacamybags118 alpacamybags118 commented Sep 24, 2021


This PR adds support for requiring an API Key on Websocket API routes. Specifically, it does the following:

  • Exposes apiKeyRequired on route object (defaults to false)
  • Exposes apiKeySelectionExpression on api object

In addition, the following has been added:

  • Logic to ensure apiKeySelectionExpression falls within the two currently supported values
  • Created a few basic integration tests for the api and route objects for websockets

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Sep 24, 2021

@alpacamybags118
Copy link
Contributor Author

I'm running into a bit of trouble when I try to add @aws-cdk/aws-apigatewayv2-integrations as a dev dependency in aws-apigatewayv2 to write some integration tests.

When I build apigatewayv2, I get the following errors:

[2021-09-24T00:20:46.130] [ERROR] jsii/compiler - Type model errors prevented the JSII assembly from being created
error JSII8002: Method and property (unless they are static readonly) names must use camelCase. Rename "@aws-cdk/aws-apigatewayv2.HttpRouteKey.DEFAULT" to "default"
error JSII8002: Method and property (unless they are static readonly) names must use camelCase. Rename "@aws-cdk/aws-apigatewayv2.PayloadFormatVersion.VERSION_1_0" to "version10"
error JSII8002: Method and property (unless they are static readonly) names must use camelCase. Rename "@aws-cdk/aws-apigatewayv2.PayloadFormatVersion.VERSION_2_0" to "version20"
Error: /mnt/c/Git/aws-cdk/tools/cdk-build-tools/node_modules/jsii/bin/jsii --silence-warnings=reserved-word exited with error code 1

If I dont include this dependency, I don't get any errors during build and everything works just fine. I'm also importing @aws-cdk/aws-lambda and I'm able to build still with that imported.

I checked the objects the error is referring to, and they are all static readonly so I'm a bit confused.

@alpacamybags118 alpacamybags118 force-pushed the api_key_websocket branch 3 times, most recently from b45ca7f to 4c168ce Compare September 28, 2021 02:41
@alpacamybags118
Copy link
Contributor Author

that issue ended up being a circular dependency between apigateway and apigateway-integrations. I made some code and test adjustments. This pr should be ready for review now

@alpacamybags118 alpacamybags118 marked this pull request as ready for review September 28, 2021 03:14
@alpacamybags118 alpacamybags118 force-pushed the api_key_websocket branch 3 times, most recently from 9b69ac1 to 729bb75 Compare October 5, 2021 22:01
@peterwoodworth peterwoodworth changed the title feat(aws-apigatewayv2): add support for api-key in websocket apis feat(aws-apigatewayv2): add support for api-key in websocket apis Oct 21, 2021
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Oct 21, 2021
@alpacamybags118 alpacamybags118 force-pushed the api_key_websocket branch 2 times, most recently from 9aeac9c to 0f3c41e Compare October 26, 2021 02:59
nija-at
nija-at previously requested changes Nov 5, 2021
Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @alpacamybags118 - please see my comments below.

@@ -336,3 +336,12 @@ webSocketApi.addRoute('sendmessage', {
}),
});
```

In addition, you can enable API key authentication to your WebSocket api:
Copy link
Contributor

@nija-at nija-at Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a separate section called 'api keys' and explain them a little more? I'm not familiar with these.

Comment on lines 23 to 36
/**
* x-api-key type
*/
X_API_KEY = '$request.header.x-api-key',

/**
* usageIdentifierKey type
*/
USAGE_IDENTIFIER_KEY = '$context.authorizer.usageIdentifierKey'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add more documentation to these on what these mean?

@@ -26,6 +41,12 @@ export interface WebSocketApiProps {
*/
readonly apiName?: string;

/**
* An API key selection expression. Currently only supports '$request.header.x-api-key' and '$context.authorizer.usageIdentifierKey'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Think of a user who is coming to the cdk and looking at these. They should be able to understand at a basic level what this is, whether they should use it, etc.

packages/@aws-cdk/aws-apigatewayv2/lib/websocket/route.ts Outdated Show resolved Hide resolved
@alpacamybags118
Copy link
Contributor Author

thanks for the feedback @nija-at will get to updating tonight

@mergify mergify bot dismissed nija-at’s stale review November 9, 2021 06:11

Pull request has been modified.

Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments below.

@@ -0,0 +1,11 @@
#!/usr/bin/env node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need an integ test for this change. Drop.

/**
* Represents the currently available API Key Selection Expressions
*/
export enum ApiKeySelectionExpression {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
* Represents the currently available API Key Selection Expressions
*/
export enum ApiKeySelectionExpression {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module holds both http api and websocket api, so name this WebSocketApiKeySelection

* x-api-key type
* This represents an API Key that is provided via an `x-api-key` header in the user request.
*/
X_API_KEY = '$request.header.x-api-key',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this HEADER_X_API_KEY and the other one AUTHORIZER_USAGE_IDENTIFIER_KEY

packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigatewayv2/README.md Show resolved Hide resolved
@alpacamybags118
Copy link
Contributor Author

Thanks for the feedback again @nija-at - ill be on vacation for a week so I'll look at this once I'm back!

Co-authored-by: Niranjan Jayakar <nija@amazon.com>
@mergify mergify bot dismissed nija-at’s stale review December 1, 2021 23:59

Pull request has been modified.

@nija-at nija-at changed the title feat(aws-apigatewayv2): add support for api-key in websocket apis feat(apigatewayv2): websocket api: api keys Dec 2, 2021
@nija-at nija-at removed their assignment Dec 2, 2021
@mergify
Copy link
Contributor

mergify bot commented Jan 11, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 5632f9a
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 24f8f74 into aws:master Jan 11, 2022
@mergify
Copy link
Contributor

mergify bot commented Jan 11, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
----
This PR adds support for requiring an API Key on Websocket API routes. Specifically, it does the following:

* Exposes `apiKeyRequired` on route object (defaults to false)
* Exposes `apiKeySelectionExpression` on api object

In addition, the following has been added:
* Logic to ensure `apiKeySelectionExpression` falls within the two currently supported values
* Created a few basic integration tests for the api and route objects for websockets

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants