Ani*_*aje 1 java amazon-web-services amazon-iam aws-lambda aws-api-gateway
我试图通过AWS API Gateway的自定义授权程序来授权API调用,
这基本上是一个自定义的lambda函数,它接受以下格式的以下标题 -
{
"authorizationToken": "0c34ba00bde34200b383abe22bcfef96",
"methodArn": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"type": "TOKEN"
}
Run Code Online (Sandbox Code Playgroud)
并期望以下格式的回复 -
{
"principalId": "xxxxxxx", // the principal user identification associated with the token send by the client
"policyDocument": { // example policy shown below, but this value is any valid policy
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"execute-api:Invoke"
],
"Resource": [
"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:xxxxxxxx:/test/*/mydemoresource/*"
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我能够使用authorizationToken执行内部逻辑并验证函数是否应该在"允许"或"拒绝"策略中响应,
但是当我尝试从控制台测试授权器时,我收到解析错误,
以下是我的请求日志 -
Execution log for request test-request
Thu Jun 29 11:48:10 UTC 2017 : Starting authorizer: 1o3dvk for request: test-request
Thu Jun 29 11:48:10 UTC 2017 : Incoming identity: **************************cfef96
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request URI: https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:855399270504:function:um_guestSessionAuthoriser/invocations
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************************************************751e60, X-Amz-Date=20170629T114810Z, x-amzn-apigateway-api-id=z6t3cv0z4m, X-Amz-Source-Arn=arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/authorizers/1o3dvk, Accept=application/json, User-Agent=AmazonAPIGateway_z6t3cv0z4m, X-Amz-Security-Token=FQoDYXdzEHQaDOcIbaPscYGsl1wF4iLBAxzOTpZlR2r3AO3g96xwhRuQjEhU9OjOaRieBWQPeosNqv53aGKnBTT2CmkrVzHo3UqOdT1eakuS7tAXAbEcUIHVheWpBnvxqTkaPcknRL7QE79RSqVeryoXo2R1Kmk0Q9Iq+JGFlOJYQQJqvY/hcUg189xqbpTGrhZjcA+pjuSp+M9D97Kce0VP0e3peu/YvON0eGvUlj59MAJAwGVPIzplMKTDFrFg5NKEj79RSxNrNE8y4bAebOwlD8xLv649Zny7++xlMBBwHqMNHu3K9lFXSnKY9DHf6kvezZmpoFB2uu8WbrpInH0eQ/bIAd [TRUNCATED]
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request body after transformations: {"type":"TOKEN","methodArn":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","authorizationToken":"0c34ba00bde34200b383abe22bcfef96"}
Thu Jun 29 11:48:10 UTC 2017 : Sending request to https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:855399270504:function:um_guestSessionAuthoriser/invocations
Thu Jun 29 11:48:21 UTC 2017 : Authorizer result body before parsing: {"principalId":"user","policyDocument":{"version":"2012-10-17","statement":[{"resource":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","action":"execute-api:Invoke","effect":"Allow"}]}}
Thu Jun 29 11:48:21 UTC 2017 : Execution failed due to configuration error: Could not parse policy: {"version":"2012-10-17","statement":[{"resource":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","action":"execute-api:Invoke","effect":"Allow"}]}
Thu Jun 29 11:48:21 UTC 2017 : AuthorizerConfigurationException
Run Code Online (Sandbox Code Playgroud)
我在Lambda函数上使用Java,我使用PoJo类(setter-getter类)构建并返回策略
在美化lambda响应后,我的策略如下所示 -
{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"action": "execute-api:Invoke",
"effect": "Allow"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它无法解析我的回复?
根据建议我试图利用响应IAM策略,
我使用@SerializedName了com.google.gson.annotations.SerializedNameimport并且能够得到以下输出 -
{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"effect": "Deny",
"action": ["execute-api:Invoke"],
"resource": ["arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/"]
}]
}
}
Run Code Online (Sandbox Code Playgroud)
但看起来我的lambda响应和API网关之间发生了一些奇怪的事情,
变量在内部得到了较低的内容,
而且我仍然得到相同的解析错误,
它是否可以接受其他格式的响应?字符串也不起作用.
我还应该尝试什么?我的政策格式错了吗?
我从这些网站获得了两种不同的政策格式 -
1. http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html
2. https://aws.amazon.com/blogs /计算/引入定制-授权人合亚马逊-API网关/
您的策略属性需要适当的大写.代替:
{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"action": "execute-api:Invoke",
"effect": "Allow"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
应该:
{
"principalId": "user",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"Action": "execute-api:Invoke",
"Effect": "Allow"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
也可以使用"PrincipalId"来保持一致.
| 归档时间: |
|
| 查看次数: |
1366 次 |
| 最近记录: |