83 lines
7.2 KiB
Python
83 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare public updater metadata using the sealed Rust codecs; never signs.
|
|
|
|
Every input is public evidence. A locally prepared document is neither a native
|
|
qualification nor trusted until the existing native verifier accepts it.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import datetime
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import uuid
|
|
import linux_updater_compatible_apply as bootstrap
|
|
|
|
SDK = bootstrap.ROOT / 'devstate/linux-updater-bootstrap-sdk/d309c949-3a70-4448-88da-49c710093221/sdk.bin'
|
|
SDK_SHA = '6fcc543d538fca30ceb499364880e4dc3689700d50597f18bd11bfe3559a0021'
|
|
|
|
def require(condition):
|
|
if not condition: raise RuntimeError('Public updater preparation contract failed')
|
|
def digest(raw): return hashlib.sha256(raw).hexdigest()
|
|
def encode(value): return json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode()
|
|
def b64(raw): return base64.urlsafe_b64encode(raw).rstrip(b'=').decode()
|
|
def unb64(value):
|
|
raw = base64.urlsafe_b64decode(value + '=' * (-len(value) % 4))
|
|
require(b64(raw) == value)
|
|
return raw
|
|
def typed(value):
|
|
require(digest(SDK.read_bytes()) == SDK_SHA)
|
|
result = subprocess.run(['docker', 'run', '--rm', '-i', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=64m', '-v', str(SDK)+':/sdk:ro', '--entrypoint', '/sdk', 'sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'], input=encode(value), capture_output=True, check=True, timeout=30)
|
|
return json.loads(result.stdout)
|
|
def prepare(action, data):
|
|
signer = data['signer']
|
|
require(signer['platform'] == 'linux' and signer['profile'] == 'agent_updater_separate_signers_v1')
|
|
common = {'case_id': signer['case_id'], 'platform': 'linux', 'job_authority': False}
|
|
if action == 'release-input':
|
|
build = json.loads(bootstrap.BASE.read_text())
|
|
require(digest(bootstrap.BASE.read_bytes()) == bootstrap.BASE_SHA)
|
|
seq = data['sequence']; require(seq in (1, 2))
|
|
published = int(datetime.datetime.now(datetime.timezone.utc).timestamp())
|
|
manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': seq, 'version': '0.1.0' if seq == 1 else '0.1.1', 'platform': 'x86_64-unknown-linux-gnu', 'published_at': published, 'expires_at': published + 604800, 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': 1, 'artifacts': []}
|
|
for role in ('agent', 'helper'):
|
|
item = build['artifacts']['source'+str(seq-1)][role]
|
|
manifest['artifacts'].append({'role': role, 'artifact_id': str(uuid.uuid4()), 'sha256': item['sha256'], 'bytes': item['bytes']})
|
|
result = typed({'operation': 'canonical_release', 'manifest': manifest})
|
|
raw = bytes(result['bytes'])
|
|
return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw)}
|
|
qualification = unb64(data['qualification_base64url']); q = json.loads(qualification)
|
|
require(q['profile'] == 'linux_compatible_baseline_qualification_v1' and q['normal_signed_sync_observed'])
|
|
require(all(q[n] is False for n in ('ready', 'helper_ready', 'unlock', 'job_authority')))
|
|
if action == 'record-input':
|
|
release = data['signed_release']; manifest = json.loads(unb64(release['envelope']['payload_bytes']))
|
|
require(manifest['release_sequence'] == 1 and manifest['version'] == '0.1.0')
|
|
record = {'schema_version': 1, 'profile': 'decoder_compatible_handoff_v1', 'platform': manifest['platform'], 'release_envelope': release['envelope'], 'manifest_sha256': release['payload_sha256'], 'activation_floor': 1, 'activated_at': int(datetime.datetime.fromisoformat(q['observed_at'].replace('Z', '+00:00')).timestamp()), 'previous_record_sha256': None, 'activation_evidence': {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)}, 'rolled_back': False}
|
|
raw = bytes(typed({'operation': 'canonical_record', 'record': record})['bytes'])
|
|
return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw), 'qualification_base64url': b64(qualification)}
|
|
require(action == 'trust-package')
|
|
package = data['bootstrap_package']; registry = data['registry']; baseline = data['signed_baseline']
|
|
record_raw = unb64(baseline['envelope']['payload_bytes']); record = json.loads(record_raw)
|
|
require(record['activation_evidence'] == {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)})
|
|
registry_raw = bytes(registry['registry_bytes']); require(digest(registry_raw) == registry['reader_registry_sha256'])
|
|
policy = {'schema_version': 1, 'profile': 'linux_general_full_v1', **{n: package[n] for n in ('tenant_id', 'asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')}, 'ledger_path': '/var/lib/rmm-helper/consumption.sqlite', 'lock_path': '/var/lib/rmm-helper/executor.lock', 'installed_record_path': '/var/lib/rmm-agent-updater/installed-record.json', 'keyring_path': '/etc/rmm-agent-updater/keyring.json', 'health_pin_path': '/etc/rmm-agent-updater/health-key.json', 'registry_path': '/etc/rmm-agent-updater/reader-registry.json', 'reader_registry_sha256': registry['reader_registry_sha256'], 'services': registry['services'], 'updater_service': registry['updater_service'], 'updater_binary_sha256': package['files']['updater.bin']['sha256'], 'initial_baseline': baseline['envelope'], 'initial_record_sha256': digest(record_raw), 'initial_manifest_sha256': record['manifest_sha256'], 'initial_activation_floor': 1}
|
|
policy_raw = bytes(typed({'operation': 'canonical_policy', 'policy': policy})['bytes'])
|
|
keyring_raw = encode({'schema_version': 1, 'generation': 1, 'keys': [{'public_key_hex': signer['release_public_key_hex'], 'platform': 'x86_64-unknown-linux-gnu', 'first_sequence': 1, 'last_sequence': 2, 'revoked': False}]})
|
|
require(keyring_raw == unb64(signer['keyring_base64url']) and digest(keyring_raw) == signer['keyring_sha256'])
|
|
health_raw = encode(signer['health_pin'])
|
|
shape = typed({'operation': 'validate_public_bundle', 'record': list(record_raw), 'policy': list(policy_raw), 'keyring': list(keyring_raw), 'health_pin': list(health_raw)})
|
|
require(shape['shape_only_not_signature_verified'] is True)
|
|
files = {'policy.json': policy_raw, 'keyring.json': keyring_raw, 'health-key.json': health_raw, 'reader-registry.json': registry_raw, 'installed-record.json': record_raw}
|
|
return {'operation': 'linux_compatible_trust_install_v1', 'case_id': package['case_id'], 'signer_case_id': signer['case_id'], 'qualification_sha256': digest(qualification), 'files': {n: {'base64': base64.b64encode(raw).decode(), 'sha256': digest(raw), 'bytes': len(raw)} for n, raw in files.items()}, 'shape_checks': shape, 'signature_verified': False, 'job_authority': False}
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('action', choices=('release-input', 'record-input', 'trust-package'))
|
|
parser.add_argument('--input', required=True, type=Path)
|
|
parser.add_argument('--output', required=True, type=Path)
|
|
args = parser.parse_args()
|
|
result = prepare(args.action, json.loads(args.input.read_text()))
|
|
bootstrap.once(args.output, encode(result))
|
|
print(json.dumps({'output': str(args.output), 'sha256': digest(args.output.read_bytes()), 'local_only': True, 'target_changed': False}))
|
|
if __name__ == '__main__': main()
|