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 Support for AuthorizationType to API #49

Closed
jkahn117 opened this issue Dec 28, 2016 · 15 comments
Closed

Add Support for AuthorizationType to API #49

jkahn117 opened this issue Dec 28, 2016 · 15 comments

Comments

@jkahn117
Copy link

API Endpoints often require authorization permissions, e.g. only IAM users. Ideally, AWS::Serverless::Api would also support AuthorizationType available for API Gateway.

For example, using IAM user:

Events:
  Type: Api
  Properties:
    Path: /secrets
    Method: get
    AuthorizationType: AWS_IAM

Or a custom authorizer:

Events:
  Type: Api
  Properties:
    Path: /secrets
    Method: get
    AuthorizationType: CUSTOM
    Authorizer: <ARN_CUSTOM_AUTHORIZER>

Realize this may be challenging in referencing the custom authorizer function if not a function defined within the template though.

@collinforrester
Copy link

If I'm understanding you correctly, have you tried defining what you're trying to get in the console and exporting the swagger file? I've found that my SAM templates support Cognito Authorization definitions by exporting an existing API built in the console and just using it as a starting point.

@jkahn117
Copy link
Author

Agreed, it is possible to make this work with a Swagger file, but that feels like an unnecessary step (export Swagger, modify, upload + manage separately) for something reasonably simple to configure as part of the API definition.

@sanathkr
Copy link
Contributor

sanathkr commented Jan 3, 2017

Thanks for the request. This is a good feature to have.

@mparaz
Copy link

mparaz commented Feb 1, 2017

It looks like using Swagger stopped working. This was previously working:

securityDefinitions:
  authorizerFunc:
    type: "apiKey"
    name: "Authorisation"
    in: "header"
    x-amazon-apigateway-authtype: "custom"
    x-amazon-apigateway-authorizer:
      authorizerUri: "arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:00000000:function:${stageVariables.AuthorizerFunctionName}/invocations"
      authorizerResultTtlInSeconds: 300
      type: "token"

But now I get:

Errors found during import: Unable to create authorizer 'authorizerFunc': Authorizers only support Lambda function invocations.

What is the correct syntax now?

@demurray
Copy link

demurray commented May 9, 2017

@mparaz, try nesting the authorizerUri value under a Fn::Sub element, i.e.:

authorizerUri:
  Fn::Sub: "arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:00000000:function:${stageVariables.AuthorizerFunctionName}/invocations"

@lafiosca
Copy link
Contributor

I don't think the Fn::Sub will help here. That looks to be an API Gateway stage variable rather than a CloudFormation variable.

@jaccus
Copy link

jaccus commented Jun 9, 2017

Fn::Sub together with ${stageVariables.MyVariableName}, where the variable is in the Variables section of the SAM template,

securityDefinitions:
          Authorizer:
            ...
            x-amazon-apigateway-authorizer:
              ...
              authorizerUri:
                Fn::Sub: "arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:<accid>:function:${stageVariables.MyVariableName}/invocations"
...
...
Variables:
        MyVariableName: !ImportValue MyImportedValue

does not seem to work for me in an inline swagger file and fails with the following error:
Status: FAILED. Reason: Template error: instance of Fn::Sub references invalid resource attribute stageVariables.MyVariableName.

Could someone confirm that this should work? Or maybe there is another way to access these variables with an inline swagger spec?

@sanathkr
Copy link
Contributor

Its happening! #248

@samwan
Copy link

samwan commented Jan 11, 2018

Whew, I spent most of the day trying to understand the workarounds and just checked this issue a few minutes ago! :-) Does #248 mean it will be straightforward to declare a AWS::ApiGateway::Authorizer resource with Type: "Cognito_user_pool"?

@johnbest
Copy link

New to AWS, and I am incredibly interested in the CORS/Authorizer thing.

(As I want to build a web stack using Cognito).

@WilixLead
Copy link

@sanathkr Still don't understand what I should use for adding Authorizer to my Api Event. Where I can found any documentation about it?
What the syntax?

@sanathkr
Copy link
Contributor

sanathkr commented Mar 8, 2018

@WilixLead Authorizer is not yet supported natively in SAM. #248 is parent tracking issue for all APIGW features.

You can always enable custom authorizers by explicitly defining APIs using Swagger file and including the Swagger with AWS::Serverless::Api resource - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-authorizer.html. Checkoug #25 for more discussion on how this could be implemented in Swagger.

@marczis
Copy link

marczis commented Mar 20, 2018

I got really confused with this.
So I can create an Authorizer like this:

    "MyAuthorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "Type": "COGNITO_USER_POOLS",
        "IdentitySource": "method.request.header.Auth",
        "Name": "MyAuthorizer",
        "ProviderARNs": ["arn:aws:cognito-idp:eu-west-1:XXXXXXXXXXXX:userpool/eu-west-1_XXXXXXXXXX"],
        "RestApiId": {
          "Ref": "ServerlessRestApi"
        }
      }

I struggle to understand if Cognito is considered as a custom authorizer or not ?
And so I do understand that SAM don't support Authorizer in the Serverless::Function right? Is there any workaround without writing swagger?

Thanks a lot in advance!

@brettstack
Copy link
Contributor

If you're interested in Authorizers and want to help shape the syntax, the RFC is over here #512. I'm going to extend the RFC closure date until end of this week.

@jkahn117 @collinforrester @mparaz @demurray @WilixLead @marczis @johnbest @jaccus @lafiosca

@sparrowt
Copy link

See #546 for recently added support

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

No branches or pull requests