| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pathlib |
| 4 |
import stat |
| 5 |
|
| 6 |
from ansible._internal._secret_input import load_secret_input_files |
| 7 |
from ansible.errors import AnsibleError |
| 8 |
from ansible.module_utils.secrets import mask_secrets |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
|
| 13 |
def _write(tmp_path: pathlib.Path, name: str, content: str) -> pathlib.Path: |
| 14 |
path = tmp_path / name |
| 15 |
path.write_text(content) |
| 16 |
return path |
| 17 |
|
| 18 |
|
| 19 |
def _write_executable(tmp_path: pathlib.Path, name: str, content: str) -> pathlib.Path: |
| 20 |
path = tmp_path / name |
| 21 |
path.write_text(content) |
| 22 |
path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 23 |
|
| 24 |
return path |
| 25 |
|
| 26 |
|
| 27 |
def test_yaml_secrets_are_masked(tmp_path: pathlib.Path) -> None: |
| 28 |
path = _write(tmp_path, 'secrets.yml', 'version: 1\nsecrets:\n - yaml-secret-alpha\n - yaml-secret-beta\n') |
| 29 |
|
| 30 |
load_secret_input_files([path]) |
| 31 |
|
| 32 |
assert mask_secrets('a yaml-secret-alpha and yaml-secret-beta z') == 'a $REDACTED$ and $REDACTED$ z' |
| 33 |
|
| 34 |
|
| 35 |
def test_json_secrets_are_masked(tmp_path: pathlib.Path) -> None: |
| 36 |
path = _write(tmp_path, 'secrets.json', '{"version": 1, "secrets": ["json-secret-value"]}') |
| 37 |
|
| 38 |
load_secret_input_files([path]) |
| 39 |
|
| 40 |
assert mask_secrets('a json-secret-value z') == 'a $REDACTED$ z' |
| 41 |
|
| 42 |
|
| 43 |
def test_multiple_files_are_masked(tmp_path: pathlib.Path) -> None: |
| 44 |
p1 = _write(tmp_path, 's1.yml', 'version: 1\nsecrets:\n - multi-file-one\n') |
| 45 |
p2 = _write(tmp_path, 's2.json', '{"version": 1, "secrets": ["multi-file-two"]}') |
| 46 |
|
| 47 |
load_secret_input_files([p1, p2]) |
| 48 |
|
| 49 |
assert mask_secrets('a multi-file-one and multi-file-two z') == 'a $REDACTED$ and $REDACTED$ z' |
| 50 |
|
| 51 |
|
| 52 |
def test_executable_secrets_are_masked(tmp_path: pathlib.Path) -> None: |
| 53 |
path = _write_executable( |
| 54 |
tmp_path, 'secrets.sh', |
| 55 |
'#!/bin/sh\nprintf \'version: 1\\nsecrets:\\n - exec-secret-value\\n\'\n', |
| 56 |
) |
| 57 |
|
| 58 |
load_secret_input_files([path]) |
| 59 |
|
| 60 |
assert mask_secrets('a exec-secret-value z') == 'a $REDACTED$ z' |
| 61 |
|
| 62 |
|
| 63 |
def test_empty_secrets_list_is_accepted(tmp_path: pathlib.Path) -> None: |
| 64 |
path = _write(tmp_path, 'secrets.yml', 'version: 1\nsecrets: []\n') |
| 65 |
|
| 66 |
# nothing to register, but the file is valid and must not raise |
| 67 |
load_secret_input_files([path]) |
| 68 |
|
| 69 |
|
| 70 |
def test_not_a_mapping(tmp_path: pathlib.Path) -> None: |
| 71 |
path = _write(tmp_path, 'secrets.yml', '- abcd\n- efgh\n') |
| 72 |
with pytest.raises(AnsibleError, match='must contain a mapping, not a list'): |
| 73 |
load_secret_input_files([path]) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize('version', ['2', 'true', '"1"']) |
| 77 |
def test_unsupported_version(tmp_path: pathlib.Path, version: str) -> None: |
| 78 |
path = _write(tmp_path, 'secrets.yml', f'version: {version}\nsecrets: [abcd]\n') |
| 79 |
with pytest.raises(AnsibleError, match='unsupported version'): |
| 80 |
load_secret_input_files([path]) |
| 81 |
|
| 82 |
|
| 83 |
def test_missing_version(tmp_path: pathlib.Path) -> None: |
| 84 |
path = _write(tmp_path, 'secrets.yml', 'secrets: [abcd]\n') |
| 85 |
with pytest.raises(AnsibleError, match='unsupported version'): |
| 86 |
load_secret_input_files([path]) |
| 87 |
|
| 88 |
|
| 89 |
def test_secrets_not_a_list(tmp_path: pathlib.Path) -> None: |
| 90 |
path = _write(tmp_path, 'secrets.yml', 'version: 1\nsecrets: notalist\n') |
| 91 |
with pytest.raises(AnsibleError, match="must contain a 'secrets' list"): |
| 92 |
load_secret_input_files([path]) |
| 93 |
|
| 94 |
|
| 95 |
def test_secrets_missing(tmp_path: pathlib.Path) -> None: |
| 96 |
path = _write(tmp_path, 'secrets.yml', 'version: 1\n') |
| 97 |
with pytest.raises(AnsibleError, match="must contain a 'secrets' list"): |
| 98 |
load_secret_input_files([path]) |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize('entry', ['12345', 'true', '1.5']) |
| 102 |
def test_non_string_secret_not_cast(tmp_path: pathlib.Path, entry: str) -> None: |
| 103 |
path = _write(tmp_path, 'secrets.yml', f'version: 1\nsecrets:\n - abcd\n - {entry}\n') |
| 104 |
# raising before registration proves the value is neither cast nor registered |
| 105 |
with pytest.raises(AnsibleError, match='must be a string'): |
| 106 |
load_secret_input_files([path]) |
| 107 |
|
| 108 |
|
| 109 |
def test_executable_non_zero_exit(tmp_path: pathlib.Path) -> None: |
| 110 |
path = _write_executable(tmp_path, 'secrets.sh', '#!/bin/sh\nexit 3\n') |
| 111 |
with pytest.raises(AnsibleError, match='returned non-zero exit status 3'): |
| 112 |
load_secret_input_files([path]) |