Skip to content

Conversation

@cfbarbero
Copy link
Contributor

@cfbarbero cfbarbero commented May 31, 2019

Issue #, if available:
Fixes #867

Description of changes:
Update the Auth property to include a new sub-property ApiKeyRequired: <boolean>. This will be supported on both the Api Auth Method and the Function Auth Method. The function level specification will override the API level.

For example:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: API Gateway with ApiKey Auth
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        ApiKeyRequired: true

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        ApiKeyFalse:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: get
            # This is a public endpoint
            Auth:
              ApiKeyRequired: false
        ApiKeyTrue:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: get
            Auth:
              ApiKeyRequired: true
        ApiKeyDefault:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /other
            Method: get

This is roughly based upon #444. It implements part of #547.

In implementing this change, I refactored the support for the Authorizers in the swagger.py file. By doing this, it changed the order in which some of the generated swagger properties are translated and there by required updates to the following translator test output files:

  • tests/translator/output/api_with_aws_iam_auth_overrides.json
  • tests/translator/output/api_with_default_aws_iam_auth.json

Description of how you validated changes:
Added additional translator tests:

  • tests/translator/input/api_with_apikey_default_override.yaml
  • tests/translator/input/api_with_apikey_required.yaml
    Updated existing translator tests:
  • tests/translator/input/globals_for_api.yaml
  • tests/translator/input/implicit_api_with_auth_and_conditions_max.yaml

Checklist:

  • Write/update tests
  • make pr passes
  • Update documentation

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

@brettstack
Copy link
Contributor

it changed the order in which some of the generated swagger properties

Please try to maintain the Swagger integrity as SAM will create an APIGW Deployment when it detects Swagger has changed and we strongly try not to do an APIGW Deployment unless the user actually made a change to their API.

@cfbarbero cfbarbero force-pushed the apikey-refactor branch 2 times, most recently from 8d692a1 to 0ff2b41 Compare June 5, 2019 14:35
@cfbarbero
Copy link
Contributor Author

@brettstack
I have fixed the bug that caused the AWS_IAM security definition to get reordered. I have reverted the test output files for the 2 tests (across all 3 regions) that I had changed to accomodate the updated ordering and they are passing.

Let me know if there's anything else you'd like me to address.

@codecov-io
Copy link

codecov-io commented Jun 5, 2019

Codecov Report

Merging #943 into develop will increase coverage by 0.08%.
The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop    #943      +/-   ##
==========================================
+ Coverage    94.71%   94.8%   +0.09%     
==========================================
  Files           69      69              
  Lines         3160    3234      +74     
  Branches       606     629      +23     
==========================================
+ Hits          2993    3066      +73     
- Misses          85      87       +2     
+ Partials        82      81       -1
Impacted Files Coverage Δ
samtranslator/model/api/api_generator.py 94.85% <100%> (+1.26%) ⬆️
samtranslator/model/eventsources/push.py 88.38% <50%> (-0.47%) ⬇️
samtranslator/swagger/swagger.py 97.49% <93.26%> (-0.18%) ⬇️

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 88df20c...36cf808. Read the comment docs.


apikey_required_setting = self.Auth.get('ApiKeyRequired')
apikey_required_setting_is_false = apikey_required_setting is not None and not apikey_required_setting
if apikey_required_setting_is_false and not api_auth.get('ApiKeyRequired'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Throwing an exception here is not required, as this would be a no-op on api gw.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like the Authorizers implementation, I am storing the explicit ApiKeyRequired: False for a function in the security block for that function as api_key_false.

"security": { "api_key_false": [] }

This security option gets cleaned up by the swagger.set_path_default_apikey_required method, however, this is only called when there is a default ApiKeyRequired setting on the Serverless API. If there is no default setting (or no explicit Serverless API) then we never loop back and cleanup the api_key_false security setting.

I assumed we didn't want to spit out this security setting, however, I'll defer to your judgement. I'm also open to other ideas on this.

self._set_default_apikey_required(swagger_editor)

# Assign the Swagger back to template
self.definition_body = swagger_editor.swagger
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please rebase and add/update a test to ensure this works with openapi 3 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.

@praneetap , I have rebased. I am happy to add a test for openapi 3. Can you provide a little direction on that? Are there other tests I can refer to as an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@praneetap , I've added a couple of openapi3 translator tests. Please let me know if there's anything else you'd like to see on this.

New Test:

  • api_with_apikey_required_openapi_3

Updated Tests:

  • api_with_auth_all_maximum_openapi_3
  • api_with_auth_all_maximum

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, will be happy to! OpenApi tests are run here.
You can find more examples of input templates here if you search for "openapi".

- Implement new translator tests
- Add swagger.py unit tests
- Update documentation
- refactor Auth handling in swagger.py
…ed just at a Function level, we need to ensure the OpenApi Auth post processing happens even if the API has no explicit Auth Settings
@cfbarbero
Copy link
Contributor Author

@praneetap I have addressed your PR comments, this is ready for review again.

rest_api.BodyS3Location = self._construct_body_s3_dict()
elif self.definition_body:
# # Post Process OpenApi Auth Settings
self.definition_body = self._openapi_auth_postprocess(self.definition_body)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@praneetap In order to support the ApiKeyRequired setting at the Function level (without requiring an explicit setting on the API), I had to move the _openapi_auth_postprocess method call up a level to ensure it was called on every template reqardless of if there's an explicit API Auth setting. If you have another suggestion I'm happy to change.

@brettstack brettstack merged commit 0d12466 into aws:develop Jul 3, 2019
@cfbarbero cfbarbero deleted the apikey-refactor branch July 8, 2019 17:02
@emonhaider
Copy link

Is there an ETA when these changes are getting merged to master?

@jlhood
Copy link
Contributor

jlhood commented Aug 28, 2019

@emonhaider We are working on the v1.14.0 release now. We can't give exact dates, but it's coming soon.

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.

6 participants