AWS Amplify - CognitoIdentityCredentials 无权执行:sts:AssumeRole 资源

AWS Amplify - CognitoIdentityCredentials is not authorized to perform: sts:AssumeRole on resource

我有一个使用 Nuxt 的 Amplify 应用程序。当用户通过 Cognito 登录时,该应用程序会尝试使用 STS 转移到另一个角色,以在 this AWS blog post.

之后获取 QuickSight 仪表板嵌入 Url

所以我在这个政策中扮演了一个角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "quicksight:GetDashboardEmbedUrl",
            "Resource": "arn:aws:quicksight:us-west-2:xxxxxxxx:dashboard/xxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxxx",
            "Effect": "Allow"
        }
    ]
}

并且我将此策略添加到我的应用程序身份验证角色

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::xxxxxxxxx:role/embed_role_name"
    }
}

当他们通过 cognito 登录时,我尝试假设 embed_role 与 STS

与:

let params = {
    RoleArn: QS_EMBED_ROLE,
    RoleSessionName: "embedding-qs",
};

let sts = new AWS.STS();

sts.assumeRole(params, function (err, data) {
    if (err) console.log(err, err.stack);
    // an error occurred
    else {
        console.log(data);
    }
});

我收到这个错误:

AccessDenied: User: arn:aws:sts::xxxxxxxxxx:assumed-role/app-authenticated-role/CognitoIdentityCredentials is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::xxxxxxxxxxxxx:role/embed_role

在文档中看起来很简单,所以我不确定我是否只是不理解某些东西。

此外,在尝试一百万件事的过程中,我认为我编辑了应用程序身份验证角色和嵌入角色的信任关系,但不确定它是否重要。

app-authenticated-role 信任策略是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "us-east-2:xxxxxxxx-xxxxxx-xxxxxx-xxxx-xxxxxxxx"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "authenticated"
        }
      }
    }
  ]
}

并且 embed_role 信任策略是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "us-east-2:xxxxxxxx-xxxxxx-xxxxxx-xxxx-xxxxxxxx"
        }
      }
    }
  ]
}

之所以没有被授权是因为我需要使用这个动作

"Action": "sts:AssumeRole"

在我的 app-authenticated-role 信任政策中而不是

"Action": "sts:AssumeRoleWithWebIdentity"

替换后它按预期工作。

如果您使用 Cognito 身份池将经过身份验证的用户映射到 IAM 角色,那么您通常不会直接调用 sts:AssumeRole,而是使用 AWS.CognitoIdentityCredentials() 为您的网站获取 IAM 凭据身份。

这会在幕后进行 2 次调用。首先,它使用给定的登录名(来自 Cognito 用户池、Facebook、Google 等身份提供者的 JWT 令牌)创建新身份或检索现有身份。其次,它将代表您调用 sts:AssumeRoleWithWebIdentity 和 return IAM 凭据。

这在Cognito Identity Pool docs.

中都有描述

因此您的经过身份验证的角色信任策略将是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "us-east-2:xxxxxxxx-xxxxxx-xxxxxx-xxxx-xxxxxxxx"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "authenticated"
        }
      }
    }
  ]
}

其中指出 Cognito Identity 可以调用 STS 以代表 Web 身份获取凭据。

与该角色关联的权限策略为:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "quicksight:GetDashboardEmbedUrl",
            "Resource": "arn:aws:quicksight:us-west-2:xxxxxxxx:dashboard/xxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxxx",
            "Effect": "Allow"
        }
    ]
}

加上您的网络用户应具有的任何其他权限。