S3 Ignore Subdomain for Bucketname Flag (#1419)

* Some circumstances need subdomains to be ignored rather that interpreted as bucketname, this patch allows such behaviour to be configured

* Adding helper case whereby localstack features as path based exception

* Remove whitespace :(
This commit is contained in:
Colin Jones 2018-03-21 16:33:09 +00:00 committed by Jack Danger
parent c13f77173f
commit 1a8a4a084d
3 changed files with 14 additions and 3 deletions

View File

@ -57,10 +57,11 @@ class ResponseObject(_TemplateEnvironmentMixin):
if not host:
host = urlparse(request.url).netloc
if (not host or host.startswith('localhost') or
if (not host or host.startswith('localhost') or host.startswith('localstack') or
re.match(r'^[^.]+$', host) or re.match(r'^.*\.svc\.cluster\.local$', host)):
# Default to path-based buckets for (1) localhost, (2) local host names that do not
# contain a "." (e.g., Docker container host names), or (3) kubernetes host names
# Default to path-based buckets for (1) localhost, (2) localstack hosts (e.g. localstack.dev),
# (3) local host names that do not contain a "." (e.g., Docker container host names), or
# (4) kubernetes host names
return False
match = re.match(r'^([^\[\]:]+)(:\d+)?$', host)

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import logging
import os
from boto.s3.key import Key
import re
@ -15,6 +16,8 @@ bucket_name_regex = re.compile("(.+).s3(.*).amazonaws.com")
def bucket_name_from_url(url):
if os.environ.get('S3_IGNORE_SUBDOMAIN_BUCKETNAME', '') in ['1', 'true']:
return None
domain = urlparse(url).netloc
if domain.startswith('www.'):

View File

@ -1,4 +1,5 @@
from __future__ import unicode_literals
import os
from sure import expect
from moto.s3.utils import bucket_name_from_url, _VersionedKeyStore, parse_region_from_url
@ -16,6 +17,12 @@ def test_localhost_without_bucket():
expect(bucket_name_from_url(
'https://www.localhost:5000/def')).should.equal(None)
def test_force_ignore_subdomain_for_bucketnames():
os.environ['S3_IGNORE_SUBDOMAIN_BUCKETNAME'] = '1'
expect(bucket_name_from_url('https://subdomain.localhost:5000/abc/resource')).should.equal(None)
del(os.environ['S3_IGNORE_SUBDOMAIN_BUCKETNAME'])
def test_versioned_key_store():
d = _VersionedKeyStore()