diff --git a/crates/server/src/bootstrap/lifecycle.py b/crates/server/src/bootstrap/lifecycle.py index 9712c52..28e4feb 100644 --- a/crates/server/src/bootstrap/lifecycle.py +++ b/crates/server/src/bootstrap/lifecycle.py @@ -494,6 +494,52 @@ def provision_serial_credential(): save(dropin, content) +def provision_proxmox_credential(): + # Proxmox VE hosts: a dedicated read-only API token (PVEAuditor) lets the + # unprivileged agent list guests and backup jobs. The secret is only shown by + # Proxmox at creation, so an existing token without a local copy is rotated. + pveum = Path('/usr/sbin/pveum') + if not (Path('/usr/bin/pveversion').is_file() and pveum.is_file()): + return + user, name = 'rmm-discovery@pve', 'inventory' + target, ca = ROOT / 'proxmox-token', ROOT / 'pve-ca.pem' + dropin = UNIT_DIR / (SERVICE + '.d') / '41-proxmox-api.conf' + content = f'[Service]\nLoadCredential=rmm-proxmox-token:{target}\nEnvironment=RMM_PROXMOX_CA_FILE={ca}\n'.encode() + require(not dropin.parent.is_symlink(), 'proxmox credential directory conflict') + + def pve(*args): + return subprocess.run([str(pveum), *args], check=True, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, timeout=60).stdout + + try: + save(ca, Path('/etc/pve/pve-root-ca.pem').read_bytes(), 0o644) + current = target.read_text().strip() if target.is_file() else '' + if not current.startswith(f'{user}!{name}='): + users = json.loads(pve('user', 'list', '--output-format', 'json')) + if not any(u.get('userid') == user for u in users): + pve('user', 'add', user, '--comment', 'Rust Discovery inventory (read-only)') + tokens = json.loads(pve('user', 'token', 'list', user, '--output-format', 'json')) + if any(t.get('tokenid') == name for t in tokens): + pve('user', 'token', 'remove', user, name) + created = json.loads(pve('user', 'token', 'add', user, name, '--privsep', '1', + '--comment', 'Rust Discovery agent', '--output-format', 'json')) + secret = created['value'] + require(re.fullmatch('[0-9a-f-]{36}', secret), 'unexpected proxmox token format') + save(target, f'{user}!{name}={secret}\n'.encode(), 0o600) + # A privilege-separated token holds the intersection of user and token rights. + pve('acl', 'modify', '/', '--users', user, '--roles', 'PVEAuditor') + pve('acl', 'modify', '/', '--tokens', f'{user}!{name}', '--roles', 'PVEAuditor') + except (OSError, ValueError, KeyError, subprocess.SubprocessError): + print('Proxmox VE read access could not be prepared; guest and backup inventory stays unavailable', + file=sys.stderr) + return + if dropin.exists() or dropin.is_symlink(): + require(not dropin.is_symlink() and dropin.read_bytes() == content, 'proxmox credential configuration conflict') + else: + dropin.parent.mkdir(mode=0o755, exist_ok=True) + save(dropin, content) + + def enable(): for name, data in units().items(): path = UNIT_DIR / name @@ -502,6 +548,7 @@ def enable(): else: save(path, data.encode()) provision_serial_credential() + provision_proxmox_credential() service('daemon-reload') service('enable', '--now', SERVICE)