generated from microsoft/ccf-app-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_common.py
588 lines (520 loc) · 17.7 KB
/
test_common.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
Common utils for testing.
"""
import base64
import hashlib
import http
import os
import time
from http import HTTPStatus
from subprocess import PIPE, Popen
from typing import List, Dict, Any
import ccf.receipt # type: ignore
# pylint: disable=import-error
import etcd_pb2 # type: ignore
import httpx
# pylint: disable=import-error
import lskvserver_pb2 # type: ignore
import pytest
import typing_extensions
from cryptography.x509 import load_pem_x509_certificate # type: ignore
from google.protobuf.json_format import MessageToDict, ParseDict
from loguru import logger
# pylint: disable=import-error
from lskv import governance # type: ignore
class Sandbox:
"""
Run the sandbox with lskv loaded.
"""
def __init__(self, http2: bool):
self.nodes = 1
self.port = 8000
self.http_version = 2 if http2 else 1
self.proc = None
def __enter__(self):
self.proc = self.spawn()
def __exit__(
self, ex_type, ex_value, ex_traceback
) -> typing_extensions.Literal[False]:
if self.proc:
logger.info("terminating store process")
self.proc.terminate()
# give it a second to shutdown
time.sleep(1)
if not self.proc.poll():
# process is still running, kill it
logger.info("killing store process")
self.proc.kill()
self.proc.wait()
logger.info("stopped")
return False
def wait_for_ready(self) -> bool:
"""
Wait for the datastore to be ready to accept requests.
"""
return self._wait_for_ready(self.port)
# pylint: disable=duplicate-code
def _wait_for_ready(self, port: int, tries=60) -> bool:
client = self.etcdctl_client()
client += ["get", "missing key"]
if self.http_version == 1:
client = [
"curl",
"--silent",
"--cacert",
self.cacert(),
"--cert",
self.cert(),
"--key",
self.key(),
"-X",
"POST",
f"https://127.0.0.1:{self.port}/v3/kv/range",
"-d",
'{"key":"bWlzc2luZyBrZXkK","serializable":true}',
"-H",
"Content-Type: application/json",
]
for i in range(0, tries):
logger.debug("running ready check with cmd {}", client)
# pylint: disable=consider-using-with
proc = Popen(client, stdout=PIPE)
if proc.wait() == 0:
out = proc.stdout
if out:
outlines = out.readlines()
if "revision" in "\n".join(map(str, outlines)):
logger.info(
"finished waiting for port ({}) to be open, try {}", port, i
)
return True
logger.warning("output didn't match: {}", outlines)
logger.debug("waiting for port ({}) to be open, try {}", port, i)
time.sleep(1)
logger.error("took too long waiting for port {} ({}s)", port, tries)
return False
def spawn(self) -> Popen:
"""
Spawn a new sandbox instance.
"""
with open(
os.path.join(self.output_dir(), "node.out"), "w", encoding="utf-8"
) as out:
with open(
os.path.join(self.output_dir(), "node.err"),
"w",
encoding="utf-8",
) as err:
libargs = ["build/liblskv.virtual.so", "-e", "virtual", "-t", "virtual"]
env = os.environ.copy()
env["VENV_DIR"] = os.path.join(os.getcwd(), ".venv")
nodes = []
for i in range(self.nodes):
nodes += ["--node", f"local://127.0.0.1:{self.port+i}"]
kvs_cmd = (
["/opt/ccf_virtual/bin/sandbox.sh", "-p"]
+ libargs
+ [
"--workspace",
self.workspace(),
"--verbose",
]
+ nodes
)
if self.http_version == 2:
kvs_cmd += ["--http2"]
logger.info("spawning lskv: {}", kvs_cmd)
return Popen(kvs_cmd, stdout=out, stderr=err, env=env)
def output_dir(self) -> str:
"""
Return the output directory for this sandbox run.
"""
return "tests"
def workspace(self):
"""
Return the workspace directory for this store.
"""
return os.path.join(os.getcwd(), self.output_dir(), "workspace")
def cacert(self) -> str:
"""
Return the path to the CA certificate.
"""
return f"{self.workspace()}/sandbox_common/service_cert.pem"
def cert(self) -> str:
"""
Return the path to the client certificate.
"""
return f"{self.workspace()}/sandbox_common/user0_cert.pem"
def key(self) -> str:
"""
Return the path to the key for the client certificate.
"""
return f"{self.workspace()}/sandbox_common/user0_privk.pem"
def member0_cert(self) -> str:
"""
Return the path to the CA certificate.
"""
return f"{self.workspace()}/sandbox_common/member0_cert.pem"
def member0_key(self) -> str:
"""
Return the path to the CA certificate.
"""
return f"{self.workspace()}/sandbox_common/member0_privk.pem"
def etcdctl_client(self) -> List[str]:
"""
Get the etcdctl client command for this datastore.
"""
return [
"bin/etcdctl",
"--endpoints",
f"https://127.0.0.1:{self.port}",
"--cacert",
self.cacert(),
"--cert",
self.cert(),
"--key",
self.key(),
]
@pytest.fixture(name="sandbox", scope="module")
def fixture_sandbox():
"""
Start the sandbox and wait to be ready.
"""
sandbox = Sandbox(http2=False)
with sandbox:
ready = sandbox.wait_for_ready()
if ready:
# setup new constitution
# this is needed since the ccf sandbox doesn't take a set of constitution files yet
gov_client = governance.Client(
"127.0.0.1:8000",
sandbox.cacert(),
sandbox.member0_key(),
sandbox.member0_cert(),
)
proposal = governance.Proposal()
# pylint: disable=duplicate-code
proposal.set_constitution(
[
"constitution/actions.js",
"constitution/apply.js",
"constitution/resolve.js",
"constitution/validate.js",
]
)
res = gov_client.propose(proposal)
if res.state != "Accepted":
gov_client.accept(res.proposal_id)
yield sandbox
else:
raise RuntimeError("failed to prepare the sandbox")
@pytest.fixture(name="http1_client", scope="module")
def fixture_http1_client(sandbox):
"""
Make a http1 client for the sandbox.
"""
cacert = sandbox.cacert()
client_cert = (sandbox.cert(), sandbox.key())
with httpx.Client(
http2=False, verify=cacert, cert=client_cert, base_url="https://127.0.0.1:8000"
) as client:
yield HttpClient(client)
@pytest.fixture(name="http1_client_unauthenticated", scope="module")
def fixture_http1_client_unauthenticated(sandbox):
"""
Make an unauthenticated http1 client for the sandbox.
"""
cacert = sandbox.cacert()
with httpx.Client(
http2=False, verify=cacert, base_url="https://127.0.0.1:8000"
) as client:
yield HttpClient(client)
@pytest.fixture(name="governance_client", scope="module")
def fixture_governance_client(sandbox):
"""
Make a governance client for the sandbox.
"""
return governance.Client(
"127.0.0.1:8000",
sandbox.cacert(),
sandbox.member0_key(),
sandbox.member0_cert(),
)
def b64encode(in_str: str) -> str:
"""
Base64 encode a string.
"""
return base64.b64encode(in_str.encode("utf-8")).decode("utf-8")
def b64decode(in_str: str) -> str:
"""
Base64 decode a string.
"""
return base64.b64decode(in_str).decode("utf-8")
class HttpClient:
"""
A raw http client for communicating with lskv over json.
"""
def __init__(self, client: httpx.Client):
"""
Create a new http client.
"""
self.client = client
def wait_for_commit(self, term: int, rev: int, tries=100) -> bool:
"""
Wait for a commit to be successful.
"""
i = 0
while True:
i += 1
tx_status = self.tx_status(term, rev)
logger.debug("tx_status: {}", tx_status)
if tx_status.status_code == HTTPStatus.OK:
body = tx_status.json()
if "status" in body:
status = body["status"]
logger.debug("tx_status.status: {}", status)
if status == "Unknown":
pass
elif status == "Pending":
pass
elif status == "Committed":
return True
elif status == "Invalid":
return False
if i > tries:
raise RuntimeError("failed to wait for commit")
time.sleep(0.1)
# pylint: disable=too-many-arguments
def get(
self, key: str, range_end: str = "", rev: int = 0, limit: int = 0, check=True
):
"""
Perform a get operation on lskv.
"""
logger.info("Get: {} {} {} {}", key, range_end, rev, limit)
req = etcd_pb2.RangeRequest()
req.key = key.encode("utf-8")
req.serializable = True
if range_end:
req.range_end = range_end.encode("utf-8")
if rev:
req.revision = rev
if limit:
req.limit = limit
j = MessageToDict(req)
res = self.client.post("/v3/kv/range", json=j)
if check:
check_response(res)
return res
# pylint: disable=too-many-arguments
def put(
self,
key: str,
value: str,
lease_id: int = 0,
wait_for_commit: bool = True,
check_receipt=True,
check=True,
):
"""
Perform a put operation on lskv.
"""
logger.info("Put: {} {}", key, value)
req = etcd_pb2.PutRequest()
req.key = key.encode("utf-8")
req.value = value.encode("utf-8")
if lease_id:
req.lease = lease_id
j = MessageToDict(req)
res = self.client.post("/v3/kv/put", json=j)
if check:
check_response(res)
if wait_for_commit:
rev, term = extract_rev_term(res)
self.wait_for_commit(term, rev)
if check_receipt:
res_pb = ParseDict(res.json(), etcd_pb2.PutResponse())
self.check_receipt("put", req, res_pb)
return res
# pylint: disable=too-many-arguments
def delete(
self,
key: str,
range_end: str = "",
wait_for_commit: bool = True,
check_receipt=True,
check=True,
):
"""
Perform a delete operation on lskv.
"""
logger.info("Delete: {} {}", key, range_end)
req = etcd_pb2.DeleteRangeRequest()
req.key = key.encode("utf-8")
if range_end:
req.range_end = range_end.encode("utf-8")
j = MessageToDict(req)
res = self.client.post("/v3/kv/delete_range", json=j)
if check:
check_response(res)
if wait_for_commit:
rev, term = extract_rev_term(res)
self.wait_for_commit(term, rev)
if check_receipt:
res_pb = ParseDict(res.json(), etcd_pb2.DeleteRangeResponse())
self.check_receipt("delete_range", req, res_pb)
return res
def get_receipt(self, rev: int, term: int):
"""
Get a receipt for a revision and term.
"""
logger.info("GetReceipt: {} {}", rev, term)
req = lskvserver_pb2.GetReceiptRequest()
req.revision = rev
req.raft_term = term
j = MessageToDict(req)
res = self.client.post(
"/v3/receipt/get_receipt",
json=j,
)
if res.status_code == http.HTTPStatus.ACCEPTED:
logger.info("GetReceipt: ACCEPTED")
# accepted, retry
res = self.client.post(
"/v3/receipt/get_receipt",
json=j,
)
check_response(res)
proto = ParseDict(res.json(), lskvserver_pb2.GetReceiptResponse())
return (res, proto)
def compact(self, rev: int, check=True):
"""
Compact the KV store at the given revision
"""
logger.info("Compact: {}", rev)
j = {"revision": rev}
res = self.client.post("/v3/kv/compact", json=j)
if check:
check_response(res)
return res
def lease_grant(self, ttl: int = 60):
"""
Perform a lease grant operation.
"""
logger.info("LeaseGrant: {}", ttl)
j = {"TTL": ttl}
res = self.client.post("/v3/lease/grant", json=j)
check_response(res)
proto = ParseDict(res.json(), etcd_pb2.LeaseGrantResponse())
return (res, proto)
def lease_revoke(self, lease_id: str):
"""
Perform a lease revoke operation.
"""
logger.info("LeaseRevoke: {}", lease_id)
j = {"ID": lease_id}
res = self.client.post("/v3/lease/revoke", json=j)
check_response(res)
proto = ParseDict(res.json(), etcd_pb2.LeaseRevokeResponse())
return (res, proto)
def lease_keep_alive(self, lease_id: str, check=True):
"""
Perform a lease keep_alive operation.
"""
logger.info("LeaseKeepAlive: {}", lease_id)
j = {"ID": lease_id}
res = self.client.post("/v3/lease/keepalive", json=j)
proto = None
if check:
check_response(res)
proto = ParseDict(res.json(), etcd_pb2.LeaseKeepAliveResponse())
return (res, proto)
def tx_status(self, term: int, rev: int, check=True):
"""
Check the status of a transaction.
"""
logger.info("TxStatus: {} {}", term, rev)
j: Dict[str, Any] = {"raftTerm": term, "revision": rev}
res = self.client.post("/v3/maintenance/tx_status", json=j)
if check:
check_response(res)
return res
def status(self):
"""
Get the status of LSKV.
"""
logger.info("Status")
res = self.client.post("/v3/maintenance/status", json={})
check_response(res)
return res
def raw(self) -> httpx.Client:
"""
Get the raw client.
"""
return self.client
# pylint: disable=too-many-locals
def check_receipt(self, req_type: str, request, response):
"""
Check a receipt for a request and response.
"""
rev, term = extract_rev_term_pb(response)
res, proto = self.get_receipt(rev, term)
receipt = proto.receipt
tx_receipt = receipt.tx_receipt
leaf_components = tx_receipt.leaf_components
claims_digest = leaf_components.claims_digest
write_set_digest = leaf_components.write_set_digest
commit_evidence = leaf_components.commit_evidence
response.ClearField("header")
commit_evidence_digest = hashlib.sha256(commit_evidence.encode()).digest()
leaf_parts = [
bytes.fromhex(write_set_digest),
commit_evidence_digest,
bytes.fromhex(claims_digest),
]
leaf = hashlib.sha256(b"".join(leaf_parts)).hexdigest()
signature = receipt.signature
cert = receipt.cert
node_cert = load_pem_x509_certificate(cert.encode())
root = ccf.receipt.root(leaf, res.json()["receipt"]["txReceipt"]["proof"])
signature = base64.b64encode(signature).decode()
logger.debug("verifying receipt signature:{}", signature)
ccf.receipt.verify(root, signature, node_cert)
# receipt is valid, check if it matches our claims too
claims = lskvserver_pb2.ReceiptClaims()
getattr(claims, f"request_{req_type}").CopyFrom(request)
getattr(claims, f"response_{req_type}").CopyFrom(response)
claims_ser = claims.SerializeToString()
claims_digest_calculated = hashlib.sha256(claims_ser).hexdigest()
assert claims_digest == claims_digest_calculated
def check_response(res):
"""
Check a response to be success.
"""
logger.info("res: {} {}", res.status_code, res.text)
assert res.status_code == 200
check_header(res.json())
def check_header(body):
"""
Check the header is well-formed.
"""
assert "header" in body
header = body["header"]
assert "clusterId" in header
assert "memberId" in header
assert "revision" in header
assert "raftTerm" in header
def extract_rev_term(res):
"""
Extract the revision and term from a response.
"""
header = res.json()["header"]
return int(header["revision"]), int(header["raftTerm"])
def extract_rev_term_pb(res):
"""
Extract the revision and term from a pb response.
"""
header = res.header
return header.revision, header.raft_term