cognito细粒度访问控制和API网关

时间:2016-12-27 05:07:34

标签: amazon-web-services amazon-iam amazon-cognito

在api网关中,我有以下资源ARN:

arn:aws:execute-api:us-east-2:XXXXXXXXXXXXX:syx381ecq9/*/GET/members/*

提供了一个基于class_id获取成员列表的链接 - / members / {id}
类中的用户只能看到属于该类的成员列表。

我已使用以下IAM策略指定了cognito用户池(假设class1为class_id)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cognito-identity:*",
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "lambda:*",
                "execute-api:*"
            ],
            "Resource": [
                "arn:aws:execute-api:us-east-2:XXXXXXXXXXXXX:syx381ecq9/*/GET/members/class1"
            ]
        }
    ]
}

然而,当使用链接 GET / members / class1 时,我收到以下消息:

Execution failed due to configuration error: API Gateway could not determine the callers credentials

我在cloudwatch中检查过,没有来自lambda的日志,因此我认为lambda没有被执行。 我继续尝试class2。这次显示以下消息:

User:arn:aws:sts::XXXXXXXXXXXX:assumed-role/Cognito-sample_client1/CognitoIdentityCredentials is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:us-east-2:********8469:syx381ecq9/sample/GET/inspectors/client2

我已检入policy stimulate,一切正常,消息允许

我不知道为什么我不能打电话给lambda?我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案。上述策略仅允许为

调用lambda函数
arn:aws:execute-api:us-east-2:XXXXXXXXXXXXX:syx381ecq9/*/GET/members/class1
因此,当认知凭证已经过去时,api将尝试调用lambda但不幸的是,该政策限制了这一点。为了完成它,我们需要将它分成另一个语句,如下所示:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cognito-identity:*",
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "execute-api:*"
            ],
            "Resource": [
                "arn:aws:execute-api:us-east-2:XXXXXXXXXXXXX:syx381ecq9/*/GET/members/class1"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

我们可以根据需要自定义特定的lambda&#sn