diff --git a/deploy/windows_msi_contract_acceptance.py b/deploy/windows_msi_contract_acceptance.py new file mode 100644 index 0000000..1dfcb18 --- /dev/null +++ b/deploy/windows_msi_contract_acceptance.py @@ -0,0 +1,692 @@ +#!/usr/bin/env python3 +"""M4 MSI pure common contracts: Linux VM FIRST, then Windows VM. + +Only fixed common test executables run in separate bootstrap scratch directories. +No RMM job, daemon, live database, PKI, policy, enrollment or capability change. +Native execution is explicit; any lost reply permits same-case readback only. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import shlex +import shutil +import stat +import subprocess +import uuid + +import vm_access_check as access + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/windows-msi-contracts' +IMAGE = 'rust-discovery-durable-final-build:local' +PROFILE = 'windows-msi-contracts-v1' +LINUX_REMOTE = '/var/lib/rmm-windows-msi-contracts' +ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +BACKEND = ROOT / 'devstate/windows-msi-integration/local-qualification.json' +BACKEND_SHA = 'b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74' +# Actual local Linux RELEASE --list was obtained before fixing these lists. +# Windows lists remain EXPECTED until the native Windows process emits them. +REVIEWED_BACKEND = {'crates/domain/src/lib.rs': '7919c099a8b568259b9b197d1742cdc11844523c64654325b6c142ae437dda1f', + 'crates/domain/src/jobs.rs': 'ff7f086a28e5e17832fa4afc974ce8395c7887360c3291d27d1cc7ae12bab117', + 'crates/domain/src/windows_execution.rs': 'aa2c5916c9d6c9d9ab8e9bd0d1051920fcb2ae9ecf4c18cf6ee89c4668b9b460', + 'crates/agent-core/src/auth.rs': '86cda2e10321bcb3591013880f11e53a8bcb01c53521bbcc28d38d6b1f5402bd', + 'crates/agent-core/tests/contracts.rs': 'f57fe9b938b8e563229b9da7c77b6df7b419f0ba41ef05abb1f7f9e3ff2e61e9', + 'crates/server/src/agent_artifacts.rs': '5f68ea4a26de529d814b7365b85f978ced7715c7da8fa2a435021e3e953b506e', + 'crates/server/src/catalog.rs': 'c4c293271a36aba80fac00b72e2936ed2c11bccf49e511d7003bd95574fb0e04', + 'crates/server/src/job_gateway.rs': '421672d7a283fcda5bc00854b53eb86c4236621336d5cf6b72c43d288fad74de', + 'crates/server/src/jobs.rs': '725a2f9c61180c4d938bcf7383ee78c5d50b83b703665195502548140126fce6', + 'crates/server/src/main.rs': 'c8ddc2a1a7cd29122f5590902f25c2711eef6e2f5488e2e7869c034ddf27394e', + 'crates/server/src/windows_policy.rs': 'd0ffd67f4f2c56a681c44617962b3834f02cbbc6ba076461b856f4e87ce6c222', + 'crates/server/src/windows_msi_catalog.rs': 'dcb704b02a5d5d5bfff5b8e34a712cf5a0b9c32cb21af242252976d0c0b0ad84', + 'migrations/0014_windows_msi_catalog.sql': '893440fc75914d4e7e3a62055f693562c6a093924eab77a20f6b5428f63d20d0'} +EXPECTED = {'linux': {'domain': ['apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', + 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', + 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth', + 'windows_execution::tests::ensure_msi_action_is_strict_opt_in_and_system_windows_only_without_linux_byte_changes', + 'windows_execution::tests::immutable_fixture_recipe_rejects_reboot_url_transform_and_implicit_upgrade', + 'windows_execution::tests::major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family'], + 'contracts': ['atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery'], + 'helper-reconcile': ['abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock', + 'abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows', + 'abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof', + 'abandon_storage_failure_rolls_back_consumption_start_and_outcome_together', + 'both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'root_and_system_are_distinct_explicit_helper_domains', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock'], + 'reconcile': ['accepted_unstarted_keeps_ids_and_never_consumes_or_executes', + 'authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecuted', + 'clean_reconnect_and_reopen_require_new_signed_challenge', + 'crash_after_durable_outcome_before_result_recovers_original_then_signed_sync_closes', + 'crash_after_result_commit_before_finish_retains_exact_result_and_local_prior', + 'crash_before_running_event_recovers_real_consumption_without_inventing_completion', + 'definite_nonstart_result_is_unconsumed_not_ambiguous', + 'exact_signatures_all_identity_channel_checkpoint_and_request_bindings', + 'final_result_preserves_original_envelope_outputs_and_exact_acks', + 'foreign_ack_and_noncanonical_outcome_are_transactionally_rejected', + 'helper_gate_requires_exact_signed_instruction_and_applied_checkpoint', + 'helper_omitting_a_consumed_root_attempt_is_a_sticky_restore_fault', + 'independent_helper_checkpoint_regression_is_sticky', + 'independent_local_outcome_mismatch_never_unlocks', + 'missing_server_job_fault_is_sticky_even_if_attempt_disappears_later', + 'negative_server_revision_never_rolls_back_local_unknown_revision', + 'original_completion_after_reconnect_unknown_is_recovered_without_reexecuting', + 'reboot_keeps_lock_until_signed_new_authenticated_boot_inventory_evidence', + 'requesting_running_and_omitted_attempts_cannot_regain_readiness', + 'rollback_and_false_clock_close_mutations_but_do_not_destroy_journal', + 'signed_attempt_mutation_or_terminal_regression_is_rejected', + 'signed_terminal_nonstart_stays_unconsumed_across_sync_and_pending_denial', + 'signed_unlock_still_rejects_a_missing_local_outcome_in_legacy_crash_window', + 'system_helper_import_requires_explicit_system_identity_and_never_root_alias', + 'terminal_labels_never_override_grant_consumption_or_unclear_attempts', + 'uncertain_request_preserves_pending_request_and_last_applied_anchor'], + 'linux-helper': ['both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock']}, + 'windows': {'domain': ['apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', + 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', + 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth', + 'windows_execution::tests::ensure_msi_action_is_strict_opt_in_and_system_windows_only_without_linux_byte_changes', + 'windows_execution::tests::immutable_fixture_recipe_rejects_reboot_url_transform_and_implicit_upgrade', + 'windows_execution::tests::major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family'], + 'contracts': ['atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery'], + 'helper-reconcile': ['abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock', + 'abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows', + 'abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof', + 'abandon_storage_failure_rolls_back_consumption_start_and_outcome_together', + 'both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'root_and_system_are_distinct_explicit_helper_domains', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock'], + 'reconcile': ['accepted_unstarted_keeps_ids_and_never_consumes_or_executes', + 'authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecuted', + 'clean_reconnect_and_reopen_require_new_signed_challenge', + 'crash_after_durable_outcome_before_result_recovers_original_then_signed_sync_closes', + 'crash_after_result_commit_before_finish_retains_exact_result_and_local_prior', + 'crash_before_running_event_recovers_real_consumption_without_inventing_completion', + 'definite_nonstart_result_is_unconsumed_not_ambiguous', + 'exact_signatures_all_identity_channel_checkpoint_and_request_bindings', + 'final_result_preserves_original_envelope_outputs_and_exact_acks', + 'foreign_ack_and_noncanonical_outcome_are_transactionally_rejected', + 'helper_gate_requires_exact_signed_instruction_and_applied_checkpoint', + 'helper_omitting_a_consumed_root_attempt_is_a_sticky_restore_fault', + 'independent_helper_checkpoint_regression_is_sticky', + 'independent_local_outcome_mismatch_never_unlocks', + 'missing_server_job_fault_is_sticky_even_if_attempt_disappears_later', + 'negative_server_revision_never_rolls_back_local_unknown_revision', + 'original_completion_after_reconnect_unknown_is_recovered_without_reexecuting', + 'reboot_keeps_lock_until_signed_new_authenticated_boot_inventory_evidence', + 'requesting_running_and_omitted_attempts_cannot_regain_readiness', + 'rollback_and_false_clock_close_mutations_but_do_not_destroy_journal', + 'signed_attempt_mutation_or_terminal_regression_is_rejected', + 'signed_terminal_nonstart_stays_unconsumed_across_sync_and_pending_denial', + 'signed_unlock_still_rejects_a_missing_local_outcome_in_legacy_crash_window', + 'system_helper_import_requires_explicit_system_identity_and_never_root_alias', + 'terminal_labels_never_override_grant_consumption_or_unclear_attempts', + 'uncertain_request_preserves_pending_request_and_last_applied_anchor']}} +COUNTS = {platform: {kind: len(names) for kind, names in lists.items()} for platform, lists in EXPECTED.items()} +KINDS = {'linux': ('domain','contracts','helper-reconcile','reconcile','linux-helper'), + 'windows': ('domain','contracts','helper-reconcile','reconcile')} + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def object_pairs(items): + value = {} + for key, item in items: + require(key not in value, 'duplicate JSON field') + value[key] = item + return value + + +def private(path, directory=False): + item = Path(path).lstat() + require(not stat.S_ISLNK(item.st_mode) and item.st_uid == os.getuid() + and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1), + 'private owned regular path') + + +def load(path): + private(path) + require(Path(path).stat().st_size <= 2 * 1024 * 1024, 'bounded private JSON') + return json.loads(Path(path).read_bytes(), object_pairs_hook=object_pairs) + + +def save(path, value): + private(Path(path).parent, True) + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + json.dump(value, stream, indent=2); stream.write('\n'); stream.flush(); os.fsync(stream.fileno()) + + +def setup_state(): + os.umask(0o077) + STATE.parent.mkdir(mode=0o700, exist_ok=True) + metadata = STATE.parent.lstat() + require(stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == os.getuid() and not metadata.st_mode & 0o002, + 'owned non-world-writable devstate parent') + STATE.mkdir(mode=0o700, exist_ok=True) + private(STATE, True) + + +def source_files(): + require(sha(BACKEND) == BACKEND_SHA, 'exact reviewed local backend qualification') + reviewed = load(BACKEND) + require(reviewed['source_files'] == REVIEWED_BACKEND, 'reviewed thirteen backend/common pins') + names = {'Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'deploy/windows_msi_contract_acceptance.py', + 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/remote_client_bootstrap.py', + 'deploy/windows/client_bootstrap.py', 'tools/apt-transaction/apt_transaction.py', + 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', + 'tests/apt_transaction/fixtures/planning-canonical.json'} + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'crates').glob('*/Cargo.toml')) + for crate in ('domain', 'agent-core', 'agent-linux', 'privileged-helper', 'protocol'): + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'crates' / crate).rglob('*') + if p.is_file() and p.suffix in ('.rs', '.toml', '.c', '.h', '.idl', '.proto')) + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'proto').rglob('*') if p.is_file()) + names.update(REVIEWED_BACKEND) + for name in names: require((ROOT / name).is_file() and not (ROOT / name).is_symlink(), 'regular fixed source inputs') + result = {name: sha(ROOT / name) for name in sorted(names)} + require(all(result[name] == digest for name, digest in REVIEWED_BACKEND.items()), 'reviewed backend sources unchanged') + # No web source is a compiler input or part of this common-contract proof. + return result + + +def names(raw): + return sorted(re.findall(r'^([A-Za-z0-9_:]+): test\r?$', raw, re.M)) + + +def check_list(raw, expected): + require(names(raw) == expected and len(set(expected)) == len(expected), 'all exact test names before execution') + require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in raw.splitlines()), + 'terse list contains only exact tests, no benchmarks or extra entries') + + +def check_output(raw, count): + require(re.search(rb'^test result: ok\. ' + str(count).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;[^\r\n]*\r?$', raw, re.M), + 'all tests passed without skipped or filtered tests') + + +def command(args, folder, timeout=300): + index = len(list(folder.glob('command-*.json'))) + try: + done = subprocess.run(args, capture_output=True, timeout=timeout) + save(folder / ('command-%03d.json' % index), dict(returncode=done.returncode, + stdout_b64=base64.b64encode(done.stdout).decode(), stderr_b64=base64.b64encode(done.stderr).decode())) + require(done.returncode == 0, 'local qualification command failed; private output retained') + return done.stdout + except subprocess.TimeoutExpired as error: + save(folder / ('command-%03d.json' % index), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('local qualification timed out; no automatic retry') from None + + +def scripts(): + return {'linux_case_sha256': hashlib.sha256(inspect.getsource(linux_case).encode()).hexdigest(), + 'windows_script_sha256': hashlib.sha256(WINDOWS.encode()).hexdigest()} + + +def build(): + setup_state() + folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700) + before = source_files(); save(folder / 'source-before.json', before) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE], folder).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable compiler image') + target = STATE / 'target'; target.mkdir(mode=0o700, exist_ok=True); private(target, True) + common = ['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m', + '-v',str(ROOT)+':/src:ro','-v',str(target)+':/target','-e','CARGO_TARGET_DIR=/target','--entrypoint','cargo',image] + artifacts = {'linux': {}, 'windows': {}} + for platform, triple in [('linux','x86_64-unknown-linux-gnu'),('windows','x86_64-pc-windows-gnu')]: + options = ['--manifest-path','/src/Cargo.toml','--locked','--offline','--target',triple] + packages = ['-p','rmm-domain','-p','rmm-agent-core'] + (['-p','rmm-privileged-helper'] if platform == 'linux' else []) + command(common + ['clippy'] + options + packages + ['--all-targets','--','-D','warnings'], folder) + selections = [(['-p','rmm-domain','--lib'], 'domain'), + (['-p','rmm-agent-core'] + (['--features','openssl-verifier'] if platform == 'linux' else []) + ['--tests'], 'core')] + if platform == 'linux': selections.append((['-p','rmm-privileged-helper','--test','reconcile'], 'linux-helper')) + links = {} + for selection, group in selections: + raw = command(common + ['test'] + options + selection + ['--release','--no-run','--message-format=json'], folder) + for line in raw.splitlines(): + try: item = json.loads(line) + except ValueError: continue + if item.get('reason') != 'compiler-artifact' or not item.get('executable') or not item.get('profile',{}).get('test'): continue + name = item['target']['name'] + kind = 'domain' if group == 'domain' else 'linux-helper' if group == 'linux-helper' else {'contracts':'contracts','helper_reconcile':'helper-reconcile','reconcile':'reconcile'}.get(name) + if kind is None: continue # empty Core lib harness is not this profile + require(kind not in links and kind in KINDS[platform], 'exact distinct selected test link') + links[kind] = target / Path(item['executable']).relative_to('/target') + require(set(links) == set(KINDS[platform]), 'all selected native artifacts') + destination = folder / platform; destination.mkdir(mode=0o700) + for kind in KINDS[platform]: + original = destination / (kind + '.linked'); delivery = destination / (kind + ('.exe' if platform == 'windows' else '')) + for path in (original, delivery): shutil.copyfile(links[kind], path); path.chmod(0o600) + linked_sha = sha(original) + tool = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' + command(['docker','run','--rm','--network','none','--read-only','-v',str(destination)+':/artifacts', + '--entrypoint',tool,image,'--strip-debug','/artifacts/'+delivery.name], folder) + require(sha(links[kind]) == sha(original) == linked_sha, 'original link preserved') + data = delivery.read_bytes() + if platform == 'linux': require(data[:6] == b'\x7fELF\x02\x01' and data[18:20] == b'\x3e\0', 'Linux x64 ELF') + else: + offset = int.from_bytes(data[60:64],'little'); require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86', 'Windows x64 PE') + artifacts[platform][kind] = dict(path=str(delivery),sha256=sha(delivery),bytes=len(data), + original_link_path=str(original),original_link_sha256=linked_sha,debug_stripped_delivery=True) + # All local Linux lists precede any local tests, using exact delivery bytes. + def invoke(item, args): + script = 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit"]+' + repr(args) + ',check=True,cwd="/tmp",env={"PATH":"/usr/bin:/bin","LANG":"C.UTF-8","TMPDIR":"/tmp","RUST_BACKTRACE":"0"})' + return command(['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,exec,size=128m', + '-v',item['path']+':/unit:ro','--entrypoint','python3',image,'-c',script], folder) + for kind, item in artifacts['linux'].items(): + raw = invoke(item, ['--list','--format','terse']); check_list(raw.decode(), EXPECTED['linux'][kind]) + save(folder / ('local-linux-'+kind+'-list.json'),dict(stdout_b64=base64.b64encode(raw).decode())) + for kind, item in artifacts['linux'].items(): + raw = invoke(item, ['--test-threads=1']); check_output(raw, COUNTS['linux'][kind]) + save(folder / ('local-linux-'+kind+'-run.json'),dict(stdout_b64=base64.b64encode(raw).decode())) + versions = command(['docker','run','--rm','--network','none','--read-only','-v',item['path']+':/unit:ro', + '--entrypoint','readelf',image,'--version-info','/unit'],folder).decode() + dynamic = command(['docker','run','--rm','--network','none','--read-only','-v',item['path']+':/unit:ro', + '--entrypoint','readelf',image,'-d','/unit'],folder).decode() + glibc = sorted(set(re.findall(r'GLIBC_(\d+\.\d+)',versions)),key=lambda n:tuple(map(int,n.split('.')))) + require(glibc and tuple(map(int,glibc[-1].split('.'))) <= (2,34), 'GLIBC at most 2.34 delivery requirement') + item['glibc_max'] = glibc[-1]; item['needed'] = sorted(re.findall(r'Shared library: \[([^\]]+)\]',dynamic)) + require(source_files() == before, 'all sources and backend pins unchanged through build') + manifest = dict(schema_version=1,profile=PROFILE,source_files=before,source_before_after_equal=True, + reviewed_backend_source_pins=REVIEWED_BACKEND,backend_qualification_sha256=BACKEND_SHA, + backend_sources_hash_bound_only=True,native_postgresql_executed=False,live_installer_executed=False, + runner_sha256=sha(__file__),scripts=scripts(),test_names=EXPECTED,test_counts=COUNTS,artifacts=artifacts, + build_image_id=image,both_targets_clippy_D_warnings=True,linux_lists_actual=True, + windows_lists_expected_until_native=True,local_linux_tests_passed=sum(COUNTS['linux'].values()), + native_linux_executed=False,native_windows_executed=False,product_state_writes=False,rmm_jobs=False) + save(folder / 'manifest.json',manifest) + print(json.dumps(dict(manifest=str(folder/'manifest.json'),manifest_sha256=sha(folder/'manifest.json'), + linux_tests=sum(COUNTS['linux'].values()),windows_tests_expected=sum(COUNTS['windows'].values()),native_executed=False))) + + +def verified(path, new_run): + path = path.absolute(); private(path.parent, True) + require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}', path.parent.name), 'fixed build manifest path') + value = load(path) + require(value['schema_version'] == 1 and value['profile'] == PROFILE and value['runner_sha256'] == sha(__file__) + and value['scripts'] == scripts() and value['test_names'] == EXPECTED and value['test_counts'] == COUNTS + and value['source_before_after_equal'] is True and value['both_targets_clippy_D_warnings'] is True + and value['reviewed_backend_source_pins'] == REVIEWED_BACKEND and value['backend_qualification_sha256'] == BACKEND_SHA + and value['backend_sources_hash_bound_only'] is True and value['native_postgresql_executed'] is False + and value['live_installer_executed'] is False, + 'original manifest/runner/remote scripts/profile binding') + if new_run: require(value['source_files'] == source_files(), 'new execution requires same current qualified source') + for platform in COUNTS: + require(set(value['artifacts'][platform]) == set(KINDS[platform]), 'all exact fixed artifacts') + for kind, item in value['artifacts'][platform].items(): + suffix = '.exe' if platform == 'windows' else '' + require(Path(item['path']) == path.parent / platform / (kind + suffix) + and Path(item['original_link_path']) == path.parent / platform / (kind + '.linked'), 'fixed private artifact paths') + for field, digest in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + private(item[field]); require(sha(item[field]) == digest, 'immutable original/delivery bytes') + require(Path(item['path']).stat().st_size == item['bytes'], 'artifact size unchanged') + return value + + +def access_settings(root, platform): + """Read strictly parsed existing access only when the parent executes native.""" + require(root == ACCESS_ROOT and not root.is_symlink(), 'original Main access-root only; no copied credentials') + root = root.absolute(); require(root.is_dir() and root.resolve() == root, 'canonical access-root') + if platform == 'linux': + cfg = access.load_config(root / 'credentials/linux-vm.conf') + require(cfg['address'] == '116.203.20.85' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed Cloud Linux bootstrap target') + prefix = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), 'root@116.203.20.85'] + else: + cfg = access.load_config(root / 'credentials/server.conf') + require(cfg['address'] == '88.99.80.202' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed server reverse-jump only') + credentials = root / 'credentials'; private(credentials, True) + record = load(credentials / 'windows-dev-access.json') + require(set(record) == {'ssh_prefix', 'expires_at'}, 'known Windows access schema') + expires = dt.datetime.fromisoformat(record['expires_at']) + require(expires.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expires, 'existing dev access still valid') + key = str(Path(cfg['ssh_key_path']).expanduser()) + jump = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-i', key, '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'ForwardAgent=no', '-o', 'IdentitiesOnly=yes', '-W', '127.0.0.1:22240', 'root@88.99.80.202'] + prefix = ['ssh', '-F', '/dev/null', '-T', '-i', key, '-o', 'ProxyCommand=' + shlex.join(jump), + '-o', 'HostKeyAlias=rmm-windows-dev', '-o', 'UserKnownHostsFile=' + str(credentials / 'windows-dev.known_hosts'), + '-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', 'rmm-dev@127.0.0.1'] + require(record['ssh_prefix'] == prefix, 'exact registered pinned reverse SSH prefix') + private(credentials / 'windows-dev.known_hosts') + return prefix + + +def transport(prefix, command_text, data, folder, timeout=60): + """No stdout/stderr escapes the private case directory on transport failure.""" + try: + result = subprocess.run(prefix + [command_text], input=data, capture_output=True, timeout=timeout) + save(folder / ('transport-' + str(uuid.uuid4()) + '.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'native transport incomplete; same-case readback only') + value = json.loads(result.stdout.decode('utf-8-sig'), object_pairs_hook=object_pairs) + require(isinstance(value, dict) and 'bootstrap_error' not in value, 'native bootstrap incomplete; private receipt retained') + return value + except subprocess.TimeoutExpired as error: + save(folder / ('transport-timeout-' + str(uuid.uuid4()) + '.json'), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('native reply lost; read original case only') from None + + +def linux_rpc(prefix, payload, folder): + code = 'import os,json,subprocess,base64,hashlib,uuid\nfrom pathlib import Path\n' + inspect.getsource(require) + '\n' + inspect.getsource(linux_case) + code += '\ntry:\n print(json.dumps(linux_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(json.dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return transport(prefix, 'python3 -', code.encode(), folder) + + +def windows_rpc(prefix, payload, folder): + script = 'try {\n' + WINDOWS + '\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command_text = 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ' + base64.b64encode(script.encode('utf-16le')).decode() + return transport(prefix, command_text, (json.dumps(payload) + '\n').encode(), folder) + + +def upload(prefix, item, destination, folder, kind): + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] + # Linux sftp uses uppercase -P; Windows prefix has no -p option. + if '-p' in args: args[args.index('-p')] = '-P' + batch = ('put ' + json.dumps(item['path']) + ' ' + json.dumps(destination) + '\n').encode() + save(folder / ('transfer-' + kind + '-intent.json'), dict(sha256=item['sha256'], bytes=item['bytes'])) + try: + result = subprocess.run(args, input=batch, capture_output=True, timeout=60) + save(folder / ('transfer-' + kind + '-result.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'fixed artifact transfer failed; no retry') + except subprocess.TimeoutExpired as error: + save(folder / ('transfer-' + kind + '-timeout.json'), dict(timed_out=True, + stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('artifact transfer incomplete; no retry') from None + + +def linux_case(p): + import resource + import stat + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'windows-msi-contracts-v1', 'fixed unit case') + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == {'domain','contracts','helper-reconcile','reconcile','linux-helper'}, 'fixed Linux root qualification') + root = Path('/var/lib/rmm-windows-msi-contracts') + def safe(path, directory): + s = path.lstat() + require(s.st_uid == 0 and stat.S_IMODE(s.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(s.st_mode) if directory else stat.S_ISREG(s.st_mode) and s.st_nlink == 1), 'private fixed unit path') + def write(path, value): + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'w') as out: json.dump(value, out); out.flush(); os.fsync(out.fileno()) + os.umask(0o077) + case = root / ident + if p['action'] == 'read' and not case.exists(): return dict(id=ident, status='remote_case_absent') + if p['action'] == 'prepare': + if not root.exists(): root.mkdir(mode=0o700) + safe(root, True); case.mkdir(mode=0o700); write(case / 'intent.json', p) + return dict(id=ident, prepared=True) + safe(root, True); safe(case, True); safe(case / 'intent.json', False) + original = json.loads((case / 'intent.json').read_bytes()) + require({k: v for k, v in p.items() if k != 'action'} == {k: v for k, v in original.items() if k != 'action'}, 'original complete unit intent') + if p['action'] == 'read': + if not (case / 'result.json').exists(): + partial = {} + for kind in ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + for suffix in ('-list.json','-result.json'): + path = case / (kind + suffix) + if path.exists(): + safe(path, False); require(path.stat().st_size <= 2*1024*1024, 'bounded original partial receipt') + partial[kind + suffix] = json.loads(path.read_bytes()) + return dict(id=ident,status='started_without_result' if (case/'started.json').exists() else 'prepared_without_start', + both_lists_verified=(case/'all-lists-verified.json').exists(),tests_started=(case/'tests-started.json').exists(),partial=partial) + safe(case / 'result.json', False); return json.loads((case / 'result.json').read_bytes()) + write(case / 'started.json', dict(manifest_sha256=p['manifest_sha256'], execute_once=True)) + temp = case / 'tmp'; temp.mkdir(mode=0o700) + env = {'PATH': '/usr/bin:/bin', 'LANG': 'C.UTF-8', 'LC_ALL': 'C.UTF-8', 'TMPDIR': str(temp), 'RUST_BACKTRACE': '0'} + def limits(): + resource.setrlimit(resource.RLIMIT_CORE, (0, 0)); resource.setrlimit(resource.RLIMIT_CPU, (15, 15)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)) + require(subprocess.run(['/usr/bin/unshare', '--net', '--', '/usr/bin/true'], env=env, capture_output=True, timeout=3).returncode == 0, 'network namespace required') + listings = {} + for kind in ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == p['artifacts'][kind]['sha256'], 'exact Linux delivery') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=5, preexec_fn=limits) + require(listed.returncode == 0 and len(listed.stdout) <= 65536 and len(listed.stderr) <= 65536, 'bounded actual listing') + actual = sorted(line[:-6] for line in listed.stdout.decode().splitlines() if line.endswith(': test')) + write(case / (kind+'-list.json'),dict(names=actual,exit_code=listed.returncode,sha256=p['artifacts'][kind]['sha256'], + stdout_b64=base64.b64encode(listed.stdout).decode(),stderr_b64=base64.b64encode(listed.stderr).decode())) + require(all(not line.strip() or __import__('re').fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) + and actual == p['names'][kind] and len(actual) == {'domain':20,'contracts':8,'helper-reconcile':26,'reconcile':26,'linux-helper':21}[kind], 'exact native test names before ANY test execution') + listings[kind] = actual + write(case/'all-lists-verified.json',dict(all_lists_before_any_tests=True,manifest_sha256=p['manifest_sha256'])) + write(case/'tests-started.json',dict(execute_tests_once=True)) + reports = {} + for kind in ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + timed = False + try: code = subprocess.run(['/usr/bin/unshare', '--net', '--', str(case / kind), '--test-threads=1'], cwd=case, env=env, + stdin=subprocess.DEVNULL, stdout=out, stderr=err, timeout=18, preexec_fn=limits).returncode + except subprocess.TimeoutExpired: timed = True; code = None + out.flush(); err.flush(); os.fsync(out.fileno()); os.fsync(err.fileno()) + raw, errors = [(case / (kind + '-' + stream + '.bin')).read_bytes() for stream in ('stdout', 'stderr')] + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native output') + reports[kind] = dict(sha256=p['artifacts'][kind]['sha256'], names=listings[kind], exit_code=code, timed_out=timed, + stdout_b64=base64.b64encode(raw).decode(), stderr_b64=base64.b64encode(errors).decode()) + write(case / (kind + '-result.json'), reports[kind]) + value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], + scripts=p['scripts'], reports=reports, all_lists_before_any_tests=True, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False) + write(case / 'result.json', value); return value + + +WINDOWS = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('prepare','run','read')-or$p.profile-cne'windows-msi-contracts-v1'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first profile case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('windows-msi-contracts-'+$p.id) +function Safe($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function MatchIntent($old){if($old.id-cne$p.id-or$old.profile-cne$p.profile-or$old.manifest_sha256-cne$p.manifest_sha256-or$old.runner_sha256-cne$p.runner_sha256-or$old.linux_proof_sha256-cne$p.linux_proof_sha256-or$old.scripts.linux_case_sha256-cne$p.scripts.linux_case_sha256-or$old.scripts.windows_script_sha256-cne$p.scripts.windows_script_sha256){throw 'Original profile intent'};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){if($old.artifacts.$kind.sha256-cne$p.artifacts.$kind.sha256-or$old.artifacts.$kind.bytes-ne$p.artifacts.$kind.bytes-or(@($old.names.$kind)-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'Original artifact/test tuple'}}} +if($p.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;status='remote_case_absent'}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($p.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case retained'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;$intent=Join-Path $case 'intent.json';Safe $intent $false;MatchIntent (Get-Content -LiteralPath $intent -Raw|ConvertFrom-Json) +if($p.action-eq'read'){$result=Join-Path $case 'result.json';if(!(Test-Path -LiteralPath $result)){$status=if(Test-Path -LiteralPath (Join-Path $case 'started.json')){'started_without_result'}else{'prepared_without_start'};$partial=@{};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){foreach($suffix in @('-list.json','-result.json')){$file=Join-Path $case ($kind+$suffix);if(Test-Path -LiteralPath $file){Safe $file $false;if((Get-Item -LiteralPath $file).Length-gt2097152){throw 'Partial report bound'};$partial[$kind+$suffix]=Get-Content -LiteralPath $file -Raw|ConvertFrom-Json}}};@{id=$p.id;status=$status;all_lists_verified=(Test-Path -LiteralPath (Join-Path $case 'all-lists-verified.json'));tests_started=(Test-Path -LiteralPath (Join-Path $case 'tests-started.json'));partial=$partial}|ConvertTo-Json -Depth 16 -Compress;exit 0};Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$systemRoot=$env:WINDIR;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$systemRoot;$s.EnvironmentVariables['WINDIR']=$systemRoot;$s.EnvironmentVariables['PATH']=$systemRoot+'\System32';$s.EnvironmentVariables['TEMP']=$script:temp;$s.EnvironmentVariables['TMP']=$script:temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(18000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($o.Result));stderr_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($e.Result))}}finally{$c.Dispose()}} +WriteNew (Join-Path $case 'started.json') @{execute_once=$true;manifest_sha256=$p.manifest_sha256};$script:temp=Join-Path $case 'tmp';New-Item -ItemType Directory -Path $script:temp|Out-Null;Safe $script:temp $true +$lists=@{};$executables=@{} +foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){$item=$p.artifacts.$kind;if($item.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$item.sha256-or(Get-Item -LiteralPath $exe).Length-ne$item.bytes){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Actual Windows listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$unexpected=@($text-split'\r?\n'|Where-Object{$_.Trim()-and$_-notmatch'^[A-Za-z0-9_:]+: test$'});if($unexpected.Count){throw 'Only exact terse test entries'};[string[]]$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''});[Array]::Sort($actual,[StringComparer]::Ordinal);$counts=@{domain=20;contracts=7;'helper-reconcile'=26;reconcile=26};$count=$counts[$kind];if($actual.Count-ne$count-or($actual-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'All exact names BEFORE any tests'};WriteNew (Join-Path $case ($kind+'-list.json')) @{names=$actual;sha256=$item.sha256;listed=$list};$lists[$kind]=$actual;$executables[$kind]=$exe} +WriteNew (Join-Path $case 'all-lists-verified.json') @{all_lists_before_any_tests=$true;manifest_sha256=$p.manifest_sha256};WriteNew (Join-Path $case 'tests-started.json') @{execute_tests_once=$true} +$reports=@{};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){$value=RunUnit $executables[$kind] '--test-threads=1';$value.sha256=$p.artifacts.$kind.sha256;$value.names=$lists[$kind];WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$value=@{id=$p.id;platform='windows';profile=$p.profile;manifest_sha256=$p.manifest_sha256;runner_sha256=$p.runner_sha256;scripts=$p.scripts;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;all_lists_before_any_tests=$true;bootstrap_administrator_units=$true;local_service_proof=$false;environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def check_result(manifest, platform, value, payload): + require(value.get('id') == payload['id'] and value.get('platform') == platform and value.get('profile') == PROFILE + and value.get('manifest_sha256') == payload['manifest_sha256'] and value.get('runner_sha256') == manifest['runner_sha256'] + and value.get('scripts') == manifest['scripts'] and value.get('product_state_writes') is False and value.get('rmm_jobs') is False + and value.get('environment_cleared') is True and value.get('all_lists_before_any_tests') is True and set(value['reports']) == set(KINDS[platform]), 'complete native original binding') + if platform == 'linux': require(value.get('network_namespace') is True, 'actual Linux netns proof') + else: require(value.get('linux_proof_sha256') == payload['linux_proof_sha256'] and value.get('local_service_proof') is False, 'original Linux proof and honest account scope') + for kind, item in value['reports'].items(): + require(item['names'] == EXPECTED[platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code'] == 0 and item['timed_out'] is False, 'all native exact test names/artifacts/exits') + raw = base64.b64decode(item['stdout_b64'], validate=True); errors = base64.b64decode(item['stderr_b64'], validate=True) + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native outputs') + check_output(raw, COUNTS[platform][kind]) + + +def execute(path, platform, access_root, case_id=None): + setup_state(); path = path.absolute(); manifest = verified(path, new_run=case_id is None) + prefix = access_settings(access_root, platform) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + payload = dict(id=case_id, profile=PROFILE, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], scripts=manifest['scripts'], + artifacts={kind: {k: manifest['artifacts'][platform][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS[platform]}, names=EXPECTED[platform]) + if platform == 'windows': + proof_path = path.parent / 'linux-passed.json'; proof = load(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'same-manifest real Linux proof FIRST') + linux_payload = dict(payload, id=proof['case_id'], artifacts={kind: {k: manifest['artifacts']['linux'][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS['linux']}, names=EXPECTED['linux']) + check_result(manifest, 'linux', proof['native'], linux_payload) + payload['linux_proof_sha256'] = sha(proof_path) + intent = dict(id=case_id, platform=platform, manifest_path=str(path), manifest_sha256=sha(path), runner_sha256=sha(__file__), + scripts=scripts(), access_root_sha256=hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), payload=payload) + save(folder / 'intent.json', intent) + save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) + run_read = False + else: + require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') + folder = STATE / case_id; private(folder, True); intent = load(folder / 'intent.json'); load(folder / 'started-local.json') + require(intent['id'] == case_id and intent['platform'] == platform and intent['manifest_path'] == str(path) + and intent['manifest_sha256'] == sha(path) and intent['runner_sha256'] == sha(__file__) and intent['scripts'] == scripts() + and intent['access_root_sha256'] == hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), 'immutable same-case readback') + payload = intent['payload']; run_read = True + rpc = linux_rpc if platform == 'linux' else windows_rpc + if not run_read: + result = rpc(prefix, dict(payload, action='prepare'), folder) + save(folder / 'prepared-remote.json', result); require(result == dict(id=case_id, prepared=True), 'fresh fixed case prepared') + for kind in KINDS[platform]: + destination = LINUX_REMOTE + '/' + case_id + '/' + kind if platform == 'linux' else '/C:/ProgramData/RustDiscoveryBootstrap/windows-msi-contracts-' + case_id + '/' + kind + '.exe' + upload(prefix, manifest['artifacts'][platform][kind], destination, folder, kind) + result = rpc(prefix, dict(payload, action='read' if run_read else 'run'), folder) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('status'): + require(run_read and result.get('id') == case_id and result['status'] in ('remote_case_absent', 'prepared_without_start', 'started_without_result'), 'fixed read-only incomplete status') + print(json.dumps(dict(verified=False, platform=platform, case_id=case_id, status=result['status'], retry_allowed=False))); return + check_result(manifest, platform, result, payload) + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): require(load(destination) == proof, 'preserve identical native proof') + else: save(destination, proof) + save(folder / 'verified.json', dict(verified=True, proof_sha256=sha(destination))) if not (folder / 'verified.json').exists() else require(load(folder / 'verified.json') == dict(verified=True, proof_sha256=sha(destination)), 'same verified receipt') + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values()), + manifest_sha256=sha(path), proof_sha256=sha(destination)))) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('build-local', 'linux', 'windows', 'readback-linux', 'readback-windows')) + parser.add_argument('--manifest', type=Path); parser.add_argument('--case'); parser.add_argument('--access-root', type=Path) + parser.add_argument('--execute-native-tests', action='store_true'); args = parser.parse_args() + if args.action == 'build-local': + require(not (args.manifest or args.case or args.access_root or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and args.access_root and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.access_root, args.case) + else: + require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'explicit new native platform run') + execute(args.manifest, args.action, args.access_root) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print(json.dumps(dict(error='WINDOWS_MSI_CONTRACT_QUALIFICATION_INCOMPLETE', error_class=type(error).__name__, details_suppressed=True))) + raise SystemExit(2) from None diff --git a/docs/windows-msi-contracts.md b/docs/windows-msi-contracts.md new file mode 100644 index 0000000..8db42ef --- /dev/null +++ b/docs/windows-msi-contracts.md @@ -0,0 +1,83 @@ +# Gemeinsame MSI-Verträge: Linux-first-Unitpack + +Der neue Runner `deploy/windows_msi_contract_acceptance.py` gehört zum isolierten +Worktree `rust-discovery-windows-msi-integration`. Historische M3-, Profil- und +NoStart-Runner sowie ihre ursprünglichen Manifeste bleiben unverändert. + +Dieser Pack prüft gemeinsame Daten-/Autorisierungs-/Ledger-/Syncverträge und die +bestehenden selbstisolierten Unixhelper-Dateiguards. Er installiert kein MSI, +startet keinen RMM-Dienst, verändert keine Produktjournale oder Identitäten und +erzeugt keine Jobs oder Genehmigungen. Insbesondere ist er kein tatsächlicher +SYSTEM-, Clock-, Cache-, Download-, Installer- oder WebUI-Wirkungsnachweis. + +## Exakte Listen und Quellen + +Die zunächst tatsächlich lokal aus den Release-Testprogrammen ermittelten +Linuxlisten enthalten Domain20, Corecontracts8, Helpersync26, Agentsync26 und +Unixhelper21: insgesamt101 Fälle. Der zusätzliche MSI-Corevertrag heißt +`historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority`. +Die Windowsliste ist bis zur tatsächlichen nativen Ausgabe ausdrücklich +**erwartet**:20+7+26+26=79. Nur der Unix-OpenSSL-Vergleich und die gesamte +Unixhelper-Dateiguardsuite entfallen dort. Es gibt keine Mindestzahlen oder +Überspringen/Filtern von Fällen zur Erfüllung dieser Zahlen. + +Das Manifest bindet68 Quellen einschließlich vollständiger Domain-/Core-/ +Unixhelper-/Linuxadapterquellen, Cargo-/Protoinputs, eng benötigter Fixtures, +des neuen Runners und seiner Bootstraptransportquellen vor/nach dem Build. +Die13 separat geprüften Domain-/Backend-/SQL-Pins sind exakt an den lokalen +Qualifikationsbeleg `b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74` +gebunden. Diese Backendpins sind ausschließlich Quellenbindung: der Unitpack +führt weder PostgreSQL noch einen Server aus. Webquellen sind kein Compilerinput +und gehören zum unabhängigen UI-Nachweis ihres Owners. + +## Tatsächlich lokal ausgeführt + +Linux- und Windows-GNU-Clippy der ausgewählten Pakete mit allen Targets und +`-D warnings`, Release-Linken der fünf Linux- und vier Windows-Testprogramme, +Prüfung beider Dateiformate und der unveränderten Original-Links waren erfolgreich. +Alle fünf tatsächlichen Linux-Lieferdateien wurden mit vollständiger Liste und +101 erfolgreichen, ungefilterten Tests im isolierten Offlinecontainer geprüft. +Alle Linux-Dateien referenzieren maximal GLIBC2.34. Nur Auslieferungskopien wurden +von Debuginformationen befreit; `.linked`-Originale bleiben separat erhalten. + +Python-Syntax und PowerShell-AST sind geprüft. Enge lokale Driverguards verweigern +fehlende/doppelte/fremde Listen und übersprungene/gefilterte Resultate. Ein +Mocklauf ohne Zielaufruf verlor die Antwort nach dem alleinigen Ausführungsaufruf; +der nachfolgende gleichnamige Readback lud keine Artefakte erneut hoch und +startete keinen Test erneut. Beide Plattformen verlangen sämtliche tatsächlichen +Listen vor irgendeinem Unitfall. Teilbelege bleiben create-only erhalten. + +Geschütztes lokales Buildmanifest: +`devstate/windows-msi-contracts/build-484a3761-6c84-426f-968d-25a638e9c6f7/manifest.json`, +SHA256 `2d96a07be72f3d71a8097510eb5f647ced6577086a45417c599b772018c29a53`. +Die reine lokale Listenerhebung davor sowie Compiler-/Fehlerausgaben bleiben +privat erhalten. Es wurden durch diesen Arbeitsschritt keine VM-/SSH-Aufrufe, +Upload-, Server-, Dienst-, Registry- oder Installeraktionen ausgeführt. + +## Noch geplante native Abnahme + +Nach unabhängiger Prüfung führt Root zuerst dieselben Lieferdateien auf der +tatsächlichen Linux-VM aus. Erst deren erfolgreicher, unveränderlich an dasselbe +Manifest gebundener Beleg erlaubt die Windows-Unitphase. Native Ausführung ist +ein ausdrücklicher CLI-Schritt mit dem ursprünglichen Main-`--access-root` nur +für den vorhandenen gepinnten SSH-Transport. Zugangsdaten und Main-`devstate` +werden weder kopiert noch verlinkt; alle neuen lokalen Belege bleiben im Worktree. + +Beide Zielseiten erhalten eigene create-only Bootstrapfälle unter +`windows-msi-contracts`. Vor Tests müssen alle fünf beziehungsweise vier +nativen Listen exakt stimmen. Antwortverlust erlaubt ausschließlich denselben +Fall zu lesen, auch bei fehlenden oder unvollständigen Ergebnissen; kein Retry +oder automatisches Resume. Unter Linux laufen die Units zusätzlich in einem +neuen Netzwerknamespace mit begrenztem Prozess-/Ausgabebudget; ihre temporären +Dateien gehören zum eigenen Bootstrapfall. Windows läuft ehrlich als +Bootstrapadministrator, nicht als Produkt-LocalService oder SYSTEM-Executor. + +Auf der tatsächlichen Cloud-Linux-VM anschließend alle101Prüfungen ohne Skips +bestanden: Fall `d015f861-e459-4b82-8ce5-460c3e4c570c`, gemeinsames Manifest +`2d96a07be72f3d71a8097510eb5f647ced6577086a45417c599b772018c29a53`, +Linuxproof-SHA256 +`09fa24e32cd3f3563e80e91265c6cc7c612321b0fdd1febe7aef3ab6de53cf2a`. +Windows79 weiterhin **geplant, nicht nativ ausgeführt**. Der Linux-Unit-PASS +ist keine Installation-/Upgrade-/Noop-Abnahme: jede tatsächliche +Paketwirkung benötigt weiterhin ihren eigenen unveränderlichen Antrag und die +Genehmigung in der WebUI. diff --git a/docs/windows-msi-integration.md b/docs/windows-msi-integration.md index fc41ae1..ad1aa8f 100644 --- a/docs/windows-msi-integration.md +++ b/docs/windows-msi-integration.md @@ -46,7 +46,7 @@ Lokaler Beleg `devstate/windows-msi-integration/local-qualification.json`, SHA256 `b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74`, bindet13Quelldateien. Unabhängiger Quellenreview ohne weiteren konkreten Blocker. -Weiter offen: Linux-first-Nativevertragsnachweise, tatsächliche WebUI-Abnahme, +Weiter offen: Windows-Nativevertragsnachweise, tatsächliche WebUI-Abnahme, Windowsdownload und geschützter SYSTEM-Cache, Helper-/Runtimeverdrahtung und jeweils echte WebUI-genehmigte Installation/Upgrade/Noops. Keine Capability, Helperpolicy oder Clock-/Jobkonfiguration auf der VM wurde geöffnet. Die @@ -78,3 +78,13 @@ SHA256 `f84d4962ab9a6ef64ef0327fcf7c524efb0bd4064d2416f174a9dfffb524a8a1`. Der ursprüngliche `fe02…`-Beleg bleibt historisch und ist ausdrücklich wegen des Codecfehlers abgelöst. Kein Clouddeployment, Browser-Nativlauf, Installer- oder tatsächlicher WebUI-Freigabenachweis durch diese Quelltests. + +## Tatsächlicher Linux-first-Vertragsnachweis + +Auf der tatsächlichen Cloud-Linux-Test-VM bestanden anschließend alle101 +ungefilterten Prüfungen ohne Skips: Domain20, Corecontracts8, HelperSync26, +AgentSync26 und Unixhelper21. Fall `d015f861-e459-4b82-8ce5-460c3e4c570c`, +Manifest `2d96a07b…`, Proof `09fa24e3…`. Nur eigene isolierte Unitprogramme, +kein RMM-Job, Dienst-/Agentdeployment, Produktjournal-/Schlüsselzugriff oder +Cloudserver-/MSI-/WebUI-Nativnachweis. Windows79 sind weiterhin erwartet und +noch nicht nativ ausgeführt. [Genauer Runnervertrag](windows-msi-contracts.md).