工具
设置
我正在尝试为我的 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"]
}
}
最佳答案
我遇到过同样的问题。这是一个有效的 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 将导入在
EndpointAuthorizer
中定义的 Cognito 授权器(在我的例子中名为 components.securitySchemes
)。 ,但如果您愿意,也可以使用 Terraform 创建它(只需确保将其从 OpenAPI 规范中删除)
关于amazon-web-services - 具有 OpenAPI 规范中定义的 Cognito Authorizer 的 AWS APIGateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56337629/