Add fresh device trust, installer recovery and scoped patch runtime
This commit is contained in:
+19
-5
@@ -2,7 +2,6 @@
|
||||
"""Prepare an Ed25519 agent release on the signing host and publish through the catalog API."""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime
|
||||
import getpass
|
||||
import hashlib
|
||||
import http.client
|
||||
@@ -11,7 +10,6 @@ import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import ssl
|
||||
import subprocess
|
||||
import tempfile
|
||||
@@ -132,11 +130,19 @@ def prepare(args):
|
||||
save_once(config / (label + '-keyring.json'), encoded(ring), args.service_uid)
|
||||
boot = {'gateway_origin': args.gateway.rstrip('/'), 'updater_sha256': blobs[2]['sha256'], 'updater_bytes': blobs[2]['bytes']}
|
||||
boot_path = config / (label + '-bootstrap.json')
|
||||
# The stable bootstrap worker is provisioned separately from replaceable code.
|
||||
# Keep the original verified worker available for publication into additional
|
||||
# tenants, even if a newer build also generated a different worker.
|
||||
stable = args.state / 'bootstrap-workers' / label
|
||||
provision_folder(stable.parent)
|
||||
if not boot_path.exists():
|
||||
save_once(boot_path, encoded(boot), args.service_uid)
|
||||
existing = json.loads(boot_path.read_bytes())
|
||||
check(existing['gateway_origin'] == boot['gateway_origin'], 'existing gateway origin differs')
|
||||
if not stable.exists() and boot == existing:
|
||||
save_once(stable, (args.artifacts / FILES[platform][2]).read_bytes())
|
||||
if stable.exists():
|
||||
check(fingerprint(stable) == (existing['updater_sha256'], existing['updater_bytes']),
|
||||
'stored bootstrap worker differs from protected configuration')
|
||||
prepared = args.state / 'prepared' / label / str(args.sequence)
|
||||
provision_folder(prepared.parent)
|
||||
if prepared.exists():
|
||||
@@ -212,9 +218,16 @@ def publish(args):
|
||||
tenant = str(uuid.UUID(args.tenant))
|
||||
check(session['tenant_id'] == tenant and 'catalog:write' in session['permissions'], 'tenant catalog write permission required')
|
||||
path = '/api/v1/tenants/' + tenant
|
||||
for name, blob in zip(FILES[platform], blobs):
|
||||
paths = [args.artifacts / name for name in FILES[platform]]
|
||||
if args.bootstrap_worker:
|
||||
paths[2] = args.bootstrap_worker
|
||||
sha, size = fingerprint(paths[2])
|
||||
name = FILES[platform][2]
|
||||
blobs[2] = {'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, platform + '/' + name + '/' + sha)),
|
||||
'sha256': sha, 'bytes': size}
|
||||
for file, blob in zip(paths, blobs):
|
||||
query = urlencode(dict(artifact_id=blob['artifact_id'], sha256=blob['sha256'], size_bytes=blob['bytes']))
|
||||
with (args.artifacts / name).open('rb') as data:
|
||||
with file.open('rb') as data:
|
||||
result = client.request('POST', path + '/agent-update-artifacts?' + query, data,
|
||||
{'Content-Type': 'application/octet-stream', 'Content-Length': str(blob['bytes'])})
|
||||
check(result['sha256'] == blob['sha256'] and result['size_bytes'] == blob['bytes'], 'artifact receipt differs')
|
||||
@@ -243,6 +256,7 @@ def main():
|
||||
cmd.add_argument('--service-uid', type=int, default=os.getuid())
|
||||
else:
|
||||
cmd.add_argument('--origin', required=True)
|
||||
cmd.add_argument('--bootstrap-worker', type=Path, help='Pinned initial stable updater when publishing subsequent code pairs')
|
||||
cmd.add_argument('--ca', type=Path)
|
||||
cmd.add_argument('--tenant', required=True)
|
||||
cmd.add_argument('--envelope', type=Path, required=True)
|
||||
|
||||
@@ -50,6 +50,33 @@ def on_server(options):
|
||||
inputs = ['--artifacts', str(build), '--build-receipt', str(build / options['receipt'])]
|
||||
subprocess.run(['python3', '-I', str(tool), 'prepare', *inputs, '--state', str(state), '--gateway', options['gateway'],
|
||||
'--service-uid','10001','--sequence',str(options['sequence']),'--version',options['version']],check=True)
|
||||
if options['label'].startswith('windows'):
|
||||
import re
|
||||
import uuid
|
||||
public = Path('/etc/rmm-time-source/time-public-key.json')
|
||||
if public.exists():
|
||||
contents = json.loads(public.read_bytes())
|
||||
if (set(contents) != {'schema_version','algorithm','server_instance','key_id','key_generation','public_key_hex'}
|
||||
or contents['algorithm'] != 'Ed25519' or contents['schema_version'] != 1 or uuid.UUID(contents['server_instance']).int == 0
|
||||
or not re.fullmatch('[A-Za-z0-9_-]{1,64}', contents['key_id']) or not re.fullmatch('[0-9a-f]{64}', contents['public_key_hex'])
|
||||
or type(contents['key_generation']) is not int or contents['key_generation'] <= 0):
|
||||
raise ValueError('time broker public key information invalid')
|
||||
expected = {'server_instance':contents['server_instance'],'key_generation':contents['key_generation'],
|
||||
'public_keys_hex':{contents['key_id']:contents['public_key_hex']}}
|
||||
path = state/'config/windows-time-trust.json'
|
||||
if not path.exists():
|
||||
with tempfile.NamedTemporaryFile(mode='w', dir=path.parent,delete=False) as tmp:
|
||||
json.dump(expected,tmp,separators=(',',':'))
|
||||
os.fchmod(tmp.fileno(),0o600)
|
||||
os.fchown(tmp.fileno(),10001,10001)
|
||||
tmp.flush()
|
||||
os.fsync(tmp.fileno())
|
||||
try:
|
||||
os.link(tmp.name,path)
|
||||
finally:
|
||||
os.unlink(tmp.name)
|
||||
if json.loads(path.read_bytes()) != expected:
|
||||
raise ValueError('time trust changed; verify rotation before new publication')
|
||||
if options['prepare_only']:
|
||||
return
|
||||
# Use the ordinary development catalog permission and CSRF-protected API.
|
||||
@@ -67,7 +94,7 @@ def on_server(options):
|
||||
subprocess.run(['python3','-I',str(tool),'publish',*inputs,'--tenant',options['tenant'],
|
||||
'--envelope',str(state/'prepared'/options['label']/str(options['sequence'])/'envelope.json'),
|
||||
'--origin','https://localhost:18443','--ca','/opt/rust-discovery/devstate/api-pki/ca.pem',
|
||||
'--dev-login-file',auth.name],check=True)
|
||||
'--dev-login-file',auth.name,'--bootstrap-worker',str(state/'bootstrap-workers'/options['label'])],check=True)
|
||||
|
||||
def main():
|
||||
import inspect
|
||||
|
||||
@@ -95,10 +95,11 @@ def activate(payload):
|
||||
run('docker','build','--network=none','--pull=false','-t',tag,str(context))
|
||||
overlay=role_dir/'image.compose.yaml'
|
||||
write_atomic(overlay,('services:\n'+''.join(f' {r}:\n image: {tag}\n pull_policy: never\n' for r in ('migrate','api','gateway'))).encode())
|
||||
for path in payload['overlays']:
|
||||
file=(root/'deploy')/path
|
||||
if not re.fullmatch(r'[\w.-]+\.compose\.yaml',path) or not file.is_file():
|
||||
raise ValueError('Dev compose overlay must exist in the deployment directory')
|
||||
for path, content in payload['overlays'].items():
|
||||
file = (root / 'deploy') / path
|
||||
if not re.fullmatch(r'server\.[\w.-]+\.compose\.yaml', path) or file.is_symlink():
|
||||
raise ValueError('Invalid development overlay name')
|
||||
write_atomic(file, base64.b64decode(content, validate=True))
|
||||
if file not in base_files:
|
||||
compose.extend(['-f',str(file)])
|
||||
compose.extend(['-f',str(overlay)])
|
||||
@@ -150,7 +151,7 @@ def main():
|
||||
import shlex
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--ref',required=True)
|
||||
parser.add_argument('--overlay',action='append',default=[],help='Additional existing compose file in remote deploy/')
|
||||
parser.add_argument('--overlay',action='append',default=[],help='Deploy and use this compose overlay from the selected Git revision')
|
||||
args=parser.parse_args()
|
||||
revision=subprocess.check_output(['git','rev-parse','--verify','--end-of-options',args.ref+'^{commit}'],text=True).strip()
|
||||
web=web_files(revision)
|
||||
@@ -158,7 +159,13 @@ def main():
|
||||
names=subprocess.check_output(['git','ls-tree','--name-only','-r',revision,'migrations'],text=True).splitlines()
|
||||
migrations={str(int(Path(n).name.split('_',1)[0])):hashlib.sha384(subprocess.check_output(['git','show',revision+':'+n])).hexdigest()
|
||||
for n in names if n.endswith('.sql')}
|
||||
payload=json.dumps(dict(revision=revision,web=web,overlays=args.overlay,migrations=migrations))
|
||||
overlays = {}
|
||||
for name in args.overlay:
|
||||
if not re.fullmatch(r'server\.[\w.-]+\.compose\.yaml', name):
|
||||
parser.error('Expected a server overlay filename from deploy/')
|
||||
raw = subprocess.check_output(['git', 'show', revision + ':deploy/' + name])
|
||||
overlays[name] = base64.b64encode(raw).decode()
|
||||
payload=json.dumps(dict(revision=revision,web=web,overlays=overlays,migrations=migrations))
|
||||
cmd=source+'\nactivate(json.loads('+repr(payload)+'))\n'
|
||||
subprocess.run(ssh_prefix('server')+[shlex.join(['python3','-I','-'])],input=cmd,text=True,check=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user