8000 deprecate env vars without prefix in CLI by alexrashed · Pull Request #11810 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

deprecate env vars without prefix in CLI #11810

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

Merged
merged 4 commits into from
Nov 20, 2024
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
4 changes: 2 additions & 2 deletions localstack-core/localstack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ def in_docker():
if TMP_FOLDER.startswith("/var/folders/") and os.path.exists("/private%s" % TMP_FOLDER):
TMP_FOLDER = "/private%s" % TMP_FOLDER

# whether to enable verbose debug logging
LS_LOG = eval_log_type("LS_LOG")
# whether to enable verbose debug logging ("LOG" is ued when using the CLI with LOCALSTACK_LOG instead of LS_LOG)
LS_LOG = eval_log_type("LS_LOG") or eval_log_type("LOG")
DEBUG = is_env_true("DEBUG") or LS_LOG in TRACE_LOG_LEVELS

# PUBLIC PREVIEW: 0 (default), 1 (preview)
Expand Down
27 changes: 26 additions & 1 deletion localstack-core/localstack/utils/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Union

from localstack import config, constants
from localstack.config import HostAndPort, default_ip, is_env_not_false, is_env_true
from localstack.config import (
HostAndPort,
default_ip,
is_env_not_false,
is_env_true,
load_environment,
)
from localstack.constants import VERSION
from localstack.runtime import hooks
from localstack.utils.container_networking import get_main_container_name
Expand Down Expand Up @@ -502,9 +508,28 @@ def _cfg(cfg: ContainerConfiguration):
@staticmethod
def config_env_vars(cfg: ContainerConfiguration):
"""Sets all env vars from config.CONFIG_ENV_VARS."""

profile_env = {}
if config.LOADED_PROFILES:
load_environment(profiles=",".join(config.LOADED_PROFILES), env=profile_env)

for env_var in config.CONFIG_ENV_VARS:
value = os.environ.get(env_var, None)
if value is not None:
if (
env_var != "CI"
and not env_var.startswith("LOCALSTACK_")
and env_var not in profile_env
):
# Show a warning here in case we are directly forwarding an environment variable from
# the system env to the container which has not been prefixed with LOCALSTACK_.
# Suppress the warning for the "CI" env var.
# Suppress the warning if the env var was set from the profile.
LOG.warning(
"Non-prefixed environment variable %(env_var)s is forwarded to the LocalStack container! "
"Please use `LOCALSTACK_%(env_var)s` instead of %(env_var)s to explicitly mark this environment variable to be forwarded form the CLI to the LocalStack Runtime.",
{"env_var": env_var},
)
cfg.env_vars[env_var] = value

@staticmethod
Expand Down
68 changes: 68 additions & 0 deletions tests/bootstrap/test_container_configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,71 @@ def test_default_localstack_container_configurator(
ports = diagnose["docker-inspect"]["NetworkSettings"]["Ports"]
for port in external_service_ports:
assert ports[f"{port}/tcp"] == [{"HostIp": "127.0.0.1", "HostPort": f"{port}"}]


def test_container_configurator_deprecation_warning(container_factory, monkeypatch, caplog):
# set non-prefixed well-known environment variable on the mocked OS env
monkeypatch.setenv("SERVICES", "1")

# config the container
container: Container = container_factory()
configure_container(container)

# assert the deprecation warning
assert "Non-prefixed environment variable" in caplog.text
assert "SERVICES" in container.config.env_vars


def test_container_configurator_no_deprecation_warning_on_prefix(
container_factory, monkeypatch, caplog
):
# set non-prefixed well-known environment variable on the mocked OS env
monkeypatch.setenv("LOCALSTACK_SERVICES", "1")

container: Container = container_factory()
configure_container(container)

assert "Non-prefixed environment variable" not in caplog.text
assert "LOCALSTACK_SERVICES" in container.config.env_vars


def test_container_configurator_no_deprecation_warning_for_CI_env_var(
container_factory, monkeypatch, caplog
):
# set the "CI" env var indicating that we are running in a CI environment
monkeypatch.setenv("CI", "1")

container: Container = container_factory()
configure_container(container)

assert "Non-prefixed environment variable" not in caplog.text
assert "CI" in container.config.env_vars


def test_container_configurator_no_deprecation_warning_on_profile(
container_factory, monkeypatch, caplog, tmp_path
):
from localstack import config

# create a test profile
tmp_config_dir = tmp_path
test_profile = tmp_config_dir / "testprofile.env"
test_profile.write_text(
textwrap.dedent(
"""
SERVICES=1
"""
).strip()
)

# patch the profile config / env
monkeypatch.setattr(config, "CONFIG_DIR", tmp_config_dir)
monkeypatch.setattr(config, "LOADED_PROFILES", ["testprofile"])
monkeypatch.setenv("SERVICES", "1")

container: Container = container_factory()
configure_container(container)

# assert that profile env vars do not raise a deprecation warning
assert "Non-prefixed environment variable SERVICES" not in caplog.text
assert "SERVICES" in co 422D ntainer.config.env_vars
Loading
0