【问题标题】:Unable to access AWS using a Facebook AssumeRoleWithWebIdentity credentials无法使用 Facebook AssumeRoleWithWebIdentity 凭证访问 AWS
【发布时间】:2013-06-28 11:32:22
【问题描述】:

短版

以 Facebook 用户身份登录,我使用我的 oAuth 令牌假设 IAM role on AWS。它返回看似有效的凭据,例如有一个 AccessKeyId,SecretAccessKey 与我们的主密钥长度相似。

当我尝试使用这些凭据访问 DynamoDB 表时,我遇到以下两个异常之一:

  1. "The remote server returned an error: (400) Bad Request.";或
  2. "The security token included in the request is invalid."

我正在使用 AWS C# 开发工具包版本 1.5.25.0

加长版

如上所述,我正在尝试使用AmazonSecurityTokenServiceClient 提供的凭据访问 AWS 上的一个 DynamoDB 表,该凭据由 Facebook Identity 授权,如this AWS guide 中所述。

我创建的 IAM 角色的策略是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "dynamodb:BatchGetItem",
        "dynamodb:PutItem",
        "dynamodb:Query",
        "dynamodb:Scan",
        "dynamodb:UpdateItem"
      ],
      "Sid": "Stmt1372350145000",
      "Resource": [
        "*"
      ],
      "Effect": "Allow"
    }
  ]
}

我如何获得凭据:

  1. 用户使用 oAuth 登录 Facebook。
  2. 使用访问令牌,我使用带有请求的 AmazonSecurityTokenServiceClient.AssumeRoleWithWebIdentity 代入 IAM 角色。
  3. 这会返回看似有效的凭据,例如与我们的主密钥长度相似的 AccessKeyId、SecretAccessKey。

    using (var tokenServiceClient = new AmazonSecurityTokenServiceClient(RegionEndpoint.USEast1))
    {
        var request = new AssumeRoleWithWebIdentityRequest
        {
            DurationSeconds = (int)TimeSpan.FromHours(1).TotalSeconds,
            ProviderId = "graph.facebook.com",
            RoleArn = "arn:aws:iam::193557284355:role/Facebook-OAuth",
            RoleSessionName = result.id,
            WebIdentityToken = FBClient.AccessToken
        };
    
        var response = tokenServiceClient.AssumeRoleWithWebIdentity(request);
    
        AWSAssumedRoleUser = response.AssumeRoleWithWebIdentityResult.AssumedRoleUser;
        AWSCredentials = response.AssumeRoleWithWebIdentityResult.Credentials;
    }
    

我如何使用这些凭据:

使用返回的凭证,然后我尝试访问 AWS DynamoDB 资源。

using (var client = new AmazonDynamoDBClient(AWSCredentials.AccessKeyId, AWSCredentials.SecretAccessKey, AWSCredentials.SessionToken, RegionEndpoint.USEast1))
{
    var context = new DynamoDBContext(client);
    var data = context.Scan<SomeData>();    
}

这会在尝试 Scan 表时返回 "The remote server returned an error: (400) Bad Request."

这是异常消息的变化所在;如果我从上面的AmazonDynamoDBClient 中省略了AWSCredentials.SessionToken

using (var client = new AmazonDynamoDBClient(AWSCredentials.AccessKeyId, AWSCredentials.SecretAccessKey, RegionEndpoint.USEast1))
{
    var context = new DynamoDBContext(client);
    var data = context.Scan<SomeData>();
}

这会在尝试 Scan 表时返回 "The security token included in the request is invalid."

问题

此时我无法判断出了什么问题,是凭证无效还是我没有将所有东西都传递给 AWS。

任何人都可以提供任何关于错误或我如何进一步调试的见解吗?

【问题讨论】:

    标签: c# oauth amazon-web-services amazon-dynamodb


    【解决方案1】:

    我将我的问题交叉发布到 AWS 论坛并收到了 Amazon 工程师的回答。

    https://forums.aws.amazon.com/message.jspa?messageID=465057


    DynamoDBContext 对象在目标表上调用 DescribeTable(并缓存此数据,因此为了获得最佳性能,您希望尽可能长时间地保留上下文对象,因此每个目标表只执行一次此调用)。修改您的策略如下:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": [
            "dynamodb:BatchGetItem",
            "dynamodb:PutItem",
            "dynamodb:Query",
            "dynamodb:Scan",
            "dynamodb:UpdateItem",
            "dynamodb:DescribeTable"        
          ],
          "Sid": "Stmt1372350145000",
          "Resource": [
            "*"
          ],
          "Effect": "Allow"
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2017-08-24
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多