Skip to content

Commit ff8c6e0

Browse files
committed
PB-2131 Add possibility to blacklist patterns
1 parent 242c88a commit ff8c6e0

6 files changed

Lines changed: 43 additions & 7 deletions

File tree

.env.default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SECRET_KEY=dummy
2020
HEALTHCHECK_ENDPOINT=healthcheck
2121
ALLOWED_HOSTS=*
2222
MANAGED_BUCKET_COLLECTION_PATTERNS=ch.meteoschweiz.ogd-,ch.bgdi-test.
23+
MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST=ch.meteoschweiz.ogd-precipitation
2324

2425
# these are just here for completeness
2526
AWS_ROLE_ARN=some-arn

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ The service is configured by Environment Variable:
548548
| AWS_S3_CUSTOM_DOMAIN | `None` | |
549549
| AWS_PRESIGNED_URL_EXPIRES | 3600 | AWS presigned url for asset upload expire time in seconds |
550550
| MANAGED_BUCKET_COLLECTION_PATTERNS | - | A list of prefix patterns for collections that go to the managed bucket |
551+
| MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST | - | A list of prefix patterns for collection that explicitly should not go to the managed bucket |
551552
| EXTERNAL_URL_REACHABLE_TIMEOUT | `5` | How long the external asset URL validator should try to connect to given asset in seconds |
552553

553554
#### **Development settings (only for local environment and DEV staging)**

app/config/settings_dev.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@
5252

5353
SHELL_PLUS_POST_IMPORTS = ['from tests.data_factory import Factory']
5454

55-
# Regex patterns of collections that should go to the managed bucket
55+
# Patterns prefixes of collections that should go to the managed bucket
5656
MANAGED_BUCKET_COLLECTION_PATTERNS = env.list(
5757
'MANAGED_BUCKET_COLLECTION_PATTERNS', default=["ch.meteoschweiz.ogd-"]
5858
)
5959

60+
# Patterns prefixes of collections that should should *not* go to the managed bucket
61+
MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST = env.list(
62+
'MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST', default=["ch.meteoschweiz.ogd-precipitation"]
63+
)
64+
6065
# Since it's impossible to recreate the service-account situation with minio
6166
# we inject some configuration in here to access the second bucket
6267
# in the same way as first bucket, via access/secrets

app/config/settings_prod.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,14 @@ def get_logging_config():
358358
) # if None, the host is taken from the request url
359359
STAC_BROWSER_BASE_PATH = env('STAC_BROWSER_BASE_PATH', default='browser/index.html')
360360

361-
# Regex patterns of collections that should go to the managed bucket
361+
# Pattern prefixes of collections that should go to the managed bucket
362362
MANAGED_BUCKET_COLLECTION_PATTERNS = env.list('MANAGED_BUCKET_COLLECTION_PATTERNS', default=[])
363363

364+
# Pattern prefixes of collections that should should *not* go to the managed bucket
365+
MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST = env.list(
366+
'MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST', default=[""]
367+
)
368+
364369
# the duration in seconds that the validator should try and reach the external URL
365370
EXTERNAL_URL_REACHABLE_TIMEOUT = env.int('EXTERNAL_URL_REACHABLE_TIMEOUT', default=5)
366371

app/stac_api/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,15 @@ def select_s3_bucket(collection_name) -> AVAILABLE_S3_BUCKETS:
602602
Select the correct s3 bucket based on matching patterns with the collection
603603
name
604604
"""
605-
patterns = settings.MANAGED_BUCKET_COLLECTION_PATTERNS
606-
607-
for pattern in patterns:
608-
if collection_name.startswith(pattern):
605+
whitelist_patterns = settings.MANAGED_BUCKET_COLLECTION_PATTERNS
606+
blacklist_patterns = settings.MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST
607+
608+
for whitelist_pattern in whitelist_patterns:
609+
if collection_name.startswith(whitelist_pattern):
610+
# if a pattern is found, let's also check it against the blacklist
611+
for blacklist_pattern in blacklist_patterns:
612+
if collection_name.startswith(blacklist_pattern):
613+
return AVAILABLE_S3_BUCKETS.legacy
609614
return AVAILABLE_S3_BUCKETS.managed
610615

611616
return AVAILABLE_S3_BUCKETS.legacy

app/tests/test_multiple_bucket_setup.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_managed_bucket_patterns(self, collection_name):
178178
default environment explicitly here.
179179
This might appear a bit artificial, but otherwise we have no way
180180
to machine-test the functioning of getting the values from the env
181-
list, use them as regex, and match the collection name.
181+
list and match the collection name.
182182
"""
183183
env = environ.Env()
184184
env.read_env("../.local.default")
@@ -188,3 +188,22 @@ def test_managed_bucket_patterns(self, collection_name):
188188
bucket_name = select_s3_bucket(collection_name)
189189

190190
self.assertEqual(bucket_name, AVAILABLE_S3_BUCKETS.managed)
191+
192+
@parameterized.expand([
193+
'ch.meteoschweiz.ogd-precipitation',
194+
])
195+
def test_managed_bucket_patterns_blacklist(self, collection_name):
196+
"""Test if the patterns in the environment work correctly. We take the
197+
default environment explicitly here.
198+
This might appear a bit artificial, but otherwise we have no way
199+
to machine-test the functioning of getting the values from the env
200+
list and match the collection name.
201+
"""
202+
env = environ.Env()
203+
env.read_env("../.local.default")
204+
205+
patterns = env.list('MANAGED_BUCKET_COLLECTION_PATTERNS_BLACKLIST')
206+
with self.settings(MANAGED_BUCKET_COLLECTION_PATTERNS=patterns):
207+
bucket_name = select_s3_bucket(collection_name)
208+
209+
self.assertEqual(bucket_name, AVAILABLE_S3_BUCKETS.legacy)

0 commit comments

Comments
 (0)