ablog

不器用で落着きのない技術者のメモ

アカウント毎にアクセス可能な S3 Prefix を制御する S3 バケットポリシー例

やりたいこと

  • データレイクアカウントと連携相手の複数アカウントの間で1対多のクロスアカウントアクセス制御を行う。
  • 連携相手のアカウントから特定S3バケットの特定 prefix 以下のみへの Put/Get/Delete を許可。
  • 連携相手のアカウント毎に Put/Get/Delete 可能な prefix を分け、他アカウントの prefix 配下への Put/Get/Delete 禁止。
  • 自アカウントはVPCエンドポイント経由以外の Put/Get/Delete 禁止。
  • VPCエンドポイント経由以外の Put/Get/Delete 禁止。
  • VPCエンドポイント経由でも許可していないロールからの Put/Get/Delete 禁止。

バケットポリシー例

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Access-to-specific-VPCE-only",
            "Effect": "Deny",
            "Principal": "*",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::test-123",
                "arn:aws:s3:::test-123/*"
            ],
            "Condition": {
                "StringNotEquals": {
                    "aws:sourceVpce": [
                        "vpce-0e6************0a",
                        "vpce-02c************64",
                        "vpce-0a6************67"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/EC2RoleA"
            },
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::test-123/dir1/",
                "arn:aws:s3:::test-123/dir1/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:sourceVpce": "vpce-02c************64"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::234567890123:role/EC2RoleB"
            },
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::test-123/dir2/",
                "arn:aws:s3:::test-123/dir2/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:sourceVpce": "vpce-0a6************67"
                }
            }
        }
    ]
}

確認したこと

  • マネジメントコンソールから Put/Get/Delete できない(設定したVPCエンドポイント経由以外では不可)。以下はダウンロードしようとした場合。
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>867BA8F95B99B06C</RequestId>
<HostId>
ZY9qTP+1ulVOJjYw7gElnIv+BO+2kKE/7wTT6SAYqS3H3TdjFu+yJa9dFKOeRmKin3xumvzUtew=
</HostId>
</Error>
  • 設定しているVPCエンドポイント経由でもバケットポリシーで指定したIAMロール以外からは Put/Get/Delete 不可。
$  aws s3 cp foo s3://test-123/dir1/
upload failed: ./foo to s3://test-123/dir1/foo An error occurred (AccessDenied) when calling the PutObject operation: Access Denied