我有一个用 JSON 编写的 AWS SAM/CloudFormation 模板,其中包含:
- API 网关 API 的定义,其中使用以下方式定义端点
OpenAPI
DefinitionBody
格式 - API 网关授权方的定义,使用 SAM/CF 格式定义
我正在尝试找到一种方法来为我希望使用授权者的每个端点 (1) 引用授权者 (2)。
代码如下:
授权者定义:
"LambdaAuthorizer":{
"Type": "AWS::ApiGateway::Authorizer",
"Properties":{
"IdentitySource":"method.request.header.Authorization",
"Type":"TOKEN",
"RestApiId":{
"Ref": "ApiName"
},
"AuthorizerUri": {
"Fn::Join" : ["", ["arn:aws:apigateway:", {"Ref": "AWS::Region"}, ":lambda:path/2015-03-31/functions/", {"Fn::GetAtt": ["AuthLambda", "Arn"]}, "/invocations"]]
},
"IdentityValidationExpression": "^[a-zA-Z0-9]{3,32}$",
"AuthorizerResultTtlInSeconds": 300,
"AuthorizerCredentials": {
"Fn::GetAtt": ["LambdaAuthorizerRole", "Arn"]
},
"Name":"lambda-authorizer"
}
},
端点定义
"API": {
"Type": "AWS::Serverless::Api",
"Properties": {
...
"DefinitionBody": {
...
"paths": {
"/endpoint": {
"post": {
"responses": {
"200": {
"description": "200 response"
}
},
"x-amazon-apigateway-integration": {
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AuthLambda.Arn}/invocations"
},
"responses": {
"default": {
"statusCode": "200",
"contentHandling": "CONVERT_TO_TEXT"
}
},
"passthroughBehavior": "when_no_match",
"httpMethod": "POST",
"contentHandling": "CONVERT_TO_TEXT",
"type": "aws_proxy"
},
"security" : [{
"NAME OF OPEN API SECURITY DEFINITION":[] // Can I reference my existing Authorizer?
}]
}
},
}
}
}
}
如您所见,我找到了 OpenAPI security
属性,根据 AWS documentation用作对可以定义授权者的 securityDefinitions
的引用。
但是我已经在 CloudFormation JSON 中定义了我的授权者。我不能引用它吗?
最佳答案
API Gateway 不允许您直接引用 OpenAPI paths
中的现有授权者 ID。不过,有一个解决方法。您可以提供 securitySchemes
定义。 securitySchemes
的格式在 OpenAPI 2.0(又名 Swagger)和 OpenAPI 3.0 ( documentation here ) 之间有所不同。
确保 securitySchemes
的详细信息与现有 API 授权程序完全匹配。 如果一切匹配,API Gateway 将使用现有的授权方。如果存在差异,那么它将创建一个新的授权者或覆盖现有的授权者。确保以下几点:
- 安全方案对象名称与授权者名称相同。在本例中是
"lambda-authorizer"
- 类型是 lambda 授权者的
"apiKey"
- name 是您用于授权的 HTTP header (通常是
"Authorization"
header ) - x-amazon-apigateway-authtype 是 lambda 授权者的
"custom"
- x-amazon-apigateway-authorizer 具有正确的类型(
"request"
或"token"
)、正确的 uri、ttl 和身份源
我已经使用区域 REST API 对此进行了测试,因此对于其他 API 网关类型,它的行为可能有所不同。以下示例是 AWS PetStore 示例 API 的简化版本。
{
"openapi" : "3.0.1",
"info" : {
"description" : "Your first API with Amazon API Gateway. This is a sample API that integrates via HTTP with our demo Pet Store endpoints",
"version" : "1",
"title" : "PetStore"
},
...
"paths" : {
"/pets" : {
"get" : {
...
"security" : [ {
"lambda-authorizer" : [ ]
} ],
"x-amazon-apigateway-integration" : {
...
}
}
}
},
"components" : {
"schemas" : {
...
},
"securitySchemes" : {
"lambda-authorizer" : {
"type" : "apiKey",
"name" : "Authorization",
"in" : "header",
"x-amazon-apigateway-authtype" : "custom",
"x-amazon-apigateway-authorizer" : {
"type" : "request",
"authorizerUri" : "arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${lambda_authorizer_arn}/invocations",
"authorizerResultTtlInSeconds" : 300,
"identitySource" : "method.request.header.Authorization"
}
}
}
}
}
在此示例中,您必须将 ${region}
替换为您的 API 网关区域,并将 ${lambdaauthorizer_arn}
替换为 lambda 函数的完整 ARN。完整的字符串将如下所示:arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:lambda-authorizer-function/invocations
TL;DR:如果您想使用现有授权者,则必须在 OpenAPI 文件中完整定义它。
关于amazon-web-services - 如何在 OpenApi 端点定义中引用现有 API 网关授权者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58626895/