AWS使用StsClient限制对临时凭证getSessionToken的访问

时间:2018-08-12 14:38:04

标签: php amazon-web-services amazon-s3 amazon-iam aws-sts

业务Senario

有多个办公室拥有自己的AWS S3存储桶。一个办公室的用户都无法访问另一个办公室的S3存储桶。

因此,每个办公室都有一个S3 Bucket和一个IAM user。每个IAM用户都只能访问一个存储桶。由于办公室不常增长,因此可以通过AWS Console手动完成IAM用户的创建和分配权限。

应用Senario

浏览器(Javascript)向服务器(PHP API)请求临时证书,以将文件上传到AWS S3。 PHP API从数据库(基于登录的办公室)获取Access key IDSecret access key。然后使用AWS PHP SDK调用StsClient并使用getSessionToken()方法获取临时凭证并将其传递给Javascript。

PHP代码

use Aws\Sts\StsClient;
$credentials = new Aws\Credentials\Credentials($resultset['awssecret'], $resultset['awspass']);

$stsoptions = [
  'region'            => 'ap-south-1',
  'version'           => '2011-06-15',
  'signature_version' => 'v4',
  'credentials'       => $credentials,
];

$stsClient = new StsClient($stsoptions);
$response = $stsClient->getSessionToken();

问题

当前,IAM用户拥有对相应存储桶的完全访问权限。我想将临时凭据访问限制为仅某些授权。就像只允许上传一样,不喜欢删除文件或列出所有文件。

是否可以向StsClient传递一个附加参数,以限制存储桶的权限?

编辑1

通过策略为用户分配了权限。以下是添加到该策略的权限。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucketByTags",
                "s3:GetBucketTagging",
                "s3:DeleteObjectVersion",
                "s3:GetObjectVersionTagging",
                "s3:ListBucketVersions",
                "s3:GetBucketLogging",
                "s3:RestoreObject",
                "s3:ListBucket",
                "s3:GetAccelerateConfiguration",
                "s3:GetObjectAcl",
                "s3:AbortMultipartUpload",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectTagging",
                "s3:GetMetricsConfiguration",
                "s3:PutObjectTagging",
                "s3:DeleteObject",
                "s3:PutBucketVersioning",
                "s3:GetIpConfiguration",
                "s3:DeleteObjectTagging",
                "s3:ListBucketMultipartUploads",
                "s3:GetBucketWebsite",
                "s3:PutObjectVersionTagging",
                "s3:DeleteObjectVersionTagging",
                "s3:GetBucketVersioning",
                "s3:GetBucketAcl",
                "s3:GetReplicationConfiguration",
                "s3:ListMultipartUploadParts",
                "s3:PutObject",
                "s3:GetObject",
                "s3:GetBucketCORS",
                "s3:GetAnalyticsConfiguration",
                "s3:GetObjectVersionForReplication",
                "s3:GetBucketLocation",
                "s3:ReplicateDelete",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "arn:aws:s3:::aws:s3:::mybucket",
                "arn:aws:s3:::*/*"
            ]
        }
    ]
}

我的解决方案基于“接受的答案”建议的答案。

根据答案中的建议,添加了权限

 {
        "Sid": "VisualEditor1",
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "*"
 }

现在我的PHP代码是

$policy = '{
  "Id": "Policy1534086947311",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1534086676951",
      "Action": [
        "s3:PutObject",
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::mybucket"
      }
    }
  ]
}';

$awsrole = [
  'Policy' => $policy,
  'RoleArn' => 'arn:aws:s3:::mybucket',
  'RoleSessionName' => 'credApi' // just a random value
];
$response = $stsClient->assumeRole($awsrole);

1 个答案:

答案 0 :(得分:2)

使用更严格的权限创建一个IAM角色,然后使用您的IAM用户凭据来调用AssumeRole以获取临时凭据。 IAM角色将只允许s3:PutObject *进入相关存储桶。 IAM用户需要permission才能担任角色。

或者只是让您的服务器生成一个预签名URL,以上传到相关S3存储桶中的对象。客户端将无法将该URL用于其他任何S3操作。