【问题标题】:AWS APIGateway with Cognito Authorizer defined in OpenAPI SpecAWS APIGateway 与 OpenAPI 规范中定义的 Cognito Authorizer
【发布时间】:2019-10-13 17:05:16
【问题描述】:

工具

  • Terraform v0.11.14

设置

  • 由 Terraform 管理的 API 网关,使用 OpenAPI 规范定义
  • Cognito 授权者

我正在尝试为我的 API 中的方法指定授权方。我可以使用控制台来做到这一点(它有很好的记录):

问题

我希望能够使用 OpenAPI 规范以编程方式进行设置。 AWS 的相关文档是here

它说你可以在 OpenAPI 规范中通过指定来创建 Authorizer 对象:

securitySchemes:
  NameOfCognitoAuthorizer:
    type: apiKey
    name: Authorization
    in: header
    x-amazon-apigateway-authtype: cognito_user_pools
    x-amazon-apigateway-authorizer:
      type: cognito_user_pools
      providerARNs:
      - 'arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}'

这按预期工作。

然后一旦完成,您应该能够将 Authorizer 应用于资源方法,如下所示:

post:
  summary: Create a new Item
  responses:
    '200':
      description: Ok
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Item'
  security:
    - NameOfCognitoAuthorizer:

但是,一旦我应用更改并在 AWS 控制台中检查 post 方法,我可以看到 Authorizer 尚未应用于 API 方法。谁能看到我做错了什么?

为了完整起见,我的 API 是使用 terraform 创建的:

resource "aws_api_gateway_rest_api" "this" {
  name        = "MyAPI"
  body        = "${file("./api-spec.yaml")}"

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

【问题讨论】:

    标签: amazon-web-services aws-api-gateway amazon-cognito


    【解决方案1】:

    我遇到了同样的问题,我发现这是因为我在方法上设置了security 属性。

    AWS docs 中提供的 JSON 示例是:

    "security": [
      {
        "MyUserPool": []
      }
    ],
    

    在 YAML 中翻译为:

    security:
    - MyUserPool: []
    

    注意空数组的显式设置。希望这也能为您解决问题。

    【讨论】:

      【解决方案2】:

      我有同样的问题。下面是一个有效的 OpenAPI 规范示例:

      openapi: 3.0.0
      info:
        title: Sample API
        description: api description here
        version: '0.1'
      paths:
        /:
          get:
            summary: Describe the endpoint
            responses:
              '200':
                description: "All good"
            security:
              - EndpointAuthorizer: ["test/read"]
            x-amazon-apigateway-integration:
              uri: arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/arn:aws:lambda:{region}:{account_id}:function:{function_name}/invocations
              passthroughBehavior: "when_no_match"
              httpMethod: "POST"
              type: "aws_proxy"
      components:
        securitySchemes:
          EndpointAuthorizer:
            type: apiKey
            name: Authorization
            in: header
            x-amazon-apigateway-authtype: cognito_user_pools
            x-amazon-apigateway-authorizer:
              type: cognito_user_pools
              providerARNs:
                - arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}
      

      关键是端点上的安全引用(注意test/read这里是我在Cognito上定义的范围,但你可以使用空数组[]):

            security:
              - EndpointAuthorizer: ["test/read"]
      

      在上面的定义中,AWS 将导入 components.securitySchemes 中定义的 Cognito Authorizer(在我的例子中名为 EndpointAuthorizer),但如果您愿意,您可以使用 Terraform 创建它(只要确保将其从OpenAPI 规范)

      【讨论】:

      • 如果您使用空数组,会有一个微妙但非常重要的区别:空数组将尝试作为 ID 令牌进行验证,而如果您有一个或多个值它将验证承载作为访问令牌
      猜你喜欢
      • 2021-09-05
      • 2020-11-08
      • 2021-02-02
      • 2018-03-23
      • 1970-01-01
      • 2021-10-08
      • 2021-09-05
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多