Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ repos:
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: fix-encoding-pragma
args: [--remove]
- id: name-tests-test
args: [--pytest-test-first]
- id: trailing-whitespace
Expand All @@ -40,7 +38,7 @@ repos:
args: ["--profile", "black", "--filter-files"]

- repo: https://github.com/asottile/pyupgrade
rev: v3.20.0
rev: v3.22.0
hooks:
- id: pyupgrade
args:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Les variables d'environnement suivantes peuvent être nécessaires, par module :
* `ROK4_S3_KEY` : Clé(s) de(s) serveur(s) S3
* `ROK4_S3_SECRETKEY` : Clé(s) secrète(s) de(s) serveur(s) S3
* `ROK4_S3_URL` : URL de(s) serveur(s) S3
* `ROK4_S3_REGION` : Région de(s) serveur(s) S3
* `ROK4_SSL_NO_VERIFY` : Désactivation de la vérification SSL pour les accès S3 (n'importe quelle valeur non vide)
* `tile_matrix_set` :
* `ROK4_TMS_DIRECTORY` : Dossier racine (fichier ou objet) des tile matrix sets
Expand Down
1 change: 1 addition & 0 deletions README.pypi.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Following environment variables could be used, by module :
* `ROK4_S3_KEY` : Key(s) for S3 server(s)
* `ROK4_S3_SECRETKEY` : Secret key(s) for S3 server(s)
* `ROK4_S3_URL` : URL(s) for S3 server(s)
* `ROK4_S3_REGION` : Region(s) for S3 server(s)
* `ROK4_SSL_NO_VERIFY` : Disable SSL conrols for S3 access (any non empty value)
* `tile_matrix_set` :
* `ROK4_TMS_DIRECTORY` : Root directory (file or object) for tile matrix sets
Expand Down
11 changes: 10 additions & 1 deletion src/rok4/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- ROK4_S3_KEY
- ROK4_S3_SECRETKEY
- ROK4_S3_URL
- ROK4_S3_REGION (optional)

To use several S3 clusters, each environment variable have to contain a list (comma-separated), with the same number of elements

Expand All @@ -36,6 +37,8 @@
- ROK4_S3_KEY=KEY1,KEY2
- ROK4_S3_SECRETKEY=SKEY1,SKEY2
- ROK4_S3_URL=https://s3.storage.fr,https://s4.storage.fr
- ROK4_S3_REGION=region1,region2


To precise the cluster to use, bucket name should be bucket_name@s3.storage.fr or bucket_name@s4.storage.fr. If no host is defined (no @) in the bucket name, first S3 cluster is used
"""
Expand Down Expand Up @@ -144,8 +147,13 @@ def __get_s3_client(bucket_name: str) -> Tuple[Dict[str, Union["boto3.client", s
keys = os.environ["ROK4_S3_KEY"].split(",")
secret_keys = os.environ["ROK4_S3_SECRETKEY"].split(",")
urls = os.environ["ROK4_S3_URL"].split(",")
# ROK4_S3_REGION est optionnelle : None est utilisé pour les clusters sans région définie
if os.environ.get("ROK4_S3_REGION", "") != "":
regions = os.environ["ROK4_S3_REGION"].split(",")
else:
regions = [None] * len(keys)

if len(keys) != len(secret_keys) or len(keys) != len(urls):
if len(keys) != len(secret_keys) or len(keys) != len(urls) or len(keys) != len(regions):
raise StorageError(
"S3",
"S3 informations in environment variables are inconsistent : same number of element in each list is required",
Expand All @@ -164,6 +172,7 @@ def __get_s3_client(bucket_name: str) -> Tuple[Dict[str, Union["boto3.client", s
aws_secret_access_key=secret_keys[i],
verify=verify,
endpoint_url=urls[i],
region_name=regions[i],
config=botocore.config.Config(tcp_keepalive=True, max_pool_connections=10),
),
"key": keys[i],
Expand Down
54 changes: 54 additions & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,60 @@ def test_s3_invalid_envs():
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(
os.environ,
{
"ROK4_S3_URL": "a,b",
"ROK4_S3_SECRETKEY": "b,c",
"ROK4_S3_KEY": "c,d",
"ROK4_S3_REGION": "r1,r2,r3",
},
clear=True,
)
def test_s3_invalid_optionnal_envs():
with pytest.raises(StorageError):
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(
os.environ,
{"ROK4_S3_URL": "https://a", "ROK4_S3_SECRETKEY": "b", "ROK4_S3_KEY": "c"},
clear=True,
)
@mock.patch("rok4.storage.boto3.client")
def test_s3_client_without_region(mocked_s3_client):
disconnect_s3_clients()
s3_instance = MagicMock()
s3_instance.get_object.return_value = {"Body": MagicMock(read=MagicMock(return_value=b"data"))}
mocked_s3_client.return_value = s3_instance
get_data_str("s3://bucket/path/to/object-no-region")
mocked_s3_client.assert_called_once()
assert mocked_s3_client.call_args.kwargs["region_name"] is None
disconnect_s3_clients()


@mock.patch.dict(
os.environ,
{
"ROK4_S3_URL": "https://a",
"ROK4_S3_SECRETKEY": "b",
"ROK4_S3_KEY": "c",
"ROK4_S3_REGION": "r1",
},
clear=True,
)
@mock.patch("rok4.storage.boto3.client")
def test_s3_client_with_region(mocked_s3_client):
disconnect_s3_clients()
s3_instance = MagicMock()
s3_instance.get_object.return_value = {"Body": MagicMock(read=MagicMock(return_value=b"data"))}
mocked_s3_client.return_value = s3_instance
get_data_str("s3://bucket/path/to/object-with-region")
mocked_s3_client.assert_called_once()
assert mocked_s3_client.call_args.kwargs["region_name"] == "r1"
disconnect_s3_clients()


@mock.patch.dict(
os.environ, {"ROK4_S3_URL": "a", "ROK4_S3_SECRETKEY": "b", "ROK4_S3_KEY": "c"}, clear=True
)
Expand Down
Loading