Files
mattandClaude Sonnet 5.5 d5636c46c7
test / test (push) Failing after 2s
release / validate (push) Successful in 2s
release / build (linux-amd64, linux-amd64) (push) Successful in 38s
release / build (macos-arm64, macos-arm64) (push) Successful in 45s
release / build (linux-arm64, linux-arm64) (push) Successful in 1m19s
release / release (push) Successful in 15s
Initial implementation of EnvBao
Single-binary CLI that injects environment variables from an OpenBao
server into the shell via eval "$(envbao)".

- Strict permission policy for ~/.envbao (exits 78 if unsafe)
- KV v1/v2 engine with auto-detection, pluggable engine registry
- Safe quoting for bash/zsh/sh/fish, protected-variable denylist
- Audit logging (stderr, private rotating file, syslog); no secrets logged
- Gitea pipelines for tests and SemVer-tag releases
- PyInstaller spec, docs, BSD-3-Clause licence

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 21:20:15 +01:00

104 lines
2.5 KiB
Python

import shlex
import subprocess
import pytest
from envbao.errors import ConfigError
from envbao.render import detect_shell, is_protected, is_valid_name, render
@pytest.mark.parametrize("name", ["A", "_a", "FOO_BAR1", "a"])
def test_valid_names(name: str) -> None:
assert is_valid_name(name)
@pytest.mark.parametrize("name", ["", "1A", "A-B", "A B", "A=B", "A\n", "A;rm", "é"])
def test_invalid_names(name: str) -> None:
assert not is_valid_name(name)
@pytest.mark.parametrize("name", ["PATH", "LD_PRELOAD", "DYLD_INSERT_LIBRARIES", "BASH_FUNC_x%%"])
def test_protected(name: str) -> None:
assert is_protected(name)
def test_not_protected() -> None:
assert not is_protected("MY_API_KEY")
def test_posix_render() -> None:
assert render({"A": "b c", "D": "it's"}) == "export A='b c'\nexport D='it'\"'\"'s'\n"
def test_empty_render() -> None:
assert render({}) == ""
def test_fish_render() -> None:
assert render({"A": "it's \\ ok"}, "fish") == "set -gx A 'it\\'s \\\\ ok'\n"
@pytest.mark.parametrize(
"value",
[
"$(touch /tmp/pwned)",
"`id`",
"a; b",
"a\nb",
"'\"'",
"$HOME",
"\\",
"*",
"!!",
"a b\tc",
"é",
],
)
def test_roundtrip_through_real_shell(value: str) -> None:
script = render({"V": value}) + 'printf %s "$V"'
out = subprocess.run( # noqa: S603
["/bin/sh", "-c", script],
capture_output=True,
check=True,
text=True, # noqa: S607
)
assert out.stdout == value
def test_values_are_single_argument() -> None:
assert shlex.split(render({"V": "a; b"}).split("=", 1)[1]) == ["a; b"]
def test_invalid_name_raises() -> None:
with pytest.raises(ConfigError, match="invalid environment"):
render({"BAD NAME": "x"})
def test_nul_raises() -> None:
with pytest.raises(ConfigError, match="NUL"):
render({"A": "x\x00y"})
def test_unknown_shell() -> None:
with pytest.raises(ConfigError, match="unsupported"):
render({"A": "x"}, "powershell")
@pytest.mark.parametrize(
"shell, expected",
[
("/bin/zsh", "zsh"),
("/usr/bin/fish", "fish"),
("/bin/bash", "bash"),
("/bin/tcsh", "sh"),
("", "sh"),
],
)
def test_detect_shell(shell: str, expected: str) -> None:
assert detect_shell({"SHELL": shell}) == expected
def test_detect_shell_from_os_environ(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("SHELL", "/bin/zsh")
assert detect_shell() == "zsh"