142 lines
7.4 KiB
Python
142 lines
7.4 KiB
Python
# Exact r39 reconciliation; this code is embedded in the root-owned runner.
|
|
CMS_RECON_ID = 'dc-cms-r39-runtime-reconciliation-v2-20261104-001'
|
|
CMS_RECON_ENTRY = 'deployment/dc-cms-r39-runtime-reconciliation-v2.json'
|
|
CMS_RECON_LEGACY_SHA = 'e50d894ff1292c8250b5c1a8eb97d5e4f7f8522a0157fac393b5cf82ced5aa1f'
|
|
CMS_DNS_FIELDS = ('Dns','DnsOptions','DnsSearch')
|
|
|
|
|
|
def cms_reconciliation_descriptor():
|
|
return {'schemaVersion':'nodedc.dc-cms.runtime-reconciliation.v2','releaseId':CMS_RECON_ID,
|
|
'failedReleaseId':CMS_AUDIT_FAILED_ID,'failedSha256':CMS_AUDIT_FAILED_SHA,
|
|
'backupId':CMS_AUDIT_BACKUP,'backupSha256':CMS_AUDIT_BACKUP_SHA,
|
|
'legacyRuntimeSha256':CMS_RECON_LEGACY_SHA,'runtimeFingerprintVersion':2,
|
|
'mode':'prove-restored-baseline-bind-order-empty-dns-only'}
|
|
|
|
|
|
def validate_cms_reconciliation_payload(payload, entries, identity):
|
|
if tuple(entries or ())!=(CMS_RECON_ENTRY,) or identity!=CMS_RECON_ID:
|
|
die('CMS reconciliation identity/entry mismatch')
|
|
marker=payload/CMS_RECON_ENTRY
|
|
if marker.is_symlink() or not marker.is_file():die('CMS reconciliation marker is unsafe')
|
|
expected=(json.dumps(cms_reconciliation_descriptor(),sort_keys=True,indent=2)+'\n').encode()
|
|
if marker.read_bytes()!=expected:die('CMS reconciliation descriptor mismatch')
|
|
|
|
|
|
def cms_runtime_fields_v2(fields):
|
|
result=json.loads(json.dumps(fields));host=result['host']
|
|
binds=host.get('Binds')
|
|
if not isinstance(binds,list) or not 1<=len(binds)<=8:
|
|
die('CMS v2 bind list is missing or outside bounds')
|
|
targets=[]
|
|
for bind in binds:
|
|
if not isinstance(bind,str) or '\0' in bind:die('CMS v2 bind is malformed')
|
|
parts=bind.split(':')
|
|
if len(parts) not in (2,3):die('CMS v2 bind is ambiguous')
|
|
for path in parts[:2]:
|
|
if not path.startswith('/') or str(PurePosixPath(path))!=path or '..' in PurePosixPath(path).parts:
|
|
die('CMS v2 bind path is not canonical')
|
|
if len(parts)==3 and not parts[2]:die('CMS v2 bind options are ambiguous')
|
|
targets.append(parts[1])
|
|
if len(set(targets))!=len(targets):die('CMS v2 duplicate bind target')
|
|
host['Binds']=sorted(binds)
|
|
# Docker 24 uses len(...) > 0 for these three settings. Nonempty lists and
|
|
# their order are untouched; every other HostConfig field remains exact.
|
|
for key in CMS_DNS_FIELDS:
|
|
if key not in host:die('CMS v2 DNS field is missing')
|
|
value=host[key]
|
|
if value is None:host[key]=[]
|
|
elif not isinstance(value,list) or any(not isinstance(v,str) for v in value):
|
|
die('CMS v2 DNS field is malformed')
|
|
return result
|
|
|
|
|
|
def cms_runtime_v2_from_sample(sample):
|
|
fields=cms_runtime_fields_v2(sample['fields'])
|
|
digests={section+'.'+key:cms_audit_digest(value) for section in ('host','config')
|
|
for key,value in fields[section].items()}
|
|
digests.update({key:cms_audit_digest(fields[key]) for key in ('mounts','networks')})
|
|
return {**sample['runtime'],'configSha256':cms_audit_digest(fields),'fieldSha256':digests}
|
|
|
|
|
|
def cms_runtime_snapshot_v2():
|
|
return cms_runtime_v2_from_sample(cms_audit_sample())
|
|
|
|
|
|
def cms_r39_representation_proof(fields, expected):
|
|
cms_runtime_fields_v2(fields) # Validate paths and types before enumeration.
|
|
if not isinstance(expected,str) or not re.fullmatch('[a-f0-9]{64}',expected):
|
|
die('CMS reconciliation predecessor digest is invalid')
|
|
if len(fields['host']['Binds'])!=2 or any(fields['host'][k] not in (None,[]) for k in CMS_DNS_FIELDS):
|
|
die('CMS r39 proof requires exactly two binds and empty DNS settings')
|
|
work=json.loads(json.dumps(fields));checked=0
|
|
for binds in itertools.permutations(fields['host']['Binds']):
|
|
work['host']['Binds']=list(binds)
|
|
for dns in itertools.product((None,[]),repeat=3):
|
|
for key,value in zip(CMS_DNS_FIELDS,dns):work['host'][key]=value
|
|
checked+=1
|
|
if cms_audit_digest(work)==expected:return checked
|
|
die('CMS runtime differs beyond bind order and empty DNS representation')
|
|
|
|
|
|
def cms_r39_failed_row():
|
|
return {'artifact':'nodedc-'+CMS_AUDIT_FAILED_ID+'.tgz.20261104-223125',
|
|
'backup_id':CMS_AUDIT_BACKUP,'component':'dc-cms-release',
|
|
'failed_at':'2026-11-04T19:34:46+00:00','id':CMS_AUDIT_FAILED_ID,
|
|
'message':'CMS environment, mounts, ports or network topology changed',
|
|
'rollback_status':'failed:DeployError','sha256':CMS_AUDIT_FAILED_SHA,
|
|
'started_apply':True,'status':'failed'}
|
|
|
|
|
|
def cms_r39_evidence():
|
|
rows=[r for r in load_state(FAILED_STATE_FILE) if r.get('id')==CMS_AUDIT_FAILED_ID or r.get('sha256')==CMS_AUDIT_FAILED_SHA]
|
|
if rows!=[cms_r39_failed_row()]:die('CMS reconciliation failed receipt mismatch')
|
|
before=cms_audit_evidence()
|
|
if before['runtime']['configSha256']!=CMS_RECON_LEGACY_SHA:
|
|
die('CMS reconciliation saved runtime digest differs')
|
|
return before
|
|
|
|
|
|
def cms_r39_restored_baseline(before):
|
|
samples=[cms_audit_sample() for _ in range(3)]
|
|
if any(sample!=samples[0] for sample in samples):die('CMS runtime changed during reconciliation')
|
|
sample=samples[0];old=before['runtime'];runtime=sample['runtime']
|
|
if runtime['imageId']!=old['imageId'] or runtime['imageRef']!=old['imageRef']:
|
|
die('CMS reconciliation original image mismatch')
|
|
checked=cms_r39_representation_proof(sample['fields'],old['configSha256'])
|
|
result=json.loads(json.dumps(before));result['runtimeFingerprintVersion']=2
|
|
result['runtime']=cms_runtime_v2_from_sample(sample)
|
|
print('cms_reconciliation_proof=exact-legacy-sha;changed-representation=bind-order,empty-dns;'
|
|
'samples=3;candidates='+str(checked)+';fingerprint=v2')
|
|
return result
|
|
|
|
|
|
def preflight_cms_reconciliation(payload, entries, identity, *, marker_installed=False):
|
|
require_root();validate_cms_reconciliation_payload(payload,entries,identity)
|
|
marker=CMS_ROOT/CMS_RECON_ENTRY
|
|
if not marker_installed and (marker.exists() or marker.is_symlink()):
|
|
die('CMS reconciliation marker already exists')
|
|
before=cms_r39_restored_baseline(cms_r39_evidence())
|
|
accept_cms_release(before,rollback=True)
|
|
print('cms_reconciliation_accept=restored-baseline-proven;services=none;build=none;runtime-mutation=none')
|
|
return before
|
|
|
|
|
|
def cms_r39_reconciled(row):
|
|
if row!=cms_r39_failed_row():return False
|
|
marker=CMS_ROOT/CMS_RECON_ENTRY
|
|
records=[r for r in load_state(STATE_FILE) if r.get('id')==CMS_RECON_ID]
|
|
if not records:
|
|
# During canonical apply the marker may precede its success receipt.
|
|
# Neither marker presence nor an in-progress apply grants queue release.
|
|
return False
|
|
if len(records)!=1 or records[0].get('component')!='dc-cms-runtime-reconciliation' or records[0].get('status')!='ok':
|
|
die('CMS reconciliation applied receipt mismatch')
|
|
validate_cms_reconciliation_payload(CMS_ROOT,(CMS_RECON_ENTRY,),CMS_RECON_ID)
|
|
with tempfile.TemporaryDirectory(prefix='cms-r39-marker-',dir=TMP_DIR) as tmp:
|
|
manifest,entries,payload=load_artifact(device_release_applied_archive(records[0]),Path(tmp))
|
|
if manifest.get('component')!='dc-cms-runtime-reconciliation' or manifest.get('id')!=CMS_RECON_ID:
|
|
die('CMS reconciliation applied archive identity mismatch')
|
|
if sha256_file(payload/CMS_RECON_ENTRY)!=sha256_file(marker):die('CMS reconciliation marker drift')
|
|
cms_r39_evidence()
|
|
return True
|