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

Lambda authorizers support (API, DataStore, PubSub) #8616

Merged
merged 3 commits into from
Jul 26, 2021
Merged

Conversation

iartemiev
Copy link
Member

@iartemiev iartemiev commented Jul 21, 2021

note: opening this as a branch in upstream in order to run e2e tests.

Description of changes

Adds support for Lambda Authorizers for AppSync APIs to the API category and DataStore.

  • refactored/DRY'd some of our existing unit tests

API for leveraging Lambda auth

API Category

let authToken = 'some-auth-token-string'; // developer-managed token

// if AWS_LAMBDA is the default auth type, i.e., aws_appsync_authenticationType: 'AWS_LAMBDA':
const blogs = await API.graphql({query: queries.listBlogs, authToken: authToken});

// if you have a different default auth type configured:
const blogs = await API.graphql({query: queries.listBlogs, authMode: 'AWS_LAMBDA', authToken: authToken});

DataStore

let authToken = 'some-auth-token-string'; // developer-managed token

DataStore.configure({
  authProviders: {
    functionAuthProvider: () => (
      {
        token: authToken,
      }
    ),
  },
});

// Supports promises
DataStore.configure({
  authProviders: {
    functionAuthProvider: async () => {
      const authToken = await getAuthToken();
      return {
        token: authToken,
      }
    },
  },
});

const blogs = await DataStore.query(Blog);

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced) (will be added before release to latest)

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

@iartemiev iartemiev force-pushed the lambda-auth branch 2 times, most recently from 646c831 to e59299b Compare July 21, 2021 18:53
@lgtm-com
Copy link

lgtm-com bot commented Jul 21, 2021

This pull request introduces 3 alerts when merging e59299b into 85e9b97 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class
  • 1 for Useless assignment to local variable

@codecov-commenter
Copy link

Codecov Report

Merging #8616 (e59299b) into main (85e9b97) will increase coverage by 0.02%.
The diff coverage is 71.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8616      +/-   ##
==========================================
+ Coverage   77.80%   77.83%   +0.02%     
==========================================
  Files         240      240              
  Lines       17122    17161      +39     
  Branches     3651     3664      +13     
==========================================
+ Hits        13322    13357      +35     
- Misses       3675     3679       +4     
  Partials      125      125              
Impacted Files Coverage Δ
packages/datastore/__tests__/helpers.ts 100.00% <ø> (ø)
...ages/datastore/src/sync/processors/subscription.ts 35.96% <0.00%> (+1.03%) ⬆️
...pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts 18.05% <14.28%> (-0.03%) ⬇️
packages/datastore/src/sync/utils.ts 88.60% <33.33%> (-1.86%) ⬇️
packages/datastore/src/sync/processors/mutation.ts 65.96% <66.66%> (-0.17%) ⬇️
packages/api-graphql/src/GraphQLAPI.ts 90.05% <95.45%> (+2.10%) ⬆️
packages/api-graphql/src/types/index.ts 100.00% <100.00%> (ø)
...astore/src/authModeStrategies/multiAuthStrategy.ts 92.06% <100.00%> (+0.53%) ⬆️
packages/datastore/src/datastore/datastore.ts 80.35% <100.00%> (+0.10%) ⬆️
packages/datastore/src/sync/processors/sync.ts 41.40% <100.00%> (+0.37%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85e9b97...e59299b. Read the comment docs.

Copy link
Contributor

@nickarocho nickarocho left a comment

Choose a reason for hiding this comment

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

LGTM! Super clean, great work @iartemiev 🥂 Would love to learn more about the use cases and can't wait to try this out once it's available.

@@ -51,6 +57,11 @@ function getAuthRules({

rules.forEach(rule => {
switch (rule.allow) {
case 'custom':
if (rule.provider === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious about the other use cases for rule.provider? When would it not be a function?

Copy link
Member Author

Choose a reason for hiding this comment

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

For this new custom auth strategy, function is the only valid provider at the moment, but we may add additional ones in the future. I added the if statement there to make it clear where to handle a new provider if/when we extend it down the road

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Very scalable and forward-thinking, great stuff Ivan 😊🚀

Copy link
Member

@svidgen svidgen left a comment

Choose a reason for hiding this comment

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

No blockers that I can see.

One thing I'd note overall, in places where we've created constants like ModelAttributeAuthProvider.FUNCTION, we really ought to be using them for the purposes of maintainability, semantic clarity, and fat-finger proofing.

}
}`;
const getEventDoc = parse(GetEvent);
const getEventQuery = print(getEventDoc);
Copy link
Member

Choose a reason for hiding this comment

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

Thank-you for DRYing!

@iartemiev iartemiev force-pushed the lambda-auth branch 2 times, most recently from d3e32e0 to 7aeacde Compare July 23, 2021 15:46
@iartemiev
Copy link
Member Author

Note: I'm going to do a normal PR merge instead of squashing in this case because I want API, PubSub, and DataStore to all get minor version bumps from this PR.

I've rebased/fixed up the previous commit messages into 3 commits total (one per category), so the commit history will remain clean and relevant.

@github-actions
Copy link

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2022
@HuiSF HuiSF deleted the lambda-auth branch September 27, 2024 16:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants