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

chore(apigateway): improve docs and default value settings for identity source in authorizer #28214

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Construct } from 'constructs';
import { IdentitySource } from './identity-source';
import * as cognito from '../../../aws-cognito';
import { Duration, FeatureFlags, Lazy, Names, Stack } from '../../../core';
import { APIGATEWAY_AUTHORIZER_CHANGE_DEPLOYMENT_LOGICAL_ID } from '../../../cx-api';
Expand Down Expand Up @@ -33,8 +34,9 @@ export interface CognitoUserPoolsAuthorizerProps {

/**
* The request header mapping expression for the bearer token. This is typically passed as part of the header, in which case
* this should be `method.request.header.Authorizer` where Authorizer is the header containing the bearer token.
* @see https://docs.aws.amazon.com/apigateway/api-reference/link-relation/authorizer-create/#identitySource
* this should be `method.request.header.Authorizer` where `Authorizer` is the header containing the bearer token.
*
* @see https://docs.aws.amazon.com/apigateway/latest/api/API_CreateAuthorizer.html#apigw-CreateAuthorizer-request-identitySource
* @default `IdentitySource.header('Authorization')`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default value setting process has been changed to fit here.

*/
readonly identitySource?: string;
Expand Down Expand Up @@ -78,7 +80,7 @@ export class CognitoUserPoolsAuthorizer extends Authorizer implements IAuthorize
type: 'COGNITO_USER_POOLS',
providerArns: props.cognitoUserPools.map(userPool => userPool.userPoolArn),
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds(),
identitySource: props.identitySource || 'method.request.header.Authorization',
identitySource: props.identitySource || IdentitySource.header('Authorization'),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change caused by this is guaranteed by this test.

};

this.authorizerProps = authorizerProps;
Expand Down
12 changes: 7 additions & 5 deletions packages/aws-cdk-lib/aws-apigateway/lib/authorizers/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Construct } from 'constructs';
import { IdentitySource } from './identity-source';
import * as iam from '../../../aws-iam';
import * as lambda from '../../../aws-lambda';
import { Arn, ArnFormat, Duration, FeatureFlags, Lazy, Names, Stack } from '../../../core';
Expand Down Expand Up @@ -182,8 +183,9 @@ export interface TokenAuthorizerProps extends LambdaAuthorizerProps {

/**
* The request header mapping expression for the bearer token. This is typically passed as part of the header, in which case
* this should be `method.request.header.Authorizer` where Authorizer is the header containing the bearer token.
* @see https://docs.aws.amazon.com/apigateway/api-reference/link-relation/authorizer-create/#identitySource
* this should be `method.request.header.Authorizer` where `Authorizer` is the header containing the bearer token.
*
* @see https://docs.aws.amazon.com/apigateway/latest/api/API_CreateAuthorizer.html#apigw-CreateAuthorizer-request-identitySource
* @default `IdentitySource.header('Authorization')`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default value setting process has been changed to fit here.

*/
readonly identitySource?: string;
Expand Down Expand Up @@ -216,7 +218,7 @@ export class TokenAuthorizer extends LambdaAuthorizer {
authorizerUri: lambdaAuthorizerArn(props.handler),
authorizerCredentials: props.assumeRole?.roleArn,
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds() ?? Duration.minutes(5).toSeconds(),
identitySource: props.identitySource || 'method.request.header.Authorization',
identitySource: props.identitySource || IdentitySource.header('Authorization'),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change caused by this is guaranteed by this test.

identityValidationExpression: props.validationRegex,
};

Expand All @@ -242,14 +244,14 @@ export interface RequestAuthorizerProps extends LambdaAuthorizerProps {
/**
* An array of request header mapping expressions for identities. Supported parameter types are
* Header, Query String, Stage Variable, and Context. For instance, extracting an authorization
* token from a header would use the identity source `IdentitySource.header('Authorizer')`.
* token from a header would use the identity source `IdentitySource.header('Authorization')`.
*
* Note: API Gateway uses the specified identity sources as the request authorizer caching key. When caching is
* enabled, API Gateway calls the authorizer's Lambda function only after successfully verifying that all the
* specified identity sources are present at runtime. If a specified identify source is missing, null, or empty,
* API Gateway returns a 401 Unauthorized response without calling the authorizer Lambda function.
*
* @see https://docs.aws.amazon.com/apigateway/api-reference/link-relation/authorizer-create/#identitySource
* @see https://docs.aws.amazon.com/apigateway/latest/api/API_CreateAuthorizer.html#apigw-CreateAuthorizer-request-identitySource
*/
readonly identitySources: string[];
}
Expand Down
Loading