From 7201b18300b3e580b10c743a64132a55c7c378e1 Mon Sep 17 00:00:00 2001
From: Kevin Janssen
Date: Sat, 21 Nov 2026 09:25:51 +0100
Subject: [PATCH] Add an HTTPS installer for the OPNsense agent and show its
command in the WebUI
Co-Authored-By: Claude Opus 5.5
---
crates/server/src/agent_updates.rs | 17 ++
crates/server/src/bootstrap/mod.rs | 4 +-
crates/server/src/bootstrap/opnsense.rs | 105 +++++++++
crates/server/src/bootstrap/opnsense.sh | 13 ++
.../server/src/bootstrap/opnsense_install.py | 199 ++++++++++++++++++
tests/browser/order-composer.spec.ts | 13 +-
tools/build.py | 3 +-
tools/dev_opnsense_bundle.py | 106 ++++++++++
web/src/main.tsx | 18 +-
9 files changed, 468 insertions(+), 10 deletions(-)
create mode 100644 crates/server/src/bootstrap/opnsense.rs
create mode 100644 crates/server/src/bootstrap/opnsense.sh
create mode 100644 crates/server/src/bootstrap/opnsense_install.py
create mode 100644 tools/dev_opnsense_bundle.py
diff --git a/crates/server/src/agent_updates.rs b/crates/server/src/agent_updates.rs
index ae0df11..801d30a 100644
--- a/crates/server/src/agent_updates.rs
+++ b/crates/server/src/agent_updates.rs
@@ -304,6 +304,21 @@ RMM_BOOTSTRAP_PYTHON
)
}),
)
+ .route(
+ "/install/opnsense.sh",
+ get(|| async {
+ (
+ [("content-type", "text/x-shellscript")],
+ concat!(
+ include_str!("bootstrap/opnsense.sh"),
+ include_str!("../../../deploy/opnsense/install-dev.sh"),
+ "RMM_OPNSENSE_FILES\ncat >\"$work/install.py\" <<'RMM_OPNSENSE_PYTHON'\n",
+ include_str!("bootstrap/opnsense_install.py"),
+ "RMM_OPNSENSE_PYTHON\n/usr/local/bin/python3 -I \"$work/install.py\" \"$work/install-files.sh\" \"$@\"\n"
+ ),
+ )
+ }),
+ )
.route(
"/install/windows.ps1",
get(|| async {
@@ -319,6 +334,8 @@ RMM_BOOTSTRAP_PYTHON
get(bootstrap::artifact),
)
.route("/agent/v1/bootstrap/updater", get(bootstrap::updater))
+ .route("/agent/v1/bootstrap/opnsense/current", get(bootstrap::opnsense::current))
+ .route("/agent/v1/bootstrap/opnsense/files/{name}", get(bootstrap::opnsense::file))
.route("/agent/v1/releases/current", get(current))
.route("/agent/v1/releases/installed", get(installed))
.route("/agent/v1/releases/device-trust", get(bootstrap::device_trust))
diff --git a/crates/server/src/bootstrap/mod.rs b/crates/server/src/bootstrap/mod.rs
index 21d26c8..1233e06 100644
--- a/crates/server/src/bootstrap/mod.rs
+++ b/crates/server/src/bootstrap/mod.rs
@@ -2,6 +2,7 @@
//! the tenant's one-time enrollment token until the final enrollment consumes it.
use super::*;
mod starter;
+pub(super) mod opnsense;
pub(super) use starter::publish_missing as publish_starters;
@@ -135,7 +136,8 @@ pub(super) async fn issue(
.await?;
tx.commit().await?;
Ok(private_json(
- json!({"token":token,"expires_at":expires,"bootstrap_origin":bootstrap_origin,"release_platforms":release_platforms}),
+ json!({"token":token,"expires_at":expires,"bootstrap_origin":bootstrap_origin,"release_platforms":release_platforms,
+ "opnsense_installer":opnsense::available()}),
))
}
async fn token_tenant(s: &crate::State, h: &HeaderMap) -> Result {
diff --git a/crates/server/src/bootstrap/opnsense.rs b/crates/server/src/bootstrap/opnsense.rs
new file mode 100644
index 0000000..a132eab
--- /dev/null
+++ b/crates/server/src/bootstrap/opnsense.rs
@@ -0,0 +1,105 @@
+//! HTTPS installation of the OPNsense agent. The program files are a fixed
+//! bundle in the release configuration (`opnsense/` next to the Linux bootstrap
+//! file). The public script holds no credentials; the bundle is served only
+//! against a valid one-time enrollment token, until enrollment consumes it.
+use super::*;
+use std::collections::BTreeMap;
+
+/// Files the installer needs; `rmm-opnsense-jobs` (script worker) is optional.
+const REQUIRED: [&str; 3] = ["agent.pyz", "collect.pyz", "discovery_agent"];
+const OPTIONAL: [&str; 1] = ["rmm-opnsense-jobs"];
+const MAX_FILE_BYTES: u64 = 32 * 1024 * 1024;
+
+#[derive(Deserialize, serde::Serialize, Clone)]
+#[serde(deny_unknown_fields)]
+pub(super) struct File {
+ sha256: String,
+ bytes: u64,
+}
+#[derive(Deserialize)]
+#[serde(deny_unknown_fields)]
+struct Manifest {
+ schema_version: u32,
+ version: String,
+ files: BTreeMap,
+}
+
+fn bundle_dir() -> Result {
+ std::env::var_os("RMM_AGENT_BOOTSTRAP_LINUX_FILE")
+ .and_then(|slot| FsPath::new(&slot).parent().map(|p| p.join("opnsense")))
+ .ok_or_else(|| fail("BOOTSTRAP_NOT_CONFIGURED"))
+}
+
+fn manifest() -> Result {
+ let raw = protected_file(&bundle_dir()?.join("manifest.json"), 8192)?;
+ let m: Manifest = serde_json::from_slice(&raw).map_err(|_| fail("OPNSENSE_BUNDLE_INVALID"))?;
+ valid(&m).then_some(m).ok_or_else(|| fail("OPNSENSE_BUNDLE_INVALID"))
+}
+
+fn valid(m: &Manifest) -> bool {
+ m.schema_version == 1
+ && (1..=64).contains(&m.version.len())
+ && m.version.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
+ && REQUIRED.iter().all(|name| m.files.contains_key(*name))
+ && m.files.iter().all(|(name, f)| {
+ (REQUIRED.contains(&name.as_str()) || OPTIONAL.contains(&name.as_str()))
+ && rmm_domain::agent_update::digest(&f.sha256)
+ && (1..=MAX_FILE_BYTES).contains(&f.bytes)
+ })
+}
+
+/// Whether the enrollment dialog can offer the OPNsense command.
+pub(super) fn available() -> bool {
+ manifest().is_ok()
+}
+
+pub(crate) async fn current(State(s): State, headers: HeaderMap) -> Result {
+ let tenant = token_tenant(&s, &headers).await?;
+ let m = manifest()?;
+ // The device gateway is the same for every platform; the Linux slot names it.
+ let gateway = config(&s, Platform::LinuxX64, tenant, None).await?.gateway_origin;
+ let ca = s.signer.as_ref().ok_or_else(missing)?.ca.to_pem().map_err(|_| invalid())?;
+ let trust = device_trust_data(&s)?;
+ Ok(private_json(json!({"schema_version":1,"tenant_id":tenant,"version":m.version,"gateway_origin":gateway,
+ "ca_pem":String::from_utf8(ca).map_err(|_| invalid())?,
+ "job_signing_public_key_hex":trust["job_signing_public_key_hex"],"files":m.files})))
+}
+
+pub(crate) async fn file(
+ State(s): State,
+ headers: HeaderMap,
+ Path(name): Path,
+) -> Result {
+ token_tenant(&s, &headers).await?;
+ let expected = manifest()?.files.remove(&name).ok_or_else(missing)?;
+ let raw = protected_file(&bundle_dir()?.join(&name), MAX_FILE_BYTES)?;
+ if raw.len() as u64 != expected.bytes || hex::encode(openssl::sha::sha256(&raw)) != expected.sha256 {
+ return Err(fail("OPNSENSE_BUNDLE_INVALID"));
+ }
+ Ok(([("content-type", "application/octet-stream"), ("cache-control", "private, no-store"),
+ ("x-content-type-options", "nosniff")], raw).into_response())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn bundle(files: &[(&str, u64)]) -> Manifest {
+ Manifest { schema_version: 1, version: "0.1.0-opnsense.6".into(),
+ files: files.iter().map(|(n, b)| (n.to_string(), File { sha256: "a".repeat(64), bytes: *b })).collect() }
+ }
+
+ #[test]
+ fn bundle_needs_the_program_files_and_accepts_only_known_names() {
+ let base = [("agent.pyz", 10), ("collect.pyz", 10), ("discovery_agent", 10)];
+ assert!(valid(&bundle(&base)));
+ assert!(valid(&bundle(&[base[0], base[1], base[2], ("rmm-opnsense-jobs", 10)])));
+ assert!(!valid(&bundle(&base[..2])));
+ assert!(!valid(&bundle(&[base[0], base[1], base[2], ("../ca.pem", 10)])));
+ assert!(!valid(&bundle(&[base[0], base[1], ("discovery_agent", 0)])));
+ assert!(!valid(&bundle(&[base[0], base[1], ("discovery_agent", MAX_FILE_BYTES + 1)])));
+ let mut bad = bundle(&base);
+ bad.version = "1.0 ; rm".into();
+ assert!(!valid(&bad));
+ }
+}
diff --git a/crates/server/src/bootstrap/opnsense.sh b/crates/server/src/bootstrap/opnsense.sh
new file mode 100644
index 0000000..c63d2b9
--- /dev/null
+++ b/crates/server/src/bootstrap/opnsense.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+# OPNsense (FreeBSD amd64). Usage as root: sh opnsense.sh https://bootstrap.example
+set -eu
+umask 077
+if [ "$(id -u)" != 0 ]; then
+ echo 'Bitte als root ausfuehren (Konsole: Option 8, Shell).' >&2; exit 1
+fi
+if [ "$(uname -s)" != FreeBSD ] || [ ! -x /usr/local/sbin/opnsense-version ] || [ ! -x /usr/local/bin/python3 ]; then
+ echo 'Dieses Installationsskript ist nur fuer OPNsense.' >&2; exit 1
+fi
+work=$(mktemp -d /var/tmp/discovery-install.XXXXXX)
+trap 'rm -rf "$work"' EXIT
+cat >"$work/install-files.sh" <<'RMM_OPNSENSE_FILES'
diff --git a/crates/server/src/bootstrap/opnsense_install.py b/crates/server/src/bootstrap/opnsense_install.py
new file mode 100644
index 0000000..7e56d00
--- /dev/null
+++ b/crates/server/src/bootstrap/opnsense_install.py
@@ -0,0 +1,199 @@
+"""HTTPS installer for the OPNsense agent. Runs as root on the firewall.
+
+Downloads the fixed program bundle with the one-time enrollment code, checks
+each file against the server's size and SHA-256, installs it with the same
+steps as the development installation and enrolls the device under the
+unprivileged `discovery` account. An existing device identity is kept.
+"""
+import getpass
+import hashlib
+import http.client
+import json
+import os
+from pathlib import Path
+import re
+import ssl
+import subprocess
+import sys
+import time
+import urllib.parse
+import uuid
+
+STATE = Path('/var/db/discovery-agent')
+CONF = Path('/usr/local/etc/discovery-agent')
+APP = Path('/usr/local/libexec/discovery-agent')
+BINDING = CONF / 'job-binding.json'
+PYTHON = '/usr/local/bin/python3'
+REQUIRED = ('agent.pyz', 'collect.pyz', 'discovery_agent')
+OPTIONAL = ('rmm-opnsense-jobs',)
+MAX_FILE = 32 * 1024 * 1024
+
+
+def stop(message):
+ raise SystemExit('Installation abgebrochen: ' + message)
+
+
+def origin(raw):
+ url = urllib.parse.urlsplit(raw)
+ if (url.scheme != 'https' or not url.hostname or url.username or url.password or url.query
+ or url.fragment or url.path not in ('', '/')):
+ stop('Die Serveradresse muss eine reine HTTPS-Adresse sein, z. B. https://server.example:8443')
+ return url
+
+
+def tls():
+ context = ssl.create_default_context()
+ if not context.get_ca_certs():
+ for bundle in ('/usr/local/openssl/cert.pem', '/usr/local/etc/ssl/cert.pem', '/etc/ssl/cert.pem'):
+ if os.path.isfile(bundle):
+ context.load_verify_locations(bundle)
+ break
+ return context
+
+
+def get(base, path, token, limit):
+ conn = http.client.HTTPSConnection(base.hostname, base.port or 443, context=tls(), timeout=30)
+ try:
+ conn.request('GET', path, headers={'Authorization': 'Enrollment ' + token})
+ res = conn.getresponse()
+ if res.status == 401:
+ stop('Der Einmalcode ist ungueltig, abgelaufen oder bereits verwendet.')
+ if res.status != 200:
+ stop(f'Server antwortet auf {path} mit HTTP {res.status}.')
+ body = res.read(limit + 1)
+ if len(body) > limit:
+ stop('Antwort des Servers ist zu gross.')
+ return body
+ finally:
+ conn.close()
+
+
+def metadata(raw):
+ meta = json.loads(raw)
+ files = meta.get('files')
+ if (meta.get('schema_version') != 1 or not isinstance(files, dict)
+ or not all(name in files for name in REQUIRED)
+ or not all(name in REQUIRED + OPTIONAL for name in files)
+ or not re.fullmatch('[a-f0-9]{64}', str(meta.get('job_signing_public_key_hex')))
+ or not str(meta.get('ca_pem', '')).startswith('-----BEGIN CERTIFICATE-----')
+ or not re.fullmatch(r'[A-Za-z0-9._-]{1,64}', str(meta.get('version')))
+ or not re.fullmatch(r'https://[a-z0-9.-]+(:[0-9]{1,5})?/?', str(meta.get('gateway_origin')))):
+ stop('Der Server liefert kein gueltiges OPNsense-Paket.')
+ uuid.UUID(meta['tenant_id'])
+ for item in files.values():
+ if not (re.fullmatch('[a-f0-9]{64}', str(item.get('sha256'))) and isinstance(item.get('bytes'), int)
+ and 0 < item['bytes'] <= MAX_FILE):
+ stop('Der Server liefert kein gueltiges OPNsense-Paket.')
+ return meta
+
+
+def as_discovery(command, payload, timeout=60):
+ # `su -m` keeps root's shell; OPNsense may set an interactive menu there.
+ return subprocess.run(['su', '-m', 'discovery', '-c', command], input=payload, capture_output=True,
+ timeout=timeout, env={**os.environ, 'SHELL': '/bin/sh'})
+
+
+# Runs as `discovery`: reads the device-owned key only after dropping privileges.
+DEVICE_QUERY = """
+import http.client,json,ssl,sys
+from pathlib import Path
+p=Path('/var/db/discovery-agent')
+ctx=ssl.create_default_context(cafile='/usr/local/etc/discovery-agent/ca.pem')
+ctx.load_cert_chain(p/'device.pem',p/'device.key')
+out={}
+for name,path in (('installed','/agent/v1/releases/installed'),('trust','/agent/v1/releases/device-trust')):
+ c=http.client.HTTPSConnection(sys.argv[1],int(sys.argv[2]),context=ctx,timeout=15)
+ c.request('GET',path)
+ r=c.getresponse()
+ if r.status!=200: raise SystemExit(name+' HTTP '+str(r.status))
+ out[name]=json.loads(r.read(65537))
+print(json.dumps(out))
+"""
+
+
+def device_view(host, port):
+ res = as_discovery(f'{PYTHON} -I - {host} {port}', DEVICE_QUERY.encode())
+ if res.returncode:
+ return None
+ return json.loads(res.stdout)
+
+
+def bind_jobs(meta, host, port):
+ """Pin the job signing key for the script worker of this exact installation."""
+ view = device_view(host, port)
+ if view is None:
+ stop('Der Gerätekanal ist nicht erreichbar; die Skriptbindung wurde nicht angelegt.')
+ identity = json.loads((STATE / 'identity.json').read_bytes())
+ installed, key = view['installed'], view['trust']['job_signing_public_key_hex']
+ if (installed['tenant_id'], installed['agent_id'], installed['asset_id'], key) != (
+ meta['tenant_id'], identity['agent_id'], identity['asset_id'], meta['job_signing_public_key_hex']):
+ stop('Gerätekanal und Installationsserver widersprechen sich.')
+ payload = dict(agent_id=identity['agent_id'], asset_id=identity['asset_id'], tenant_id=meta['tenant_id'],
+ enrollment_generation=int(installed['enrollment_generation']),
+ assignment_version=int(installed['assignment_version']), public_key_hex=key)
+ with BINDING.open('x') as f:
+ os.fchmod(f.fileno(), 0o444)
+ json.dump(payload, f)
+ f.flush()
+ os.fsync(f.fileno())
+
+
+def running():
+ return all(Path(f'/var/run/discovery-{part}.pid').is_file() for part in ('agent', 'collect'))
+
+
+def main():
+ if len(sys.argv) not in (3, 5) or (len(sys.argv) == 5 and sys.argv[3] != '--token-file'):
+ stop('Aufruf: sh opnsense.sh https://server.example:8443')
+ files_script, base = Path(sys.argv[1]), origin(sys.argv[2])
+ existing = (STATE / 'identity.json').is_file() and (STATE / 'identity.json').stat().st_size > 0
+ if existing:
+ print('Vorhandene Installation gefunden. Programmdateien werden aktualisiert, die Geräteidentität bleibt.')
+ if len(sys.argv) == 5:
+ token = Path(sys.argv[4]).read_text().strip()
+ else:
+ token = getpass.getpass('Einmalcode: ').strip()
+ if not re.fullmatch('[0-9a-f]{64}', token):
+ stop('Der Einmalcode besteht aus 64 Zeichen 0-9 und a-f.')
+ meta = metadata(get(base, '/agent/v1/bootstrap/opnsense/current', token, 65536))
+ if existing and BINDING.is_file() and json.loads(BINDING.read_bytes())['tenant_id'] != meta['tenant_id']:
+ stop('Diese Installation gehört zu einem anderen Kunden. Für einen Kundenwechsel das Gerät neu aufnehmen.')
+ stage = files_script.parent / 'stage'
+ stage.mkdir(mode=0o700)
+ for name, item in sorted(meta['files'].items()):
+ data = get(base, '/agent/v1/bootstrap/opnsense/files/' + name, token, item['bytes'])
+ if len(data) != item['bytes'] or hashlib.sha256(data).hexdigest() != item['sha256']:
+ stop(f'{name} stimmt nicht mit der Prüfsumme des Servers überein.')
+ (stage / name).write_bytes(data)
+ (stage / 'ca.pem').write_text(meta['ca_pem'])
+ print(f'OPNsense-Agent {meta["version"]} geladen und geprüft. Installation läuft …')
+ subprocess.run(['sh', str(files_script), str(stage)], check=True)
+ gateway = urllib.parse.urlsplit(meta['gateway_origin'])
+ host, port = gateway.hostname, gateway.port or 443
+ if not existing:
+ res = as_discovery(f'{PYTHON} -I {APP}/agent.pyz enroll', (token + '\n').encode())
+ if res.returncode:
+ stop('Die Aufnahme beim Server ist fehlgeschlagen. Bitte einen neuen Einmalcode erzeugen.')
+ print('Gerät beim Server aufgenommen.')
+ if (APP / 'rmm-opnsense-jobs').is_file() and not BINDING.exists():
+ bind_jobs(meta, host, port)
+ print('Skriptausführung für diese Installation gebunden.')
+ before = device_view(host, port) if existing else None
+ generation = int(before['installed']['connection_generation']) if before else -1
+ subprocess.run(['service', 'discovery_agent', 'restart' if running() else 'start'], check=True,
+ stdout=subprocess.DEVNULL)
+ for _ in range(30):
+ time.sleep(3)
+ view = running() and device_view(host, port)
+ # A new client hello raises the connection generation and reports the installed version.
+ if (view and view['installed'].get('agent_version') == meta['version']
+ and int(view['installed']['connection_generation']) > generation):
+ print(f'Fertig. Das Gerät meldet sich mit Version {view["installed"]["agent_version"]} '
+ 'und erscheint in der WebUI unter Geräte.')
+ return
+ stop('Der Dienst läuft, hat sich aber noch nicht beim Server gemeldet. '
+ 'Diagnose: tail /var/log/messages und service discovery_agent restart.')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tests/browser/order-composer.spec.ts b/tests/browser/order-composer.spec.ts
index 2d763a4..ac162aa 100644
--- a/tests/browser/order-composer.spec.ts
+++ b/tests/browser/order-composer.spec.ts
@@ -165,7 +165,7 @@ test('shows the execution stop and prepares the Windows restart through a versio
expect(writes).toHaveLength(2);
});
-test('offers one Linux command for x86-64 and ARM64, a root command for Proxmox VE and keeps the code outside all installer commands', async ({ page }) => {
+test('offers one Linux command for x86-64 and ARM64, root commands for Proxmox VE and OPNsense and keeps the code outside all installer commands', async ({ page }) => {
const token = 'f'.repeat(64), api = '/api/v1/tenants/' + tenantId;
const requests: string[] = [];
await page.route(origin + '/auth/config', r => r.fulfill({ json: { dev_login_enabled: true, oidc_configured: false, oidc_login_url: null } }));
@@ -176,7 +176,8 @@ test('offers one Linux command for x86-64 and ARM64, a root command for Proxmox
if (url.pathname === '/api/v1/tenants') return r.fulfill({ json: listing(auth.tenants) });
if (url.pathname === api + '/enrollment-tokens') return r.fulfill({ json: {
token, expires_at: new Date(Date.now() + 900000).toISOString(), bootstrap_origin: 'https://bootstrap.example',
- release_platforms: ['x86_64-unknown-linux-gnu', 'aarch64-unknown-linux-gnu', 'x86_64-pc-windows-gnu']
+ release_platforms: ['x86_64-unknown-linux-gnu', 'aarch64-unknown-linux-gnu', 'x86_64-pc-windows-gnu'],
+ opnsense_installer: true
} });
return r.fulfill({ json: { ...listing([]), pending_count: 0 } });
});
@@ -187,16 +188,20 @@ test('offers one Linux command for x86-64 and ARM64, a root command for Proxmox
await enroll.getByRole('button', { name: 'Einmalcode erstellen' }).click();
await expect(enroll.getByRole('heading', { name: 'Linux (x86-64 und ARM64)' })).toBeVisible();
const commands = enroll.locator('pre');
- await expect(commands).toHaveCount(3);
+ await expect(commands).toHaveCount(4);
await expect(commands.nth(0)).toContainText("&& sudo sh linux.sh 'https://bootstrap.example'");
await expect(enroll.getByRole('heading', { name: 'Proxmox VE' })).toBeVisible();
await expect(commands.nth(1)).toHaveText("curl --fail --show-error --silent 'https://bootstrap.example/install/linux.sh' -o linux.sh && sh linux.sh 'https://bootstrap.example'");
+ await expect(enroll.getByRole('heading', { name: 'OPNsense' })).toBeVisible();
+ await expect(commands.nth(2)).toHaveText("fetch -o opnsense.sh 'https://bootstrap.example/install/opnsense.sh' && sh opnsense.sh 'https://bootstrap.example'");
expect((await commands.allTextContents()).join(' ')).not.toMatch(/enable-admin-scripts|EnableAdministratorScripts/);
await option.check();
await expect(commands.nth(0)).toContainText('--enable-admin-scripts');
await expect(commands.nth(1)).toContainText("sh linux.sh 'https://bootstrap.example' --enable-admin-scripts");
await expect(commands.nth(1)).not.toContainText('sudo');
- await expect(commands.nth(2)).toContainText('-EnableAdministratorScripts');
+ await expect(commands.nth(2)).not.toContainText('admin');
+ await expect(enroll.getByText(/Root-Helper/)).toBeVisible();
+ await expect(commands.nth(3)).toContainText('-EnableAdministratorScripts');
await expect(enroll.getByText(/Für jeden Auftrag gelten zusätzlich/)).toBeVisible();
expect((await commands.allTextContents()).join(' ')).not.toContain(token);
expect(requests).toEqual([api + '/enrollment-tokens']);
diff --git a/tools/build.py b/tools/build.py
index 8f33873..3cfabd3 100644
--- a/tools/build.py
+++ b/tools/build.py
@@ -42,7 +42,8 @@ TARGETS = {
}
REMOTE_TARGETS = ("server-arm64", "linux-arm64")
SOURCE_PATHS = ["Cargo.toml", "Cargo.lock", "rust-toolchain.toml", "crates", "proto",
- "migrations", "tools/apt-transaction", "deploy/linux", "tests"]
+ "migrations", "tools/apt-transaction", "deploy/linux",
+ "deploy/opnsense/install-dev.sh", "tests"]
def git(*args):
diff --git a/tools/dev_opnsense_bundle.py b/tools/dev_opnsense_bundle.py
new file mode 100644
index 0000000..7cd91f6
--- /dev/null
+++ b/tools/dev_opnsense_bundle.py
@@ -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()
diff --git a/web/src/main.tsx b/web/src/main.tsx
index 6471350..476f676 100644
--- a/web/src/main.tsx
+++ b/web/src/main.tsx
@@ -194,7 +194,7 @@ function AgentReleaseCatalog({ tenantId, auth }: { tenantId: string; auth: Sessi
}
function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string; auth: Session; customerName: string }) {
- const [enrollment, setEnrollment] = useState<{ token: string; expires: string; origin: string | null; platforms: string[] } | null>(null);
+ const [enrollment, setEnrollment] = useState<{ token: string; expires: string; origin: string | null; platforms: string[]; opnsense: boolean } | null>(null);
const [enrollmentError, setEnrollmentError] = useState(null);
const [issuing, setIssuing] = useState(false);
const [copied, setCopied] = useState(false);
@@ -209,6 +209,7 @@ function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string;
/** Proxmox VE is Debian on x86-64 and ships without sudo; the installer then runs from a root shell. */
const proxmoxInstaller = linuxInstaller && enrollment?.platforms.includes('x86_64-unknown-linux-gnu') ? linuxInstaller : null;
const adminFlag = adminScripts ? ' --enable-admin-scripts' : '';
+ const opnsenseInstaller = origin && enrollment?.opnsense ? `${origin}/install/opnsense.sh` : null;
const windowsPlatforms = ['x86_64-pc-windows-gnu', 'x86_64-pc-windows-msvc'].filter(platform => enrollment?.platforms.includes(platform));
const windowsInstaller = origin && windowsPlatforms.length > 0 ? `${origin}/install/windows.ps1` : null;
return Gerät aufnehmen
@@ -220,6 +221,7 @@ function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string;
if (typeof result.token !== 'string' || !/^[a-f0-9]{64}$/.test(result.token) || typeof result.expires_at !== 'string') throw new Error('Ungültiger Einmalcode');
const bootstrap = typeof result.bootstrap_origin === 'string' ? new URL(result.bootstrap_origin) : null;
setEnrollment({ token: result.token, expires: result.expires_at, origin: bootstrap?.protocol === 'https:' ? bootstrap.origin : null,
+ opnsense: result.opnsense_installer === true,
platforms: Array.isArray(result.release_platforms) ? result.release_platforms.filter((p): p is string => typeof p === 'string') : [] });
} catch (error) { setEnrollmentError(errorText(error)); } finally { setIssuing(false); }
}}>Einmalcode erstellen
@@ -235,9 +237,9 @@ function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string;
Installation auf dem neuen Gerät
- Öffnen Sie auf dem neuen Linux-Gerät ein Terminal mit sudo-Rechten, auf einem Proxmox-VE-Host eine Shell als root oder auf dem neuen Windows-Gerät Windows PowerShell 5.1 (64 Bit) als Administrator.
+ Öffnen Sie auf dem neuen Linux-Gerät ein Terminal mit sudo-Rechten, auf einem Proxmox-VE-Host oder einer OPNsense-Firewall eine Shell als root oder auf dem neuen Windows-Gerät Windows PowerShell 5.1 (64 Bit) als Administrator.
Führen Sie den passenden Befehlsblock unten aus. Er lädt das Installationsskript per HTTPS vom Server und startet es.
- Fügen Sie den oben angezeigten Einmalcode ein, sobald das Skript nach Einmalcode (Linux) oder One-time enrollment token (Windows) fragt. Bestätigen Sie mit Enter. Eine vorherige Passwortabfrage von sudo gehört zur Anmeldung mit Administratorrechten.
+ Fügen Sie den oben angezeigten Einmalcode ein, sobald das Skript nach Einmalcode (Linux, Proxmox VE, OPNsense) oder One-time enrollment token (Windows) fragt. Bestätigen Sie mit Enter. Eine vorherige Passwortabfrage von sudo gehört zur Anmeldung mit Administratorrechten.
Prüfen Sie nach der Installation, ob das neue Gerät unter Geräte beim gewählten Kunden erscheint.
setAdminScripts(e.target.checked)}/>
@@ -247,7 +249,7 @@ function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string;
{' '}Für jeden Auftrag gelten zusätzlich die Skript- und Administratorrechte des Kunden und die Genehmigung in der WebUI.
}
{!enrollment && Die Downloadlinks und Befehle erscheinen hier, sobald ein Einmalcode angezeigt wird.
}
{enrollment && !origin && Für die Geräteaufnahme ist keine HTTPS-Downloadadresse konfiguriert.
}
- {origin && !linuxInstaller && !windowsInstaller && Für diesen Kunden ist derzeit kein Linux- oder Windows-Installer verfügbar.
}
+ {origin && !linuxInstaller && !windowsInstaller && !opnsenseInstaller && Für diesen Kunden ist derzeit kein Installer verfügbar.
}
{linuxInstaller && <>
Linux ({availableLinuxArchitectures.join(' und ')})
Installationsskript linux.sh
@@ -259,6 +261,14 @@ function DeviceEnrollment({ tenantId, auth, customerName }: { tenantId: string;
{' '}Der Installer richtet zusätzlich einen nur lesenden Proxmox-Zugang für Gäste und Backups ein.
{`curl --fail --show-error --silent '${proxmoxInstaller}' -o linux.sh && sh linux.sh '${origin}'${adminFlag}`}
>}
+ {opnsenseInstaller && <>
+ OPNsense
+ Installationsskript opnsense.sh .
+ {' '}Führen Sie den Befehl als root aus, über SSH oder an der Konsole mit Option 8) Shell .
+ {' '}Der Installer lädt den Agenten, prüft jede Datei gegen die Prüfsumme des Servers, nimmt die Firewall auf und startet den Dienst.
+ {adminScripts && <>{' '}Skripte mit root-Rechten brauchen auf OPNsense zusätzlich den separat eingerichteten Root-Helper; der Installer richtet ihn nicht ein.>}
+ {`fetch -o opnsense.sh '${opnsenseInstaller}' && sh opnsense.sh '${origin}'`}
+ >}
{windowsInstaller && <>
Windows (x64)
Installationsskript windows.ps1 {windowsPlatforms.length > 1 && '. Führen Sie einen der beiden Befehlsblöcke aus.'}