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

Add authorizationscopes #917

Merged
merged 4 commits into from
Dec 6, 2019
Merged

Conversation

klmz
Copy link
Contributor

@klmz klmz commented May 14, 2019

Issue #, if available:
#652

Description of changes:
This adds support for authorisation scopes to the SAM spec. It is a small hanges to the swagger translator that add authorization scopes per method.

Description of how you validated changes:
I have added an e2e test in the from of an example yaml file (api_with_auth_with_scopes.yaml) with all usage options. I also added swagger specific tests to test_swagger.py.

Checklist:

  • Write/update tests
  • make pr passes
  • Update documentation
  • Add swagger test

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

@klmz klmz force-pushed the add-authorizationscopes branch from 117f266 to 274470e Compare May 14, 2019 07:44
@klmz
Copy link
Contributor Author

klmz commented May 14, 2019

Ok. I must admit that I don't know how. But I'm guessing it had something to do with linting. Once I made sure make pr passed the test worked on python 2 as well. Sorry for the noise, still have to write some test though. I will do this somewhere this week!

@klmz klmz force-pushed the add-authorizationscopes branch from a72d3d7 to f02b74d Compare May 14, 2019 17:53
@klmz klmz marked this pull request as ready for review May 14, 2019 17:57
@codecov-io
Copy link

codecov-io commented May 14, 2019

Codecov Report

Merging #917 into develop will increase coverage by 0.08%.
The diff coverage is 90%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #917      +/-   ##
===========================================
+ Coverage     94.5%   94.58%   +0.08%     
===========================================
  Files           72       78       +6     
  Lines         3746     4269     +523     
  Branches       742      850     +108     
===========================================
+ Hits          3540     4038     +498     
- Misses         104      111       +7     
- Partials       102      120      +18
Impacted Files Coverage Δ
samtranslator/model/api/api_generator.py 95.22% <ø> (ø) ⬆️
samtranslator/model/eventsources/push.py 90.51% <0%> (+0.67%) ⬆️
samtranslator/model/apigateway.py 97.9% <100%> (+0.01%) ⬆️
samtranslator/swagger/swagger.py 92.77% <100%> (+0.2%) ⬆️
samtranslator/model/sam_resources.py 95.08% <0%> (-0.57%) ⬇️
samtranslator/plugins/api/implicit_api_plugin.py 98.23% <0%> (-0.39%) ⬇️
samtranslator/model/naming.py 100% <0%> (ø) ⬆️
samtranslator/sdk/resource.py 100% <0%> (ø) ⬆️
samtranslator/translator/verify_logical_id.py 100% <0%> (ø) ⬆️
...ator/plugins/api/default_definition_body_plugin.py 100% <0%> (ø) ⬆️
... and 10 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 b56a09d...b8943c6. Read the comment docs.

@fyayc-ale
Copy link

@keetonian, sorry to ping you, would be possible to merge this? We are using a lambda to achieve the same and this will reduce errors on our side by a lot!

@klmz
Copy link
Contributor Author

klmz commented May 21, 2019

@keetonian, sorry to ping you, would be possible to merge this? We are using a lambda to achieve the same and this will reduce errors on our side by a lot!

Depends on how much you need this but you can pull this branch and run the transformation manually as described here: Dev guide

If you ever worked with python and virtualenv you can have it done in minutes.

@keetonian
Copy link
Contributor

@fyayc-ale @klmz Will review today. Thank you for pinging me.

@brettstack
Copy link
Contributor

@klmz thanks! Looks like it's missing AuthorizationScopes at the Api level? This should set scopes on all methods which use this authorizer (and can be overridden at the method level)

@klmz
Copy link
Contributor Author

klmz commented May 22, 2019

@brettstack I didn't do that because I tried to stay as close to the cloudformation spec. A default scope doesn't make a whole lot of sense to me. If it is default you can just as well leave the scope out I guess? Although I guess there are use cases where it could make sense. If you want most endpoints to be with a scope and just a couple without scopes. Or if you use the same token with different api's or something like that? Would like to hear you thoughts on this.
Actually, would also like your thoughts on my assumption of staying close to the CF spec. Is that something you strive for? Maybe this is more of general discussion for somewhere else 😅.

