【问题标题】:Not authorized to perform AssumeRoleWithWebIdentity with Cognito User无权使用 Cognito 用户执行 AssumeRoleWithWebIdentity
【发布时间】:2016-08-30 12:46:47
【问题描述】:

使用AWS-Cognito-Identity-Js,我为经过身份验证的 Cognito 用户获取会话 ID 令牌 session.getIdToken().getJwtToken()

我将此 token 传递给我的 AWSInitialize 函数并更新 AWS 凭证:

var AWSInitialize = function(token){
  Logins = {};
  Logins['cognito-idp.' + AWSCognito.config.region + '.amazonaws.com/' + poolData.UserPoolId] = token;

  AWS.config.update({
    region: AWSCognito.config.region,
    credentials: new AWS.CognitoIdentityCredentials({
        IdentityPoolId : identityPoolId,
        region: AWSCognito.config.region,
        Logins : Logins
    })
  });
};

这可以正常工作,因为现在我可以代表经过身份验证的 Cognito 用户执行 Lambda 函数。

 var lambda = new AWS.Lambda({});
 lambda.invoke({FunctionName: 'createToken'}, function(err, data) ...

这是可能的,因为在Cognito_myAppAuth_Role 中我附加了一个允许我执行这个 Lambda 函数的策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1471300653000",
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "arn:aws:lambda:eu-west-1:593845191076:function:createToken"
            ]
        }
    ]
}

现在我要做的是为相同的用户获取带有 STS 的令牌

为此,我将另一项政策附加到 Cognito_myAppAuth_Role。它应该允许 Cognito 用户调用 assumeRoleWithWebIdentity:

 {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1472560044000",
            "Effect": "Allow",
            "Action": [
                "sts:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

但是当我运行这段代码时:

var sts = new AWS.STS({});

var params = {
  RoleArn: 'arn:aws:iam::593845191076:role/Cognito_myAppAuth_Role', /* required */
  RoleSessionName: "UserName", /* required */
  WebIdentityToken: token, /* required */
};

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

我收到以下错误:

  AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity

我不明白为什么用户无权执行 sts:AssumeRoleWithWebIdentity。我为经过身份验证的角色附加了一个 STS 策略,并且 Lambda-Policy 也适用于用户

问题可能出在哪里? 我该如何解决这个问题?

非常感谢!

【问题讨论】:

  • 只是澄清一下,这些政策都附加到同一个角色?您是否使用相同的用户/身份运行它?为什么要手动使用 STS?

标签: amazon-web-services lambda policy amazon-cognito


【解决方案1】:

我怀疑此失败的原因是您尝试将 Cognito Your User Pool 令牌直接用于 STS。虽然我不再直接在 Cognito 上工作,但我认为这不会奏效。

您应该尝试以下方法之一:

  1. 推荐)使用 Cognito 联合身份调用 GetIdGetCredentialsForIdentity 获取您的临时凭证。这就是您的第一个代码块在后台执行的操作。
  2. 使用 Cognito 联合身份调用 GetIdGetOpenIdToken,然后在您的 AssumeRoleForWebIdentity 调用中使用 那个 令牌。

希望这会有所帮助。

【讨论】:

  • 嗨@BobKinney,感谢您的回答。我假设为了以 Cognito 用户的身份调用 AWS API Gateway,首先我必须获得他的 AWS 凭证。我认为凭证是构造 Authenticated-Header 所必需的。但我可以发现,作为 Cognito-User 对 AWS API Gateway 的经过身份验证的调用与作为 IAM-User 的调用不同。在标头中传递 Cognito-Users session.getIdToken().getJwtToken()-Token 就足够了,不需要凭据。有任何安全问题吗?祝你有美好的一天!
  • @JohnSmith 不记名令牌安全性不如 SigV4 中使用的 HMAC 签名强。如果有人在飞行中访问令牌,他们可以向您的 API 发出任何他们想要的请求。只要与端点的通信是 SSL(API 网关需要)并且客户端是安全的,就可以缓解这种情况。
  • 感谢您抽出宝贵时间解释问题。祝你有美好的一天!
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 2020-06-29
  • 2018-11-07
  • 2016-03-18
  • 2021-02-26
  • 1970-01-01
相关资源
最近更新 更多