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

cognito fine grained access control and API gateway

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

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

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

我已经使用以下 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"
            ]
        }
    ]
}

但是,当使用 link 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 并且消息 Allowed 一切正常

我不知道为什么我不能调用 lambda?我该如何解决这个问题?

谢谢

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

调用 lambda 函数
arn:aws:execute-api:us-east-2:XXXXXXXXXXXXX:syx381ecq9/*/GET/members/class1

因此,当 cognito 凭据通过时,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 的 arn