70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Guest-only synthetic client for the isolated clean-host installer check."""
|
|
|
|
import base64
|
|
import http.client
|
|
import json
|
|
import os
|
|
import secrets
|
|
import ssl
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
os.umask(0o077)
|
|
state = Path("/var/lib/ndc-mission-vps")
|
|
context = ssl.create_default_context(cafile=str(state / "tls.crt"))
|
|
context.check_hostname = False # This exact generated certificate is the only CA.
|
|
credentials = Path("/acceptance-client.json")
|
|
|
|
|
|
def request(method, path, body=None, token=None):
|
|
connection = http.client.HTTPSConnection("127.0.0.1", 19443, context=context, timeout=10)
|
|
headers = {"Content-Type": "application/json"}
|
|
if token:
|
|
headers["Authorization"] = "Bearer " + token
|
|
connection.request(method, path, json.dumps(body or {}), headers)
|
|
response = connection.getresponse()
|
|
value = json.loads(response.read())
|
|
connection.close()
|
|
assert response.status == 200, response.status
|
|
return value
|
|
|
|
|
|
if sys.argv[1] == "seed":
|
|
encoded = (state / "connect.key").read_text().strip().removeprefix("MCV1.")
|
|
invitation = json.loads(base64.urlsafe_b64decode(encoded + "=" * (-len(encoded) % 4)))
|
|
client = {
|
|
"core_id": "e" * 32,
|
|
"token": secrets.token_urlsafe(32),
|
|
"secret": invitation["secret"],
|
|
}
|
|
paired = request("POST", "/v1/pair", client)
|
|
client["host_id"] = paired["host_id"]
|
|
credentials.write_text(json.dumps(client))
|
|
request(
|
|
"PUT",
|
|
"/v1/devices",
|
|
{"items": [{"imei": "123456789012345", "instance_id": "a" * 32}]},
|
|
client["token"],
|
|
)
|
|
operation = request(
|
|
"POST",
|
|
"/v1/operations",
|
|
{
|
|
"id": "b" * 32,
|
|
"kind": "request-position",
|
|
"imei": "123456789012345",
|
|
"model": 231,
|
|
"firmware": "0.01",
|
|
},
|
|
client["token"],
|
|
)
|
|
assert operation["state"] == "queued" and operation["attempt_count"] == 0
|
|
else:
|
|
client = json.loads(credentials.read_text())
|
|
status = request("GET", "/v1/status", token=client["token"])
|
|
assert status["host_id"] == client["host_id"] and status["devices"] == 1
|
|
operation = request("GET", "/v1/operations/" + "b" * 32, token=client["token"])
|
|
assert operation["state"] == "queued" and operation["attempt_count"] == 0
|
|
print(json.dumps({"paired_identity": True, "device_registered": True, "queued_job_retained": True}))
|