forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apigatewayv2): Lambda authorizer for WebSocket API (aws#16886)
closes aws#13869 By this PR, you will be able to enable WebSocket authorizer as the below code: ```ts const integration = new LambdaWebSocketIntegration({ handler, }); const authorizer = new WebSocketLambdaAuthorizer('Authorizer', authHandler); new WebSocketApi(stack, 'WebSocketApi', { connectRouteOptions: { integration, authorizer, }, }); ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
12 changed files
with
418 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './http'; | ||
export * from './websocket'; |
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/aws-apigatewayv2-authorizers/lib/websocket/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lambda'; |
89 changes: 89 additions & 0 deletions
89
packages/@aws-cdk/aws-apigatewayv2-authorizers/lib/websocket/lambda.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
WebSocketAuthorizer, | ||
WebSocketAuthorizerType, | ||
WebSocketRouteAuthorizerBindOptions, | ||
WebSocketRouteAuthorizerConfig, | ||
IWebSocketRouteAuthorizer, | ||
IWebSocketApi, | ||
} from '@aws-cdk/aws-apigatewayv2'; | ||
import { ServicePrincipal } from '@aws-cdk/aws-iam'; | ||
import { IFunction } from '@aws-cdk/aws-lambda'; | ||
import { Stack, Names } from '@aws-cdk/core'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct as CoreConstruct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* Properties to initialize WebSocketTokenAuthorizer. | ||
*/ | ||
export interface WebSocketLambdaAuthorizerProps { | ||
|
||
/** | ||
* The name of the authorizer | ||
* @default - same value as `id` passed in the constructor. | ||
*/ | ||
readonly authorizerName?: string; | ||
|
||
/** | ||
* The identity source for which authorization is requested. | ||
* | ||
* @default ['$request.header.Authorization'] | ||
*/ | ||
readonly identitySource?: string[]; | ||
} | ||
|
||
/** | ||
* Authorize WebSocket Api routes via a lambda function | ||
*/ | ||
export class WebSocketLambdaAuthorizer implements IWebSocketRouteAuthorizer { | ||
private authorizer?: WebSocketAuthorizer; | ||
private webSocketApi?: IWebSocketApi; | ||
|
||
constructor( | ||
private readonly id: string, | ||
private readonly handler: IFunction, | ||
private readonly props: WebSocketLambdaAuthorizerProps = {}) { | ||
} | ||
|
||
public bind(options: WebSocketRouteAuthorizerBindOptions): WebSocketRouteAuthorizerConfig { | ||
if (this.webSocketApi && (this.webSocketApi.apiId !== options.route.webSocketApi.apiId)) { | ||
throw new Error('Cannot attach the same authorizer to multiple Apis'); | ||
} | ||
|
||
if (!this.authorizer) { | ||
this.webSocketApi = options.route.webSocketApi; | ||
this.authorizer = new WebSocketAuthorizer(options.scope, this.id, { | ||
webSocketApi: options.route.webSocketApi, | ||
identitySource: this.props.identitySource ?? [ | ||
'$request.header.Authorization', | ||
], | ||
type: WebSocketAuthorizerType.LAMBDA, | ||
authorizerName: this.props.authorizerName ?? this.id, | ||
authorizerUri: lambdaAuthorizerArn(this.handler), | ||
}); | ||
|
||
this.handler.addPermission(`${Names.nodeUniqueId(this.authorizer.node)}-Permission`, { | ||
scope: options.scope as CoreConstruct, | ||
principal: new ServicePrincipal('apigateway.amazonaws.com'), | ||
sourceArn: Stack.of(options.route).formatArn({ | ||
service: 'execute-api', | ||
resource: options.route.webSocketApi.apiId, | ||
resourceName: `authorizers/${this.authorizer.authorizerId}`, | ||
}), | ||
}); | ||
} | ||
|
||
return { | ||
authorizerId: this.authorizer.authorizerId, | ||
authorizationType: 'CUSTOM', | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* constructs the authorizerURIArn. | ||
*/ | ||
function lambdaAuthorizerArn(handler: IFunction) { | ||
return `arn:${Stack.of(handler).partition}:apigateway:${Stack.of(handler).region}:lambda:path/2015-03-31/functions/${handler.functionArn}/invocations`; | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/@aws-cdk/aws-apigatewayv2-authorizers/test/websocket/lambda.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Template } from '@aws-cdk/assertions'; | ||
import { WebSocketApi } from '@aws-cdk/aws-apigatewayv2'; | ||
import { WebSocketLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations'; | ||
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import { WebSocketLambdaAuthorizer } from '../../lib'; | ||
|
||
describe('WebSocketLambdaAuthorizer', () => { | ||
test('default', () => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
const handler = new Function(stack, 'auth-function', { | ||
runtime: Runtime.NODEJS_12_X, | ||
code: Code.fromInline('exports.handler = () => {return true}'), | ||
handler: 'index.handler', | ||
}); | ||
const integration = new WebSocketLambdaIntegration( | ||
'Integration', | ||
handler, | ||
); | ||
|
||
const authorizer = new WebSocketLambdaAuthorizer('default-authorizer', handler); | ||
|
||
// WHEN | ||
new WebSocketApi(stack, 'WebSocketApi', { | ||
connectRouteOptions: { | ||
integration, | ||
authorizer, | ||
}, | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Authorizer', { | ||
Name: 'default-authorizer', | ||
AuthorizerType: 'REQUEST', | ||
IdentitySource: [ | ||
'$request.header.Authorization', | ||
], | ||
}); | ||
|
||
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Route', { | ||
AuthorizationType: 'CUSTOM', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.