|
| 1 | +import os |
| 2 | +import s3fs |
| 3 | +import pathlib |
| 4 | +import json |
| 5 | +import moto |
| 6 | +import pytest |
| 7 | + |
| 8 | +from moto.moto_server.threaded_moto_server import ThreadedMotoServer |
| 9 | + |
| 10 | + |
| 11 | +# some spoofy server parameters |
| 12 | +# test parameters; don't modify these |
| 13 | +port = 5555 |
| 14 | +endpoint_uri = "http://127.0.0.1:%s/" % port |
| 15 | +test_bucket_name = "test" |
| 16 | +versioned_bucket_name = "test-versioned" |
| 17 | +secure_bucket_name = "test-secure" |
| 18 | + |
| 19 | +def get_boto3_client(): |
| 20 | + from botocore.session import Session |
| 21 | + |
| 22 | + # NB: we use the sync botocore client for setup |
| 23 | + session = Session() |
| 24 | + return session.create_client("s3", endpoint_url=endpoint_uri) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="module") |
| 28 | +def s3_base(): |
| 29 | + # writable local S3 system |
| 30 | + |
| 31 | + # This fixture is module-scoped, meaning that we can re-use the MotoServer across all tests |
| 32 | + ##### |
| 33 | + # lifted from https://github.com/fsspec/s3fs/blob/main/s3fs/tests/test_s3fs.py |
| 34 | + ##### |
| 35 | + server = ThreadedMotoServer(ip_address="127.0.0.1", port=port) |
| 36 | + server.start() |
| 37 | + # the user ID and secret key are needed when accessing a public bucket |
| 38 | + # since our S3 FS and bucket are not actually on an AWS system, they can have |
| 39 | + # bogus values |
| 40 | + if "AWS_SECRET_ACCESS_KEY" not in os.environ: |
| 41 | + os.environ["AWS_SECRET_ACCESS_KEY"] = "foo" |
| 42 | + if "AWS_ACCESS_KEY_ID" not in os.environ: |
| 43 | + os.environ["AWS_ACCESS_KEY_ID"] = "foo" |
| 44 | + os.environ.pop("AWS_PROFILE", None) |
| 45 | + |
| 46 | + print("server up") |
| 47 | + yield |
| 48 | + print("moto done") |
| 49 | + server.stop() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture() |
| 53 | +def s3fs_s3(s3_base): |
| 54 | + """ |
| 55 | + Create a fully functional "virtual" S3 FileSystem compatible with fsspec/s3fs. |
| 56 | + Method inspired by https://github.com/fsspec/s3fs/blob/main/s3fs/tests/test_s3fs.py |
| 57 | +
|
| 58 | + The S3 FS, being AWS-like but not actually physically deployed anywhere, still needs |
| 59 | + all the usual user IDs, secret keys, endpoint URLs etc; the setup makes use of the ACL=public |
| 60 | + configuration (public-read, or public-read-write). Public DOES NOT mean anon=True, but rather, |
| 61 | + All Users group – https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html |
| 62 | + Access permission to this group allows anyone with AWS credentials to access the resource. |
| 63 | + The requests need be signed (authenticated) or not. |
| 64 | +
|
| 65 | + Also, keys are encrypted using AWS-KMS |
| 66 | + https://docs.aws.amazon.com/kms/latest/developerguide/overview.html |
| 67 | + """ |
| 68 | + client = get_boto3_client() |
| 69 | + |
| 70 | + # see not above about ACL=public-read |
| 71 | + client.create_bucket(Bucket=test_bucket_name, ACL="public-read") |
| 72 | + |
| 73 | + client.create_bucket(Bucket=versioned_bucket_name, ACL="public-read") |
| 74 | + client.put_bucket_versioning( |
| 75 | + Bucket=versioned_bucket_name, VersioningConfiguration={"Status": "Enabled"} |
| 76 | + ) |
| 77 | + |
| 78 | + # initialize secure bucket |
| 79 | + client.create_bucket(Bucket=secure_bucket_name, ACL="public-read") |
| 80 | + policy = json.dumps( |
| 81 | + { |
| 82 | + "Version": "2012-10-17", |
| 83 | + "Id": "PutObjPolicy", |
| 84 | + "Statement": [ |
| 85 | + { |
| 86 | + "Sid": "DenyUnEncryptedObjectUploads", |
| 87 | + "Effect": "Deny", |
| 88 | + "Principal": "*", |
| 89 | + "Action": "s3:PutObject", |
| 90 | + "Resource": "arn:aws:s3:::{bucket_name}/*".format( |
| 91 | + bucket_name=secure_bucket_name |
| 92 | + ), |
| 93 | + "Condition": { |
| 94 | + "StringNotEquals": { |
| 95 | + "s3:x-amz-server-side-encryption": "aws:kms" |
| 96 | + } |
| 97 | + }, |
| 98 | + } |
| 99 | + ], |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + client.put_bucket_policy(Bucket=secure_bucket_name, Policy=policy) |
| 104 | + s3fs.S3FileSystem.clear_instance_cache() |
| 105 | + s3 = s3fs.S3FileSystem(anon=False, client_kwargs={"endpoint_url": endpoint_uri}) |
| 106 | + s3.invalidate_cache() |
| 107 | + |
| 108 | + yield s3 |
0 commit comments