gpt4 book ai didi

amazon-web-services - 如何使用自定义授权者和空的 $context.authorizer.* 变量测试 API Gateway 方法?

转载 作者:行者123 更新时间:2023-12-04 15:31:01 25 4
gpt4 key购买 nike

我有一个带有 POST 方法的 API 网关,该方法直接放入 DynamoDB 表。我的方法还配置为通过 Lambda 使用自定义授权方。

在我的模板映射中,我使用了一些授权变量,例如 $context.authorizer.principalId$context.authorizer.accountId 。简化的模板映射如下所示:

{ 
"TableName": "$stageVariables.tableName",
"Item": {
"AccountId": {
"S": "$context.authorizer.accountId"
},
"Id": {
"S": "$context.requestId"
},
"Content": {
"S": "$input.path('$.content')"
},
"UserId": {
"S": "$context.authorizer.principalId"
}
}

}

现在,当我向部署到实际阶段的此 API 方法发出 HTTP 请求时,该请求将通过自定义授权方并提供/填写模板中的所有 $context.authorizer.* 变量,可能如下所示:
{ 
"TableName": "MyTable",
"Item": {
"AccountId": {
"S": "12345"
},
"Id": {
"S": "6fd5ff08-34c0-11e7-bf96-591a565835b3"
},
"Content": {
"S": "my content"
},
"UserId": {
"S": "userid-123456789"
}
}

}

通过 API Gateway 测试按钮测试 API 方法时,测试请求绕过了自定义授权方(这是有道理的,因为授权方可以单独测试)并产生如下结果:
{ 
"TableName": "MyTable",
"Item": {
"AccountId": {
"S": ""
},
"Id": {
"S": "test-invoke-request"
},
"Content": {
"S": "my content"
},
"UserId": {
"S": ""
}
}

}

以下内容现在无效,因为所有字段都针对模型进行了验证,并出现以下验证错误:
Endpoint response body before transformations: {"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: An AttributeValue may not contain an empty string"}

在测试 API Gateway 方法时,是否有某种方法可以指定授权变量?
或者是否有一些聪明的方法可以在模板映射中定义一个回退变量,以便当它解析为空时,它会回退到例如 test-invoke-principalid ,就像 $context.requestId 开箱即用一样?

我想要的只是仍然能够使用 API Gateway 测试功能,同时保留所有验证/授权设置。

最佳答案

当然,我们可以考虑为测试调用功能添加占位符。当然对于principalId。至于自定义上下文变量,这可能会更困难,我们将拭目以待。我们最终也希望有一个更好的端到端测试解决方案。

关于amazon-web-services - 如何使用自定义授权者和空的 $context.authorizer.* 变量测试 API Gateway 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43891631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com