Skip to content

feat(connectors): allow HiddenValue in _env - #1853

Merged
wowi42 merged 4 commits into
pyinfra-dev:3.xfrom
Jesse-11:feat-hiddenvalues-env
Jul 14, 2026
Merged

wowi42 merged 4 commits into
pyinfra-dev:3.xfrom
Jesse-11:feat-hiddenvalues-env

Conversation

@Jesse-11

@Jesse-11 Jesse-11 commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR is to allow _env values to accept the new HiddenValue so secret env variables can be passed into shell commands and not be exposed in the command logs.

The issue was that _env values were treated as strings when building the export KEY=value command. This meant that HiddenValue values would be converted into their masked representation too early, this subsequently caused the masked value to be used in the actual command instead of only the displayed command

This PR preserves the HiddenValue objects while constructing the env variables. Hence the raw value can be used during execution while keeping the masked value in logs.

Linked Issue

Fixes #1798

Changes

  • Updates _env typing to support HiddenValue values:
EnvValue = str | HiddenValue

def generate_env(config: Config, value: Mapping[str, EnvValue] | None) -> dict[str, EnvValue]:
  • Allow env value to be None as operation typing currently allow _env=None for safer handling
  • Updates the environmental command construction to preserve HiddenValue until the command is rendered.
  • Keeps the full KEY=value assignment quoted as one shell token.
  • Adds tests covering:
    • HiddenValue being used as an _env value.
    • Raw command rendering using real secret
    • Masked command rendering hiding the secret
    • Shell-special characters remain safetly quoted

Testing

Executed connectors tests:

uv run pytest tests/test_connectors/test_util.py -k "env"

Ran a tmp deploy (as shown in issues #1798) with full success:

uv run pyinfra @local ../deploy.py -vv -y

Checklist

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • Pull request includes documentation for any new/updated operations/facts
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)
  • Pull request title follows the
    conventional commits format

@wowi42 wowi42 added new feature connectors Connector issues - builtin integrations with other tools. API API mode specific issues. labels Jun 24, 2026
@wowi42

wowi42 commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Reviewed this. The core fix is correct: swapping QuoteString(f"{key}={value}") for QuoteString(StringCommand(key, value, _separator="=")) keeps the HiddenValue object intact until render time, so get_raw_value unmasks to the real secret while get_masked_value falls back to HiddenValue.__str__ and shows *MASKED*. _env is only consumed in make_unix_command and all connectors route through it, so the change is well contained. Tests cover the raw, masked, and shell-char cases.

Two low severity nits, neither blocking:

  1. arguments_typed.py hardcodes None | Mapping[str, str | HiddenValue] while arguments.py defines and uses the EnvValue = str | HiddenValue alias. The two files are meant to stay in sync. The sync linter only warns on type mismatches so CI still passes, but if EnvValue gains a member later this signature will silently drift. Using EnvValue in both keeps them coupled.

  2. generate_env uses if value: rather than if value is not None:. Functionally harmless since the default is {} and dict.update({}) is a no-op, but it is the truthy-check pattern the repo conventions call out (distinguish unset from empty).

Otherwise looks good.

@Jesse-11

Jesse-11 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @wowi42. I've pushed through changes addressing the small nits you brought up, should be good for merging 😄.

@wowi42

wowi42 commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

LGTM

@wowi42
wowi42 merged commit d207c0f into pyinfra-dev:3.x Jul 14, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API API mode specific issues. connectors Connector issues - builtin integrations with other tools. new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_env in shell and script operations should accept HiddenValue

2 participants