| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import argparse |
| 6 |
import json |
| 7 |
import boto3 |
| 8 |
import botocore |
| 9 |
import sys |
| 10 |
import os |
| 11 |
import random |
| 12 |
|
| 13 |
REGION = 'us-west-2' |
| 14 |
REGION_EAST_1 = 'us-east-1' |
| 15 |
s3 = boto3.resource('s3') |
| 16 |
s3_client = boto3.client('s3', region_name=REGION) |
| 17 |
s3_client_east1 = boto3.client('s3', region_name=REGION_EAST_1) |
| 18 |
|
| 19 |
|
| 20 |
MB = 1024*1024 |
| 21 |
GB = 1024*1024*1024 |
| 22 |
|
| 23 |
parser = argparse.ArgumentParser() |
| 24 |
parser.add_argument( |
| 25 |
'action', |
| 26 |
choices=['init', 'clean'], |
| 27 |
help='Initialize or clean up the test buckets') |
| 28 |
parser.add_argument( |
| 29 |
'bucket_name', |
| 30 |
nargs='?', |
| 31 |
help='The bucket name base to use for the test buckets. If not specified, the $CRT_S3_TEST_BUCKET_NAME will be used, if set. \ |
| 32 |
Otherwise, a random name will be generated.') |
| 33 |
parser.add_argument( |
| 34 |
'--large_objects', |
| 35 |
action='store_true', |
| 36 |
help='enable helper to create pre-existing large objects.') |
| 37 |
parser.add_argument( |
| 38 |
'--create_public_bucket', |
| 39 |
action='store_true', |
| 40 |
help='Allow script to create a public bucket. If not specified, the script will not attempt to create a public bucket. \ |
| 41 |
Note: Some aws-c-s3 tests will fail without public bucket.') |
| 42 |
|
| 43 |
args = parser.parse_args() |
| 44 |
|
| 45 |
if args.bucket_name is not None: |
| 46 |
BUCKET_NAME_BASE = args.bucket_name |
| 47 |
elif "CRT_S3_TEST_BUCKET_NAME" in os.environ: |
| 48 |
BUCKET_NAME_BASE = os.environ['CRT_S3_TEST_BUCKET_NAME'] |
| 49 |
else: |
| 50 |
|
| 51 |
BUCKET_NAME_BASE = 'aws-c-s3-test-bucket-' + str(random.random())[2:8] |
| 52 |
|
| 53 |
PUBLIC_BUCKET_NAME = BUCKET_NAME_BASE + "-public" |
| 54 |
|
| 55 |
|
| 56 |
ASYNC_ERROR_XML = ( |
| 57 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 58 |
'<Error>' |
| 59 |
'<Code>InternalError</Code>' |
| 60 |
'<Message>We encountered an internal error. Please try again.</Message>' |
| 61 |
'<RequestId>656c76696e6727732072657175657374</RequestId>' |
| 62 |
'<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>' |
| 63 |
'</Error>' |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def create_bytes(size): |
| 68 |
return bytes(size) |
| 69 |
|
| 70 |
|
| 71 |
def put_pre_existing_objects(size_or_body, keyname, bucket=BUCKET_NAME_BASE, |
| 72 |
sse=None, public_read=False, content_type=None, |
| 73 |
client=s3_client): |
| 74 |
print(f"s3://{bucket}/{keyname} - Uploading...") |
| 75 |
if size_or_body == 0: |
| 76 |
client.put_object(Bucket=bucket, Key=keyname) |
| 77 |
return |
| 78 |
|
| 79 |
if isinstance(size_or_body, int): |
| 80 |
body = create_bytes(size_or_body) |
| 81 |
else: |
| 82 |
body = size_or_body |
| 83 |
|
| 84 |
args = {'Bucket': bucket, 'Key': keyname, 'Body': body} |
| 85 |
if sse == 'aes256': |
| 86 |
args['ServerSideEncryption'] = 'AES256' |
| 87 |
elif sse == 'aes256-c': |
| 88 |
random_key = os.urandom(32) |
| 89 |
args['SSECustomerKey'] = random_key |
| 90 |
args['SSECustomerAlgorithm'] = 'AES256' |
| 91 |
elif sse == 'kms': |
| 92 |
args['ServerSideEncryption'] = 'aws:kms' |
| 93 |
args['SSEKMSKeyId'] = 'alias/aws/s3' |
| 94 |
|
| 95 |
if public_read: |
| 96 |
args['ACL'] = 'public-read' |
| 97 |
|
| 98 |
if content_type: |
| 99 |
args['ContentType'] = content_type |
| 100 |
|
| 101 |
try: |
| 102 |
client.put_object(**args) |
| 103 |
except botocore.exceptions.ClientError as e: |
| 104 |
if public_read and e.response['Error']['Code'] == 'AccessDenied': |
| 105 |
print("Check your account level S3 settings, public access may be blocked.") |
| 106 |
raise |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
def create_bucket(client, **kwargs): |
| 112 |
try: |
| 113 |
print(f"s3://{kwargs['Bucket']} - Creating bucket...") |
| 114 |
client.create_bucket(**kwargs) |
| 115 |
|
| 116 |
except botocore.exceptions.ClientError as e: |
| 117 |
if e.response['Error']['Code'] in ('BucketAlreadyOwnedByYou', 'BucketAlreadyExists'): |
| 118 |
return |
| 119 |
|
| 120 |
raise e |
| 121 |
|
| 122 |
|
| 123 |
def create_bucket_with_lifecycle(availability_zone=None, client=s3_client, region=REGION): |
| 124 |
if availability_zone is not None: |
| 125 |
bucket_config = { |
| 126 |
'Location': { |
| 127 |
'Type': 'AvailabilityZone', |
| 128 |
'Name': availability_zone |
| 129 |
}, |
| 130 |
'Bucket': { |
| 131 |
'Type': 'Directory', |
| 132 |
'DataRedundancy': 'SingleAvailabilityZone' |
| 133 |
} |
| 134 |
} |
| 135 |
bucket_name = BUCKET_NAME_BASE+f"--{availability_zone}--x-s3" |
| 136 |
else: |
| 137 |
bucket_config = {'LocationConstraint': region} |
| 138 |
bucket_name = BUCKET_NAME_BASE |
| 139 |
|
| 140 |
create_bucket(client, |
| 141 |
Bucket=bucket_name, |
| 142 |
CreateBucketConfiguration=bucket_config) |
| 143 |
print(f"s3://{bucket_name} - Configuring bucket...") |
| 144 |
if availability_zone is not None: |
| 145 |
|
| 146 |
|
| 147 |
account_id = boto3.client( |
| 148 |
'sts').get_caller_identity().get('Account') |
| 149 |
bucket_policy = { |
| 150 |
"Version": "2008-10-17", |
| 151 |
"Statement": [ |
| 152 |
{ |
| 153 |
"Effect": "Allow", |
| 154 |
"Principal": { |
| 155 |
"Service": "lifecycle.s3.amazonaws.com" |
| 156 |
}, |
| 157 |
"Action": "s3express:CreateSession", |
| 158 |
"Condition": { |
| 159 |
"StringEquals": { |
| 160 |
"s3express:SessionMode": "ReadWrite" |
| 161 |
} |
| 162 |
}, |
| 163 |
"Resource": [ |
| 164 |
f"arn:aws:s3express:{region}:{account_id}:bucket/{bucket_name}" |
| 165 |
] |
| 166 |
} |
| 167 |
] |
| 168 |
} |
| 169 |
client.put_bucket_policy( |
| 170 |
Bucket=bucket_name, Policy=json.dumps(bucket_policy)) |
| 171 |
|
| 172 |
client.put_bucket_lifecycle_configuration( |
| 173 |
Bucket=bucket_name, |
| 174 |
LifecycleConfiguration={ |
| 175 |
'Rules': [ |
| 176 |
{ |
| 177 |
'ID': 'Abort all incomplete multipart uploads after 1 day', |
| 178 |
'Status': 'Enabled', |
| 179 |
'Filter': {'Prefix': ''}, |
| 180 |
'AbortIncompleteMultipartUpload': {'DaysAfterInitiation': 1}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
'ID': 'Objects under upload directory expire after 1 day', |
| 184 |
'Status': 'Enabled', |
| 185 |
'Filter': {'Prefix': 'upload/'}, |
| 186 |
'Expiration': {'Days': 1}, |
| 187 |
}, |
| 188 |
]}) |
| 189 |
|
| 190 |
put_pre_existing_objects( |
| 191 |
10*MB, 'pre-existing-10MB', bucket=bucket_name, client=client) |
| 192 |
put_pre_existing_objects( |
| 193 |
1*MB, 'pre-existing-1MB', bucket=bucket_name, client=client) |
| 194 |
|
| 195 |
if args.large_objects: |
| 196 |
put_pre_existing_objects( |
| 197 |
2*GB, 'pre-existing-2GB', bucket=bucket_name, client=client) |
| 198 |
|
| 199 |
if availability_zone is None: |
| 200 |
put_pre_existing_objects( |
| 201 |
10*MB, 'pre-existing-10MB-aes256-c', sse='aes256-c', bucket=bucket_name) |
| 202 |
put_pre_existing_objects( |
| 203 |
10*MB, 'pre-existing-10MB-aes256', sse='aes256', bucket=bucket_name) |
| 204 |
put_pre_existing_objects( |
| 205 |
10*MB, 'pre-existing-10MB-kms', sse='kms', bucket=bucket_name) |
| 206 |
put_pre_existing_objects( |
| 207 |
1*MB, 'pre-existing-1MB-@', bucket=bucket_name) |
| 208 |
put_pre_existing_objects( |
| 209 |
0, 'pre-existing-empty', bucket=bucket_name) |
| 210 |
put_pre_existing_objects( |
| 211 |
ASYNC_ERROR_XML, 'pre-existing-async-error-xml', bucket=bucket_name, content_type='application/xml') |
| 212 |
if args.large_objects: |
| 213 |
put_pre_existing_objects( |
| 214 |
256*MB, 'pre-existing-256MB', bucket=bucket_name) |
| 215 |
put_pre_existing_objects( |
| 216 |
256*MB, 'pre-existing-256MB-@', bucket=bucket_name) |
| 217 |
put_pre_existing_objects( |
| 218 |
2*GB, 'pre-existing-2GB-@', bucket=bucket_name) |
| 219 |
else: |
| 220 |
print("Skipping large objects, run with --large_objects if you need these.") |
| 221 |
|
| 222 |
|
| 223 |
def create_bucket_with_public_object(): |
| 224 |
create_bucket(s3_client, |
| 225 |
Bucket=PUBLIC_BUCKET_NAME, |
| 226 |
CreateBucketConfiguration={'LocationConstraint': REGION}, |
| 227 |
ObjectOwnership='ObjectWriter' |
| 228 |
) |
| 229 |
print(f"s3://{PUBLIC_BUCKET_NAME} - Configuring bucket...") |
| 230 |
s3_client.put_public_access_block( |
| 231 |
Bucket=PUBLIC_BUCKET_NAME, |
| 232 |
PublicAccessBlockConfiguration={ |
| 233 |
'BlockPublicAcls': False, |
| 234 |
} |
| 235 |
) |
| 236 |
|
| 237 |
put_pre_existing_objects( |
| 238 |
1*MB, 'pre-existing-1MB', bucket=PUBLIC_BUCKET_NAME, public_read=True) |
| 239 |
|
| 240 |
|
| 241 |
def cleanup(bucket_name, availability_zone=None, client=s3_client): |
| 242 |
if availability_zone is not None: |
| 243 |
bucket_name = bucket_name+f"--{availability_zone}--x-s3" |
| 244 |
|
| 245 |
print(f"s3://{bucket_name}/* - Listing objects...") |
| 246 |
try: |
| 247 |
response = client.list_objects_v2(Bucket=bucket_name) |
| 248 |
|
| 249 |
objects = response.get("Contents", []) |
| 250 |
except botocore.exceptions.ClientError as e: |
| 251 |
if e.response['Error']['Code'] == 'NoSuchBucket': |
| 252 |
print(f"s3://{bucket_name} - Did not exist. Moving on...") |
| 253 |
return |
| 254 |
raise |
| 255 |
objects = list(map(lambda x: {"Key": x["Key"]}, objects)) |
| 256 |
if objects: |
| 257 |
print(f"s3://{bucket_name}/* - Deleting {len(objects)} objects...") |
| 258 |
client.delete_objects(Bucket=bucket_name, Delete={"Objects": objects}) |
| 259 |
print(f"s3://{bucket_name} - Deleting bucket...") |
| 260 |
client.delete_bucket(Bucket=bucket_name) |
| 261 |
|
| 262 |
|
| 263 |
if args.action == 'init': |
| 264 |
create_bucket_with_lifecycle("use1-az4", s3_client_east1, "us-east-1") |
| 265 |
create_bucket_with_lifecycle("usw2-az1") |
| 266 |
create_bucket_with_lifecycle() |
| 267 |
if args.create_public_bucket: |
| 268 |
create_bucket_with_public_object() |
| 269 |
else: |
| 270 |
print("Skipping public bucket, run with --create_public_bucket if you need these.") |
| 271 |
|
| 272 |
if os.environ.get('CRT_S3_TEST_BUCKET_NAME') != BUCKET_NAME_BASE: |
| 273 |
print( |
| 274 |
f"*** Set the environment variable $CRT_S3_TEST_BUCKET_NAME to {BUCKET_NAME_BASE} before running the tests ***") |
| 275 |
|
| 276 |
elif args.action == 'clean': |
| 277 |
if "CRT_S3_TEST_BUCKET_NAME" not in os.environ and args.bucket_name is None: |
| 278 |
exit("Set the environment variable CRT_S3_TEST_BUCKET_NAME before clean up, or pass in bucket_name as argument.") |
| 279 |
cleanup(BUCKET_NAME_BASE, "use1-az4", s3_client_east1) |
| 280 |
cleanup(BUCKET_NAME_BASE, "usw2-az1") |
| 281 |
cleanup(BUCKET_NAME_BASE) |
| 282 |
cleanup(PUBLIC_BUCKET_NAME) |