389 lines
27 KiB
Python
389 lines
27 KiB
Python
#!/usr/bin/env python3
|
|
"""Fixed qualified422 gateway deployment. Source-only until actual broker proof is pinned.
|
|
|
|
One owned deployment case; no build, UI rewrite, reset, enrollment or approval.
|
|
After a failed/lost reply only host-read of this same case is permitted.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import types
|
|
|
|
ROOT = Path('/opt/rust-discovery')
|
|
CASE_ID = 'fb87aa88-2b67-4cff-9c90-4cd713380889'
|
|
CASE = ROOT / 'devstate' / ('time-gateway-deploy-' + CASE_ID)
|
|
STAGE_ID = 'd11a6fb7-cfdd-41b2-87c5-b48a2cfd2e27'
|
|
STAGE = ROOT / 'devstate' / ('source-upgrade-' + STAGE_ID)
|
|
PROFILE = 'rmm.server-time-gateway.fixed-deploy.v1'
|
|
PIN_CONTROLLER_SHA = 'fac85dd0399009af36f625232daa5b67ab7fda391dbf95229d4237c307b142ca'
|
|
PIN_CONTROLLER = Path('/var/lib/rmm-time-source-operators') / PIN_CONTROLLER_SHA / 'pin_bootstrap.py'
|
|
IMAGE = 'sha256:422c2c28a91ac512e9317908f4d1fb22247eef857ca95d45e1b1880598b0d611'
|
|
OLD_IMAGE = 'sha256:6f8383f7c762ac77ed1013c94eeafa2b76838c00aec358a375f5847645ea87ec'
|
|
SOURCE_SHA = '4419f229599d827a63b69ded3c57b3a3fe4066a10d431368f68a2e415f77b3da'
|
|
SOURCE_RECORD_SHA = '521bd003500e1dc51b5ec430900aa0c2e17f9ec050b7fc077f0cbbdf58ba7c0f'
|
|
BUILD_RESULT_SHA = 'b8c0f61d054548753767c378e29019bd60f997980c2d407f8fd6e7b763047fdc'
|
|
BUILD_LOG_SHA = 'f820345ef15268b47f2487222dcc2a80e9d8849ad9be005f7097fb7dd4232a8f'
|
|
COMPOSE_SHA = 'd2044b0ee6d584f360990635bec149511d5a0623ec562976541900d751998818'
|
|
UI_SHA = '95d6f9058cc713c6af36fbd22ccc60678f081765a853b53d19ac6f0d219804f8'
|
|
POLICY_SHA = 'cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4'
|
|
OVERLAY_SHA = 'dbd08faea8f58c032562d822f4191c719f9a6828b00d6c368fc2596155a9a314'
|
|
UPGRADE_SHA = '2ed358fa20d74bbad20da728c1ef469e0b9613e5d53f41513402a01684c8fd62'
|
|
CLOUD_READ_RECEIPT_SHA = '438958c318a47b95d717a73f701ebd5e7287b08d4dd066870f62287d91f54a1a'
|
|
BROKER_START_CONTROLLER_SHA = 'e1458cb3feecd3d3302f2fccaff4850cfc8c403ee3314563f5f641e7c8bdfc39'
|
|
BROKER_START_RESULT_SHA = '8fc6ed5193f9802b7686a93523730c7e805595415c8e887552ffcf4224968d4d'
|
|
BROKER_START_READ_RECEIPT_SHA = '731527c0e30aaaddf386f414ffcdd11333000b9897b856cb994da1eccd255d54'
|
|
BROKER_SIGNATURE_CONTROLLER_SHA = 'a8afe8a2167f91e743e8c976b58d9f5227fa64914ad492ffc489b0dc98392585'
|
|
BROKER_SIGNATURE_RESULT_SHA = '38ed1a3079f20438fe07a57f1bfa5a37348bdec9fc7780d7822ca3d6967e5ea1'
|
|
BROKER_SIGNATURE_READ_RECEIPT_SHA = '4b442a9022f1ee56f8e851e25ad323d630b220cf47aa8169b83b9c4412c50b8d'
|
|
TIME_PUBLIC_PIN_SHA = 'c31d31d2d61f881d04f55f704abbbf98f4822650c016f39e39d4d2318bc3f6e2'
|
|
BROKER_START = Path('/var/lib/rmm-time-source-bootstrap/5b358997-3abc-43db-945f-f41855b4e3c0/broker-start-acceptance-v1')
|
|
OLD_IDS = {'api':'307c24bbc3a1249d598f0361780b7f742023dd65772f96e943c1b6af5685b4e9',
|
|
'gateway':'bbf4ce19c77c3f48eb90f3b9a1c3b9ef86eb1fd2f8cd3ad107a9ea6704d9cc6d',
|
|
'postgres':'3ce274d1794aa189309b24147d63a67ae8f420663bd021628fbe8bb9ed9ec4dc'}
|
|
WEB_FILES = ('assets/index-BALnnpV2.js','assets/index-Be1zm1OZ.css','assets/index-BmiTugvc.js',
|
|
'assets/index-BvPcFQTr.js','assets/index-CTgvl2jr.js','assets/index-DJcsgK_5.js','index.html')
|
|
RECORDS = ('intent.json','before.json','overlay-intent.json','overlay-installed.json',
|
|
'upgrade-tool-intent.json','upgrade-tool-installed.json','old-upgrade-tool.bin','reservation.json',
|
|
'tag-intent.json','tag-exit.json','tag.log','migrate-intent.json','migrate-exit.json','migrate.log',
|
|
'recreate-intent.json','recreate-exit.json','recreate.log','after.json','result.json','failure.json')
|
|
|
|
class GuardDenied(RuntimeError):
|
|
pass
|
|
|
|
def require(ok,tag):
|
|
if not ok:raise GuardDenied(tag)
|
|
|
|
def sha(raw):return hashlib.sha256(raw).hexdigest()
|
|
|
|
def load_pin_controller():
|
|
fd = os.open('/', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
try:
|
|
for part in PIN_CONTROLLER.parent.parts[1:]:
|
|
info = os.fstat(fd)
|
|
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_ancestry')
|
|
next_fd = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=fd)
|
|
os.close(fd)
|
|
fd = next_fd
|
|
info = os.fstat(fd)
|
|
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_parent')
|
|
file_fd = os.open(PIN_CONTROLLER.name, os.O_RDONLY | os.O_NOFOLLOW, dir_fd=fd)
|
|
with os.fdopen(file_fd, 'rb') as stream:
|
|
before = os.fstat(stream.fileno())
|
|
require(stat.S_ISREG(before.st_mode) and before.st_uid == before.st_gid == 0 and
|
|
before.st_nlink == 1 and stat.S_IMODE(before.st_mode) == 0o600 and
|
|
before.st_size <= 128 * 1024, 'reviewed_root_controller_file')
|
|
raw = stream.read(128 * 1024 + 1)
|
|
stable = lambda s: tuple(getattr(s, 'st_' + k) for k in
|
|
('dev', 'ino', 'uid', 'gid', 'mode', 'nlink', 'size', 'mtime_ns', 'ctime_ns'))
|
|
require(stable(before) == stable(os.fstat(stream.fileno())) ==
|
|
stable(os.stat(PIN_CONTROLLER.name, dir_fd=fd, follow_symlinks=False)) and
|
|
len(raw) == before.st_size and sha(raw) == PIN_CONTROLLER_SHA, 'exact_pin_controller_bytes')
|
|
finally:
|
|
os.close(fd)
|
|
pin = importlib.util.module_from_spec(importlib.util.spec_from_loader('rmm_peer_pin_guard', loader=None))
|
|
pin.__file__ = str(PIN_CONTROLLER)
|
|
exec(compile(raw, str(PIN_CONTROLLER), 'exec'), pin.__dict__)
|
|
held = pin.read_file(PIN_CONTROLLER, 128 * 1024)
|
|
require(held[0] == raw, 'same_loaded_pin_controller')
|
|
return pin, lambda: require(pin.read_file(PIN_CONTROLLER, 128 * 1024) == held, 'pin_controller_unchanged')
|
|
|
|
|
|
def loaded(pin,path,expected):
|
|
raw,metadata = pin.read_file(path,131072)
|
|
require(sha(raw) == expected,'exact_protected_deploy_dependency')
|
|
module = importlib.util.module_from_spec(importlib.util.spec_from_loader('rmm_fixed_gateway_dependency',loader=None))
|
|
module.__file__ = str(path)
|
|
exec(compile(raw,str(path),'exec'),module.__dict__)
|
|
return module,lambda:require(pin.read_file(path,131072) == (raw,metadata),'deploy_dependency_unchanged')
|
|
|
|
|
|
def create(pin,parent,name,value):
|
|
raw = value if type(value) is bytes else pin.encoded(value)
|
|
require(len(raw) <= 1024*1024,'bounded_private_deploy_record')
|
|
fd = os.open(name,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600,dir_fd=parent)
|
|
try:
|
|
view = memoryview(raw)
|
|
while view:
|
|
count = os.write(fd,view);require(count>0,'complete_deploy_record');view=view[count:]
|
|
os.fsync(fd)
|
|
finally:os.close(fd)
|
|
os.fsync(parent)
|
|
|
|
|
|
def containers(operator):
|
|
result = {}
|
|
template = '[{{json .Id}},{{json .Image}},{{json .Config.User}},{{json .HostConfig.PortBindings}},{{json .Mounts}},{{json .HostConfig.ReadonlyRootfs}},{{json .HostConfig.CapDrop}},{{json .HostConfig.SecurityOpt}},{{json .State.Running}},{{json .State.Pid}},{{json .State.StartedAt}},{{json .RestartCount}}]'
|
|
for role in ('api','gateway','postgres'):
|
|
result[role] = json.loads(operator.run(['docker','inspect','rust-discovery-server-'+role+'-1',
|
|
'--format',template],limit=65536))
|
|
for role,port in (('api','18443'),('gateway','18444')):
|
|
row = result[role]
|
|
require(len(row)==12 and row[2]=='10001:10001' and row[3]=={'18443/tcp':[{'HostIp':'127.0.0.1','HostPort':port}]}
|
|
and row[5] is True and row[6]==['ALL'] and row[7]==['no-new-privileges:true']
|
|
and row[8] is True and type(row[9]) is int and row[9]>1 and row[11]==0,'same_unprivileged_loopback_server_role')
|
|
pg=result['postgres']
|
|
require(pg[0]==OLD_IDS['postgres'] and pg[1]=='sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3'
|
|
and pg[3] in ({},None) and pg[8] is True
|
|
and any(mount.get('Type')=='volume' and mount.get('Name')=='rust-discovery-server_pgdata'
|
|
and mount.get('Destination')=='/var/lib/postgresql/data' for mount in pg[4]),'same_existing_private_postgres_container_volume')
|
|
return result
|
|
|
|
|
|
def file_tree(path,public=False):
|
|
result={}
|
|
for item in [path,*sorted(path.rglob('*'))]:
|
|
info=item.lstat()
|
|
require(not item.is_symlink() and (stat.S_ISDIR(info.st_mode) or stat.S_ISREG(info.st_mode))
|
|
and info.st_uid in (0,10001) and not info.st_mode&0o022,'protected_retained_ui_pki_key_path')
|
|
value={key:getattr(info,'st_'+key) for key in ('dev','ino','uid','gid','mode','nlink','size','mtime_ns','ctime_ns')}
|
|
if stat.S_ISREG(info.st_mode):
|
|
require(info.st_nlink==1,'single_link_retained_public_or_key_file')
|
|
if public:
|
|
require(info.st_size<=2*1024*1024,'bounded_public_web_asset')
|
|
value['sha256']=sha(item.read_bytes())
|
|
require(all(getattr(item.lstat(),'st_'+key)==value[key] for key in value if key!='sha256'),'stable_public_web_asset')
|
|
result[str(item)]=value
|
|
return result
|
|
|
|
|
|
def migrations(operator,cmd):
|
|
query = "BEGIN READ ONLY; SELECT COALESCE(json_agg(version ORDER BY version),'[]'::json) FROM _sqlx_migrations WHERE success; COMMIT;"
|
|
raw = operator.run(cmd+['exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1',
|
|
'-U','postgres','-d','rmm','-c',query],timeout=10,limit=8192)
|
|
value = json.loads(raw)
|
|
require(type(value) is list and all(type(item) is int for item in value),'actual_successful_migration_versions')
|
|
return value
|
|
|
|
|
|
class BrokerRuntime:
|
|
def __init__(self,pin,operator):
|
|
self.process=None
|
|
module_path=Path('/var/lib/rmm-time-source-operators')/BROKER_START_CONTROLLER_SHA/'broker_start_acceptance.py'
|
|
self.module,self.guard=loaded(pin,module_path,BROKER_START_CONTROLLER_SHA)
|
|
self.record=pin.read_file(BROKER_START/'result.json',65536)
|
|
require(sha(self.record[0])==BROKER_START_RESULT_SHA,'actual_original_successful_broker_start')
|
|
self.result=pin.parsed(self.record[0])
|
|
require(self.result['controller_sha256']==BROKER_START_CONTROLLER_SHA
|
|
and self.result['start_executed_once'] is True and self.result['effective_unit_qualified'] is True
|
|
and self.result['unix_request_sent'] is False and self.result['signed_attestation'] is False,
|
|
'actual_started_broker_not_time_signature_authority')
|
|
raw,metadata=pin.read_file(self.module.BINARY,2*1024*1024,0o500)
|
|
require(sha(raw)==self.module.BINARY_SHA and metadata==self.result['process']['image_metadata'],
|
|
'same_actual_started_broker_binary_inode')
|
|
self.proof=types.SimpleNamespace(pin=pin,binary=types.SimpleNamespace(metadata=metadata),
|
|
context=types.SimpleNamespace(original=types.SimpleNamespace(operator=operator)))
|
|
state=self.module.effective(self.module.typed_snapshot(self.proof),True)
|
|
self.process=self.module.ProcessProof(self.proof,state)
|
|
self.before=self.verify()
|
|
|
|
def verify(self):
|
|
self.guard()
|
|
require(self.proof.pin.read_file(BROKER_START/'result.json',65536)==self.record,'broker_start_record_unchanged')
|
|
state=self.module.effective(self.module.typed_snapshot(self.proof),True)
|
|
process=self.process.verify(state)
|
|
socket=self.module.socket_proof(self.proof,self.process)
|
|
require(process==self.result['process'] and socket==self.result['socket'],'same_actual_started_broker_process_and_listener')
|
|
return dict(process=process,socket=socket,start_result_sha256=BROKER_START_RESULT_SHA)
|
|
|
|
def close(self):
|
|
if self.process is not None:self.process.close()
|
|
|
|
|
|
def host_read(pin):
|
|
records={}
|
|
for name in RECORDS:
|
|
try:
|
|
raw,metadata=pin.read_file(CASE/name,8*1024*1024 if name.endswith('.log') else 1024*1024)
|
|
records[name]=dict(sha256=sha(raw),metadata=metadata,record=pin.parsed(raw) if name.endswith('.json') else None)
|
|
except FileNotFoundError:records[name]=None
|
|
print(pin.encoded(dict(profile=PROFILE,case=CASE_ID,read_only=True,records=records,no_effects=True)).decode(),end='')
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('command',choices=('host-preflight','host-deploy','host-read'))
|
|
args=parser.parse_args()
|
|
require(sys.flags.isolated==1 and os.geteuid()==0 and os.getegid()==0 and os.uname().machine=='aarch64','isolated_root_cloud_arm64_only')
|
|
require(not any(name in os.environ for name in ('DOCKER_HOST','DOCKER_CONTEXT','COMPOSE_PROJECT_NAME','COMPOSE_FILE','COMPOSE_PROFILES')),
|
|
'fixed_local_docker_compose_context')
|
|
os.umask(0o077);pin,pin_guard=load_pin_controller()
|
|
if args.command=='host-read':host_read(pin);return
|
|
operator,operator_guard=pin.load_operator()
|
|
require(type(BROKER_SIGNATURE_RESULT_SHA) is str and len(BROKER_SIGNATURE_RESULT_SHA)==64
|
|
and type(BROKER_SIGNATURE_READ_RECEIPT_SHA) is str and len(BROKER_SIGNATURE_READ_RECEIPT_SHA)==64,
|
|
'actual_signature_pass_and_read_must_be_pinned')
|
|
signature_path=BROKER_START.parent/'broker-signature-acceptance-v1/result.json'
|
|
signature_controller_path=Path('/var/lib/rmm-time-source-operators')/BROKER_SIGNATURE_CONTROLLER_SHA/'broker_signature_acceptance.py'
|
|
signature_controller=pin.read_file(signature_controller_path,131072)
|
|
require(sha(signature_controller[0])==BROKER_SIGNATURE_CONTROLLER_SHA,'actual_reviewed_signature_controller_source')
|
|
signature_record=pin.read_file(signature_path,65536)
|
|
require(sha(signature_record[0])==BROKER_SIGNATURE_RESULT_SHA,'actual_positive_broker_signature_result')
|
|
signature_value=pin.parsed(signature_record[0])
|
|
require(signature_value['profile']=='rmm.server-time-broker.unix-signature-acceptance.v1'
|
|
and signature_value['controller_sha256']==BROKER_SIGNATURE_CONTROLLER_SHA
|
|
and signature_value['start_result_sha256']==BROKER_START_RESULT_SHA
|
|
and signature_value['public_pin_sha256']==TIME_PUBLIC_PIN_SHA
|
|
and signature_value['two_actual_signatures_verified'] is True
|
|
and signature_value['private_signer_read'] is False and signature_value['clock_authority'] is False,
|
|
'actual_non_authoritative_signature_acceptance')
|
|
signature_phase=pin.directory(signature_path.parent)
|
|
try:pin.absence(signature_phase,'failure.json')
|
|
finally:os.close(signature_phase)
|
|
time_public=pin.read_file(Path('/etc/rmm-time-source/time-public-key.json'),8192)
|
|
require(sha(time_public[0])==TIME_PUBLIC_PIN_SHA,'same_separate_public_time_key_c31')
|
|
own=pin.read_file(Path(__file__),131072);own_dir=Path(__file__).parent
|
|
require(own_dir.name==sha(own[0]) and Path(__file__).name=='gateway_deploy.py','fixed_protected_gateway_deploy_controller')
|
|
overlay=pin.read_file(own_dir/'server.time-broker.compose.yaml',4096)
|
|
upgrade=pin.read_file(own_dir/'cloud_server_upgrade.py',131072)
|
|
require(len(overlay[0])==199 and sha(overlay[0])==OVERLAY_SHA and sha(upgrade[0])==UPGRADE_SHA,'fixed_public_overlay_upgrade_tool_inputs')
|
|
source=pin.read_file(STAGE/'source.json',1024*1024);built=pin.read_file(STAGE/'build-result.json',65536)
|
|
require(sha(source[0])==SOURCE_RECORD_SHA and sha(built[0])==BUILD_RESULT_SHA,'exact_qualified_original_arm_build_records')
|
|
source_value,built_value=pin.parsed(source[0]),pin.parsed(built[0])
|
|
require(source_value['source_sha256']==SOURCE_SHA and source_value['old_image']==OLD_IMAGE
|
|
and built_value=={'exit_code':0,'image_id':IMAGE},'qualified_422_source_build_not_moving_main')
|
|
require(sha(pin.read_file(STAGE/'build.log',16*1024*1024)[0])==BUILD_LOG_SHA,'actual_qualified_build_log')
|
|
require(all(sha(pin.read_file(ROOT/name,2*1024*1024,0o644)[0])==digest for name,digest in source_value['files'].items()),
|
|
'unchanged_published_209_source_files')
|
|
require(len(source_value['files'])==209,'exact_qualified_source_file_set')
|
|
require(json.loads(operator.run(['docker','image','inspect',IMAGE,'--format','[{{json .Id}},{{json .Architecture}},{{json .Os}}]']))
|
|
==[IMAGE,'arm64','linux'],'qualified_existing_native_image_no_build')
|
|
compose=pin.read_file(ROOT/'deploy/server.compose.yaml',8192,0o644)
|
|
require(sha(compose[0])==COMPOSE_SHA,'same_actual_server_compose')
|
|
cmd=['docker','compose','--env-file',str(ROOT/'devstate/.env'),'-f',str(ROOT/'deploy/server.compose.yaml')]
|
|
broker=BrokerRuntime(pin,operator);phase=deploy_fd=stage_fd=-1
|
|
try:
|
|
before_containers=containers(operator)
|
|
require(all(before_containers[role][0]==OLD_IDS[role] and before_containers[role][1]==OLD_IMAGE for role in ('api','gateway')),
|
|
'exact_original_6f_role_containers')
|
|
require(operator.run(['docker','image','inspect','rust-discovery-server:local','--format','{{.Id}}']).decode().strip()==OLD_IMAGE,
|
|
'old_runtime_tag_cas')
|
|
before_actual=operator.preflight()
|
|
web=file_tree(ROOT/'web/dist',True)
|
|
require(sorted(str(Path(name).relative_to(ROOT/'web/dist')) for name in web if stat.S_ISREG(web[name]['mode']))==list(WEB_FILES)
|
|
and web[str(ROOT/'web/dist/index.html')]['sha256']==UI_SHA,'all_original_web_assets_and_index_cas')
|
|
keys={str(path):file_tree(path) for path in (ROOT/'devstate/pki',ROOT/'devstate/api-pki',ROOT/'devstate/jobs',Path('/etc/rmm-time-source'),ROOT/'devstate/.env',ROOT/'devstate/root-policy/policy.json')}
|
|
policy_info=(ROOT/'devstate/root-policy/policy.json').lstat()
|
|
require(policy_info.st_uid==policy_info.st_gid==10001 and stat.S_IMODE(policy_info.st_mode)==0o600
|
|
and sha((ROOT/'devstate/root-policy/policy.json').read_bytes())==POLICY_SHA,'same_closed_root_policy_bytes')
|
|
before_migrations=migrations(operator,cmd)
|
|
require(before_migrations==list(range(1,15)),'actual_existing_migrations_one_through_fourteen')
|
|
deploy_fd=pin.directory(ROOT/'deploy');stage_fd=pin.directory(STAGE)
|
|
pin.absence(deploy_fd,'server.time-broker.compose.yaml')
|
|
pin.absence(stage_fd,'time-gateway-deploy-intent.json');pin.absence(stage_fd,'deploy-result.json')
|
|
try:old_upgrade=pin.read_file(ROOT/'deploy/cloud_server_upgrade.py',131072,0o644)
|
|
except FileNotFoundError:old_upgrade=None
|
|
binding=dict(profile=PROFILE,case=CASE_ID,stage=STAGE_ID,controller_sha256=sha(own[0]),image_id=IMAGE,
|
|
old_image=OLD_IMAGE,source_sha256=SOURCE_SHA,source_record_sha256=SOURCE_RECORD_SHA,build_result_sha256=BUILD_RESULT_SHA,
|
|
overlay_sha256=OVERLAY_SHA,upgrade_tool_sha256=UPGRADE_SHA,broker_start_result_sha256=BROKER_START_RESULT_SHA,
|
|
broker_start_read_receipt_sha256=BROKER_START_READ_RECEIPT_SHA,cloud_read_receipt_sha256=CLOUD_READ_RECEIPT_SHA,
|
|
broker_signature_result_sha256=BROKER_SIGNATURE_RESULT_SHA,
|
|
broker_signature_controller_sha256=BROKER_SIGNATURE_CONTROLLER_SHA,
|
|
broker_signature_read_receipt_sha256=BROKER_SIGNATURE_READ_RECEIPT_SHA,time_public_pin_sha256=TIME_PUBLIC_PIN_SHA)
|
|
baseline=dict(containers=before_containers,actual=before_actual,web=web,key_metadata=keys,migrations=before_migrations,broker=broker.before)
|
|
if args.command=='host-preflight':
|
|
print(pin.encoded(dict(binding,read_only=True,before=baseline,overlay_initially_absent=True,no_build=True,no_ui_write=True)).decode(),end='');return
|
|
parent=pin.directory(CASE.parent)
|
|
try:pin.absence(parent,CASE.name);os.mkdir(CASE.name,0o700,dir_fd=parent);os.fsync(parent)
|
|
finally:os.close(parent)
|
|
phase=pin.directory(CASE)
|
|
save=lambda name,value:create(pin,phase,name,value)
|
|
save('intent.json',dict(binding,once=True,no_retry=True,no_build=True,no_ui_write=True,migration_version=15))
|
|
save('before.json',dict(binding,witness=baseline))
|
|
def common():
|
|
pin_guard();operator_guard();broker.verify()
|
|
require(pin.read_file(signature_path,65536)==signature_record
|
|
and pin.read_file(signature_controller_path,131072)==signature_controller
|
|
and pin.read_file(Path('/etc/rmm-time-source/time-public-key.json'),8192)==time_public,
|
|
'signature_pass_and_public_time_key_unchanged')
|
|
require(pin.read_file(Path(__file__),131072)==own and pin.read_file(own_dir/'server.time-broker.compose.yaml',4096)==overlay
|
|
and pin.read_file(own_dir/'cloud_server_upgrade.py',131072)==upgrade,'retained_exact_controller_public_inputs')
|
|
require(pin.read_file(STAGE/'source.json',1024*1024)==source and pin.read_file(STAGE/'build-result.json',65536)==built
|
|
and pin.read_file(ROOT/'deploy/server.compose.yaml',8192,0o644)==compose,'retained_build_records_and_base_compose')
|
|
require(file_tree(ROOT/'web/dist',True)==web
|
|
and {str(path):file_tree(path) for path in (ROOT/'devstate/pki',ROOT/'devstate/api-pki',ROOT/'devstate/jobs',Path('/etc/rmm-time-source'),ROOT/'devstate/.env',ROOT/'devstate/root-policy/policy.json')}==keys,
|
|
'original_web_pki_keys_and_public_time_pins_preserved')
|
|
common()
|
|
reservation=dict(binding,original_case_only=True,no_generic_deploy_retry=True)
|
|
create(pin,stage_fd,'time-gateway-deploy-intent.json',reservation);save('reservation.json',reservation)
|
|
save('overlay-intent.json',dict(binding,path='deploy/server.time-broker.compose.yaml',create_only=True))
|
|
temp='.time-broker-'+CASE_ID+'.new';fd=os.open(temp,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644,dir_fd=deploy_fd)
|
|
with os.fdopen(fd,'wb') as out:out.write(overlay[0]);out.flush();os.fchmod(out.fileno(),0o644);os.fsync(out.fileno())
|
|
pin.absence(deploy_fd,'server.time-broker.compose.yaml')
|
|
os.link(temp,'server.time-broker.compose.yaml',src_dir_fd=deploy_fd,dst_dir_fd=deploy_fd,follow_symlinks=False)
|
|
os.unlink(temp,dir_fd=deploy_fd);os.fsync(deploy_fd)
|
|
installed_overlay=pin.read_file(ROOT/'deploy/server.time-broker.compose.yaml',4096,0o644)
|
|
require(installed_overlay[0]==overlay[0],'exact_created_gateway_only_overlay');save('overlay-installed.json',dict(binding,metadata=installed_overlay[1]))
|
|
if old_upgrade is not None:save('old-upgrade-tool.bin',old_upgrade[0])
|
|
save('upgrade-tool-intent.json',dict(binding,path='deploy/cloud_server_upgrade.py',old_sha256=sha(old_upgrade[0]) if old_upgrade else None))
|
|
temp='.cloud-upgrade-'+CASE_ID+'.new';fd=os.open(temp,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644,dir_fd=deploy_fd)
|
|
with os.fdopen(fd,'wb') as out:out.write(upgrade[0]);out.flush();os.fchmod(out.fileno(),0o644);os.fsync(out.fileno())
|
|
if old_upgrade is None:pin.absence(deploy_fd,'cloud_server_upgrade.py')
|
|
else:require(pin.read_file(ROOT/'deploy/cloud_server_upgrade.py',131072,0o644)==old_upgrade,'old_upgrade_tool_cas')
|
|
os.replace(temp,'cloud_server_upgrade.py',src_dir_fd=deploy_fd,dst_dir_fd=deploy_fd);os.fsync(deploy_fd)
|
|
installed_upgrade=pin.read_file(ROOT/'deploy/cloud_server_upgrade.py',131072,0o644)
|
|
require(installed_upgrade[0]==upgrade[0],'exact_persistent_optional_overlay_deploy_tool')
|
|
save('upgrade-tool-installed.json',dict(binding,metadata=installed_upgrade[1]))
|
|
final_cmd=cmd+['-f',str(ROOT/'deploy/server.time-broker.compose.yaml')]
|
|
def effect(name,argv,timeout):
|
|
common()
|
|
require(pin.read_file(ROOT/'deploy/server.time-broker.compose.yaml',4096,0o644)==installed_overlay
|
|
and pin.read_file(ROOT/'deploy/cloud_server_upgrade.py',131072,0o644)==installed_upgrade,'same_installed_overlay_and_upgrade_tool')
|
|
save(name+'-intent.json',dict(binding,argv=argv,once=True))
|
|
code=operator.long_command(argv,CASE/(name+'.log'),timeout)
|
|
save(name+'-exit.json',dict(binding,exit_code=code));require(code==0,'fixed_deploy_command_failed_original_read_only')
|
|
require(containers(operator)==before_containers and migrations(operator,cmd)==before_migrations,'last_original_runtime_and_migrations_cas')
|
|
effect('tag',['docker','tag',IMAGE,'rust-discovery-server:local'],10)
|
|
require(operator.run(['docker','image','inspect','rust-discovery-server:local','--format','{{.Id}}']).decode().strip()==IMAGE,'new_fixed_runtime_tag')
|
|
effect('migrate',final_cmd+['run','--rm','--no-deps','migrate'],25)
|
|
effect('recreate',final_cmd+['up','-d','--no-deps','--force-recreate','--no-build','api','gateway'],25)
|
|
after_containers=containers(operator);after_actual=operator.preflight();after_migrations=migrations(operator,final_cmd)
|
|
require(after_containers['postgres']==before_containers['postgres'],'same_postgres_container_identity_mounts_runtime')
|
|
for role in ('api','gateway'):
|
|
old,new=before_containers[role],after_containers[role]
|
|
require(new[0]!=old[0] and new[1]==IMAGE and new[2:4]==old[2:4] and new[5:9]==old[5:9],
|
|
'only_qualified_server_role_image_and_process_generation_changed')
|
|
expected=list(old[4])
|
|
if role=='gateway':
|
|
require(any(mount.get('Source')=='/run/rmm-time-source' and mount.get('Destination')=='/run/rmm-time-source'
|
|
and mount.get('Type')=='bind' and mount.get('RW') is False for mount in new[4]),'actual_gateway_only_readonly_broker_mount')
|
|
filtered=[mount for mount in new[4] if mount.get('Destination')!='/run/rmm-time-source']
|
|
else:filtered=new[4]
|
|
require(sorted(filtered,key=lambda item:item['Destination'])==sorted(expected,key=lambda item:item['Destination']),
|
|
'all_existing_role_mounts_preserved_no_api_time_mount')
|
|
for key in ('builder','boot_id','chrony_observation','chrony_image_metadata'):
|
|
require(before_actual[key]==after_actual[key],'same_boot_chrony_generation_image_builder')
|
|
old_chrony=pin.observation_key(before_actual)['chrony_binding'];new_chrony=pin.observation_key(after_actual)['chrony_binding']
|
|
require(old_chrony==new_chrony,'same_actual_chrony_process_cgroup_socket_binding')
|
|
require(after_migrations==list(range(1,16)),'actual_successful_additive_migrations_through_fifteen')
|
|
common()
|
|
save('after.json',dict(binding,witness=dict(containers=after_containers,actual=after_actual,migrations=after_migrations,broker=broker.verify())))
|
|
save('result.json',dict(binding,deployed=True,migrations=list(range(1,16)),web_assets_bytes_and_inodes_preserved=True,
|
|
pki_keys_pg_container_volume_preserved=True,
|
|
immutable_corehistory_witness='external_root_before_after_receipts_required',gateway_only_readonly_broker_mount=True,
|
|
approvals_created=False,rmm_jobs_created=False,source_rebuilt=False,time_signed=False))
|
|
print(pin.encoded(dict(binding,deployed=True,no_ui_write=True,no_build=True)).decode(),end='')
|
|
except Exception as error:
|
|
if phase>=0:
|
|
tag=str(error) if isinstance(error,GuardDenied) and 0<len(str(error))<=128 and all(c in 'abcdefghijklmnopqrstuvwxyz0123456789_' for c in str(error)) else 'unexpected_fixed_gateway_deploy_exception'
|
|
create(pin,phase,'failure.json',dict(profile=PROFILE,case=CASE_ID,fixed_tag=tag,failed=True,read_original_case_only=True,no_automatic_rollback=True))
|
|
raise
|
|
finally:
|
|
broker.close()
|
|
for fd in (phase,deploy_fd,stage_fd):
|
|
if fd>=0:os.close(fd)
|
|
|
|
|
|
if __name__=='__main__':
|
|
try:main()
|
|
except Exception as error:
|
|
tag=str(error) if isinstance(error,GuardDenied) and 0<len(str(error))<=128 and all(c in 'abcdefghijklmnopqrstuvwxyz0123456789_' for c in str(error)) else 'unexpected_fixed_gateway_deploy_exception'
|
|
print(json.dumps(dict(profile=PROFILE,case=CASE_ID,failed=True,fixed_tag=tag,read_original_case_only=True)))
|
|
raise SystemExit(1)
|
|
|