Skip to content

confused about apigateway authorization #2127

@autohandle

Description

@autohandle

i created a template using helloworld & i created 2 functions:

get /auth
get /noauth

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  AuthNoauth

  Sample SAM Template for AuthNoauth


Globals:
  Function:
    Timeout: 20

Resources:
  HelloAuthWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            RestApiId: !Ref TheApiGateway
            Path: /auth
            Method: get
            Auth:
              Authorizer: CustomAuthorizer
  HelloNoAuthWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api
          Properties:
            RestApiId: !Ref TheApiGateway
            Path: /noauth
            Method: get

i made an api gateway with a custom authorizer:

TheApiGateway:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: Prod
      Auth:
#        DefaultAuthorizer: CustomAuthorizer
        Authorizers:
          CustomAuthorizer:
            FunctionArn: !GetAtt AuthFunction.Arn
AuthFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: hello_world/
    Handler: app.lambda_handler
    Runtime: python3.8

this worked and i can see the authorizer on the Amazon API Gateway screen

then i added a DefinitionBody to the api gateway:

  TheApiGateway:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: Prod
      Auth:
#        DefaultAuthorizer: CustomAuthorizer
        Authorizers:
          CustomAuthorizer:
            FunctionArn: !GetAtt AuthFunction.Arn
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: openapi.yaml

and created an openapi for the endpoints

openapi: "3.0.2"
info:
  title: AuthNoAuth API
  version: "1.0"

paths:
  /auth:
    get:
      security:
        - ApiKeyAuth: []
      responses:
        '200':
          description: OK
      x-amazon-apigateway-integration:
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloAuthWorldFunction.Arn}/invocations
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"
  /noauth:
    get:
      responses:
        '200':
          description: OK
      x-amazon-apigateway-integration:
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloNoAuthWorldFunction.Arn}/invocations
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"

components:
  securitySchemes:
    ApiKeyAuth:
      name: Authorization
      type: apiKey
      in: header
      "x-amazon-apigateway-authtype": "CUSTOM"
      "x-amazon-apigateway-authorizer":
        "authorizerUri":
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AuthFunction.Arn}/invocations"
        "authorizerResultTtlInSeconds": 300
        "type": "token"

but, when i (re)visit the api gateway console, the authorizer is gone:

actually, it seems to be controlled by the DefaultAuthorizer in the 'AWS::Serverless::Api', if the default is on, they both have an authorizer and if it is commented out, neither does.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions