138 lines
7.7 KiB
Python
Executable File
138 lines
7.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Stage a build on the development server, sign there, and publish over the loopback HTTPS management API."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
|
|
import agent_release
|
|
from dev_stage import ssh_prefix
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
def on_server(options):
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import uuid
|
|
import tempfile
|
|
|
|
os.umask(0o077)
|
|
state = Path('/opt/rust-discovery/devstate/mvp-releases')
|
|
stage = Path.home() / 'rmm-dev-stage'
|
|
tool = stage / options['tool'] / 'agent_release.py'
|
|
if hashlib.sha256(tool.read_bytes()).hexdigest() != options['tool']:
|
|
raise ValueError('staged release tool differs')
|
|
build = state / 'builds' / options['label'] / str(options['sequence'])
|
|
build.mkdir(parents=True, exist_ok=True)
|
|
for name, digest in options['files'].items():
|
|
source = stage / digest / name
|
|
dest = build / name
|
|
if hashlib.sha256(source.read_bytes()).hexdigest() != digest:
|
|
raise ValueError('staged build differs: ' + name)
|
|
if not dest.exists():
|
|
with tempfile.NamedTemporaryFile(dir=build, delete=False) as tmp:
|
|
with source.open('rb') as f:
|
|
shutil.copyfileobj(f, tmp)
|
|
tmp.flush()
|
|
os.fsync(tmp.fileno())
|
|
try:
|
|
os.link(tmp.name, dest)
|
|
finally:
|
|
os.unlink(tmp.name)
|
|
if hashlib.sha256(dest.read_bytes()).hexdigest() != digest:
|
|
raise ValueError('sequence already contains different build: ' + name)
|
|
inputs = ['--artifacts', str(build), '--build-receipt', str(build / options['receipt'])]
|
|
subprocess.run(['python3', '-I', str(tool), 'prepare', *inputs, '--state', str(state), '--gateway', options['gateway'],
|
|
'--service-uid','10001','--minimum-updater-version',str(options['minimum']),'--sequence',str(options['sequence']),'--version',options['version']],check=True)
|
|
if options['label'].startswith('windows'):
|
|
import re
|
|
import uuid
|
|
public = Path('/etc/rmm-time-source/time-public-key.json')
|
|
if public.exists():
|
|
contents = json.loads(public.read_bytes())
|
|
if (set(contents) != {'schema_version','algorithm','server_instance','key_id','key_generation','public_key_hex'}
|
|
or contents['algorithm'] != 'Ed25519' or contents['schema_version'] != 1 or uuid.UUID(contents['server_instance']).int == 0
|
|
or not re.fullmatch('[A-Za-z0-9_-]{1,64}', contents['key_id']) or not re.fullmatch('[0-9a-f]{64}', contents['public_key_hex'])
|
|
or type(contents['key_generation']) is not int or contents['key_generation'] <= 0):
|
|
raise ValueError('time broker public key information invalid')
|
|
expected = {'server_instance':contents['server_instance'],'key_generation':contents['key_generation'],
|
|
'public_keys_hex':{contents['key_id']:contents['public_key_hex']}}
|
|
path = state/'config/windows-time-trust.json'
|
|
if not path.exists():
|
|
with tempfile.NamedTemporaryFile(mode='w', dir=path.parent,delete=False) as tmp:
|
|
json.dump(expected,tmp,separators=(',',':'))
|
|
os.fchmod(tmp.fileno(),0o600)
|
|
os.fchown(tmp.fileno(),10001,10001)
|
|
tmp.flush()
|
|
os.fsync(tmp.fileno())
|
|
try:
|
|
os.link(tmp.name,path)
|
|
finally:
|
|
os.unlink(tmp.name)
|
|
if json.loads(path.read_bytes()) != expected:
|
|
raise ValueError('time trust changed; verify rotation before new publication')
|
|
if options['prepare_only']:
|
|
return
|
|
# Use the ordinary development catalog permission and CSRF-protected API.
|
|
# Credentials are read within the dev server and never returned over SSH.
|
|
cid = subprocess.check_output(['docker','ps','-q','--filter','label=com.docker.compose.project=rust-discovery-server',
|
|
'--filter','label=com.docker.compose.service=api']).decode().strip().splitlines()
|
|
if len(cid) != 1:
|
|
raise ValueError('expected one running development API')
|
|
env = dict(x.partition('=')[::2] for x in json.loads(subprocess.check_output(['docker','inspect',cid[0]]))[0]['Config']['Env'])
|
|
if env.get('RMM_PROFILE') != 'development' or env.get('RMM_DEV_AUTH') != 'true':
|
|
raise ValueError('explicit dev API required')
|
|
with tempfile.NamedTemporaryFile(mode='w', dir=state) as auth:
|
|
json.dump({'username':env['RMM_DEV_USERNAME'],'password':env['RMM_DEV_PASSWORD']},auth)
|
|
auth.flush()
|
|
label=options['label']
|
|
tenant=str(uuid.UUID(options['tenant']))
|
|
pin=(state/'config'/(label+'-bootstrap.json')).with_suffix('.'+tenant+'.json')
|
|
if options['rotate']:
|
|
selection=['--rotate-bootstrap-from',options['rotate'],'--bootstrap-state',str(state)]
|
|
elif pin.is_file():
|
|
sha=json.loads(pin.read_bytes())['current']['sha256']
|
|
selection=['--bootstrap-worker',str(state/'objects'/tenant/sha)]
|
|
else:
|
|
selection=['--bootstrap-worker',str(state/'bootstrap-workers'/label)]
|
|
subprocess.run(['python3','-I',str(tool),'publish',*inputs,'--tenant',options['tenant'],
|
|
'--envelope',str(state/'prepared'/options['label']/str(options['sequence'])/'envelope.json'),
|
|
'--origin','https://localhost:18443','--ca','/opt/rust-discovery/devstate/api-pki/ca.pem',
|
|
'--dev-login-file',auth.name,*selection],check=True)
|
|
|
|
def main():
|
|
import inspect
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--artifacts',type=Path,required=True)
|
|
parser.add_argument('--build-receipt',type=Path,required=True)
|
|
parser.add_argument('--sequence',type=int,required=True)
|
|
parser.add_argument('--version',required=True)
|
|
parser.add_argument('--minimum-updater-version',type=int,default=1)
|
|
parser.add_argument('--rotate-bootstrap-from',help='Rotate only this tenant from the given previous worker SHA-256')
|
|
parser.add_argument('--gateway',default='https://discovery.ease-technologies.de')
|
|
parser.add_argument('--tenant',default='11111111-1111-4111-8111-111111111111')
|
|
parser.add_argument('--prepare-only',action='store_true')
|
|
args=parser.parse_args()
|
|
platform, _ = agent_release.binaries(args.artifacts,args.build_receipt)
|
|
files=[*(args.artifacts/name for name in agent_release.FILES[platform]),args.build_receipt]
|
|
tool=Path(agent_release.__file__).resolve()
|
|
if args.build_receipt.name in agent_release.FILES[platform]:
|
|
raise ValueError('build receipt and binary names must differ')
|
|
params={'files':{f.name:hashlib.sha256(f.read_bytes()).hexdigest() for f in files},'tool':hashlib.sha256(tool.read_bytes()).hexdigest(),
|
|
'receipt':args.build_receipt.name,'sequence':args.sequence,'version':args.version,'tenant':args.tenant,
|
|
'minimum':args.minimum_updater_version,'rotate':args.rotate_bootstrap_from,'gateway':args.gateway,'prepare_only':args.prepare_only,'label':{agent_release.LINUX:'linux',
|
|
agent_release.WINDOWS:'windows',agent_release.WINDOWS_GNU:'windows-gnu'}[platform]}
|
|
subprocess.run([sys.executable,str(ROOT/'tools/dev_stage.py'),'server',*map(str,files),str(tool)],check=True)
|
|
command=inspect.getsource(on_server)+'\non_server('+repr(params)+')\n'
|
|
subprocess.run([*ssh_prefix('server'),shlex.join(['python3','-I','-'])],input=command,text=True,check=True)
|
|
|
|
if __name__=='__main__':
|
|
main()
|