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 - IAM authorizer support #21393

Merged
merged 10 commits into from
Aug 4, 2022
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2-authorizers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [IAM Authorizers](#iam-authorizers)
- [WebSocket APIs](#websocket-apis)
- [Lambda Authorizer](#lambda-authorizer)
- [IAM Authorizers](#iam-authorizer)

## Introduction

Expand Down Expand Up @@ -256,3 +257,24 @@ new apigwv2.WebSocketApi(this, 'WebSocketApi', {
},
});
```

### IAM Authorizer
d0z0 marked this conversation as resolved.
Show resolved Hide resolved

IAM authorizers can be used to allow identity-based access to your WebSocket API.

```ts
import { WebSocketIamAuthorizer } from '@aws-cdk/aws-apigatewayv2-authorizers';
import { WebSocketLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';

// This function handles your connect route
declare const connectHandler: lambda.Function;

const webSocketApi = new apigwv2.WebSocketApi(this, 'WebSocketApi');

webSocketApi.addRoute('$connect', {
integration: new WebSocketLambdaIntegration('Integration', connectHandler),
authorizer: new WebSocketIamAuthorizer()
});

d0z0 marked this conversation as resolved.
Show resolved Hide resolved

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
WebSocketAuthorizerType,
WebSocketRouteAuthorizerBindOptions,
WebSocketRouteAuthorizerConfig,
IWebSocketRouteAuthorizer,
} from '@aws-cdk/aws-apigatewayv2';

/**
* Authorize WebSocket API Routes with IAM
*/
export class WebSocketIamAuthorizer implements IWebSocketRouteAuthorizer {
public bind(
_options: WebSocketRouteAuthorizerBindOptions,
): WebSocketRouteAuthorizerConfig {
return {
authorizationType: WebSocketAuthorizerType.IAM,
};
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lambda';
export * from './iam';
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/aws-lambda": "^8.10.101",
"@aws-cdk/integ-tests": "0.0.0",
"@types/jest": "^27.5.2"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"Resources": {
"User00B015A1": {
"Type": "AWS::IAM::User"
},
"UserAccessEC42ADF7": {
"Type": "AWS::IAM::AccessKey",
"Properties": {
"UserName": {
"Ref": "User00B015A1"
}
}
},
"authfunctionServiceRoleFCB72198": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"authfunction96361832": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = () => {return true}"
},
"Role": {
"Fn::GetAtt": [
"authfunctionServiceRoleFCB72198",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
"authfunctionServiceRoleFCB72198"
]
},
"WebSocketApi34BCF99B": {
"Type": "AWS::ApiGatewayV2::Api",
"Properties": {
"Name": "WebSocketApi",
"ProtocolType": "WEBSOCKET",
"RouteSelectionExpression": "$request.body.action"
}
},
"WebSocketApiconnectRouteWebSocketLambdaIntegrationPermission76CD86C6": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"authfunction96361832",
"Arn"
]
},
"Principal": "apigateway.amazonaws.com",
"SourceArn": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":execute-api:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "WebSocketApi34BCF99B"
},
"/*/*$connect"
]
]
}
}
},
"WebSocketApiconnectRouteWebSocketLambdaIntegration3D2B13DD": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "WebSocketApi34BCF99B"
},
"IntegrationType": "AWS_PROXY",
"IntegrationUri": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":apigateway:",
{
"Ref": "AWS::Region"
},
":lambda:path/2015-03-31/functions/",
{
"Fn::GetAtt": [
"authfunction96361832",
"Arn"
]
},
"/invocations"
]
]
}
}
},
"WebSocketApiconnectRoute846149DD": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
"ApiId": {
"Ref": "WebSocketApi34BCF99B"
},
"RouteKey": "$connect",
"AuthorizationType": "AWS_IAM",
"Target": {
"Fn::Join": [
"",
[
"integrations/",
{
"Ref": "WebSocketApiconnectRouteWebSocketLambdaIntegration3D2B13DD"
}
]
]
}
}
},
"AllowInvoke767865EA": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":execute-api:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "WebSocketApi34BCF99B"
}
]
]
}
}
],
"Version": "2012-10-17"
},
"PolicyName": "AllowInvoke767865EA",
"Users": [
{
"Ref": "User00B015A1"
}
]
}
}
},
"Outputs": {
"TESTACCESSKEYID": {
"Value": {
"Ref": "UserAccessEC42ADF7"
}
},
"TESTSECRETACCESSKEY": {
"Value": {
"Fn::GetAtt": [
"UserAccessEC42ADF7",
"SecretAccessKey"
]
}
},
"TESTREGION": {
"Value": {
"Ref": "AWS::Region"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"20.0.0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "20.0.0",
"testCases": {
"ApiGatewayV2WebSocketIamTest/DefaultTest": {
"stacks": [
"IntegApiGatewayV2Iam"
],
"assertionStack": "ApiGatewayV2WebSocketIamTestDefaultTestDeployAssert2B412D7B"
}
}
}
Loading