Files
rust-discovery/deploy/windows_certificate_acceptance.py
T
2026-11-16 14:24:44 +01:00

74 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Render one local fixed Windows certificate plan; performs no network or target action.
Actual bootstrap/token staging, MCP submit, UI approval and fixture revocation
are separate coordinated actions. The permanent enrolled device is never revoked.
"""
import argparse
from datetime import timedelta
import hashlib
import json
import os
from pathlib import Path
import uuid
import cloud_mvp_acceptance as cloud
SDK_SHA256 = 'c6b581b60ce803318ff946cd3cc8306aa8589d6bff6b022c0bf1d48eb8f5a8e1'
SDK = 'C:\\Program Files\\RustDiscoveryCertificateAcceptance\\' + SDK_SHA256 + '\\WindowsCertificateAcceptanceSDK.exe'
ROOT = 'C:\\ProgramData\\RustDiscovery\\certificate-acceptance'
def script(case_id, mode):
assert str(uuid.UUID(case_id)) == case_id and mode in ('renew', 'revoked-read')
return ("$ErrorActionPreference = 'Stop'\r\n"
"$exe = '" + SDK + "'\r\n"
"$guard = [IO.File]::Open($exe,'Open','Read','Read')\r\n"
"try {\r\n"
" $sha = [Security.Cryptography.SHA256]::Create()\r\n"
" try { $hash = ([BitConverter]::ToString($sha.ComputeHash($guard))).Replace('-','').ToLowerInvariant() } finally { $sha.Dispose() }\r\n"
" if ($hash -cne '" + SDK_SHA256 + "') { throw 'Certificate SDK hash mismatch' }\r\n"
" $lines = @(& $exe '" + mode + "' '" + case_id + "')\r\n"
" if ($LASTEXITCODE -ne 0) { throw 'Certificate fixture failed; preserve case and do not retry' }\r\n"
" $result = ($lines -join [Environment]::NewLine) | ConvertFrom-Json\r\n"
" if ($result.verified -ne $true -or $result.execution_sid -cne 'S-1-5-19' -or $result.permanent_identity_read -ne $false) { throw 'Exact LocalService fixture proof required' }\r\n"
" Write-Output 'CertificateAcceptance:" + mode + ":verified'\r\n"
"} finally { $guard.Dispose() }\r\n")
def request(case_id, mode):
start = cloud.contract.now()
text = script(case_id, mode)
return dict(tenant_id=cloud.TENANT, action='run_script', targets=dict(asset_ids=[cloud.WINDOWS_ASSET]),
parameters=dict(execution_identity='agent_service', timeout_seconds=120, max_output_bytes=16384,
arguments=[], expected_stdout='CertificateAcceptance:' + mode + ':verified\r\n',
variants=[dict(os_family='windows', interpreter='powershell', encoding='utf-8', script_utf8=text,
windows_execution_profile='powershell_51_remote_signed')]),
schedule=cloud.contract.schedule(start, start + timedelta(minutes=15)),
rollout=dict(max_parallel=1, halt_after_failures=1),
reason='Separate Windows certificate fixture ' + mode + '; permanent identity unchanged; no retry')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--case-id', required=True)
parser.add_argument('--mode', choices=('renew', 'revoked-read'), required=True)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
if str(uuid.UUID(args.case_id)) != args.case_id or uuid.UUID(args.case_id).int == 0:
parser.error('canonical nonempty fixture UUID required')
os.umask(0o077)
rendered = request(args.case_id, args.mode)
result = dict(schema_version=1, prepared_only=True, executed=False, fixture_provisioned=False,
case_id=args.case_id, mode=args.mode, sdk_sha256=SDK_SHA256,
script_sha256=hashlib.sha256(script(args.case_id,args.mode).encode()).hexdigest(),
public_receipt=ROOT + '\\' + args.case_id + '\\' + ('renewal-result.json' if args.mode == 'renew' else 'revocation-result.json'),
bootstrap_input_keys=['case_id','ca_base64','ca_sha256','enrollment_token'], request=rendered)
with args.output.open('x') as out:
json.dump(result,out,indent=2)
print(args.output)
if __name__ == '__main__':
main()