|
| 1 | +import boto3 |
| 2 | +from botocore.exceptions import ClientError |
| 3 | +import json |
| 4 | +import pytest |
| 5 | + |
| 6 | +from . import ( |
| 7 | + configfile, |
| 8 | + setup_teardown, |
| 9 | + get_iam_root_client, |
| 10 | + get_iam_root_account_id, |
| 11 | + get_new_bucket_name, |
| 12 | + ) |
| 13 | +from .utils import ( |
| 14 | + assert_raises, |
| 15 | + _get_status_and_error_code, |
| 16 | + ) |
| 17 | + |
| 18 | +def test_account_public_access_block(): |
| 19 | + s3control = get_iam_root_client(service_name='s3control', region_name='us-east-1') |
| 20 | + account_id = get_iam_root_account_id() |
| 21 | + |
| 22 | + # delete default configuration if it exists |
| 23 | + response = s3control.delete_public_access_block(AccountId=account_id) |
| 24 | + assert response['ResponseMetadata']['HTTPStatusCode'] == 204 |
| 25 | + # re-delete should still return 204 |
| 26 | + response = s3control.delete_public_access_block(AccountId=account_id) |
| 27 | + assert response['ResponseMetadata']['HTTPStatusCode'] == 204 |
| 28 | + |
| 29 | + # get returns 404 |
| 30 | + e = assert_raises(ClientError, s3control.get_public_access_block, AccountId=account_id) |
| 31 | + assert (404, 'NoSuchPublicAccessBlockConfiguration') == _get_status_and_error_code(e.response) |
| 32 | + |
| 33 | + s3control.put_public_access_block( |
| 34 | + AccountId=account_id, |
| 35 | + PublicAccessBlockConfiguration={ |
| 36 | + 'BlockPublicAcls': True, |
| 37 | + 'IgnorePublicAcls': False, |
| 38 | + 'BlockPublicPolicy': False, |
| 39 | + 'RestrictPublicBuckets': False |
| 40 | + }) |
| 41 | + try: |
| 42 | + response = s3control.get_public_access_block(AccountId=account_id) |
| 43 | + assert response['PublicAccessBlockConfiguration']['BlockPublicAcls'] |
| 44 | + assert not response['PublicAccessBlockConfiguration']['IgnorePublicAcls'] |
| 45 | + assert not response['PublicAccessBlockConfiguration']['BlockPublicPolicy'] |
| 46 | + assert not response['PublicAccessBlockConfiguration']['RestrictPublicBuckets'] |
| 47 | + |
| 48 | + s3 = get_iam_root_client(service_name='s3') |
| 49 | + bucket = get_new_bucket_name() |
| 50 | + |
| 51 | + # reject CreateBucket with public acls |
| 52 | + e = assert_raises(ClientError, s3.create_bucket, Bucket=bucket, ACL='public-read') |
| 53 | + assert (403, 'AccessDenied') == _get_status_and_error_code(e.response) |
| 54 | + |
| 55 | + s3.create_bucket(Bucket=bucket) |
| 56 | + try: |
| 57 | + # reject PutBucketAcl with public acls |
| 58 | + e = assert_raises(ClientError, s3.put_bucket_acl, Bucket=bucket, ACL='public-read') |
| 59 | + assert (403, 'AccessDenied') == _get_status_and_error_code(e.response) |
| 60 | + |
| 61 | + # test interaction with bucket-level configuration |
| 62 | + s3.put_public_access_block( |
| 63 | + Bucket=bucket, |
| 64 | + PublicAccessBlockConfiguration={ |
| 65 | + 'BlockPublicAcls': False, |
| 66 | + 'IgnorePublicAcls': False, |
| 67 | + 'BlockPublicPolicy': True, |
| 68 | + 'RestrictPublicBuckets': False |
| 69 | + }) |
| 70 | + public_policy = json.dumps({ |
| 71 | + "Version": "2012-10-17", |
| 72 | + "Statement": [{ |
| 73 | + "Effect": "Allow", |
| 74 | + "Principal": {"AWS": "*"}, |
| 75 | + "Action": "*", |
| 76 | + "Resource": [ |
| 77 | + f"arn:aws:s3:::{bucket}", |
| 78 | + f"arn:aws:s3:::{bucket}/*" |
| 79 | + ] |
| 80 | + }] |
| 81 | + }) |
| 82 | + # reject PutBucketPolicy with public policy based on bucket config |
| 83 | + e = assert_raises(ClientError, s3.put_bucket_policy, |
| 84 | + Bucket=bucket, Policy=public_policy) |
| 85 | + assert (403, 'AccessDenied') == _get_status_and_error_code(e.response) |
| 86 | + finally: |
| 87 | + s3.delete_bucket(Bucket=bucket) |
| 88 | + finally: |
| 89 | + s3control.delete_public_access_block(AccountId=account_id) |
0 commit comments