feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s

This commit is contained in:
BuTzZ
2026-11-12 22:06:25 +01:00
commit 6c3203d725
194 changed files with 49234 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
FROM rust:1.99.1-bookworm AS build
WORKDIR /src
COPY Cargo.toml Cargo.lock rust-toolchain.toml ./
COPY crates ./crates
COPY migrations ./migrations
COPY tools/apt-transaction/apt_transaction.py tools/apt-transaction/hook ./tools/apt-transaction/
COPY deploy/linux/rmm-apt-helper.service ./deploy/linux/rmm-apt-helper.service
RUN cargo build --locked --release -p rmm-server -p rmm-agent
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends libssl3 ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=build /src/target/release/rmm-server /usr/local/bin/rmm-server
COPY --from=build /src/target/release/rmm-agent /usr/local/bin/rmm-agent
USER 65532:65532
ENTRYPOINT ["/usr/local/bin/rmm-server"]
+54
View File
@@ -0,0 +1,54 @@
# Isolated local browser integration — no VM execution
`run_pending.py` uses existing test bootstrap helpers to enroll a real mTLS device
fixture on the local dev backend, never a forged database agent identity. It sends
a ClientHello advertising `run_script` only to exercise plan validation; it does
not implement an executor, ask for a permit, authorize or report an execution.
The browser sends a durable pending request via the actual UI. The fixture is
revoked afterwards; its pending request and enrollment history are retained.
An artifact-count guard checks that jobs/outbox/grants did not increase.
Requirements: initialized/running dev Docker backend on loopback 18443, built UI
served from `web/dist`, public CA `devstate/pki/ca.pem`, private existing browser
login `devstate/browser-login.json` (owned, mode 0600). Credential values are never
CLI arguments, printed or copied into source. No VM credentials are used.
Project-local tools (commands for reproducing setup; actual execution occurred in
the implementation session):
```sh
cd web
npm ci --ignore-scripts
PLAYWRIGHT_BROWSERS_PATH="$PWD/node_modules/.cache/playwright-browsers" npx playwright install chromium
mkdir -p node_modules/.cache/nss-tools
cd node_modules/.cache/nss-tools
apt-get download libnss3-tools
dpkg-deb -x libnss3-tools*.deb root
```
No package installed globally. The runner uses installed `certutil` if available,
otherwise the locally extracted binary. It imports the **public** CA into isolated
`web/node_modules/.cache/browser-home/.pki/nssdb`, with browser HOME restricted to
that directory. Existing global browser/user trust and hosts files are untouched.
DNS overrides map only fixed test names to 127.0.0.1; TLS/SNI still verifies the
original DNS name. No certificate bypass flag or ignoreHTTPS option exists.
From repository root:
```sh
python3 deploy/browser/run_pending.py
```
Real Playwright tests verify normal trusted HTTPS plus rejection of wrong DNS name
and an untrusted CA. The positive UI case only plans/submits and never clicks
approval. CSRF is tested by deleting the token from an actual logout UI request;
403 and continued session visibility are required, followed by valid logout.
No direct API/DB approval path is used. Reporter suppresses raw errors, action logs,
stdout/stderr; traces/screenshots/video are off. Failure artifacts remain private
and must not be published. Sanitized evidence records booleans/statuses only, no
hostnames/keys/passwords. Bounded retries are not automatic: a failing run fails;
a separate rerun must reauthenticate and creates a new explicit pending plan.
`RMM_E2E_MODE=execute` is an additional separate opt-in for the prepared positive
approval/result test, to use only after a real authorized agent path is ready.
This pending-only run is neither native VM evidence nor positive approval proof.
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""Explicit future dev/test UI click for one protected immutable case file."""
import argparse
import os
import shutil
import subprocess
import sys
from run_pending import CA, ROOT, STATE, quiet_run
sys.path.insert(0, str(ROOT / 'deploy'))
from vm_mvp_acceptance import protected_json, require, EXECUTABLE
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--case-file', required=True)
parser.add_argument('--dev-test-ui-approve', action='store_true', required=True)
parser.add_argument('--prepare-only', action='store_true')
args = parser.parse_args()
value = protected_json(args.case_file)
require(value.get('profile') == 'dev/test' and value.get('case') in EXECUTABLE
and value.get('expected_principal') == 'simulated-testprincipal-a', 'fixed_dev_test_case_required')
if args.prepare_only:
require(value.get('phase')=='prepare' and value['request']['parameters']['execution_identity']=='root', 'root_UI_preparation_only')
cache = ROOT / 'web/node_modules/.cache'
home = cache / 'browser-home'
nss = home / '.pki/nssdb'; nss.mkdir(parents=True, exist_ok=True, mode=0o700); home.chmod(0o700)
certutil = shutil.which('certutil') or str(cache / 'nss-tools/root/usr/bin/certutil')
if not (nss / 'cert9.db').exists():
quiet_run([certutil, '-N', '-d', 'sql:' + str(nss), '--empty-password'])
quiet_run([certutil, '-A', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-t', 'C,,', '-i', str(CA)])
quiet_run([certutil, '-V', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-u', 'L'])
env = {k: v for k, v in os.environ.items() if k not in ('DEBUG', 'PWDEBUG', 'NODE_DEBUG')}
env.update(RMM_E2E_PROFILE='dev/test', RMM_E2E_MODE='mvp-prepare' if args.prepare_only else 'mvp-actions', RMM_E2E_CASE_FILE=args.case_file,TZ='UTC',
RMM_E2E_LOGIN_FILE=str(STATE / 'browser-login.json'), PLAYWRIGHT_BROWSERS_PATH=str(cache / 'playwright-browsers'))
return subprocess.run(['npm', 'run', 'test:browser', '--', 'mvp-actions.spec.ts'], cwd=ROOT / 'web', env=env).returncode
if __name__ == '__main__':
try:
sys.exit(main())
except Exception:
print('MVP_BROWSER_INCOMPLETE: private diagnostics withheld')
sys.exit(2)
+76
View File
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
"""Local authenticated synthetic fixture + isolated browser trust; never approve/run a job.
Private login/operator/enrollment values are consumed by existing helpers/browser code,
never printed, passed in argv or placed in browser artifacts. No VM access.
"""
import json
import os
from pathlib import Path
import shutil
import ssl
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / 'tests'))
from backend_contract import request # noqa: E402
from local_integration import STATE, CA, A, sql, token, WebSocket, envelope, field # noqa: E402
def quiet_run(args, **kwargs):
result = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **kwargs)
if result.returncode:
raise RuntimeError('private subprocess failed')
def main():
cache = ROOT / 'web/node_modules/.cache'
home = cache / 'browser-home'
nss = home / '.pki/nssdb'
nss.mkdir(parents=True, exist_ok=True, mode=0o700)
home.chmod(0o700)
certutil = shutil.which('certutil') or str(cache / 'nss-tools/root/usr/bin/certutil')
if not (nss / 'cert9.db').exists():
quiet_run([certutil, '-N', '-d', 'sql:' + str(nss), '--empty-password'])
quiet_run([certutil, '-A', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-t', 'C,,', '-i', str(CA)])
quiet_run([certutil, '-V', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-u', 'L'])
baseline = sql('SELECT (SELECT count(*) FROM jobs)||\':\'||(SELECT count(*) FROM outbox)||\':\'||(SELECT count(*) FROM execution_permits);')
agent = None
stage = 'authenticated fixture enrollment'
try:
with tempfile.TemporaryDirectory(prefix='browser-fixture-', dir=STATE) as folder:
folder = Path(folder)
quiet_run(['openssl', 'req', '-new', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1', '-nodes', '-keyout', str(folder/'device.key'), '-out', str(folder/'device.csr'), '-subj', '/CN=synthetic-local-browser-contract'])
status, _, enrolled = request('/agent/v1/enroll', 'POST', {'token': token(A), 'csr_pem': (folder/'device.csr').read_text()})
if status != 200: raise RuntimeError('fixture enrollment failed')
agent = enrolled['agent_id']
(folder/'device.pem').write_text(enrolled['certificate_pem'])
context = ssl.create_default_context(cafile=str(CA)); context.load_cert_chain(folder/'device.pem', folder/'device.key')
ws = WebSocket(context)
try:
ws.send(envelope(agent, 1, 0, 10, field(1, 'synthetic-local-browser-contract') + field(2, 'run_script')))
if ws.receive()[0] != 2: raise RuntimeError('fixture mTLS hello failed')
stage = 'browser TLS and pending-only UI checks'
env = {k: v for k, v in os.environ.items() if k not in ('DEBUG', 'PWDEBUG', 'NODE_DEBUG')}
env.update(RMM_E2E_PROFILE='dev/test', RMM_E2E_MODE='pending-only', RMM_E2E_LOGIN_FILE=str(STATE/'browser-login.json'), RMM_E2E_ASSET_ID=enrolled['asset_id'], PLAYWRIGHT_BROWSERS_PATH=str(cache/'playwright-browsers'))
result = subprocess.run(['npm', 'run', 'test:browser', '--', 'pending.spec.ts'], cwd=ROOT/'web', env=env)
if result.returncode: raise RuntimeError('browser checks failed')
stage = 'preapproval execution artifact guard'
after = sql('SELECT (SELECT count(*) FROM jobs)||\':\'||(SELECT count(*) FROM outbox)||\':\'||(SELECT count(*) FROM execution_permits);')
if baseline != after: raise RuntimeError('execution artifacts changed; review concurrent activity')
finally:
ws.close()
except Exception:
print('FAIL: ' + stage + '; private details suppressed.', file=sys.stderr)
return 1
finally:
if agent:
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print('PASS: isolated browser trust, authenticated local fixture, pending-only UI, no new jobs/outbox/grants. No VM or approval/execution evidence.')
return 0
if __name__ == '__main__':
sys.exit(main())
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
"""Explicit UI-only approval of an already pending Hello-World VM operation.
Planned for use after controlled non-root deployment and sync acceptance. This
runner does not provision an agent, create a plan, or bypass a blocked VM gate.
It uses the same isolated NSS store as run_pending.py, never global trust.
"""
import argparse
import os
from pathlib import Path
import shutil
import subprocess
import sys
import uuid
from run_pending import CA, ROOT, STATE, quiet_run
def identifier(value):
return str(uuid.UUID(value))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--operation-id', type=identifier, required=True)
parser.add_argument('--asset-id', type=identifier, required=True)
parser.add_argument('--dev-test-approve', action='store_true', required=True)
args = parser.parse_args()
cache = ROOT / 'web/node_modules/.cache'
home = cache / 'browser-home'
nss = home / '.pki/nssdb'
nss.mkdir(parents=True, exist_ok=True, mode=0o700)
home.chmod(0o700)
certutil = shutil.which('certutil') or str(cache / 'nss-tools/root/usr/bin/certutil')
if not (nss / 'cert9.db').exists():
quiet_run([certutil, '-N', '-d', 'sql:' + str(nss), '--empty-password'])
quiet_run([certutil, '-A', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA',
'-t', 'C,,', '-i', str(CA)])
quiet_run([certutil, '-V', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-u', 'L'])
env = {k: v for k, v in os.environ.items() if k not in ('DEBUG', 'PWDEBUG', 'NODE_DEBUG')}
env.update(RMM_E2E_PROFILE='dev/test', RMM_E2E_MODE='vm-hello',
RMM_E2E_LOGIN_FILE=str(STATE / 'browser-login.json'),
RMM_E2E_ASSET_ID=args.asset_id, RMM_E2E_OPERATION_ID=args.operation_id,
PLAYWRIGHT_BROWSERS_PATH=str(cache / 'playwright-browsers'))
return subprocess.run(['npm', 'run', 'test:browser', '--', 'vm-jobs.spec.ts'],
cwd=ROOT / 'web', env=env).returncode
if __name__ == '__main__':
try:
sys.exit(main())
except Exception:
print('FAIL: VM browser runner incomplete; private diagnostics withheld', file=sys.stderr)
sys.exit(1)
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
"""Verify same-origin build parity with normal CA/SAN validation, no credentials."""
import hashlib
import http.client
import json
from pathlib import Path
import re
import socket
import ssl
import sys
ROOT = Path(__file__).resolve().parents[2]
class LoopbackHTTPS(http.client.HTTPSConnection):
def connect(self):
sock = socket.create_connection(('127.0.0.1', 18443), timeout=10)
self.sock = self._context.wrap_socket(sock, server_hostname='rmm-dev.internal')
def get(path):
connection = LoopbackHTTPS('rmm-dev.internal', 18443, context=ssl.create_default_context(cafile=str(ROOT / 'devstate/pki/ca.pem')), timeout=10)
try:
connection.request('GET', path)
response = connection.getresponse()
data = response.read(2097153)
if response.status != 200 or len(data) > 2097152:
raise RuntimeError('bounded asset request failed')
return data
finally:
connection.close()
def main():
local_index = (ROOT / 'web/dist/index.html').read_bytes()
remote_index = get('/')
paths = re.findall(r'(?:src|href)="(/assets/[A-Za-z0-9_.-]+)"', local_index.decode('utf-8'))
if not paths:
raise RuntimeError('no build asset references')
rows = []
for path in ['/', *paths]:
local = local_index if path == '/' else (ROOT / 'web/dist' / path.lstrip('/')).read_bytes()
remote = remote_index if path == '/' else get(path)
rows.append({'path': path, 'local_sha256': hashlib.sha256(local).hexdigest(), 'served_sha256': hashlib.sha256(remote).hexdigest(), 'equal': local == remote})
result = {'verified_tls': True, 'same_origin': True, 'parity': all(row['equal'] for row in rows), 'assets': rows}
print(json.dumps(result, indent=2))
return 0 if result['parity'] else 1
if __name__ == '__main__':
try:
sys.exit(main())
except Exception:
print('Asset verification failed; no response contents or private configuration printed.', file=sys.stderr)
sys.exit(1)
+65
View File
@@ -0,0 +1,65 @@
name: rust-discovery-dev
services:
postgres:
image: postgres:17.6-bookworm
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?run deploy/dev.py init}
volumes:
- pgdata:/var/lib/postgresql/data
- ../devstate/init.sql:/docker-entrypoint-initdb.d/10-rmm.sql:ro
healthcheck:
test: [CMD-SHELL, "pg_isready -U postgres -d rmm"]
interval: 2s
timeout: 3s
retries: 30
networks: [backend]
migrate:
build:
context: ..
dockerfile: deploy/Dockerfile
image: rust-discovery-dev:local
command: [migrate]
environment:
MIGRATION_DATABASE_URL: ${MIGRATION_DATABASE_URL:?}
depends_on:
postgres:
condition: service_healthy
networks: [backend]
server:
image: rust-discovery-dev:local
environment:
RMM_PROFILE: development
RMM_DEV_AUTH: "true"
RMM_SERVER_ROLE: all
DATABASE_URL: ${DATABASE_URL:?}
RMM_VIEWER_A: ${RMM_VIEWER_A:?}
RMM_VIEWER_B: ${RMM_VIEWER_B:?}
RMM_TLS_DIR: /pki
RMM_JOB_KEY_FILE: /jobs/ed25519.pem
RMM_WEB_DIR: /web
RMM_DEV_USERNAME: ${RMM_DEV_USERNAME:-}
RMM_DEV_PASSWORD: ${RMM_DEV_PASSWORD:-}
RMM_OPERATOR_A: ${RMM_OPERATOR_A:-}
RMM_OPERATOR_B: ${RMM_OPERATOR_B:-}
ports: ["127.0.0.1:18443:18443"]
volumes:
- ../devstate/pki:/pki:ro
- ../devstate/jobs:/jobs
- ../web/dist:/web:ro
user: "${RMM_UID:?}:${RMM_GID:?}"
read_only: true
cap_drop: [ALL]
security_opt: [no-new-privileges:true]
depends_on:
migrate:
condition: service_completed_successfully
# Docker does not publish ports on an internal-only bridge on this host.
# Only the TLS server joins ingress; PostgreSQL remains backend-only.
networks: [backend, ingress]
restart: unless-stopped
networks:
ingress:
backend:
internal: true
volumes:
pgdata:
+102
View File
@@ -0,0 +1,102 @@
#!/usr/bin/env python3
"""Local development administration only. Never print generated secrets."""
import hashlib
import os
from pathlib import Path
import secrets
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
STATE = ROOT / "devstate"
OPENSSL_CONFIG = ROOT / "deploy/openssl.cnf"
TENANTS = {"a": "11111111-1111-4111-8111-111111111111", "b": "22222222-2222-4222-8222-222222222222"}
def private(path, data, mode=0o600):
with open(os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, mode), "w") as f:
f.write(data)
os.chmod(path, mode)
def compose(*args, **kwargs):
return subprocess.run(["docker", "compose", "--env-file", str(STATE / ".env"), "-f", str(ROOT / "deploy/compose.yaml"), "-p", "rust-discovery-dev", *args], check=True, **kwargs)
def init():
STATE.mkdir(mode=0o700, exist_ok=True)
os.chmod(STATE, 0o700)
if (STATE / ".env").exists():
raise SystemExit("development state already initialized; refusing to overwrite keys")
passwords = {k: secrets.token_hex(32) for k in ("postgres", "migrator", "app", "a", "b")}
private(STATE / ".env", f"POSTGRES_PASSWORD={passwords['postgres']}\nMIGRATION_DATABASE_URL=postgres://rmm_migrator:{passwords['migrator']}@postgres/rmm\nDATABASE_URL=postgres://rmm_app:{passwords['app']}@postgres/rmm\nRMM_VIEWER_A={passwords['a']}\nRMM_VIEWER_B={passwords['b']}\nRMM_UID={os.getuid()}\nRMM_GID={os.getgid()}\n")
for tenant in TENANTS:
private(STATE / f"viewer-{tenant}.token", passwords[tenant])
# Readable inside postgres container; host parent directory remains 0700.
private(STATE / "init.sql", f"CREATE ROLE rmm_migrator LOGIN NOSUPERUSER NOBYPASSRLS PASSWORD '{passwords['migrator']}';\nCREATE ROLE rmm_app LOGIN NOSUPERUSER NOBYPASSRLS PASSWORD '{passwords['app']}';\nCREATE DATABASE rmm OWNER rmm_migrator;\n\\connect rmm\nALTER SCHEMA public OWNER TO rmm_migrator;\nREVOKE CREATE ON SCHEMA public FROM PUBLIC;\n", 0o444)
pki = STATE / "pki"
pki.mkdir(mode=0o700)
commands = [
["req", "-x509", "-config", str(OPENSSL_CONFIG), "-extensions", "ca", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:secp384r1", "-nodes", "-keyout", str(pki / "ca.key"), "-out", str(pki / "ca.pem"), "-days", "365", "-subj", "/CN=RMM local development CA"],
["req", "-new", "-config", str(OPENSSL_CONFIG), "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:prime256v1", "-nodes", "-keyout", str(pki / "server.key"), "-out", str(pki / "server.csr"), "-subj", "/CN=rmm-dev.internal"],
["x509", "-req", "-in", str(pki / "server.csr"), "-CA", str(pki / "ca.pem"), "-CAkey", str(pki / "ca.key"), "-CAcreateserial", "-out", str(pki / "server.pem"), "-days", "30", "-extfile", str(OPENSSL_CONFIG), "-extensions", "server"],
]
for args in commands:
subprocess.run(["openssl", *args], env=dict(os.environ, OPENSSL_CONF=str(OPENSSL_CONFIG)), check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
for path in pki.iterdir():
path.chmod(0o600)
jobs_auth()
print("Private development configuration and CA generated under devstate/.")
def token(tenant):
if tenant not in TENANTS:
raise SystemExit("tenant must be a or b")
value = secrets.token_hex(32)
digest = hashlib.sha256(value.encode()).hexdigest()
sql = f"INSERT INTO enrollment_tokens(token_hash,tenant_id,expires_at) VALUES('{digest}','{TENANTS[tenant]}',now()+interval '15 minutes');\n"
compose("exec", "-T", "postgres", "psql", "-U", "postgres", "-d", "rmm", "-v", "ON_ERROR_STOP=1", input=sql.encode(), stdout=subprocess.DEVNULL)
path = STATE / f"enroll-{tenant}.token"
if path.exists():
path.unlink()
private(path, value)
print(f"One-use 15-minute enrollment token written to devstate/enroll-{tenant}.token (not displayed).")
def jobs_auth():
"""Add dedicated interactive and MCP credentials, never reuse viewer tokens."""
path = STATE / ".env"
current = path.read_text()
if "RMM_DEV_USERNAME=" in current:
raise SystemExit("job authentication already configured; refusing credential rotation")
username = "simulated-testprincipal-a"
password, operator_a, operator_b = (secrets.token_hex(32) for _ in range(3))
with path.open("a") as f:
f.write(f"RMM_DEV_USERNAME={username}\nRMM_DEV_PASSWORD={password}\nRMM_OPERATOR_A={operator_a}\nRMM_OPERATOR_B={operator_b}\n")
private(STATE / "browser-login.json", __import__("json").dumps({"username": username, "password": password}))
private(STATE / "operator-a.token", operator_a)
private(STATE / "operator-b.token", operator_b)
folder = STATE / "jobs"
folder.mkdir(mode=0o700, exist_ok=True)
folder.chmod(0o700)
print("Dedicated dev browser/MCP credentials generated under private devstate; no approval performed.")
if __name__ == "__main__":
os.umask(0o077)
command = sys.argv[1:]
if command == ["init"]:
init()
elif len(command) == 2 and command[0] == "token":
token(command[1])
elif command == ["up"]:
compose("build", "migrate")
compose("up", "-d", "--pull", "never", "server")
elif command == ["jobs-auth"]:
jobs_auth()
elif command == ["down"]:
compose("down")
elif command == ["status"]:
compose("ps")
else:
raise SystemExit("usage: python3 deploy/dev.py init|jobs-auth|up|down|status|token a|token b")
+277
View File
@@ -0,0 +1,277 @@
# Linux agent/helper bootstrap contract (not a VM acceptance)
**Current integrated runtime contract:** [runtime-README.md](runtime-README.md).
The module-only blockers and integration steps below record the earlier increments;
they are historical and superseded by that runtime document. In particular Sync,
HTTPS, rotation and authenticated helper apply are now wired in source, not VM-
accepted. Use the current document and installed units for new bootstrap work.
Dedicated APT runner, exact config/unit/CLI/readiness/agent-routing preflight:
[`apt-helper-README.md`](apt-helper-README.md). This newer profile does not weaken
the existing general/readiness helper unit. Native VM measurements remain open.
No install/start/reboot command in this document has been executed on the VM.
No RustDesk component is installed. Deploy only the artifacts/configuration for
the explicitly agreed test; do not copy central signing/TLS private keys.
## Built artifacts and process configuration
Planned build command (the debug test/build commands actually executed are listed
in `tests/agent_jobs/README.md`):
```sh
cargo build --release -p rmm-agent -p rmm-privileged-helper
```
Both binaries are environment-configured, **not** positional-argument CLIs.
Agent: `RMM_AGENT_STATE` (default `devstate/agent`), `RMM_CA_FILE` (default
`devstate/pki/ca.pem`), `RMM_CONNECT_ADDR` (default `127.0.0.1:18443`),
`RMM_ENROLLMENT_TOKEN_FILE` (first registration only), and optional
`RMM_JOB_BINDING_FILE`, plus optional `RMM_HELPER_SOCKET` for explicit-root grants.
The TCP override never changes the TLS DNS/SNI name
`rmm-dev.internal`. No proxy or invalid-certificate bypass. Without a binding,
the agent remains passive. It refuses to advertise/execute actions as UID 0.
Provision a dedicated unprivileged `rmm-agent` system account/group. Install the
agent binary root-owned and non-writable by that account at
`/usr/local/libexec/rmm-agent`. State must be owned by that account, mode 0700;
its files are 0600. The sample service uses systemd `StateDirectory` and no
ambient capabilities. Install only the public CA at `/etc/rmm/ca.pem`. An
optional root-owned 0600 `/etc/rmm/agent.env` supplies the first-enrollment token
**path**, never an inline token. Remove that bootstrap token after registration.
Retain the enrolled VM-generated private device key on the VM.
The operator must provision the job signing key over the authenticated bootstrap
channel: authenticated `GET https://rmm-dev.internal:18443/api/v1/job-signing-key`
using the verified CA, checking `algorithm == "Ed25519"`, `schema_version == 1`,
32-byte `public_key_hex` and `key_id == SHA256(raw_public_key)`. Do not trust a key
sent alongside a job, use an unauthenticated GET, put bearer credentials in
command arguments, or substitute TLS/update keys. This increment does not
rotate this trust anchor. Optional automatic agent fetching is described below;
the helper remains independently provisioned. The authenticated operator/bootstrap
must also resolve the enrolled agent's tenant, asset, generation and assignment
from server records, not accept an offer as identity provisioning.
Agent binding JSON, owned by `rmm-agent`, 0600, in a root-owned configuration
directory (replace every placeholder; invalid placeholders intentionally fail):
```json
{
"tenant_id": "PROVISIONED-TENANT-UUID",
"asset_id": "ENROLLED-ASSET-UUID",
"agent_id": "ENROLLED-AGENT-UUID",
"enrollment_generation": 1,
"assignment_version": 1,
"public_key_hex": "AUTHENTICATED-32-BYTE-KEY-IN-HEX"
}
```
The agent additionally compares asset/agent IDs to its enrollment response.
Generation/assignment changes require an authenticated re-provisioning procedure,
not an in-band config assertion. State lock, integrity check and journal capacity
errors fail closed; never delete journal/consumption files to force a retry.
## Independent helper boundary
Helper: `RMM_HELPER_CONFIG`, default `/etc/rmm/helper.json`. It requires UID 0,
a root-owned 0600 config, root-owned private 0700 state directory, and a
root-owned socket parent not writable by group/others. `/run/rmm-helper` may be
0750 with group `rmm-agent`. The root-owned socket is 0660 with the configured
group; `SO_PEERCRED` additionally requires **exactly** the configured non-root
agent UID. Group membership alone grants no execution authority.
Example `/etc/rmm/helper.json` (root:root, 0600):
```json
{
"binding": {
"tenant_id": "PROVISIONED-TENANT-UUID",
"asset_id": "ENROLLED-ASSET-UUID",
"agent_id": "ENROLLED-AGENT-UUID",
"enrollment_generation": 1,
"assignment_version": 1,
"public_key_hex": "AUTHENTICATED-32-BYTE-KEY-IN-HEX"
},
"agent_uid": 12345,
"socket_gid": 12345,
"socket": "/run/rmm-helper/control.sock",
"state": "/var/lib/rmm-helper",
"allowed_services": ["rmm-acceptance.service"],
"allow_reboot": false,
"allow_root_scripts": false
}
```
Use the actual dedicated account's numeric UID/GID, not the example numbers.
The helper re-verifies exact Ed25519 bytes, all identity/hash/action/parameter
bindings, a <=60-second grant interval, synchronized bounded host clock, explicit
`execution_identity:"root"`, local action policy and its **own** SQLite consumption
ledger before invoking the typed OS provider. A request-provided bearer, key or
identity assertion is never an authority. The config has a separate, default-off
root-script permission; enabling it is consciously broad administration, still
subject to the sample service sandbox. Only exact allowed service units with
running/stopped state and >=180s runtime are accepted; restart is not silently
rewritten as start. Reboot is default-off and never yields exit-code success.
IPC framing: one big-endian u32 length plus JSON `Request` (<=512 KiB) per Unix
connection. Fields are `job_bytes`, `permit_bytes`, `signature` (JSON byte arrays),
`plan_hash`, `authorization_id`. Response has the same framing and an `Outcome`.
Read/write deadlines are 5s. No request errors/secrets are echoed. The server
process is serial; unknown effects retain its global device resource lock, even
after restart. Duplicate consumed grants return stored evidence/unknown, never
execute again. A new grant for an already consumed attempt is also refused.
The helper refuses a preexisting socket; systemd removes its RuntimeDirectory
on stop. Manual stale-socket recovery requires checking that no helper is alive,
not blindly unlinking a path at startup.
## Explicit integration/acceptance blockers
* Backend currently permits only `agent_service`. Network agent intentionally
advertises only `run_script` and `refresh_inventory`, not privileged actions.
Network-agent → Unix-helper invocation is wired for explicit-root signed jobs
when `RMM_HELPER_SOCKET` is configured, with root socket/peer authentication.
A backend-authorized explicit root identity plus helper capability negotiation
must land before service/reboot/root-script E2E tests. Helper policy denies
unconfigured actions independently; the agent never relabels a service-identity
grant into root authority.
* Real APT mutation remains unsupported. A simulation followed by `apt-get`
cannot guarantee exact approved resolution under dpkg fcntl locks or suppress
arbitrary maintainer-script reboots. Neither `ensure_package` nor
`apply_updates` is advertised. No false flock-on-dpkg-lock implementation.
* No backend SyncRequest/Response exists. Only the **first** authenticated session
of a journal may start effects. On reconnect/restart new effects are blocked;
in-flight reservations become unknown, durable evidence still replays and
heartbeats remain fresh. No DB deletion/new-grant shortcut. Existing previously
connected state also needs real synchronization before jobs can be enabled.
* Running-script cancellation is recorded but not claimed as confirmed abort;
bounded execution finishes and reports its real result. A reboot does not
complete without authenticated new-boot evidence; automatic reboot recovery
synchronization is not implemented.
* Extended collectors now map to bounded per-source observations inside the
existing `linux.base` JSON, not new protobuf scopes/tags. Enable only after
backend JSON-extension acceptance is confirmed, as described below.
* No VM deployment, systemd install, root action, package/update/reboot host
effects or real UI approval were performed by this increment.
## Additional environment variables and bootstrap (second increment)
* `RMM_JOB_SIGNING_KEY_BOOTSTRAP=1`: after enrollment, use **device mTLS** to GET
`/api/v1/job-signing-key` over verified TLS. DNS/SNI remains `rmm-dev.internal`;
`RMM_CONNECT_ADDR` changes TCP only. Built-in roots, proxies and redirects are
disabled. The response is bounded to 4096 bytes and Ed25519/schema/key-hash
checked. No bearer is copied into the agent. A preconfigured binding key must
match; an empty key allows initial authenticated provisioning. The resulting
`RMM_AGENT_STATE/job-signing-key.json` is private and create-only. A changed
existing pin **fails**, never silently rotates. Keep the bootstrap flag enabled
on subsequent starts when the binding's key is empty, so the endpoint is
reauthenticated and checked against the cached pin. If the current backend
endpoint accepts bearer identity only, bootstrap fails closed; backend must
explicitly authorize the authenticated enrolled device at that endpoint.
* Helper config optional `"signing_key_file":"/etc/rmm/helper-signing-key.json"`:
root-owned 0600 JSON with `key_id`, `algorithm`, `schema_version`,
`public_key_hex` in the GET response shape. Provision via the trusted operator
bootstrap, **not** from an agent-provided file/path/request. If an inline helper
binding key is present it must match. The helper has no network key-fetch or
auto-replacement operation; agent cache changes cannot change helper trust.
* `RMM_LOG_UNITS_FILE`: private, agent-owned 0600 JSON array of 1–128 exact
`.service` names. `query_logs` is advertised only when this allowlist and a
native journald executable/runtime exist. The signed action must bind an
allowlisted unit, nonnegative UTC window <=24h, <=1000 lines, >=20s runtime and
bounded output. No arbitrary paths or caller-defined journal arguments.
Access/JSON/output failures retain quality/error metadata. The current action
has **no cursor input field**; no cursor is fabricated or advanced. Results
explicitly report `cursor_status:not_advanced_no_ack_contract` and partial
coverage until a concrete durable cursor-ACK contract exists.
* `RMM_EXTENDED_INVENTORY=1`: opt-in additive snapshot JSON fields `observations`,
`collection_started_at`, `collection_completed_at`. Every observation has
`scope`, source, real RFC3339 observation time, quality, completeness,
truncation, error and data. CPU, memory, OS, interfaces, routes, DNS, volumes,
installed packages, local update metadata and DMI are mapped; SMART remains
explicitly unsupported. Per-scope output is <=32 KiB and entire data <=512 KiB.
**Do not enable against a gateway deserializing the old deny-unknown-fields
Inventory struct.** Confirm backend storage accepts these additive JSON fields
first. Default remains compatible base JSON; no wire/protocol file changed.
Passive collection now runs in one bounded-inflight blocking worker with a
bounded completion channel, not inside the network/heartbeat loop. A reconnect
does not spawn a second collector while one is already running. A refresh job
still collects a **new** base observation for its signed `linux.base` scope;
it never returns an old stored passive snapshot as a live measurement.
Awaiting-reboot evidence now persists before effect: original native boot ID,
exact signed request, reservation time and reconnect deadline in each process's
own `execution_evidence` ledger. Awaiting/unknown outcomes persist without
unlocking the consumed attempt. Neither exit code nor a process restart supplies
authenticated new-boot completion. Cancellation arriving while a permit is
outstanding is checked before effect; running cancellation remains conservative.
An earlier future-scheduled job no longer starves later ready jobs.
## Offline package installer (not executed on VM)
`deploy/linux/install.py` is create-only. Planned invocation:
```sh
python3 deploy/linux/install.py --root /PATH/TO/STAGING \
--agent-binary target/release/rmm-agent \
--helper-binary target/release/rmm-privileged-helper --ca-file /PATH/TO/PUBLIC-CA.pem
```
Staging writes only the x86-64 ELF artifacts, public CA and unit files. A live
installation additionally requires root, `--root / --install-live` and an already
provisioned unprivileged `rmm-agent` account. It refuses overwrites, writable or
symlinked install directories, non-x86-64 artifacts and private-key CA inputs.
It does **not** create accounts/config/identities/state, call systemctl, enable or
start anything, install packages, change the network or reboot. Configure trust
and enrollment first; systemd StateDirectory/RuntimeDirectory creates the correct
runtime state when a later explicitly authorized bootstrap starts the units.
The units retain empty agent capabilities, NoNewPrivileges, private temp,
read-only system/home protection and no privileged-shell bypass. Privileged
scripts are separately default-off and remain subject to the helper sandbox.
## Concrete backend contracts needed next
### APT bridge increment (not installed or VM-tested)
The helper's typed APT bridge is implemented at
`crates/privileged-helper/src/apt.rs`, through its existing SO_PEERCRED/signature/
parent-consumption boundary. Root config has optional `apt_provider`; default is
disabled. The exact schema-2 signed manifest, additive domain action fields,
compile-time-known create-only provider installation, pre-grant sealed archive/
baseline layout, context/ledger mapping and conservative outcome contract are
documented in [`../../tools/apt-transaction/HELPER-CONTRACT.md`](../../tools/apt-transaction/HELPER-CONTRACT.md).
This does not remove the package-capability blocker: the domain must preserve
the complete typed `action.apt_manifest`, validate the narrow fixture closure,
and support dedicated `apt_native_acceptance`; the agent remains unchanged.
Legacy package/update actions without this field fail closed. No RunScript
conversion, arbitrary archive URL or unsigned IPC manifest is accepted.
Planned root-only `rmm-privileged-helper --install-apt-provider` installs only
embedded reviewed provider/hook bytes and fixed metadata, create-only. It does
not start services, create grants, install .deb packages or create acceptance.
`provision-fixture-store` prepares only the pinned fixture artifacts before any
final grant. Neither command was executed in this increment.
The existing helper unit's ProtectSystem=strict/other protections are **not**
an APT write/setup profile. A reviewed native fixture runner/unit configuration
and actual VM tests are still required; no unit restriction was relaxed here.
Do not advertise APT merely because the Rust bridge and local tests compile.
Before removing `begin_session`'s fail-closed reconnect gate, agree a versioned
authenticated SyncRequest/Response carrying device identity/assignment and
connection generation, journal identity/checkpoint, fixed job/attempt, payload
hash, grant ID/bytes or reservation ambiguity, local revision/state, original
boot ID, persisted outcomes and concrete durable event receipts. Server must
return an explicit reconciled/fenced decision, not a blanket `ok`. Unknown or
missing old reservations must retain resource locks; no response may authorize a
new attempt or silently manufacture a replacement grant. Restored/mismatched
generations must stay blocked. Reboot success requires authenticated current
envelope boot ID different from persisted original boot and bound postconditions.
Long-poll must deliver the same typed envelopes and concrete ACKs with one active
generation, including late evidence ingestion; no new endpoint/wire assumption
is implemented here. Root planning must bind explicit root identity and local
helper capabilities/policy, never reinterpret `agent_service`. The public key
GET must authorize enrolled device mTLS. Additive inventory JSON acceptance must
be confirmed before enabling extended collection in a real agent.
+60
View File
@@ -0,0 +1,60 @@
# Dev/test root-helper readiness, not package acceptance
`python3 deploy/vm_root_bootstrap.py inspect` is read-only. `build` creates a
private minimal Docker context containing only Rust sources, pinned Cargo files,
migrations and the reviewed APT-tool sources; it never includes credentials.
`install` is the separately authorized create-only systemd bootstrap. It refuses
existing root-helper paths, unresolved VM jobs, an active agent/tunnel, changed
agent identity/signing authority or incompatible artifacts. It never resets an
identity/journal or authorizes a job. Do not retry a partial install blindly.
Fixed resources: `/usr/local/libexec/rmm-privileged-helper`, root-owned 0600
`/etc/rmm/helper-dev-test-v1.json` and `helper-signing-key-v1.json`, private root
state `/var/lib/rmm-helper`, `rmm-helper-dev-test.service`, and root-owned agent
drop-in `10-dev-helper-v1.conf`. The drop-in adds only
`RMM_HELPER_SOCKET=/run/rmm-helper/control.sock`; it does not restart the agent.
The helper's root-owned socket is 0660/group `rmm-agent` in a 0750 root-owned
runtime directory. `SO_PEERCRED` separately checks exactly the provisioned
non-root agent UID; a connect-only readiness probe is not a signed IPC test.
Public signing authority comes from the authenticated, CA/name-verified server
GET and must match the preserved agent binding. No server private key is copied.
Helper trust/ledger remain inaccessible to the network service. The helper is
enabled for its expressly named dev/test service but stopped after readiness
only when its own consumption ledger is confirmed empty. No agent/tunnel is
started. Creation lists, native measurements and sanitized failure class/line
numbers are stored privately; raw stderr, payloads and exceptions are withheld.
## Intentionally disabled execution gates
The dev/test config allows only the future exact `rmm-mvp-test.service` unit;
this bootstrap **does not create or start that test service**. Root scripts and
reboot are disabled. `apt_provider` is null. Installing the compiled-in provider
via `--install-apt-provider` creates trusted code/hook and empty sealed-store
directories/metadata only. No package, fixture archive, baseline, active context,
grant or native-acceptance record is generated. `inspect-runtime` and
`print-config` are measurements, not authority. Their hashes belong to a future
explicitly UI-approved immutable plan.
`inspect-sandbox` is invoked read-only as root in the parent environment. A veto
there is expected and reported as **not passed**, never bypassed with fabricated
`host-namespaces.json`/sandbox-ready files. The actual provider requires its
signed worker context before building the final sandbox.
The new readiness unit preserves restrictive protections; it is deliberately
**not APT-execution-capable**. Package setup requires CAP_SYS_ADMIN for private
mount/PID/network namespaces and mount setup, CAP_MKNOD for the four basic device
nodes, and write access to the actual fixture package payload/dpkg/APT stores.
The final package child must then retain exactly CHOWN, DAC_OVERRIDE, FOWNER,
FSETID, SETGID, SETUID, SETPCAP and SETFCAP, NNP and seccomp; no SYS_ADMIN,
SYS_BOOT, KILL or MKNOD may remain. Host control sockets must be hidden and the
private PID1/mount/network checks must pass. The readiness unit's deny-mount
syscall policy and ProtectSystem=strict prevent this setup. Do not loosen them
blindly: a separately reviewed native APT runner/profile is still required.
Future backend root planning, manifest preservation, helper policy changes and
agent capability negotiation do not follow from installation. Reconnect/sync
must be ready before starting new jobs. Every service/package/update/reboot or
new attempt still needs its own fixed plan and actual dev/test WebUI approval.
Actual commands/results are recorded in `docs/implementation-status.md`; this
document alone claims no VM execution or Linux-MVP acceptance.
+227
View File
@@ -0,0 +1,227 @@
# Dedicated APT runner / VM preflight contract
**Implemented, built and locally tested, not deployed or measured on a VM.**
Every command below is **planned, not executed here**. No host package, service,
reboot, root installation or acceptance operation was executed in this dispatch.
Readiness is not package acceptance or authorization. Existing readiness/general
helper unit is unchanged: no broadening its write paths/capabilities/filters.
## Architecture and fixed paths
Same helper binary, two separate daemon profiles:
| Profile | Socket | Allowed operations |
|---|---|---|
| general (existing/default) | existing configured general socket | existing explicitly permitted root operations, never APT |
| `apt_only` | `/run/rmm-apt-helper/helper.sock` | read-only readiness and typed fixture APT jobs only |
APT profile requires agent UID **993**, root-owned agent-group socket ACL,
empty allowed_services, allow_reboot=false, allow_root_scripts=false and a valid
apt_provider policy. Unknown profiles/actions/CLI programs fail closed. No shell,
service, reboot or generic-program dispatch exists in apt_only. Root administrative
CLI modes are fixed install/probe/inspect operations, not network-callable argv.
**Both daemons must run the same new helper build** and use the exact same
`/var/lib/rmm-helper/consumption.sqlite` and `executor.lock` inodes. All SQLite
sidecars stay in that shared directory (current ledger uses synchronous FULL /
DELETE journal mode; no database/WAL/SHM copy or private bind). Flank lock is now
per actual request/effect, not daemon lifetime, allowing coexistence. Startup DDL
is locked; consume/execute/finish are locked. An unresolved consumed row blocks
all new effects across both profiles, including reboot/package conflicts.
No tombstone deletion, marker reset, database copy or grant replacement shortcut.
APT-owned store: `/var/lib/rmm-apt-transaction` (0700), including archives,
baselines, history, probes, state, log and active handoff. The child sees neither
the shared ledger nor `/etc/rmm` keys/config, `/var/lib/rmm-agent`, home directories,
host manager sockets or inherited private fds. Only the trusted outside root
controller sees the signing-key pin/ledger. Central private signing keys must
never be deployed to the VM.
## Installation order (planned)
1. Authenticated operator installs the reviewed newly built helper at fixed
`/usr/local/libexec/rmm-privileged-helper` using the existing controlled
artifact deployment process. Both helper daemons need this same build; do not
blindly overwrite an active old helper or restart away unknown effects.
2. Before starting the new unit, run the root-only known-payload installer:
```text
/usr/local/libexec/rmm-privileged-helper --install-apt-provider
```
It exclusively creates known embedded provider/hook files, schema-2 install
metadata, the exact `rmm-apt-helper.service` at `/etc/systemd/system/`, private
store directories and empty `/usr/share/rmm-mvp-test`. It does NOT install a
.deb, start systemd, create grants or create an acceptance record. Existing/
partial paths are refused, not overwritten, chmod-repaired or deleted.
3. Root-provision `/etc/rmm/apt-helper.json` (0600) from
`apt-helper.example.json`; replace all invalid placeholders and verify actual
rmm-agent UID is 993 and socket_gid is the actual rmm-agent group GID. Keep
the existing separately authenticated Ed25519 key pin/binding procedure.
4. Inspect the installed provider/config/runtime, including installed helper
binary and exact unit profile:
```text
/usr/bin/python3 -I /usr/lib/rmm-apt-transaction/apt_transaction.py print-config
/usr/bin/python3 -I /usr/lib/rmm-apt-transaction/apt_transaction.py inspect-runtime
```
Set approved apt_config_sha256 from exact config bytes. Set runtime_sha256
from the canonical runtime object **without its trailing newline**. Review
the contents/code/configuration; blindly hashing unreviewed configuration is
not a security review. No timestamp/hash placeholder can enable execution.
5. After valid private config/key provisioning, inspect the helper identity pin:
```text
RMM_HELPER_CONFIG=/etc/rmm/apt-helper.json /usr/local/libexec/rmm-privileged-helper --inspect-apt-profile
```
Operator installs its exact JSON output at e.g.
`/etc/rmm/apt-helper-proof.json`, root:rmm-agent 0640, in root-owned,
non-group/world-writable configuration parents. This pin contains public
identity/hash metadata, no grant or secret. Agent must be able to read it but
not alter it. The inspected executable must match the installed daemon bytes.
6. Validate the unit on the VM before start (`systemd-analyze verify`), then
perform the separately authorized unit bootstrap. `ExecStartPre` executes:
```text
RMM_HELPER_CONFIG=/etc/rmm/apt-helper.json /usr/local/libexec/rmm-privileged-helper --probe-native
```
**The probe runs under the actual dedicated service profile**, not a generic
root shell: controller verification requires NoNewPrivileges, exact ten-bit
setup capability set, zero inherited/ambient caps, the actual
rmm-apt-helper.service cgroup and readonly /usr, /etc and /var. Direct daemon/
probe invocation outside that profile is rejected. `--inspect-apt-profile`
remains a read-only administrative metadata operation and is not readiness.
Controller verification also requires an outer seccomp filter, actual writable
declared targets, and rejects unexpected writable submounts below /usr,/etc,
/var. Inspect effective systemd configuration (`systemctl cat/show`) on the VM;
do not introduce unreviewed drop-ins or substitute a root-shell launch.
## Unit and irreversible worker sandbox
`rmm-apt-helper.service` provides NNP, PrivateTmp/PrivateNetwork, strict readonly
system/home/kernel/control-group protection, AF_UNIX only, native syscall ABI,
mount/PID/net namespaces only, no ambient caps. Bounding set is filesystem/UID
bits 0,1,3,4,6,7,8,31 plus SYS_ADMIN/MKNOD for trusted setup; no SYS_BOOT,
SYS_CHROOT or SYS_TIME. DevicePolicy=strict permits only null/zero/random/urandom
rwm; PrivateDevices=no permits trusted construction of a minimal private /dev.
Outer filter allows mount/unshare/fork/capset/prctl/seccomp trusted setup instead
of an outer @mount prohibition. Reboot/module/raw-io/swap/debug and clock setters
remain denied. The narrow adjtimex exception is necessary for the existing
read-only modes=0 trusted-clock query; mutating adjtimex still needs CAP_SYS_TIME,
which is absent. This is not a clock-verification bypass.
Write paths are **only** `/var/lib/dpkg`, shared helper ledger, private provider
store and precreated `/usr/share/rmm-mvp-test`. No whole /usr, /etc or /var writes.
APT terminal/history logs and dpkg log are explicitly under provider `log/`;
APT cache/state and extended_states are private, no default /var/log/apt write.
These are configured paths, **not natively proven log/write requirements** yet.
Unexpected native write failures must be reported, not fixed with blanket writes.
One common Python setup function serves mutation and readiness. It makes mount
propagation private, hides keys/ledger/sockets/fds, mounts minimal devices and
kernel controls, then irreversibly drops E/P/B to the original eight caps,
clears I/A, retains NNP and installs seccomp. Both clone namespace flags
(including NEWUSER/NEWTIME) and clone3 pointer flags are closed: clone3 returns
ENOSYS for libc's ordinary fork fallback; clone namespace flags return EPERM.
Ordinary fork/exec remains allowed. No reconfiguration/reboot/kexec escape is
allowed after setup. Trusted setup with SYS_ADMIN is explicitly in the TCB.
## Readiness probe: no package transaction
`--probe-native` does NOT create jobs, grants, receipts, launch/effect markers or
native-acceptance records. It exclusively creates a generated private probe dir,
copies host dpkg status (host opened read-only), binds the copy over /var/lib/dpkg
inside the worker namespace, uses source/hook-isolated private APT config, tests
private-db frontend lock conflict and runs positive `apt-get check` on that copy.
The host frontend lock is only observed with O_RDONLY/F_GETLK: never F_SETLK,
flock/lockf, deletion or killing an owner. Invalid-magic/flags reboot/kexec probes
must return EPERM; no valid calls or PID1 signals are attempted.
The worker checks namespaces/PID1, capabilities/NNP/seccomp, readonly control
mounts, hidden sockets and masked sysrq. Parent verifies complete result and
private namespace identities, unchanged host status/runtime and publishes only
root-private `native-readiness.json` (readiness_only=true, native_acceptance=false).
It is boot-/code-/OS-/binary-/unit-bound and invalidated by changes. Partial,
failed or malformed results do not publish new readiness. Acceptance/bootstrap
attempt tombstones are neither created nor reset by this operation.
## Agent environment / read-only IPC API
```
RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock
RMM_APT_HELPER_PROOF_FILE=/etc/rmm/apt-helper-proof.json
```
`RMM_HELPER_SOCKET` remains the general route. Typed ensure_package,
apply_updates and apt_native_acceptance exclusively use the dedicated socket.
Missing/wrong APT socket/pin/readiness never falls back to general helper, SSH,
shell or local execution. The unprivileged agent obtains a fresh read-only probe
before capability advertisement and before use; it authenticates root SO_PEERCRED,
root-owned safe socket/parents and independent root-provisioned pin.
Same u32-big-endian framed JSON socket, a distinct read-only request shape:
```
operation: "apt_readiness_v1"
challenge: fresh nonnil UUID
identity: {tenant_id,asset_id,agent_id,enrollment_generation,assignment_version,signing_key_id}
expected_stack_sha256: independently pinned helper/identity/policy/unit stack hash
```
Response strictly binds nonce, identity, apt_only profile and provider stack;
includes shared ledger/lock dev/inodes and separate fixture_acceptance_ready,
bootstrap_recipes and accepted_recipes. Wrong nonce/identity/generation/profile/
stack/root peer is rejected. An unresolved shared ledger suppresses all APT
readiness/capabilities. This API never consumes a grant or authorizes a job.
Readiness alone advertises **only apt_native_acceptance** for fixed allowed
fixtures. Valid native acceptance records enable only matching fixture recipes:
ensure_package, and apply_updates only for the accepted update recipe. Agent
also checks each actual typed job's manifest recipe, not just aggregate action
capability names. No production/arbitrary catalog capability is manufactured.
## Actual jobs remain UI-authorized
Domain's additive typed schema-2 apt_manifest and dedicated AptNativeAcceptance
are now present; local tests roundtrip the actual domain variant through exact
signed bytes into the helper validator. See
`tools/apt-transaction/HELPER-CONTRACT.md` for the full immutable payload/context
schema. Readiness does not replace independent Ed25519 verification, all target/
tenant/generation/plan/job/attempt/authorization/action/hash/time bindings,
current trusted clock, parent consumption and immutable WebUI approval.
Use provision-fixture-store / prepare-baseline **before** final grant to stage
the two known archives and snapshot. One UI-approved dedicated fixture job may
install 1.0, another may update 1.0 -> 1.1. Same guard/native lock/deadline/hash/
postcondition pipeline, no weak path. Failed/stale/partial/unknown never acceptance.
Armed/unknown outcomes retain both parent resource fencing and effect markers.
One-time failures are not blindly reset: technical no-start recovery needs a
separate durable signed reconciliation procedure, not implemented as a shortcut.
No actual native namespace/APT/service/root execution has been measured here.
The subsequent VM worker must execute this profile/probe and UI-authorized
fixture jobs, retain actual evidence and report all native failures separately.
## Executed local verification (this increment)
* Source-tree rustfmt check passed for helper lib/main and agent main/modules.
* Targeted Clippy `-p rmm-privileged-helper -p rmm-agent --lib --bins -- -D warnings`
passed.
* `cargo test -p rmm-privileged-helper -p rmm-agent --lib --bins`: **37 passed**
(20 helper library, 3 helper binary, 14 agent).
* `PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s tests/apt_transaction -p 'test_*.py' -v`:
**48 passed**; AST syntax checks **7 Python files**; example JSON syntax passed.
* Local unprivileged seccomp invalid-input/clone-flag denial and ordinary
fork/exec really ran. Root setup/installer/APT/probe unit execution was mocked
where unsafe. Temporary mock readiness files are not deployed native evidence.
Package-wide verification has a concurrent-work limitation: the last full
`cargo fmt --check` encountered formatting in the other worker's
`crates/privileged-helper/tests/reconcile.rs`; the last all-target Clippy attempt
failed on missing `validate_history` in its included `src/reconcile.rs`. Those
Sync files were not edited here. The focused source checks/tests above passed
afterwards; this is not a claim that the parallel Sync integration suite passed.
+27
View File
@@ -0,0 +1,27 @@
{
"profile": "apt_only",
"binding": {
"tenant_id": "REPLACE-TENANT-UUID",
"asset_id": "REPLACE-ASSET-UUID",
"agent_id": "REPLACE-AGENT-UUID",
"enrollment_generation": 1,
"assignment_version": 1,
"public_key_hex": ""
},
"signing_key_file": "/etc/rmm/helper-signing-key.json",
"agent_uid": 993,
"socket_gid": 993,
"socket": "/run/rmm-apt-helper/helper.sock",
"state": "/var/lib/rmm-helper",
"allowed_services": [],
"allow_reboot": false,
"allow_root_scripts": false,
"apt_provider": {
"schema_version": 1,
"execution_profile": "dev-test",
"allow_fixture_bootstrap": true,
"allowed_recipes": ["rmm-apt-fixture-install-1.0", "rmm-apt-fixture-update-1.1"],
"runtime_sha256": "REPLACE-WITH-REVIEWED-RUNTIME-OBJECT-SHA256",
"apt_config_sha256": "REPLACE-WITH-REVIEWED-CONFIG-BYTES-SHA256"
}
}
+96
View File
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
"""Offline, create-only package installation. Never starts/enables services.
Default staging is testable without root. A live filesystem install requires
both --root / and --install-live plus a preexisting dedicated system account.
No device/private signing keys or bearer credentials are accepted as inputs.
"""
from __future__ import annotations
import argparse
import os
from pathlib import Path
import pwd
import stat
def directory(path: Path, mode: int) -> None:
if not path.exists():
path.mkdir(mode=mode)
metadata = path.lstat()
if not stat.S_ISDIR(metadata.st_mode) or metadata.st_mode & 0o022:
raise ValueError("unsafe installation directory")
if metadata.st_uid != os.geteuid():
raise ValueError("installation directory has unexpected owner")
def create(path: Path, data: bytes, mode: int) -> None:
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
with os.fdopen(fd, "wb") as stream:
stream.write(data)
stream.flush()
os.fsync(stream.fileno())
fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
os.fsync(fd)
finally:
os.close(fd)
def elf(path: Path) -> bytes:
if path.stat().st_size > 100 * 1024 * 1024:
raise ValueError("binary size limit")
data = path.read_bytes()
if len(data) < 64 or data[:6] != b"\x7fELF\x02\x01" or data[18:20] != b"\x3e\x00":
raise ValueError("expected Linux x86-64 ELF artifact")
return data
def install(root: Path, agent: Path, ca: Path, helper: Path | None, live: bool) -> None:
if not root.is_absolute() or ".." in root.parts:
raise ValueError("absolute installation root required")
if root == Path("/") and not live:
raise ValueError("live installation requires --install-live")
if live and (root != Path("/") or os.geteuid() != 0):
raise ValueError("live installation requires root and --root /")
account = pwd.getpwnam("rmm-agent") if live else None
if account is not None and (account.pw_uid == 0 or account.pw_gid == 0):
raise ValueError("rmm-agent must be a dedicated unprivileged account")
# Validate all inputs before creating anything. CA is public, never private.
agent_data = elf(agent)
helper_data = elf(helper) if helper else None
ca_data = ca.read_bytes()
if len(ca_data) > 65536 or b"-----BEGIN CERTIFICATE-----" not in ca_data or b"PRIVATE KEY" in ca_data:
raise ValueError("expected public CA certificates only")
directory(root, 0o755)
for relative in ["usr", "usr/local", "usr/local/libexec", "etc", "etc/rmm",
"etc/systemd", "etc/systemd/system", "var", "var/lib"]:
directory(root / relative, 0o755)
units = Path(__file__).parent
files = [("usr/local/libexec/rmm-agent", agent_data, 0o755),
("etc/rmm/ca.pem", ca_data, 0o644),
("etc/systemd/system/rmm-agent.service", (units / "rmm-agent.service").read_bytes(), 0o644)]
if helper_data is not None:
files += [("usr/local/libexec/rmm-privileged-helper", helper_data, 0o755),
("etc/systemd/system/rmm-privileged-helper.service", (units / "rmm-privileged-helper.service").read_bytes(), 0o644)]
# Never overwrite binaries, CA, service units, config or enrollment state.
if any((root / relative).exists() or (root / relative).is_symlink() for relative, _, _ in files):
raise ValueError("existing installation requires a separate controlled upgrade")
for relative, data, mode in files:
create(root / relative, data, mode)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, required=True)
parser.add_argument("--agent-binary", type=Path, required=True)
parser.add_argument("--helper-binary", type=Path)
parser.add_argument("--ca-file", type=Path, required=True)
parser.add_argument("--install-live", action="store_true")
args = parser.parse_args()
install(args.root, args.agent_binary, args.ca_file, args.helper_binary, args.install_live)
print("Package files installed. No account, device identity or authorization config created; services not started/enabled.")
if __name__ == "__main__":
main()
+35
View File
@@ -0,0 +1,35 @@
[Unit]
Description=RMM unprivileged outbound Linux agent
After=network-online.target time-sync.target
Wants=network-online.target
[Service]
Type=simple
User=rmm-agent
Group=rmm-agent
StateDirectory=rmm-agent
StateDirectoryMode=0700
UMask=0077
Environment=RMM_AGENT_STATE=/var/lib/rmm-agent
Environment=RMM_CA_FILE=/etc/rmm/ca.pem
Environment=RMM_TRANSPORT=auto
Environment=RMM_EXTENDED_INVENTORY=1
Environment=RMM_CONNECT_ADDR=127.0.0.1:18443
EnvironmentFile=-/etc/rmm/agent.env
ExecStart=/usr/local/libexec/rmm-agent
Restart=on-failure
RestartSec=10
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictSUIDSGID=true
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
CapabilityBoundingSet=
AmbientCapabilities=
[Install]
WantedBy=multi-user.target
+52
View File
@@ -0,0 +1,52 @@
[Unit]
Description=RMM dedicated fixture APT helper (no generic root commands)
After=local-fs.target
[Service]
Type=simple
User=root
Group=rmm-agent
Environment=RMM_HELPER_CONFIG=/etc/rmm/apt-helper.json
ExecStartPre=/usr/local/libexec/rmm-privileged-helper --probe-native
ExecStart=/usr/local/libexec/rmm-privileged-helper
WorkingDirectory=/
RuntimeDirectory=rmm-apt-helper
RuntimeDirectoryMode=0750
StateDirectory=rmm-helper rmm-apt-transaction
StateDirectoryMode=0700
UMask=0077
NoNewPrivileges=true
PrivateTmp=true
PrivateNetwork=true
PrivateDevices=no
DevicePolicy=strict
DeviceAllow=/dev/null rwm
DeviceAllow=/dev/zero rwm
DeviceAllow=/dev/random rwm
DeviceAllow=/dev/urandom rwm
ProtectSystem=strict
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictSUIDSGID=true
RestrictAddressFamilies=AF_UNIX
SystemCallArchitectures=native
RestrictNamespaces=mnt pid net
AmbientCapabilities=
CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE CAP_FOWNER CAP_FSETID CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_SETFCAP CAP_SYS_ADMIN CAP_MKNOD
# Trusted setup needs mount/unshare/fork/capset/prctl/seccomp. There is deliberately
# no outer @mount deny; the irreversible worker filter denies it after setup.
SystemCallFilter=~@reboot @module @raw-io @swap @clock @debug
# Existing helper clock trust reads adjtimex(modes=0). Mutating adjtimex still
# requires CAP_SYS_TIME, absent from the bounding set; other @clock calls denied.
SystemCallFilter=adjtimex
SystemCallErrorNumber=EPERM
ReadWritePaths=/var/lib/dpkg /var/lib/rmm-helper /var/lib/rmm-apt-transaction /usr/share/rmm-mvp-test
Restart=on-failure
RestartSec=5s
TimeoutStopSec=infinity
KillMode=process
[Install]
WantedBy=multi-user.target
+35
View File
@@ -0,0 +1,35 @@
[Unit]
Description=RMM dev-test root helper readiness (APT execution disabled)
After=time-sync.target
[Service]
Type=simple
User=root
Group=rmm-agent
StateDirectory=rmm-helper
StateDirectoryMode=0700
RuntimeDirectory=rmm-helper
RuntimeDirectoryMode=0750
UMask=0077
Environment=RMM_HELPER_CONFIG=/etc/rmm/helper-dev-test-v1.json
ExecStart=/usr/local/libexec/rmm-privileged-helper
Restart=no
NoNewPrivileges=true
PrivateTmp=true
PrivateNetwork=true
ProtectSystem=strict
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictSUIDSGID=true
RestrictAddressFamilies=AF_UNIX
CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE CAP_FOWNER CAP_FSETID CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_SETFCAP
AmbientCapabilities=
SystemCallFilter=~@mount @reboot @swap @raw-io @module @clock @debug
# This readiness unit intentionally CANNOT set up the APT worker's namespaces
# or device nodes. Do not remove these restrictions to make a demo pass.
# apt_provider remains null until a separately reviewed native runner exists.
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,29 @@
[Unit]
Description=RMM independently authorizing local privileged executor
After=time-sync.target
[Service]
Type=simple
User=root
Group=rmm-agent
StateDirectory=rmm-helper
StateDirectoryMode=0700
RuntimeDirectory=rmm-helper
RuntimeDirectoryMode=0750
UMask=0077
Environment=RMM_HELPER_CONFIG=/etc/rmm/helper.json
ExecStart=/usr/local/libexec/rmm-privileged-helper
Restart=on-failure
RestartSec=10
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictSUIDSGID=true
RestrictAddressFamilies=AF_UNIX
[Install]
WantedBy=multi-user.target
+311
View File
@@ -0,0 +1,311 @@
# Agent/helper runtime — integrated source, VM acceptance pending
This is the current runtime contract. It supersedes the earlier module-only
handoffs in `README.md` and `tests/agent_sync/README.md`. No installation, target
job, service/reboot/package mutation, SSH tunnel or server restart was performed
by this integration. Actual VM execution/acceptance belongs to the next worker.
Nothing here approves an RMM job or substitutes for the WebUI approval boundary.
## Binaries and complete CLI/environment
Both binaries support `--help`; unknown arguments fail closed. Normal agent
operation has no positional arguments. Planned bootstrap invocation:
```text
RMM_AGENT_STATE=/var/lib/rmm-agent
RMM_CA_FILE=/etc/rmm/ca.pem
RMM_CONNECT_ADDR=127.0.0.1:18443
RMM_TRANSPORT=https
/usr/local/libexec/rmm-agent
```
These are environment assignments, not an instruction to source a credential
file. The trusted CA and TLS name **rmm-dev.internal** are always checked. TCP
override changes neither DNS/SNI nor certificate verification. No proxy trust,
invalid-certificate bypass or public Docker port is introduced.
| Variable | Meaning/default |
|---|---|
| `RMM_AGENT_STATE` | Private 0700 owned agent state; default `devstate/agent` |
| `RMM_CA_FILE` | Public trusted CA; default `devstate/pki/ca.pem` |
| `RMM_CONNECT_ADDR` | Parsed TCP SocketAddr; default `127.0.0.1:18443` |
| `RMM_TRANSPORT` | `auto` (default), `wss`, or `https`; HTTPS can be explicitly selected for VM acceptance |
| `RMM_ENROLLMENT_TOKEN_FILE` | First registration only; remove the bootstrap token after enrollment |
| `RMM_JOB_BINDING_FILE` | Optional private agent-owned JSON Binding; absent means passive, no job capabilities |
| `RMM_JOB_SIGNING_KEY_BOOTSTRAP=1` | Authenticated device-mTLS GET, using the current rotation-store identity; pins without replacing existing trust |
| `RMM_HELPER_SOCKET` | Configured general helper socket, e.g. `/run/rmm-helper/control.sock` |
| `RMM_HELPER_POLICY_FILE` | Root-owned, agent-readable public general policy pin; required with general socket |
| `RMM_APT_HELPER_SOCKET` | Dedicated fixed `/run/rmm-apt-helper/helper.sock`, never generic fallback |
| `RMM_APT_HELPER_PROOF_FILE` | Root-owned public APT stack/profile/identity pin; see `apt-helper-README.md` |
| `RMM_LOG_UNITS_FILE` | Private agent-owned JSON array of exact allowed service units for unprivileged live logs |
| `RMM_EXTENDED_INVENTORY` | `1` by default with the now-compatible backend, `0` for legacy base-only JSON |
| `RMM_AGENT_PROFILE` | `development` required only for the rotation-test interval override |
| `RMM_ROTATION_TEST_AFTER_SECONDS` | Positive development-only certificate-age trigger; never bypasses chain/expiry/EKU/key/identity verification |
Binding fields: tenant_id, asset_id, agent_id, enrollment_generation,
assignment_version, public_key_hex. Device/asset must match enrollment. Empty key
is allowed only with authenticated signing-key bootstrap; later startup must
reauthenticate that bootstrap or use a fully provisioned binding. Key response is
Ed25519/schema 1/raw 32-byte key with SHA-256 key ID, bounded and no redirects.
The immutable agent cache is `job-signing-key.json`. Signing-pin replacement and
identity/assignment recovery are controlled provisioning operations, not automatic
trust in a key from a job. Helper trust stays independently root-provisioned.
The unit leaves the job-binding optional in `/etc/rmm/agent.env`, allowing a first
passive enrollment. Set binding/key-bootstrap/helper variables only after the
authenticated operator resolves the enrolled IDs and provisions their pins.
Do not delete an old identity/journal to get a new first-session shortcut.
## Integrated transport and sync
One bounded actor and one dispatcher serve WSS and HTTPS with identical envelopes,
journal, result receipts, cancellation and signature checks. `auto` tries WSS
first and falls back only after two typed HTTP upgrade-block classifications;
401/403, TLS/identity failures and ambiguous errors never select a bypass.
WSS Ping/Pong are handled. HTTPS uses the existing MessageBatch/25-second poll
contract. Poll cancellation cannot create a new attempt; fresh heartbeat IDs/
sequences are never reused or queued as offline telemetry. Actor queues are bounded.
Every authenticated Hello advertises `rmm.sync.v1` and closes all local effect
gates. Agent obtains actual root snapshots, durably reserves the fresh SyncRequest,
checks exact signed SyncResponse bytes and all device/channel/boot/checkpoint/
request-hash/time bindings, commits targeted journal/receipt changes, applies its
own consumption decisions, forwards the **original** signed response plus original
request protobuf to every configured helper, then confirms actual applied root
checkpoints. No generic ready boolean opens either process.
Both root profiles share actual DB/lock inodes. They join an unchanged pending
root capture created after both process births, so ONE signed response/checkpoint
can be independently applied by both. A new process never inherits readiness
from the shared DB. Stale process captures are fenced; known outcomes may unlock
specifically while an unknown retains the resource and global gates stay closed.
Consumed grant/attempt tombstones are never removed.
Scheduler AND immediate worker effect admission check the current signed lease;
the worker also rechecks fresh grant/time and the disconnect/rotation atomic fence.
Helper checks its own process gate under the same executor guard before consumption.
Unknown, missing authoritative history, clock failure and rollback remain closed.
Clock/helper sync failure does not stop independent passive heartbeat/inventory
traffic. Sync retries are bounded, happen on fresh inventory/result ACK and on a
lease timer, and never retry a lost permit request as a new grant.
After reboot, old/new boot ID alone is not success. Fresh current-boot inventory
is durably ACKed, a new signed decision must bind the persisted original boot and
current authenticated boot plus inventory evidence, then each helper independently
applies that specific canonical terminal outcome/unlock. Thus an initial negative
sync does not deadlock waiting forever without the post-inventory resync.
Result envelopes are archived before concrete ACK deletion in one SQLite
transaction. Historical message identity/output is retained for reconciliation.
Inventory mapping preserves source/time/quality/completeness and unsupported states.
Live journal permission warnings become permission_denied, not a fake empty success.
## Helper bootstrap/policy inspection
Helper normal daemon: `RMM_HELPER_CONFIG=/etc/rmm/helper.json` (root-owned 0600).
Both profiles use `/var/lib/rmm-helper/consumption.sqlite` and `executor.lock`.
Helper config uses the existing binding, optional independent signing_key_file,
agent_uid, socket_gid, socket, shared state, allowed_services, allow_reboot,
allow_root_scripts; APT adds its existing sealed apt_provider configuration.
Public general pin, **planned command, not executed on VM here**:
```text
RMM_HELPER_CONFIG=/etc/rmm/helper.json /usr/local/libexec/rmm-privileged-helper --inspect-general-profile
```
Install the exact JSON as root:rmm-agent 0640 in non-writable root-owned parents,
then set RMM_HELPER_POLICY_FILE. It binds profile, device identity/key and exact
policy hash. General policy probe uses root socket/peer credentials and a fresh
challenge; capabilities reflect actual policy/interpreter availability. Root
scripts remain default-off broad administration. Services are exact allowed
running/stopped units with >=180s runtime, reboot separately enabled with >=60s.
Root jobs are NEVER relabelled into agent_service.
APT install/probe/inspection modes and profile sandbox remain exactly documented
in `apt-helper-README.md`. Both daemons need this same installed build and reviewed
new proof hashes (binary bytes are part of the APT proof). Prefetched sealed
archives, receipts predating the grant and exact baselines are still prerequisites;
this integration introduces no unsafe generic APT download/install/simulation path.
Root native-acceptance and accepted-recipe/manifest capabilities come only from
authenticated real readiness. Successful native acceptance triggers a controlled
reconnect to advertise newly accepted recipes; periodic safe capability refresh
also reconnects rather than silently changing the Hello contract.
## Passive APT planning and declarative no-change
The normal extended inventory worker now runs at startup and every 60 seconds
(one inflight collector; not in the heartbeat handler). Unacknowledged durable
traffic backpressures new collection without deleting old data; job results and
control receipts retain priority. It includes the fixed
`linux.apt.planning` scope from `root-helper:apt_planning_v1`. This currently
refreshes the existing extended inventory scopes too; their actual individual
source timestamps/quality remain intact. There is no arbitrary package query,
requested live diagnosis or software effect hidden in this collector.
`apt_planning_v1` uses the existing UID-authenticated dedicated Unix socket,
fresh challenge, device/generation/signing-key identity, root peer and separately
root-provisioned stack pin. Its response is the readiness envelope with a typed
`planning` result and **empty capability/acceptance fields**. It does not depend
on a prior native-acceptance record and cannot create one. Root validates the
installed code, exact runtime/config policy pins and actual archive bytes, sizes,
ownership/modes/link count and private prefetch receipts. No artifact is fetched
or installed by this RPC. Administratively stage the known fixture archives via
`provision-fixture-store` before planning/the final 60-second grant.
The fixed metadata child has a 4-second whole-process/output budget (32 KiB),
the socket has 5-second IO limits, and root measurement timestamps are preserved.
Dirty dpkg updates/triggers, foreign locks, unsafe/missing receipts, permission
failure, timeout or missing helper yield an unknown observation with null data,
not fake hashes or accepted capability. Runtime policy drift fails closed.
Complete observations contain the suite's `fixture_package`, `baseline_sha256`,
`sealed_baseline_sha256`, `status_fingerprint`, `dpkg_updates_empty`,
`pending_triggers_empty`, `runtime`, `runtime_sha256`, `provider_sha256`,
`hook_sha256`, `apt_config_sha256` and `prefetch`. The full package baseline stays
root-private at `baselines/<sha256>.json`; only hashes, fixture projection and
minimal public runtime/artifact pins cross the wire. Stable sampling is checked
before and after content-addressed exclusive write/file+directory fsync; existing
identical seals are verified, never overwritten. This is passive metadata, not
job authority. The ordinary durable inventory outbox/ACK path persists it.
Normal accepted fixture `ensure_package`/`apply_updates` may return
`POSTCONDITION_ALREADY_MET`, `changed=false`, `already_desired=true`,
`postcondition_already_met=true`, `transaction_executed=false` only when the
**entire selected set** is independently installed at its exact target version
and architecture, clean, matches the current approved baseline/fingerprint and
has the expected fixture payload. Root signature, all original bindings,
expiry/sync/local-policy/resource gates, all manifest/code/runtime/artifact pins,
native accepted-recipe record and durable grant/attempt consumption remain
mandatory. Only the historical `old_version` condition is skipped in this
no-mutation branch. No APT/hook/native launch/effect marker is created. A foreign
lock, dirty/unknown state or changed baseline cannot become success; native
acceptance itself never uses this branch. Other versions still follow the
unchanged no-reinstall/no-downgrade/no-remove mutation guards.
VM suite owner must update its absent/1.0-only adapter: after native update to
1.1, a normal exact desired-1.1 job must use the accepted update recipe with a
fresh planning seal. A desired-1.0 job on 1.1 is still correctly denied; this
implementation does not authorize removal/downgrade to make a stale test pass.
No backend/domain/protobuf signatures or plan bytes have been changed.
Provider source changes change the embedded provider/helper/stack/runtime pins.
Deploy both helpers from this build and provision reviewed new proof/policy
pins **before** native acceptance; old native records cannot silently certify
new runtime bytes. Manifest schema 2 / transaction schema 1 / fixture archives
1.0 and 1.1 (20,672 bytes, unchanged hashes) remain unchanged.
These are implemented source/local-fixture semantics, not VM or MVP acceptance.
## Certificate rotation and recovery
`RMM_AGENT_STATE/certificates/current.json` is the authoritative atomic selector
over private versioned device key/cert files. Existing enrolled files initialize
the store exactly once; later connections/bootstrap use the selected identity.
Rotation checks chain, current validity, exact agent CN, clientAuth purpose and
public/private key match. It persists one pending key/CSR and retries those SAME
CSR bytes after response loss. A verified swap closes the old actor/effect fence
and requires a new Hello/Sync. Agent/enrollment IDs and independent job/helper
signing pins do not change. Expired/revoked/invalid certs remain blocked and need
authorized identity recovery; no auto-reenrollment or TLS downgrade.
## Verification boundaries / concrete remaining policy issue
Local Rust source/unit/integration tests cover actual loopback mTLS dispatcher
WSS/HTTPS clean reconnect, Ping, forged sync rejection, passive traffic with an
untrusted effect clock, journal reopen/replay and independent helper checkpoint/
outcome/boot checks. Unsafe OS providers are mocked or denied; no local service,
APT install or reboot was executed. This is not a real WebUI-approved VM result,
native package acceptance, production auth acceptance or full Linux-MVP sign-off.
Concrete backend policy limitation, not bypassed here: `RootScope::permits` in
`crates/server/src/root_policy.rs` does not match `Action::QueryLogs` and falls
through to false. The typed, bounded root helper log handler exists, and the
unprivileged allowlisted path is live, but a root live-log plan will be denied by
that backend policy until its owner adds an explicit permitted scope. Do NOT
work around it by relabelling identity, direct approval or a generic root shell.
## Executed local verification
Using `/home/Kevin/.cargo/bin/cargo`, actually passed during integration:
```sh
cargo test -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux --all-targets --locked
cargo clippy -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux --all-targets --locked -- -D warnings
cargo build -p rmm-agent -p rmm-privileged-helper --locked
cargo fmt -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux -- --check
python3 tests/agent_jobs/test_install.py
target/debug/rmm-agent --help
target/debug/rmm-privileged-helper --help
```
111 executed Rust test cases passed (including reused transport tests), two
create-only installer guard tests passed, both debug binaries built and both
help commands returned successfully without loading secret configuration. No
warnings/type errors were suppressed. A boxed tungstenite HTTP-response test
constructor was corrected, not bypassed. No coverage percentage was measured.
Coverage boundaries: real loopback TLS/dispatcher WSS and HTTPS reconnect and
forged sync rejection are exercised; helper cryptography/checkpoints/two-process
shared signature and replay/unknown/reboot decisions use real SQLite/signatures
with local fixtures. Native service/APT/reboot effects and positive root UID-0
daemon/VM RPC acceptance are NOT substituted by these tests. The built artifacts
are `target/debug/rmm-agent` and `target/debug/rmm-privileged-helper`; a reviewed
VM-compatible release artifact still needs the target worker's ELF/glibc/library
preflight and actual authorized VM run.
## Executed local verification — planning/no-change increment
After **all** source changes, the following complete targeted block passed with
`/home/Kevin/.cargo/bin` on PATH (2026-11-12):
```sh
cargo test -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux --all-targets --locked
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s tests/apt_transaction -p 'test_*.py' -v
cargo clippy -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux --all-targets --locked -- -D warnings
cargo fmt -p rmm-agent -p rmm-privileged-helper -p rmm-agent-linux -- --check
cargo build -p rmm-agent -p rmm-privileged-helper --locked
cargo build -p rmm-agent -p rmm-privileged-helper --release --locked
python3 tests/agent_jobs/test_install.py
target/debug/rmm-agent --help
target/debug/rmm-privileged-helper --help
```
Results: **115 Rust tests**, **56 Python APT tests**, **2 installer guard tests**
passed; warning-denying all-target Clippy and formatting passed; both Debug and
Release binaries built; both help commands succeeded. Separate final AST parsing
passed for **8 Python files**. No full workspace/backend/web test suite was run.
New regressions cover shared Python/Rust canonical golden bytes/hash, receipt
and timestamp freshness, private content-addressed seal reuse/change without
full-baseline disclosure, sampling drift/dirty/foreign-lock/bad-artifact failure,
normal exact already-desired install/update postconditions, no launch/effect
markers, old-version mutation rejection, wrong architecture/newer version,
audit/payload/expiry rejection, no-change outcome binding and durable duplicate
grant/attempt rejection, honest missing-helper data and collection backpressure.
Root ownership/kernel state and unsafe native package processes are mocked;
existing harmless unprivileged seccomp tests really execute. No native host
package mutation, root systemd/probe/VM action, approval or reboot was performed.
Earlier intermediate failures were corrected, not suppressed: the inherited two
unformatted source edits, a mock reader missing the `limit` keyword argument,
and a newly inserted function accidentally inheriting a test-only cfg attribute.
The final complete block above passed after these corrections.
Final artifact SHA-256 (native developer-PC builds; **not** Bookworm/VM proof):
| Artifact | SHA-256 |
|---|---|
| `target/debug/rmm-agent` | `90a4eab064301740cd938a41a76a510ad3b4abc3e48a188ba12bfe22f123266c` |
| `target/debug/rmm-privileged-helper` | `54db2fe17841fef9af48bbeb446f45bda52f62e442a4900e0543e358095900b7` |
| `target/release/rmm-agent` | `eac5434fd7a1e509c0971e4e05d4e1aedbde1bb90f7d7a8f9ec4d48ab4d34597` |
| `target/release/rmm-privileged-helper` | `581a033478b41365066df2dc9ac0d11e314160909b5fab612be0be97be1bbbdc` |
| `tools/apt-transaction/apt_transaction.py` | `57b695bf57ea2703393e39f92a1b6ef2314b4838f3dfb29e4e70da79687c9987` |
Remaining: suite-owner adapter update for exact desired 1.1 after native upgrade,
reviewed deployment/new pins, actual dedicated-profile metadata RPC and native
UI-approved fixture/no-change VM proof, target ELF/library compatibility, and
VM resource/retention measurements for the new 60-second extended collection.
No real current baseline change was induced on the host/VM: freshness tests use
changed temporary/mock package state, not a disguised package installation.
Linux-MVP and production acceptance remain open. Backend Root QueryLogs policy
is owned by its separate worker and was neither changed nor reverified here.
+28
View File
@@ -0,0 +1,28 @@
# Isolated development PKI: never inherit host request extensions.
[ req ]
distinguished_name = distinguished_name
prompt = no
[ distinguished_name ]
CN = Rust Discovery Development
[ ca ]
basicConstraints = critical,CA:TRUE,pathlen:0
keyUsage = critical,keyCertSign,cRLSign
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always
[ server ]
basicConstraints = critical,CA:FALSE
keyUsage = critical,digitalSignature,keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = DNS:rmm-dev.internal
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always
[ client ]
basicConstraints = critical,CA:FALSE
keyUsage = critical,digitalSignature
extendedKeyUsage = clientAuth
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always
+28
View File
@@ -0,0 +1,28 @@
[Unit]
Description=RMM passive Linux agent (development slice)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=rmm-agent
Group=rmm-agent
StateDirectory=rmm-agent
StateDirectoryMode=0700
Environment=RMM_AGENT_STATE=/var/lib/rmm-agent
Environment=RMM_CA_FILE=/etc/rmm-agent/ca.pem
Environment=RMM_CONNECT_ADDR=127.0.0.1:18443
Environment=RMM_ENROLLMENT_TOKEN_FILE=/etc/rmm-agent/enrollment.token
ExecStart=/usr/local/bin/rmm-agent
Restart=on-failure
RestartSec=10
UMask=0077
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
CapabilityBoundingSet=
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
[Install]
WantedBy=multi-user.target
+145
View File
@@ -0,0 +1,145 @@
#!/usr/bin/env python3
"""Read-only VM SSH preflight; never accepts or installs SSH host keys."""
import ipaddress
import os
from pathlib import Path
import re
import stat
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
FIELDS = {
"hostname", "address", "os_version", "architecture", "access_method",
"username", "password", "ssh_key_path", "port", "vmid",
}
def load_config(path):
if path.is_symlink() or path.parent.is_symlink():
raise ValueError("credential symlinks are not supported")
for item, mode in ((path.parent, 0o700), (path, 0o600)):
if stat.S_IMODE(item.stat().st_mode) != mode or item.stat().st_uid != os.getuid():
raise ValueError("credential ownership or permissions are unsafe")
rules = (ROOT / ".gitignore").read_text().splitlines()
if not any(x.strip() in ("credentials/", "/credentials/", "credentials", "/credentials") for x in rules):
raise ValueError("credential ignore rule is missing")
values = {}
for line in path.read_text().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
match = re.fullmatch(r"([a-z_]+)\s*[:=]\s*(.*)", line)
if not match or match[1] not in FIELDS or match[1] in values:
raise ValueError("invalid or duplicate credential field (contents withheld)")
value = match[2].strip()
if value[:1] in ("'", '"'):
if len(value) < 2 or value[-1] != value[0]:
raise ValueError("unmatched credential quote")
value = value[1:-1]
if any(ord(c) < 32 or ord(c) == 127 for c in value):
raise ValueError("control character in credential field")
values[match[1]] = value
address = values.get("address", "")
try:
ipaddress.ip_address(address)
except ValueError:
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9.-]{0,252}", address):
raise ValueError("invalid VM address") from None
if not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_.-]*", values.get("username", "")):
raise ValueError("invalid SSH username")
if not values.get("port", "22").isdigit() or not 1 <= int(values.get("port", "22")) <= 65535:
raise ValueError("invalid SSH port")
return values
def run(args, **kwargs):
return subprocess.run(args, capture_output=True, timeout=30, **kwargs)
def fingerprints(address, port):
# Unauthenticated scan: fingerprints are for human comparison, never trust input.
scan = run(["ssh-keyscan", "-T", "5", "-p", port, address])
keys = b"\n".join(x for x in scan.stdout.splitlines() if x and not x.startswith(b"#"))
if not keys:
print("UNCONFIRMED_HOSTKEY: scan returned no public keys; connectivity remains unverified")
return
result = run(["ssh-keygen", "-l", "-E", "sha256", "-f", "-"], input=keys)
for line in result.stdout.decode(errors="replace").splitlines():
parts = line.split()
if len(parts) >= 2 and parts[1].startswith("SHA256:"):
print("UNCONFIRMED_HOSTKEY:", parts[0], parts[1], parts[-1])
def main():
config = load_config(ROOT / "credentials/linux-vm.conf")
address, port = config["address"], config.get("port", "22")
known = Path.home() / ".ssh/known_hosts"
lookup = address if port == "22" else f"[{address}]:{port}"
found = run(["ssh-keygen", "-F", lookup, "-f", str(known)]) if known.is_file() else None
if found is None or found.returncode != 0:
print("BLOCKED: no existing trusted VM host key in user known_hosts; no authentication attempted")
fingerprints(address, port)
return 2
ssh = ["ssh", "-F", "/dev/null", "-T", "-p", port,
"-o", "StrictHostKeyChecking=yes", "-o", f"UserKnownHostsFile={known}",
"-o", "GlobalKnownHostsFile=/dev/null", "-o", "ConnectTimeout=10",
"-o", "ConnectionAttempts=1", "-o", "ForwardAgent=no",
"-o", "ClearAllForwardings=yes", "-o", "LogLevel=ERROR"]
key = config.get("ssh_key_path", "")
if key:
key_path = Path(key).expanduser()
if not key_path.is_file():
raise ValueError("configured SSH identity file is missing")
ssh += ["-i", str(key_path), "-o", "IdentitiesOnly=yes"]
# No hostname, username, home path, process command lines, or remote secrets in output.
remote = """set -eu
printf 'ARCH='; uname -m
printf 'KERNEL='; uname -s
printf 'GLIBC='; getconf GNU_LIBC_VERSION 2>/dev/null || true
printf 'OS_ID='; sed -n 's/^ID=//p' /etc/os-release
printf 'OS_VERSION='; sed -n 's/^VERSION_ID=//p' /etc/os-release
test -n "$(hostname)" && printf 'HOSTNAME_READ=ok\n'
test -d "$HOME" && printf 'USER_HOME=present\n'
if command -v ss >/dev/null 2>&1; then
if ss -H -ltn | awk '{print $4}' | grep -Eq '(^|:)18443$'; then
printf 'PORT_18443=occupied\n'
else printf 'PORT_18443=free\n'; fi
else printf 'PORT_18443=unknown_no_ss\n'; fi
"""
target = f"{config['username']}@{address}"
result = run(ssh + ["-o", "BatchMode=yes", target, remote])
if result.returncode and b"Permission denied" in result.stderr and config.get("password"):
with tempfile.TemporaryDirectory(prefix="rmm-ssh-", dir=ROOT / "credentials") as directory:
secret = Path(directory) / "password"
secret.write_text(config["password"] + "\n")
secret.chmod(0o600)
askpass = Path(directory) / "askpass"
askpass.write_text('#!/bin/sh\nexec cat "$RMM_SSH_PASSWORD_FILE"\n')
askpass.chmod(0o700)
env = dict(os.environ, SSH_ASKPASS=str(askpass), SSH_ASKPASS_REQUIRE="force",
DISPLAY="rmm-preflight:0", RMM_SSH_PASSWORD_FILE=str(secret))
result = run(ssh + ["-o", "BatchMode=no", "-o", "NumberOfPasswordPrompts=1",
"-o", "PreferredAuthentications=password", target, remote],
env=env, stdin=subprocess.DEVNULL, start_new_session=True)
if result.returncode:
if b"Host key verification failed" in result.stderr or b"REMOTE HOST IDENTIFICATION HAS CHANGED" in result.stderr:
print("BLOCKED: SSH host key verification failed; no trust changes made")
fingerprints(address, port)
else:
print("BLOCKED: SSH authentication, connectivity, or remote preflight failed; private stderr withheld")
return 2
for line in result.stdout.decode(errors="replace").splitlines():
if re.fullmatch(r"(?:ARCH|KERNEL|GLIBC|OS_ID|OS_VERSION|HOSTNAME_READ|USER_HOME|PORT_18443)=[A-Za-z0-9_. \"-]{1,100}", line):
print(line)
print("SSH_READ_ONLY_PREFLIGHT=passed; no tunnel, deployment, package or system changes performed")
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except (ValueError, OSError, subprocess.TimeoutExpired):
print("BLOCKED: local validation/tool execution failed (private details withheld)")
raise SystemExit(2) from None
+321
View File
@@ -0,0 +1,321 @@
#!/usr/bin/env python3
"""Explicitly authorized non-root systemd bootstrap, preserving the old identity.
No enrollment, job approval, package operation, reboot or journal repair exists
here. Installation is create-only and refuses partially installed resources.
Rollback is manual: stop an idle service, verify manifests, rename state back
on the same filesystem and restore original root ownership. Never run both paths.
"""
import base64
from contextlib import closing, contextmanager
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
import uuid
import vm_access_check as access
from vm_passive_test import execute, remote_python, require
ROOT = access.ROOT
@contextmanager
def connection():
config = access.load_config(ROOT / 'credentials/linux-vm.conf')
known = Path.home() / '.ssh/known_hosts'
port, address = config.get('port', '22'), config['address']
execute(['ssh-keygen', '-F', address if port == '22' else f'[{address}]:{port}', '-f', str(known)])
with tempfile.TemporaryDirectory(prefix='rmm-bootstrap-', dir=ROOT / 'credentials') as folder:
folder = Path(folder)
secret, askpass = folder / 'password', folder / 'askpass'
secret.write_text(config.get('password', '') + '\n'); secret.chmod(0o600)
askpass.write_text('#!/bin/sh\nexec cat "$RMM_SSH_PASSWORD_FILE"\n'); askpass.chmod(0o700)
env = dict(os.environ, SSH_ASKPASS=str(askpass), SSH_ASKPASS_REQUIRE='force',
DISPLAY='rmm-bootstrap:0', RMM_SSH_PASSWORD_FILE=str(secret))
ssh = ['ssh', '-F', '/dev/null', '-T', '-p', port,
'-o', 'StrictHostKeyChecking=yes', '-o', f'UserKnownHostsFile={known}',
'-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'ForwardAgent=no',
'-o', 'ConnectTimeout=10', '-o', 'ConnectionAttempts=1',
'-o', 'NumberOfPasswordPrompts=1', '-o', 'LogLevel=ERROR']
if config.get('ssh_key_path'):
ssh += ['-i', str(Path(config['ssh_key_path']).expanduser()), '-o', 'IdentitiesOnly=yes']
yield ssh, config['username'] + '@' + address, env
def manifest(directory):
"""Byte identity plus read-only SQLite schema/row evidence; never returns rows."""
import sqlite3
result = {}
for path in sorted(directory.rglob('*')):
meta = path.lstat()
require(not path.is_symlink() and (path.is_dir() or path.is_file()), 'unsafe state entry')
if path.is_file():
require(meta.st_nlink == 1, 'state file has external hard links')
result[str(path.relative_to(directory))] = hashlib.sha256(path.read_bytes()).hexdigest()
databases = {}
for path in sorted(directory.rglob('*.sqlite')):
with closing(sqlite3.connect(path.as_uri() + '?mode=ro', uri=True)) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'journal integrity failed')
schema = db.execute('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name').fetchall()
tables = {}
for kind, name, _, _ in schema:
if kind != 'table':
continue
quoted = '"' + name.replace('"', '""') + '"'
rows = db.execute('SELECT * FROM ' + quoted).fetchall()
digest = hashlib.sha256(repr(sorted(rows, key=repr)).encode()).hexdigest()
tables[name] = dict(rows=len(rows), digest=digest)
databases[path.name] = dict(schema_hash=hashlib.sha256(repr(schema).encode()).hexdigest(), tables=tables)
return dict(files=result, databases=databases)
def remote_install(payload, root=Path('/')):
"""Runs only on the VM as root through verified SSH stdin."""
import fcntl
import grp
import pwd
os.umask(0o077)
owner = os.geteuid()
require(root != Path('/') or owner == 0, 'bootstrap requires root SSH')
source = Path(payload['source'])
state = source / 'state'
destination = root / 'var/lib/rmm-agent'
config = root / 'etc/rmm'
binary = root / 'usr/local/libexec/rmm-agent'
unit = root / 'etc/systemd/system/rmm-agent.service'
require(state.is_dir() and not state.is_symlink() and state.stat().st_uid == owner,
'preserved root-owned state missing')
require(not destination.exists() and not destination.is_symlink(), 'destination must be absent')
require(not config.is_symlink() and not binary.is_symlink() and not unit.is_symlink(), 'unsafe destination symlink')
for parent in (destination.parent, config.parent, binary.parent if binary.parent.exists() else binary.parent.parent, unit.parent):
require(parent.is_dir() and not parent.is_symlink() and parent.stat().st_uid == owner
and parent.stat().st_mode & 0o022 == 0, 'unsafe install parent')
require(state.stat().st_dev == destination.parent.stat().st_dev, 'atomic migration requires same filesystem')
lock = (state / 'agent.lock').open('rb')
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
for process in Path('/proc').glob('[0-9]*'):
try:
require(Path(os.readlink(process / 'exe')).name != 'rmm-agent', 'existing agent process')
except (FileNotFoundError, PermissionError):
pass
for descriptor in (process / 'fd').glob('*'):
try:
opened = os.readlink(descriptor)
require(not opened.startswith(str(state) + '/') or
(process.name == str(os.getpid()) and opened == str(state / 'agent.lock')),
'state has active file handles')
except (FileNotFoundError, PermissionError):
pass
identity = json.loads((state / 'identity.json').read_text())
require(identity['agent_id'] == payload['binding']['agent_id'] and
identity['asset_id'] == payload['binding']['asset_id'], 'server/device identity mismatch')
before = manifest(state)
report_path = config / 'vm-bootstrap-report.json'
report = dict(created=[], migration_complete=False, before=before,
original_state=str(state), service_started=False, attempts=[])
if config.exists():
require(config.stat().st_uid == owner and config.stat().st_mode & 0o022 == 0,
'unsafe existing config directory')
require(report_path.is_file() and not report_path.is_symlink() and
report_path.stat().st_uid == owner and report_path.stat().st_mode & 0o777 == 0o600,
'existing configuration has no protected bootstrap provenance')
report = json.loads(report_path.read_text())
require(not report['migration_complete'] and report['original_state'] == str(state)
and report['before'] == before and str(config) in report['created'],
'partial bootstrap provenance or original state changed')
require({p.name for p in config.iterdir()} <= {'vm-bootstrap-report.json', 'ca.pem', 'agent-binding.json'},
'unknown configuration file')
report.setdefault('attempts', [])
encoded = base64.b64decode(payload['binary'], validate=True)
require(hashlib.sha256(encoded).hexdigest() == payload['binary_sha256'], 'artifact hash mismatch')
require(encoded[:6] == b'\x7fELF\x02\x01' and encoded[18:20] == b'\x3e\x00', 'wrong ELF platform')
ca = payload['ca'].encode()
require(b'BEGIN CERTIFICATE' in ca and b'PRIVATE KEY' not in ca, 'public CA required')
try:
account = pwd.getpwnam('rmm-agent')
except KeyError:
account = None
if account:
require(0 < account.pw_uid < 1000 and account.pw_gid > 0 and
account.pw_shell in ('/usr/sbin/nologin', '/sbin/nologin') and
grp.getgrnam('rmm-agent').gr_gid == account.pw_gid and
not any('rmm-agent' in g.gr_mem for g in grp.getgrall()), 'existing account unsafe')
else:
try:
grp.getgrnam('rmm-agent')
raise RuntimeError('existing group without dedicated account; review required')
except KeyError:
pass
if binary.exists():
require(str(binary) in report['created'] and binary.stat().st_uid == owner and
binary.stat().st_mode & 0o777 == 0o755 and
hashlib.sha256(binary.read_bytes()).hexdigest() == payload['previous_binary_sha256'],
'existing binary is not the proven prior artifact')
require(not unit.exists() or str(unit) in report['created'], 'foreign existing unit')
def create(path, data, mode, uid=owner, gid=None):
if gid is None:
gid = os.getegid()
if path.exists():
require(str(path) in report['created'] and path.read_bytes() == data and
path.stat().st_uid == uid and path.stat().st_mode & 0o777 == mode,
'existing resource differs from proven bootstrap')
return
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
with os.fdopen(fd, 'wb') as stream:
stream.write(data); stream.flush(); os.fsync(stream.fileno())
os.fchown(stream.fileno(), uid, gid); os.fchmod(stream.fileno(), mode)
report['created'].append(str(path))
def save_report():
temporary = config / ('.bootstrap-report-' + uuid.uuid4().hex)
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(report, stream, indent=2); stream.flush(); os.fsync(stream.fileno())
os.replace(temporary, report_path)
attempt = {'completed': False}
report['attempts'].append(attempt)
try:
if not config.exists():
config.mkdir(mode=0o755)
report['created'].append(str(config))
os.chmod(config, 0o755)
save_report()
if not binary.parent.exists():
binary.parent.mkdir(mode=0o755)
report['created'].append(str(binary.parent))
elif binary.parent.stat().st_mode & 0o777 != 0o755:
require({p.name for p in binary.parent.iterdir()} <= {'rmm-agent'},
'nonstandard binary directory requires review')
os.chmod(binary.parent, 0o755)
save_report()
if account is None:
subprocess.run(['groupadd', '--system', 'rmm-agent'], check=True, capture_output=True)
report['created'].append('system-group:rmm-agent')
save_report()
subprocess.run(['useradd', '--system', '--gid', 'rmm-agent', '--home-dir', str(destination),
'--no-create-home', '--shell', '/usr/sbin/nologin', 'rmm-agent'],
check=True, capture_output=True)
report['created'].append('system-user:rmm-agent')
save_report()
account = pwd.getpwnam('rmm-agent')
require(account.pw_uid != 0 and account.pw_gid != 0, 'account must be non-root')
staged = binary.with_name('.rmm-agent-' + uuid.uuid4().hex)
try:
create(staged, encoded, 0o755)
subprocess.run(['/lib64/ld-linux-x86-64.so.2', '--list', str(staged)],
check=True, capture_output=True)
os.replace(staged, binary)
report['created'].remove(str(staged))
if str(binary) not in report['created']:
report['created'].append(str(binary))
report['binary_sha256'] = payload['binary_sha256']
finally:
if staged.exists():
staged.unlink()
report['created'].remove(str(staged))
save_report()
create(config / 'ca.pem', ca, 0o644)
# Binding reader requires service ownership/0600. Root owns the directory;
# the service cannot replace the file or change its pinned authority.
create(config / 'agent-binding.json', json.dumps(payload['binding']).encode(),
0o600, account.pw_uid, account.pw_gid)
create(unit, payload['unit'].encode(), 0o644)
os.rename(state, destination)
report['migration_complete'] = True
for path in [destination] + list(destination.rglob('*')):
require(not path.is_symlink(), 'unexpected migration symlink')
os.chown(path, account.pw_uid, account.pw_gid)
os.chmod(path, 0o700 if path.is_dir() else 0o600)
after = manifest(destination)
require(before == after, 'migration changed state bytes or SQLite data')
report.update(after=after, byte_and_sqlite_preservation=True,
agent_uid=account.pw_uid, agent_gid=account.pw_gid)
subprocess.run(['systemctl', 'daemon-reload'], check=True, capture_output=True)
attempt['completed'] = True
except Exception as error:
attempt['error'] = sanitized_error(error)
raise
finally:
if config.is_dir():
save_report()
lock.close()
# No automatic start: caller first establishes and verifies reverse tunnel.
return dict(migration_complete=True, byte_and_sqlite_preservation=True,
agent_uid=account.pw_uid, service_started=False,
created=report['created'], databases=after['databases'])
def sanitized_error(error):
import traceback
return {'exception_class': type(error).__name__,
'traceback_lines': [frame.lineno for frame in traceback.extract_tb(error.__traceback__)]}
def remote_script(payload, staging_root=None):
source = Path(__file__).read_text()
source = source[:source.index('\ndef install():')]
source = source.replace('import vm_access_check as access\nfrom vm_passive_test import execute, remote_python, require\n\nROOT = access.ROOT',
'def require(value, message):\n if not value: raise RuntimeError(message)\nROOT = Path("/")')
argument = '' if staging_root is None else ', root=Path(' + repr(str(staging_root)) + ')'
return source + '\ntry:\n print(json.dumps({"result": remote_install(' + repr(payload) + argument + ')}))\nexcept Exception as error:\n print(json.dumps({"error": sanitized_error(error)}))\n'
def install():
sys.path.insert(0, str(ROOT / 'tests'))
from backend_contract import request
from local_integration import A, CA, STATE
prior = json.loads((STATE / 'vm-passive/result.json').read_text())
require(prior.get('agent_stopped') and prior.get('tunnel_stopped'), 'old lifecycle not stopped')
agent = str(uuid.UUID(prior['agent_id']))
operator = (STATE / 'operator-a.token').read_text().strip()
status, _, devices = request(f'/api/v1/tenants/{A}/devices', token=operator)
require(status == 200 and not devices['truncated'], 'device lookup incomplete')
device = next((x for x in devices['items'] if x['agent_id'] == agent), None)
require(device is not None, 'preserved enrolled device absent from server')
require('enrollment_generation' in device and 'assignment_version' in device,
'backend must expose authenticated identity generations before bootstrap')
status, _, key = request('/api/v1/job-signing-key', token=operator)
require(status == 200 and key['algorithm'] == 'Ed25519' and key['schema_version'] == 1,
'authenticated signing key GET rejected')
raw = bytes.fromhex(key['public_key_hex'])
require(len(raw) == 32 and hashlib.sha256(raw).hexdigest() == key['key_id'], 'invalid signing key')
binding = {name: device[name] for name in ('asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')}
binding.update(tenant_id=A, public_key_hex=raw.hex())
artifact = STATE / 'vm-jobs/docker/rmm-agent'
versions = execute(['readelf', '--version-info', str(artifact)]).decode()
required = max(tuple(map(int, x.split('.'))) for x in re.findall(r'GLIBC_(\d+\.\d+)', versions))
require(required <= (2, 39), 'artifact exceeds Ubuntu 24.04 glibc')
data = artifact.read_bytes()
payload = dict(source=prior['remote_directory'], binding=binding,
binary=base64.b64encode(data).decode(), binary_sha256=hashlib.sha256(data).hexdigest(),
ca=CA.read_text(), unit=(ROOT / 'deploy/linux/rmm-agent.service').read_text())
previous = STATE / 'vm-jobs/build/release/rmm-agent'
payload['previous_binary_sha256'] = hashlib.sha256(previous.read_bytes()).hexdigest()
# No source/module import on the VM; all trusted code and data travel on stdin.
code = remote_script(payload)
with connection() as (ssh, target, env):
response = json.loads(remote_python(ssh, target, env, code))
if 'error' in response:
print('REMOTE_BOOTSTRAP_ERROR=' + json.dumps(response['error']))
raise RuntimeError('remote bootstrap failed; inspect protected report before retry')
result = response['result']
result.update(artifact_sha256=payload['binary_sha256'], required_glibc='.'.join(map(str, required)),
agent_id=device['agent_id'], asset_id=device['asset_id'], signing_key_authenticated=True)
report = STATE / 'vm-jobs/bootstrap.json'
with report.open('x') as stream:
json.dump(result, stream, indent=2)
report.chmod(0o600)
print('PASS create-only non-root systemd bootstrap; identity/SQLite bytes preserved; service not started')
if __name__ == '__main__':
os.umask(0o077)
try:
install()
except Exception:
print('BLOCKED: bootstrap incomplete; private diagnostics withheld; inspect managed state before retry')
raise SystemExit(2) from None
+201
View File
@@ -0,0 +1,201 @@
#!/usr/bin/env python3
"""Bounded real systemd-agent/MCP/browser Hello-World check; no root jobs."""
import datetime
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import uuid
import vm_job_bootstrap as bootstrap
from vm_passive_test import execute, remote_python, require
ROOT = bootstrap.ROOT
sys.path.insert(0, str(ROOT / 'tests'))
from backend_contract import request
from local_integration import A, B, STATE, sql
def store(path, value):
with path.open('w') as stream:
json.dump(value, stream, indent=2)
path.chmod(0o600)
def can_stop(states):
return isinstance(states, list) and all(state in ('succeeded', 'failed', 'cancelled', 'expired') for state in states)
def run():
os.umask(0o077)
directory = STATE / 'vm-jobs'
installed = json.loads((directory / 'bootstrap.json').read_text())
require(installed['migration_complete'] and installed['byte_and_sqlite_preservation'],
'controlled migration not proven')
require(not (directory / 'active.json').exists(), 'previous active lifecycle requires review')
agent = str(uuid.UUID(installed['agent_id']))
asset = str(uuid.UUID(installed['asset_id']))
operator = (STATE / 'operator-a.token').read_text().strip()
viewer_b = (STATE / 'viewer-b.token').read_text().strip()
base = f'/api/v1/tenants/{A}'
report = dict(agent_id=agent, asset_id=asset, testsimulation=True, service_started=False,
tunnel_started=False, jobs_verified=False)
active = directory / 'active.json'
def counts():
return sql(f"SELECT json_build_array((SELECT count(*) FROM jobs WHERE agent_id='{agent}'),(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id) WHERE j.agent_id='{agent}'),(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id) WHERE j.agent_id='{agent}'));")
def idle():
states = json.loads(sql(f"SELECT coalesce(json_agg(state),'[]'::json) FROM jobs WHERE agent_id='{agent}';"))
return can_stop(states)
baseline = json.loads(counts())
require(baseline == [0, 0, 0], 'existing VM job history requires separate continuation; no new run')
with bootstrap.connection() as (ssh, target, env):
# Refuse to compete with any existing reverse tunnel or native agent.
for path in Path('/proc').glob('[0-9]*/cmdline'):
try:
args = path.read_bytes().split(b'\0')
except OSError:
continue
require(not (args[0].split(b'/')[-1] == b'ssh' and
any(b'18443:127.0.0.1:18443' in x for x in args)), 'existing reverse tunnel')
preflight = '''import json,os,subprocess,pwd
from pathlib import Path
ports=subprocess.run(['ss','-H','-ltn'],capture_output=True,text=True,check=True).stdout
occupied=any(x.split()[3].rsplit(':',1)[-1]=='18443' for x in ports.splitlines())
status=subprocess.run(['systemctl','is-active','rmm-agent.service'],capture_output=True,text=True).stdout.strip()
account=pwd.getpwnam('rmm-agent')
print(json.dumps(dict(port_occupied=occupied,already_active=status=='active',uid=account.pw_uid,
state_uid=Path('/var/lib/rmm-agent').stat().st_uid,old_source_exists=Path(%r).exists())))
''' % (json.loads((STATE / 'vm-passive/result.json').read_text())['remote_directory'] + '/state')
facts = json.loads(remote_python(ssh, target, env, preflight))
require(not facts['port_occupied'] and not facts['already_active'] and
facts['uid'] == installed['agent_uid'] and facts['state_uid'] == facts['uid']
and facts['uid'] > 0 and not facts['old_source_exists'], 'VM service readiness failed')
control = str(directory / 'ssh-control')
require(not Path(control).exists(), 'prior SSH control socket requires review')
with (directory / 'tunnel.log').open('wb') as log:
tunnel = subprocess.Popen(ssh + ['-M', '-S', control, '-N', '-T',
'-o', 'ExitOnForwardFailure=yes', '-o', 'ServerAliveInterval=30',
'-o', 'ServerAliveCountMax=3', '-R', '127.0.0.1:18443:127.0.0.1:18443', target],
env=env, stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True)
report.update(tunnel_started=True, tunnel_pid=tunnel.pid)
store(active, report)
connected = ssh + ['-S', control, '-o', 'BatchMode=yes']
try:
deadline = time.monotonic() + 15
while not Path(control).exists():
require(tunnel.poll() is None and time.monotonic() < deadline, 'strict reverse tunnel setup failed')
time.sleep(.1)
listeners = execute(connected + [target, "ss -H -ltn 'sport = :18443'"], env=env).decode()
require([line.split()[3] for line in listeners.splitlines()] == ['127.0.0.1:18443'],
'reverse listener not exclusively IPv4 loopback')
execute(connected + [target, 'systemctl start rmm-agent.service'], env=env)
report['service_started'] = True
store(active, report)
contacts = set()
deadline = time.monotonic() + 100
while time.monotonic() < deadline:
status, _, device = request(base + '/devices/' + asset, token=operator)
require(status == 200 and device['agent_id'] == agent, 'preserved server identity mismatch')
if device['last_contact_at']:
moment = datetime.datetime.fromisoformat(device['last_contact_at'].replace('Z', '+00:00'))
if (datetime.datetime.now(datetime.timezone.utc) - moment).total_seconds() < 90:
contacts.add(device['last_contact_at'])
if len(contacts) >= 2 and 'run_script' in device['capabilities']:
break
time.sleep(2)
require(len(contacts) >= 2 and 'run_script' in device['capabilities'], 'fresh native agent heartbeats/capability missing')
report['heartbeat_times'] = sorted(contacts)
code = '''import json,subprocess
from pathlib import Path
pid=int(subprocess.run(['systemctl','show','--property=MainPID','--value','rmm-agent.service'],capture_output=True,text=True,check=True).stdout)
uid=Path('/proc/'+str(pid)).stat().st_uid
logs=subprocess.run(['journalctl','-u','rmm-agent.service','--since','5 minutes ago','-o','cat','--no-pager'],capture_output=True,text=True,check=True).stdout
print(json.dumps(dict(agent_pid=pid,agent_uid=uid,sync_blocked='restore/reconnect sync unavailable' in logs)))
'''
runtime = json.loads(remote_python(connected, target, env, code))
report.update(runtime)
require(runtime['agent_uid'] == installed['agent_uid'] and not runtime['sync_blocked'],
'non-root runtime or reconnect synchronization gate blocked')
require(request(f'/api/v1/tenants/{B}/devices/{asset}', token=viewer_b)[0] == 404,
'cross-tenant device exposure')
headers = {'Accept': 'application/json, text/event-stream', 'MCP-Protocol-Version': '2025-11-25'}
init = {'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': {
'protocolVersion': '2025-11-25', 'capabilities': {}, 'clientInfo': {'name': 'real-vm-hello', 'version': '1'}}}
status, response_headers, response = request('/mcp', 'POST', init, operator, headers)
require(status == 200 and response['result']['protocolVersion'] == '2025-11-25', 'MCP initialize failed')
session = response_headers.get('mcp-session-id', response_headers.get('Mcp-Session-Id'))
if session:
headers['Mcp-Session-Id'] = session
require(request('/mcp', 'POST', {'jsonrpc': '2.0', 'method': 'notifications/initialized'}, operator, headers)[0] == 202,
'MCP session initialization failed')
def call(name, arguments, identifier):
status, _, response = request('/mcp', 'POST', {'jsonrpc': '2.0', 'id': identifier,
'method': 'tools/call', 'params': {'name': name, 'arguments': arguments}}, operator, headers)
require(status == 200 and 'result' in response and not response['result'].get('isError'), 'MCP tool failed')
return response['result']['structuredContent']
now = datetime.datetime.now(datetime.timezone.utc)
plan_input = dict(tenant_id=A, action='run_script', targets={'asset_ids': [asset]},
parameters={'variants': [{'os_family': 'linux', 'interpreter': 'posix_sh', 'encoding': 'utf-8',
'script_utf8': "printf '%s\\n' 'Hello World'\n"}], 'arguments': [], 'execution_identity': 'agent_service',
'timeout_seconds': 60, 'max_output_bytes': 65536, 'expected_stdout': 'Hello World\n'},
schedule={'not_before': now.isoformat(), 'start_before': (now + datetime.timedelta(minutes=15)).isoformat(),
'offline_policy': 'wait_until_deadline'}, rollout={'max_parallel': 1, 'halt_after_failures': 1},
reason='Real Linux VM · non-root systemd agent · simulated dev/test UI approval')
plan = call('operations.plan', {'tenant_id': A, 'input': plan_input}, 2)
require(hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest() == plan['plan_hash']
and len(plan['jobs']) == 1 and plan['jobs'][0]['agent_id'] == agent, 'immutable plan binding mismatch')
report.update(plan_id=plan['plan_id'], plan_hash=plan['plan_hash'], job_id=plan['jobs'][0]['job_id'])
store(active, report)
pending = call('operations.submit', {'tenant_id': A, 'plan_id': plan['plan_id'],
'idempotency_key': 'vm-hello-' + uuid.uuid4().hex}, 3)
require(pending['approval_state'] == 'pending_approval' and pending['job_count'] == 0
and pending['execution_state'] is None and json.loads(counts()) == baseline,
'preapproval executable-artifact invariant failed')
operation = str(uuid.UUID(pending['operation_id']))
report.update(operation_id=operation, preapproval_counts=baseline)
store(active, report)
browser = subprocess.run([sys.executable, str(ROOT / 'deploy/browser/run_vm_jobs.py'),
'--operation-id', operation, '--asset-id', asset, '--dev-test-approve'], timeout=150)
report['browser_exit_code'] = browser.returncode
current = request(base + '/operations/' + operation, token=operator)[2]
report['final_operation'] = current
require(browser.returncode == 0 and current['approval_state'] == 'approved' and
current['job_count'] == 1 and current['execution_state'] == 'succeeded',
'browser or VM result not confirmed; never retry effect')
job = request(base + '/jobs/' + report['job_id'], token=operator)[2]
result = job['result']
require(job['state'] == 'succeeded' and bytes(result['stdout']) == b'Hello World\n' and
result['exit_code'] == 0 and result['evidence']['postconditions_passed'] is True,
'exact output/postconditions not confirmed')
audit = sql(f"SELECT json_build_object('approval_origin',approval_origin,'testsimulation',testsimulation,'principal',principal,'plan_hash',plan_hash) FROM authorizations WHERE tenant_id='{A}' AND operation_id='{operation}';")
report['approval_evidence'] = json.loads(audit)
require(report['approval_evidence']['testsimulation'] is True and
report['approval_evidence']['approval_origin'] == 'dev_test_browser_simulation' and
report['approval_evidence']['plan_hash'] == plan['plan_hash'], 'approval simulation audit mismatch')
report.update(jobs_verified=True, postapproval_counts=json.loads(counts()), output_verified=True)
print('PASS actual non-root systemd VM Hello World via MCP pending request and explicit simulated UI click')
finally:
# Unknown/queued/in-flight jobs retain the service and owned tunnel.
# No effect is retried and no process is killed on an ambiguous result.
if idle():
if report['service_started']:
execute(connected + [target, 'systemctl stop rmm-agent.service'], env=env)
report['service_stopped'] = True
tunnel.terminate(); tunnel.wait(timeout=15)
report['tunnel_stopped'] = True
active.unlink(missing_ok=True)
else:
report['lifecycle_retained_for_active_or_unknown_jobs'] = True
store(active, report)
store(directory / 'result.json', report)
if __name__ == '__main__':
try:
run()
except Exception as error:
print('VM_JOB_CHECK_INCOMPLETE=' + json.dumps(bootstrap.sanitized_error(error)))
raise SystemExit(2) from None
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
"""Read-only legacy readiness gate, or explicit bounded systemd Hello-World run.
Without arguments this checks the original passive installation read-only.
After the separately authorized migration, --run-service-hello starts the
installed non-root service and owned tunnel, submits a fixed MCP request and
uses actual dev/test browser clicks. It never approves via API/DB, runs the
network agent as root, or creates/resets a device identity.
"""
import json
import os
from pathlib import Path
import subprocess
import tempfile
import vm_access_check as access
from vm_passive_test import execute, remote_python, require
ROOT = access.ROOT
def readiness(facts):
blockers = []
if not facts['identity_present']:
blockers.append('preserved_device_identity_missing')
if facts['ssh_uid'] == 0:
blockers.append('root_ssh_requires_existing_unprivileged_account_deployment')
if not facts['dedicated_account_present']:
blockers.append('no_existing_dedicated_unprivileged_test_account')
if facts['state_owner_uid'] == 0:
blockers.append('preserved_state_root_owned_requires_controlled_account_migration')
if facts['agent_count'] or facts['port_occupied']:
blockers.append('existing_agent_or_tunnel_requires_lifecycle_review')
# Even a successful readiness check is not deployment or sync acceptance.
return blockers
def private_report(value):
directory = ROOT / 'devstate/vm-jobs'
directory.mkdir(mode=0o700, exist_ok=True)
require(not directory.is_symlink() and directory.stat().st_uid == os.getuid()
and directory.stat().st_mode & 0o777 == 0o700, 'unsafe report directory')
path = directory / 'preflight.json'
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW
fd = os.open(path, flags, 0o600)
with os.fdopen(fd, 'w') as stream:
os.fchmod(stream.fileno(), 0o600)
json.dump(value, stream, indent=2)
def main():
os.umask(0o077)
config = access.load_config(ROOT / 'credentials/linux-vm.conf')
prior = json.loads((ROOT / 'devstate/vm-passive/result.json').read_text())
require(prior.get('agent_stopped') and prior.get('tunnel_stopped'),
'previous passive lifecycle not confirmed stopped')
directory = prior['remote_directory']
require(isinstance(directory, str) and directory.startswith('/')
and '\n' not in directory, 'invalid preserved state path')
known = Path.home() / '.ssh/known_hosts'
address, port = config['address'], config.get('port', '22')
lookup = address if port == '22' else f'[{address}]:{port}'
execute(['ssh-keygen', '-F', lookup, '-f', str(known)])
with tempfile.TemporaryDirectory(prefix='rmm-job-', dir=ROOT / 'credentials') as folder:
folder = Path(folder)
password = folder / 'password'
password.write_text(config.get('password', '') + '\n')
password.chmod(0o600)
askpass = folder / 'askpass'
askpass.write_text('#!/bin/sh\nexec cat "$RMM_SSH_PASSWORD_FILE"\n')
askpass.chmod(0o700)
env = dict(os.environ, SSH_ASKPASS=str(askpass), SSH_ASKPASS_REQUIRE='force',
DISPLAY='rmm-test:0', RMM_SSH_PASSWORD_FILE=str(password))
ssh = ['ssh', '-F', '/dev/null', '-T', '-p', port,
'-o', 'StrictHostKeyChecking=yes', '-o', f'UserKnownHostsFile={known}',
'-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'ForwardAgent=no',
'-o', 'ClearAllForwardings=yes', '-o', 'ConnectTimeout=10',
'-o', 'ConnectionAttempts=1', '-o', 'NumberOfPasswordPrompts=1',
'-o', 'LogLevel=ERROR']
if config.get('ssh_key_path'):
ssh += ['-i', str(Path(config['ssh_key_path']).expanduser()), '-o', 'IdentitiesOnly=yes']
# Only presence/ownership/process facts leave the VM. Never read a key,
# identity payload, log, process command line, or credential into output.
code = '''import glob,json,os,pwd,subprocess
from pathlib import Path
p=Path(%r)
agents=0
for f in glob.glob('/proc/[0-9]*/exe'):
try:
if os.path.basename(os.readlink(f))=='rmm-agent': agents+=1
except OSError: pass
ports=subprocess.run(['ss','-H','-ltn'],capture_output=True,text=True,check=True).stdout
print(json.dumps(dict(ssh_uid=os.geteuid(),identity_present=(p/'state/identity.json').is_file(),
state_owner_uid=p.stat().st_uid,dedicated_account_present=any(x.pw_uid>0 and
x.pw_name in ('rmm-agent','rmm-test') for x in pwd.getpwall()),agent_count=agents,
port_occupied=any(x.split()[3].rsplit(':',1)[-1]=='18443' for x in ports.splitlines()))))
''' % directory
facts = json.loads(remote_python(ssh, config['username'] + '@' + address, env, code))
blockers = readiness(facts)
report = dict(facts=facts, blockers=blockers, deployment_performed=False,
ui_approval_performed=False, jobs_executed=False,
identity_or_journal_modified=False)
private_report(report)
print('VM_JOB_PREFLIGHT=' + ('blocked' if blockers else 'ready_for_controlled_deployment'))
for blocker in blockers:
print('BLOCKED:', blocker)
print('No agent/tunnel started; no plan, approval, job, account or state change.')
return 2 if blockers else 0
if __name__ == '__main__':
import sys
if sys.argv[1:] == ['--run-service-hello']:
from vm_job_live import run
try:
run()
except Exception as error:
from vm_job_bootstrap import sanitized_error
print('VM_JOB_CHECK_INCOMPLETE=' + json.dumps(sanitized_error(error)))
raise SystemExit(2) from None
raise SystemExit(0)
try:
raise SystemExit(main())
except (RuntimeError, ValueError, OSError, KeyError, subprocess.TimeoutExpired):
print('BLOCKED: VM readiness check incomplete; private diagnostics withheld')
raise SystemExit(2) from None
+392
View File
@@ -0,0 +1,392 @@
#!/usr/bin/env python3
"""Read-only by default. Explicit future dev/test UI acceptance, never autofreedom.
This harness never starts/stops a VM service outside approved RMM jobs, edits target policy, prepares APT
baselines over SSH, resets journals, approves over HTTP/SQL, or retries effects.
Unwired fault/reboot/planning adapters are reported pending_protocol, not passed.
"""
import argparse
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import re
import ssl
import stat
import subprocess
import sys
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.modules.setdefault('vm_mvp_acceptance', sys.modules[__name__])
STATE = ROOT / 'devstate'
CA = STATE / 'pki/ca.pem'
ORIGIN = 'https://rmm-dev.internal:18443'
UNIT = 'rmm-mvp-test.service'
HELLO = "printf '%s\\n' 'Hello World'\n"
SERVICE_SETUP = """set -eu
unit=/etc/systemd/system/rmm-mvp-test.service
test -f "$unit"
test ! -L "$unit"
test ! -s "$unit"
test "$(stat -c '%u:%a:%h' "$unit")" = '0:600:1'
printf '%s' '[Unit]
Description=RMM explicitly approved temporary MVP test service
[Service]
Type=simple
User=rmm-agent
Group=rmm-agent
ExecStart=/usr/bin/sleep infinity
Restart=no
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
' > "$unit"
chmod 0644 "$unit"
systemctl daemon-reload
test "$(systemctl show --property=LoadState --value rmm-mvp-test.service)" = loaded
printf '%s\\n' 'RMM test unit prepared'
"""
TERMINAL = {'succeeded', 'failed', 'cancelled', 'expired'}
BASIC = ('hello', 'refresh_inventory', 'query_logs', 'service_setup', 'service_start', 'service_stop')
ADVANCED = ('apt_native_install', 'apt_native_update', 'ensure_package', 'apply_updates',
'duplicate_offer', 'lost_result_ack', 'cancel_queued', 'expired_offline', 'reboot_last')
EXECUTABLE = BASIC + ADVANCED
PENDING = {} # Routes are defined. Missing deployment/measurements are runtime blockers.
class GuardError(RuntimeError):
pass
def require(value, code):
if not value:
raise GuardError(code)
def now():
return dt.datetime.now(dt.timezone.utc)
def timestamp(value):
require(isinstance(value, str), 'timestamp_type')
parsed = dt.datetime.fromisoformat(value.replace('Z', '+00:00'))
require(parsed.utcoffset() is not None, 'timestamp_timezone_required')
return parsed
def strict_object(pairs):
result = {}
for key, value in pairs:
require(key not in result, 'duplicate_json_field')
result[key] = value
return result
def protected_json(path):
path = Path(path)
require(not path.is_symlink() and not path.parent.is_symlink(), 'protected_file_symlink')
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
with os.fdopen(fd, 'rb') as stream:
metadata = os.fstat(stream.fileno())
require(stat.S_ISREG(metadata.st_mode) and metadata.st_uid == os.getuid()
and metadata.st_mode & 0o777 == 0o600 and metadata.st_size <= 1024 * 1024,
'protected_file_owner_mode_size')
return json.loads(stream.read(), object_pairs_hook=strict_object)
def write_private(path, value):
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(value, stream, indent=2); stream.flush(); os.fsync(stream.fileno())
def config(value):
require(set(value) == {'profile', 'origin', 'connect_address', 'ca_file', 'tenant_id', 'asset_id', 'agent_id', 'agent_uid'}, 'config_fields')
require(value['profile'] == 'dev/test' and value['origin'] == ORIGIN
and value['connect_address'] == '127.0.0.1:18443' and Path(value['ca_file']) == CA,
'dev_test_loopback_verified_tls_only')
for name in ('tenant_id', 'asset_id', 'agent_id'):
require(str(uuid.UUID(value[name])) == value[name], 'canonical_fixed_uuid_required')
require(type(value['agent_uid']) is int and value['agent_uid'] > 0, 'network_agent_must_be_nonroot')
return value
def default_config():
installed = protected_json(STATE / 'vm-jobs/bootstrap.json')
return config(dict(profile='dev/test', origin=ORIGIN, connect_address='127.0.0.1:18443', ca_file=str(CA),
tenant_id='11111111-1111-4111-8111-111111111111', asset_id=installed['asset_id'],
agent_id=installed['agent_id'], agent_uid=installed['agent_uid']))
def schedule(start, deadline):
require(start.tzinfo is not None and deadline.tzinfo is not None
and start < deadline and deadline > now() and deadline - start <= dt.timedelta(hours=1),
'fixed_bounded_future_deadline_required')
return dict(not_before=start.isoformat(), start_before=deadline.isoformat(), offline_policy='wait_until_deadline')
def case_input(name, settings, start=None, planning=None):
require(name in EXECUTABLE, 'unknown_case')
if name in ADVANCED:
from vm_mvp_adapters import advanced_input
return advanced_input(name, settings, start or now(), planning)
start = start or now()
action, identity, timeout = name, 'agent_service', 60
parameters = {}
if name in ('hello', 'service_setup'):
action = 'run_script'
script, output = (HELLO, 'Hello World\n') if name == 'hello' else (SERVICE_SETUP, 'RMM test unit prepared\n')
identity = 'agent_service' if name == 'hello' else 'root'
parameters = dict(variants=[dict(os_family='linux', interpreter='posix_sh', encoding='utf-8', script_utf8=script)], arguments=[], expected_stdout=output)
elif name == 'refresh_inventory':
parameters = dict(scope='linux.base')
elif name == 'query_logs':
parameters = dict(unit=UNIT, since=(start-dt.timedelta(minutes=5)).isoformat(), until=start.isoformat(), max_lines=50)
else:
action, identity, timeout = 'set_service_state', 'root', 180
parameters = dict(unit=UNIT, state='running' if name == 'service_start' else 'stopped')
parameters.update(execution_identity=identity, timeout_seconds=timeout, max_output_bytes=65536)
return dict(tenant_id=settings['tenant_id'], action=action, targets={'asset_ids': [settings['asset_id']]}, parameters=parameters,
schedule=schedule(start, start+dt.timedelta(minutes=15)), rollout=dict(max_parallel=1, halt_after_failures=1),
reason='Explicit dev/test VM acceptance · ' + name)
def validate_run_artifact(artifact, settings, cases):
require(set(artifact) == {'profile', 'tenant_id', 'asset_id', 'agent_id', 'approved_cases', 'expires_at',
'permission_origin', 'chat_date', 'testsimulation', 'expected_principal'}, 'run_artifact_fields')
require(artifact['profile'] == 'dev/test' and artifact['testsimulation'] is True
and artifact['permission_origin'] == 'explicit_chat_user_permission'
and artifact['chat_date'] == '2026-11-12'
and artifact['expected_principal'] == 'simulated-testprincipal-a', 'explicit_simulated_chat_permission_required')
require(all(artifact[key] == settings[key] for key in ('tenant_id', 'asset_id', 'agent_id')), 'run_artifact_target_mismatch')
require(now() < timestamp(artifact['expires_at']) <= now()+dt.timedelta(hours=1), 'run_artifact_expired_or_unbounded')
require(isinstance(artifact['approved_cases'], list) and cases and len(set(cases)) == len(cases)
and set(cases) <= set(artifact['approved_cases']) and set(cases) <= set(EXECUTABLE), 'explicit_fixed_case_scope_required')
if any(name.startswith('service_') for name in cases):
require(all(name in cases for name in ('service_setup', 'service_start', 'service_stop'))
and cases.index('service_setup') < cases.index('service_start') < cases.index('service_stop'),
'service_setup_start_stop_must_be_fixed_explicit_order')
require('reboot_last' not in cases or cases[-1] == 'reboot_last', 'reboot_must_be_last')
def safe_states(states):
require(isinstance(states, list) and all(state in TERMINAL for state in states), 'outstanding_or_unknown_jobs_block_execution')
def validate_apt_manifest(value):
"""Pure format verification, NEVER evidence that live planning/acceptance ran."""
fields = {'type', 'schema_version', 'action', 'execution_profile', 'transaction_sha256', 'resolved_apt_transaction',
'transaction_bytes', 'provider_sha256', 'hook_sha256', 'apt_config_sha256', 'runtime_sha256', 'test_result_id'}
require(set(value) == fields and value['type'] == 'rmm.apt.helper-action' and value['schema_version'] == 2
and value['action'] in ('apt-native-acceptance', 'apt-execute') and value['execution_profile'] == 'dev-test', 'apt_manifest_schema')
raw = value['transaction_bytes']
require(isinstance(raw, list) and len(raw) <= 65536 and all(type(n) is int and 0 <= n < 256 for n in raw), 'apt_exact_transaction_bytes')
raw = bytes(raw)
require(hashlib.sha256(raw).hexdigest() == value['transaction_sha256'], 'apt_transaction_hash')
transaction = json.loads(raw, object_pairs_hook=strict_object)
require(transaction == value['resolved_apt_transaction'] and set(transaction) == {'schema_version', 'recipe_id', 'recipe_version', 'platform',
'resolver_config_version', 'no_reboot', 'status_fingerprint', 'baseline_sha256', 'packages'}, 'apt_full_typed_transaction')
require(type(transaction['schema_version']) is int and transaction['schema_version'] == 1 and transaction['recipe_version'] == '1' and transaction['platform'] == 'ubuntu-24.04-amd64'
and transaction['resolver_config_version'] == 'rmm-apt-local-v1' and transaction['no_reboot'] is True, 'apt_fixture_profile_no_reboot')
pins = {'rmm-apt-fixture-install-1.0': (None, '1.0', '74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867'),
'rmm-apt-fixture-update-1.1': ('1.0', '1.1', 'ef67c9fe0ba1443fcfe465768e1204763063c369a2a1da04af6027d8a9c00491')}
require(transaction['recipe_id'] in pins and len(transaction['packages']) == 1, 'apt_fixed_single_package_recipe')
old, new, pin = pins[transaction['recipe_id']]
package = transaction['packages'][0]
require(package == dict(name='rmm-mvp-test', architecture='all', old_version=old, new_version=new, origin='rmm-test',
sha256=pin, size=20672, archive_path='/var/lib/rmm-apt-transaction/archives/' + pin + '.deb'), 'apt_pinned_fixture_archive')
for digest in [value[name] for name in ('provider_sha256', 'hook_sha256', 'apt_config_sha256', 'runtime_sha256')] + [transaction['status_fingerprint'], transaction['baseline_sha256']]:
require(isinstance(digest, str) and re.fullmatch('[a-f0-9]{64}', digest), 'apt_full_measurement_pin_required')
require(isinstance(value['test_result_id'], str) and re.fullmatch('[A-Za-z0-9_.-]{1,128}', value['test_result_id']), 'apt_test_result_binding')
return transaction
def validate_reboot_checkpoint(checkpoint, new_boot, final_job):
# Pure verifier only: reboot/reconnect driver intentionally not invented.
require(set(checkpoint) == {'tenant_id', 'agent_id', 'asset_id', 'operation_id', 'job_id', 'attempt_id', 'plan_hash', 'old_boot_id'}, 'reboot_checkpoint_fields')
for field in set(checkpoint)-{'plan_hash'}:
uuid.UUID(checkpoint[field])
require(re.fullmatch('[a-f0-9]{64}', checkpoint['plan_hash']) and new_boot != checkpoint['old_boot_id'], 'real_new_boot_required')
uuid.UUID(new_boot)
require(final_job.get('job_id') == checkpoint['job_id'] and final_job.get('attempt_id') == checkpoint['attempt_id']
and final_job.get('state') == 'succeeded' and final_job.get('result', {}).get('evidence', {}).get('new_boot_id') == new_boot,
'authenticated_bound_reboot_result_required')
class Backend:
def __init__(self):
sys.path.insert(0, str(ROOT / 'tests'))
from backend_contract import request
from local_integration import sql
self.request, self.sql = request, sql
self.token = (STATE / 'operator-a.token').read_text().strip()
self.headers = {'Accept': 'application/json, text/event-stream', 'MCP-Protocol-Version': '2025-11-25'}
self.sequence = 1
# Standard verified CA and DNS name, no custom trust or invalid-cert flag.
ssl.create_default_context(cafile=str(CA))
init = dict(jsonrpc='2.0', id=1, method='initialize', params=dict(protocolVersion='2025-11-25', capabilities={}, clientInfo=dict(name='vm-mvp-readiness', version='1')))
status, headers, response = self.request('/mcp', 'POST', init, self.token, self.headers)
require(status == 200 and response['result']['protocolVersion'] == '2025-11-25', 'mcp_initialize_failed')
session = next((value for key, value in headers.items() if key.lower() == 'mcp-session-id'), None)
require(session, 'mcp_session_missing')
self.headers['Mcp-Session-Id'] = session
require(self.request('/mcp', 'POST', dict(jsonrpc='2.0', method='notifications/initialized'), self.token, self.headers)[0] == 202, 'mcp_initialized_failed')
def call(self, name, arguments):
require(name in ('tenants.list', 'inventory.list_devices', 'inventory.get_device', 'monitoring.get_health', 'operations.plan', 'operations.submit', 'jobs.get', 'jobs.cancel'), 'mcp_tool_not_allowlisted')
self.sequence += 1
status, _, response = self.request('/mcp', 'POST', dict(jsonrpc='2.0', id=self.sequence, method='tools/call', params=dict(name=name, arguments=arguments)), self.token, self.headers)
require(status == 200 and 'result' in response and not response['result'].get('isError'), 'mcp_tool_failed')
return response['result']['structuredContent']
def counts(self, tenant, job):
uuid.UUID(tenant); uuid.UUID(job)
return json.loads(self.sql(f"SELECT json_build_array((SELECT count(*) FROM jobs WHERE tenant_id='{tenant}' AND job_id='{job}'),(SELECT count(*) FROM outbox WHERE tenant_id='{tenant}' AND job_id='{job}'),(SELECT count(*) FROM execution_permits WHERE tenant_id='{tenant}' AND job_id='{job}'));"))
def outstanding_guard(self, settings):
agent = str(uuid.UUID(settings['agent_id']))
safe_states(json.loads(self.sql(f"SELECT coalesce(json_agg(state),'[]'::json) FROM jobs WHERE agent_id='{agent}';")))
def preflight(backend, settings):
status, _, auth = backend.request('/auth/config')
require(status == 200 and auth.get('dev_login_enabled') is True, 'actual_dev_test_login_profile_required')
tenants = backend.call('tenants.list', {})
require(any(x['tenant_id'] == settings['tenant_id'] for x in tenants['items']), 'tenant_not_visible')
devices = backend.call('inventory.list_devices', dict(tenant_id=settings['tenant_id']))
# Detail lookup remains authoritative even if a bounded list is truncated.
device = backend.call('inventory.get_device', dict(tenant_id=settings['tenant_id'], asset_id=settings['asset_id']))
require(device['agent_id'] == settings['agent_id'] and device['asset_id'] == settings['asset_id'], 'stored_vm_identity_mismatch')
backend.call('monitoring.get_health', dict(tenant_id=settings['tenant_id']))
backend.outstanding_guard(settings)
from vm_mvp_adapters import planning_from_device, PLANNING_FIELDS
try:
planning_from_device(device);planning_status='stored_planning_valid_format_not_native_acceptance'
except GuardError as error:
planning_status=str(error)
return device, dict(mcp_readonly_smoke=True, fixed_vm_resolved=True, list_truncated=devices['truncated'],
presence=device['presence'], capabilities=device['capabilities'],
executable_adapters=list(EXECUTABLE), adapter_status={name: ('unsupported_native_fault_injection_safe_replay_available' if name in ('duplicate_offer','lost_result_ack') else 'ready_adapter_runtime_not_configured') for name in EXECUTABLE},
apt_planning_status=planning_status, required_stored_planning_fields=list(PLANNING_FIELDS),
certificate_API_fingerprint_available=isinstance(device.get('certificate_sha256'),str), effects_performed=False)
def execute_case(backend, settings, artifact, name, folder, runtime=None):
if name in ADVANCED or name in ('service_setup','service_start','service_stop'):
from vm_mvp_adapters import execute_advanced
return execute_advanced(backend, settings, artifact, name, folder, runtime)
backend.outstanding_guard(settings)
device = backend.call('inventory.get_device', dict(tenant_id=settings['tenant_id'], asset_id=settings['asset_id']))
require(device['presence'] == 'online' and 0 <= (now()-timestamp(device['last_contact_at'])).total_seconds() < 90, 'fresh_live_agent_required')
plan_input = case_input(name, settings)
if runtime is not None: runtime.require_action(plan_input)
required = {plan_input['action'], 'rmm.sync.v1'}
if plan_input['parameters']['execution_identity'] == 'root':
required |= {'helper.root.v1', 'root:' + plan_input['action']}
require(any(re.fullmatch('helper.policy.sha256:[a-f0-9]{64}', c) for c in device['capabilities']), 'helper_policy_digest_missing')
require(required <= set(device['capabilities']), 'actual_negotiated_capability_missing')
plan = backend.call('operations.plan', dict(tenant_id=settings['tenant_id'], input=plan_input))
require(hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest() == plan['plan_hash'] and len(plan['jobs']) == 1, 'stored_plan_hash_or_target_count')
job = plan['jobs'][0]
require(job['agent_id'] == settings['agent_id'] and job['asset_id'] == settings['asset_id'], 'plan_target_changed')
expected = dict(profile='dev/test', case=name, tenant_id=settings['tenant_id'], agent_id=settings['agent_id'], asset_id=settings['asset_id'],
plan_hash=plan['plan_hash'], plan_payload_bytes=plan['plan_payload_bytes'], job_payload=job['payload'], job_payload_hash=job['job_payload_hash'],
expected_principal=artifact['expected_principal'])
pending = backend.call('operations.submit', dict(tenant_id=settings['tenant_id'], plan_id=plan['plan_id'], idempotency_key='mvp-' + uuid.uuid4().hex))
require(pending['approval_state'] == 'pending_approval' and pending['job_count'] == 0 and pending['execution_state'] is None
and backend.counts(settings['tenant_id'], job['job_id']) == [0, 0, 0], 'preapproval_artifact_invariant')
expected['operation_id'] = str(uuid.UUID(pending['operation_id']))
checkpoint = folder / (name + '-checkpoint.json')
write_private(checkpoint, dict(plan_id=plan['plan_id'], operation_id=expected['operation_id'], job_id=job['job_id'], attempt_id=job['attempt_id'],
plan_hash=plan['plan_hash'], state='pending_approval', no_retry=True))
expected_file = folder / (name + '-browser.json'); write_private(expected_file, expected)
process = subprocess.run([sys.executable, str(ROOT / 'deploy/browser/run_mvp_actions.py'), '--case-file', str(expected_file), '--dev-test-ui-approve'], timeout=120)
require(process.returncode == 0, 'browser_not_confirmed_preserve_checkpoint_do_not_retry')
deadline = time.monotonic() + plan_input['parameters']['timeout_seconds'] + 45
while True:
result = backend.call('jobs.get', dict(tenant_id=settings['tenant_id'], job_id=job['job_id']))
if result['state'] in TERMINAL or result['state'] in ('unknown', 'awaiting_reboot'):
break
require(time.monotonic() < deadline, 'result_timeout_unknown_no_retry')
time.sleep(2)
evidence = result.get('result', {}).get('evidence', {})
require(result['state'] == 'succeeded' and evidence.get('postconditions_passed') is True, 'job_not_verified_no_retry')
if name in ('hello', 'service_setup'):
expected_output = b'Hello World\n' if name == 'hello' else b'RMM test unit prepared\n'
require(result['result']['exit_code'] == 0 and bytes(result['result']['stdout']) == expected_output, 'exact_script_output')
if name == 'refresh_inventory':
require(timestamp(evidence['observed_at']) >= timestamp(plan_input['schedule']['not_before']), 'inventory_not_new')
if name == 'query_logs':
require(evidence.get('quality') == 'valid' and len(result['result']['stdout'])+len(result['result']['stderr']) <= 65536, 'bounded_valid_log_evidence_required')
require(backend.counts(settings['tenant_id'], job['job_id']) == [1, 1, 1], 'exactly_one_job_outbox_grant_required')
operation = expected['operation_id']
audit = json.loads(backend.sql(f"SELECT json_build_object('origin',approval_origin,'simulation',testsimulation,'principal',principal,'plan_hash',plan_hash) FROM authorizations WHERE tenant_id='{settings['tenant_id']}' AND operation_id='{operation}';"))
require(audit == dict(origin='dev_test_browser_simulation', simulation=True, principal=artifact['expected_principal'], plan_hash=plan['plan_hash']), 'simulated_approval_audit_mismatch')
write_private(folder / (name + '-result.json'), dict(case=name, verified=True, job_id=job['job_id'], attempt_id=job['attempt_id'],
operation_id=operation, plan_hash=plan['plan_hash'], audit=audit, output_not_published=True))
def main():
os.umask(0o077)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--config', type=Path)
parser.add_argument('--run-realtime-dev-ui-acceptance', action='store_true')
parser.add_argument('--user-artifact', type=Path)
parser.add_argument('--create-chat-user-artifact', type=Path)
parser.add_argument('--wire-verdict', type=Path)
parser.add_argument('--transport', choices=('wss','https'), default='wss')
parser.add_argument('--exercise-server-restart', action='store_true')
parser.add_argument('--exercise-native-renewal', action='store_true')
parser.add_argument('--case', action='append', choices=tuple(EXECUTABLE)+tuple(PENDING), default=[])
args = parser.parse_args()
settings = config(protected_json(args.config)) if args.config else default_config()
require(settings == default_config(), 'only_preserved_linux_vm_configuration_allowed')
if args.create_chat_user_artifact:
require(not args.run_realtime_dev_ui_acceptance and not args.case, 'artifact_creation_is_not_execution')
path=args.create_chat_user_artifact
path.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
require(path.parent.stat().st_mode & 0o777 == 0o700 and STATE in path.resolve().parents, 'private_devstate_artifact_directory_required')
write_private(path, dict(profile='dev/test', **{key:settings[key] for key in ('tenant_id','asset_id','agent_id')},
approved_cases=list(EXECUTABLE), expires_at=(now()+dt.timedelta(hours=1)).isoformat(),
permission_origin='explicit_chat_user_permission',chat_date='2026-11-12',testsimulation=True,
expected_principal='simulated-testprincipal-a'))
print('CHAT_PERMISSION_RECORDED: no readiness/proof/capability flags, no VM action')
return
artifact = None
if args.run_realtime_dev_ui_acceptance:
require(args.user_artifact is not None, 'protected_explicit_user_artifact_required')
artifact = protected_json(args.user_artifact)
validate_run_artifact(artifact, settings, args.case)
else:
require(not args.case and args.user_artifact is None and not args.exercise_server_restart and not args.exercise_native_renewal, 'case_selection_requires_explicit_run_flag')
backend = Backend()
_, report = preflight(backend, settings)
print(json.dumps(report, indent=2))
if artifact is not None:
require(args.wire_verdict is not None, 'current_wire_worker_verdict_required_before_any_native_effect')
from vm_mvp_runtime import OwnedRuntime
runtime = OwnedRuntime(settings,backend,protected_json(args.wire_verdict))
folder = STATE / ('mvp-acceptance-' + uuid.uuid4().hex)
folder.mkdir(mode=0o700)
try:
runtime.acquire(args.transport)
if args.exercise_server_restart: runtime.restart_server()
if args.exercise_native_renewal: runtime.native_renewal()
for name in args.case:
execute_case(backend, settings, artifact, name, folder, runtime)
finally:
runtime.release_if_idle()
print('SELECTED_CASES_COMPLETED: inspect individual verified/unsupported fault statuses; no full-MVP claim')
if __name__ == '__main__':
try:
main()
except Exception as error:
# No raw SDK/HTTP/browser/log errors, payloads, argv or credential values.
print('ACCEPTANCE_INCOMPLETE:' + (str(error) if isinstance(error, GuardError) else type(error).__name__))
raise SystemExit(2) from None
+235
View File
@@ -0,0 +1,235 @@
#!/usr/bin/env python3
"""Concrete public-contract acceptance adapters. Importing performs no I/O."""
import datetime as dt
import hashlib
import json
import subprocess
import sys
import time
import uuid
import vm_mvp_acceptance as m
APT_CASES = ('apt_native_install','apt_native_update','ensure_package','apply_updates')
RECIPES = {'apt_native_install':'rmm-apt-fixture-install-1.0','ensure_package':'rmm-apt-fixture-update-1.1',
'apt_native_update':'rmm-apt-fixture-update-1.1','apply_updates':'rmm-apt-fixture-update-1.1'}
PINS = {'1.0':'74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867',
'1.1':'ef67c9fe0ba1443fcfe465768e1204763063c369a2a1da04af6027d8a9c00491'}
PLANNING_FIELDS = ('fixture_package','baseline_sha256','sealed_baseline_sha256','status_fingerprint',
'dpkg_updates_empty','pending_triggers_empty','runtime','runtime_sha256',
'provider_sha256','hook_sha256','apt_config_sha256','prefetch')
def canonical(value):
return json.dumps(value,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode()
def digest(value):
return hashlib.sha256(value).hexdigest()
def planning_from_device(device):
"""Only already stored passive observation, never an SSH package scan."""
observations=(device.get('inventory') or {}).get('observations',[])
observation=next((x for x in observations if x.get('scope')=='linux.apt.planning'),None)
m.require(observation is not None,'not_configured:stored_linux.apt.planning_observation_missing')
m.require(observation.get('quality')=='valid' and observation.get('completeness')=='complete'
and observation.get('truncated') is False and 0 <= (m.now()-m.timestamp(observation['observed_at'])).total_seconds()<90,
'not_configured:fresh_complete_apt_planning_required')
data=observation.get('data',{})
missing=[key for key in PLANNING_FIELDS if key not in data]
m.require(not missing,'not_configured:apt_planning_fields_missing:'+','.join(missing))
m.require(data['dpkg_updates_empty'] is True and data['pending_triggers_empty'] is True,'not_configured:dpkg_dirty')
m.require(data['baseline_sha256']==data['sealed_baseline_sha256'] and __import__('re').fullmatch('[a-f0-9]{64}',data['baseline_sha256']),
'not_configured:actual_sealed_baseline_binding')
if 'baseline' in data:
m.require(digest(canonical(data['baseline']))==data['baseline_sha256'] and data['baseline'].get('rmm-mvp-test:all')==data['fixture_package'],'not_configured:full_baseline_hash_or_fixture_projection')
if 'status_records' in data:
m.require(digest(canonical(data['status_records']))==data['status_fingerprint'],'not_configured:resolver_fingerprint')
m.require(digest(canonical(data['runtime']))==data['runtime_sha256'] and
data['runtime']['apt_config_sha256']==data['apt_config_sha256'],'not_configured:runtime_config_binding')
for key in ('baseline_sha256','sealed_baseline_sha256','status_fingerprint','runtime_sha256','provider_sha256','hook_sha256','apt_config_sha256'):
m.require(isinstance(data[key],str) and __import__('re').fullmatch('[a-f0-9]{64}',data[key]),'not_configured:measured_sha256_required:'+key)
return data
def manifest(name,data,start):
recipe=RECIPES[name]; version='1.1' if recipe.endswith('1.1') else '1.0';old='1.0' if version=='1.1' else None
installed=data['fixture_package']
if old is None:
m.require(installed is None or installed.get('status') in ('purge ok not-installed','unknown ok not-installed'),
'not_configured:fixture_already_installed_no_approved_removal_or_idempotent_recipe')
else:
already_desired=name in ('ensure_package','apply_updates') and installed==dict(version=version,status='install ok installed')
m.require(already_desired or installed==dict(version=old,status='install ok installed'),'not_configured:fixture_upgrade_requires_actual_1.0_or_approved_exact_noop_baseline')
pin=PINS[version]
receipt=data['prefetch'].get(pin,{})
m.require(receipt.get('schema_version')==1 and receipt.get('sha256')==pin and receipt.get('size')==20672
and type(receipt.get('sealed_at_ms')) is int and receipt['sealed_at_ms']<=int(start.timestamp()*1000),
'not_configured:real_pregrant_prefetch_receipt_required')
transaction=dict(schema_version=1,recipe_id=recipe,recipe_version='1',platform='ubuntu-24.04-amd64',
resolver_config_version='rmm-apt-local-v1',no_reboot=True,status_fingerprint=data['status_fingerprint'],
baseline_sha256=data['baseline_sha256'],packages=[dict(name='rmm-mvp-test',architecture='all',old_version=old,
new_version=version,origin='rmm-test',sha256=pin,size=20672,archive_path='/var/lib/rmm-apt-transaction/archives/'+pin+'.deb')])
raw=canonical(transaction)
result=dict(type='rmm.apt.helper-action',schema_version=2,action='apt-native-acceptance' if name.startswith('apt_native_') else 'apt-execute',
execution_profile='dev-test',transaction_sha256=digest(raw),resolved_apt_transaction=transaction,transaction_bytes=list(raw),
**{key:data[key] for key in ('provider_sha256','hook_sha256','apt_config_sha256','runtime_sha256')},test_result_id='vm-'+uuid.uuid4().hex)
m.validate_apt_manifest(result)
return result
def advanced_input(name,settings,start,planning=None):
m.require(name not in ('duplicate_offer','lost_result_ack'),'existing_attempt_replay_has_no_new_plan')
identity='agent_service';timeout=60;end=start+dt.timedelta(minutes=15)
if name in APT_CASES:
m.require(planning is not None,'not_configured:stored_linux.apt.planning_required')
apt=manifest(name,planning,start);recipe=RECIPES[name];identity='root';timeout=300
if name.startswith('apt_native_'):
action='apt_native_acceptance';parameters=dict(recipe_id=recipe,recipe_version='1',apt_manifest=apt)
elif name=='ensure_package':
action='ensure_package';parameters=dict(provider='apt',package='rmm-mvp-test',version='1.1',architecture='all',
installation_context='system',recipe_id=recipe,recipe_version='1',apt_manifest=apt)
else:
action='apply_updates';parameters=dict(provider='apt',packages=['rmm-mvp-test'],reboot_policy='never',apt_manifest=apt)
elif name in ('cancel_queued','expired_offline'):
action='run_script';parameters=dict(variants=[dict(os_family='linux',interpreter='posix_sh',encoding='utf-8',script_utf8=m.HELLO)],
arguments=[],expected_stdout='Hello World\n')
if name=='cancel_queued':start=start+dt.timedelta(minutes=3);end=start+dt.timedelta(minutes=3)
else:end=start+dt.timedelta(seconds=60)
elif name=='reboot_last':
identity='root';timeout=240;action='reboot';parameters=dict(reason='Explicit final Linux VM dev/test acceptance',
dependency_group='single-test-vm',reconnect_timeout_seconds=180)
else:
raise m.GuardError('unknown_advanced_case')
parameters.update(execution_identity=identity,timeout_seconds=timeout,max_output_bytes=65536)
return dict(tenant_id=settings['tenant_id'],action=action,targets=dict(asset_ids=[settings['asset_id']]),parameters=parameters,
schedule=m.schedule(start,end),rollout=dict(max_parallel=1,halt_after_failures=1),reason='Explicit dev/test VM acceptance · '+name)
def authorize(backend,settings,artifact,name,folder,request,old_boot=None,verify_boot=None):
if request['parameters']['execution_identity']=='root':
# Default automation has no root:execute permission. Do not edit its
# membership or borrow cookies: the simulated human prepares root plans
# through actual UI interactions, then MCP reads the same stored request.
output=folder/(name+'-prepared.json')
preparation=folder/(name+'-prepare.json')
m.write_private(preparation,dict(profile='dev/test',case=name,phase='prepare',request=request,
asset_id=settings['asset_id'],tenant_id=settings['tenant_id'],agent_id=settings['agent_id'],
expected_principal=artifact['expected_principal'],output_file=str(output)))
process=subprocess.run([sys.executable,str(m.ROOT/'deploy/browser/run_mvp_actions.py'),'--case-file',str(preparation),
'--prepare-only','--dev-test-ui-approve'],timeout=120)
m.require(process.returncode==0,'root_UI_preparation_unconfirmed_no_requester_scope_bypass')
prepared=m.protected_json(output);plan=prepared['plan'];pending=prepared['operation']
stored=backend.call('jobs.get',dict(tenant_id=settings['tenant_id'],operation_id=pending['operation_id']))
m.require(stored['plan_hash']==plan['plan_hash'] and stored['approval_state']=='pending_approval','root_UI_preparation_MCP_binding')
else:
plan=backend.call('operations.plan',dict(tenant_id=settings['tenant_id'],input=request))
pending=backend.call('operations.submit',dict(tenant_id=settings['tenant_id'],plan_id=plan['plan_id'],idempotency_key='mvp-'+uuid.uuid4().hex))
m.require(digest(bytes(plan['plan_payload_bytes']))==plan['plan_hash'] and len(plan['jobs'])==1,'exact_advanced_plan_bytes_required')
job=plan['jobs'][0]
m.require(job['agent_id']==settings['agent_id'] and job['asset_id']==settings['asset_id']
and job['payload']['execution_identity']==request['parameters']['execution_identity'],'fixed_advanced_job_binding')
m.require(pending['approval_state']=='pending_approval' and pending['job_count']==0 and pending['execution_state'] is None
and backend.counts(settings['tenant_id'],job['job_id'])==[0,0,0],'preapproval_zero_artifacts')
checkpoint={**{key:settings[key] for key in ('tenant_id','asset_id','agent_id')},'operation_id':str(uuid.UUID(pending['operation_id'])),
'job_id':job['job_id'],'attempt_id':job['attempt_id'],'plan_hash':plan['plan_hash']}
if old_boot is not None:
m.require(verify_boot is not None and verify_boot()==old_boot,'preapproval_boot_changed_no_reboot_approval')
checkpoint['old_boot_id']=old_boot
m.write_private(folder/(name+'-checkpoint.json'),checkpoint)
expected=dict(profile='dev/test',case=name,tenant_id=settings['tenant_id'],asset_id=settings['asset_id'],agent_id=settings['agent_id'],
plan_hash=plan['plan_hash'],plan_payload_bytes=plan['plan_payload_bytes'],job_payload=job['payload'],job_payload_hash=job['job_payload_hash'],
expected_principal=artifact['expected_principal'],operation_id=checkpoint['operation_id'])
path=folder/(name+'-browser.json');m.write_private(path,expected)
process=subprocess.run([sys.executable,str(m.ROOT/'deploy/browser/run_mvp_actions.py'),'--case-file',str(path),'--dev-test-ui-approve'],timeout=120)
m.require(process.returncode==0,'browser_advanced_approval_unconfirmed_no_retry')
return checkpoint
def wait_job(backend,settings,job_id,timeout):
end=time.monotonic()+timeout
while True:
result=backend.call('jobs.get',dict(tenant_id=settings['tenant_id'],job_id=job_id))
if result['state'] in m.TERMINAL or result['state']=='unknown':return result
m.require(time.monotonic()<end,'unknown:advanced_result_timeout_no_retry')
time.sleep(2)
def audit(backend,settings,checkpoint,artifact):
tenant=str(uuid.UUID(settings['tenant_id']));operation=str(uuid.UUID(checkpoint['operation_id']))
result=json.loads(backend.sql(f"SELECT json_build_object('origin',approval_origin,'simulation',testsimulation,'principal',principal,'plan_hash',plan_hash) FROM authorizations WHERE tenant_id='{tenant}' AND operation_id='{operation}';"))
m.require(result==dict(origin='dev_test_browser_simulation',simulation=True,principal=artifact['expected_principal'],plan_hash=checkpoint['plan_hash']), 'advanced_simulation_audit_binding')
return result
def execute_advanced(backend,settings,artifact,name,folder,runtime):
m.require(runtime is not None,'not_configured:owned_native_runtime_required')
backend.outstanding_guard(settings)
if name in ('duplicate_offer','lost_result_ack'):
# Public API can replay a durable original result. It cannot synthesize
# a server JobOffer or deliberately withhold the native agent's ACK.
path=folder/'hello-result.json';m.require(path.exists(),'not_configured:completed_same_run_hello_required')
result=runtime.replay_original_result(m.protected_json(path))
result.update(case=name,fault_injection='unsupported_native_fault_injection',
regression='original_durable_mtls_result_replay',native_fault_test_passed=False)
m.write_private(folder/(name+'-result.json'),result)
return
device=backend.call('inventory.get_device',dict(tenant_id=settings['tenant_id'],asset_id=settings['asset_id']))
planning=planning_from_device(device) if name in APT_CASES else None
request=m.case_input(name,settings) if name in m.BASIC else advanced_input(name,settings,m.now(),planning)
runtime.require_action(request)
if name in APT_CASES:
probe=runtime.apt_probe()
pins=runtime.apt_metadata()
m.require(all(planning[key]==pins[key] for key in ('provider_sha256','hook_sha256','apt_config_sha256','runtime_sha256')),
'not_configured:passive_planning_and_actual_root_installed_pins_differ')
recipe=RECIPES[name]
if name.startswith('apt_native_'):
m.require(probe['fixture_acceptance_ready'] and recipe in probe['bootstrap_recipes'],'not_configured:native_apt_bootstrap_readiness')
else:m.require(recipe in probe['accepted_recipes'],'not_configured:genuine_native_recipe_acceptance_required')
before=runtime.server_state()
if name in ('cancel_queued','expired_offline'):runtime.pause_idle()
if name=='reboot_last':runtime.prepare_reboot()
checkpoint=authorize(backend,settings,artifact,name,folder,request,before['boot_id'] if name=='reboot_last' else None,
(lambda:runtime.server_state()['boot_id']) if name=='reboot_last' else None)
job_id=checkpoint['job_id']
if name=='cancel_queued':
m.require(backend.counts(settings['tenant_id'],job_id)==[1,1,0],'offline_cancel_must_have_no_grant')
backend.call('jobs.cancel',dict(tenant_id=settings['tenant_id'],job_id=job_id,reason='Cancel fixed queued no-start VM acceptance'))
result=wait_job(backend,settings,job_id,30)
m.require(result['state']=='cancelled' and backend.counts(settings['tenant_id'],job_id)==[1,1,0],'cancel_not_confirmed_or_grant_issued')
runtime.resume_after_offline(job_id)
elif name=='expired_offline':
remaining=(m.timestamp(request['schedule']['start_before'])-m.now()).total_seconds()
if remaining>0:time.sleep(min(remaining+2,62))
runtime.resume_after_offline(job_id)
result=wait_job(backend,settings,job_id,60)
m.require(result['state']=='expired' and backend.counts(settings['tenant_id'],job_id)==[1,1,0],'expired_offline_effect_not_excluded')
else:
if name=='reboot_last':runtime.recover_reboot(checkpoint['old_boot_id'])
result=wait_job(backend,settings,job_id,request['parameters']['timeout_seconds']+45)
evidence=(result.get('result') or {}).get('evidence',{})
m.require(result['state']=='succeeded' and evidence.get('postconditions_passed') is True,'advanced_effect_not_verified_no_retry')
m.require(backend.counts(settings['tenant_id'],job_id)==[1,1,1],'exactly_one_advanced_attempt_grant')
if name=='reboot_last':
m.validate_reboot_checkpoint(checkpoint,runtime.server_state()['boot_id'],result)
inventory=backend.call('inventory.get_device',dict(tenant_id=settings['tenant_id'],asset_id=settings['asset_id']))
m.require(inventory['agent_id']==settings['agent_id'] and m.timestamp(inventory['observed_at'])>=m.timestamp(request['schedule']['not_before']),
'unknown:authenticated_new_boot_inventory_missing')
if name in APT_CASES:
apt=evidence.get('apt',{});package=request['parameters']['apt_manifest']['resolved_apt_transaction']['packages'][0]
m.require(apt.get('packages')==[dict(name=package['name'],architecture=package['architecture'],version=package['new_version'])]
and apt.get('fixture_payload_sha256')==digest((package['new_version']+'\n').encode()),'independent_apt_version_payload_postconditions')
if name.startswith('apt_native_'):
m.require(apt.get('test_acceptance') is True and apt.get('noProductionEvidence') is True
and apt.get('test_result_id')==request['parameters']['apt_manifest']['test_result_id']
and isinstance(apt.get('native_record_sha256'),str),'genuine_native_acceptance_record_required')
elif planning['fixture_package']==dict(version=package['new_version'],status='install ok installed'):
m.require(apt.get('changed') is False and apt.get('already_desired') is True
and apt.get('postcondition_already_met') is True and apt.get('transaction_executed') is False,
'exact_desired_noop_must_not_execute_a_transaction')
if name=='service_setup':
m.require(result['result']['exit_code']==0 and bytes(result['result']['stdout'])==b'RMM test unit prepared\n','root_test_service_setup_exact_result')
record=dict(case=name,verified=True,**checkpoint,audit=audit(backend,settings,checkpoint,artifact),output_not_published=True)
m.write_private(folder/(name+'-result.json'),record)
+384
View File
@@ -0,0 +1,384 @@
#!/usr/bin/env python3
"""Owned, explicitly opted-in VM lifecycle. Nothing runs at import/default smoke.
No package inventory, baseline resolver, private key export, policy expansion,
journal edits or reboot syscall. A reboot is solely a separately approved job.
"""
from contextlib import ExitStack
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import uuid
import vm_mvp_acceptance as m
sys.path.insert(0,str(m.ROOT/'deploy'))
import vm_job_bootstrap as bootstrap
class OwnedRuntime:
def __init__(self,settings,backend,verdict):
for field in ('agent_sha256','helper_sha256'):
m.require(isinstance(verdict.get(field),str) and __import__('re').fullmatch('[a-f0-9]{64}',verdict[field]),'not_configured:current_wire_build_hashes_required')
m.require(verdict.get('agent_id')==settings['agent_id'],'wire_verdict_fixed_identity_required')
self.settings,self.backend,self.verdict=settings,backend,verdict
self.stack=ExitStack();self.tunnel=None;self.started=False;self.owned_pid=None
self.directory=m.STATE/('mvp-owned-'+uuid.uuid4().hex)
self.directory.mkdir(mode=0o700)
self.transport_file='/etc/systemd/system/rmm-agent.service.d/90-mvp-owned-transport.conf'
self.original_enabled=None
self.helpers_started=[]
def remote(self,body):
code=('import os,json,hashlib,subprocess,sqlite3,socket,struct,ssl,http.client,time,uuid\nfrom pathlib import Path\n'
'from contextlib import closing\n'+body)
return json.loads(bootstrap.remote_python(self.ssh,self.target,self.env,code))
def server_state(self):
agent=str(uuid.UUID(self.settings['agent_id']))
value=self.backend.sql(f"SELECT json_build_object('agent_id',agent_id,'generation',enrollment_generation,'assignment',assignment_version,'connection',connection_generation,'boot_id',connection_boot_id,'transport',transport,'fingerprint',certificate_sha256,'sync_required',sync_required,'sync_ready',sync_ready,'helper_ready',helper_sync_ready,'restore_blocked',restore_blocked,'recovery',to_jsonb(a)->'sync_recovery_required') FROM agent_installations a WHERE agent_id='{agent}';")
m.require(value,'not_configured:server_identity_missing')
return json.loads(value)
def inspect(self):
return self.remote('''account=__import__('pwd').getpwnam('rmm-agent')
identity=json.loads(Path('/var/lib/rmm-agent/identity.json').read_text())
pid=int(subprocess.run(['systemctl','show','--property=MainPID','--value','rmm-agent.service'],capture_output=True,check=True).stdout)
ports=subprocess.run(['ss','-H','-ltn','sport = :18443'],capture_output=True,text=True,check=True).stdout
result=dict(agent_id=identity['agent_id'],asset_id=identity['asset_id'],uid=account.pw_uid,pid=pid,
boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),listener=bool(ports.strip()),
agent_sha256=hashlib.sha256(Path('/usr/local/libexec/rmm-agent').read_bytes()).hexdigest(),
helper_sha256=hashlib.sha256(Path('/usr/local/libexec/rmm-privileged-helper').read_bytes()).hexdigest(),
state_uid=Path('/var/lib/rmm-agent').stat().st_uid)
if pid:
result['process_uid']=Path('/proc/'+str(pid)).stat().st_uid;result['exe']=os.readlink('/proc/'+str(pid)+'/exe')
status=dict(line.split(':',1) for line in Path('/proc/'+str(pid)+'/status').read_text().splitlines() if ':' in line)
result['cap_eff']=status['CapEff'].strip();result['cap_amb']=status['CapAmb'].strip();result['nnp']=status['NoNewPrivs'].strip()
print(json.dumps(result))
''')
def verify_installation(self,facts):
m.require(facts['agent_id']==self.settings['agent_id'] and facts['asset_id']==self.settings['asset_id']
and facts['uid']==self.settings['agent_uid']==facts['state_uid'] and facts['uid']>0
and facts['agent_sha256']==self.verdict['agent_sha256'] and facts['helper_sha256']==self.verdict['helper_sha256'],
'not_configured:actual_nonroot_identity_or_wire_artifact_mismatch')
def acquire(self,transport):
self.backend.outstanding_guard(self.settings)
self.ssh,self.target,self.env=self.stack.enter_context(bootstrap.connection())
facts=self.inspect();self.verify_installation(facts)
old_connection=self.server_state()['connection']
m.require(facts['pid']==0 and not facts['listener'],'not_configured:idle_lifecycle_transfer_required_no_foreign_process_stopped')
self.transport=transport
content='[Service]\nEnvironment=RMM_TRANSPORT='+transport+'\n'
self.remote('''path=Path(%r)
assert not path.exists() and not path.is_symlink()
parent=path.parent
assert parent.is_dir() and not parent.is_symlink() and parent.stat().st_uid==0 and parent.stat().st_mode&0o022==0
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644)
with os.fdopen(fd,'w') as stream:stream.write(%r)
subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True)
print('{}')
'''%(self.transport_file,content))
self.start_helpers();self.start_tunnel();self.start_agent();state=self.wait_sync(old_connection)
m.require(state['transport']==transport,'not_configured:requested_native_transport_not_wired')
m.write_private(self.directory/'acquired.json',dict(agent_id=self.settings['agent_id'],agent_pid=self.owned_pid,
tunnel_pid=self.tunnel.pid,transport=transport,server_state=state))
def start_tunnel(self):
facts=self.inspect();m.require(not facts['listener'],'not_configured:foreign_reverse_listener')
control=str(self.directory/'ssh-control')
m.require(not Path(control).exists(),'owned_control_socket_requires_review')
self.log=self.stack.enter_context((self.directory/'tunnel.log').open('ab'))
self.tunnel=subprocess.Popen(self.ssh+['-M','-S',control,'-N','-T','-o','ExitOnForwardFailure=yes',
'-o','ServerAliveInterval=30','-o','ServerAliveCountMax=3','-R','127.0.0.1:18443:127.0.0.1:18443',self.target],
env=self.env,stdin=subprocess.DEVNULL,stdout=self.log,stderr=self.log,start_new_session=True)
end=time.monotonic()+15
while not Path(control).exists():
m.require(self.tunnel.poll() is None and time.monotonic()<end,'owned_reverse_tunnel_failed')
time.sleep(.1)
check=self.remote("lines=subprocess.run(['ss','-H','-ltn','sport = :18443'],capture_output=True,text=True,check=True).stdout.splitlines();print(json.dumps([line.split()[3] for line in lines]))")
m.require(check==['127.0.0.1:18443'],'reverse_listener_must_be_only_ipv4_loopback')
def start_helpers(self):
units=self.verdict.get('helper_units',{})
m.require(isinstance(units,dict) and set(units)<= {'rmm-helper-dev-test.service','rmm-apt-helper.service'},'only_owned_installed_helper_units_may_be_bootstrapped')
for unit,expected_hash in units.items():
state=self.remote("p=Path('/etc/systemd/system')/%r;assert p.is_file() and not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0;assert hashlib.sha256(p.read_bytes()).hexdigest()==%r;r=subprocess.run(['systemctl','is-active',%r],capture_output=True,text=True);print(json.dumps(r.stdout.strip()=='active'))"%(unit,expected_hash,unit))
if state:continue # Existing worker-owned daemon is not ours to stop.
self.helpers_started.append(unit)
self.remote("subprocess.run(['systemctl','start',%r],capture_output=True,check=True);print('{}')"%unit)
def start_agent(self):
facts=self.inspect();self.verify_installation(facts)
m.require(facts['pid']==0,'no_foreign_agent_start_or_restart')
self.remote("subprocess.run(['systemctl','start','rmm-agent.service'],capture_output=True,check=True);print('{}')")
for _ in range(50):
facts=self.inspect()
if facts['pid'] and facts.get('exe')=='/usr/local/libexec/rmm-agent':break
time.sleep(.1)
self.verify_installation(facts)
if facts['pid'] and facts.get('exe')=='/usr/local/libexec/rmm-agent':
self.started=True;self.owned_pid=facts['pid']
m.require(facts.get('process_uid')==self.settings['agent_uid'],'actual_network_agent_is_not_expected_nonroot_uid')
m.require(int(facts['cap_eff'],16)==0 and int(facts['cap_amb'],16)==0 and facts['nnp']=='1','network_agent_must_have_no_effective_or_ambient_capabilities')
def wait_sync(self,minimum_generation=None,timeout=100):
end=time.monotonic()+timeout
while True:
state=self.server_state()
if (state['sync_required'] and state['sync_ready'] and not state['restore_blocked']
and state.get('recovery') is False and (minimum_generation is None or state['connection']>minimum_generation)):
device=self.backend.call('inventory.get_device',dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id']))
if device['presence']=='online' and 0 <= (m.now()-m.timestamp(device['last_contact_at'])).total_seconds()<90:return state
m.require(time.monotonic()<end,'not_configured:actual_signed_sync_gate_or_fresh_heartbeat_blocked')
time.sleep(2)
def pause_idle(self):
self.backend.outstanding_guard(self.settings)
facts=self.inspect();self.verify_installation(facts)
m.require(self.started and facts['pid']==self.owned_pid,'only_owned_agent_may_be_stopped')
self.remote("subprocess.run(['systemctl','stop','rmm-agent.service'],capture_output=True,check=True);print('{}')")
self.started=False
def resume_after_offline(self,job_id):
# Resume ONLY this fixed approved queued/expired/cancelled attempt. No
# reset, regrant, changed deadline or fresh execution attempt is created.
agent=str(uuid.UUID(self.settings['agent_id']));job=str(uuid.UUID(job_id))
other=self.backend.sql(f"SELECT count(*) FROM jobs WHERE agent_id='{agent}' AND job_id<>'{job}' AND state NOT IN ('succeeded','failed','expired','cancelled');")
m.require(other=='0' and self.backend.counts(self.settings['tenant_id'],job)[2]==0,'offline_resume_ambiguous_grant_or_other_active_job')
self.start_agent();self.wait_sync()
def require_action(self,request):
state=self.wait_sync()
device=self.backend.call('inventory.get_device',dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id']))
required={request['action'],'rmm.sync.v1'}
if request['parameters']['execution_identity']=='root':
required|={'helper.root.v1','root:'+request['action']}
m.require(state['helper_ready'],'not_configured:independent_helper_sync_not_applied')
policy=self.control_probe('general')
m.require('helper.policy.sha256:'+policy['policy_sha256'] in device['capabilities'],'not_configured:actual_root_peer_policy_and_negotiated_digest_differ')
rules=policy['policy']
if request['action']=='reboot':m.require(rules['allow_reboot'],'not_configured:root_allow_reboot_false')
if request['action']=='run_script':
m.require(rules['allow_root_scripts'],'not_configured:narrow_test_root_script_policy_or_ReadWritePaths_missing')
if request['parameters'].get('variants',[{}])[0].get('script_utf8')==m.SERVICE_SETUP:
paths=self.remote("p=subprocess.run(['systemctl','show','--property=ReadWritePaths','--value','rmm-helper-dev-test.service'],capture_output=True,text=True,check=True);print(json.dumps(p.stdout.split()))")
m.require('/etc/systemd/system/rmm-mvp-test.service' in paths,
'not_configured:fixed_test_service_inode_ReadWritePaths_missing_no_broad_etc_write')
if request['action']=='set_service_state':m.require(request['parameters']['unit'] in rules['allowed_services'],'not_configured:fixed_test_service_not_in_helper_policy')
m.require(required<=set(device['capabilities']),'not_configured:actual_negotiated_action_capabilities_missing')
def control_probe(self,profile):
apt=profile=='apt_only'
pin=self.verdict.get('apt_pin_file' if apt else 'general_pin_file')
m.require(isinstance(pin,str) and pin.startswith('/etc/rmm/') and '..' not in Path(pin).parts,'not_configured:independently_root_provisioned_public_pin_file_required')
socket_path='/run/rmm-apt-helper/helper.sock' if apt else '/run/rmm-helper/control.sock'
# Execute as the real unprivileged peer. Only public metadata and nonce-
# bound read-only readiness/policy replies leave the VM, never a key.
script='''import json,os,socket,struct,uuid
from pathlib import Path
pin_path=Path(%r);sock_path=Path(%r)
for path in list(pin_path.parents)+[pin_path]+list(sock_path.parents):
meta=path.lstat();assert not path.is_symlink() and meta.st_uid==0 and meta.st_mode&0o022==0
meta=sock_path.lstat();assert meta.st_uid==0 and meta.st_mode&0o007==0
pin=json.loads(pin_path.read_text());challenge=str(uuid.uuid4())
apt=%r
request=dict(operation='apt_readiness_v1' if apt else 'general_policy_v1',challenge=challenge,identity=pin['identity'])
if apt:request['expected_stack_sha256']=pin['provider_stack_sha256']
s=socket.socket(socket.AF_UNIX);s.settimeout(10);s.connect(str(sock_path))
assert struct.unpack('3i',s.getsockopt(socket.SOL_SOCKET,socket.SO_PEERCRED,12))[1]==0
raw=json.dumps(request,separators=(',',':')).encode();s.sendall(struct.pack('!I',len(raw))+raw)
def receive(n):
b=b''
while len(b)<n:
c=s.recv(n-len(b));assert c;b+=c
return b
n=struct.unpack('!I',receive(4))[0];assert n<=1024*1024
response=json.loads(receive(n));s.close()
assert response['schema_version']==1 and response['challenge']==challenge and response['identity']==pin['identity'] and response['profile']==pin['profile']
field='provider_stack_sha256' if apt else 'policy_sha256';assert response[field]==pin[field]
assert response['ledger_inode'] and response['lock_inode']
print(json.dumps(response))
'''%(pin,socket_path,apt)
result=self.remote("p=subprocess.run(['/usr/sbin/runuser','-u','rmm-agent','--','/usr/bin/python3','-I','-c',%r],capture_output=True,text=True,check=True);print(p.stdout)"%script)
m.require(all(result['identity'][key]==self.settings[key] for key in ('tenant_id','asset_id','agent_id')),'root_peer_identity_mismatch')
server=self.server_state()
status,_,key=self.backend.request('/api/v1/job-signing-key',token=self.backend.token)
m.require(status==200 and result['identity']['signing_key_id']==key['key_id']
and result['identity']['enrollment_generation']==server['generation']
and result['identity']['assignment_version']==server['assignment'],'root_peer_current_authenticated_key_or_generation_mismatch')
if not apt:
policy=result['policy']
raw=json.dumps(policy,separators=(',',':')).encode()
m.require(hashlib.sha256(raw).hexdigest()==result['policy_sha256'],'independent_general_policy_bytes_hash_mismatch')
return result
def apt_probe(self):
general=self.control_probe('general')
apt=self.control_probe('apt_only')
m.require(all(apt[key]==general[key] for key in ('ledger_device','ledger_inode','lock_device','lock_inode')),
'not_configured:APT_and_general_helper_must_share_exact_ledger_and_lock_inodes')
return apt
def apt_metadata(self):
# Administrative installed-code/config pins only. No package/status/
# baseline read, resolver call, native probe or acceptance marker write.
return self.remote("p=Path('/etc/rmm/apt-helper.json');assert not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o777==0o600;c=json.loads(p.read_text())['apt_provider'];print(json.dumps(dict(runtime_sha256=c['runtime_sha256'],apt_config_sha256=c['apt_config_sha256'],provider_sha256=hashlib.sha256(Path('/usr/lib/rmm-apt-transaction/apt_transaction.py').read_bytes()).hexdigest(),hook_sha256=hashlib.sha256(Path('/usr/lib/rmm-apt-transaction/hook').read_bytes()).hexdigest())))")
def prepare_reboot(self):
m.require(self.tunnel is not None and self.tunnel.poll() is None and self.started,'reboot_requires_owned_agent_and_tunnel')
self.original_enabled=self.remote("p=subprocess.run(['systemctl','is-enabled','rmm-agent.service'],capture_output=True,text=True);print(json.dumps(p.stdout.strip()=='enabled'))")
if not self.original_enabled:self.remote("subprocess.run(['systemctl','enable','rmm-agent.service'],capture_output=True,check=True);print('{}')")
def recover_reboot(self,old_boot):
end=time.monotonic()+180;facts=None
while time.monotonic()<end:
try:
candidate=self.inspect()
if candidate['boot_id']!=old_boot:facts=candidate;break
except (RuntimeError,OSError,subprocess.TimeoutExpired):pass
time.sleep(2)
m.require(facts is not None,'unknown:reboot_no_authenticated_new_boot_no_retry')
self.verify_installation(facts)
if self.tunnel.poll() is None:self.tunnel.terminate();self.tunnel.wait(timeout=15)
# The old VM listener disappeared with its boot. Never unlink a foreign
# socket/listener or stop another worker's tunnel to force recovery.
self.start_tunnel()
m.require(facts['pid']>0 and facts.get('process_uid')==self.settings['agent_uid'],'unknown:agent_did_not_autostart_after_real_reboot')
self.owned_pid=facts['pid'];self.started=True
state=self.wait_sync(timeout=100)
m.require(state['boot_id']==facts['boot_id'] and state['helper_ready'],'unknown:new_boot_helper_sync_not_confirmed')
def restart_server(self):
self.backend.outstanding_guard(self.settings);before=self.server_state()
command=['docker','compose','--env-file',str(m.STATE/'.env'),'-f',str(m.ROOT/'deploy/compose.yaml'),'-p','rust-discovery-dev','restart','server']
subprocess.run(command,capture_output=True,check=True,timeout=60)
# Same deployed env/profile/volumes: no OIDC/config reset or DB mutation.
end=time.monotonic()+60
while True:
try:self.backend.__init__();break
except (OSError,m.GuardError):
m.require(time.monotonic()<end,'server_restart_verified_TLS_MCP_recovery_timeout')
time.sleep(2)
self.wait_sync(before['connection'])
def native_renewal(self):
arguments=dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id'])
before_api=self.backend.call('inventory.get_device',arguments)
m.require(isinstance(before_api.get('certificate_sha256'),str),'not_configured:device_API_certificate_sha256_not_exposed')
before=self.server_state();end=time.monotonic()+90
m.require(before_api['certificate_sha256']==before['fingerprint'],'certificate_API_and_server_ledger_disagree')
while time.monotonic()<end:
after=self.server_state()
if after['fingerprint']!=before['fingerprint']:
m.require(after['agent_id']==before['agent_id'] and after['generation']==before['generation'],'native_rotation_changed_device_identity')
fingerprint=self.remote('''root=Path('/var/lib/rmm-agent');agent=%r
selectors=[]
for path in root.rglob('current.json'):
value=json.loads(path.read_text())
if value.get('schema_version')==1 and value.get('agent_id')==agent and 'version' in value:selectors.append(path.parent/'versions'/str(uuid.UUID(value['version'])))
assert len(selectors)<=1
certificate=(selectors[0] if selectors else root)/'device.pem'
der=subprocess.run(['openssl','x509','-in',str(certificate),'-outform','DER'],capture_output=True,check=True).stdout
print(json.dumps(hashlib.sha256(der).hexdigest()))
'''%self.settings['agent_id'])
m.require(fingerprint==after['fingerprint'],'native_rotated_certificate_and_server_fingerprint_mismatch')
after_api=self.backend.call('inventory.get_device',arguments)
m.require(after_api['agent_id']==before_api['agent_id'] and after_api['enrollment_generation']==before_api['enrollment_generation']
and after_api.get('certificate_sha256')==fingerprint,'native_rotation_API_identity_generation_fingerprint_binding')
self.wait_sync();return
time.sleep(2)
raise m.GuardError('not_configured:native_renewal_not_due_or_no_exposed_native_trigger_no_private_key_export')
def replay_original_result(self,checkpoint):
tenant=self.settings['tenant_id'];job=str(uuid.UUID(checkpoint['job_id']));attempt=str(uuid.UUID(checkpoint['attempt_id']))
before=self.backend.call('jobs.get',dict(tenant_id=tenant,job_id=job))
m.require(before['state']=='succeeded' and self.backend.counts(tenant,job)==[1,1,1], 'completed_original_attempt_required_for_replay')
fingerprint=self.server_state()['fingerprint']
script='''job=%r;attempt=%r;agent=%r;expected_fingerprint=%r
root=Path('/var/lib/rmm-agent')
with closing(sqlite3.connect((root/'journal.sqlite').as_uri()+'?mode=ro',uri=True)) as db:
row=db.execute('SELECT envelope FROM reconcile_results WHERE job=? AND attempt=? ORDER BY revision DESC LIMIT 1',(job,attempt)).fetchone()
assert row is not None, 'ORIGINAL_RESULT_NOT_ARCHIVED'
original=bytes(row[0]);assert 0<len(original)<=1048576
def varint(number):
out=bytearray()
while number>127:out.append((number&127)|128);number>>=7
out.append(number);return bytes(out)
def fields(data):
index=0;result={}
def number():
nonlocal index
value=0
for shift in range(0,70,7):
b=data[index];index+=1;value|=(b&127)<<shift
if b<128:return value
raise ValueError('WIRE')
while index<len(data):
tag=number();kind=tag&7;tag>>=3
if kind==0:value=number()
elif kind==2:
size=number();value=data[index:index+size];index+=size;assert len(value)==size
else:raise ValueError('WIRE')
result.setdefault(tag,[]).append(value)
return result
outer=fields(original);result=fields(outer[25][0])
assert outer[4]==[agent.encode()] and result[1]==[job.encode()] and result[2]==[attempt.encode()]
assert result[4]==[b'succeeded'];message=outer[3][0].decode();uuid.UUID(message)
selectors=[]
for path in root.rglob('current.json'):
value=json.loads(path.read_text())
if value.get('schema_version')==1 and value.get('agent_id')==agent and 'version' in value:
version=str(uuid.UUID(value['version']));selectors.append(path.parent/'versions'/version)
assert len(selectors)<=1
identity=selectors[0] if selectors else root
certificate=identity/'device.pem';key=identity/'device.key'
der=subprocess.run(['openssl','x509','-in',str(certificate),'-outform','DER'],capture_output=True,check=True).stdout
assert hashlib.sha256(der).hexdigest()==expected_fingerprint
context=ssl.create_default_context(cafile='/etc/rmm/ca.pem');context.load_cert_chain(certificate,key)
class Connection(http.client.HTTPSConnection):
def connect(self):
raw=socket.create_connection(('127.0.0.1',18443),timeout=10)
self.sock=self._context.wrap_socket(raw,server_hostname='rmm-dev.internal')
batch=b'\\x08\\x01'+varint((2<<3)|2)+varint(len(original))+original
def consumed():
with closing(sqlite3.connect((root/'consumption.sqlite').as_uri()+'?mode=ro',uri=True)) as db:
return sum(job in row and attempt in row for row in db.execute('SELECT * FROM consumed'))
before=consumed();assert before==1
statuses=[]
for _ in range(2):
c=Connection('rmm-dev.internal',18443,context=context,timeout=10)
c.request('POST','/agent/v1/messages',body=batch,headers={'Content-Type':'application/x-protobuf'})
response=c.getresponse();statuses.append(response.status);response.read(1048577);c.close()
assert statuses==[200,200] and consumed()==before
print(json.dumps(dict(message_id=message,envelope_sha256=hashlib.sha256(original).hexdigest(),
statuses=statuses,local_attempt_consumptions=before,original_metadata_preserved=True)))
'''%(job,attempt,self.settings['agent_id'],fingerprint)
proof=self.remote(script)
after=self.backend.call('jobs.get',dict(tenant_id=tenant,job_id=job))
m.require(after==before and self.backend.counts(tenant,job)==[1,1,1],'replay_changed_result_attempt_or_grant')
message=str(uuid.UUID(proof['message_id']))
receipts=self.backend.sql(f"SELECT count(*) FROM job_receipts WHERE tenant_id='{tenant}' AND agent_id='{self.settings['agent_id']}' AND message_id='{message}';")
m.require(receipts=='1','original_result_receipt_not_deduplicated')
return dict(regression_verified=True,job_id=job,attempt_id=attempt,proof=proof,server_result_unchanged=True,server_receipts=1)
def release_if_idle(self):
try:self.backend.outstanding_guard(self.settings)
except Exception:return # Preserve process, tunnel, protected logs and journals.
try:
if self.started:self.pause_idle()
if self.original_enabled is False:self.remote("subprocess.run(['systemctl','disable','rmm-agent.service'],capture_output=True,check=True);print('{}')")
if self.tunnel and self.tunnel.poll() is None:self.tunnel.terminate();self.tunnel.wait(timeout=15)
for unit in reversed(self.helpers_started):
self.remote("with closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True)) as db:\n assert db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0\nsubprocess.run(['systemctl','stop',%r],capture_output=True,check=True);print('{}')"%unit)
if hasattr(self,'transport'):
content='[Service]\nEnvironment=RMM_TRANSPORT='+self.transport+'\n'
self.remote("p=Path(%r);assert p.is_file() and not p.is_symlink() and p.read_text()==%r;p.unlink();subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True);print('{}')"%(self.transport_file,content))
self.stack.close()
except Exception:
# Unknown cleanup is not permission for broad kill/rm/reset.
raise m.GuardError('owned_lifecycle_cleanup_unconfirmed_preserve_private_state')
+253
View File
@@ -0,0 +1,253 @@
#!/usr/bin/env python3
"""Bounded real-VM passive acceptance check. No root bootstrap or job execution."""
import hashlib
import io
import json
import os
from pathlib import Path
import re
import shlex
import subprocess
import sys
import tarfile
import tempfile
import time
import vm_access_check as access
ROOT = access.ROOT
sys.path.insert(0, str(ROOT / "tests"))
import local_integration as api
def require(value, message):
if not value:
raise RuntimeError(message)
def execute(args, **kwargs):
result = subprocess.run(args, capture_output=True, timeout=40, **kwargs)
require(result.returncode == 0, "command failed; private diagnostics withheld")
return result.stdout
def remote_python(ssh, target, env, code):
return execute(ssh + [target, "python3 -"], env=env, input=code.encode())
PREFLIGHT = r'''
import glob,json,os,platform,socket,subprocess
agents=0
for path in glob.glob('/proc/[0-9]*/exe'):
try:
if os.path.basename(os.readlink(path))=='rmm-agent': agents+=1
except OSError: pass
ports=subprocess.run(['ss','-H','-ltn'],capture_output=True,text=True,check=True).stdout
occupied=any(line.split()[3].rsplit(':',1)[-1]=='18443' for line in ports.splitlines())
libs=subprocess.run(['/sbin/ldconfig','-p'],capture_output=True,text=True,check=True).stdout
print(json.dumps(dict(architecture=platform.machine(),glibc=platform.libc_ver()[1],
hostname=socket.gethostname(),agents=agents,occupied=occupied,libraries=libs,
test_directories=len(glob.glob(os.path.expanduser('~/rmm-passive-test.*'))))))
'''
def main():
os.umask(0o077)
config = access.load_config(ROOT / "credentials/linux-vm.conf")
known = Path.home() / ".ssh/known_hosts"
address, port = config["address"], config.get("port", "22")
lookup = address if port == "22" else f"[{address}]:{port}"
require(known.is_file(), "trusted host key file missing")
execute(["ssh-keygen", "-F", lookup, "-f", str(known)])
for path in Path('/proc').glob('[0-9]*/cmdline'):
try:
args = path.read_bytes().split(b'\0')
except OSError:
continue
require(not (args[0].split(b'/')[-1] == b'ssh' and
any(b'18443:127.0.0.1:18443' in x for x in args)),
"existing reverse tunnel found; refusing competing listener")
require(api.request('/healthz')[0] == 200, "local trusted TLS health failed")
with tempfile.TemporaryDirectory(prefix="rmm-vm-", dir=ROOT / "credentials") as private:
private = Path(private)
password = private / "password"
password.write_text(config.get("password", "") + "\n")
password.chmod(0o600)
askpass = private / "askpass"
askpass.write_text('#!/bin/sh\nexec cat "$RMM_SSH_PASSWORD_FILE"\n')
askpass.chmod(0o700)
env = dict(os.environ, SSH_ASKPASS=str(askpass), SSH_ASKPASS_REQUIRE="force",
DISPLAY="rmm-test:0", RMM_SSH_PASSWORD_FILE=str(password))
ssh = ["ssh", "-F", "/dev/null", "-T", "-p", port,
"-o", "StrictHostKeyChecking=yes", "-o", f"UserKnownHostsFile={known}",
"-o", "GlobalKnownHostsFile=/dev/null", "-o", "ConnectTimeout=10",
"-o", "ConnectionAttempts=1", "-o", "ForwardAgent=no",
"-o", "NumberOfPasswordPrompts=1", "-o", "LogLevel=ERROR",
"-o", "PreferredAuthentications=publickey,password"]
if config.get("ssh_key_path"):
ssh += ["-i", str(Path(config['ssh_key_path']).expanduser()), "-o", "IdentitiesOnly=yes"]
target = f"{config['username']}@{address}"
facts = json.loads(remote_python(ssh, target, env, PREFLIGHT))
require(facts['architecture'] == 'x86_64', "unsupported VM architecture")
require(facts['agents'] == 0 and not facts['occupied'], "VM agent or port already occupied; nothing stopped")
print("PASS existing-process/port preflight; prior private test directory count:", facts['test_directories'])
artifact = private / "rmm-agent"
execute(["docker", "compose", "--env-file", str(ROOT / "devstate/.env"),
"-f", str(ROOT / "deploy/compose.yaml"), "-p", "rust-discovery-dev",
"cp", "server:/usr/local/bin/rmm-agent", str(artifact)])
artifact.chmod(0o700)
elf = artifact.read_bytes()
require(elf[:6] == b'\x7fELF\x02\x01' and int.from_bytes(elf[18:20], 'little') == 62,
"agent artifact is not Linux x86-64 ELF")
versions = execute(["readelf", "--version-info", str(artifact)]).decode()
required_glibc = max(tuple(map(int, x.split('.'))) for x in re.findall(r'GLIBC_(\d+\.\d+)', versions))
require(tuple(map(int, facts['glibc'].split('.'))) >= required_glibc, "agent requires newer glibc")
dynamic = execute(["readelf", "-d", str(artifact)]).decode()
libraries = re.findall(r'\(NEEDED\).*\[(.*?)\]', dynamic)
require(libraries and all(re.search(r'^\s*' + re.escape(lib) + r'\s', facts['libraries'], re.M) for lib in libraries),
"VM runtime library missing")
print("PASS artifact ELF/glibc/dependency compatibility; required glibc:", '.'.join(map(str, required_glibc)))
control = str(private / 'control')
tunnel_log = private / 'ssh.log'
remote_dir = None
agent_pid = None
report = {'artifact_sha256': hashlib.sha256(elf).hexdigest(),
'architecture': facts['architecture'], 'glibc': facts['glibc']}
report_dir = ROOT / 'devstate/vm-passive'
report_dir.mkdir(mode=0o700, exist_ok=True)
require(not (report_dir / 'active.json').exists(), "previous managed test record exists; inspect before retry")
with tunnel_log.open('wb') as log:
tunnel = subprocess.Popen(ssh + ['-M', '-S', control, '-N', '-T',
'-o', 'ExitOnForwardFailure=yes', '-o', 'ServerAliveInterval=30',
'-o', 'ServerAliveCountMax=3', '-R', '127.0.0.1:18443:127.0.0.1:18443', target],
env=env, stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True)
try:
deadline = time.monotonic() + 15
while not Path(control).exists():
require(tunnel.poll() is None, "strict SSH reverse tunnel failed")
require(time.monotonic() < deadline, "SSH control socket timeout")
time.sleep(.1)
connected = ssh + ['-S', control, '-o', 'BatchMode=yes']
execute(connected + ['-O', 'check', target], env=env)
listeners = execute(connected + [target, "ss -H -ltn 'sport = :18443'"], env=env).decode()
endpoints = [line.split()[3] for line in listeners.splitlines()]
require(endpoints == ['127.0.0.1:18443'], "reverse listener not exclusively IPv4 loopback")
print("PASS PC-initiated SSH reverse tunnel; VM listener exclusively 127.0.0.1:18443")
raw_token = api.token(api.A)
bundle = io.BytesIO()
with tarfile.open(fileobj=bundle, mode='w') as archive:
for name, data, mode in [('rmm-agent', elf, 0o700),
('ca.pem', api.CA.read_bytes(), 0o600), ('enroll.token', raw_token.encode(), 0o600)]:
info = tarfile.TarInfo(name)
info.size, info.mode = len(data), mode
archive.addfile(info, io.BytesIO(data))
remote_dir = execute(connected + [target,
'umask 077; d=$(mktemp -d "$HOME/rmm-passive-test.XXXXXXXX") || exit; '
'tar -xf - -C "$d" || exit; printf "%s" "$d"'], env=env, input=bundle.getvalue()).decode()
require(remote_dir.startswith('/') and '\n' not in remote_dir, "invalid private deployment path")
quoted = shlex.quote(remote_dir)
# The loader verifies symbol versions before any agent main function runs.
execute(connected + [target, f'/lib64/ld-linux-x86-64.so.2 --list {quoted}/rmm-agent'], env=env)
print("PASS VM dynamic loader resolved concrete artifact before native agent launch")
launch = f'''import os,subprocess
directory={remote_dir!r}
os.umask(0o077)
env=dict(os.environ,RMM_AGENT_STATE=directory+'/state',RMM_CA_FILE=directory+'/ca.pem',
RMM_ENROLLMENT_TOKEN_FILE=directory+'/enroll.token',RMM_CONNECT_ADDR='127.0.0.1:18443')
with open(directory+'/agent.log','ab') as log:
process=subprocess.Popen([directory+'/rmm-agent'],cwd=directory,env=env,
stdin=subprocess.DEVNULL,stdout=log,stderr=log,start_new_session=True)
print(process.pid)
'''
agent_pid = int(remote_python(connected, target, env, launch))
# Identify only the exact binary in the newly created private directory.
code = f'''import glob,json,os
path={remote_dir!r}+'/rmm-agent'
pids=[]
for p in glob.glob('/proc/[0-9]*/exe'):
try:
if os.readlink(p)==path:pids.append(int(p.split('/')[2]))
except OSError:pass
print(json.dumps(pids))
'''
pids = json.loads(remote_python(connected, target, env, code))
require(pids == [agent_pid], "new VM agent process not uniquely identified")
agent_pid = pids[0]
report.update(remote_directory=remote_dir, agent_pid=agent_pid, tunnel_pid=tunnel.pid)
(report_dir / 'active.json').write_text(json.dumps(report))
identity = None
observed = None
contacts = set()
deadline = time.monotonic() + 100
viewer = (ROOT / 'devstate/viewer-a.token').read_text().strip()
while time.monotonic() < deadline:
if identity is None:
result = subprocess.run(connected + [target, f'cat {quoted}/state/identity.json'],
capture_output=True, env=env, timeout=15)
if result.returncode == 0:
identity = json.loads(result.stdout)
if identity:
status, data = api.request(f'/api/v1/tenants/{api.A}/devices', viewer)
require(status == 200, "tenant reader failed")
observed = next((x for x in json.loads(data)['items'] if x['agent_id'] == identity['agent_id']), None)
if observed and observed['last_contact_at']:
contacts.add(observed['last_contact_at'])
if observed and observed['inventory'] and len(contacts) >= 2:
break
require(tunnel.poll() is None, "tunnel ended before acceptance")
time.sleep(2)
require(observed and observed['inventory'] and len(contacts) >= 2, "real VM inventory/two fresh heartbeats not observed")
inventory = observed['inventory']
require(inventory['hostname'] == facts['hostname'] and inventory['architecture'] == 'x86_64'
and '24.04' in inventory['os'] and inventory['root_available_bytes'] > 0
and inventory['root_available_inodes'] > 0 and observed['observed_at'] and observed['received_at']
and observed['presence'] == 'online', "VM inventory evidence invalid")
viewer_b = (ROOT / 'devstate/viewer-b.token').read_text().strip()
status, data = api.request(f'/api/v1/tenants/{api.B}/devices', viewer_b)
require(status == 200 and identity['agent_id'] not in data.decode(), "VM tenant isolation failed")
execute(connected + [target, f'rm -- {quoted}/enroll.token'], env=env)
report.update(agent_id=identity['agent_id'], observed_at=observed['observed_at'],
received_at=observed['received_at'], heartbeat_times=sorted(contacts),
verified=True, tenant_isolation=True)
print("PASS actual VM enrollment/mTLS-WSS/inventory, two distinct fresh heartbeats, tenant isolation")
finally:
try:
if remote_dir and agent_pid:
cleanup = f'''import os,signal,time
pid={agent_pid}
expected={remote_dir!r}+'/rmm-agent'
try:
if os.readlink('/proc/'+str(pid)+'/exe')==expected: os.kill(pid,signal.SIGTERM)
except FileNotFoundError:pass
for _ in range(30):
try:
if os.readlink('/proc/'+str(pid)+'/exe')!=expected:break
except FileNotFoundError:break
time.sleep(.1)
else:raise RuntimeError('agent did not stop')
token={remote_dir!r}+'/enroll.token'
try:os.unlink(token)
except FileNotFoundError:pass
print('stopped')
'''
remote_python(ssh + ['-S', control, '-o', 'BatchMode=yes'], target, env, cleanup)
report['agent_stopped'] = True
finally:
tunnel.terminate()
tunnel.wait(timeout=15)
report['tunnel_stopped'] = True
(report_dir / 'result.json').write_text(json.dumps(report, indent=2))
if report.get('agent_stopped'):
(report_dir / 'active.json').unlink(missing_ok=True)
print("PASS bounded test stopped its own agent/tunnel; private PID/path evidence: devstate/vm-passive/result.json")
if __name__ == '__main__':
try:
main()
except RuntimeError as error:
print("FAILED:", error)
raise SystemExit(1) from None
except Exception:
print("FAILED: VM passive test incomplete; private details withheld; inspect managed state before retry")
raise SystemExit(1) from None
+422
View File
@@ -0,0 +1,422 @@
#!/usr/bin/env python3
"""Create-only root-helper readiness, never an RMM execution/acceptance grant.
Build in a private minimal Docker context, inspect/install with existing strict
SSH bootstrap. No package/service test, baseline/fixture, reboot, root script,
agent restart, enrollment, approval API or application database mutation.
"""
import argparse
import base64
from contextlib import closing
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import sqlite3
import subprocess
import sys
import tempfile
import uuid
import vm_job_bootstrap as agent_bootstrap
from vm_passive_test import execute, remote_python, require
ROOT = agent_bootstrap.ROOT
DIRECTORY = ROOT / 'devstate/vm-root'
UNIT = 'rmm-helper-dev-test.service'
def sha(data):
return hashlib.sha256(data).hexdigest()
def helper_config(binding, uid, gid):
require(uid > 0 and gid > 0, 'non-root agent identity required')
return dict(binding=dict(binding, public_key_hex=''), signing_key_file='/etc/rmm/helper-signing-key-v1.json',
agent_uid=uid, socket_gid=gid, socket='/run/rmm-helper/control.sock',
state='/var/lib/rmm-helper', allowed_services=['rmm-mvp-test.service'],
allow_reboot=False, allow_root_scripts=False, apt_provider=None)
def helper_can_stop(report):
return report.get('helper_started') is True and report.get('helper_consumed_count') == 0
def confined_runtime(status, gid):
mask = sum(1 << number for number in (0, 1, 3, 4, 6, 7, 8, 31))
return (status['NoNewPrivs'].strip() == '1' and status['Seccomp'].strip() == '2'
and all(int(value) == 0 for value in status['Uid'].split())
and all(int(value) == gid for value in status['Gid'].split())
and int(status['CapEff'].strip(), 16) & ~mask == 0
and int(status['CapBnd'].strip(), 16) == mask)
def build():
DIRECTORY.mkdir(mode=0o700, exist_ok=True)
require(not (DIRECTORY / 'rmm-privileged-helper').exists(), 'build artifact already exists; review version before replacement')
with tempfile.TemporaryDirectory(prefix='build-', dir=DIRECTORY) as folder:
folder = Path(folder)
for name in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml'):
shutil.copyfile(ROOT / name, folder / name)
for name in ('crates', 'tools/apt-transaction', 'migrations'):
shutil.copytree(ROOT / name, folder / name,
ignore=shutil.ignore_patterns('__pycache__', '*.pyc', 'target', 'node_modules',
'.git', 'devstate', 'credentials', 'secrets'))
(folder / 'Dockerfile').write_text('FROM rust:1.99.1-bookworm\nWORKDIR /src\nCOPY . .\nRUN cargo build --locked --release -p rmm-privileged-helper\n')
pins = {name: sha((folder / 'tools/apt-transaction' / name).read_bytes()) for name in ('apt_transaction.py', 'hook')}
subprocess.run(['docker', 'build', '-t', 'rmm-root-helper-build:dev', str(folder)], check=True)
container = execute(['docker', 'create', 'rmm-root-helper-build:dev']).decode().strip()
try:
execute(['docker', 'cp', container + ':/src/target/release/rmm-privileged-helper', str(DIRECTORY / 'rmm-privileged-helper')])
finally:
execute(['docker', 'rm', container])
artifact = DIRECTORY / 'rmm-privileged-helper'
artifact.chmod(0o700)
(DIRECTORY / 'build.json').write_text(json.dumps(dict(tool_pins=pins, artifact_sha256=sha(artifact.read_bytes())), indent=2))
print('PASS isolated OpenSSL-3 helper build; no VM changes')
def remote_inspect():
"""Strictly read-only VM inventory, with only nonsecret readiness facts."""
import pwd
account = pwd.getpwnam('rmm-agent')
paths = {'helper_binary': '/usr/local/libexec/rmm-privileged-helper',
'helper_state': '/var/lib/rmm-helper', 'helper_runtime': '/run/rmm-helper',
'helper_config': '/etc/rmm/helper-dev-test-v1.json',
'helper_key': '/etc/rmm/helper-signing-key-v1.json',
'helper_unit': '/etc/systemd/system/rmm-helper-dev-test.service',
'agent_dropin': '/etc/systemd/system/rmm-agent.service.d/10-dev-helper-v1.conf',
'provider_code': '/usr/lib/rmm-apt-transaction', 'provider_store': '/var/lib/rmm-apt-transaction'}
result = {'agent_uid': account.pw_uid, 'agent_gid': account.pw_gid, 'paths': {}}
for name, value in paths.items():
path = Path(value)
record = {'exists': path.exists(), 'symlink': path.is_symlink()}
if path.exists():
meta = path.lstat()
record.update(uid=meta.st_uid, gid=meta.st_gid, mode=oct(meta.st_mode & 0o777))
result['paths'][name] = record
for name in ('rmm-agent.service', 'rmm-helper-dev-test.service'):
process = subprocess.run(['systemctl', 'is-active', name], capture_output=True, text=True)
result[name] = process.stdout.strip() == 'active'
ports = subprocess.run(['ss', '-H', '-ltn', 'sport = :18443'], capture_output=True, text=True, check=True)
result['listener_present'] = bool(ports.stdout.strip())
state = Path('/var/lib/rmm-agent')
result['agent_state_safe'] = state.is_dir() and state.stat().st_uid == account.pw_uid and state.stat().st_mode & 0o777 == 0o700
return result
def remote_prepare(payload):
import pwd
import socket
import stat
os.umask(0o077)
require(os.geteuid() == 0, 'root bootstrap required')
facts = remote_inspect()
require(not facts['rmm-agent.service'] and not facts['rmm-helper-dev-test.service']
and not facts['listener_present'] and facts['agent_state_safe'], 'existing active lifecycle or unsafe state')
require(all(not value['exists'] and not value['symlink'] for value in facts['paths'].values()),
'root installation is create-only; existing resources require explicit review')
account = pwd.getpwnam('rmm-agent')
require(account.pw_uid == payload['agent_uid'] and account.pw_gid == payload['agent_gid'] and account.pw_uid > 0,
'dedicated agent account mismatch')
# Current operator-authenticated binding must match both preserved enrollment
# and the already pinned agent authority. No offer/request supplies trust.
identity = json.loads(Path('/var/lib/rmm-agent/identity.json').read_text())
binding = json.loads(Path('/etc/rmm/agent-binding.json').read_text())
require(binding == payload['binding'] and all(identity[name] == binding[name] for name in ('agent_id', 'asset_id')),
'existing identity/key binding differs; no automatic overwrite')
snapshot = manifest(Path('/var/lib/rmm-agent'))
report = {'created': [], 'completed': False, 'helper_started': False,
'agent_state_before': snapshot, 'no_rmm_jobs_executed': True,
'apt_execution_enabled': False, 'reboot_enabled': False}
report_path = Path('/etc/rmm/root-bootstrap-v1-report.json')
require(not report_path.exists() and not report_path.is_symlink(), 'prior root report requires review')
binary = Path('/usr/local/libexec/rmm-privileged-helper')
encoded = base64.b64decode(payload['binary'], validate=True)
require(sha(encoded) == payload['artifact_sha256'] and encoded[:6] == b'\x7fELF\x02\x01'
and encoded[18:20] == b'\x3e\x00', 'wrong helper artifact')
for parent in (binary.parent, Path('/etc/rmm'), Path('/etc/systemd/system'), Path('/var/lib')):
require(parent.is_dir() and not parent.is_symlink() and parent.stat().st_uid == 0
and parent.stat().st_mode & 0o022 == 0, 'unsafe root installation parent')
def create(path, data, mode):
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, mode)
with os.fdopen(fd, 'wb') as stream:
stream.write(data); stream.flush(); os.fsync(stream.fileno()); os.fchmod(stream.fileno(), mode)
report['created'].append(str(path))
def save():
temporary = report_path.with_name('.root-bootstrap-' + uuid.uuid4().hex)
fd = os.open(temporary, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(report, stream, indent=2); stream.flush(); os.fsync(stream.fileno())
os.replace(temporary, report_path)
try:
save()
create(binary, encoded, 0o755)
subprocess.run(['/lib64/ld-linux-x86-64.so.2', '--list', str(binary)], capture_output=True, check=True)
# Compiled-in trusted bytes only. No fixtures, baselines, active context,
# acceptance records, package execution or unsigned grants are produced.
subprocess.run([str(binary), '--install-apt-provider'], capture_output=True, check=True, umask=0o022)
report['created'] += ['/usr/lib/rmm-apt-transaction', '/var/lib/rmm-apt-transaction']
provider = Path('/usr/lib/rmm-apt-transaction')
require(all(sha((provider / name).read_bytes()) == digest for name, digest in payload['tool_pins'].items()),
'installed provider does not match reviewed build snapshot')
store = Path('/var/lib/rmm-apt-transaction')
metadata_path = store / 'install-metadata.json'
metadata = json.loads(metadata_path.read_text())
require(metadata == dict(schema_version=1, provider_sha256=payload['tool_pins']['apt_transaction.py'],
hook_sha256=payload['tool_pins']['hook']), 'trusted provider metadata mismatch')
require(metadata_path.stat().st_uid == 0 and metadata_path.stat().st_mode & 0o777 == 0o600
and {p.name for p in store.iterdir()} == {'archives', 'baselines', 'history', 'install-metadata.json'}
and all(not any((store / name).iterdir()) for name in ('archives', 'baselines', 'history')),
'unexpected artifacts, baseline or acceptance evidence')
report['provider_metadata_verified'] = True
report['no_fixture_baseline_or_acceptance_created'] = True
command = ['/usr/bin/python3', '-I', str(provider / 'apt_transaction.py')]
runtime = subprocess.run(command + ['inspect-runtime'], capture_output=True, check=True).stdout
runtime_object = json.loads(runtime)
canonical = json.dumps(runtime_object, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()
config_bytes = subprocess.run(command + ['print-config'], capture_output=True, check=True).stdout
sandbox = subprocess.run(command + ['inspect-sandbox'], capture_output=True)
report.update(runtime=runtime_object, runtime_sha256=sha(canonical), apt_config_sha256=sha(config_bytes),
tool_pins=payload['tool_pins'], inspect_sandbox_exit=sandbox.returncode,
inspect_sandbox_passed=sandbox.returncode == 0,
sandbox_note='Direct parent inspection is not the final signed worker sandbox; no fake active context created')
report['apt_readiness_blockers'] = [
'apt_provider_is_null_no_package_capability',
'readiness_unit_denies_mount_and_has_no_CAP_SYS_ADMIN_or_CAP_MKNOD',
'APT_setup_needs_private_mount_PID_net_namespaces_and_basic_device_nodes',
'ProtectSystem_strict_does_not_allow_package_database_and_payload_writes',
'final_child_must_drop_to_exact_eight_capabilities_NNP_seccomp_and_hide_control_sockets',
'no_signed_active_context_baseline_fixtures_or_native_acceptance_exist']
require(runtime_object['platform'] == 'ubuntu-24.04-amd64', 'unsupported native platform')
create(Path('/etc/rmm/helper-signing-key-v1.json'), json.dumps(payload['key']).encode(), 0o600)
config = helper_config(binding, account.pw_uid, account.pw_gid)
create(Path('/etc/rmm/helper-dev-test-v1.json'), json.dumps(config).encode(), 0o600)
state = Path('/var/lib/rmm-helper'); state.mkdir(mode=0o700); report['created'].append(str(state))
unit = Path('/etc/systemd/system/rmm-helper-dev-test.service')
create(unit, payload['unit'].encode(), 0o644)
dropins = Path('/etc/systemd/system/rmm-agent.service.d')
if not dropins.exists():
dropins.mkdir(mode=0o755); dropins.chmod(0o755); report['created'].append(str(dropins))
require(not dropins.is_symlink() and dropins.stat().st_uid == 0 and dropins.stat().st_mode & 0o022 == 0,
'unsafe agent dropin directory')
create(dropins / '10-dev-helper-v1.conf', b'[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper/control.sock\n', 0o644)
subprocess.run(['systemd-analyze', 'verify', str(unit)], capture_output=True, check=True)
report['unit_verify_passed'] = True
subprocess.run(['systemctl', 'daemon-reload'], capture_output=True, check=True)
subprocess.run(['systemctl', 'enable', UNIT], capture_output=True, check=True)
report['helper_enabled'] = True
subprocess.run(['systemctl', 'start', UNIT], capture_output=True, check=True)
report['helper_started'] = True
path = Path('/run/rmm-helper/control.sock')
import time
for _ in range(50):
if path.exists():
break
time.sleep(.1)
metadata = path.lstat()
pid = int(subprocess.run(['systemctl', 'show', '--property=MainPID', '--value', UNIT],
capture_output=True, check=True).stdout)
require(os.readlink('/proc/' + str(pid) + '/exe') == str(binary), 'helper has not execed reviewed binary')
status = dict(line.split(':', 1) for line in Path('/proc/' + str(pid) + '/status').read_text().splitlines() if ':' in line)
require(confined_runtime(status, account.pw_gid), 'helper kernel sandbox not active')
report['helper_runtime'] = {name: status[name].strip() for name in ('Uid', 'Gid', 'NoNewPrivs', 'Seccomp', 'CapEff', 'CapBnd')}
report['helper_pid'] = pid
parent = path.parent.stat()
require(stat.S_ISSOCK(metadata.st_mode) and metadata.st_uid == 0 and metadata.st_gid == account.pw_gid
and metadata.st_mode & 0o777 == 0o660 and parent.st_uid == 0 and parent.st_mode & 0o777 == 0o750,
'helper socket ACL mismatch')
peer = subprocess.run(['/usr/sbin/runuser', '-u', 'rmm-agent', '--', '/usr/bin/python3', '-I', '-c',
"import socket;s=socket.socket(socket.AF_UNIX);s.connect('/run/rmm-helper/control.sock');s.close()"],
capture_output=True, check=True)
report['socket_acl_verified'] = True
report['agent_uid_can_connect_without_request'] = peer.returncode == 0
with closing(sqlite3.connect('/var/lib/rmm-helper/consumption.sqlite')) as db:
report['helper_consumed_count'] = db.execute('SELECT count(*) FROM consumed').fetchone()[0]
require(report['helper_consumed_count'] == 0, 'unexpected helper consumption; retain lifecycle')
report['agent_state_after'] = manifest(Path('/var/lib/rmm-agent'))
require(snapshot == report['agent_state_after'], 'agent identity/journal changed during root readiness')
report['agent_state_unchanged'] = True
report['completed'] = True
except Exception as error:
report['error'] = sanitized_error(error)
raise
finally:
if helper_can_stop(report):
subprocess.run(['systemctl', 'stop', UNIT], capture_output=True, check=True)
report['helper_stopped'] = True
save()
return report
def remote_recheck(expected):
"""Versioned repair of proven own payload modes plus post-exec readiness."""
import time
require(os.geteuid() == 0, 'root required')
facts = remote_inspect()
require(not facts['rmm-agent.service'] and not facts['rmm-helper-dev-test.service']
and not facts['listener_present'], 'unexpected active lifecycle')
previous = json.loads(Path('/etc/rmm/root-bootstrap-v1-report.json').read_text())
require(previous['completed'] and previous['helper_consumed_count'] == 0, 'unproven previous bootstrap')
before = manifest(Path('/var/lib/rmm-agent'))
require(before == previous['agent_state_after'], 'agent state changed since readiness')
binary = Path('/usr/local/libexec/rmm-privileged-helper')
require(sha(binary.read_bytes()) == expected['artifact_sha256'], 'helper artifact changed')
config = json.loads(Path('/etc/rmm/helper-dev-test-v1.json').read_text())
require(config['apt_provider'] is None and not config['allow_reboot'] and not config['allow_root_scripts'], 'execution policy unexpectedly enabled')
report = dict(completed=False, mode_corrections=[], helper_started=False, helper_consumed_count=None)
report_path = Path('/etc/rmm/root-readiness-v2-report.json')
require(not report_path.exists() and not report_path.is_symlink(), 'v2 report already exists; no blind recheck')
code = Path('/usr/lib/rmm-apt-transaction')
require(not code.is_symlink() and code.stat().st_uid == 0 and code.stat().st_mode & 0o022 == 0, 'unsafe provider directory')
for name, mode in (('apt_transaction.py', 0o644), ('hook', 0o755)):
path = code / name
require(not path.is_symlink() and path.stat().st_uid == 0 and path.stat().st_nlink == 1
and sha(path.read_bytes()) == expected['tool_pins'][name], 'payload provenance mismatch')
report['mode_corrections'].append(dict(name=name, old=oct(path.stat().st_mode & 0o777), new=oct(mode)))
try:
for correction in report['mode_corrections']:
(code / correction['name']).chmod(int(correction['new'], 8))
code.chmod(0o755)
with closing(sqlite3.connect('/var/lib/rmm-helper/consumption.sqlite')) as db:
require(db.execute('SELECT count(*) FROM consumed').fetchone()[0] == 0, 'nonempty root ledger')
subprocess.run(['systemctl', 'start', UNIT], capture_output=True, check=True)
report['helper_started'] = True
for _ in range(50):
if Path('/run/rmm-helper/control.sock').exists():
break
time.sleep(.1)
pid = int(subprocess.run(['systemctl', 'show', '--property=MainPID', '--value', UNIT], capture_output=True, check=True).stdout)
require(os.readlink('/proc/' + str(pid) + '/exe') == str(binary), 'helper has not execed reviewed artifact')
status = dict(line.split(':', 1) for line in Path('/proc/' + str(pid) + '/status').read_text().splitlines() if ':' in line)
require(confined_runtime(status, facts['agent_gid']), 'post-exec kernel sandbox mismatch')
report.update(helper_pid=pid, kernel_sandbox_verified=True,
helper_runtime={name: status[name].strip() for name in ('Uid', 'Gid', 'NoNewPrivs', 'Seccomp', 'CapEff', 'CapBnd')})
with closing(sqlite3.connect('/var/lib/rmm-helper/consumption.sqlite')) as db:
report['helper_consumed_count'] = db.execute('SELECT count(*) FROM consumed').fetchone()[0]
require(report['helper_consumed_count'] == 0 and manifest(Path('/var/lib/rmm-agent')) == before,
'unexpected helper or agent journal change')
report['agent_state_unchanged'] = True
report['completed'] = True
except Exception as error:
report['error'] = sanitized_error(error)
raise
finally:
if helper_can_stop(report):
subprocess.run(['systemctl', 'stop', UNIT], capture_output=True, check=True)
report['helper_stopped'] = True
fd = os.open(report_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(report, stream, indent=2); stream.flush(); os.fsync(stream.fileno())
return report
def remote_finish_readiness():
"""Finish the already-running v2 probe, without restarting or relaxing it."""
require(os.geteuid() == 0, 'root required')
previous = json.loads(Path('/etc/rmm/root-readiness-v2-report.json').read_text())
require(previous['helper_started'] and not previous.get('helper_stopped'), 'no pending owned readiness lifecycle')
facts = remote_inspect()
require(not facts['rmm-agent.service'] and not facts['listener_present'], 'unexpected agent or tunnel lifecycle')
pid = int(subprocess.run(['systemctl', 'show', '--property=MainPID', '--value', UNIT], capture_output=True, check=True).stdout)
require(pid > 0 and os.readlink('/proc/' + str(pid) + '/exe') == '/usr/local/libexec/rmm-privileged-helper', 'owned helper missing')
status = dict(line.split(':', 1) for line in Path('/proc/' + str(pid) + '/status').read_text().splitlines() if ':' in line)
require(confined_runtime(status, facts['agent_gid']), 'observed helper exceeds readiness confinement')
with closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro', uri=True)) as db:
require(db.execute('SELECT count(*) FROM consumed').fetchone()[0] == 0, 'active root consumption requires reconciliation')
bootstrap = json.loads(Path('/etc/rmm/root-bootstrap-v1-report.json').read_text())
require(manifest(Path('/var/lib/rmm-agent')) == bootstrap['agent_state_after'], 'agent state changed')
report = dict(completed=True, helper_pid=pid, kernel_sandbox_verified=True, helper_consumed_count=0,
agent_state_unchanged=True, effective_capabilities_subset=True,
helper_runtime={name: status[name].strip() for name in ('Uid', 'Gid', 'NoNewPrivs', 'Seccomp', 'CapEff', 'CapBnd')},
supersedes_preexec_v1_and_overstrict_parent_v2_measurements=True)
path = Path('/etc/rmm/root-readiness-v3-report.json')
require(not path.exists() and not path.is_symlink(), 'v3 report already exists')
subprocess.run(['systemctl', 'stop', UNIT], capture_output=True, check=True)
report['helper_stopped'] = True
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(report, stream, indent=2); stream.flush(); os.fsync(stream.fileno())
return report
def program(invocation):
"""Trusted remote source with one sanitized JSON response, never stderr echo."""
# Reuse only pure manifest/error functions, not bootstrap transport/config.
import inspect
common = '\n\n'.join(inspect.getsource(function) for function in
(agent_bootstrap.manifest, agent_bootstrap.sanitized_error))
own = '\n\n'.join(inspect.getsource(function) for function in (sha, helper_config, helper_can_stop, confined_runtime, remote_inspect, remote_prepare, remote_recheck, remote_finish_readiness))
return ('import base64,hashlib,json,os,subprocess,sqlite3,uuid\nfrom pathlib import Path\nfrom contextlib import closing\n'
'UNIT="rmm-helper-dev-test.service"\ndef require(value,message):\n if not value: raise RuntimeError(message)\n'
+ common + '\n' + own + '\ntry:\n print(json.dumps({"result":' + invocation
+ '}))\nexcept Exception as error:\n print(json.dumps({"error":sanitized_error(error)}))\n')
def remote(code):
with agent_bootstrap.connection() as (ssh, target, env):
response = json.loads(remote_python(ssh, target, env, code))
if 'error' in response:
print('ROOT_BOOTSTRAP_ERROR=' + json.dumps(response['error']))
raise RuntimeError('root readiness incomplete; inspect protected remote report before retry')
return response['result']
def install():
sys.path.insert(0, str(ROOT / 'tests'))
from backend_contract import request
from local_integration import A, STATE, sql
agent = json.loads((STATE / 'vm-jobs/bootstrap.json').read_text())
identifier = str(uuid.UUID(agent['agent_id']))
require(sql(f"SELECT count(*) FROM jobs WHERE agent_id='{identifier}' AND state NOT IN ('succeeded','failed','expired','cancelled');") == '0',
'unresolved VM job prevents helper bootstrap')
operator = (STATE / 'operator-a.token').read_text().strip()
status, _, device = request(f'/api/v1/tenants/{A}/devices/{agent["asset_id"]}', token=operator)
require(status == 200 and device['agent_id'] == identifier, 'preserved authenticated server binding missing')
status, _, key = request('/api/v1/job-signing-key', token=operator)
require(status == 200 and key['algorithm'] == 'Ed25519' and key['schema_version'] == 1
and len(bytes.fromhex(key['public_key_hex'])) == 32 and sha(bytes.fromhex(key['public_key_hex'])) == key['key_id'],
'authenticated signing key invalid')
facts = remote(program('remote_inspect()'))
print('ROOT_PREFLIGHT=' + json.dumps(facts))
require(not facts['rmm-agent.service'] and not facts['listener_present'], 'agent or tunnel unexpectedly active')
data = (DIRECTORY / 'rmm-privileged-helper').read_bytes()
built = json.loads((DIRECTORY / 'build.json').read_text())
require(sha(data) == built['artifact_sha256'], 'helper artifact changed after reviewed build')
versions = execute(['readelf', '--version-info', str(DIRECTORY / 'rmm-privileged-helper')]).decode()
glibc = max(tuple(map(int, x.split('.'))) for x in re.findall(r'GLIBC_(\d+\.\d+)', versions))
require(glibc <= (2, 39), 'helper glibc incompatible')
binding = {name: device[name] for name in ('asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')}
binding.update(tenant_id=A, public_key_hex=key['public_key_hex'])
payload = dict(binding=binding, key=key, agent_uid=facts['agent_uid'], agent_gid=facts['agent_gid'],
binary=base64.b64encode(data).decode(), artifact_sha256=built['artifact_sha256'], tool_pins=built['tool_pins'],
unit=(ROOT / 'deploy/linux/rmm-helper-dev-test.service').read_text())
result = remote(program('remote_prepare(' + repr(payload) + ')'))
result.update(helper_sha256=sha(data), required_glibc='.'.join(map(str, glibc)))
with (DIRECTORY / 'result.json').open('x') as stream:
json.dump(result, stream, indent=2)
print('PASS root-helper install/socket readiness; APT execution/reboot disabled; no RMM job or package mutation')
if __name__ == '__main__':
os.umask(0o077)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('mode', choices=('build', 'inspect', 'install', 'verify'))
args = parser.parse_args()
try:
if args.mode == 'build':
build()
elif args.mode == 'inspect':
print(json.dumps(remote(program('remote_inspect()'))))
elif args.mode == 'install':
install()
else:
result = remote(program('remote_recheck(' + repr(json.loads((DIRECTORY / 'build.json').read_text())) + ')'))
with (DIRECTORY / 'readiness-v2.json').open('x') as stream:
json.dump(result, stream, indent=2)
print('PASS post-exec helper kernel sandbox and provenance-checked payload modes; helper stopped')
except Exception as error:
print('ROOT_BOOTSTRAP_INCOMPLETE=' + json.dumps(agent_bootstrap.sanitized_error(error)))
raise SystemExit(2) from None
+315
View File
@@ -0,0 +1,315 @@
#!/usr/bin/env python3
"""Versioned final-source build/install/probe. No RMM job or package mutation.
Existing identity/journals stay in place. Only manifest-proven own deployment
files/empty old provider stores are moved to root-private history. Failures retain
reports/backups and never reset a journal, grant, acceptance or safety flag.
"""
import argparse
import base64
from contextlib import closing
import hashlib
import inspect
import json
import os
from pathlib import Path
import re
import shutil
import sqlite3
import subprocess
import sys
import uuid
import vm_job_bootstrap as previous
from vm_passive_test import execute, require, remote_python
ROOT=previous.ROOT
BASE=ROOT/'devstate/vm-final'
BOUND=('crates/agent','crates/agent-linux','crates/privileged-helper','crates/domain','crates/protocol',
'tools/apt-transaction/apt_transaction.py','tools/apt-transaction/hook','deploy/linux/rmm-apt-helper.service',
'Cargo.toml','Cargo.lock','rust-toolchain.toml')
def sha(data):return hashlib.sha256(data).hexdigest()
def bound_sources(root):
values={}
for relative in BOUND:
path=root/relative
paths=path.rglob('*') if path.is_dir() else [path]
for item in paths:
if item.is_file() and not any(part in ('target','__pycache__','.git') for part in item.parts):
require(not item.is_symlink(),'bound source symlink')
values[str(item.relative_to(root))]=sha(item.read_bytes())
return values
def build():
os.umask(0o077);BASE.mkdir(mode=0o700,parents=True,exist_ok=True)
run=BASE/uuid.uuid4().hex;run.mkdir(mode=0o700)
context=run/'context';context.mkdir(mode=0o700)
for name in ('Cargo.toml','Cargo.lock','rust-toolchain.toml'):shutil.copyfile(ROOT/name,context/name)
for name in ('crates','tools/apt-transaction','deploy/linux','migrations'):
shutil.copytree(ROOT/name,context/name,ignore=shutil.ignore_patterns('target','__pycache__','*.pyc','.git','devstate','credentials','secrets'))
sources=bound_sources(context)
require(sources==bound_sources(ROOT),'source changed during build snapshot')
(context/'Dockerfile').write_text('FROM rust:1.99.1-bookworm\nWORKDIR /src\nCOPY . .\nRUN cargo build --locked --release -p rmm-agent -p rmm-privileged-helper\n')
image='rmm-vm-final:'+run.name
subprocess.run(['docker','build','-t',image,str(context)],check=True)
container=execute(['docker','create',image]).decode().strip()
artifacts={}
try:
for name in ('rmm-agent','rmm-privileged-helper'):
path=run/name;execute(['docker','cp',container+':/src/target/release/'+name,str(path)]);path.chmod(0o700)
data=path.read_bytes();require(data[:6]==b'\x7fELF\x02\x01' and data[18:20]==b'\x3e\x00','wrong ELF target')
versions=execute(['readelf','--version-info',str(path)]).decode()
glibc=max(tuple(map(int,x.split('.'))) for x in re.findall(r'GLIBC_(\d+\.\d+)',versions))
require(glibc<=(2,39),'unsupported VM glibc')
dynamic=execute(['readelf','-d',str(path)]).decode()
needed=re.findall(r'\(NEEDED\).*\[(.*?)\]',dynamic)
require('libssl.so.3' in needed and 'libssl.so.1.1' not in needed,'OpenSSL-3 build required')
artifacts[name]=dict(sha256=sha(data),glibc='.'.join(map(str,glibc)),needed=needed)
finally:execute(['docker','rm',container])
receipt=dict(build_id=run.name,sources=sources,artifacts=artifacts,own_verification='locked Bookworm release build and ELF/dependency inspection',
source_owner_reported_tests=dict(rust=115,python_apt=56,installer=2,rerun_by_this_build=False))
(run/'build.json').write_text(json.dumps(receipt,indent=2))
# Pointer is not trust: install rechecks every source/artifact and old VM hash.
pointer=BASE/'current.json';temporary=BASE/('.current-'+run.name)
temporary.write_text(json.dumps(dict(build_id=run.name)));temporary.chmod(0o600);os.replace(temporary,pointer)
print('PASS final Bookworm agent/helper build; receipt directory: devstate/vm-final/'+run.name)
def remote_upgrade(payload):
import fcntl
import pwd
os.umask(0o077)
require(os.geteuid()==0,'root installation required')
account=pwd.getpwnam('rmm-agent');require((account.pw_uid,account.pw_gid)==(993,984),'dedicated UID/GID drift')
for unit in ('rmm-agent.service','rmm-helper-dev-test.service','rmm-apt-helper.service'):
status=subprocess.run(['systemctl','is-active',unit],capture_output=True,text=True)
require(status.stdout.strip() not in ('active','activating','deactivating'),'existing active service; no stop performed')
ports=subprocess.run(['ss','-H','-ltn','sport = :18443'],capture_output=True,check=True).stdout
require(not ports,'existing reverse listener')
require(not Path('/run/rmm-helper/control.sock').exists() and not Path('/run/rmm-apt-helper/helper.sock').exists(),'existing helper socket')
state=Path('/var/lib/rmm-agent');helper_state=Path('/var/lib/rmm-helper')
for path in (state,helper_state):
require(path.is_dir() and not path.is_symlink() and path.stat().st_mode&0o777==0o700,'unsafe preserved state')
lock=(state/'agent.lock').open('rb');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
for database in (state/'journal.sqlite',state/'consumption.sqlite',helper_state/'consumption.sqlite'):
with closing(sqlite3.connect(database.as_uri()+'?mode=ro',uri=True)) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','preserved SQLite corruption')
tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
if 'job_attempts' in tables:
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','expired','cancelled')").fetchone()[0]==0,'unresolved agent attempt')
if 'consumed' in tables:require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0,'unresolved consumption')
for process in Path('/proc').glob('[0-9]*'):
try:require(Path(os.readlink(process/'exe')).name not in ('rmm-agent','rmm-privileged-helper'),'existing runtime process')
except (FileNotFoundError,PermissionError):pass
for descriptor in (process/'fd').glob('*'):
try:
target=os.readlink(descriptor)
require(not (target.startswith(str(state)+'/') or target.startswith(str(helper_state)+'/'))
or (process.name==str(os.getpid()) and target==str(state/'agent.lock')),'active journal file handle')
except (FileNotFoundError,PermissionError):pass
before=manifest(state);helper_before=manifest(helper_state)
binding=json.loads(Path('/etc/rmm/agent-binding.json').read_text())
require(binding==payload['binding'],'authenticated current binding differs; no automatic recovery')
old_key=json.loads(Path('/etc/rmm/helper-signing-key-v1.json').read_text())
require(old_key==payload['key'],'independent helper key changed')
for path,expected in payload['expected_old'].items():
p=Path(path);require(p.is_file() and not p.is_symlink() and p.stat().st_uid==0 and sha(p.read_bytes())==expected,'unexpected old deployment bytes')
store=Path('/var/lib/rmm-apt-transaction');code=Path('/usr/lib/rmm-apt-transaction')
require({p.name for p in code.iterdir()}=={'apt_transaction.py','hook'},'foreign provider files')
require({p.name for p in store.iterdir()}=={'archives','baselines','history','install-metadata.json'},'provider store already used or changed')
require(all(not any((store/name).iterdir()) for name in ('archives','baselines','history')),'nonempty provider history; do not move acceptance')
old_metadata=json.loads((store/'install-metadata.json').read_text())
require(old_metadata==dict(schema_version=1,provider_sha256=payload['old_tool_pins']['apt_transaction.py'],hook_sha256=payload['old_tool_pins']['hook']), 'unexpected old install metadata')
history=Path('/var/lib/rmm-bootstrap-history')
if not history.exists():history.mkdir(mode=0o700)
require(not history.is_symlink() and history.stat().st_uid==0 and history.stat().st_mode&0o777==0o700,'unsafe history parent')
backup=history/payload['build_id'];backup.mkdir(mode=0o700)
require(all(Path(path).stat().st_dev==backup.stat().st_dev for path in list(payload['expected_old'])+[str(store),str(code)]),'backup requires same filesystem')
report=dict(stage='validated',backups={},created=[],agent_before=before,helper_before=helper_before,completed=False)
def save():
temp=backup/'.report.tmp';fd=os.open(temp,os.O_WRONLY|os.O_CREAT|os.O_TRUNC|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as stream:json.dump(report,stream,indent=2);stream.flush();os.fsync(stream.fileno())
os.replace(temp,backup/'report.json')
def retain(path):
path=Path(path);destination=backup/(str(path).lstrip('/').replace('/','__'))
require(not destination.exists(),'backup collision')
os.rename(path,destination);report['backups'][str(path)]=str(destination);save()
def create(path,data,mode,uid=0,gid=0):
path=Path(path);fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode)
with os.fdopen(fd,'wb') as stream:
stream.write(data);stream.flush();os.fsync(stream.fileno());os.fchown(stream.fileno(),uid,gid);os.fchmod(stream.fileno(),mode)
report['created'].append(str(path));save()
try:
save()
# Loader-check both newly staged binaries BEFORE replacing an old file.
staged={}
for name,encoded in payload['binaries'].items():
raw=base64.b64decode(encoded,validate=True);require(sha(raw)==payload['artifact_hashes'][name],'artifact changed in transit')
path=backup/name;create(path,raw,0o755)
subprocess.run(['/lib64/ld-linux-x86-64.so.2','--list',str(path)],capture_output=True,check=True);staged[name]=path
for path in payload['replace_files']:retain(path)
retain(store);retain(code)
for name,path in staged.items():
destination=Path('/usr/local/libexec')/name
require(not destination.exists(),'binary destination occupied after retain')
os.rename(path,destination);report['created'].append(str(destination));save()
# Correct restrictive umask only for reviewed public executable payloads.
subprocess.run(['/usr/local/libexec/rmm-privileged-helper','--install-apt-provider'],capture_output=True,check=True,umask=0o022)
report['stage']='provider_installed';save()
provider=Path('/usr/lib/rmm-apt-transaction/apt_transaction.py')
require(sha(provider.read_bytes())==payload['tool_pins']['apt_transaction.py'],'installed provider source mismatch')
command=['/usr/bin/python3','-I',str(provider)]
runtime=json.loads(subprocess.run(command+['inspect-runtime'],capture_output=True,check=True).stdout)
canonical=json.dumps(runtime,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode()
config_bytes=subprocess.run(command+['print-config'],capture_output=True,check=True).stdout
report.update(runtime=runtime,runtime_sha256=sha(canonical),apt_config_sha256=sha(config_bytes))
general=dict(profile='general',binding=dict(binding,public_key_hex=''),signing_key_file='/etc/rmm/helper-signing-key-v1.json',
agent_uid=993,socket_gid=984,socket='/run/rmm-helper/control.sock',state='/var/lib/rmm-helper',
allowed_services=['rmm-mvp-test.service'],allow_root_scripts=False,allow_reboot=False,apt_provider=None)
apt=dict(general,profile='apt_only',socket='/run/rmm-apt-helper/helper.sock',allowed_services=[],apt_provider=dict(schema_version=1,
execution_profile='dev-test',allow_fixture_bootstrap=True,allowed_recipes=['rmm-apt-fixture-install-1.0','rmm-apt-fixture-update-1.1'],
runtime_sha256=report['runtime_sha256'],apt_config_sha256=report['apt_config_sha256']))
create('/etc/rmm/helper-general-v2.json',json.dumps(general).encode(),0o600)
create('/etc/rmm/apt-helper.json',json.dumps(apt).encode(),0o600)
create('/etc/systemd/system/rmm-helper-dev-test.service',payload['general_unit'].encode(),0o644)
create('/etc/systemd/system/rmm-agent.service',payload['agent_unit'].encode(),0o644)
for name,config_file,mode in [('general','/etc/rmm/helper-general-v2.json','--inspect-general-profile'),('apt','/etc/rmm/apt-helper.json','--inspect-apt-profile')]:
result=subprocess.run(['/usr/local/libexec/rmm-privileged-helper',mode],env=dict(os.environ,RMM_HELPER_CONFIG=config_file),capture_output=True,check=True)
proof=json.loads(result.stdout)
require(proof['identity']['agent_id']==binding['agent_id'] and proof['identity']['signing_key_id']==payload['key']['key_id'],'inspected helper identity differs')
create('/etc/rmm/'+name+'-helper-proof-v2.json',result.stdout,0o640,0,984)
report[name+'_pin']=proof
# No actual service content: one root-private zero-byte writable inode
# is exclusively reserved for a later explicitly UI-approved setup job.
create('/etc/systemd/system/rmm-mvp-test.service',b'',0o600)
dropin=b'[Service]\nEnvironment=RMM_JOB_BINDING_FILE=/etc/rmm/agent-binding.json\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper/control.sock\nEnvironment=RMM_HELPER_POLICY_FILE=/etc/rmm/general-helper-proof-v2.json\nEnvironment=RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock\nEnvironment=RMM_APT_HELPER_PROOF_FILE=/etc/rmm/apt-helper-proof-v2.json\nEnvironment=RMM_JOB_SIGNING_KEY_BOOTSTRAP=1\nEnvironment=RMM_EXTENDED_INVENTORY=1\n'
create('/etc/systemd/system/rmm-agent.service.d/20-runtime-v2.conf',dropin,0o644)
create('/etc/rmm/log-units-v2.json',b'["rmm-mvp-test.service"]\n',0o600,993,984)
create('/etc/systemd/system/rmm-agent.service.d/21-log-units-v2.conf',b'[Service]\nEnvironment=RMM_LOG_UNITS_FILE=/etc/rmm/log-units-v2.json\n',0o644)
for unit in ('rmm-agent.service','rmm-helper-dev-test.service','rmm-apt-helper.service'):
subprocess.run(['systemd-analyze','verify','/etc/systemd/system/'+unit],capture_output=True,check=True)
subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True)
report['agent_after']=manifest(state);report['helper_after']=manifest(helper_state)
require(before==report['agent_after'] and helper_before==report['helper_after'],'installation changed retained identity or SQLite bytes')
report.update(completed=True,stage='installed_not_started',journals_and_identity_unchanged=True)
except Exception as error:report['error']=sanitized_error(error);raise
finally:save();lock.close()
return report
def remote_probe():
os.umask(0o077)
config=Path('/etc/rmm/apt-helper.json');require(config.is_file(),'missing reviewed APT config')
before=manifest(Path('/var/lib/rmm-agent'))
report=dict(stage='preseal',package_effects=False,service_business_effects=False)
try:
# Explicit administrative fixture bootstrap: fixed known archives and
# private seals only. Never invoke apt/dpkg install or acceptance here.
command=['/usr/bin/python3','-I','/usr/lib/rmm-apt-transaction/apt_transaction.py']
preparation=subprocess.run(command+['provision-fixture-store'],capture_output=True,check=True)
report['fixture_preparation']=json.loads(preparation.stdout)
report['stage']='native_dedicated_unit_probe'
result=subprocess.run(['systemctl','start','rmm-apt-helper.service'],capture_output=True)
report['start_exit']=result.returncode
report['unit_state']=subprocess.run(['systemctl','show','rmm-apt-helper.service','--property=ActiveState,SubState,Result,ExecMainStatus'],capture_output=True,text=True,check=True).stdout.splitlines()
path=Path('/var/lib/rmm-apt-transaction/native-readiness.json')
report['native_readiness_present']=path.is_file()
if path.is_file():report['native_readiness']=json.loads(path.read_text())
require(result.returncode==0 and path.is_file(),'native probe failed; no job may proceed')
with closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True)) as db:
report['root_consumed_count']=db.execute('SELECT count(*) FROM consumed').fetchone()[0]
require(report['root_consumed_count']==0,'unexpected root consumption; do not stop/retry')
report['agent_state_unchanged']=manifest(Path('/var/lib/rmm-agent'))==before
require(report['agent_state_unchanged'],'probe changed agent state')
report['completed']=True
except Exception as error:report['error']=sanitized_error(error);raise
finally:
with closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True)) as db:
unsettled=db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]
if unsettled==0:
subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,check=True);report['helper_stopped']=True
path=Path('/etc/rmm/runtime-v2-probe-report.json')
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as stream:json.dump(report,stream,indent=2);stream.flush();os.fsync(stream.fileno())
return report
def program(invocation):
common='\n\n'.join(inspect.getsource(function) for function in (previous.manifest,previous.sanitized_error,sha,remote_upgrade,remote_probe))
return ('import base64,json,os,subprocess,sqlite3,uuid\nfrom pathlib import Path\nfrom contextlib import closing\nimport hashlib\n'
'def require(value,message):\n if not value: raise RuntimeError(message)\n'+common+'\ntry:\n print(json.dumps({"result":'+invocation+'}))\nexcept Exception as error:\n print(json.dumps({"error":sanitized_error(error)}))\n')
def remote(code):
with previous.connection() as (ssh,target,env):
result=subprocess.run(ssh+[target,'python3 -'],env=env,input=code.encode(),capture_output=True,timeout=180)
require(result.returncode==0,'SSH/bootstrap transport failed; private stderr withheld')
response=json.loads(result.stdout)
if 'error' in response:
print('RUNTIME_UPGRADE_ERROR='+json.dumps(response['error']));raise RuntimeError('remote phase failed; inspect private retained report')
return response['result']
def install():
sys.path.insert(0,str(ROOT/'tests'))
from backend_contract import request
from local_integration import STATE,A,sql
current=json.loads((BASE/'current.json').read_text());run=BASE/current['build_id'];receipt=json.loads((run/'build.json').read_text())
require(receipt['sources']==bound_sources(ROOT),'bound source changed since build')
agent_old=json.loads((STATE/'vm-jobs/bootstrap.json').read_text());root_old=json.loads((STATE/'vm-root/result.json').read_text())
agent=str(uuid.UUID(agent_old['agent_id']))
require(sql(f"SELECT count(*) FROM jobs WHERE agent_id='{agent}' AND state NOT IN ('succeeded','failed','expired','cancelled');")=='0','unresolved server jobs')
operator=(STATE/'operator-a.token').read_text().strip()
status,_,device=request(f'/api/v1/tenants/{A}/devices/{agent_old["asset_id"]}',token=operator)
require(status==200 and device['agent_id']==agent,'preserved server identity missing')
status,_,key=request('/api/v1/job-signing-key',token=operator)
require(status==200 and key['algorithm']=='Ed25519' and key['schema_version']==1 and sha(bytes.fromhex(key['public_key_hex']))==key['key_id'],'authenticated signing key invalid')
binding={field:device[field] for field in ('asset_id','agent_id','enrollment_generation','assignment_version')}
binding.update(tenant_id=A,public_key_hex=key['public_key_hex'])
general_old=(ROOT/'deploy/linux/rmm-helper-dev-test.service').read_bytes()
general=general_old.decode().replace('/etc/rmm/helper-dev-test-v1.json','/etc/rmm/helper-general-v2.json')
general=general.replace('SystemCallFilter=~@mount @reboot @swap @raw-io @module @clock @debug',
'SystemCallFilter=~@mount @reboot @swap @raw-io @module @clock @debug\nSystemCallFilter=adjtimex')
# General remains without SYS_ADMIN/MKNOD. Only a precreated empty target
# inode (not all /etc) may be written by a later authorized fixed setup job.
general=general.replace('ProtectSystem=strict','ProtectSystem=strict\nReadWritePaths=/etc/systemd/system/rmm-mvp-test.service')
old_agent_unit=(ROOT/'devstate/vm-jobs/docker-agent-unit-old.txt')
# Old unit was the exact pre-runtime unit deployed by the earlier bootstrap.
original_agent=(ROOT/'deploy/linux/rmm-agent.service').read_text().replace('Environment=RMM_TRANSPORT=auto\n','').replace('Environment=RMM_EXTENDED_INVENTORY=1\n','')
original_agent=original_agent.replace('Environment=RMM_CA_FILE=/etc/rmm/ca.pem\n','Environment=RMM_CA_FILE=/etc/rmm/ca.pem\nEnvironment=RMM_JOB_BINDING_FILE=/etc/rmm/agent-binding.json\n')
expected={'/usr/local/libexec/rmm-agent':agent_old['artifact_sha256'],
'/usr/local/libexec/rmm-privileged-helper':root_old['helper_sha256'],
'/usr/lib/rmm-apt-transaction/apt_transaction.py':root_old['tool_pins']['apt_transaction.py'],
'/usr/lib/rmm-apt-transaction/hook':root_old['tool_pins']['hook'],
'/etc/systemd/system/rmm-helper-dev-test.service':sha(general_old),
'/etc/systemd/system/rmm-agent.service':sha(original_agent.encode())}
payload=dict(build_id=run.name,binding=binding,key=key,expected_old=expected,
old_tool_pins=root_old['tool_pins'],replace_files=['/usr/local/libexec/rmm-agent','/usr/local/libexec/rmm-privileged-helper',
'/etc/systemd/system/rmm-helper-dev-test.service','/etc/systemd/system/rmm-agent.service'],
binaries={name:base64.b64encode((run/name).read_bytes()).decode() for name in ('rmm-agent','rmm-privileged-helper')},
artifact_hashes={name:value['sha256'] for name,value in receipt['artifacts'].items()},
tool_pins={name:receipt['sources']['tools/apt-transaction/'+name] for name in ('apt_transaction.py','hook')},
general_unit=general,agent_unit=(ROOT/'deploy/linux/rmm-agent.service').read_text())
result=remote(program('remote_upgrade('+repr(payload)+')'))
(run/'install.json').write_text(json.dumps(result,indent=2))
print('PASS versioned runtime upgrade; identities/journals byte-preserved; services not started')
if __name__=='__main__':
os.umask(0o077)
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('build','install','probe'))
args=parser.parse_args()
try:
if args.mode=='build':build()
elif args.mode=='install':install()
else:
current=json.loads((BASE/'current.json').read_text());run=BASE/current['build_id']
result=remote(program('remote_probe()'));(run/'probe.json').write_text(json.dumps(result,indent=2))
print('PASS actual dedicated-profile native probe; no package/RMM effects; helper stopped')
except Exception as error:
print('RUNTIME_PHASE_INCOMPLETE='+json.dumps(previous.sanitized_error(error)));raise SystemExit(2) from None