That being said, I guess there is some use for it so I expect to have some time to add the api level scopes this week.

@brettstack
Copy link
Contributor

Yes it's the scenario where you want most endpoints to have a certain configuration and you override the outliers.

@eterjoost
Copy link

eterjoost commented May 28, 2019

Lol. That was much more complex than I anticipated. I think I got the behavior down, but I need to write tests still and then check if I can refactor it a bit to make it more clear. I expect to have some time to finish this on Thursday!

@brettstack
Copy link
Contributor

Thanks @eterjoost we'll review when it's all ready. 👍

Copy link
Contributor

@brettstack brettstack left a comment

Choose a reason for hiding this comment

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

Looking good! Could you submit an example or update the existing Cognito example?

@klmz
Copy link
Contributor Author

klmz commented Jun 11, 2019

I need some help with fixing the tests. Some tests are now failing, but it is very hard to see why in the output. Am I doing something wrong? Or is this how it works? Maybe there is a better way of using the tests instead of just running them all?

@klmz
Copy link
Contributor Author

klmz commented Jun 11, 2019

It is an issue with the china and us-gov region. The output doesn't match the expected output, but I can't see what is different.

@keetonian
Copy link
Contributor

@klmz ok, we can look at it, thanks for bringing it up!

@ghost
Copy link

ghost commented Aug 9, 2019

Hi All,

This is a hugely important pull request and is great work by @klmz. Having the ability to use scopes within a SAM template is the only way to implement access tokens in an enterprise scenario.

Can someone have a look, help @klmz to get to the end goal or provide some input to how we could fix this?

Thanks in advance,

Mike

@leantorres73
Copy link

+1, This is a very important feature for implementing access_token and client_credentials grant type.

@louism517
Copy link

+1 - the client_credentials flow actually requires a scope for authentication.

@MohammedAlSafwanOld
Copy link

Any progress on this issue?!

@keetonian
Copy link
Contributor

@leantorres73 @louism517 I'm not familiar with those features, could you provide some more information about them?

@Michael1d I resolved the merge conflicts locally, but had a couple of questions regarding this feature and the overall scope. Could you give some more information about access tokens and how you use/would like to use them?

@ghost
Copy link

ghost commented Sep 19, 2019

Hi @keetonian,

Apologies for not get back to you sooner. In my scenario, I have API's that have many potential "clients" - think an SPA, an analytics ETL process, a B2B customer etc. I have created Cognito Application Clients that represent those use cases, assigning a single scope to each one. Therefore, I have the requirement to add one or more scopes selectively to serverless functions deployed by SAM. I hope that makes sense.

On the standard CF AWS::ApiGateway::Method, there is a property for AuthorizationScopes and the same functionality is required here with a AWS::Serverless::Function in SAM. I also like the idea of being able to set default scopes on a AWS::Serverless::Api as discussed in here as well.

Please let me know if you have any other questions or clarifications.

@keetonian
Copy link
Contributor

Just pushed up a few changes to fully support this feature.

@keetonian keetonian removed their assignment Dec 5, 2019
@keetonian keetonian changed the title [WIP] Add authorizationscopes Add authorizationscopes Dec 5, 2019
Auth:
Authorizer: MyDefaultCognitoAuth
AuthorizationScopes: []
CognitoDefaultAuythDefaultScopesNone:
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Copy link
Contributor

@praneetap praneetap left a comment

Choose a reason for hiding this comment

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

Can you add a test with openapi to make sure the auth scopes get transformed into openapi correctly? Also try deploying it end to end.

@keetonian
Copy link
Contributor

Scopes match the Swagger/OpenApi spec: https://swagger.io/docs/specification/authentication/

@keetonian
Copy link
Contributor

No APIGW extensions for scopes, they use the same implementation: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-authorizer.html

@keetonian
Copy link
Contributor

Implementation/deployment verified for scopes changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.