Skip to content

Commit 1aedeef

Browse files
authored
fix s3 path detection (#27)
1 parent ecb1491 commit 1aedeef

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ please refer to the man pages of `terraform --help`.
3939

4040
## Change Log
4141

42+
* v0.13: Fix S3 automatic `use_s3_path_style` detection when setting S3_HOSTNAME or LOCALSTACK_HOSTNAME
4243
* v0.12: Fix local endpoint overrides for Terraform AWS provider 5.9.0; fix parsing of alias and region defined as value lists
4344
* v0.11: Minor fix to handle boolean values in S3 backend configs
4445
* v0.10: Add support for storing state files in local S3 backends

bin/tflocal

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ your TF config.
1010
"""
1111

1212
import os
13-
import re
1413
import sys
1514
import glob
1615
import subprocess
1716

17+
from urllib.parse import urlparse
18+
1819
PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
1920
if os.path.isdir(os.path.join(PARENT_FOLDER, '.venv')):
2021
sys.path.insert(0, PARENT_FOLDER)
@@ -212,9 +213,18 @@ def generate_s3_backend_config() -> str:
212213
# ---
213214

214215
def use_s3_path_style() -> bool:
215-
"""Whether to use S3 path addressing (depending on the configured S3 endpoint)"""
216-
regex = r"^[a-z]+://(localhost|[0-9.]+)(:[0-9]+)?$"
217-
return bool(re.match(regex, get_service_endpoint("s3")))
216+
"""
217+
Whether to use S3 path addressing (depending on the configured S3 endpoint)
218+
If the endpoint starts with the `s3.` prefix, LocalStack will recognize virtual host addressing. If the endpoint
219+
does not start with it, use path style. This also allows overriding the endpoint to always use path style in case of
220+
inter container communications in Docker.
221+
"""
222+
try:
223+
host = urlparse(get_service_endpoint("s3")).hostname
224+
except ValueError:
225+
host = ""
226+
227+
return not host.startswith("s3.")
218228

219229

220230
def get_region() -> str:

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = terraform-local
3-
version = 0.12
3+
version = 0.13
44
url = https://github.com/localstack/terraform-local
55
author = LocalStack Team
66
author_email = [email protected]

tests/test_apply.py

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ def test_use_s3_path_style(monkeypatch):
3434
import_cli_code()
3535
assert use_s3_path_style() # noqa
3636

37+
# test the case where the S3_HOSTNAME could be a Docker container name
38+
monkeypatch.setenv("S3_HOSTNAME", "localstack")
39+
import_cli_code()
40+
assert use_s3_path_style() # noqa
41+
42+
# test the case where the S3_HOSTNAME could be an arbitrary host starting with `s3.`
43+
monkeypatch.setenv("S3_HOSTNAME", "s3.internal.host")
44+
import_cli_code()
45+
assert not use_s3_path_style() # noqa
46+
3747

3848
def test_provider_aliases(monkeypatch):
3949
queue_name1 = f"q{short_uid()}"

0 commit comments

Comments
 (0)