【问题标题】:AWS S3: private access using id_token from Cognito like to the ApiGateway: is it possible?AWS S3:使用来自 Cognito 的 id_token 对 ApiGateway 进行私有访问:可能吗?
【发布时间】:2018-03-08 19:19:29
【问题描述】:

我使用 Cognito 中的 token_id 成功设置了对 Lambda 的访问 - 客户端添加标头 Authorization: <token_id> 并且 Api Gateway 验证此令牌。我希望我可以从客户端浏览器设置对 S3 的类似访问。为此,我为 S3 存储桶编写了策略(每个用户都有自己的文件目录):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::buc/${cognito-identity.amazonaws.com:sub}/*"
        }
    ]
}

我试过这个电话:

curl -v --header 'Authorization: [id_token_from_cognito_after_signup]' https://s3.amazonaws.com/buc/<sub>/myfile.jpg

返回 400 Authorization header is invalid -- one and only one ' ' (space) required。在网上我发现这个错误可能与空格无关 - 因为这个请求有正确的空格数量(我也试过没有空格的请求)。

有趣的是,在documentation 中关于 S3 并没有提到 Cognito。

为了速度和简单性,我不想在客户端使用 js sdk。

【问题讨论】:

  • 我有理由确定您需要使用 cognito 凭据来为 S3 创建签名请求——而不是直接发送令牌。

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


【解决方案1】:

您可以使用 cognito 令牌访问您的 API,因为 API 网关中集成了 cognito,正如 aws blog 中完整描述的那样

Cognito 用户池与 API Gateway 的集成提供了一种新方式 保护您的 API 工作负载

但是,截至今天,S3 没有这种可能性。您需要自己计算签名,正如您参考的文档中所解释的那样。 (各种例子参考here

【讨论】:

    【解决方案2】:

    你有没有试过here提到的这个政策

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": ["s3:ListBucket"],
          "Resource": ["arn:aws:s3:::<BUCKET-NAME>"],
          "Condition": {"StringLike": {"s3:prefix": ["cognito/<APPLICATION-NAME>/"]}}
        },
        {
          "Effect": "Allow",
          "Action": [
            "s3:GetObject",
            "s3:PutObject",
            "s3:DeleteObject"
          ],
          "Resource": [
            "arn:aws:s3:::<BUCKET-NAME>/cognito/<APPLICATION-NAME>/${cognito-identity.amazonaws.com:sub}",
            "arn:aws:s3:::<BUCKET-NAME>/cognito/<APPLICATION-NAME>/${cognito-identity.amazonaws.com:sub}/*"
          ]
        }
      ]
    }
    

    【讨论】:

    • 嗯,我认为原则上这就像我目前的政策 - 这只是增加了删除和放置的条件和权限。同样有趣的是,由于Missing required field Principal,此文档中的策略示例无法保存在存储桶首选项中。
    猜你喜欢
    • 2020-02-29
    • 2019-07-08
    • 1970-01-01
    • 2020-03-04
    • 2021-07-04
    • 2021-08-11
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多