-
Notifications
You must be signed in to change notification settings - Fork 4k
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(appsync): Lambda Authorizer for AppSync GraphqlApi #16743
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd582b2
add lambdaauthorizerconfig
kaizencc fcf825f
wire lambdaconfig through graphql
kaizencc eb35dd4
test lambda auth
kaizencc 9146285
add Authorization to README and fix linter
kaizencc b415fca
add test for mode/type mismatch and single lambda clause
kaizencc a13acc2
Merge branch 'master' into appsync-lambda-auth
kaizencc f705b17
change property from 'functionArn' to 'handler'
kaizencc 10a4854
Merge branch 'appsync-lambda-auth' of https://github.com/kaizen303159…
kaizencc 4c09cbe
README update too
kaizencc 4247123
Merge branch 'master' into appsync-lambda-auth
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -29,6 +29,10 @@ export enum AuthorizationType { | |
* OpenID Connect authorization type | ||
*/ | ||
OIDC = 'OPENID_CONNECT', | ||
/** | ||
* Lambda authorization type | ||
*/ | ||
LAMBDA = 'AWS_LAMBDA', | ||
} | ||
|
||
/** | ||
|
@@ -58,6 +62,11 @@ export interface AuthorizationMode { | |
* @default - none | ||
*/ | ||
readonly openIdConnectConfig?: OpenIdConnectConfig; | ||
/** | ||
* If authorizationType is `AuthorizationType.LAMBDA`, this option is required. | ||
* @default - none | ||
*/ | ||
readonly lambdaAuthorizerConfig?: lambdaAuthorizerConfig; | ||
} | ||
|
||
/** | ||
|
@@ -150,6 +159,38 @@ export interface OpenIdConnectConfig { | |
readonly oidcProvider: string; | ||
} | ||
|
||
/** | ||
* Configuration for Lambda authorization in AppSync | ||
*/ | ||
export interface lambdaAuthorizerConfig { | ||
/** | ||
* The ARN for the authorizer lambda function. This may be a standard Lambda ARN, a version ARN (.../v3) or alias ARN. | ||
* Note: This Lambda function must have the following resource-based policy assigned to it. | ||
* When configuring Lambda authorizers in the console, this is done for you. | ||
* To do so with the AWS CLI, run the following: | ||
* | ||
* `aws lambda add-permission --function-name "arn:aws:lambda:us-east-2:111122223333:function:my-function" --statement-id "appsync" --principal appsync.amazonaws.com --action lambda:InvokeFunction` | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-graphqlapi-lambdaauthorizerconfig.html | ||
*/ | ||
readonly functionArn: string; | ||
|
||
/** | ||
* How long the results are cached. | ||
* Disable caching by setting this to 0. | ||
* | ||
* @default Duration.minutes(5) | ||
*/ | ||
readonly resultsCacheTtl?: Duration; | ||
|
||
/** | ||
* A regular expression for validation of tokens before the Lambda function is called. | ||
* | ||
* @default - no regex filter will be applied. | ||
*/ | ||
readonly validationRegex?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also accept |
||
} | ||
|
||
/** | ||
* Configuration of the API authorization modes. | ||
*/ | ||
|
@@ -418,6 +459,7 @@ export class GraphqlApi extends GraphqlApiBase { | |
logConfig: this.setupLogConfig(props.logConfig), | ||
openIdConnectConfig: this.setupOpenIdConnectConfig(defaultMode.openIdConnectConfig), | ||
userPoolConfig: this.setupUserPoolConfig(defaultMode.userPoolConfig), | ||
lambdaAuthorizerConfig: this.setupLambdaAuthorizerConfig(defaultMode.lambdaAuthorizerConfig), | ||
additionalAuthenticationProviders: this.setupAdditionalAuthorizationModes(additionalModes), | ||
xrayEnabled: props.xrayEnabled, | ||
}); | ||
|
@@ -497,6 +539,9 @@ export class GraphqlApi extends GraphqlApiBase { | |
if (mode.authorizationType === AuthorizationType.USER_POOL && !mode.userPoolConfig) { | ||
throw new Error('Missing User Pool Configuration'); | ||
} | ||
if (mode.authorizationType === AuthorizationType.LAMBDA && !mode.lambdaAuthorizerConfig) { | ||
throw new Error('Missing Lambda Configuration'); | ||
} | ||
}); | ||
if (modes.filter((mode) => mode.authorizationType === AuthorizationType.API_KEY).length > 1) { | ||
throw new Error('You can\'t duplicate API_KEY configuration. See https://docs.aws.amazon.com/appsync/latest/devguide/security.html'); | ||
|
@@ -551,13 +596,23 @@ export class GraphqlApi extends GraphqlApiBase { | |
}; | ||
} | ||
|
||
private setupLambdaAuthorizerConfig(config?: lambdaAuthorizerConfig) { | ||
if (!config) return undefined; | ||
return { | ||
authorizerResultTtlInSeconds: config.resultsCacheTtl?.toSeconds(), | ||
authorizerUri: config.functionArn, | ||
identityValidationExpression: config.validationRegex, | ||
}; | ||
} | ||
|
||
private setupAdditionalAuthorizationModes(modes?: AuthorizationMode[]) { | ||
if (!modes || modes.length === 0) return undefined; | ||
return modes.reduce<CfnGraphQLApi.AdditionalAuthenticationProviderProperty[]>((acc, mode) => [ | ||
...acc, { | ||
authenticationType: mode.authorizationType, | ||
userPoolConfig: this.setupUserPoolConfig(mode.userPoolConfig), | ||
openIdConnectConfig: this.setupOpenIdConnectConfig(mode.openIdConnectConfig), | ||
lambdaAuthorizerConfig: this.setupLambdaAuthorizerConfig(mode.lambdaAuthorizerConfig), | ||
}, | ||
], []); | ||
} | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is most similar to what CFN wants, which is an
authorizerUri
. However, I feel like it is a CDK pattern to ask forreadonly handler: lambda.IFunction
instead and then generate the ARN under the hood. Wanted to bring it up to make sure a second pair of eyes looks at this decision.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's even in the design guidelines:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. I'll reread the design guidelines and change this up to be
IFunction
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@otaviomacedo, I made the change, so this should be ready for primetime whenever you have a chance.