Use script-only execution on Linux and preserve the enrolled helper through updates
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
"""Bootstrap a Linux systemd client and maintain its signed release pair."""
|
||||
import argparse
|
||||
import contextlib
|
||||
import datetime
|
||||
import fcntl
|
||||
import getpass
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -14,6 +16,7 @@ import shutil
|
||||
import socket
|
||||
import sqlite3
|
||||
import ssl
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -26,6 +29,9 @@ ROOT = Path('/opt/rust-discovery-client')
|
||||
STATE = Path('/var/lib/rmm-discovery')
|
||||
SERVICE = 'rmm-discovery-agent.service'
|
||||
TIMER = 'rmm-discovery-update.timer'
|
||||
HELPER = 'rmm-discovery-managed-script-helper.service'
|
||||
HELPER_SOCKET = Path('/run/rmm-discovery-script-helper/control.sock')
|
||||
OLD_HELPER_CONFIG = Path('/etc/rmm-discovery-managed-scripts')
|
||||
PLATFORM = 'x86_64-unknown-linux-gnu'
|
||||
MAX_BINARY = 256 * 1024 * 1024
|
||||
UNIT_DIR = Path('/etc/systemd/system')
|
||||
@@ -177,7 +183,7 @@ def installed():
|
||||
return json.loads(as_device('fetch', '/agent/v1/releases/installed', stdout=subprocess.PIPE).stdout)
|
||||
|
||||
|
||||
def healthy(version, baseline, duration=90):
|
||||
def healthy(version, baseline, duration=90, pin=None):
|
||||
until = time.monotonic() + duration
|
||||
started = time.time()
|
||||
while time.monotonic() < until:
|
||||
@@ -188,6 +194,8 @@ def healthy(version, baseline, duration=90):
|
||||
recent = datetime.datetime.fromisoformat(contact.replace('Z', '+00:00')).timestamp()
|
||||
if (started - 1 <= recent <= time.time() + 10 and record['agent_version'] == version
|
||||
and record['connection_generation'] > baseline
|
||||
and (pin is None or {'root:run_script', 'helper.policy.sha256:' + pin['policy_sha256'],
|
||||
'helper.binary.sha256:' + pin['helper_binary_sha256']}.issubset(record['capabilities']))
|
||||
and subprocess.run(['systemctl', 'is-active', '--quiet', SERVICE], timeout=10).returncode == 0):
|
||||
return True
|
||||
except (subprocess.SubprocessError, ValueError, OSError):
|
||||
@@ -200,6 +208,212 @@ def service(*args):
|
||||
subprocess.run(['systemctl', *args], check=True, timeout=60)
|
||||
|
||||
|
||||
def active_folder():
|
||||
release = canonical_id(read(ROOT / 'current/manifest.json')['release_id'])
|
||||
folder = ROOT / 'releases' / release
|
||||
require((ROOT / 'current').resolve(strict=True) == folder and folder.resolve() == folder,
|
||||
'unexpected active generation path')
|
||||
return folder
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def account_file(path, uid):
|
||||
# Open only a checked inode a second time for TLS. No privileged open of a
|
||||
# device or FIFO, and no path re-resolution after validation.
|
||||
fd = os.open(path, os.O_PATH | os.O_NOFOLLOW)
|
||||
try:
|
||||
info = os.fstat(fd)
|
||||
require(stat.S_ISREG(info.st_mode) and info.st_uid == uid and info.st_nlink == 1
|
||||
and info.st_mode & 0o077 == 0 and 0 < info.st_size <= 65536,
|
||||
'device credential metadata differs')
|
||||
yield Path('/proc/self/fd') / str(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def script_binding(account):
|
||||
# The root helper trusts only the signing key from the original HTTPS
|
||||
# bootstrap and the binding returned by the authenticated device gateway.
|
||||
boot = read(ROOT / 'bootstrap.json')
|
||||
certs = STATE / 'certificates'
|
||||
with account_file(certs / 'current.json', account.pw_uid) as selector:
|
||||
version = canonical_id(read(selector)['version'])
|
||||
creds = certs / 'versions' / version
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
context.load_verify_locations(cafile=str(ROOT / 'gateway-ca.pem'))
|
||||
with (account_file(creds / 'device.pem', account.pw_uid) as cert,
|
||||
account_file(creds / 'device.key', account.pw_uid) as key):
|
||||
context.load_cert_chain(str(cert), str(key))
|
||||
response = io.BytesIO()
|
||||
conf = read(ROOT / 'config.json')
|
||||
retrieve(conf['gateway'], '/agent/v1/releases/installed', response, context=context)
|
||||
own = json.loads(response.getvalue())
|
||||
public_key = boot.get('job_signing_public_key_hex')
|
||||
if public_key is None:
|
||||
trust = io.BytesIO()
|
||||
retrieve(conf['gateway'], '/agent/v1/releases/device-trust', trust, context=context)
|
||||
public_key = json.loads(trust.getvalue())['job_signing_public_key_hex']
|
||||
require(re.fullmatch('[0-9a-f]{64}', public_key), 'invalid job signing key')
|
||||
require(own['tenant_id'] == conf['tenant'] == boot['tenant_id'], 'device binding tenant mismatch')
|
||||
binding = {field: canonical_id(own[field]) for field in ('tenant_id', 'asset_id', 'agent_id')}
|
||||
for field in ('enrollment_generation', 'assignment_version'):
|
||||
require(type(own[field]) is int and own[field] > 0, 'invalid device generation')
|
||||
binding[field] = own[field]
|
||||
binding['public_key_hex'] = public_key
|
||||
return binding
|
||||
|
||||
|
||||
def script_settings(binding, account, digest):
|
||||
return dict(profile='full_maintenance_v1', binding=binding, agent_uid=account.pw_uid,
|
||||
socket_gid=account.pw_gid, socket=str(HELPER_SOCKET), state='/var/lib/rmm-discovery-helper',
|
||||
allowed_services=[], allow_reboot=False, allow_root_scripts=True,
|
||||
allowed_script_profiles=['full_maintenance_v1'], helper_binary_sha256=digest)
|
||||
|
||||
|
||||
def prepare_script_helper(folder, binding, account):
|
||||
manifest = json.loads(subprocess.check_output([
|
||||
ROOT / 'updater', '--verify-bootstrap', ROOT / 'keyring.json', folder / 'envelope.json',
|
||||
folder / 'agent', folder / 'helper'], timeout=120))
|
||||
require(manifest == read(folder / 'manifest.json') and manifest['release_id'] == folder.name,
|
||||
'helper generation differs from signed release')
|
||||
digest = next(a['sha256'] for a in manifest['artifacts'] if a['role'] == 'helper')
|
||||
settings = script_settings(binding, account, digest)
|
||||
config = folder / 'script-helper.json'
|
||||
if config.exists():
|
||||
require(read(config) == settings, 'installed helper binding changed')
|
||||
else:
|
||||
save(config, settings, 0o600)
|
||||
raw = subprocess.check_output([folder / 'helper', '--inspect-full-maintenance-profile'],
|
||||
env={'PATH': '/usr/bin:/bin', 'RMM_HELPER_CONFIG': str(config)}, timeout=30)
|
||||
pin = json.loads(raw)
|
||||
require(pin['helper_binary_sha256'] == digest and re.fullmatch('[0-9a-f]{64}', pin['policy_sha256']),
|
||||
'helper pin differs from signed release')
|
||||
save(folder / 'script-pin.json', raw)
|
||||
return pin
|
||||
|
||||
|
||||
def helper_unit(executable, config=None):
|
||||
command = f'Environment=RMM_HELPER_CONFIG={config}\n' if config else ''
|
||||
return f"""[Unit]
|
||||
Description=Rust Discovery helper for approved root scripts
|
||||
After=time-sync.target
|
||||
|
||||
[Service]
|
||||
User=root
|
||||
Group=rmm-discovery
|
||||
StateDirectory=rmm-discovery-helper
|
||||
StateDirectoryMode=0700
|
||||
RuntimeDirectory=rmm-discovery-script-helper
|
||||
RuntimeDirectoryMode=0750
|
||||
UMask=0077
|
||||
{command}ExecStart={executable}
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
""".encode()
|
||||
|
||||
|
||||
def helper_unit_files(binding, account):
|
||||
unit = UNIT_DIR / HELPER
|
||||
dropdir = UNIT_DIR / (SERVICE + '.d')
|
||||
dropdir.mkdir(mode=0o755, exist_ok=True)
|
||||
require(not dropdir.is_symlink() and not dropdir.stat().st_mode & 0o022, 'unsafe service drop-in directory')
|
||||
drop = dropdir / '50-managed-scripts.conf'
|
||||
order = f'[Unit]\nAfter={HELPER}\n'.encode()
|
||||
old_order = (f'[Unit]\nWants={HELPER}\nAfter={HELPER}\n'
|
||||
f'\n[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET={HELPER_SOCKET}\n'
|
||||
f'Environment=RMM_FULL_MAINTENANCE_POLICY_FILE={OLD_HELPER_CONFIG}/agent-pin.json\n').encode()
|
||||
changed = []
|
||||
for path, desired in (
|
||||
(unit, helper_unit(f'/usr/bin/python3 -I {ROOT}/lifecycle.py run-helper')),
|
||||
(drop, order),
|
||||
):
|
||||
if path.is_symlink():
|
||||
raise ValueError('unexpected systemd symlink: ' + str(path))
|
||||
if path.exists():
|
||||
current = path.read_bytes()
|
||||
if current == desired:
|
||||
continue
|
||||
old = old_order
|
||||
if path == unit:
|
||||
exe = re.search(rb'^ExecStart=(.+)$', current, re.M)
|
||||
require(exe is not None, 'unrecognized helper service')
|
||||
candidate = Path(os.fsdecode(exe[1]))
|
||||
require(candidate == ROOT / 'releases' / canonical_id(candidate.parent.name) / 'helper',
|
||||
'unexpected legacy helper image')
|
||||
private = OLD_HELPER_CONFIG / 'helper.json'
|
||||
old = helper_unit(candidate, private)
|
||||
settings = read(private)
|
||||
require(settings == script_settings(binding, account, settings['helper_binary_sha256']),
|
||||
'existing helper belongs to a different binding or profile')
|
||||
require(current == old, 'unrecognized managed service file: ' + str(path))
|
||||
changed.append((path, desired))
|
||||
return changed
|
||||
|
||||
|
||||
def start_pair():
|
||||
service('start', HELPER)
|
||||
until = time.monotonic() + 15
|
||||
while not HELPER_SOCKET.is_socket():
|
||||
require(time.monotonic() < until, 'script helper socket unavailable')
|
||||
time.sleep(.2)
|
||||
service('start', SERVICE)
|
||||
|
||||
|
||||
def helper_state():
|
||||
probe = subprocess.run(['systemctl', 'is-enabled', HELPER], stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL, text=True, timeout=10)
|
||||
return probe.stdout.strip()
|
||||
|
||||
|
||||
def provision_scripts(enable_admin_scripts):
|
||||
with (ROOT / 'release.lock').open('rb') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
pending = ROOT / 'pending.json'
|
||||
if pending.exists():
|
||||
revert(read(pending))
|
||||
folder = active_folder()
|
||||
intent = ROOT / 'script-helper-installing.json'
|
||||
state = helper_state()
|
||||
known = (UNIT_DIR / HELPER).exists() or OLD_HELPER_CONFIG.exists() or (folder / 'script-helper.json').exists()
|
||||
if state != 'enabled':
|
||||
new = enable_admin_scripts and not known and state in ('', 'not-found')
|
||||
resume = (enable_admin_scripts and intent.is_file() and read(intent) == folder.name
|
||||
and state in ('', 'not-found', 'disabled'))
|
||||
if not (new or resume):
|
||||
print('Keeping the local script helper activation policy: ' + (state or 'not installed'), file=sys.stderr)
|
||||
return
|
||||
else:
|
||||
require((UNIT_DIR / HELPER).is_file(), 'refusing to replace an externally managed helper unit')
|
||||
require(as_device('idle', stdout=subprocess.PIPE).stdout.strip() == b'idle', 'active job prevents helper switch')
|
||||
account = pwd.getpwnam('rmm-discovery')
|
||||
require(account.pw_uid > 0 and account.pw_gid > 0, 'dedicated service account required')
|
||||
binding = script_binding(account)
|
||||
changed = helper_unit_files(binding, account)
|
||||
if state != 'enabled':
|
||||
save(intent, folder.name)
|
||||
pin = prepare_script_helper(folder, binding, account)
|
||||
observed = installed()
|
||||
if (not changed and 'root:run_script' in observed['capabilities']
|
||||
and 'helper.binary.sha256:' + pin['helper_binary_sha256'] in observed['capabilities']
|
||||
and 'helper.policy.sha256:' + pin['policy_sha256'] in observed['capabilities']):
|
||||
intent.unlink(missing_ok=True)
|
||||
return
|
||||
service('stop', SERVICE)
|
||||
if (UNIT_DIR / HELPER).exists():
|
||||
service('stop', HELPER)
|
||||
for path, raw in changed:
|
||||
save(path, raw)
|
||||
service('daemon-reload')
|
||||
service('enable', HELPER)
|
||||
start_pair()
|
||||
require(healthy(read(folder / 'manifest.json')['version'], observed['connection_generation'], pin=pin),
|
||||
'device has not confirmed the script helper')
|
||||
intent.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def units():
|
||||
program = '/usr/bin/python3 -I ' + str(ROOT / 'lifecycle.py')
|
||||
return {
|
||||
@@ -266,7 +480,7 @@ def save_token(base, token, gid):
|
||||
os.chown(path, 0, gid)
|
||||
|
||||
|
||||
def install(base, token_file):
|
||||
def install(base, token_file, enable_admin_scripts=False):
|
||||
require(platform.machine() == 'x86_64', 'Linux x86-64 required')
|
||||
base = origin(base)
|
||||
if ROOT.exists():
|
||||
@@ -288,8 +502,12 @@ def install(base, token_file):
|
||||
current = Path(__file__).read_bytes()
|
||||
if harness.read_bytes() != current:
|
||||
save(harness, current)
|
||||
pending = ROOT / 'pending.json'
|
||||
if pending.exists():
|
||||
revert(read(pending), report_failure=False)
|
||||
enable()
|
||||
require(healthy(read(ROOT / 'current/manifest.json')['version'], 0), 'awaiting authenticated device heartbeat')
|
||||
provision_scripts(enable_admin_scripts)
|
||||
service('enable', '--now', TIMER)
|
||||
(ROOT / 'first.token').unlink(missing_ok=True)
|
||||
return
|
||||
@@ -330,6 +548,7 @@ def install(base, token_file):
|
||||
os.rename(staging, ROOT)
|
||||
enable()
|
||||
require(healthy(manifest['version'], 0), 'installed, awaiting authenticated device heartbeat')
|
||||
provision_scripts(enable_admin_scripts)
|
||||
# OnBootSec may already be in the past on a running machine. The first
|
||||
# timer check can run immediately once the device has reported its platform.
|
||||
service('enable', '--now', TIMER)
|
||||
@@ -349,17 +568,32 @@ def run_agent():
|
||||
RMM_ENROLLMENT_TENANT=conf['tenant'], RMM_JOB_SIGNING_KEY_BOOTSTRAP='1',
|
||||
RMM_RELEASE_LOCK=str(ROOT / 'release.lock'),
|
||||
RMM_INSTALLED_RELEASE_VERSION=read(ROOT / 'current/manifest.json')['version'])
|
||||
exe = str(ROOT / 'current/agent')
|
||||
folder = active_folder()
|
||||
if (folder / 'script-pin.json').is_file() and helper_state() in ('enabled', 'enabled-runtime'):
|
||||
os.environ.update(RMM_FULL_MAINTENANCE_HELPER_SOCKET=str(HELPER_SOCKET),
|
||||
RMM_FULL_MAINTENANCE_POLICY_FILE=str(folder / 'script-pin.json'),
|
||||
RMM_SCRIPT_HELPER_RELEASE=folder.name)
|
||||
exe = str(folder / 'agent')
|
||||
os.execv(exe, [exe])
|
||||
|
||||
|
||||
def revert(intent):
|
||||
def revert(intent, report_failure=True):
|
||||
# Persist failed sequence before restoring. An interruption re-enters recovery.
|
||||
save(ROOT / 'failed.json', {'release_sequence': intent['sequence'], 'reason': 'activation_unconfirmed'})
|
||||
enabled = intent.get('script_helper') and helper_state() in ('enabled', 'enabled-runtime')
|
||||
if intent.get('script_helper'):
|
||||
service('stop', SERVICE, HELPER)
|
||||
link(ROOT, intent['previous'])
|
||||
service('restart', SERVICE)
|
||||
if enabled:
|
||||
start_pair()
|
||||
previous = active_folder()
|
||||
require(healthy(read(previous / 'manifest.json')['version'], 0, pin=read(previous / 'script-pin.json')),
|
||||
'predecessor has not yet confirmed the previous script helper')
|
||||
else:
|
||||
service('restart', SERVICE)
|
||||
(ROOT / 'pending.json').unlink()
|
||||
raise RuntimeError('release activation unconfirmed; predecessor selected')
|
||||
if report_failure:
|
||||
raise RuntimeError('release activation unconfirmed; predecessor selected')
|
||||
|
||||
|
||||
def update():
|
||||
@@ -401,10 +635,28 @@ def update():
|
||||
(staging / 'current.json').unlink()
|
||||
os.rename(staging, dest)
|
||||
intent = {'previous': active['release_id'], 'sequence': manifest['release_sequence']}
|
||||
active_config = active_folder() / 'script-helper.json'
|
||||
pin = None
|
||||
if active_config.exists() and helper_state() in ('enabled', 'enabled-runtime'):
|
||||
settings = read(active_config)
|
||||
account = pwd.getpwnam('rmm-discovery')
|
||||
active_hash = next(a['sha256'] for a in active['artifacts'] if a['role'] == 'helper')
|
||||
require(settings == script_settings(settings['binding'], account, active_hash),
|
||||
'existing script helper policy changed')
|
||||
pin = prepare_script_helper(dest, settings['binding'], account)
|
||||
intent['script_helper'] = True
|
||||
save(pending, intent)
|
||||
link(ROOT, release)
|
||||
service('restart', SERVICE)
|
||||
if not healthy(manifest['version'], previous_health['connection_generation']):
|
||||
try:
|
||||
if pin:
|
||||
service('stop', SERVICE, HELPER)
|
||||
link(ROOT, release)
|
||||
if pin:
|
||||
start_pair()
|
||||
else:
|
||||
service('restart', SERVICE)
|
||||
except (OSError, ValueError, subprocess.SubprocessError):
|
||||
revert(intent)
|
||||
if not healthy(manifest['version'], previous_health['connection_generation'], pin=pin):
|
||||
revert(intent)
|
||||
pending.unlink()
|
||||
(ROOT / 'first.token').unlink(missing_ok=True)
|
||||
@@ -412,18 +664,25 @@ def update():
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=('install', 'update', 'run-agent', 'fetch', 'idle'))
|
||||
parser.add_argument('action', choices=('install', 'update', 'run-agent', 'run-helper', 'fetch', 'idle'))
|
||||
parser.add_argument('values', nargs='*')
|
||||
parser.add_argument('--token-file')
|
||||
parser.add_argument('--enable-admin-scripts', action='store_true',
|
||||
help='enable this enrolled device to run individually approved root scripts')
|
||||
args = parser.parse_args()
|
||||
if args.action in ('install', 'update'):
|
||||
require(os.geteuid() == 0, 'administrative installation required')
|
||||
os.umask(0o022)
|
||||
if args.action == 'install':
|
||||
require(len(args.values) == 1, 'one HTTPS bootstrap origin required')
|
||||
install(args.values[0], args.token_file)
|
||||
install(args.values[0], args.token_file, args.enable_admin_scripts)
|
||||
elif args.action == 'update':
|
||||
update()
|
||||
elif args.action == 'run-helper':
|
||||
require(os.geteuid() == 0, 'privileged script helper service required')
|
||||
folder = active_folder()
|
||||
os.environ['RMM_HELPER_CONFIG'] = str(folder / 'script-helper.json')
|
||||
os.execv(folder / 'helper', [str(folder / 'helper')])
|
||||
elif args.action == 'run-agent':
|
||||
run_agent()
|
||||
elif args.action == 'fetch':
|
||||
|
||||
Reference in New Issue
Block a user