将API密钥设置为AWS ApiGateway端点所需的密钥(Swagger导入)

9njqaruj  于 2023-06-29  发布在  Pig
关注(0)|答案(3)|浏览(184)

我尝试使用Swagger/OpenAPI定义我的AWS API Gateway基础设施。到目前为止,一切都正常,但是我在为我的端点启用API密钥时遇到了问题。
我的Swagger文件看起来像这样(缩短):

---
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...

链接到CloudFormation模板中的Swagger文件已成功处理。但是当我在AWS Web Console中打开端点时,API Key Required的标志仍然是 false
有什么建议吗?谢谢

z4iuyo4d

z4iuyo4d1#

找到解决方案:API密钥必须命名为x-api-key(全部小写)。
似乎只有这样才能在导入过程中识别设置。

pbwdgjma

pbwdgjma2#

要启用所需的API密钥,您需要在安全方案块中添加此"x-amazon-apigateway-api-key-source" : "HEADER"
请看一个例子:

"components" : {
        "securitySchemes" : {
          "api-key" : {
            "type" : "apiKey",
            "name" : "x-api-key",
            "in" : "header",
            "x-amazon-apigateway-api-key-source" : "HEADER"
          }
        }
      }

这是一个使用代理请求的例子。JSON应该是这样的:Openapi3

{
    "openapi": "3.0.3",
    "info": {
      "title": "User Portal",
      "description": "API focused in User Portal.",
      "version": "v1"
    },
    "paths": {
      "users/{proxy+}": {
        "options": {
            "x-amazon-apigateway-integration": {
              "httpMethod": "OPTIONS",
              "payloadFormatVersion": "1.0",
              "type": "MOCK"
            }
          },
        "x-amazon-apigateway-any-method": {
          "produces":[ "application/json"],
          "parameters": [
            {
              "name": "proxy",
              "in": "path",
              "required": "true",
              "type": "string"
            }
          ],
          "responses": {},
          "security": [
            {
              "api-key": []
          }
        ],
          "x-amazon-apigateway-integration": {
            "uri":"https://test.com.br/users/{proxy}",
            "httpMethod":"ANY",
            "type": "HTTP_PROXY"
          }
        }
      }
    },
    "components" : {
        "securitySchemes" : {
          "api-key" : {
            "type" : "apiKey",
            "name" : "x-api-key",
            "in" : "header",
            "x-amazon-apigateway-api-key-source" : "HEADER"
          }
        }
      }
  }

在openapi2中,你可以在你的yml中添加这个。

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
    x-amazon-apigateway-api-key-source: HEADER

如果你在使用openapi时遇到了问题,你可以看看这篇文章:Working with API Gateway extensions to OpenAPI

yrefmtwq

yrefmtwq3#

我使用sam模板进行部署,并且我没有为API或Functions指定ApiKeyRequired(因为我的理解是sam模板不会覆盖外部定义主体中定义的api密钥配置),并且正在尝试在我创建的开放api规范中包含的主体定义中定义。我能够获得API_key以选择性地应用于我的每个方法/端点的唯一方法是使用Open API2而不是Open Api3
在我的Open API 2版本中,在需要api密钥的端点上,我在需要密钥的端点上设置了这个:
安全:- API_key:[详细]
然后是这个
安全性定义:API_key:type:“apiKey”名称:“x-api-key”在:“标题”
这样就可以工作了,sam deploy将API_key适当地应用到我需要它的端点上。
但是,如果我使用Open API 3,api密钥应用:
安全:- API_key:[]::components:securitySchemes:API_key:type:“apiKey”名称:“x-api-key”在:“header”“x-amazon-apigateway-api-key-source”:“标题”
有谁知道我需要做什么来获得API_key以使用Open Api 3文档应用?我的所有其他API都使用3规范,并不是真的想使用2,只是因为我不能正确部署3。

相关问题