Skip to content

Commit

Permalink
feat(config): limit use of CRAFT_* env vars
Browse files Browse the repository at this point in the history
CRAFT_* environment variables are now only used if the config item
is known to craft-application (not for app-specific config).
  • Loading branch information
lengau committed Aug 15, 2024
1 parent f618d27 commit f013ae9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion craft_application/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from craft_cli import emit
from typing_extensions import override

from craft_application import application, util
from craft_application import application, util, _config
from craft_application.services import base

if TYPE_CHECKING:
Expand Down Expand Up @@ -72,8 +72,18 @@ def get_raw(self, item: str) -> str:
class CraftEnvironmentHandler(ConfigHandler):
"""Configuration handler to get values from CRAFT environment variables."""

def __init__(self, app: application.AppMetadata) -> None:
super().__init__(app)
self._fields = _config.ConfigModel.model_fields


@override
def get_raw(self, item: str) -> str:
# Ensure that CRAFT_* env vars can only be used for configuration items
# known to craft-application.
if item not in self._fields:
raise KeyError(f"{item!r} not a general craft-application config item.")

return os.environ[f"CRAFT_{item.upper()}"]


Expand Down
22 changes: 21 additions & 1 deletion tests/unit/services/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import craft_cli
import pytest
import pytest_subprocess
import craft_application
from craft_application.services import config
from hypothesis import given, strategies

Expand Down Expand Up @@ -100,7 +101,7 @@ def test_app_environment_handler(app_environment_handler, item: str, content: st


@given(
item=strategies.text(alphabet=string.ascii_letters + "_", min_size=1),
item=strategies.sampled_from(list(craft_application.ConfigModel.model_fields)),
content=strategies.text(
alphabet=strategies.characters(categories=["L", "M", "N", "P", "S", "Z"])
),
Expand All @@ -112,6 +113,25 @@ def test_craft_environment_handler(craft_environment_handler, item: str, content
assert craft_environment_handler.get_raw(item) == content


@pytest.mark.parametrize(
("item", "content", "_"), CRAFT_APPLICATION_TEST_ENTRY_VALUES
)
def test_craft_environment_handler_success(monkeypatch, craft_environment_handler, item: str, content: str, _):
monkeypatch.setenv(f"CRAFT_{item.upper()}", content)

assert craft_environment_handler.get_raw(item) == content


@pytest.mark.parametrize(
("item", "content", "_"), APP_SPECIFIC_TEST_ENTRY_VALUES
)
def test_craft_environment_handler_error(monkeypatch, craft_environment_handler, item: str, content: str, _):
monkeypatch.setenv(f"CRAFT_{item.upper()}", content)

with pytest.raises(KeyError):
assert craft_environment_handler.get_raw(item) == content


@given(
item=strategies.text(alphabet=string.ascii_letters + "_", min_size=1),
content=strategies.text(
Expand Down

0 comments on commit f013ae9

Please sign in to comment.