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(apigateway): lambda token authorizer #5197

Merged
merged 14 commits into from
Jan 2, 2020

Conversation

nija-at
Copy link
Contributor

@nija-at nija-at commented Nov 26, 2019

Authorizers provide functionality to manage and control access to
specific or all methods on a RestApi endpoint.

The lambda token authorizer expects the authorization token to be part
of the request's header and passes this to a lambda function that can
then either allow or deny access the requester access to the resource.

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

closes #1402


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

@nija-at nija-at self-assigned this Nov 26, 2019
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 26, 2019
@mergify
Copy link
Contributor

mergify bot commented Nov 26, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

@nija-at nija-at changed the title feat(apigateway): L2 support for lambda token authorizers feat(apigateway): l2 support for lambda token authorizers Nov 26, 2019
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

  • PR title: we normally don't use the term L2 formally because everything we implement is effectively at the L2 layer. Therefore I would change the title to feat(apigateway): lambda token authorizers.

packages/@aws-cdk/aws-apigateway/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/README.md Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/method.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/package.json Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
@nija-at nija-at changed the title feat(apigateway): l2 support for lambda token authorizers feat(apigateway): lambda token authorizers Nov 26, 2019
@nija-at nija-at changed the title feat(apigateway): lambda token authorizers feat(apigateway): lambda token authorizer Nov 26, 2019
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/package.json Outdated Show resolved Hide resolved
/**
* Returns a lazy that will resolve to the restApiId at the time of synthesis.
*/
protected get restApiId(): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I think the base class is basically not needed. The IAuthorizer interface should implement the "bind" pattern (like lambda.Code) and allow derived classes to access the RestApi object. There is no need to store it at the base class.

This is also inline with my comment below about authorizers reporting their own type, which will make the API much cleaner.

So instead of:

authorizationType: apigateway.AuthorizationType.CUSTOM,
authorizer: auth

The eventual API I would envision is:

authorizer: apigw.Authorizer.custom(lambdaHandler, { /* ... */ });

Or for IAM:

authorizer: apigw.Authorizer.iam()

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Still think we should do Authorizer.token

packages/@aws-cdk/aws-apigateway/lib/authorizer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

packages/@aws-cdk/aws-apigateway/lib/authorizer.ts Outdated Show resolved Hide resolved
* Check if the given object is of type CustomAuthorizer
* @internal
*/
public static _isAuthorizer(x: any): x is AuthorizerBase {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this internal? Wouldn't users want this functionality as well?

* The authorizer ID.
* @attribute
*/
public abstract readonly authorizerId: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a duplication, no? Also available in AuthorizerConfig...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Required since this is a subclass of IAuthorizer. Typescript enforces that all abstract subclasses either implement abstract methods and properties, or re-indicate them as being abstract.

packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

5 similar comments
@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

@mergify
Copy link
Contributor

mergify bot commented Dec 16, 2019

Thanks so much for taking the time to contribute to the AWS CDK ❤️

We will shortly assign someone to review this pull request and help get it
merged. In the meantime, please take a minute to make sure you follow this
checklist
:

  • PR title type(scope): text
    • type: fix, feat, refactor go into CHANGELOG, chore is hidden
    • scope: name of module without aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
    • text: use all lower-case, do not end with a period, do not include issue refs
  • PR Description
    • Rationale: describe rationale of change and approach taken
    • Issues: indicate issues fixed via: fixes #xxx or closes #xxx
    • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Testing
    • Unit test added. Prefer to add a new test rather than modify existing tests
    • CLI or init templates change? Re-run/add CLI integration tests
  • Documentation
    • README: update module README to describe new features
    • API docs: public APIs must be documented. Copy from official AWS docs when possible
    • Design: for significant features, follow design process

* Authorizer -> Authorization
* drop using Physical Name
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Elad Ben-Israel and others added 2 commits January 2, 2020 10:07
* simplify authorizers class design

- rename `AuthorizerBase` to `Authorizer`. This class should actually have the `CfnAuthorizer` instantiation, but will only be introduced when an additional authorizer is included.
- simplify `AuthorizerBase` dramatically
- move logic to cache `restApiId` from `AuthorizerBase` to `TokenAuthorizer`. When an additional authorizer is added, we will refactor.
- remove the usage `Authorizer.token`. It is non-idiomatic in this context since we support one authorizer reused multiple times.

* moved Authorizer to authorizer.ts

* fix broken references and types

Co-authored-by: Niranjan Jayakar <16217941+nija-at@users.noreply.github.com>
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

eladb
eladb previously requested changes Jan 2, 2020
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

A few comments were marked as resolved but the code still wasn't changed. Please make sure to only resolve comments that are applied.

packages/@aws-cdk/aws-apigateway/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/package.json Show resolved Hide resolved
@@ -586,23 +586,23 @@ export = {
ResourceId: { Ref: "myapichildA0A65412" },
Integration: { Type: 'AWS' },
AuthorizerId: 'AUTHID',
AuthorizationType: 'COGNITO_USER_POOLS',
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this test modified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CF doc for Method states in the the AuthorizationType property section - "If you specify the AuthorizerId property, specify CUSTOM for this property. "

The original change contained validation to this effect and hence the test needed to be modified.

Since then, I think there is a documentation error here because the rule is not true when using cognito for authentication. In this case, both the AuthenticationType property on the Method and type property on the Authorizer should be COGNITO_USER_POOL.

I've reverted changes to this test file. At some point in the future, we might need additional validation, but I'd rather not mix this up with this (already long) PR.

packages/@aws-cdk/aws-apigateway/test/test.restapi.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-apigateway/test/test.restapi.ts Outdated Show resolved Hide resolved
@nija-at
Copy link
Contributor Author

nija-at commented Jan 2, 2020

A few comments were marked as resolved but the code still wasn't changed. Please make sure to only resolve comments that are applied.

I am in the process of modifying the PR. The resolved comments have been addressed locally on my workspace, but the code is not ready to be pushed yet.
Resolving comments and pushing code updates cannot be performed atomically. Once it's all ready for another round of review, I will either 're-request review' or 'dismiss review'.

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Please provide a bit more detail in the PR description

@nija-at nija-at added pr/do-not-merge This PR should not be merged at this time. and removed pr/do-not-merge This PR should not be merged at this time. labels Jan 2, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 2, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 2, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@mergify mergify bot merged commit 5c16744 into master Jan 2, 2020
@mergify mergify bot deleted the nija-at/apigateway-lambdaauthorizer branch January 2, 2020 16:28
@netroy
Copy link
Contributor

netroy commented Jan 7, 2020

@nija-at @eladb
looks like Authorizer name isn't optional.
CFN docs seem inconsistent with the APIGateway API docs (both v1 & v2).

This is breaking the aws-apigateway integration tests as well.
another related issue: #5678

I can send a quick PR to make either make name required, or define a default.
What would you prefer?

@nija-at
Copy link
Contributor Author

nija-at commented Jan 8, 2020

Internal reference: issues/CFN-30099

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[apigateway] add support for lambda token authorizer
4 participants