Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

9.9 KB
Raw Blame
#!/usr/bin/env python3
"""Private encrypted-copy acceptance on the owned Asahi target VM fixture."""
from contextlib import ExitStack
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import subprocess
import sys
import time


def load(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


fixture = load('fixture', '/var/tmp/harness-target-guest.py')
storage = load('storage', '/var/tmp/storage.py')
PASSWORD = 'firstboot-local-42'
STATE = fixture.PLAN.with_name('storage.json')
INPUT = Path('/var/tmp/harness-storage-input.json')
EVIDENCE = Path('/var/tmp/harness-storage-evidence.json')
RESULT = Path('/var/tmp/harness-storage-result.json')
ENCRYPTED = '/dev/vdb6'


def payload():
    inputs = json.loads(INPUT.read_text())
    return storage.Payload('/dev/vdc', inputs['image_sha256'], inputs['source_commit'])


def install_copy(password):
    source = payload()
    verifications = 0
    verify = source.verify_pristine
    def observed():
        nonlocal verifications
        verify()
        verifications += 1
    source.verify_pristine = observed
    # Match the installer's outer read-only lifetime across nested stages.
    # Every independent retry constructs and authenticates a fresh source.
    with source.open():
        mounted = (source.root, source.home, source.boot, source.esp)
        state = storage.install(fixture.PLAN, source, password)
        assert mounted == (source.root, source.home, source.boot, source.esp)
        assert all(path.is_dir() for path in mounted)
        assert verifications == 1
    assert all(path is None for path in (source.root, source.home, source.boot, source.esp))
    return state


def guard():
    fixture.guard()
    storage.target.platform_esp = lambda: fixture.ESP
    serial = fixture.run('lsblk', '-ndo', 'SERIAL', '/dev/vdc')
    assert serial == 'HARNESS_PAYLOAD', repr(serial)
    assert fixture.run('blockdev', '--getro', '/dev/vdc') == '1'
    fixture.mount()


def header():
    metadata = json.loads(storage.run('cryptsetup', 'luksDump', '--dump-json-metadata', ENCRYPTED))
    offset = int(metadata['segments']['0']['offset'])
    with open(ENCRYPTED, 'rb', buffering=0) as handle:
        content = os.pread(handle.fileno(), offset, 0)
    return {'sha256': hashlib.sha256(content).hexdigest(), 'bytes': offset,
            'uuid': storage.run('cryptsetup', 'luksUUID', ENCRYPTED),
            'kdf': metadata['keyslots']['0']['kdf']['type']}


def secret_absent():
    for path in (STATE, fixture.PLAN):
        assert PASSWORD not in path.read_text()
    state = json.loads(STATE.read_text())
    assert all('pass' not in key and 'key' not in key for key in state)


def interrupt_encryption():
    command = storage.run
    def interrupted(*args, **kwargs):
        result = command(*args, **kwargs)
        if args[:2] == ('cryptsetup', 'luksFormat'):
            evidence = {'header': header(), 'phase_after_format': json.loads(STATE.read_text())['phase']}
            assert evidence['phase_after_format'] == 'planned'
            secret_absent()
            EVIDENCE.write_text(json.dumps(evidence, indent=2) + '\n')
            os._exit(76)
        return result
    storage.run = interrupted
    install_copy(PASSWORD)
    raise AssertionError('The encryption interruption was not exercised')


def interrupt_copy():
    evidence = json.loads(EVIDENCE.read_text())
    assert header() == evidence['header']
    record = STATE.read_bytes()
    try:
        install_copy('incorrect-password')
        raise AssertionError('The retry accepted an incorrect disk password')
    except storage.StorageError as error:
        assert 'cryptsetup failed' in str(error)
    assert STATE.read_bytes() == record and header() == evidence['header']
    assert not storage.probe('/dev/vdb5').get('TYPE')
    evidence['incorrect_password_rejected_before_formatting'] = True
    original = storage.run
    def stop_during_copy(*args, **kwargs):
        if args[0] != 'rsync':
            return original(*args, **kwargs)
        destination = Path(args[-1])
        with open('/var/tmp/interrupted-rsync.log', 'wb') as log:
            process = subprocess.Popen(list(map(str, args)), stdout=log, stderr=log)
            deadline = time.monotonic() + 60
            marker = destination / 'etc/passwd'
            while not marker.is_file():
                if process.poll() is not None or time.monotonic() >= deadline:
                    if process.poll() is None:
                        process.kill()
                    process.wait(timeout=15)
                    raise AssertionError('rsync did not reach a live partial copy')
                time.sleep(.02)
            assert process.poll() is None
            process.kill()
            assert process.wait(timeout=15) == -9
        state = json.loads(STATE.read_text())
        assert state['phase'] == 'copying'
        assert header() == evidence['header']
        evidence.update(partial_copy=True, partial_state=state, partial_passwd_sha256=fixture.digest(marker))
        EVIDENCE.write_text(json.dumps(evidence, indent=2) + '\n')
        secret_absent()
        os._exit(77)
    storage.run = stop_during_copy
    install_copy(PASSWORD)
    raise AssertionError('The real rsync interruption was not exercised')


def assert_protected():
    baseline = json.loads(fixture.BASELINE.read_text())
    assert fixture.protected() == {k: v for k, v in baseline.items() if k not in ('table', 'started_at')}


def verify_copy(state):
    with payload().open() as source, storage.encrypted_root(ENCRYPTED, state, PASSWORD.encode()) as mapper:
        with storage.work_directory() as root, ExitStack() as mounts:
            top = mounts.enter_context(storage.mounted(mapper, root / 'top', 'ro,rescue=nologreplay,subvolid=5'))
            boot = mounts.enter_context(storage.mounted('/dev/vdb5', root / 'boot', 'ro,noload'))
            separate = ('boot', 'home', 'dev', 'proc', 'sys', 'run')
            for name in separate:
                directory = top / 'root' / name
                assert directory.is_dir() and not directory.is_symlink() and not list(directory.iterdir()), name
            for src, dst, excludes, filters in [
                    (source.root, top / 'root', ['/' + name + '/***' for name in separate], []),
                    (source.home, top / 'home', [], []),
                    (source.boot, boot, ['/efi/***'], ['--filter=-x system.*', '--filter=-x security.selinux'])]:
                changes = storage.run('rsync', '-aHAXnci', '--numeric-ids', '--one-file-system', '--delete',
                                      *filters, *('--exclude=' + value for value in excludes),
                                      str(src) + '/', str(dst) + '/', timeout=180)
                assert not changes, changes
            image = json.loads((top / 'root/usr/share/harness-os/image.json').read_text())
            manifest = image['session_package']['files']
            for name, expected in manifest.items():
                assert fixture.digest(top / 'root' / name) == expected, name
            for name, expected in image['first_boot']['files'].items():
                assert fixture.digest(top / 'root' / name) == expected['sha256'], name
            return {'runtime_files_checked': len(manifest), 'empty_mountpoints': list(separate),
                    'source_commit': image['source_commit'],
                    'root_uuid': storage.probe(mapper)['UUID'], 'boot_uuid': storage.probe('/dev/vdb5')['UUID']}


def preserved_project(state, *, create=False):
    with storage.encrypted_root(ENCRYPTED, state, PASSWORD.encode()) as mapper, storage.work_directory() as root:
        with storage.mounted(mapper, root / 'home', 'subvol=home,noatime') as home:
            project = home / 'me/projects/keep/result.txt'
            if create:
                project.parent.mkdir(parents=True)
                project.write_text('Keep work after installation is copied.\n')
                storage.run('sync', '-f', home)
            return fixture.digest(project)


def finish():
    evidence = json.loads(EVIDENCE.read_text())
    assert json.loads(STATE.read_text())['phase'] == 'copying'
    assert header() == evidence['header']
    state = install_copy(PASSWORD)
    assert state['phase'] == 'copied' and header() == evidence['header']
    evidence['copy_verification'] = verify_copy(state)
    assert evidence['copy_verification']['root_uuid'] == state['root_uuid']
    assert evidence['copy_verification']['boot_uuid'] == state['boot_uuid']
    assert_protected()
    project = preserved_project(state, create=True)
    record = STATE.read_bytes()
    commands = []
    command = storage.run
    def observed(*args, **kwargs):
        commands.append(args)
        return command(*args, **kwargs)
    storage.run = observed
    assert install_copy(PASSWORD) == state
    storage.run = command
    assert not any(args[0] in ('rsync', 'mkfs.btrfs', 'mkfs.ext4') or args[:2] == ('cryptsetup', 'luksFormat') for args in commands)
    assert STATE.read_bytes() == record
    assert preserved_project(state) == project and header() == evidence['header']
    assert_protected()
    secret_absent()
    fixture.run('umount', fixture.ROOT)
    assert not Path('/dev/mapper/harness-stage-' + state['luks_uuid'].replace('-', '')).exists()
    evidence.update(status='passed', state=state, project_sha256=project,
                    source_verified_once_per_attempt=True, source_mounts_held_through_copy=True,
                    completed_retry_preserves_work=True, protected_data_unchanged=True,
                    header_unchanged=True, no_secret_in_records=True, mapping_closed=True)
    RESULT.write_text(json.dumps(evidence, indent=2) + '\n')
    print('Fresh encrypted copy resumed; complete file comparison, runtime hashes and project preservation passed.', flush=True)


if __name__ == '__main__':
    guard()
    {'interrupt-encryption': interrupt_encryption, 'interrupt-copy': interrupt_copy, 'finish': finish}[sys.argv[1]]()