Files
rust-discovery/tools/dev_linux_helper_rollover.py
T

277 lines
14 KiB
Python

#!/usr/bin/env python3
"""Rotate the existing Linux development helper after the signed agent pair has switched."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import re
import shlex
import stat
import subprocess
import sys
import tempfile
import time
import uuid
ROOT = Path('/opt/rust-discovery-client')
CONFIG = Path('/etc/rmm-discovery-managed-scripts')
UNIT = Path('/etc/systemd/system/rmm-discovery-managed-script-helper.service')
AGENT = 'rmm-discovery-agent.service'
TIMER = 'rmm-discovery-update.timer'
POLICY = Path('/opt/rust-discovery/devstate/managed-scripts/policy.json')
HASH = re.compile(r'[0-9a-f]{64}')
PINS = ('helper_binary_sha256', 'helper_policy_sha256')
def require(test, error):
if not test:
raise ValueError(error)
def encoded(obj):
return (json.dumps(obj, separators=(',', ':')) + '\n').encode()
def protected(path, uid=0):
for parent in path.parents:
info = parent.lstat()
require(stat.S_ISDIR(info.st_mode) and info.st_uid in (0, uid) and not info.st_mode & 0o022,
f'Untrusted directory: {parent}')
info = path.lstat()
require(stat.S_ISREG(info.st_mode) and info.st_uid == uid and not info.st_mode & 0o022,
f'Untrusted file: {path}')
return info
def replace(path, data):
info = path.lstat()
if path.read_bytes() == data:
return
fd, name = tempfile.mkstemp(dir=path.parent)
try:
with os.fdopen(fd, 'wb') as stream:
os.fchown(stream.fileno(), info.st_uid, info.st_gid)
os.fchmod(stream.fileno(), stat.S_IMODE(info.st_mode))
stream.write(data)
stream.flush()
os.fsync(stream.fileno())
os.replace(name, path)
finally:
Path(name).unlink(missing_ok=True)
directory = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY)
try:
os.fsync(directory)
finally:
os.close(directory)
def run(*args, **kwargs):
return subprocess.check_output(args, timeout=120, **kwargs)
def prop(unit, field):
return run('systemctl', 'show', '--property', field, '--value', unit).decode().strip()
def process_image(service):
pid = int(prop(service, 'MainPID'))
return Path(f'/proc/{pid}/exe').resolve(strict=True) if pid else None
def pair(release_id):
release = ROOT / 'releases' / release_id
names = ('envelope.json', 'agent', 'helper', 'manifest.json')
for name in names:
protected(release / name)
manifest = json.loads(run(ROOT / 'updater', '--verify-bootstrap', ROOT / 'keyring.json',
release / 'envelope.json', release / 'agent', release / 'helper'))
require(manifest == json.loads((release / 'manifest.json').read_bytes())
and manifest['release_id'] == release_id and manifest['platform'] == 'x86_64-unknown-linux-gnu',
'Signed release binding mismatch')
helper, = [x for x in manifest['artifacts'] if x['role'] == 'helper']
return release, manifest, helper['sha256']
def repoint_unit(contents, old, new, destination):
lines = contents.splitlines(keepends=True)
paths = [f'ExecStart={path}/helper\n'.encode() for path in (old, new)]
starts = [i for i, line in enumerate(lines) if line.startswith(b'ExecStart=')]
require(len(starts) == 1 and lines[starts[0]] in paths, 'Unexpected managed helper ExecStart')
lines[starts[0]] = f'ExecStart={destination}/helper\n'.encode()
return b''.join(lines)
def device(args, activate):
require(os.geteuid() == 0, 'Linux maintenance requires the development administrator')
for path in [ROOT / n for n in ('updater', 'keyring.json', 'release.lock', 'config.json', 'lifecycle.py')]:
protected(path)
with (ROOT / 'release.lock').open('rb') as guard:
fcntl.flock(guard, fcntl.LOCK_EX | fcntl.LOCK_NB)
require(prop(TIMER, 'ActiveState') == 'inactive' and
prop('rmm-discovery-update.service', 'ActiveState') == 'inactive',
'Pause the update timer and wait for the running release check')
old, old_manifest, old_hash = pair(args.previous)
new, manifest, new_hash = pair(args.current)
require((ROOT/'current').resolve(strict=True) == new and old_hash != new_hash and
manifest['release_sequence'] > old_manifest['release_sequence'], 'Unexpected active release pair')
require(json.loads((ROOT/'config.json').read_bytes())['tenant'] == args.tenant, 'Wrong installed tenant')
identity = json.loads(run('runuser', '-u', 'rmm-discovery', '--', 'cat',
'/var/lib/rmm-discovery/identity.json'))
require(identity['agent_id'] == args.agent and identity['asset_id'] == args.asset, 'Wrong device')
require(run('runuser', '-u', 'rmm-discovery', '--', 'python3', '-I', ROOT/'lifecycle.py', 'idle').strip()
== b'idle', 'Unresolved device attempt')
cfg, proof = CONFIG/'helper.json', CONFIG/'agent-pin.json'
for path in (UNIT, cfg, proof):
protected(path)
command = prop(UNIT.name, 'ExecStart')
paths = (old/'helper', new/'helper')
effective = [path for path in paths if f'path={path} ; argv[]={path} ;' in command]
require(prop(UNIT.name, 'FragmentPath') == str(UNIT) and len(effective) == 1 and
command.count('argv[]=') == 1 and
'RMM_HELPER_CONFIG=' + str(cfg) in shlex.split(prop(UNIT.name, 'Environment')),
'Unexpected effective managed helper configuration')
require(process_image(UNIT.name) in (*paths, None) and process_image(AGENT) in (new/'agent', None),
'Running process does not match either signed generation')
settings = json.loads(cfg.read_bytes())
binding = {k: settings['binding'][k] for k in
('tenant_id', 'agent_id', 'asset_id', 'enrollment_generation', 'assignment_version')}
require((binding['tenant_id'], binding['asset_id'], binding['agent_id']) == (args.tenant, args.asset, args.agent)
and settings['profile'] == 'full_maintenance_v1' and settings[PINS[0]] in (old_hash, new_hash),
'Wrong managed profile or helper hash')
pin = json.loads(proof.read_bytes())
candidates = []
with tempfile.TemporaryDirectory(dir=CONFIG) as work:
temp = Path(work)/'helper.json'
for folder, binary in [(old, old_hash), (new, new_hash)]:
profile = encoded(dict(settings, helper_binary_sha256=binary))
temp.write_bytes(profile)
temp.chmod(0o600)
native = run(folder/'helper', '--inspect-full-maintenance-profile',
env={'PATH':'/usr/bin:/bin', 'RMM_HELPER_CONFIG':str(temp)})
computed = json.loads(native)
require(computed['helper_binary_sha256'] == binary and HASH.fullmatch(computed['policy_sha256']),
'Native profile verification failed')
candidates.append((profile, native, computed))
source, target = candidates
require(pin in [source[2], target[2]] and
{k:v for k,v in source[2].items() if k not in ('policy_sha256', PINS[0])} ==
{k:v for k,v in target[2].items() if k not in ('policy_sha256', PINS[0])},
'Existing and proposed proof must bind the same profile and identity')
undo = args.action == 'rollback'
profile, native, chosen = source if undo else target
destination = old if undo else new
unit = repoint_unit(UNIT.read_bytes(), old, new, destination)
if activate:
running = process_image(UNIT.name) == destination/'helper' and process_image(AGENT) == new/'agent'
if not (settings[PINS[0]] == chosen[PINS[0]] and pin == chosen and UNIT.read_bytes() == unit and running):
run('systemctl', 'stop', AGENT, UNIT.name)
replace(cfg, profile)
replace(proof, native)
replace(UNIT, unit)
run('systemctl', 'daemon-reload')
run('systemctl', 'start', UNIT.name, AGENT)
# The agent unit starts its Python launcher before exec'ing the signed binary.
deadline = time.monotonic() + 15
for service, expected in [(UNIT.name, destination/'helper'), (AGENT, new/'agent')]:
while process_image(service) != expected:
require(time.monotonic() < deadline, f'{service} did not start its verified binary')
time.sleep(0.2)
return dict(binding=binding, version=manifest['version'], previous=[old_hash, source[2]['policy_sha256']],
current=[new_hash, target[2]['policy_sha256']])
def existing_scope(policy, receipt):
require(policy['schema_version'] == 1, 'Unknown scope format')
scopes = [s for s in policy['linux'] if s['agent_id'] == receipt['binding']['agent_id']]
require(len(scopes) == 1, 'Exactly one pre-existing scope is required')
scope = scopes[0]
require(all(scope[k] == v for k,v in receipt['binding'].items()) and
[scope[k] for k in PINS] in (receipt['previous'], receipt['current']), 'Existing scope changed')
return scope
def server(args, receipt, activate):
require([receipt['binding'][k] for k in ('tenant_id','asset_id','agent_id')] ==
[args.tenant,args.asset,args.agent], 'Wrong scope receipt')
for pins in [receipt['previous'], receipt['current']]:
require(len(pins) == 2 and all(HASH.fullmatch(x) for x in pins), 'Invalid helper hashes')
owner = (POLICY.parent.parent/'jobs/ed25519.pem').stat().st_uid
directory = POLICY.parent.lstat()
require(stat.S_ISDIR(directory.st_mode) and directory.st_uid == owner and not directory.st_mode & 0o077,
'Invalid managed policy directory')
lock = POLICY.parent/'operator.lock'
for path in (POLICY, lock):
info = path.lstat()
require(stat.S_ISREG(info.st_mode) and info.st_uid in (0, owner) and not info.st_mode & 0o022,
'Invalid managed policy file')
wanted = receipt['previous' if args.action == 'rollback' else 'current']
query = """BEGIN READ ONLY;
SELECT json_build_object('generation',enrollment_generation,'assignment',assignment_version,
'fresh',sync_ready AND helper_sync_ready AND NOT restore_blocked
AND last_authenticated_at > now()-interval '2 minutes' AND certificate_sha256=last_authenticated_certificate_sha256,
'version',reported_agent_version,'caps',capabilities,
'idle',NOT EXISTS(SELECT 1 FROM jobs j WHERE j.agent_id=i.agent_id AND j.tenant_id=i.tenant_id AND
j.state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')))
FROM agent_installations i WHERE tenant_id=:'tenant' AND asset_id=:'asset' AND agent_id=:'agent'
AND revoked_at IS NULL;
COMMIT;"""
deadline = time.monotonic()+120
while True:
with lock.open('rb') as guard:
fcntl.flock(guard, fcntl.LOCK_EX)
policy = json.loads(POLICY.read_bytes())
scope = existing_scope(policy, receipt)
state = json.loads(run('docker','exec','-i','rust-discovery-server-postgres-1','psql','-XqAt',
'-U','postgres','-d','rmm','-v','ON_ERROR_STOP=1',
'-v',f'tenant={args.tenant}','-v',f'asset={args.asset}','-v',f'agent={args.agent}',
input=query.encode()))
require(state['idle'] and state['generation'] == scope['enrollment_generation'] and
state['assignment'] == scope['assignment_version'], 'Unresolved job or changed enrollment')
if not activate:
return {'existing_scope':True}
if state['fresh'] and state['version'] == receipt['version'] and all(
p in state['caps'] for p in ['helper.binary.sha256:'+wanted[0],'helper.policy.sha256:'+wanted[1]]):
if [scope[k] for k in PINS] != wanted:
scope.update(zip(PINS,wanted))
replace(POLICY, encoded(policy))
return {'scoped_helper_binary':wanted[0],'scoped_helper_policy':wanted[1]}
require(time.monotonic()<deadline, 'Waiting for matching signed Linux device and helper reconciliation timed out')
time.sleep(3)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('action',choices=('check','switch','rollback'))
for name in ('tenant','asset','agent','previous','current'):
parser.add_argument('--'+name, required=True, type=lambda v: str(uuid.UUID(v)))
parser.add_argument('--worker',choices=('device-check','device','scope-check','scope'), help=argparse.SUPPRESS)
parser.add_argument('--receipt',help=argparse.SUPPRESS)
args=parser.parse_args()
if args.worker:
result = device(args, args.worker == 'device') if args.worker.startswith('device') else server(
args, json.loads(args.receipt), args.worker == 'scope')
else:
from dev_stage import ssh_prefix
program=Path(__file__).read_bytes()
base = [args.action]
for k in ('tenant', 'asset', 'agent', 'previous', 'current'):
base.extend(['--' + k, getattr(args, k)])
def remote(host, worker, receipt=None):
params=[*base, '--worker',worker]
if receipt:
params.extend(['--receipt',json.dumps(receipt,separators=(',',':'))])
return json.loads(subprocess.check_output([*ssh_prefix(host),shlex.join(['python3','-I','-',*params])],
input=program,timeout=210))
result=remote('linux','device-check')
remote('server','scope-check',result)
if args.action != 'check':
require(remote('linux','device') == result, 'The prepared profile changed')
result.update(remote('server','scope',result))
print(json.dumps(result))
if __name__ == '__main__':
main()