Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set API Key to be required for AWS ApiGateway endpoint (Swagger import)

I try to define my AWS Api Gateway infrastructure using Swagger/OpenAPI. Everything is working so far, however I have problems enabling the need for an API-Key for my endpoints.

My Swagger file looks like this (shortened):

---
swagger: 2.0
basePath: /dev
info:
  title: My API
  description: Proof of concept
schemes:
  - https
securityDefinitions:
  api_key:
    type: apiKey
    name: X-Api-Key
    in: header

paths:
  /example-path:
    options:
      consumes:
        - application/json
      produces:
        - application/json
      x-amazon-apigateway-integration:
        type: mock
        requestTemplates:
          application/json: |
            {
              "statusCode" : 200
            }
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'GET,HEAD,OPTIONS'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
            responseTemplates:
              application/json: |
                {}
    responses:
      200:
        description: Default response for CORS method
        headers:
          Access-Control-Allow-Headers:
            type: "string"
          Access-Control-Allow-Methods:
            type: "string"
          Access-Control-Allow-Origin:
            type: "string"          

    get:
      security:
        - api_key: []
      x-amazon-apigateway-integration:

        # Further definition of the endpoint, calling Lambda etc...

Linked inside a CloudFormation template the Swagger file is processed successfully. But when I open the endpoint in the AWS Web Console, the flag for API Key Required is still false.

Any suggestions? Thanks.

like image 347
philsch Avatar asked Mar 02 '17 10:03

philsch


1 Answers

Found the solution: the API key has to be named x-api-key (all lowercase).

It seems like only this way the setting is recognized during import.

like image 185
philsch Avatar answered Oct 07 '22 22:10

philsch