#!/usr/bin/env python3 """Prepare an Ed25519 agent release on the signing host and publish through the catalog API.""" import argparse import base64 import fcntl import getpass import hashlib import http.client from http.cookies import SimpleCookie import json import os from pathlib import Path import re import ssl import subprocess import tempfile import time from urllib.parse import urlencode, urlsplit import uuid LINUX = 'x86_64-unknown-linux-gnu' WINDOWS = 'x86_64-pc-windows-msvc' FILES = {LINUX: ('rmm-agent', 'rmm-privileged-helper', 'rmm-agent-updater'), WINDOWS: ('rmm-agent-windows.exe', 'rmm-helper-windows.exe', 'rmm-updater-windows.exe')} WINDOWS_GNU = 'x86_64-pc-windows-gnu' FILES[WINDOWS_GNU] = FILES[WINDOWS] LIMIT = 256 * 1024 * 1024 PREFIX = b'rmm.agent-release.v1\0' def check(test, message): if not test: raise ValueError(message) def encoded(value): return json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode() def b64(data): return base64.urlsafe_b64encode(data).rstrip(b'=').decode() def fingerprint(path): digest = hashlib.sha256() size = path.stat().st_size check(path.is_file() and 0 < size <= LIMIT, 'invalid release binary size') with path.open('rb') as f: for block in iter(lambda: f.read(1024 * 1024), b''): digest.update(block) return digest.hexdigest(), size def verify_native_image(path, target, size): with path.open('rb') as binary: header = binary.read(64) check(len(header) == 64, 'truncated binary header: ' + path.name) if target == LINUX: check(header[:6] == b'\x7fELF\x02\x01' and header[16:18] in (b'\x02\x00', b'\x03\x00') and header[18:20] == b'\x3e\x00', 'Linux x86-64 ELF image required: ' + path.name) else: pe = int.from_bytes(header[60:64], 'little') check(header[:2] == b'MZ' and 64 <= pe <= size - 26, 'Windows PE image required: ' + path.name) binary.seek(pe) coff = binary.read(26) check(coff[:6] == b'PE\0\0\x64\x86' and coff[24:26] == b'\x0b\x02', 'Windows x86-64 PE32+ image required: ' + path.name) def binaries(folder, receipt, worker_only=False): record = json.loads(receipt.read_text(encoding='utf-8-sig')) if 'target' in record and 'sha256' in record: target, hashes = record['target'], record['sha256'] else: check(record.get('completed') is True and record['stage'] == 'built_not_deployed', 'completed MSVC receipt required') target = WINDOWS entries = record['artifacts'] names = FILES[target][2:] if worker_only else FILES[target] check(all(entries[name[:-4]]['build_target'] == target for name in names), 'incorrect build target') hashes = {name: entries[name[:-4]]['sha256'] for name in names} check(target in FILES, 'unknown release target') result = [] for name in FILES[target][2:] if worker_only else FILES[target]: path = folder / name digest, size = fingerprint(path) verify_native_image(path, target, size) check(digest == hashes[name], 'file differs from build receipt: ' + name) result.append({'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, target + '/' + name + '/' + digest)), 'sha256': digest, 'bytes': size}) return target, result def openssl(*args): return subprocess.check_output(['openssl', *map(str, args)], stderr=subprocess.DEVNULL, timeout=20) def save_once(path, raw, uid=None): if path.exists(): check(path.read_bytes() == raw, 'existing configuration differs: ' + path.name) return with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as f: try: f.write(raw) os.fchmod(f.fileno(), 0o600) if uid is not None: os.fchown(f.fileno(), uid, uid) f.flush() os.fsync(f.fileno()) os.link(f.name, path) finally: os.unlink(f.name) def provision_folder(path, uid=None): path.mkdir(mode=0o700, parents=True, exist_ok=True) check(path.is_dir() and not path.is_symlink(), 'release state directory required') if uid is not None and path.stat().st_uid != uid: os.chown(path, uid, uid) os.chmod(path, 0o700) def https(value): url = urlsplit(value) check(url.scheme == 'https' and url.hostname and not url.username and not url.password and url.path in ('', '/') and not url.query and not url.fragment, 'HTTPS origin required') return url def prepare(args): provision_folder(args.state) provision_folder(args.state/'private') with (args.state/'private'/'signing.lock').open('a') as lock: fcntl.flock(lock, fcntl.LOCK_EX) prepare_locked(args) def prepare_locked(args): https(args.gateway) check(0 < args.sequence < 2**63 and re.fullmatch('[A-Za-z0-9.+_-]{1,64}', args.version), 'invalid release version/sequence') minimum = getattr(args, 'minimum_updater_version', 1) check(minimum in (1,2), 'unknown minimum updater version') check(0 < args.days <= 3650, 'invalid validity in days') platform, blobs = binaries(args.artifacts, args.build_receipt) label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform] check(minimum == 1 or platform == WINDOWS_GNU, 'version 2 currently applies to the GNU Windows worker') provision_folder(args.state) config, private = args.state / 'config', args.state / 'private' provision_folder(config, args.service_uid) provision_folder(args.state / 'objects', args.service_uid) provision_folder(private) key = private / (label + '.pem') if not key.exists(): save_once(key, openssl('genpkey', '-algorithm', 'ED25519')) check(not key.is_symlink() and key.stat().st_mode & 0o077 == 0, 'private release key permissions') public = openssl('pkey', '-in', key, '-pubout', '-outform', 'DER') check(len(public) == 44 and public[:12] == bytes.fromhex('302a300506032b6570032100'), 'Ed25519 key required') pub = public[12:] ring = {'schema_version': 1, 'generation': 1, 'keys': [{'public_key_hex': pub.hex(), 'platform': platform, 'first_sequence': 1, 'last_sequence': 2**63-1, 'revoked': False}]} 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') # 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(): prior = json.loads((prepared / 'inputs.json').read_bytes()) check(prior.get('version') == args.version and prior.get('blobs') == blobs and prior.get('minimum_updater_version', 1) == minimum, 'sequence already bound') print(prepared / 'envelope.json') return now = int(time.time()) manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': args.sequence, 'version': args.version, 'platform': platform, 'published_at': now, 'expires_at': now + args.days * 86400, 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': minimum, 'artifacts': [dict(role=role, **blob) for role, blob in zip(('agent', 'helper'), blobs[:2])]} raw = encoded(manifest) with tempfile.TemporaryDirectory(dir=prepared.parent) as tmpdir: work = Path(tmpdir) msg = work / 'message' msg.write_bytes(PREFIX + raw) sig = openssl('pkeyutl', '-sign', '-rawin', '-inkey', key, '-in', msg) (work / 'public.der').write_bytes(public) (work / 'signature').write_bytes(sig) openssl('pkeyutl', '-verify', '-rawin', '-pubin', '-keyform', 'DER', '-inkey', work / 'public.der', '-in', msg, '-sigfile', work / 'signature') output = work / 'release' output.mkdir() env = {'schema_version': 1, 'key_id': hashlib.sha256(pub).hexdigest(), 'payload_bytes': b64(raw), 'signature': b64(sig)} save_once(output / 'envelope.json', encoded(env)) save_once(output / 'inputs.json', encoded({'version': args.version, 'blobs': blobs, 'minimum_updater_version': minimum})) output.rename(prepared) print(prepared / 'envelope.json') class Publisher: def __init__(self, address, ca): self.origin = address.rstrip('/') self.url = https(self.origin) self.ctx = ssl.create_default_context(cafile=ca) self.headers = {'Origin': self.origin} def request(self, method, route, body=None, headers=None): conn = http.client.HTTPSConnection(self.url.hostname, self.url.port or 443, context=self.ctx, timeout=180) try: conn.request(method, route, body=body, headers={**self.headers, **(headers or {})}) reply = conn.getresponse() data = reply.read(1048577) check(200 <= reply.status < 300, f'{method} {route.split("?")[0]} returned HTTP {reply.status}') check(len(data) <= 1048576, 'response exceeds limit') if reply.getheader('Set-Cookie'): cookie = SimpleCookie(reply.getheader('Set-Cookie')) token = cookie['__Host-rmm_session'].value self.headers['Cookie'] = '__Host-rmm_session=' + token return json.loads(data) if data else None finally: conn.close() def rotate_pin(state, label, tenant, expected, new_pin): tenant = str(uuid.UUID(tenant)) check(re.fullmatch('[0-9a-f]{64}', expected), 'expected predecessor SHA-256 required') directory = state/'config' default = directory/(label + '-bootstrap.json') path = default.with_suffix('.' + tenant + '.json') # The already completed upload attests the API read and stored the object. # Independently reopen the actual tenant object before changing the pin. sha, size = fingerprint(state/'objects'/tenant/new_pin['sha256']) check(new_pin == {'sha256':sha,'bytes':size}, 'uploaded stable updater differs') with (state/'private'/'bootstrap-rotation.lock').open('a') as lock: fcntl.flock(lock, fcntl.LOCK_EX) if path.exists(): pins = json.loads(path.read_bytes()) else: baseline = json.loads(default.read_bytes()) pins = {'current':{'sha256':baseline['updater_sha256'],'bytes':baseline['updater_bytes']},'previous':[]} now = int(time.time()) if pins['current'] == new_pin: prior = pins['previous'][-1]['pin']['sha256'] if pins['previous'] else None check(expected in (prior, new_pin['sha256']), 'bootstrap predecessor changed') return check(pins['current']['sha256'] == expected, 'bootstrap predecessor changed') previous = [item for item in pins['previous'] if item['expires_at'] > now and item['pin'] not in (new_pin,pins['current'])] previous.append({'pin':pins['current'],'expires_at':now+900}) raw = encoded({'current':new_pin,'previous':previous}) check(len(raw) <= 32768, 'too many concurrent bootstrap rotations') with tempfile.NamedTemporaryFile(dir=directory,delete=False) as f: try: f.write(raw) os.fchmod(f.fileno(),0o600) os.fchown(f.fileno(),default.stat().st_uid,default.stat().st_gid) f.flush() os.fsync(f.fileno()) os.replace(f.name,path) finally: Path(f.name).unlink(missing_ok=True) fd = os.open(directory,os.O_DIRECTORY) try: os.fsync(fd) finally: os.close(fd) def publish(args): worker_only = args.rotate_worker_only platform, blobs = binaries(args.artifacts, args.build_receipt, worker_only) if worker_only: check(args.envelope is None and args.rotate_bootstrap_from, 'worker rotation requires a predecessor and no release envelope') else: check(args.envelope is not None, 'a release envelope is required') env = json.loads(args.envelope.read_bytes()) manifest = json.loads(base64.urlsafe_b64decode(env['payload_bytes'] + '===')) check(manifest['platform'] == platform and manifest['artifacts'] == [dict(role=role, **blob) for role, blob in zip(('agent', 'helper'), blobs[:2])], 'input files differ from release envelope') check(bool(args.rotate_bootstrap_from) == bool(args.bootstrap_state), 'rotation needs previous SHA and local signing state') check(not (args.rotate_bootstrap_from and args.bootstrap_worker), 'a rotation uses the new receipt-bound stable worker') client = Publisher(args.origin, args.ca) if args.dev_login_file: path = args.dev_login_file check(path.stat().st_mode & 0o077 == 0, 'dev login file requires private permissions') credentials = json.loads(path.read_bytes()) login = {k: credentials[k] for k in ('username','password')} else: login = {'username': args.dev_username, 'password': getpass.getpass('Development WebUI password: ')} client.request('POST', '/auth/dev/login', encoded(login), {'Content-Type': 'application/json'}) session = client.request('GET', '/auth/session') client.headers['X-CSRF-Token'] = session['csrf_token'] try: 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 names = FILES[platform][2:] if worker_only else FILES[platform] paths = [args.artifacts / name for name in names] if args.bootstrap_worker: paths[2] = args.bootstrap_worker sha, size = fingerprint(paths[2]) verify_native_image(paths[2], platform, size) 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 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') if args.rotate_bootstrap_from: stable = blobs[-1] label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform] rotate_pin(args.bootstrap_state,label,tenant,args.rotate_bootstrap_from, {'sha256':stable['sha256'],'bytes':stable['bytes']}) if worker_only: print(json.dumps({'tenant_id':tenant,'platform':platform,'updater_sha256':stable['sha256'], 'updater_bytes':stable['bytes']})) return result = client.request('POST', path + '/agent-releases', encoded(env), {'Content-Type': 'application/json'}) check(result['release_envelope'] == env, 'published envelope changed') print(json.dumps({'release_id': result['release_id'], 'version': manifest['version'], 'platform': platform, 'manifest_sha256': result['manifest_sha256']})) finally: client.request('POST', '/auth/logout', b'{}', {'Content-Type':'application/json'}) def main(): parser = argparse.ArgumentParser(description=__doc__) sub = parser.add_subparsers(required=True) for name, fn in (('prepare', prepare), ('publish', publish)): cmd = sub.add_parser(name) cmd.set_defaults(run=fn) cmd.add_argument('--artifacts', type=Path, required=True) cmd.add_argument('--build-receipt', type=Path, required=True) if name == 'prepare': cmd.add_argument('--state', type=Path, required=True) cmd.add_argument('--gateway', required=True) cmd.add_argument('--sequence', type=int, required=True) cmd.add_argument('--version', required=True) cmd.add_argument('--days', type=int, default=90) cmd.add_argument('--minimum-updater-version',type=int,default=1) cmd.add_argument('--service-uid', type=int, default=os.getuid()) else: cmd.add_argument('--origin', required=True) cmd.add_argument('--bootstrap-state',type=Path,help='Local signing host state for an explicit stable updater rotation') cmd.add_argument('--rotate-bootstrap-from',help='Compare-and-swap the tenant pin from this SHA-256 after verified upload') 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, help='Prepared agent and helper release') cmd.add_argument('--rotate-worker-only', action='store_true', help='Rotate the stable worker independently of a code-pair release') auth = cmd.add_mutually_exclusive_group(required=True) auth.add_argument('--dev-username') auth.add_argument('--dev-login-file', type=Path) args = parser.parse_args() args.run(args) if __name__ == '__main__': main()