-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_cli.py
397 lines (344 loc) · 12.8 KB
/
test_cli.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import base64
import json
import shlex
from pathlib import Path
import pycose.headers
import pytest
from cryptography.x509 import ExtendedKeyUsage, load_der_x509_certificate
from loguru import logger as LOG
from pycose.messages import CoseMessage
from pyscitt import crypto
from pyscitt.cli.governance import (
SCITT_CONSTITUTION_MARKER_END,
SCITT_CONSTITUTION_MARKER_START,
)
from pyscitt.cli.main import main
from pyscitt.governance import ProposalNotAccepted
from .infra.assertions import service_error
from .infra.generate_cacert import generate_ca_cert_and_key
@pytest.fixture
def run(request):
def f(*cmd, with_service_url=False, with_member_auth=False):
args = [str(c) for c in cmd]
# We necessary, we insert extra flags that depend on the service we are running against.
# We request the fixtures dynamically by using `getfixturevalue` rather than as
# dependencies of the `run` fixture, to avoid needlessly start a cchost process
# if only running "offline" tests.
if with_service_url:
url = request.getfixturevalue("service_url")
args.extend(["--url", url, "--development"])
if with_member_auth:
paths = request.getfixturevalue("member_auth_path")
args.extend(["--member-cert", str(paths[0])])
args.extend(["--member-key", str(paths[1])])
LOG.info(shlex.join(["scitt"] + args))
main(args)
return f
def test_local_development(run, service_url, tmp_path: Path):
# Note that unlike other commands, we don't need to set the --development flag, since that is
# the default.
run(
"governance",
"local_development",
"--service-trust-store",
tmp_path / "trust_store",
"--url",
service_url,
with_member_auth=True,
)
def test_adhoc_signer(run, tmp_path: Path):
generate_ca_cert_and_key(
output_dir=str(tmp_path),
key_filename="cacert_privk.pem",
cacert_filename="cacert.pem",
alg="ES256",
ec_curve="P-256",
key_type="ec",
eku="1.2.3",
)
(tmp_path / "statement.json").write_text(json.dumps({"foo": "bar"}))
run(
"sign",
"--uses-cwt",
"--key",
tmp_path / "cacert_privk.pem",
"--x5c",
tmp_path / "cacert.pem",
"--issuer",
"foo.bar.baz",
"--feed",
"https://example.com/feed",
"--statement",
tmp_path / "statement.json",
"--content-type",
"application/json",
"--out",
tmp_path / "signed_statement.cose",
)
data = (tmp_path / "signed_statement.cose").read_bytes()
msg = CoseMessage.decode(data)
x5c = msg.get_attr(pycose.headers.X5chain)
assert isinstance(x5c, list)
assert len(x5c) == 2
signing_cert = load_der_x509_certificate(x5c[0])
eku_extension = signing_cert.extensions.get_extension_for_class(ExtendedKeyUsage)
ekus = eku_extension.value
assert len(ekus) == 1
eku = ekus[0]
assert eku.dotted_string == "1.2.3"
content_type = msg.get_attr(pycose.headers.ContentType)
assert content_type == "application/json"
cwt = msg.get_attr(crypto.CWTClaims)
assert cwt == {
crypto.CWT_ISS: "foo.bar.baz",
crypto.CWT_SUB: "https://example.com/feed",
}
def test_registration_info(run, tmp_path: Path):
generate_ca_cert_and_key(
output_dir=str(tmp_path),
key_filename="cacert_privk.pem",
cacert_filename="cacert.pem",
alg="ES256",
ec_curve="P-256",
key_type="ec",
)
(tmp_path / "statement.json").write_text(json.dumps({"foo": "bar"}))
binary_data = b"\xde\xad\xbe\xef"
binary_path = tmp_path / "binary.txt"
binary_path.write_bytes(binary_data)
# This is a Grinning Face emoji
unicode_data = "\U0001F600"
unicode_path = tmp_path / "unicode.txt"
unicode_path.write_text(unicode_data, encoding="utf-8")
run(
"sign",
"--registration-info=int:foo=42",
"--registration-info=int:negative=-42",
"--registration-info=text:bar=hello",
"--registration-info=with_default_type=world",
f"--registration-info=bytes:binary_data=@{binary_path}",
f"--registration-info=text:unicode_data=@{unicode_path}",
"--key",
tmp_path / "cacert_privk.pem",
"--x5c",
tmp_path / "cacert.pem",
"--content-type",
"application/json",
"--statement",
tmp_path / "statement.json",
"--out",
tmp_path / "signed_statement.cose",
)
data = (tmp_path / "signed_statement.cose").read_bytes()
msg = CoseMessage.decode(data)
info = msg.get_attr(crypto.SCITTRegistrationInfo)
assert info == {
"foo": 42,
"negative": -42,
"bar": "hello",
"with_default_type": "world",
"binary_data": binary_data,
"unicode_data": unicode_data,
}
def test_extract_payload_from_cose(run, tmp_path: Path):
generate_ca_cert_and_key(
output_dir=str(tmp_path),
key_filename="cacert_privk.pem",
cacert_filename="cacert.pem",
alg="ES256",
ec_curve="P-256",
key_type="ec",
)
(tmp_path / "statement.json").write_text(json.dumps({"foo": "bar"}))
run(
"sign",
"--key",
tmp_path / "cacert_privk.pem",
"--x5c",
tmp_path / "cacert.pem",
"--content-type",
"application/json",
"--statement",
tmp_path / "statement.json",
"--out",
tmp_path / "signed_statement.cose",
)
run(
"split-payload",
tmp_path / "signed_statement.cose",
"--out",
tmp_path / "payload.json",
)
data = (tmp_path / "payload.json").read_bytes()
claims = json.loads(data)
assert claims.get("foo") == "bar"
@pytest.mark.isolated_test
class TestUpdateScittConstitution:
# These tests run in an isolated cchost process. This ensures that if
# something goes wrong with the test and leaves the service with an invalid
# constitution, the other tests can carry on unaffected.
#
# The isolated process is shared by the entire class though, so one broken
# test could still affect other tests of this class.
@pytest.fixture(autouse=True)
def original_constitution(self, run, tmp_path_factory):
"""
Save the original constitution and restore it after the test has run.
The fixture provides the core constitution's contents, that is with the
SCITT amendments stripped.
It is marked as autouse, making the save/restore automatic for all tests
in this class.
"""
tmp = tmp_path_factory.mktemp("original_constitution")
path = tmp / "constitution.js"
run(
"governance",
"constitution",
"--output",
path,
with_service_url=True,
)
try:
parts = path.read_text().split(SCITT_CONSTITUTION_MARKER_START)
core_constitution = parts[0]
# Propose a new constitution, truncating anything after the marker.
# This provides a consistent starting point for all the tests.
core_path = tmp / "core_constitution.js"
core_path.write_text(core_constitution)
run(
"governance",
"propose_constitution",
"--constitution-file",
core_path,
with_service_url=True,
with_member_auth=True,
)
yield core_constitution
finally:
# Whatever happens in the test, do our best to restore it.
run(
"governance",
"propose_constitution",
"--constitution-file",
path,
with_service_url=True,
with_member_auth=True,
)
@pytest.fixture
def update_scitt_constitution(self, run, tmp_path):
def f(script, include_markers=True, yes=True):
path = tmp_path / "scitt.js"
if include_markers:
path.write_text(
SCITT_CONSTITUTION_MARKER_START
+ script
+ SCITT_CONSTITUTION_MARKER_END
)
else:
path.write_text(script)
run(
"governance",
"update_scitt_constitution",
"--scitt-constitution-file",
path,
*(["--yes"] if yes else []),
with_service_url=True,
with_member_auth=True,
)
return f
def test_update_scitt_constitution(self, run, tmp_path, update_scitt_constitution):
proposal = tmp_path / "proposal.json"
proposal.write_text(json.dumps({"actions": [{"name": "my_action"}]}))
# The my_action action does not exist yet.
with service_error("my_action: no such action"):
run(
"governance",
"propose_generic",
"--proposal-path",
proposal,
with_service_url=True,
with_member_auth=True,
)
update_scitt_constitution(
'actions.set("my_action", new Action(function(args) { }, function(args) { }))'
)
run(
"governance",
"propose_generic",
"--proposal-path",
proposal,
with_service_url=True,
with_member_auth=True,
)
update_scitt_constitution(
'actions.set("another_action", new Action(function(args) { }, function(args) { }))'
)
# After updating the constitution again, the my_action action will
# fail to run, showing that we actually modified the SCITT constitution,
# and didn't just append to it.
with service_error("my_action: no such action"):
run(
"governance",
"propose_generic",
"--proposal-path",
proposal,
with_service_url=True,
with_member_auth=True,
)
def test_invalid_constitution(self, update_scitt_constitution):
with pytest.raises(RuntimeError, match="does not start with marker"):
update_scitt_constitution("", include_markers=False)
with pytest.raises(RuntimeError, match="does not end with marker"):
update_scitt_constitution(
SCITT_CONSTITUTION_MARKER_START, include_markers=False
)
with pytest.raises(RuntimeError, match="does not end with marker"):
update_scitt_constitution(
SCITT_CONSTITUTION_MARKER_START
+ SCITT_CONSTITUTION_MARKER_END
+ "// Trailing",
include_markers=False,
)
def test_trailing_text(
self, run, original_constitution, update_scitt_constitution, tmp_path
):
# Write a constitution with the right markers, but with some text after it.
# We can't use update_scitt_constitution for this because of its safety rails so
# we use the more low-level propose_constitution.
tmp_path.joinpath("constitution.js").write_text(
original_constitution
+ SCITT_CONSTITUTION_MARKER_START
+ SCITT_CONSTITUTION_MARKER_END
+ "// Trailing stuff"
)
run(
"governance",
"propose_constitution",
"--constitution-file",
tmp_path / "constitution.js",
with_service_url=True,
with_member_auth=True,
)
# Allowing this would be bad, as it would risk dropping the trailing text.
with pytest.raises(
RuntimeError,
match="Existing constitution does not end with the right marker",
):
update_scitt_constitution("")
def test_race_condition(self, update_scitt_constitution, monkeypatch):
# We want to make two concurrent modifications to the constitution, and
# make sure update_scitt_constitution detects this.
# The way we do this is by running update_scitt_constitution in interactive mode (yes=False),
# but monkeypatch the input() function. The input will always return yes
# to proceed, but before doing so it makes its own change to the constitution
def confirm(*args):
try:
update_scitt_constitution("// Concurrent change")
except ProposalNotAccepted:
raise RuntimeError("concurrent change not accepted")
return "yes"
monkeypatch.setattr("builtins.input", confirm)
with pytest.raises(ProposalNotAccepted):
update_scitt_constitution("// Main change", yes=False)