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

fix(apigatewayv2): defaultAuthorizer cannot be applied to HttpRoute #27576

Merged
merged 28 commits into from
Oct 27, 2023

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Oct 17, 2023

This PR fixes a bug that defaultAuthorizer cannot be applied to HttpRoute without an authorizer.

Closes #27436.


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

@github-actions github-actions bot added bug This issue is a bug. p2 admired-contributor [Pilot] contributed between 13-24 PRs to the CDK labels Oct 17, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team October 17, 2023 14:41
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation dismissed their stale review October 17, 2023 16:21

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@go-to-k go-to-k marked this pull request as ready for review October 19, 2023 05:57
@go-to-k go-to-k changed the title fix(apigatewayv2): can't apply defaultAuthorizer to HttpRoute fix(apigatewayv2): defaultAuthorizer can not be applied to HttpRoute Oct 19, 2023
@go-to-k go-to-k changed the title fix(apigatewayv2): defaultAuthorizer can not be applied to HttpRoute fix(apigatewayv2): defaultAuthorizer cannot be applied to HttpRoute Oct 19, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Oct 19, 2023
Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

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

Looks good overall 👍
Adding the properties to the IHttpApi interface results in a clearer implementation and it should not cause problems in my opinion.

Comment on lines 196 to 206
if (props.authorizer) {
this.authBindResult = props.authorizer.bind({
route: this,
scope: this.httpApi instanceof Construct ? this.httpApi : this, // scope under the API if it's not imported
});
} else if (this.httpApi instanceof HttpApi && this.httpApi.defaultAuthorizer) { // because IHttpApi as it is does not have a defaultAuthorizer
this.authBindResult = this.httpApi.defaultAuthorizer.bind({
route: this,
scope: this.httpApi, // this.httpApi is also a Construct because it is an HttpApi
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (props.authorizer) {
this.authBindResult = props.authorizer.bind({
route: this,
scope: this.httpApi instanceof Construct ? this.httpApi : this, // scope under the API if it's not imported
});
} else if (this.httpApi instanceof HttpApi && this.httpApi.defaultAuthorizer) { // because IHttpApi as it is does not have a defaultAuthorizer
this.authBindResult = this.httpApi.defaultAuthorizer.bind({
route: this,
scope: this.httpApi, // this.httpApi is also a Construct because it is an HttpApi
});
}
const authorizer = props.authorizer ?? this.httpApi.defaultAuthorizer;
this.authBindResult = authorizer?.bind({
route: this,
scope: this.httpApi instanceof Construct ? this.httpApi : this, // scope under the API if it's not imported
});

It should be fine to add defaultAuthorizer and defaultAuthorizationScopes to the IHttpApi interface (unless I'm missing something).

Comment on lines 214 to 225
if (this.authBindResult) {
if (props.authorizationScopes) {
authorizationScopes = Array.from(new Set([
...authorizationScopes ?? [],
...props.authorizationScopes,
]));
} else if (this.httpApi instanceof HttpApi && this.httpApi.defaultAuthorizationScopes) {// because IHttpApi as it is does not have a defaultAuthorizationScopes
authorizationScopes = Array.from(new Set([
...authorizationScopes ?? [],
...this.httpApi.defaultAuthorizationScopes,
]));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (this.authBindResult) {
if (props.authorizationScopes) {
authorizationScopes = Array.from(new Set([
...authorizationScopes ?? [],
...props.authorizationScopes,
]));
} else if (this.httpApi instanceof HttpApi && this.httpApi.defaultAuthorizationScopes) {// because IHttpApi as it is does not have a defaultAuthorizationScopes
authorizationScopes = Array.from(new Set([
...authorizationScopes ?? [],
...this.httpApi.defaultAuthorizationScopes,
]));
}
if (this.authBindResult && (props.authorizationScopes || this.httpApi.defaultAuthorizationScopes)) {
authorizationScopes = Array.from(new Set([
...authorizationScopes ?? [],
...props.authorizationScopes ?? this.httpApi.defaultAuthorizationScopes ?? [],
]));
}

const httpApi = new HttpApi(stack, 'MyHttpApi');
const httpApiWithDefaultAuthorizer = new HttpApi(stack, 'MyHttpApiWithDefaultAuthorizer', {
defaultAuthorizer,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add defaultAuthorizationScopes to the httpApiWithDefaultAuthorizer API?

Copy link
Contributor Author

@go-to-k go-to-k Oct 23, 2023

Choose a reason for hiding this comment

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

I changed integ.user-pool instead of integ.lambda because defaultAuthorizationScopes (Authorization Scopes) are only valid for COGNITO_USER_POOLS and JWT authorization types

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Thanks for the clarification.

@go-to-k
Copy link
Contributor Author

go-to-k commented Oct 23, 2023

@lpizzinidev

Thanks for your review. I changed, so please review again!

change an integ test
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation dismissed their stale review October 23, 2023 16:40

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

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

Thanks 👍
Some documentation adjustments are needed.
Also, can you please add back the default authorizer in the lambda integration test?
Is good to have some extra coverage.

readonly defaultAuthorizer?: IHttpRouteAuthorizer;

/**
* Default OIDC scopes attached to all routes in the gateway, unless explicitly configured on the route.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Default OIDC scopes attached to all routes in the gateway, unless explicitly configured on the route.
* Default OIDC scopes attached to all routes in the gateway, unless explicitly configured on the route.
* The scopes are used with a COGNITO_USER_POOLS authorizer to authorize the method invocation.
*

Can you please align this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check.

I also added a JWT authorizer to this doc.

40819a1

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please provide documentation that describes the usage with a JWT authorizer?
I was only able to verify the COGNITO_USER_POOLS authorizer.

Copy link
Contributor Author

@go-to-k go-to-k Oct 24, 2023

Choose a reason for hiding this comment

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

Oh... I tried in the integ.lambda with defaultAuthorizationScopes in httpApiWithDefaultAuthorizer, CFn occurred the following error message.

I'll see if I can find any documentation.

UPDATE_ROLLBACK_COMPLETE: Resource handler returned message: "Invalid Route authorization type specified. Authorization Scopes are only valid for COGNITO_USER_POOLS and JWT authorization types (Service: AmazonApiGatewayV2; Status Code: 400; Error Code: BadRequestException; Request ID: xxxxxx; Proxy: null)" (RequestToken: xxxxxx, HandlerErrorCode: GeneralServiceException)

Copy link
Contributor Author

@go-to-k go-to-k Oct 24, 2023

Choose a reason for hiding this comment

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

There is If you specify the AuthorizerId property, specify CUSTOM or COGNITO_USER_POOLS for this property. in the CFn doc.

I guess this CFn message represents CUSTOM COGNITO_USER_POOLS as JWT.

What should we do in CDK doc?

Copy link
Contributor Author

@go-to-k go-to-k Oct 24, 2023

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm starting to feel that COGNITO_USER_POOLS is all I need.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think that COGNITO_USER_POOLS is sufficient since it generates a JWT authorizer.

Copy link
Contributor Author

@go-to-k go-to-k Oct 24, 2023

Choose a reason for hiding this comment

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

In the meantime, please review again as I have added only COGNITO_USER_POOLS for the message.

Comment on lines 25 to 29
/**
* Default Authorizer to applied to all routes in the gateway
* @attribute
* @default - No authorizer
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/**
* Default Authorizer to applied to all routes in the gateway
* @attribute
* @default - No authorizer
*/
/**
* Default Authorizer applied to all routes in the gateway.
*
* @attribute
* @default - no default authorizer
*/

Can you please align this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check.

40819a1

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Oct 24, 2023
@go-to-k
Copy link
Contributor Author

go-to-k commented Oct 24, 2023

Also, can you please add back the default authorizer in the lambda integration test?

OK, I changed the integ.lambda and added snapshots.

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 24, 2023
Copy link
Contributor

@sumupitchayan sumupitchayan 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 you contribution!

@mergify
Copy link
Contributor

mergify bot commented Oct 27, 2023

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

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 27, 2023
@mergify
Copy link
Contributor

mergify bot commented Oct 27, 2023

Thank you for contributing! Your pull request will be updated from main 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: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 375b130
  • 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 f397071 into aws:main Oct 27, 2023
10 checks passed
@mergify
Copy link
Contributor

mergify bot commented Oct 27, 2023

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

mrgrain pushed a commit that referenced this pull request Nov 1, 2023
…27576)

This PR fixes a bug that `defaultAuthorizer` cannot be applied to `HttpRoute` without an authorizer.

Closes #27436.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@go-to-k go-to-k deleted the fix/apgwv2-route-authorizer branch November 22, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK bug This issue is a bug. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@aws-cdk/aws-apigatewayv2-authorizers-alpha: can't apply lambda defaultAuthorizer
4 participants