Add an HTTPS installer for the OPNsense agent and show its command in the WebUI
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the OPNsense agent bundle and place it on the dev server for the HTTPS installer.
|
||||
|
||||
The bundle is `agent.pyz`, `collect.pyz`, the rc.d script and optionally the native
|
||||
script worker `rmm-opnsense-jobs` (FreeBSD amd64, built natively in the test guest).
|
||||
It lands in `devstate/mvp-releases/config/opnsense/` next to the Linux bootstrap
|
||||
configuration and is served only against a valid one-time enrollment code.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
|
||||
|
||||
def activate(payload):
|
||||
"""Runs on the dev server as root."""
|
||||
import os
|
||||
target = Path('/opt/rust-discovery/devstate/mvp-releases/config/opnsense')
|
||||
owner = os.stat(target.parent)
|
||||
target.mkdir(mode=0o700, exist_ok=True)
|
||||
os.chown(target, owner.st_uid, owner.st_gid)
|
||||
os.chmod(target, 0o700)
|
||||
wanted = set(payload['files']) | {'manifest.json'}
|
||||
for name, data in [*payload['files'].items(), ('manifest.json', payload['manifest'])]:
|
||||
raw = base64.b64decode(data, validate=True)
|
||||
tmp = target / ('.' + name + '.new')
|
||||
with open(tmp, 'wb') as f:
|
||||
os.fchmod(f.fileno(), 0o600)
|
||||
f.write(raw)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.chown(tmp, owner.st_uid, owner.st_gid)
|
||||
os.replace(tmp, target / name)
|
||||
for old in target.iterdir():
|
||||
if old.name not in wanted:
|
||||
old.unlink()
|
||||
print(json.dumps({'directory': str(target), 'files': sorted(wanted)}))
|
||||
|
||||
|
||||
def build_zipapps(out):
|
||||
python = ROOT / 'devstate/opnsense-vm/venv/bin/python'
|
||||
if not python.is_file():
|
||||
raise SystemExit('Build-venv fehlt: devstate/opnsense-vm/venv mit grpcio-tools==1.76.0 und protobuf==6.33.5')
|
||||
subprocess.run([str(python), str(ROOT / 'deploy/opnsense/build.py'), str(out)], check=True)
|
||||
|
||||
|
||||
def worker_from_guest():
|
||||
credentials = ROOT / 'credentials'
|
||||
ssh = ['ssh', '-F', '/dev/null', '-T', '-i', str(credentials / 'opnsense-dev-key'),
|
||||
'-o', 'UserKnownHostsFile=' + str(credentials / 'opnsense-dev.known_hosts'),
|
||||
'-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes',
|
||||
'-o', 'IdentitiesOnly=yes', '-o', 'ConnectTimeout=10', 'root@192.168.247.1']
|
||||
return subprocess.run([*ssh, 'cat /usr/local/libexec/discovery-agent/rmm-opnsense-jobs'],
|
||||
check=True, capture_output=True).stdout
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
worker = parser.add_mutually_exclusive_group()
|
||||
worker.add_argument('--worker', type=Path, help='Native rmm-opnsense-jobs (FreeBSD amd64) to include')
|
||||
worker.add_argument('--worker-from-guest', action='store_true',
|
||||
help='Take the installed rmm-opnsense-jobs from the OPNsense test guest')
|
||||
parser.add_argument('--prepare-only', type=Path, metavar='DIR', help='Write the bundle locally, do not upload')
|
||||
args = parser.parse_args()
|
||||
version = re.search(r"^VERSION = '([A-Za-z0-9._-]+)'$", (ROOT / 'deploy/opnsense/agent.py').read_text(), re.M)[1]
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = Path(tmp)
|
||||
build_zipapps(out)
|
||||
files = {name: (out / name).read_bytes() for name in ('agent.pyz', 'collect.pyz')}
|
||||
files['discovery_agent'] = (ROOT / 'deploy/opnsense/discovery_agent').read_bytes()
|
||||
if args.worker:
|
||||
files['rmm-opnsense-jobs'] = args.worker.read_bytes()
|
||||
elif args.worker_from_guest:
|
||||
files['rmm-opnsense-jobs'] = worker_from_guest()
|
||||
if 'rmm-opnsense-jobs' in files and not files['rmm-opnsense-jobs'].startswith(b'\x7fELF'):
|
||||
raise SystemExit('rmm-opnsense-jobs ist keine ELF-Datei')
|
||||
manifest = json.dumps({'schema_version': 1, 'version': version, 'files': {
|
||||
name: {'sha256': hashlib.sha256(data).hexdigest(), 'bytes': len(data)} for name, data in sorted(files.items())
|
||||
}}, indent=1).encode()
|
||||
if args.prepare_only:
|
||||
args.prepare_only.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
for name, data in [*files.items(), ('manifest.json', manifest)]:
|
||||
(args.prepare_only / name).write_bytes(data)
|
||||
print(manifest.decode())
|
||||
return
|
||||
from dev_stage import ssh_prefix
|
||||
import inspect
|
||||
payload = json.dumps({'manifest': base64.b64encode(manifest).decode(),
|
||||
'files': {n: base64.b64encode(d).decode() for n, d in files.items()}})
|
||||
source = 'import base64, json, os\nfrom pathlib import Path\n' + inspect.getsource(activate)
|
||||
subprocess.run(ssh_prefix('server') + [shlex.join(['python3', '-I', '-'])],
|
||||
input=source + '\nactivate(json.loads(' + repr(payload) + '))\n', text=True, check=True)
|
||||
print(manifest.decode())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user