Produces a S3 IAM text for selective access of data. Only a prefix can be listed, gotten, or written to when a credential is subject to this policy text.
(bucket_name, prefix, allow_get_location=False)
| 80 | |
| 81 | |
| 82 | def make_policy(bucket_name, prefix, allow_get_location=False): |
| 83 | """Produces a S3 IAM text for selective access of data. |
| 84 | |
| 85 | Only a prefix can be listed, gotten, or written to when a |
| 86 | credential is subject to this policy text. |
| 87 | """ |
| 88 | bucket_arn = "arn:aws:s3:::" + bucket_name |
| 89 | prefix_arn = "arn:aws:s3:::{0}/{1}/*".format(bucket_name, prefix) |
| 90 | |
| 91 | structure = { |
| 92 | "Version": "2012-10-17", |
| 93 | "Statement": [ |
| 94 | { |
| 95 | "Action": ["s3:ListBucket"], |
| 96 | "Effect": "Allow", |
| 97 | "Resource": [bucket_arn], |
| 98 | "Condition": {"StringLike": {"s3:prefix": [prefix + '/*']}}, |
| 99 | }, |
| 100 | { |
| 101 | "Effect": "Allow", |
| 102 | "Action": ["s3:PutObject", "s3:GetObject"], |
| 103 | "Resource": [prefix_arn] |
| 104 | }]} |
| 105 | |
| 106 | if allow_get_location: |
| 107 | structure["Statement"].append( |
| 108 | {"Action": ["s3:GetBucketLocation"], |
| 109 | "Effect": "Allow", |
| 110 | "Resource": [bucket_arn]}) |
| 111 | |
| 112 | return json.dumps(structure, indent=2) |
| 113 | |
| 114 | |
| 115 | @pytest.fixture |