Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix s3 path detection #27

Merged
merged 2 commits into from
Sep 20, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ please refer to the man pages of `terraform --help`.

## Change Log

* v0.13: Fix S3 automatic `use_s3_path_style` detection when setting S3_HOSTNAME or LOCALSTACK_HOSTNAME
* v0.12: Fix local endpoint overrides for Terraform AWS provider 5.9.0; fix parsing of alias and region defined as value lists
* v0.11: Minor fix to handle boolean values in S3 backend configs
* v0.10: Add support for storing state files in local S3 backends
Expand Down
18 changes: 14 additions & 4 deletions bin/tflocal
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ your TF config.
"""

import os
import re
import sys
import glob
import subprocess

from urllib.parse import urlparse

PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
if os.path.isdir(os.path.join(PARENT_FOLDER, '.venv')):
sys.path.insert(0, PARENT_FOLDER)
Expand Down Expand Up @@ -212,9 +213,18 @@ def generate_s3_backend_config() -> str:
# ---

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

return not host.startswith("s3.")


def get_region() -> str:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = terraform-local
version = 0.12
version = 0.13
url = https://github.com/localstack/terraform-local
author = LocalStack Team
author_email = [email protected]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def test_use_s3_path_style(monkeypatch):
import_cli_code()
assert use_s3_path_style() # noqa

# test the case where the S3_HOSTNAME could be a Docker container name
monkeypatch.setenv("S3_HOSTNAME", "localstack")
import_cli_code()
assert use_s3_path_style() # noqa

# test the case where the S3_HOSTNAME could be an arbitrary host starting with `s3.`
monkeypatch.setenv("S3_HOSTNAME", "s3.internal.host")
import_cli_code()
assert not use_s3_path_style() # noqa


def test_provider_aliases(monkeypatch):
queue_name1 = f"q{short_uid()}"
Expand Down
Loading