Skip to content

Commit a387d4c

Browse files
committed
fix: do not log encoded secrets
Signed-off-by: Dariusz Duda <dariusz.duda@canonical.com>
1 parent 8e9893d commit a387d4c

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

craft_application/application.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ def run_managed(self, platform: str | None, build_for: str | None) -> None:
437437
# If using build secrets, put them in the environment of the managed
438438
# instance.
439439
secret_values = cast(secrets.BuildSecrets, self._secrets)
440+
# disable logging CRAFT_SECRETS value passed to the managed instance
441+
craft_cli.emit.set_secrets(list(secret_values.environment.values()))
442+
440443
env.update(secret_values.environment)
441444

442445
extra_args["env"] = env

docs/reference/changelog.rst

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ Utils
1515
- Add ``is_managed_mode()`` helper to check if running in managed mode.
1616
- Add ``get_hostname()`` helper to get a name of current host.
1717

18+
.. For a complete list of commits, check out the `4.8.0`_ release on GitHub.
19+
20+
4.7.1 (2025-Jan-13)
21+
-------------------
22+
23+
Application
24+
===========
25+
26+
- Do not log encoded secrets in managed mode if ``build_secrets``
27+
``AppFeature`` is enabled.
28+
29+
Documentation
30+
=============
31+
32+
- Add missing links to the GitHub releases.
33+
34+
For a complete list of commits, check out the `4.7.1`_ release on GitHub.
35+
1836
4.7.0 (2024-Dec-19)
1937
-------------------
2038

@@ -23,6 +41,8 @@ Application
2341

2442
- Allow applications to implement multi-base build plans.
2543

44+
For a complete list of commits, check out the `4.7.0`_ release on GitHub.
45+
2646
4.6.0 (2024-Dec-13)
2747
-------------------
2848

@@ -63,7 +83,7 @@ Git
6383
- Use ``craft.git`` for Git-related operations run with ``subprocess`` in
6484
``GitRepo``.
6585

66-
.. For a complete list of commits, check out the `4.6.0`_ release on GitHub.
86+
For a complete list of commits, check out the `4.6.0`_ release on GitHub.
6787

6888
4.5.0 (2024-Nov-28)
6989
-------------------
@@ -93,8 +113,8 @@ Services
93113

94114
- Add version to the template generation context of ``InitService``.
95115

96-
..
97-
For a complete list of commits, check out the `4.5.0`_ release on GitHub.
116+
117+
For a complete list of commits, check out the `4.5.0`_ release on GitHub.
98118

99119
4.4.0 (2024-Nov-08)
100120
-------------------
@@ -477,3 +497,6 @@ For a complete list of commits, check out the `2.7.0`_ release on GitHub.
477497
.. _4.4.0: https://github.com/canonical/craft-application/releases/tag/4.4.0
478498
.. _4.5.0: https://github.com/canonical/craft-application/releases/tag/4.5.0
479499
.. _4.6.0: https://github.com/canonical/craft-application/releases/tag/4.6.0
500+
.. _4.7.0: https://github.com/canonical/craft-application/releases/tag/4.7.0
501+
.. _4.7.1: https://github.com/canonical/craft-application/releases/tag/4.7.1
502+
.. _4.8.0: https://github.com/canonical/craft-application/releases/tag/4.7.1

tests/unit/test_application.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,35 @@ def test_run_managed_failure(app, fake_project, fake_build_plan):
559559

560560

561561
@pytest.mark.enable_features("build_secrets")
562-
def test_run_managed_secrets(app, fake_project, fake_build_plan):
562+
@pytest.mark.parametrize(
563+
"fake_encoded_environment",
564+
[
565+
pytest.param({}, id="empty"),
566+
pytest.param(
567+
{
568+
"CRAFT_TEST": "banana",
569+
},
570+
id="fake-env",
571+
),
572+
pytest.param(
573+
{
574+
"CRAFT_TEST_FRUIT": "banana",
575+
"CRAFT_TEST_VEGETABLE": "cucumber",
576+
},
577+
id="multiple-entries-env",
578+
),
579+
],
580+
)
581+
def test_run_managed_secrets(
582+
app, fake_project, fake_build_plan, fake_encoded_environment: dict[str, str], check
583+
):
563584
mock_provider = mock.MagicMock(spec_set=services.ProviderService)
564585
instance = mock_provider.instance.return_value.__enter__.return_value
565586
mock_execute = instance.execute_run
566587
app.services.provider = mock_provider
567588
app.project = fake_project
568589
app._build_plan = fake_build_plan
569590

570-
fake_encoded_environment = {
571-
"CRAFT_TEST": "banana",
572-
}
573591
app._secrets = secrets.BuildSecrets(
574592
environment=fake_encoded_environment,
575593
secret_strings=set(),
@@ -581,7 +599,13 @@ def test_run_managed_secrets(app, fake_project, fake_build_plan):
581599
assert len(mock_execute.mock_calls) == 1
582600
call = mock_execute.mock_calls[0]
583601
execute_env = call.kwargs["env"]
584-
assert execute_env["CRAFT_TEST"] == "banana"
602+
for secret_key, secret_val in fake_encoded_environment.items():
603+
with check:
604+
# value is passed to the underlying provider
605+
assert execute_env[secret_key] == secret_val
606+
assert secret_key in craft_cli.emit._log_filepath.read_text()
607+
# value is not leaking in logs
608+
assert secret_val not in craft_cli.emit._log_filepath.read_text()
585609

586610

587611
def test_run_managed_multiple(app, fake_project):

0 commit comments

Comments
 (0)