Granting access to S3 resources based on role name

后端 未结 2 940
南方客
南方客 2020-12-06 22:01

IAM policy variables are quite cool and let you create generic policys to, for example, give users access to paths in an S3 bucket based on their username, like this:

<
相关标签:
2条回答
  • 2020-12-06 22:22

    (Cross-posted to AWS S3 IAM policy for role for restricting few instances to connect to S3 bucket based in instance tag or instance id)

    Instead of using aws:SourceArn, use aws:userid!

    The Request Information That You Can Use for Policy Variables documentation that you mentioned has a table showing various values of aws:userid including:

    For Role assigned to an Amazon EC2 instance, it is set to role-id:ec2-instance-id

    Therefore, you could use the Role ID of the role that is used to launch the Amazon EC2 instance to permit access OR the Instance ID.

    For example, this one is based on a Role ID:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "SID123",
                "Effect": "Allow",
                "Action": [
                    "s3:*"
                ],
                "Resource": [
                    "*"
                ],
                "Condition": {
                    "StringLike": {
                        "aws:userid": [
                            "AROAIIPEUJOUGITIU5BB6*"
                        ]
                    }
                }
            }
        ]
    }
    

    Of course, if you are going to assign permission based on a Role ID, then you can just as easily grant permissions within the Role itself.

    This one is based on an Instance ID:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "SID123",
                "Effect": "Allow",
                "Action": [
                    "s3:*"
                ],
                "Resource": [
                    "*"
                ],
                "Condition": {
                    "StringLike": {
                        "aws:userid": [
                            "*:i-03c9a5f3fae4b630a"
                        ]
                    }
                }
            }
        ]
    }
    

    The Instance ID will remain with the instance, but a new one will be assigned if a new instance is launched, even from the same Amazon Machine Image (AMI).

    0 讨论(0)
  • 2020-12-06 22:31

    I've been looking for the same and after a lot of searching my conclusion was that it is not possible to use the role name as a variable in a IAM policy (I'd love to be proven wrong though).

    Instead, I tagged my role with a name and ended up with this:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": ["s3:GetObject","s3:PutObject","s3:DeleteObject"],
                "Effect": "Allow",
                "Resource": "arn:aws:s3:::fooCorp-user-files/${aws:PrincipalTag/name}/*"
            },
            {
                "Action": "s3:ListBucket",
                "Effect": "Allow",
                "Resource": "arn:aws:s3:::fooCorp-user-files"
            }
        ]
    }
    
    0 讨论(0)
提交回复
热议问题