-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli.py
More file actions
189 lines (169 loc) · 7.71 KB
/
test_cli.py
File metadata and controls
189 lines (169 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from collections.abc import Sequence
from pathlib import Path
import pyrage
import pytest
from saltstack_age.cli import main
from saltstack_age.identities import read_identity_file
from saltstack_age.secure_value import (
IdentitySecureValue,
PassphraseSecureValue,
parse_secure_value,
)
def test_encrypt__passphrase(capsys: pytest.CaptureFixture[str]) -> None:
# Run the CLI tool
main(["-P", "woah that is so secret", "enc", "another secret"])
# Ensure we get a passphrase secure value string
secure_value_string = capsys.readouterr().out
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, PassphraseSecureValue)
# Ensure we can decrypt it
assert secure_value.decrypt("woah that is so secret") == "another secret"
def test_encrypt__single_recipient(
capsys: pytest.CaptureFixture[str],
example_age_key_path_str: str,
) -> None:
# Run the CLI tool
main(["-i", example_age_key_path_str, "enc", "foo"])
# Ensure we get an identity secure value string
secure_value_string = capsys.readouterr().out
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, IdentitySecureValue)
# Ensure we can decrypt it using the same identity
assert secure_value.decrypt(read_identity_file(example_age_key_path_str)) == "foo"
def test_encrypt__recipient_only(capsys: pytest.CaptureFixture[str]) -> None:
identity = pyrage.x25519.Identity.generate()
public_key = str(identity.to_public())
# Run the CLI tool with a recipient public key
main(["-r", public_key, "enc", "foo"])
# Ensure we get an identity secure value string
secure_value_string = capsys.readouterr().out
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, IdentitySecureValue)
# Ensure we can decrypt it using the original identity
assert secure_value.decrypt(identity) == "foo"
def test_encrypt__recipient_and_identity(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
identity_file = pyrage.x25519.Identity.generate()
identity_file_path = tmp_path / "identity"
_ = identity_file_path.write_text(str(identity_file))
identity_recipient = pyrage.x25519.Identity.generate()
public_key = str(identity_recipient.to_public())
# Run the CLI tool with both -i and -r
main(["-i", str(identity_file_path), "-r", public_key, "enc", "foo"])
# Ensure we get an identity secure value string
secure_value_string = capsys.readouterr().out
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, IdentitySecureValue)
# Ensure we can decrypt it with either key
assert secure_value.decrypt(identity_file) == "foo"
assert secure_value.decrypt(identity_recipient) == "foo"
def test_encrypt__recipient_with_passphrase_fails() -> None:
public_key = str(pyrage.x25519.Identity.generate().to_public())
with pytest.raises(SystemExit):
main(["-r", public_key, "-P", "pw", "enc", "foo"])
def test_decrypt__recipient_fails() -> None:
public_key = str(pyrage.x25519.Identity.generate().to_public())
with pytest.raises(SystemExit):
main(["-r", public_key, "dec", "ENC[age-identity,deadbeef]"])
def test_recipient__invalid_string_fails() -> None:
with pytest.raises(SystemExit):
main(["-r", "not-a-valid-age-recipient", "enc", "foo"])
def test_encrypt__multiple_recipients(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
# Generate identities
identity1 = pyrage.x25519.Identity.generate()
identity1_path = tmp_path / "identity1"
_ = identity1_path.write_text(str(identity1))
identity2 = pyrage.x25519.Identity.generate()
identity2_path = tmp_path / "identity2"
_ = identity2_path.write_text(str(identity2))
# Run the CLI tool
main(
[
"-i",
str(identity1_path),
"--identity",
str(identity2_path),
"encrypt",
"foo",
]
)
# Ensure we get an identity secure value string
secure_value_string = capsys.readouterr().out
secure_value = parse_secure_value(secure_value_string)
assert isinstance(secure_value, IdentitySecureValue)
# Ensure we can decrypt it using all the recipient identities
for identity_path in (identity1_path, identity2_path):
assert secure_value.decrypt(read_identity_file(identity_path)) == "foo"
@pytest.mark.parametrize(
("environment", "args", "result"),
[
# Test decryption by using an identity file passed as CLI argument
(
None,
(
"-i",
"example/config/age.key",
"dec",
"ENC[age-identity,YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBkWHZYRkU2bjc4M2VtaElEZGxudmkwNW95ZHlNZy84K3U4MmlXejIzRkJNCktPbkhLU0h4VXBFYTZUUDlzbFFzdUx5R1VyaDZhd2doNkE2QnFpUmV6OFEKLS0tIFd3Wlg1UWQ3NHEwKyt6bTZkdmp3bWRCTTZkakppTFovbkhBcDhFeGdJazgKnf48DyGjBm2wOpM11YZ0+1btASDDSdgqXiM4SXXEMHhylmW8G9pSoTtovj0aZu9QVA==]",
),
"test-secret-value",
),
# Test decryption by using an identity file passed through environment
(
{"AGE_IDENTITY_FILE": "example/config/age.key"},
(
"dec",
"ENC[age-identity,YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBkWHZYRkU2bjc4M2VtaElEZGxudmkwNW95ZHlNZy84K3U4MmlXejIzRkJNCktPbkhLU0h4VXBFYTZUUDlzbFFzdUx5R1VyaDZhd2doNkE2QnFpUmV6OFEKLS0tIFd3Wlg1UWQ3NHEwKyt6bTZkdmp3bWRCTTZkakppTFovbkhBcDhFeGdJazgKnf48DyGjBm2wOpM11YZ0+1btASDDSdgqXiM4SXXEMHhylmW8G9pSoTtovj0aZu9QVA==]",
),
"test-secret-value",
),
# Test decryption by using an identity string passed through environment
(
{"AGE_IDENTITY": str(read_identity_file("example/config/age.key"))},
(
"dec",
"ENC[age-identity,YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBkWHZYRkU2bjc4M2VtaElEZGxudmkwNW95ZHlNZy84K3U4MmlXejIzRkJNCktPbkhLU0h4VXBFYTZUUDlzbFFzdUx5R1VyaDZhd2doNkE2QnFpUmV6OFEKLS0tIFd3Wlg1UWQ3NHEwKyt6bTZkdmp3bWRCTTZkakppTFovbkhBcDhFeGdJazgKnf48DyGjBm2wOpM11YZ0+1btASDDSdgqXiM4SXXEMHhylmW8G9pSoTtovj0aZu9QVA==]",
),
"test-secret-value",
),
# Test decryption using a passphrase passed through CLI argument
(
None,
(
"-P",
"secret-passphrase",
"dec",
"ENC[age-passphrase,YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB6SVZXRVlTQnFzdWh0VWp2YkFGV2pnIDE4CnNXUEZLdi81WjdmS0JSaFoxRG9md3E1RXMwMUhqVDd2d2lHUFhPUHFvT1EKLS0tIGlEKzg0Qjc4cWM3WEVwOStqZUJNQUc4SXJUSXA0QzNtM21vQUZmSit6ZncKf+ubsIChW5+VqkQMrnaMPbaf4jOHAVRQXU6xWrlGSmoWnq4GuqJzX79fluc4bvPwqQ==]",
),
"test-secret-value",
),
# Test decryption using a passphrase passed through environment
(
{"AGE_PASSPHRASE": "secret-passphrase"},
(
"decrypt",
"ENC[age-passphrase,YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB6SVZXRVlTQnFzdWh0VWp2YkFGV2pnIDE4CnNXUEZLdi81WjdmS0JSaFoxRG9md3E1RXMwMUhqVDd2d2lHUFhPUHFvT1EKLS0tIGlEKzg0Qjc4cWM3WEVwOStqZUJNQUc4SXJUSXA0QzNtM21vQUZmSit6ZncKf+ubsIChW5+VqkQMrnaMPbaf4jOHAVRQXU6xWrlGSmoWnq4GuqJzX79fluc4bvPwqQ==]",
),
"test-secret-value",
),
],
)
def test_decrypt(
environment: None | dict[str, str],
args: Sequence[str],
result: str,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Setup environment variables
for name, value in (environment or {}).items():
monkeypatch.setenv(name, value)
# Run the CLI tool
main(args)
# Ensure we get the expected result
assert capsys.readouterr().out == result