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
+51
View File
@@ -0,0 +1,51 @@
# Targeted local agent/helper checks
Actually executed on the development Linux host, not the target VM:
```sh
/home/Kevin/.cargo/bin/cargo test -p rmm-agent -p rmm-agent-linux -p rmm-privileged-helper
```
First-increment result: **24 unit tests passed** (4 agent, 12 Linux provider,
6 helper verifier/ledger/clock, 2 helper peer-ACL/policy). Tests live next to their private
implementation APIs. They cover SQLite reopen, exact duplicate/conflicting
offers, request reservation ambiguity, state/event rollback, cancellation/
expiry evidence, duplicate grants and attempts, unknown resource retention,
every permit/identity binding, forged signatures, payload tampering, clock
window endpoints, unsafe paths/corruption, peer UID mismatch, root execution
denial in the network agent, actual harmless script bytes/output/limits and
provider process-group cleanup. Unsafe service/package/reboot effects use
existing typed mocks; native APT mutation is specifically tested as denied.
Also actually passed for these packages: `cargo clippy --all-targets -- -D warnings`,
`cargo fmt -- --check`, and debug `cargo build -p rmm-agent -p rmm-privileged-helper`
(using the explicit `/home/Kevin/.cargo/bin/cargo` path). Initial Clippy errors in
two provider test slice clones, helper accept-loop style and agent nested-if
style were corrected, not suppressed.
This is not a real browser-approved WSS job, VM service, package or reboot
acceptance. The complete bootstrap and known hard blockers are in
`deploy/linux/README.md`. Do not run native host mutation to replace VM evidence.
## Second increment — actually executed locally
The targeted Rust command above now passes **34 tests** (10 agent, 15 Linux,
7 helper-library, 2 helper-binary). Added tests cover native observation wire
mapping/size/unsupported metadata, fresh live inventory time, typed log request
allowlist/window/limits including inward fractional-time rounding, future-schedule fairness, write-once signing-key pins,
actual loopback **mTLS** key bootstrap with DNS/SNI-preserving connect override,
wrong-server-name rejection without creating trust, and durable original boot/
awaiting-reboot evidence with retained resource locking.
Also actually passed:
```sh
python3 tests/agent_jobs/test_install.py
python3 -m py_compile deploy/linux/install.py tests/agent_jobs/test_install.py
```
Two create-only staging/guard tests passed; the installer was not run against
the host root or VM. An initial Python annotation incompatibility on the local
interpreter was corrected using postponed annotations; tests then passed.
Targeted Clippy with `-D warnings`, debug builds and Rust formatting checks also
passed. No native service/package/reboot effects, VM access or server restart.
+59
View File
@@ -0,0 +1,59 @@
import importlib.util
from pathlib import Path
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location("rmm_linux_install", ROOT / "deploy/linux/install.py")
assert SPEC is not None and SPEC.loader is not None
installer = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(installer)
class InstallationTests(unittest.TestCase):
def test_create_only_staging_and_no_service_activation(self):
with tempfile.TemporaryDirectory(prefix="rmm-package-test-") as temporary:
path = Path(temporary)
binary = path / "agent"
data = bytearray(64)
data[:6] = b"\x7fELF\x02\x01"
data[18:20] = b"\x3e\x00"
binary.write_bytes(data)
ca = path / "ca.pem"
ca.write_bytes(b"-----BEGIN CERTIFICATE-----\ntest public fixture\n-----END CERTIFICATE-----\n")
stage = path / "stage"
installer.install(stage, binary, ca, binary, False)
self.assertTrue((stage / "usr/local/libexec/rmm-agent").is_file())
self.assertFalse((stage / "var/lib/rmm-agent").exists())
self.assertFalse((stage / "etc/rmm/helper.json").exists())
self.assertFalse((stage / "etc/systemd/system/multi-user.target.wants").exists())
with self.assertRaises(ValueError):
installer.install(stage, binary, ca, binary, False)
with self.assertRaises(ValueError):
installer.install(Path("/"), binary, ca, None, False)
def test_rejects_private_keys_and_symlink_directories(self):
with tempfile.TemporaryDirectory(prefix="rmm-package-guard-") as temporary:
path = Path(temporary)
root = path / "root"
root.mkdir()
(root / "usr").symlink_to(path)
with self.assertRaises(ValueError):
installer.directory(root / "usr", 0o755)
artifact = path / "not-elf"
artifact.write_bytes(b"not an executable")
with self.assertRaises(ValueError):
installer.elf(artifact)
data = bytearray(64)
data[:6] = b"\x7fELF\x02\x01"
data[18:20] = b"\x3e\x00"
artifact.write_bytes(data)
ca = path / "private.pem"
ca.write_bytes(b"-----BEGIN CERTIFICATE-----\n-----BEGIN PRIVATE KEY-----\n")
with self.assertRaises(ValueError):
installer.install(path / "no-install", artifact, ca, None, False)
self.assertFalse((path / "no-install").exists())
if __name__ == "__main__":
unittest.main()
+264
View File
@@ -0,0 +1,264 @@
# Reconciliation client handoff — isolated module, not yet production wire-up
**Historical module-only handoff.** Production wire-up has since been implemented
in the agent dispatcher, helper control RPC and shared transport actor. Current
contract and verification boundaries: `deploy/linux/runtime-README.md`. The
earlier patch-position instructions below are retained as implementation history,
not an assertion that the modules are still unregistered.
Only new files were added for this dispatch:
`crates/agent/src/reconcile.rs`, `crates/agent/tests/reconcile.rs`, this document.
No existing agent/helper/backend/domain/protocol file or Cargo manifest was
changed. No VM, SSH, API approval, root effect or server restart was performed.
The test module uses the actual public protocol/domain contracts, OpenSSL
Ed25519 and real SQLite files, not substitute wire structs or an in-memory mock
journal. Build-directory locks were allowed to resolve normally.
## Public module API
* `ClientSync::new(Binding)` — process-local mutation gate starts closed. Existing
`safety('connected')` is never reset/deleted. The binding contains the already
provisioned administrative key, not a key received with a response.
* `Channel { connection_generation, boot_id }` — construct only after validating
the current authenticated ServerHello. Use the native Linux boot UUID, never
the agent instance/process UUID. Enrollment/assignment/tenant/asset/agent remain
bound by `Binding` and by every decoded local offer.
* `SyncClock::now()` and production `NativeClock::default()` — native synchronized
clock plus wall/monotonic jump checks. A false clock closes mutation authority;
it does not prevent independent passive telemetry.
* `initialize(&Connection)` — creates only additive `reconcile_*` tables in the
already locked, privately opened agent journal. Existing tables are required:
`counter`, `job_attempts`, `job_events`. The second connection is the existing
private `consumption.sqlite` with `consumed`/`execution_evidence`.
* `prepare(&mut journal, &consumption, Channel, Option<&HelperSnapshot>, &clock)`
returns `PreparedSync { envelope, request }`. It closes the old gate, checks
integrity/checkpoint/generation, archives original results, records ambiguous
requesting/running/verifying as unknown, reads real offers/consumption/boot/
outcomes/events and durably reserves the fresh request and sequence BEFORE
sending. Resend that exact envelope on the same live channel. After lost
response/reconnect, prepare a NEW request, using the durable prior request ID;
it does not invent a checkpoint or execution grant.
* `apply(&mut journal, &SyncResponse, &clock)` returns `Applied { decision_id,
acknowledged_message_ids, helper_instruction, reason }`. Verify exact signed
bytes before parsing, the separate reconciliation domain/profile, exact prost
request hash, every current identity/channel/boot/journal/checkpoint/key/time
binding, complete attempt coverage and exact receipt IDs. All agent changes,
result archives, ACK deletions and decision chain commit in one transaction.
Identical already-applied current responses are idempotent; stale/different
responses cannot open a gate. Terminal local state is never downgraded. Negative
server unknown decisions may carry an older server revision, but local revision
is retained with max(local, server), never reset and never made startable.
* `apply_consumption(&mut consumption, &clock)` applies only the agent process's
own targeted signed unlocks in an independent SQLite transaction. Recompare
grant/attempt/device and previous canonical local outcome, preserve consumed
tombstones, persist exact decision evidence, then allow the process gate to open.
Failure/crash between journal and consumption commits leaves mutations closed.
* `HelperInstruction` is the ORIGINAL `SyncResponse` plus its signed decision ID,
helper journal and checkpoint. Forward it unchanged to the authenticated helper.
It is never a new grant, permit, shell command or unsigned unlock boolean.
* `confirm_helper(&HelperSnapshot)` requires the independently authenticated root
helper to report that exact decision/checkpoint as durably applied and no
unsettled effects. `can_start(&Channel, root: bool, &clock)` stays false for
root until then, and expires with the signed decision deadline. Select `root`
from the verified immutable job identity, not from caller convenience.
* `close()` on every disconnect, new Hello, transport fencing or revocation.
* `archive_events(&Connection)` INSIDE the existing event-ACK transaction, before
deleting an ACKed event. This preserves original result envelope ID/sequence/
boot/time/raw outputs for future sync. No historical result is rewrapped with
a new sync envelope identity.
* `verify_response(...)` is the pure independently testable signature/binding
verifier; it does not apply or execute anything.
`HelperSnapshot { journal_id, checkpoint, previous_decision_id, attempts }` is a
complete snapshot obtained from the root-owned helper journal via authenticated
socket/peer credentials. It must NEVER be deserialized from a server response or
an unauthenticated local socket. Canonical helper outcome bytes can be imported
as a **new** durable agent JobResult with fresh event identity/revision (no new
job/attempt/effect). This resolves a lost helper response while retaining both
the older unknown result and the actual helper result. A consumed root attempt
omitted by the helper, changed helper journal/checkpoint, missing authoritative
server job/history, sequence rollback or generation regression remains blocked.
Sticky recovery faults cannot be cleared merely by omitting the offending entry.
## Minimal integration patch positions (after APT ownership release)
1. `main.rs` module declarations: add `mod reconcile;`. Agent fields: retain one
`ClientSync` and `NativeClock`, initialized from the pinned job Binding. A
passive agent without a binding must not advertise sync/root readiness.
2. `Agent::session`: advertise `rmm.sync.v1`; after validated ServerHello, before
any permit request, obtain the authenticated helper snapshot if configured,
call `prepare`, send its envelope. Both WSS and HTTPS singleton MessageBatch
carry the SAME envelope. No transport-specific readiness shortcut.
3. Inbound payload match: route current valid `Payload::SyncResponse` to `apply`;
then `apply_consumption`; forward any helper instruction unchanged, fetch the
helper's post-apply authenticated snapshot and call `confirm_helper`.
4. Replace the first-session `safe_session` condition in the scheduler AND the
immediately-before-effect dispatcher with `can_start`. Retain fresh independent
ExecutionPermit verification/consumption, immutable plan binding, normal
cancellation checks and provider resource locks. Sync never supplies start
authority or creates an executable job. Resync before the <=60s gate lease
expires, and call `close()` immediately when a channel fails/is fenced.
5. In `jobs::ack`, call `initialize` once during open and `archive_events` in the
same transaction as the concrete ACK's deletion. Without this hook, this module
still archives pending results during prepare, but cannot recover an original
event already erased by old code. Legacy final results can still reconcile
canonical hashes against authoritative stored server results.
6. Helper RPC needs typed authenticated snapshot/apply operations. Its new process
gate must start closed. Independently verify the original decision signature,
all helper identity/checkpoint/attempt/hash/action/boot/previous-outcome bindings
against ROOT-owned state, apply targeted unlocks transactionally, keep consumed
tombstones, then acknowledge its signed decision/checkpoint. An ordinary APT
probe/capability ready flag is NOT sufficient. This dispatch deliberately did
not modify the concurrently owned helper/transport files or claim this root
RPC consumer already wired. Never call agent `apply_consumption` on a helper's
root database as a substitute for independent helper verification.
The adapter refuses >256 attempts/receipts, >512 KiB canonical outcomes or a
>1 MiB request, rather than dropping unknown attempts/results. There is no unsafe
automatic compaction, journal deletion, new-grant retry or recovery-fault reset.
Coordinated rollback of every independent ledger still requires operator recovery.
## Targeted verification
Actually executed successfully during implementation:
Final result: **18 reconciliation integration tests passed**; targeted Clippy
with `-D warnings` and formatting check passed. No warnings were suppressed.
```sh
/home/Kevin/.cargo/bin/cargo test -p rmm-agent --test reconcile --locked
/home/Kevin/.cargo/bin/cargo clippy -p rmm-agent --test reconcile --locked -- -D warnings
/home/Kevin/.cargo/bin/rustfmt --edition 2024 --check crates/agent/src/reconcile.rs crates/agent/tests/reconcile.rs
```
Coverage includes real SQLite reopen, clean reconnect and accepted-unstarted jobs,
exact signatures/all identity bindings/fake key, replay and response-loss recovery,
original result retention and exact ACKs, canonical outcome rejection, unknown
resource retention, sticky missing server/helper records and checkpoint regression,
independent local outcome mismatch, actual helper-result import and original/new
boot evidence. Helper/server responses in these local tests are fixtures; no real
approval, helper root operation or backend/VM end-to-end acceptance is claimed.
## Independent helper consumer (separate ownership increment)
New files: `crates/privileged-helper/src/reconcile.rs` and
`crates/privileged-helper/tests/reconcile.rs`. Existing helper lib/main/APT and
agent files were deliberately NOT changed. This is a complete SQLite/verifier
core, **not yet registered in the production RPC dispatcher**.
### Helper API and locking
* `ExecutorGuard::acquire(state_directory, owner_uid)` opens the SAME existing
`executor.lock`. Production uses owner UID 0. `snapshot`, `apply` and `can_start`
verify the supplied connection is that directory's actual `consumption.sqlite`,
not a memory/private copy. Replace, rather than nest, existing per-request
`lock_state` acquisition. Hold this guard through grant consumption and effect.
* `HelperSync::new(independently_provisioned_Binding, &clock)` always starts closed.
The pin cannot be supplied in IPC or automatically replaced. The root-owned
journal persists a binding hash; changed key/identity requires controlled
recovery, not an in-band rebind.
* `snapshot(&mut db, &guard, generation, &clock, &boot)` closes the previous lease,
reads **every** actual consumed row plus saved Request, verifies the historical
exact Ed25519 grant at its original validity window, checks current bound
device/action/job/attempt/plan/authorization, and derives canonical outcomes,
original native boot and durable per-row revisions. It atomically reserves a
fresh monotone independent helper checkpoint. No agent-provided attempt list is
used to construct this root snapshot.
* `apply(&mut db, &guard, &SignedResponse, original_request_protobuf, &clock, &boot)`
independently verifies the exact reconciliation signature/domain/profile/key,
device and all current request/channel/journal/checkpoint/boot/time bindings.
It hashes the ORIGINAL canonical `SyncRequest::encode_to_vec()` bytes, not a
JSON rendering. All captured root attempts must appear in both request and
signed decisions; root grant/action/plan/previous canonical outcome must match.
The entire actual ledger fingerprint is rechecked while holding the same
executor guard and SQLite immediate transaction. Mutation after capture,
omissions, missing original evidence, unknown state, checkpoint/history rollback
and independently detected agent-journal replacement fail closed; recovery
faults are sticky and have no automatic reset API.
* Only explicit, independently matched signed per-attempt `unlock` decisions
update `consumed.result` and the saved outcome. The consumed grant/attempt stays
permanently present. Definitive known outcomes may be unlocked while another
unknown retains the global resource lock and readiness remains false. An unknown
script cannot become successful from a server-ready/callback boolean or a new
body; only the separately validated reboot reconciliation can supply a new
definitive body. Reboot additionally compares the helper's OWN current boot
source with the original persisted native boot and signed new-boot/inventory
evidence. Process instance UUIDs are not a boot source.
* Apply returns `Applied { snapshot, unlocked_grants, ready }`. Forward its
`snapshot` as the authenticated checkpoint ACK. It has the applied decision ID
and SAME checkpoint expected by `ClientSync::confirm_helper`. Do not call
`snapshot()` again to make the ACK: that creates a new challenge/checkpoint.
* `can_start(&db, &guard, generation, &clock, &boot)` rechecks process-local lease,
deadline, actual boot, persisted decision/checkpoint, complete ledger fingerprint
and absence of unresolved consumed resources immediately before a fresh grant.
`close()` removes the process lease. Native implementations are `NativeClock`
(synchronization/monotonic jump checks) and `NativeBoot` (Linux boot_id).
Gate admission is checked immediately before grant consumption while retaining
the executor guard through save_start, the existing final fresh-grant/clock check
and actual execution. After consumption the changed fingerprint intentionally
blocks **new** starts; it does not retroactively revoke the already admitted
locked operation. Do not retry admission after consuming the same grant or
release/reacquire the guard between admission and execution. ExecutorGuard is
movable but not shareable by reference across threads, preventing accidental
concurrent use of one flock guard by separate request handlers.
No protocol dependency or manifest change was added: the helper currently has no
direct prost/rmm-protocol dependency. Its private bounded parser reads the actual
existing SyncRequest tags 1–12 and SyncAttempt tags 1–13. Unknown/duplicate fields,
bad wire types, nonminimal/overflowing varints, truncation and size/count violations
are denied. Nested JobResult/Envelope never serve as independent root proof.
Production glue passes bytes from the real agent protocol type; shared-wire
schema changes must update this fail-closed decoder explicitly, not silently.
### Mapping and production patch positions
1. Register `mod reconcile;` in the helper **binary** `main.rs` (the standalone
module imports the package's existing library types). Construct one HelperSync
from the root-owned config/key and NativeClock; do not restore an open gate
from SQLite. Keep existing action policy, APT manifest/runtime validation,
artifact checks, privilege boundary and peer ACL intact.
2. Add typed snapshot/apply RPC variants after socket ACL/SO_PEERCRED validation.
Apply accepts original `decision_bytes`, `signature` and original canonical
SyncRequest bytes. It accepts NO external key or unlock/ready callback flag.
General and APT listeners must share the same root DB and executor guard.
3. Root `Snapshot` maps to agent `HelperSnapshot`:
journal_id/checkpoint/previous_decision_id unchanged; each LocalAttempt maps
UUIDs to strings and copies job_payload_hash, grant_id, consumption, state,
revision, original_boot_id, outcome_sha256 and canonical outcome_bytes.
Set optional result/result_envelope to None and result_message_id empty. The
agent core already imports actual helper outcomes as new durable agent results
when needed. Additional root-only plan/request hash fields remain available
for audit and independent checks.
4. Map agent `HelperInstruction.response` byte arrays directly to SignedResponse;
pass the prepared SyncRequest's original prost bytes alongside it. Send the
apply-returned authenticated snapshot to `ClientSync::confirm_helper`.
5. Add `HelperSync::can_start` checks under ExecutorGuard before **every** general
or APT grant consumption/effect path. Probe/capability flags do not replace
that check. Shared checkpoint state alone never opens another process's gate.
A fresh capture fences older process leases. Prefer one shared broker/gate for
both profiles; if they are separate processes, reconcile the selected process
before use rather than assuming both inherited readiness.
### Replay safety bound without a new protocol nonce
Each new process requires a fresh in-memory capture and a newly committed helper
checkpoint. Cached DB readiness/captures are not loaded as process authority.
Additionally the signed issued_at must be >= process birth AND the private fresh
capture time, and validity is at most 60 seconds. Thus an old cached response
cannot initialize a restarted helper, even while its old TTL has not expired;
ordinary same-process retries of the exact already-applied response are idempotent
and do not extend the lease. A local clock ahead of the signer may cause a safe
clock/reconciliation block: there is deliberately no skew grace that would admit
a pre-process response. This is a strict liveness limitation of the current
nonce-less contract, not an invented attestation or perpetual authorization.
Coordinated restore of all independent histories remains operator recovery.
The helper integration tests use actual SQLite files, real Ed25519 and the shared
executor lock, with fixture signatures and an injectable boot source for the
simulated positive reboot change. No native reboot/service/APT effect or root
deployment was performed. Targeted command:
```sh
/home/Kevin/.cargo/bin/cargo test -p rmm-privileged-helper --test reconcile --locked
```
+48
View File
@@ -0,0 +1,48 @@
# Native agent transport regressions
These are local Linux tests, **not VM/MVP acceptance**. They use ephemeral PKI,
strict server-name/CA verification and mandatory client certificates on loopback.
No production endpoint, VM, enrollment, job approval or database is touched.
The OpenSSL fixture must set a session-id context for client authentication.
On this host (OpenSSL 1.1.1w), the isolated replay regression originally failed
on reconnect with `ssl_get_prev_session`, code `336433429`, reason
`session id context uninitialized`; the client received TLS `InternalError`.
Disabling the session cache and tickets did not provide that context. The fix
sets a fixture-specific session context and restores normal caching/tickets;
it changes neither production TLS nor certificate verification. Handshake
diagnostics print only SSL codes and static library/function/reason metadata.
Locked versions: openssl 0.10.82 / openssl-sys 0.9.118; reqwest 0.12.28 uses
rustls 0.23.46 with ring 0.17.14 (workspace disables reqwest default TLS).
The replay test asserts missing response returns an error without a fabricated
ACK, repeats the exact encoded batch/message ID, and retains one deduplicated
application record. No execution retry is introduced. Rotation retains its
three identity/crash/recovery tests. Its harness also runs the four HTTPS
contract tests because it compiles and uses that same transport implementation;
rotation-only fixtures live in the rotation test, without dead-code allowances.
Reproduce locally (commands, not a VM acceptance recipe):
```sh
OPENSSL_CONF=/dev/null cargo test -p rmm-agent --test https_channel --locked real_mtls_hello_poll_durable_replay_and_fencing -- --exact --nocapture
cargo test -p rmm-agent --test https_channel --test certificate_rotation --locked
cargo clippy -p rmm-agent --test https_channel --test certificate_rotation --locked -- -D warnings
```
`OPENSSL_CONF=/dev/null` isolates only the test process from host OpenSSL config;
it does not disable TLS, trust, name verification or mandatory mTLS.
## Observed local verification (2026-11-12)
- Isolated pre-fix replay: failed with the server reason/code above and client
`received fatal alert: InternalError` on the second explicit send.
- After the context fix: isolated pair of targets passed (7 + 4 tests).
- Five additional consecutive native runs with the normal environment passed
(7 + 4 each; 55 test executions, seven distinct cases because the HTTPS
contract is deliberately included in the rotation harness).
- Targeted `cargo clippy ... -- -D warnings` passed; file-scoped rustfmt check
passed. No dead-code/lint suppression was added.
- VM execution, production backend integration and production wiring were not
performed by this dispatch; Linux-MVP acceptance remains outside this result.
+270
View File
@@ -0,0 +1,270 @@
use openssl::{
asn1::Asn1Time,
bn::{BigNum, MsbOption},
ec::{EcGroup, EcKey},
hash::MessageDigest,
nid::Nid,
pkey::{PKey, Private},
ssl::{SslAcceptor, SslMethod, SslVerifyMode},
x509::{
X509, X509NameBuilder,
extension::{BasicConstraints, ExtendedKeyUsage, KeyUsage, SubjectAlternativeName},
},
};
use std::{
io::{Read, Write},
net::{SocketAddr, TcpListener},
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
thread,
time::Duration,
};
pub fn key() -> PKey<Private> {
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
PKey::from_ec_key(EcKey::generate(&group).unwrap()).unwrap()
}
pub struct Pki {
pub ca: X509,
pub ca_key: PKey<Private>,
}
impl Pki {
pub fn new() -> Self {
let ca_key = key();
let mut name = X509NameBuilder::new().unwrap();
name.append_entry_by_text("CN", "Ephemeral transport test CA")
.unwrap();
let name = name.build();
let mut b = X509::builder().unwrap();
b.set_version(2).unwrap();
let mut serial = BigNum::new().unwrap();
serial.rand(128, MsbOption::MAYBE_ZERO, false).unwrap();
b.set_serial_number(&serial.to_asn1_integer().unwrap())
.unwrap();
b.set_subject_name(&name).unwrap();
b.set_issuer_name(&name).unwrap();
b.set_pubkey(&ca_key).unwrap();
let now = chrono::Utc::now().timestamp();
b.set_not_before(&Asn1Time::from_unix(now - 60 * 86400).unwrap())
.unwrap();
b.set_not_after(&Asn1Time::from_unix(now + 365 * 86400).unwrap())
.unwrap();
b.append_extension(BasicConstraints::new().critical().ca().build().unwrap())
.unwrap();
b.append_extension(
KeyUsage::new()
.critical()
.key_cert_sign()
.crl_sign()
.build()
.unwrap(),
)
.unwrap();
b.sign(&ca_key, MessageDigest::sha256()).unwrap();
Self {
ca: b.build(),
ca_key,
}
}
pub fn issue(
&self,
public: &openssl::pkey::PKeyRef<impl openssl::pkey::HasPublic>,
cn: &str,
server: bool,
from: i64,
until: i64,
) -> X509 {
let mut name = X509NameBuilder::new().unwrap();
name.append_entry_by_text("CN", cn).unwrap();
let mut b = X509::builder().unwrap();
b.set_version(2).unwrap();
let mut serial = BigNum::new().unwrap();
serial.rand(128, MsbOption::MAYBE_ZERO, false).unwrap();
b.set_serial_number(&serial.to_asn1_integer().unwrap())
.unwrap();
b.set_subject_name(&name.build()).unwrap();
b.set_issuer_name(self.ca.subject_name()).unwrap();
b.set_pubkey(public).unwrap();
b.set_not_before(&Asn1Time::from_unix(from).unwrap())
.unwrap();
b.set_not_after(&Asn1Time::from_unix(until).unwrap())
.unwrap();
b.append_extension(BasicConstraints::new().critical().build().unwrap())
.unwrap();
b.append_extension(
KeyUsage::new()
.critical()
.digital_signature()
.build()
.unwrap(),
)
.unwrap();
let mut eku = ExtendedKeyUsage::new();
if server {
eku.server_auth();
} else {
eku.client_auth();
}
b.append_extension(eku.build().unwrap()).unwrap();
if server {
let san = SubjectAlternativeName::new()
.dns(cn)
.build(&b.x509v3_context(Some(&self.ca), None))
.unwrap();
b.append_extension(san).unwrap();
}
b.sign(&self.ca_key, MessageDigest::sha256()).unwrap();
b.build()
}
}
pub struct Request {
pub path: String,
pub body: Vec<u8>,
pub content_type: String,
}
pub struct Response {
pub status: u16,
pub media: &'static str,
pub body: Vec<u8>,
}
impl Response {
pub fn protobuf(body: Vec<u8>) -> Self {
Self {
status: 200,
media: "application/x-protobuf",
body,
}
}
}
pub struct Server {
pub address: SocketAddr,
stop: Arc<AtomicBool>,
worker: Option<thread::JoinHandle<()>>,
}
impl Server {
pub fn start(
pki: &Pki,
dns: &str,
handler: impl Fn(Request) -> Option<Response> + Send + 'static,
) -> Self {
let server_key = key();
let now = chrono::Utc::now().timestamp();
let cert = pki.issue(&server_key, dns, true, now - 60, now + 86400);
let mut tls = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
// OpenSSL requires a session-id context when verifying client certificates,
// including when a reconnect offers a previous session. Cache/ticket
// disabling alone does not satisfy ssl_get_prev_session on OpenSSL 1.1.1.
tls.set_session_id_context(b"agent-transport-test").unwrap();
tls.set_private_key(&server_key).unwrap();
tls.set_certificate(&cert).unwrap();
tls.cert_store_mut().add_cert(pki.ca.clone()).unwrap();
tls.set_verify(SslVerifyMode::PEER | SslVerifyMode::FAIL_IF_NO_PEER_CERT);
let tls = tls.build();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
listener.set_nonblocking(true).unwrap();
let stop = Arc::new(AtomicBool::new(false));
let stopped = stop.clone();
let worker = thread::spawn(move || {
while !stopped.load(Ordering::SeqCst) {
let stream = match listener.accept() {
Ok((stream, _)) => stream,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(5));
continue;
}
Err(_) => break,
};
stream
.set_read_timeout(Some(Duration::from_secs(3)))
.unwrap();
stream
.set_write_timeout(Some(Duration::from_secs(3)))
.unwrap();
let mut stream = match tls.accept(stream) {
Ok(stream) => stream,
Err(openssl::ssl::HandshakeError::Failure(stream)) => {
let error = stream.error();
eprintln!("TEST_TLS_ACCEPT code={:?}", error.code());
if let Some(stack) = error.ssl_error() {
for frame in stack.errors() {
// Static library metadata only: never print error data,
// peer names, certificates, keys or request contents.
eprintln!(
"TEST_TLS_FRAME code={} library={:?} function={:?} reason={:?}",
frame.code(),
frame.library(),
frame.function(),
frame.reason()
);
}
}
continue;
}
Err(_) => {
eprintln!("TEST_TLS_ACCEPT setup_or_would_block");
continue;
}
};
let mut header = Vec::new();
while !header.ends_with(b"\r\n\r\n") && header.len() < 16384 {
let mut byte = [0];
if stream.read_exact(&mut byte).is_err() {
break;
}
header.push(byte[0]);
}
let header = String::from_utf8_lossy(&header);
let path = header
.split_whitespace()
.nth(1)
.unwrap_or_default()
.to_owned();
let mut length = 0;
let mut content_type = String::new();
for line in header.lines().skip(1) {
if let Some((name, value)) = line.split_once(':') {
if name.eq_ignore_ascii_case("content-length") {
length = value.trim().parse::<usize>().unwrap();
}
if name.eq_ignore_ascii_case("content-type") {
content_type = value.trim().into();
}
}
}
assert!(length <= 1024 * 1024);
let mut body = vec![0; length];
if stream.read_exact(&mut body).is_err() {
continue;
}
if let Some(response) = handler(Request {
path,
body,
content_type,
}) {
let header = format!(
"HTTP/1.1 {} Test\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
response.status,
response.media,
response.body.len()
);
let _ = stream.write_all(header.as_bytes());
let _ = stream.write_all(&response.body);
let _ = stream.flush();
}
}
});
Self {
address,
stop,
worker: Some(worker),
}
}
}
impl Drop for Server {
fn drop(&mut self) {
self.stop.store(true, Ordering::SeqCst);
self.worker.take().unwrap().join().unwrap();
}
}
@@ -0,0 +1,10 @@
VERSION 3
DPkg::Pre-Install-Pkgs::=/usr/lib/rmm-apt-transaction/hook
DPkg::Tools::Options::/usr/lib/rmm-apt-transaction/hook::Version=3
DPkg::Tools::Options::/usr/lib/rmm-apt-transaction/hook::InfoFD=0
Dir::Bin::dpkg=/usr/bin/dpkg
Dir::State::status=/var/lib/dpkg/status
DPkg::ConfigurePending=false
rmm-mvp-test - - none < 1.1 all none /var/lib/rmm-apt-transaction/archives/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.deb
rmm-mvp-test - - none < 1.1 all none **CONFIGURE**
@@ -0,0 +1 @@
{"apt_config_sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","binaries":{"/usr/bin/dpkg":{"sha256":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}},"schema_version":1,"versions":{"dpkg":"Debian dpkg version 1.22"}}
+308
View File
@@ -0,0 +1,308 @@
import hashlib
import importlib.util
import io
import json
import os
from pathlib import Path
import stat
import subprocess
import sys
import tarfile
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location('apt_transaction', ROOT / 'tools/apt-transaction/apt_transaction.py')
apt = importlib.util.module_from_spec(SPEC)
sys.modules[SPEC.name] = apt
SPEC.loader.exec_module(apt)
def transaction(old=None):
sha = 'a' * 64
return {'schema_version': 1, 'recipe_id': 'rmm-mvp-test', 'recipe_version': '1',
'platform': 'ubuntu-24.04-amd64', 'resolver_config_version': apt.RESOLVER,
'no_reboot': True, 'status_fingerprint': 'b' * 64, 'baseline_sha256': 'c' * 64,
'packages': [{'name': 'rmm-mvp-test', 'architecture': 'all', 'old_version': old,
'new_version': '1.1', 'origin': 'rmm-test', 'sha256': sha, 'size': 100,
'archive_path': str(apt.BASE / 'archives' / (sha + '.deb'))}]}
def protocol(old=None):
old_fields = old + ' all none' if old else '- - none'
action = 'rmm-mvp-test ' + old_fields + ' < 1.1 all none '
return ('VERSION 3\nDPkg::Pre-Install-Pkgs::=' + apt.HOOK + '\n'
'DPkg::Tools::Options::' + apt.HOOK + '::Version=3\n'
'DPkg::Tools::Options::' + apt.HOOK + '::InfoFD=0\n'
'Dir::Bin::dpkg=/usr/bin/dpkg\nDir::State::status=/var/lib/dpkg/status\n'
'DPkg::ConfigurePending=false\n\n' + action + transaction()['packages'][0]['archive_path'] +
'\n' + action + '**CONFIGURE**\n').encode('ascii')
class ProtocolTests(unittest.TestCase):
def test_upstream_v3_new_install_and_update(self):
for old in (None, '1.0'):
tx = apt.ResolvedAptTransaction.parse(transaction(old))
config, actions = apt.parse_protocol3(protocol(old))
apt.check_hook_config(config)
apt.validate_actions(tx, actions)
def test_fixture_file(self):
data = (Path(__file__).parent / 'fixtures/install-v3.txt').read_bytes()
config, actions = apt.parse_protocol3(data)
apt.check_hook_config(config)
apt.validate_actions(apt.ResolvedAptTransaction.parse(transaction()), actions)
def test_not_five_field_or_fantasy_hash_protocol(self):
for data in (b'VERSION 2\n\npkg - < 1 /file\n',
b'VERSION 3\n\npkg - < 1 /file sha256=aaa\n'):
with self.assertRaises(apt.Veto):
apt.parse_protocol3(data)
def test_strict_malformed_and_bounds(self):
original = protocol()
for data in (b'', original[:-1], original.replace(b'VERSION 3', b'VERSION 4'),
original.replace(b'all none', b'all nonsense'),
original.replace(b'\n\n', b'\n'), original + b'\n',
original.replace(b'rmm-mvp-test ', b'rmm-mvp-test '),
original.replace(b'InfoFD=0', b'InfoFD=%GG'),
original.replace(b'InfoFD=0', b'InfoFD=0\x00'), b'x' * (apt.MAX_INPUT + 1)):
with self.subTest(data=data[:80]), self.assertRaises(apt.Veto):
apt.parse_protocol3(data)
def test_config_escaping_repeated_list_and_invalid_hook_binding(self):
config, _ = apt.parse_protocol3(protocol().replace(b'VERSION 3\n',
b'VERSION 3\nExample::=a%0Ab%25c\nExample::=two\n'))
self.assertIn(('Example::', 'a\nb%c'), config)
self.assertIn(('Example::', 'two'), config)
for injected in (b'DPkg::Pre-Invoke::=/bin/true\n',
('DPkg::Pre-Install-Pkgs::=' + apt.HOOK + '\n').encode()):
config, _ = apt.parse_protocol3(protocol().replace(b'\n\n', b'\n' + injected + b'\n'))
with self.assertRaises(apt.Veto):
apt.check_hook_config(config)
def test_entire_action_set_dependency_change_remove_configure(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
original = protocol()
for data in (original.replace(b'**CONFIGURE**', b'**REMOVE**'),
original.replace(b'rmm-mvp-test -', b'other-dependency -'),
original.replace(b'1.1 all', b'1.2 all'),
original.replace(b'1.1 all', b'1.1 amd64'),
original.replace(b' < ', b' > '),
original.replace(b'a' * 64 + b'.deb', b'b' * 64 + b'.deb'),
original + original.split(b'\n')[-2] + b'\n',
b'\n'.join(original.split(b'\n')[:-2]) + b'\n'):
with self.subTest(data=data[-100:]), self.assertRaises(apt.Veto):
_, actions = apt.parse_protocol3(data)
apt.validate_actions(tx, actions)
def test_downgrade_and_version_order(self):
for old in ('1.1', '1.2', '1:1.0'):
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(transaction(old))
apt.ResolvedAptTransaction.parse(transaction('1.1~rc1'))
def test_schema_no_removal_unknown_fields_and_no_reboot(self):
for key, value in (('no_reboot', False), ('packages', []),
('schema_version', True), ('platform', 'debian'),
('resolver_config_version', 'anything')):
obj = transaction()
obj[key] = value
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
for key, value in (('new_version', None), ('name', 'linux-image-test'),
('archive_path', '/tmp/evil.deb'), ('origin', 'https://llm.invalid/x'),
('sha256', 'z' * 64), ('size', True), ('remove', True)):
obj = transaction()
obj['packages'][0][key] = value
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
obj = transaction()
obj['packages'] *= 2
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
def test_immutable_and_duplicate_json(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
with self.assertRaises(AttributeError):
tx.packages[0].new_version = '2'
with self.assertRaises(apt.Veto):
apt.json_bytes(b'{"a":1,"a":2}')
class PreconditionsTests(unittest.TestCase):
def test_both_deadlines_and_boot_bound(self):
context = apt.VerifiedHelperContext.parse({
'transaction_sha256': 'a' * 64, 'effect_armed_ledger_id': 'ledger-1',
'grant_id': 'grant-1', 'not_before_ms': 1000, 'start_before_ms': 61000,
'boot_id': 'boot-1', 'monotonic_start_before_ns': 10000,
'schema_version': 2, 'job_payload_sha256': 'd' * 64, 'action': 'apt-execute',
'execution_profile': 'dev-test', 'job_id': 'job-1', 'attempt_id': 'attempt-1',
'authorization_id': 'authorization-1', 'plan_sha256': 'e' * 64})
context.check_deadline(1000, 1, 'boot-1')
for now, mono, boot in ((999, 1, 'boot-1'), (61000, 1, 'boot-1'),
(2000, 10000, 'boot-1'), (2000, 1, 'boot-2')):
with self.assertRaises(apt.Veto):
context.check_deadline(now, mono, boot)
def test_fingerprint_baseline_and_old_version(self):
baseline = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'}}
obj = transaction('1.0')
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
tx = apt.ResolvedAptTransaction.parse(obj)
with patch.object(apt, 'status_fingerprint', return_value=tx.status_fingerprint), \
patch.object(apt, 'installed_snapshot', return_value=baseline):
apt.check_preconditions(tx, baseline)
with self.assertRaises(apt.Veto):
apt.check_preconditions(tx, {})
with patch.object(apt, 'status_fingerprint', return_value='d' * 64):
with self.assertRaisesRegex(apt.Veto, 'STATUS_FINGERPRINT'):
apt.check_preconditions(tx, baseline)
with patch.object(apt, 'status_fingerprint', return_value=tx.status_fingerprint), \
patch.object(apt, 'installed_snapshot', return_value={}):
with self.assertRaisesRegex(apt.Veto, 'STATUS_BASELINE_CHANGED'):
apt.check_preconditions(tx, baseline)
def test_durable_once_double_hook(self):
with tempfile.TemporaryDirectory() as temp, patch.object(apt, 'secure_path'):
marker = Path(temp) / 'armed.json'
with patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
apt.durable_once(marker, {'ledger': 'one'})
self.assertEqual(sync.call_count, 2)
with self.assertRaisesRegex(apt.Veto, 'ALREADY_HANDED_OFF'):
apt.durable_once(marker, {'ledger': 'two'})
self.assertEqual(json.loads(marker.read_bytes()), {'ledger': 'one'})
def test_archive_root_mode_size_links(self):
valid = dict(st_uid=0, st_mode=stat.S_IFREG | 0o600, st_size=100, st_nlink=1)
apt.archive_metadata(SimpleNamespace(**valid), 100)
for key, value in (('st_uid', 1000), ('st_mode', stat.S_IFREG | 0o620),
('st_mode', stat.S_IFLNK | 0o777), ('st_size', 99), ('st_nlink', 2)):
with self.assertRaises(apt.Veto):
apt.archive_metadata(SimpleNamespace(**{**valid, key: value}), 100)
def test_symlink_and_user_owned_path_rejected(self):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / 'file'
path.write_bytes(b'file')
link = Path(temp) / 'link'
link.symlink_to(path)
with self.assertRaises(apt.Veto):
apt.secure_path(link)
if os.getuid() != 0:
with self.assertRaises(apt.Veto):
apt.secure_path(path)
def test_postcondition_checks_entire_snapshot(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
expected = {'rmm-mvp-test:all': {'version': '1.1', 'status': 'install ok installed'}}
with patch.object(apt, 'installed_snapshot', return_value=expected), \
patch.object(apt, 'run_read', return_value=b''):
self.assertEqual(apt.verify_after(tx, {})['packages'][0]['version'], '1.1')
for snapshot in ({}, {**expected, 'unexpected:all': {'version': '1', 'status': 'install ok installed'}}):
with patch.object(apt, 'installed_snapshot', return_value=snapshot):
with self.assertRaises(apt.Veto):
apt.verify_after(tx, {})
def test_unprivileged_execute_and_sandbox_fail_closed(self):
if os.geteuid() != 0:
self.assertEqual(apt.execute({})['code'], 'ROOT_REQUIRED')
with self.assertRaises(apt.Veto):
apt.inspect_sandbox()
def test_previous_launch_or_handoff_is_unknown_never_relaunched(self):
for marker in ('launch.json', 'hook-armed.json'):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / marker).write_text('{}')
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', base), \
patch.object(apt, 'secure_path'), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'load_active', return_value=(None, None, {})), \
patch.object(apt.subprocess, 'Popen') as spawn:
result = apt.execute({'transaction_sha256': 'a' * 64,
'effect_armed_ledger_id': 'ledger-1'})
self.assertEqual(result['effect_state'], 'unknown')
self.assertEqual(result['code'], 'PRIOR_ATTEMPT_UNKNOWN')
spawn.assert_not_called()
def test_unprivileged_seccomp_load_in_disposable_child(self):
# Installs a filter only on this short-lived child, never the test
# runner/host. No reboot, mount or other effectful syscall is attempted.
code = '''import importlib.util, pathlib, sys
sys.dont_write_bytecode = True
s = importlib.util.spec_from_file_location('apt_transaction', sys.argv[1])
m = importlib.util.module_from_spec(s); sys.modules[s.name] = m; s.loader.exec_module(m)
m.seccomp_filter()
status = pathlib.Path('/proc/self/status').read_text()
assert 'NoNewPrivs:\\t1' in status and 'Seccomp:\\t2' in status
print('FILTER_LOADED')
'''
output = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(output, b'FILTER_LOADED\n')
def test_exact_argv_config_and_fixed_hook(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
argv = apt.apt_argv(tx)
self.assertIn('--no-download', argv)
self.assertIn('--no-remove', argv)
self.assertNotIn('update', argv)
self.assertEqual(argv[-1], tx.packages[0].archive_path)
self.assertEqual((ROOT / 'tools/apt-transaction/hook').read_bytes(), apt.hook_script())
self.assertIn(b'Dir::Etc::parts', apt.apt_config())
class FixtureTests(unittest.TestCase):
def test_actual_archive_hash_and_control_mismatch(self):
with tempfile.TemporaryDirectory() as temp:
records = json.loads(subprocess.check_output([
sys.executable, str(ROOT / 'tools/apt-transaction/build_fixture.py'), temp]))
record = records[0]
package = apt.PackageArchive('rmm-mvp-test', 'all', None, '1.0', 'rmm-test',
record['sha256'], record['size'], record['local_build_path'])
# Only root ownership is mocked for this unprivileged byte/control
# test. Production secure_path and archive_metadata are never relaxed.
with patch.object(apt, 'secure_path', side_effect=lambda path: Path(path)), \
patch.object(apt, 'archive_metadata'):
apt.verify_archive(package)
wrong_hash = apt.PackageArchive(**{**package.__dict__, 'sha256': '0' * 64})
with self.assertRaisesRegex(apt.Veto, 'ARCHIVE_HASH'):
apt.verify_archive(wrong_hash)
wrong_version = apt.PackageArchive(**{**package.__dict__, 'new_version': '2.0'})
with self.assertRaisesRegex(apt.Veto, 'DEB_CONTROL_OR_CONFLICT'):
apt.verify_archive(wrong_version)
def test_build_deb_versions_hash_control_payload_without_install(self):
with tempfile.TemporaryDirectory() as temp:
records = json.loads(subprocess.check_output([
sys.executable, str(ROOT / 'tools/apt-transaction/build_fixture.py'), temp]))
self.assertEqual([r['new_version'] for r in records], ['1.0', '1.1'])
for record in records:
path = Path(record['local_build_path'])
self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), record['sha256'])
self.assertEqual(path.stat().st_size, record['size'])
control = apt.run_read(['/usr/bin/dpkg-deb', '--show',
'--showformat=${Package}\n${Version}\n${Architecture}\n', str(path)])
self.assertEqual(control.decode().splitlines(), ['rmm-mvp-test', record['new_version'], 'all'])
payload = apt.run_read(['/usr/bin/dpkg-deb', '--fsys-tarfile', str(path)])
apt.validate_payload(payload)
with tarfile.open(fileobj=io.BytesIO(payload)) as archive:
self.assertEqual(archive.extractfile('./usr/share/rmm-mvp-test/version').read(),
(record['new_version'] + '\n').encode())
def test_protected_payload_paths_even_in_innocent_package(self):
for name in ('./boot/vmlinuz', './etc/cron.d/reboot', './usr/lib/systemd/system/a.service',
'../escape', '/absolute', './var/lib/rmm-apt-transaction/active/context.json'):
output = io.BytesIO()
with tarfile.open(fileobj=output, mode='w') as archive:
entry = tarfile.TarInfo(name)
entry.size = 1
archive.addfile(entry, io.BytesIO(b'x'))
with self.assertRaises(apt.Veto):
apt.validate_payload(output.getvalue())
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,87 @@
"""Regression: slow durable handoff must not extend a native startgrant."""
import io
import os
from pathlib import Path
import sys
import tempfile
from contextlib import ExitStack
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import Mock, patch
from test_guard import apt, protocol, transaction
from test_native_acceptance import context
class HookDeadlineTests(TestCase):
def test_deadline_after_fsync_vetoes_keeps_marker_and_prevents_reuse(self):
# Test both clocks together and each independently: neither clock can
# compensate for expiry of the other at the post-fsync gate.
for expired in ('both', 'utc', 'monotonic'):
with self.subTest(expired=expired), tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
active = Path(temp)
ctx = context('apt-execute')
tx = apt.ResolvedAptTransaction.parse(transaction())
clock = {'utc_ns': 2000 * 1000000, 'monotonic_ns': 100}
ready = apt.canonical({'transaction_sha256': ctx.transaction_sha256,
'sandbox_version': 1})
original_fsync = os.fsync
sync_count = 0
def slow_fsync(fd):
nonlocal sync_count
original_fsync(fd)
sync_count += 1
if sync_count == 2: # directory fsync finished successfully
if expired in ('both', 'utc'):
clock['utc_ns'] = ctx.start_before_ms * 1000000
if expired in ('both', 'monotonic'):
clock['monotonic_ns'] = ctx.monotonic_start_before_ns
dpkg_start = Mock()
def apt_handoff():
# Models the CLI's Veto -> nonzero contract and APT's
# mandatory zero-only gate. No real APT/dpkg is started.
try:
code = apt.main()
except apt.Veto as error:
return 100, str(error)
if code == 0:
dpkg_start()
return code, None
with ExitStack() as stack:
for replacement in (
patch.object(apt, 'ACTIVE', active),
patch.object(apt.os, 'geteuid', return_value=0),
patch.dict(os.environ, {'APT_HOOK_INFO_FD': '0', 'DPKG_FRONTEND_LOCKED': 'true'}),
patch.object(apt, 'load_active', return_value=(tx, ctx, {})),
patch.object(apt, 'load_signed_action', return_value={}),
patch.object(apt, 'runtime_identity', return_value={}),
patch.object(apt, 'check_native_gate'), patch.object(apt, 'inspect_sandbox'),
patch.object(apt, 'sealed_read', return_value=ready),
patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'),
patch.object(apt, 'secure_path'),
patch.object(Path, 'read_text', return_value=ctx.boot_id),
patch.object(apt.time, 'time_ns', side_effect=lambda: clock['utc_ns']),
patch.object(apt.time, 'monotonic_ns', side_effect=lambda: clock['monotonic_ns']),
patch.object(apt.os, 'fsync', side_effect=slow_fsync),
patch.object(sys, 'argv', ['apt_transaction.py', 'hook']),
patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))),
):
stack.enter_context(replacement)
self.assertEqual(apt_handoff(), (100, 'START_DEADLINE'))
self.assertEqual(sync_count, 2)
marker = active / 'hook-armed.json'
self.assertTrue(marker.is_file())
marker_bytes = marker.read_bytes()
dpkg_start.assert_not_called()
# Even a subsequently valid clock cannot reuse the durable
# marker: no reset/delete/retry shortcut exists.
clock.update(utc_ns=2000 * 1000000, monotonic_ns=100)
with patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))):
self.assertEqual(apt_handoff(), (100, 'ALREADY_HANDED_OFF'))
self.assertEqual(marker.read_bytes(), marker_bytes)
self.assertEqual(sync_count, 2)
dpkg_start.assert_not_called()
@@ -0,0 +1,387 @@
"""No host package mutation: native root/child execution is mocked explicitly."""
import copy
import io
import json
import os
from pathlib import Path
import stat
import subprocess
import sys
import tarfile
import tempfile
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, transaction, ROOT
def fixture_transaction(recipe='rmm-apt-fixture-install-1.0'):
old, new = apt.FIXTURE_RECIPES[recipe]
obj = transaction(old)
obj['recipe_id'] = recipe
obj['packages'][0].update(new_version=new, sha256=apt.FIXTURE_SHA256[new],
size=len(apt.fixture_archive(new)),
archive_path=str(apt.BASE / 'archives' / (apt.FIXTURE_SHA256[new] + '.deb')))
return obj
def context(action='apt-native-acceptance', profile='dev-test'):
return apt.VerifiedHelperContext.parse({
'schema_version': 2, 'transaction_sha256': 'a' * 64,
'effect_armed_ledger_id': 'ledger-1', 'grant_id': 'grant-1',
'not_before_ms': 1000, 'start_before_ms': 61000, 'boot_id': 'boot-1',
'monotonic_start_before_ns': 10000, 'job_payload_sha256': 'b' * 64,
'action': action, 'execution_profile': profile, 'job_id': 'job-1',
'attempt_id': 'attempt-1', 'authorization_id': 'approval-1', 'plan_sha256': 'c' * 64})
def action_manifest(ctx, obj, runtime):
return {'type': 'rmm.apt.helper-action', 'schema_version': 2,
'action': ctx.action, 'execution_profile': ctx.execution_profile,
'transaction_sha256': ctx.transaction_sha256, 'resolved_apt_transaction': obj,
'transaction_bytes': list(apt.canonical(obj)),
'provider_sha256': apt.digest(b'code'), 'hook_sha256': apt.digest(apt.hook_script()),
'apt_config_sha256': apt.digest(apt.apt_config()),
'runtime_sha256': apt.digest(apt.canonical(runtime)), 'test_result_id': 'native-test-1'}
def signed_job(ctx, manifest):
tx = manifest['resolved_apt_transaction']
return {'schema_version': 1, 'tenant_id': 'tenant-1', 'asset_id': 'asset-1',
'agent_id': 'agent-1', 'enrollment_generation': 1, 'assignment_version': 1,
'job_id': ctx.job_id, 'attempt_id': ctx.attempt_id, 'execution_identity': 'root',
'not_before': '2026-11-12T12:00:00Z', 'start_before': '2026-11-12T12:01:00Z',
'max_runtime_seconds': 300, 'max_output_bytes': 4096,
'action': {'action': 'apt_native_acceptance' if ctx.action == 'apt-native-acceptance' else 'ensure_package',
'recipe_id': tx['recipe_id'], 'recipe_version': tx['recipe_version'],
'apt_manifest': manifest}}
def prechecks(ctx):
return {'schema_version': 1, 'transaction_sha256': ctx.transaction_sha256,
'job_payload_sha256': ctx.job_payload_sha256, 'boot_id': ctx.boot_id,
'checks': {'namespaces_caps_nnp_seccomp': True, 'readonly_controlpaths': True,
'private_tmpfs_sockets': True,
'control_syscalls': {'invalid_reboot_magic': 'EPERM',
'invalid_kexec_load_flags': 'EPERM',
'invalid_kexec_file_fds_flags': 'EPERM'},
'frontend_lock': 'native-apt-check-conflict'}}
class BindingTests(TestCase):
def test_pinned_fixture_is_reproducible_and_has_no_scripts_or_dependencies(self):
for version in ('1.0', '1.1'):
first = apt.fixture_archive(version)
self.assertEqual(first, apt.fixture_archive(version))
self.assertEqual(apt.digest(first), apt.FIXTURE_SHA256[version])
# Inspect real deb control tar through dpkg-deb, never install.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
path = Path(temp) / 'fixture.deb'
path.write_bytes(first)
data = apt.run_read(['/usr/bin/dpkg-deb', '--ctrl-tarfile', str(path)])
with tarfile.open(fileobj=io.BytesIO(data)) as archive:
self.assertEqual(archive.getnames(), ['./control'])
control = archive.extractfile('./control').read()
self.assertNotIn(b'Depends:', control)
self.assertIn(('Version: ' + version + '\n').encode(), control)
def test_fixture_scope_only_one_exact_package_and_two_transitions(self):
for recipe in apt.FIXTURE_RECIPES:
apt.fixture_scope(apt.ResolvedAptTransaction.parse(fixture_transaction(recipe)))
for field, value in (('name', 'another-package'), ('new_version', '9.0'),
('sha256', 'd' * 64), ('size', 1), ('origin', 'catalog:external'),
('architecture', 'amd64'), ('old_version', '0.5')):
obj = fixture_transaction()
obj['packages'][0][field] = value
if field == 'sha256':
obj['packages'][0]['archive_path'] = str(apt.BASE / 'archives' / (value + '.deb'))
with self.subTest(field=field), self.assertRaises(apt.Veto):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
obj = fixture_transaction()
obj['recipe_id'] = 'ordinary-catalog-recipe'
with self.assertRaisesRegex(apt.Veto, 'FIXTURE_RECIPE'):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
def test_context_schema1_cannot_authorize_bootstrap(self):
obj = dict(context().__dict__)
obj['schema_version'] = 1
with self.assertRaisesRegex(apt.Veto, 'CONTEXT_SCHEMA'):
apt.VerifiedHelperContext.parse(obj)
def test_signed_nested_manifest_is_mandatory_and_hash_bound(self):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context()
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'transaction_sha256': apt.digest(apt.canonical(obj))})
manifest = action_manifest(ctx, obj, {})
raw = apt.canonical(signed_job(ctx, manifest))
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(raw)})
def read(path, *args):
return {apt.ACTIVE / 'signed-action.json': raw,
apt.ACTIVE / 'expectation.json': apt.canonical(obj),
apt.CODE: b'code', Path(apt.HOOK): apt.hook_script()}[path]
with patch.object(apt, 'sealed_read', side_effect=read):
self.assertEqual(apt.load_signed_action(tx, ctx), manifest)
wrong_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': '0' * 64})
with self.assertRaisesRegex(apt.Veto, 'SIGNED_ACTION_HASH'):
apt.load_signed_action(tx, wrong_ctx)
for change in ('nested', 'action', 'code', 'profile'):
altered = copy.deepcopy(manifest)
if change == 'nested':
altered['resolved_apt_transaction']['packages'][0]['new_version'] = '9.0'
elif change == 'action':
altered['action'] = 'apt-execute'
elif change == 'code':
altered['provider_sha256'] = '0' * 64
else:
altered['execution_profile'] = 'production'
altered_raw = apt.canonical(signed_job(ctx, altered))
altered_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(altered_raw)})
with patch.object(apt, 'sealed_read', side_effect=lambda path, *args:
altered_raw if path == apt.ACTIVE / 'signed-action.json' else read(path)):
with self.subTest(change=change), self.assertRaises(apt.Veto):
apt.load_signed_action(tx, altered_ctx)
def test_bootstrap_mode_cannot_replace_normal_or_production_action(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
for mode, ctx in ((True, context('apt-execute')), (False, context()),
(True, context(profile='production')), (False, context('apt-execute', 'production'))):
with self.subTest(mode=mode, action=ctx.action), self.assertRaises(apt.Veto):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, mode)
def test_normal_execution_requires_real_scoped_record(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context('apt-execute')
with patch.object(apt, 'sealed_read', side_effect=FileNotFoundError):
with self.assertRaises(FileNotFoundError):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, False)
def test_runtime_changes_are_rejected_even_for_bootstrap(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context()
with self.assertRaisesRegex(apt.Veto, 'SIGNED_RUNTIME_BINDING'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {'new': True}, True)
class NativeProbeTests(TestCase):
def test_native_mount_introspection_and_checks_result_with_mocked_kernel(self):
self.inspect_kernel_scenario('valid')
def test_native_introspection_rejects_bad_caps_namespaces_mounts_and_sockets(self):
for scenario in ('capability', 'nnp', 'namespace', 'readonly', 'tmpfs', 'socket'):
with self.subTest(scenario=scenario), self.assertRaises(apt.Veto):
self.inspect_kernel_scenario(scenario)
def inspect_kernel_scenario(self, scenario):
mask = sum(1 << n for n in (0, 1, 3, 4, 6, 7, 8, 31))
if scenario == 'capability':
mask |= 1 << 22
status = 'NoNewPrivs: ' + ('0' if scenario == 'nnp' else '1') + '\nSeccomp: 2\n'
status += ''.join(key + ': ' + format(mask, 'x') + '\n' for key in ('CapEff', 'CapPrm', 'CapBnd'))
status += 'CapInh: 0\nCapAmb: 0\n'
mountinfo = []
for path in ('/sys', '/proc/sys', '/usr/lib/rmm-apt-transaction', str(apt.BASE / 'archives')):
flags = 'rw' if scenario == 'readonly' and path == '/sys' else 'ro'
mountinfo.append('1 0 0:1 / ' + path + ' ' + flags + ' - sysfs sysfs ro')
for path in ('/run', '/tmp', '/var/tmp', '/dev'):
kind = 'ext4' if scenario == 'tmpfs' and path == '/run' else 'tmpfs'
mountinfo.append('1 0 0:1 / ' + path + ' rw - ' + kind + ' tmpfs rw')
def text(path, *args, **kwargs):
return {'/proc/self/status': status, '/proc/self/mountinfo': '\n'.join(mountinfo)}[str(path)]
with patch.object(Path, 'read_text', text), \
patch.object(Path, 'read_bytes', return_value=b'/usr/bin/python3\x00-I\x00'), \
patch.object(Path, 'exists', side_effect=lambda: scenario == 'socket'), \
patch.object(Path, 'stat', return_value=type('MockStat', (), {'st_mode': stat.S_IFCHR})()), \
patch.object(apt, 'sealed_read', return_value=apt.canonical({'pid': 'host', 'mnt': 'host', 'net': 'host'})), \
patch.object(apt.os, 'readlink', return_value='host' if scenario == 'namespace' else 'child'), \
patch.object(apt, 'invalid_control_syscall_probes', return_value=prechecks(context())['checks']['control_syscalls']), \
patch.object(apt, 'frontend_lock_probe', return_value='native-apt-check-conflict'), \
patch.object(apt.VerifiedHelperContext, 'check_deadline'):
self.assertEqual(apt.native_prechecks(context()), prechecks(context()))
def test_safe_invalid_syscall_probes_in_unprivileged_filtered_child(self):
code = '''import importlib.util,sys,json
sys.dont_write_bytecode=True
s=importlib.util.spec_from_file_location('apt_transaction',sys.argv[1])
m=importlib.util.module_from_spec(s);sys.modules[s.name]=m;s.loader.exec_module(m)
m.seccomp_filter()
print(json.dumps(m.invalid_control_syscall_probes()))
'''
output = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(set(json.loads(output).values()), {'EPERM'})
def test_frontend_probe_uses_native_check_with_actual_fcntl_lock_but_no_local_apt(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
lock = Path(temp) / 'native-lock'
lock.touch()
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'E: Could not get lock /var/lib/dpkg/lock-frontend') as read:
self.assertEqual(apt.frontend_lock_probe(), 'native-apt-check-conflict')
self.assertEqual(read.call_args.args[0][-1], 'check')
self.assertEqual(read.call_args.kwargs['expected_returncode'], 100)
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'an unrelated error'):
with self.assertRaisesRegex(apt.Veto, 'NATIVE_APT_LOCK_CONFLICT_NOT_PROVEN'):
apt.frontend_lock_probe()
class CompletionTests(TestCase):
def setup_files(self, base, ctx):
active = base / 'active'
active.mkdir()
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
(active / 'hook-armed.json').write_bytes(apt.canonical({'grant_id': ctx.grant_id}))
(base / 'bootstrap-attempt-rmm-apt-fixture-install-1.0.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256, 'job_payload_sha256': ctx.job_payload_sha256,
'grant_id': ctx.grant_id, 'attempt_id': ctx.attempt_id}))
return active
def test_record_after_complete_mocked_native_pipeline_is_scoped_and_fsynced(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
result = apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': False})
self.assertTrue(result['test_acceptance'])
self.assertTrue(result['noProductionEvidence'])
self.assertGreaterEqual(sync.call_count, 3)
proof = json.loads(apt.record_path(tx).read_bytes())
self.assertEqual(proof['job_payload_sha256'], ctx.job_payload_sha256)
self.assertEqual(proof['scope']['recipe_id'], tx.recipe_id)
self.assertEqual(proof['runtime'], {})
normal_ctx = context('apt-execute')
apt.check_native_gate(tx, normal_ctx, action_manifest(normal_ctx, obj, {}), {}, False)
with self.assertRaisesRegex(apt.Veto, 'ACCEPTANCE_ALREADY_RECORDED'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, obj, {}), {}, True)
with self.assertRaises(apt.Veto):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
def test_missing_partial_negative_or_wrong_prechecks_never_publish_record(self):
for variation in ('missing', 'partial', 'negative', 'binding', 'reboot', 'runtime'):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
checkfile = active / 'native-prechecks.json'
if variation == 'missing':
checkfile.unlink()
elif variation == 'partial':
checkfile.write_bytes(b'{')
elif variation in ('negative', 'binding'):
obj = prechecks(ctx)
if variation == 'negative':
obj['checks']['namespaces_caps_nnp_seccomp'] = False
else:
obj['transaction_sha256'] = '0' * 64
checkfile.write_bytes(apt.canonical(obj))
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={'changed': True} if variation == 'runtime' else {}):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
with self.subTest(variation=variation), self.assertRaises((apt.Veto, OSError)):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': variation == 'reboot'})
self.assertFalse(apt.record_path(tx).exists())
def test_staged_write_failure_never_publishes_final_record(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'durable_once', side_effect=OSError('fsync failure')):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
with self.assertRaises(OSError):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
self.assertFalse(apt.record_path(tx).exists())
class DriverTests(TestCase):
def test_entire_driver_mocked_child_success_and_redelivery_no_second_launch(self):
self.run_driver_scenario('success')
def test_failed_unknown_stale_or_partial_driver_never_creates_acceptance(self):
for scenario in ('child-failed', 'no-hook', 'missing-prechecks', 'postcondition-failed', 'stale'):
with self.subTest(scenario=scenario):
self.run_driver_scenario(scenario)
def run_driver_scenario(self, scenario):
# No unshare/apt/root execution: only the process boundary and root
# preflight functions are mocked. Actual gate/one-shot/fsync/record logic
# runs on private temporary files. Such files never leave this test dir.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = base / 'active'
active.mkdir()
for name in ('empty', 'lists', 'cache'):
(active / name).mkdir()
(active / 'apt.conf').write_bytes(apt.apt_config())
ctx = context()
native_code = 100 if scenario == 'child-failed' else 0
def read(path, *args):
return apt.hook_script() if path == Path(apt.HOOK) else path.read_bytes()
def child(argv, **kwargs):
self.assertEqual(argv[0], '/usr/bin/unshare')
self.assertIn('--pid', argv)
if scenario != 'no-hook':
(active / 'hook-armed.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id, 'grant_id': ctx.grant_id}))
(active / 'sandbox-result.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256, 'apt_exit_code': native_code,
'reboot_required': False}))
if scenario != 'missing-prechecks':
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
return type('MockNativeChild', (), {'wait': lambda _: native_code})()
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt.os, 'geteuid', return_value=0), patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'check_current_baseline'), patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'), \
patch.object(apt, 'run_read', return_value=b''), patch.object(apt, 'installed_snapshot', return_value={}), \
patch.object(apt.VerifiedHelperContext, 'check_deadline',
side_effect=apt.Veto('START_DEADLINE') if scenario == 'stale' else None), \
patch.object(apt.subprocess, 'Popen', side_effect=child) as spawn:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
manifest = action_manifest(ctx, obj, {})
evidence = {'reboot_required': False, 'snapshot_sha256': 'e' * 64}
with patch.object(apt, 'load_active', return_value=(tx, ctx, {})), \
patch.object(apt, 'load_signed_action', return_value=manifest), \
patch.object(apt, 'verify_after', return_value=evidence,
side_effect=apt.Veto('POSTCONDITION_OR_UNEXPECTED_CHANGE')
if scenario == 'postcondition-failed' else None):
request = {'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id}
result = apt.execute(request, native_acceptance=True)
self.assertTrue(result['evidence']['test_acceptance'])
self.assertTrue(result['evidence']['noProductionEvidence'])
if scenario == 'success':
self.assertEqual(result['effect_state'], 'verified')
self.assertTrue(apt.record_path(tx).is_file())
self.assertEqual(spawn.call_count, 1)
repeated = apt.execute(request, native_acceptance=True)
self.assertEqual(repeated['effect_state'], 'unknown')
self.assertEqual(spawn.call_count, 1)
else:
self.assertNotEqual(result['effect_state'], 'verified')
self.assertFalse(apt.record_path(tx).exists())
self.assertEqual(spawn.call_count, 0 if scenario == 'stale' else 1)
@@ -0,0 +1,198 @@
"""Harmless tmp fixtures only; root ownership/system state/providers mocked."""
import copy
import json
import os
from pathlib import Path
import stat
import tempfile
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, ROOT
from test_native_acceptance import fixture_transaction, context, action_manifest
class PlanningTests(TestCase):
def run_planning(self, variation=None):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'archives').mkdir()
(base / 'baselines').mkdir()
for version, sha in apt.FIXTURE_SHA256.items():
(base / 'archives' / (sha + '.deb')).write_bytes(apt.fixture_archive(version))
(base / ('prefetch-' + sha + '.json')).write_bytes(apt.canonical({
'schema_version': 1, 'sha256': sha, 'size': 20672, 'sealed_at_ms': 1000}))
pin = apt.FIXTURE_SHA256['1.0']
if variation == 'bytes':
(base / 'archives' / (pin + '.deb')).write_bytes(b'x' * 20672)
if variation == 'receipt':
(base / ('prefetch-' + pin + '.json')).write_bytes(apt.canonical({
'schema_version': 1, 'sha256': pin, 'size': 20672, 'sealed_at_ms': 10**30}))
if variation == 'missing':
(base / ('prefetch-' + pin + '.json')).unlink()
initial = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'},
'private-business-package:amd64': {'version': 'secret', 'status': 'install ok installed'}}
current = copy.deepcopy(initial)
runtime = {'apt_config_sha256': apt.digest(apt.apt_config()), 'schema_version': 1}
def read(path, *args):
if path == apt.CODE:
return b'code'
if path == Path(apt.HOOK):
return apt.hook_script()
return path.read_bytes()
original_metadata = apt.archive_metadata
def metadata(st, size):
# Only ownership is simulated. Real bytes/size/link count are
# used, and production metadata validation remains exercised.
original_metadata(SimpleNamespace(st_uid=0,
st_mode=stat.S_IFREG | (0o622 if variation == 'writable' else 0o600),
st_size=st.st_size, st_nlink=st.st_nlink), size)
fingerprint_error = apt.Veto('DPKG_DIRTY_UPDATES') if variation == 'dirty' else None
if variation == 'drift':
fingerprint_error = ['b' * 64, 'c' * 64]
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), \
patch.object(apt, 'archive_metadata', side_effect=metadata), \
patch.object(apt, 'observe_host_frontend_lock', return_value=variation == 'locked'), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64, side_effect=fingerprint_error), \
patch.object(apt, 'installed_snapshot', side_effect=lambda: copy.deepcopy(current)), \
patch.object(apt, 'runtime_identity', return_value=runtime), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
first = apt.planning_status()
self.assertGreaterEqual(sync.call_count, 2)
self.assertNotIn('private-business-package', json.dumps(first))
sealed = base / 'baselines' / (first['baseline_sha256'] + '.json')
self.assertEqual(apt.digest(sealed.read_bytes()), first['baseline_sha256'])
self.assertEqual(json.loads(sealed.read_bytes()), initial)
self.assertEqual(first['baseline_sha256'], first['sealed_baseline_sha256'])
self.assertEqual(first['runtime_sha256'], apt.digest(apt.canonical(runtime)))
before = sealed.stat().st_mtime_ns
self.assertEqual(apt.planning_status()['baseline_sha256'], first['baseline_sha256'])
self.assertEqual(sealed.stat().st_mtime_ns, before)
current['rmm-mvp-test:all']['version'] = '1.1'
fresh = apt.planning_status()
self.assertNotEqual(first['baseline_sha256'], fresh['baseline_sha256'])
self.assertEqual(fresh['fixture_package']['version'], '1.1')
self.assertEqual(len(list((base / 'baselines').iterdir())), 2)
self.assertFalse((base / 'active').exists())
def test_passive_seal_is_content_addressed_fresh_and_private(self):
self.run_planning()
def test_bad_receipts_bytes_ownership_dirty_or_locked_are_not_observations(self):
for variation in ('bytes', 'receipt', 'missing', 'writable', 'dirty', 'locked'):
with self.subTest(variation=variation), self.assertRaises((apt.Veto, OSError)):
self.run_planning(variation)
def test_state_changes_during_sampling_cannot_publish(self):
with patch.object(apt, 'durable_once') as write:
with self.assertRaisesRegex(apt.Veto, 'PLANNING_STATE_CHANGED'):
self.run_planning('drift')
write.assert_not_called()
def test_shared_canonical_golden_bytes(self):
raw = (ROOT / 'tests/apt_transaction/fixtures/planning-canonical.json').read_bytes().rstrip(b'\n')
self.assertEqual(apt.canonical(json.loads(raw)), raw)
self.assertEqual(apt.digest(raw), 'f5c1f569110924eca83ad5d347882390b47e077fbef91c4eb86abb68621b92c4')
class NoChangeTests(TestCase):
def check_noop(self, recipe, version=None, status='install ok installed', locked=False, changed=False,
architecture='all', dirty=False, audit=b'', payload=None, expired=False):
obj = fixture_transaction(recipe)
desired = obj['packages'][0]['new_version']
baseline = {'rmm-mvp-test:' + architecture: {'version': version or desired, 'status': status}}
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
obj['status_fingerprint'] = 'b' * 64
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context('apt-execute')
current = copy.deepcopy(baseline)
if changed:
current['rmm-mvp-test:all']['version'] = '9.0'
with patch.object(apt, 'installed_snapshot', return_value=current), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64,
side_effect=apt.Veto('DPKG_PENDING_TRIGGERS') if dirty else None), \
patch.object(apt, 'observe_host_frontend_lock', return_value=locked), \
patch.object(apt, 'run_read', return_value=audit), \
patch.object(apt, 'sealed_read', return_value=payload if payload is not None else (desired + '\n').encode()), \
patch.object(apt.VerifiedHelperContext, 'check_deadline',
side_effect=apt.Veto('START_DEADLINE') if expired else None), \
patch.object(apt.subprocess, 'Popen') as spawn:
result = apt.verified_no_change(tx, baseline, ctx)
spawn.assert_not_called()
return result, tx, baseline
def test_install_and_update_already_desired_skip_only_old_version(self):
for recipe in apt.FIXTURE_RECIPES:
result, tx, baseline = self.check_noop(recipe)
self.assertFalse(result['changed'])
self.assertTrue(result['postcondition_already_met'])
self.assertFalse(result['transaction_executed'])
# Mutating preconditions still forbid a reinstall of that version.
with patch.object(apt, 'status_fingerprint', return_value='b' * 64), \
patch.object(apt, 'installed_snapshot', return_value=baseline):
with self.assertRaisesRegex(apt.Veto, 'PACKAGE_PRECONDITION'):
apt.check_preconditions(tx, baseline)
def test_missing_target_dirty_arch_or_newer_version_cannot_claim_no_change(self):
for version, status in (('1.0', 'install ok installed'), ('1.2', 'install ok installed'),
('1.1', 'deinstall ok config-files')):
result, _, _ = self.check_noop('rmm-apt-fixture-update-1.1', version, status)
self.assertIsNone(result)
for variation in ({'locked': True}, {'changed': True}):
with self.assertRaises(apt.Veto):
self.check_noop('rmm-apt-fixture-update-1.1', **variation)
result, _, _ = self.check_noop('rmm-apt-fixture-update-1.1', architecture='amd64')
self.assertIsNone(result)
def test_noop_dirty_state_audit_payload_and_expiry_are_not_success(self):
for variation in ({'dirty': True}, {'audit': b'pending package'}, {'payload': b'wrong\n'}, {'expired': True}):
with self.subTest(variation=variation), self.assertRaises(apt.Veto):
self.check_noop('rmm-apt-fixture-update-1.1', **variation)
def test_full_driver_no_launch_no_effect_markers_after_all_checks(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = base / 'active'
active.mkdir()
for directory in ('empty', 'lists', 'cache'):
(active / directory).mkdir()
(active / 'apt.conf').write_bytes(apt.apt_config())
obj = fixture_transaction('rmm-apt-fixture-update-1.1')
baseline = {'rmm-mvp-test:all': {'version': '1.1', 'status': 'install ok installed'}}
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
obj['status_fingerprint'] = 'b' * 64
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context('apt-execute')
def read(path, *args, **kwargs):
if path == Path(apt.HOOK):
return apt.hook_script()
if str(path) == '/usr/share/rmm-mvp-test/version':
return b'1.1\n'
return path.read_bytes()
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'load_active', return_value=(tx, ctx, baseline)), \
patch.object(apt, 'load_signed_action', return_value=action_manifest(ctx, obj, {})) as signed, \
patch.object(apt, 'check_native_gate') as gate, patch.object(apt, 'verify_archive') as archive, \
patch.object(apt, 'installed_snapshot', return_value=baseline), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64), \
patch.object(apt, 'observe_host_frontend_lock', return_value=False), \
patch.object(apt, 'run_read', return_value=b''), \
patch.object(apt.VerifiedHelperContext, 'check_deadline'), \
patch.object(apt.subprocess, 'Popen') as spawn:
request = {'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id}
result = apt.execute(request)
self.assertEqual(result['effect_state'], 'verified_no_change')
self.assertEqual(result['code'], 'POSTCONDITION_ALREADY_MET')
signed.assert_called_once()
gate.assert_called_once()
archive.assert_called_once()
spawn.assert_not_called()
self.assertEqual({p.name for p in active.iterdir()}, {'empty', 'lists', 'cache', 'apt.conf'})
@@ -0,0 +1,184 @@
"""Readiness isolation tests. No root setup/APT/host package effects are run."""
import json
import os
from pathlib import Path
import subprocess
import tempfile
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, ROOT
from test_native_acceptance import prechecks, context
class ReadinessProbeTests(TestCase):
def test_controller_requires_actual_unit_caps_nnp_and_readonly_system(self):
mask = sum(1 << bit for bit in (0, 1, 3, 4, 6, 7, 8, 31, 21, 27))
for fault in ('none', 'boot-cap', 'nnp', 'filter', 'unit', 'writable', 'readonly-target', 'extra-write'):
current_mask = mask | (1 << 22) if fault == 'boot-cap' else mask
status = ('NoNewPrivs: ' + ('0' if fault == 'nnp' else '1') + '\n' +
'Seccomp: ' + ('0' if fault == 'filter' else '2') + '\n' +
''.join(key + ': ' + format(current_mask, 'x') + '\n' for key in ('CapEff', 'CapPrm', 'CapBnd')) +
'CapInh: 0\nCapAmb: 0\n')
cgroup = '0::/system.slice/' + ('other.service' if fault == 'unit' else 'rmm-apt-helper.service')
mounts = '1 0 0:1 / /usr ro - ext4 root ro\n'
if fault == 'extra-write':
mounts += '2 1 0:1 /usr/share /usr/share rw - ext4 root rw\n'
def text(path, *args, **kwargs):
return {'/proc/self/status': status, '/proc/self/cgroup': cgroup,
'/proc/self/mountinfo': mounts}[str(path)]
def flags(path):
ro = path in ('/usr', '/etc', '/var')
if fault == 'writable':
ro = False
if fault == 'readonly-target':
ro = True
return SimpleNamespace(f_flag=os.ST_RDONLY if ro else 0)
with self.subTest(fault=fault), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(Path, 'read_text', text), patch.object(apt.os, 'statvfs', side_effect=flags):
if fault == 'none':
apt.verify_controller_profile()
else:
with self.assertRaises(apt.Veto):
apt.verify_controller_profile()
def test_host_lock_observation_uses_only_readonly_getlk(self):
calls = []
def query(fd, operation, data):
calls.append(operation)
return data # same F_WRLCK query denotes a foreign owner; no acquisition
with patch.object(apt, 'secure_path', return_value=Path('/var/lib/dpkg/lock-frontend')), \
patch.object(apt.os, 'open', return_value=123) as opened, \
patch.object(apt.os, 'close'), patch.object(apt.fcntl, 'fcntl', side_effect=query), \
patch.object(apt.fcntl, 'lockf') as lockf, patch.object(apt.fcntl, 'flock') as flock:
self.assertTrue(apt.observe_host_frontend_lock())
self.assertEqual(calls, [apt.fcntl.F_GETLK])
flags = opened.call_args.args[1]
self.assertEqual(flags & os.O_ACCMODE, os.O_RDONLY)
lockf.assert_not_called()
flock.assert_not_called()
def test_probe_config_has_no_hooks_sources_or_install_command(self):
workspace = Path('/var/lib/rmm-apt-transaction/probes/00000000-0000-4000-8000-000000000001')
config = apt.probe_config(workspace)
self.assertNotIn(b'DPkg::Pre-Install-Pkgs', config)
self.assertNotIn(b'DPkg::Tools::Options', config)
self.assertIn(b'Dir::Etc::sourcelist "/dev/null"', config)
self.assertIn(str(workspace / 'log').encode(), config)
def test_probe_publishes_only_readiness_with_mocked_native_child(self):
self.probe_scenario('success')
def test_failed_or_partial_native_probe_never_publishes_readiness(self):
for scenario in ('child-failed', 'partial', 'syscalls', 'same-namespace'):
with self.subTest(scenario=scenario):
self.probe_scenario(scenario)
def probe_scenario(self, scenario):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'probes').mkdir(mode=0o700)
host_status = b'Package: read-only-host-baseline\n'
original_open = os.open
def no_host_writes(path, flags, *args, **kwargs):
if flags & (os.O_WRONLY | os.O_RDWR | os.O_CREAT | os.O_TRUNC):
self.assertTrue(str(path).startswith(str(base) + '/'), str(path))
return original_open(path, flags, *args, **kwargs)
def sealed(path, *args):
if path == Path('/var/lib/dpkg/status'):
return host_status
return path.read_bytes()
def native(argv, **kwargs):
self.assertEqual(argv[-1], 'probe-worker')
self.assertNotIn('install', argv)
request = json.loads(kwargs['input'])
workspace = base / 'probes' / request['probe_id']
self.assertEqual((workspace / 'dpkg/status').read_bytes(), host_status)
self.assertFalse((workspace / 'context.json').exists())
if scenario != 'child-failed':
result = {'probe_id': request['probe_id'], 'checks': prechecks(context())['checks'],
'isolated_dpkg_check': True,
'namespaces': {name: name + ':[999999999]' for name in ('pid', 'mnt', 'net')}}
if scenario == 'partial':
result.pop('checks')
elif scenario == 'syscalls':
result['checks']['control_syscalls']['invalid_reboot_magic'] = 'EINVAL'
elif scenario == 'same-namespace':
result['namespaces'] = json.loads((workspace / 'host-namespaces.json').read_bytes())
(workspace / 'probe-result.json').write_bytes(apt.canonical(result))
return SimpleNamespace(returncode=100 if scenario == 'child-failed' else 0)
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path'), patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'sealed_read', side_effect=sealed), \
patch.object(apt, 'observe_host_frontend_lock', return_value=False), \
patch.object(apt, 'verify_controller_profile'), \
patch.object(apt.os, 'open', side_effect=no_host_writes), \
patch.object(apt.subprocess, 'run', side_effect=native):
if scenario == 'success':
outcome = apt.probe_native()
self.assertTrue(outcome['readiness_only'])
self.assertFalse(outcome['native_acceptance'])
status = apt.readiness_status()
self.assertTrue(status['fixture_acceptance_ready'])
self.assertEqual(status['accepted_recipes'], [])
# A valid readiness report cannot promote a partial fixture
# acceptance file into an accepted package capability.
partial = base / ('acceptance-' + next(iter(apt.FIXTURE_RECIPES)) + '.json')
partial.write_bytes(b'{"schema_version":2,"test_acceptance":true}')
with self.assertRaises(apt.Veto):
apt.readiness_status()
partial.unlink() # only this artificially injected temp test file
else:
with self.assertRaises(apt.Veto):
apt.probe_native()
self.assertFalse((base / 'native-readiness.json').exists())
for pattern in ('**/hook-armed.json', '**/launch.json', '**/context.json',
'**/prefetch-*.json', '**/acceptance-*.json', '**/bootstrap-attempt-*.json'):
self.assertEqual(list(base.glob(pattern)), [])
def test_clone_namespace_flags_and_clone3_closed_but_fork_exec_works(self):
code = '''import ctypes,importlib.util,sys,subprocess,errno
sys.dont_write_bytecode=True
s=importlib.util.spec_from_file_location('apt_transaction',sys.argv[1])
m=importlib.util.module_from_spec(s);sys.modules[s.name]=m;s.loader.exec_module(m)
m.seccomp_filter()
lib=ctypes.CDLL('libseccomp.so.2');lib.seccomp_syscall_resolve_name.argtypes=[ctypes.c_char_p]
c=ctypes.CDLL(None,use_errno=True);c.syscall.restype=ctypes.c_long
clone=lib.seccomp_syscall_resolve_name(b'clone');clone3=lib.seccomp_syscall_resolve_name(b'clone3')
for bit in (0x80,0x20000,0x2000000,0x4000000,0x8000000,0x10000000,0x20000000,0x40000000):
ctypes.set_errno(0)
# Invalid exit signal ensures no child can be created even without a filter.
assert c.syscall(clone,ctypes.c_ulong(bit|255),0,0,0,0)==-1
assert ctypes.get_errno()==errno.EPERM
ctypes.set_errno(0);assert c.syscall(clone3,0,0)==-1 and ctypes.get_errno()==errno.ENOSYS
assert subprocess.check_output(['/usr/bin/python3','-I','-c','print("fork-ok")'])==b'fork-ok\\n'
print('CLONE_GAPS_CLOSED')
'''
result = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(result, b'CLONE_GAPS_CLOSED\n')
def test_dedicated_unit_has_narrow_profile_without_weakening_general_unit(self):
unit = (ROOT / 'deploy/linux/rmm-apt-helper.service').read_text()
for required in ('NoNewPrivileges=true', 'PrivateNetwork=true', 'ProtectSystem=strict',
'RestrictNamespaces=mnt pid net', 'DevicePolicy=strict', 'PrivateDevices=no',
'AmbientCapabilities=', 'SystemCallArchitectures=native'):
self.assertIn(required, unit)
bounding = next(line for line in unit.splitlines() if line.startswith('CapabilityBoundingSet='))
self.assertIn('CAP_SYS_ADMIN', bounding)
self.assertIn('CAP_MKNOD', bounding)
self.assertNotIn('CAP_SYS_BOOT', bounding)
self.assertNotIn('CAP_SYS_CHROOT', bounding)
writable = next(line for line in unit.splitlines() if line.startswith('ReadWritePaths='))
self.assertEqual(writable.split('=', 1)[1].split(), ['/var/lib/dpkg', '/var/lib/rmm-helper',
'/var/lib/rmm-apt-transaction', '/usr/share/rmm-mvp-test'])
@@ -0,0 +1,69 @@
"""The provider consumes the exact signed domain job, never a detached manifest."""
import copy
import json
import tempfile
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt
from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job
class SignedHelperEnvelopeTests(TestCase):
def test_pregrant_baselines_are_content_addressed_and_do_not_reset_artifacts(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'baselines').mkdir(mode=0o700)
snapshot = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'}}
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path'), patch.object(apt, 'installed_snapshot', return_value=snapshot), \
patch.object(apt, 'status_fingerprint', return_value='a' * 64), \
patch.object(apt, 'sealed_read', side_effect=lambda path: path.read_bytes()):
result = apt.prepare_baseline_store()
path = base / 'baselines' / (result['baseline_sha256'] + '.json')
self.assertEqual(json.loads(path.read_bytes()), snapshot)
self.assertFalse(result['authorization_created'])
self.assertFalse(result['native_acceptance_created'])
initial = path.stat().st_mtime_ns
self.assertEqual(apt.prepare_baseline_store(), result)
self.assertEqual(path.stat().st_mtime_ns, initial)
snapshot['rmm-mvp-test:all']['version'] = '1.1'
newer = apt.prepare_baseline_store()
self.assertNotEqual(newer['baseline_sha256'], result['baseline_sha256'])
self.assertEqual(len(list((base / 'baselines').iterdir())), 2)
def test_detached_manifest_aliases_wrong_ids_and_nonexact_bytes_are_rejected(self):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
raw_tx = apt.canonical(obj)
ctx = context()
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'transaction_sha256': apt.digest(raw_tx)})
manifest = action_manifest(ctx, obj, {})
valid = signed_job(ctx, manifest)
variants = [manifest]
for fault in ('job-id', 'script-alias', 'transaction-byte', 'transaction-bool', 'schema', 'extra-root'):
changed = copy.deepcopy(valid)
if fault == 'job-id':
changed['job_id'] = 'another-job'
elif fault == 'script-alias':
changed['action']['action'] = 'run_script'
elif fault == 'schema':
changed['action']['apt_manifest']['schema_version'] = 1
elif fault == 'extra-root':
changed['unsigned_manifest'] = manifest
else:
changed['action']['apt_manifest']['transaction_bytes'][0] = True if fault == 'transaction-bool' else 0
variants.append(changed)
for value in variants:
raw = apt.canonical(value)
candidate = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(raw)})
def read(path, *args):
return {apt.ACTIVE / 'signed-action.json': raw,
apt.ACTIVE / 'expectation.json': raw_tx,
apt.CODE: b'code', Path(apt.HOOK): apt.hook_script()}[path]
with self.subTest(value=value.get('type', 'job')), patch.object(apt, 'sealed_read', side_effect=read):
with self.assertRaises(apt.Veto):
apt.load_signed_action(tx, candidate)
+170
View File
@@ -0,0 +1,170 @@
#!/usr/bin/env python3
"""Local TLS/PostgreSQL backend contracts; no VM, no approval or execution.
Creates a clearly synthetic device fixture and pending requests, then revokes the
fixture. Never calls authorization with browser credentials or writes approvals.
"""
import datetime
import hashlib
import json
import secrets
import uuid
import subprocess
import tempfile
from pathlib import Path
from local_integration import LocalHTTPS, WebSocket, field, envelope, token as enrollment_token, CA, STATE, A, B, sql, check
import ssl
def request(path, method="GET", body=None, token=None, extra=None):
c = LocalHTTPS("rmm-dev.internal", 18443, context=ssl.create_default_context(cafile=str(CA)), timeout=10)
h = {"Content-Type": "application/json", **(extra or {})}
if token:
h["Authorization"] = "Bearer " + token
c.request(method, path, body=json.dumps(body) if body is not None else None, headers=h)
r = c.getresponse()
status, headers = r.status, dict(r.getheaders())
if "text/event-stream" in headers.get("content-type", headers.get("Content-Type", "")):
data = None
while True:
line = r.readline()
if not line:
break
if line.startswith(b"data: ") and line[6:].strip():
parsed = json.loads(line[6:])
if "result" in parsed or "error" in parsed:
data = parsed
break
else:
raw = r.read()
data = (raw.decode() if "text/html" in headers.get("content-type", headers.get("Content-Type", "")) else json.loads(raw)) if raw else None
c.close()
return status, headers, data
def main():
operator = (STATE / "operator-a.token").read_text().strip()
viewer = (STATE / "viewer-a.token").read_text().strip()
operator_b = (STATE / "operator-b.token").read_text().strip()
asset, agent = str(uuid.uuid4()), str(uuid.uuid4())
sql(f"INSERT INTO assets VALUES('{A}','{asset}'); INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256,capabilities) VALUES('{A}','{agent}','{asset}','{secrets.token_hex(32)}','[\"run_script\",\"query_logs\"]');")
base = f"/api/v1/tenants/{A}"
try:
now = datetime.datetime.now(datetime.timezone.utc)
end = now + datetime.timedelta(hours=1)
plan_input = {"tenant_id": A, "action": "run_script", "targets": {"asset_ids": [asset]}, "parameters": {"variants": [{"os_family": "linux", "interpreter": "posix_sh", "encoding": "utf-8", "script_utf8": "printf 'Hello World\\n'\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": end.isoformat(), "offline_policy": "wait_until_deadline"}, "rollout": {"max_parallel": 1, "halt_after_failures": 1}, "reason": "synthetic backend contract fixture; never execute"}
before = sql(f"SELECT (SELECT count(*) FROM jobs WHERE agent_id='{agent}')||':'||(SELECT count(*) FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}')||':'||(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}');")
check(request(base + "/operation-plans", "POST", plan_input, viewer)[0] == 403, "viewer cannot plan")
status, _, plan = request(base + "/operation-plans", "POST", plan_input, operator)
check(status == 200, "operator creates immutable UI-shaped plan")
pid = plan["plan_id"]
check(hashlib.sha256(bytes(plan["plan_payload_bytes"])).hexdigest() == plan["plan_hash"], "plan hash binds stored exact bytes")
check(plan["jobs"][0]["asset_id"] == asset and plan["resolved_targets"][0]["asset_id"] == asset and plan["excluded_targets"] == [], "UI gets complete fixed target/job previews")
check(request(base + f"/operation-plans/{pid}", token=operator_b)[0] == 404, "foreign plan indistinguishable from missing")
key = "backend-" + uuid.uuid4().hex
status, _, pending = request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": key}, operator)
check(status == 200 and pending["approval_state"] == "pending_approval" and pending["job_count"] == 0 and pending["execution_state"] is None, "submit persists only pending approval with zero jobs")
again = request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": key}, operator)[2]
check(again["operation_id"] == pending["operation_id"], "submit redelivery preserves operation identity")
detail = request(base + f"/operation-plans/{pid}", token=operator)[2]
check(detail["approval_state"] == "pending_approval", "plan reload exposes pending request to UI")
p2 = request(base + "/operation-plans", "POST", plan_input, operator)[2]
alias_key="alias-"+uuid.uuid4().hex
check(request(base + f"/operation-plans/{pid}/submit", "POST", {"idempotency_key": alias_key}, operator)[2]["operation_id"]==pending["operation_id"],"new caller key deduplicates against the same submitted plan")
alias_conflict=request(base + f"/operation-plans/{p2['plan_id']}/submit", "POST", {"idempotency_key": alias_key}, operator)
check(alias_conflict[0]==409 and alias_conflict[2]["code"]=="IDEMPOTENCY_CONFLICT","deduplicated submit still durably binds its new key")
conflict = request(base + f"/operation-plans/{p2['plan_id']}/submit", "POST", {"idempotency_key": key}, operator)
check(conflict[0] == 409 and conflict[2]["code"] == "IDEMPOTENCY_CONFLICT", "same key changed plan conflicts")
check(request(base + f"/operation-plans/{pid}/authorizations", "POST", {"plan_hash": plan["plan_hash"]}, operator)[0] == 403, "operator bearer cannot approve")
after = sql(f"SELECT (SELECT count(*) FROM jobs WHERE agent_id='{agent}')||':'||(SELECT count(*) FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}')||':'||(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}');")
check(before == after, "no jobs/outbox/grants created before browser approval")
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); UPDATE operation_plans SET payload='changed' WHERE plan_id='{pid}';", "rmm_app", fail=True)
check(sql("SELECT count(*) FROM operation_plans;", "rmm_app") == "0", "new plan RLS denies missing context")
check(sql(f"BEGIN; SELECT set_config('app.tenant_id','{B}',true); SELECT count(*) FROM operation_plans WHERE plan_id='{pid}'; ROLLBACK;", "rmm_app").splitlines()[-1] == "0", "new plan RLS blocks cross tenant")
log_query={"tenant_id":A,"asset_id":asset,"unit":"ssh.service","since":(now-datetime.timedelta(minutes=5)).isoformat(),"until":now.isoformat(),"max_lines":20,"live":True,"idempotency_key":"logs-"+uuid.uuid4().hex}
status,_,logs_pending=request(base+"/log-queries","POST",log_query,operator)
check(status==200 and logs_pending["approval_state"]=="pending_approval" and logs_pending["job_count"]==0,"live logs create only a fixed pending request")
check(request(base+"/log-queries","POST",log_query,operator)[2]["operation_id"]==logs_pending["operation_id"],"live logs retry preserves fixed plan/request identity")
log_query["max_lines"]=21
check(request(base+"/log-queries","POST",log_query,operator)[2]["code"]=="IDEMPOTENCY_CONFLICT","live logs changed parameters conflict with same key")
log_query["live"]=False
check(request(base+"/log-queries","POST",log_query,viewer)[0]==403,"viewer cannot read operator-scoped logs")
# Dedicated login is tested only for session/CSRF. No browser approval.
login = json.loads((STATE / "browser-login.json").read_text())
check(request("/auth/dev/login", "POST", login)[0] == 403, "login denies missing Origin")
origin = {"Origin": "https://rmm-dev.internal:18443"}
status, headers, _ = request("/auth/dev/login", "POST", login, extra=origin)
cookie = headers.get("set-cookie", headers.get("Set-Cookie", ""))
check(status == 200 and all(x in cookie for x in ["HttpOnly", "Secure", "SameSite=Strict", "__Host-"]), "dedicated interactive secure cookie")
browser = {"Cookie": cookie.split(";")[0]}
session = request("/auth/session", extra=browser)[2]
check(session["testsimulation"] is True and session["csrf_token"], "session explicitly labels test simulation")
check(request(base + "/operation-plans", "POST", plan_input, extra={**origin, **browser})[0] == 403, "browser mutations require CSRF")
csrf = {**browser, **origin, "X-CSRF-Token": session["csrf_token"]}
root_input=json.loads(json.dumps(plan_input));root_input["parameters"]["execution_identity"]="root"
root_denied=request(base+"/operation-plans","POST",root_input,extra=csrf)
check(root_denied[0]==409 and root_denied[2]["code"]=="UNSUPPORTED_CAPABILITY","even dev human root permission cannot enable root without selected verified-helper policy")
check(request(base + f"/operation-plans/{pid}/authorizations", "POST", {"plan_hash": plan["plan_hash"]}, operator, csrf)[0] == 403, "bearer cannot borrow an interactive cookie's approval rights")
check(request("/auth/logout", "POST", {}, extra={**csrf,"Origin":"https://wrong.internal"})[0] == 403, "valid CSRF cannot bypass Origin check")
check(request("/auth/logout", "POST", {}, extra=csrf)[0] == 204, "CSRF protected logout")
check(request("/auth/session", extra=browser)[0] == 401, "logout revokes session")
check(request("/", token=None)[0] == 200, "same-origin compiled frontend served")
# rmcp real Streamable HTTP lifecycle, sessions and tenant checks.
h = {"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": "rmm-backend-contract", "version": "1"}}}
status, headers, response = request("/mcp", "POST", init, operator, h)
check(status == 200 and response["result"]["protocolVersion"] == "2025-11-25", "rmcp Streamable HTTP negotiates pinned protocol")
session_id = headers.get("mcp-session-id", headers.get("Mcp-Session-Id"))
if session_id:
h["Mcp-Session-Id"] = session_id
check(request("/mcp", "POST", {"jsonrpc": "2.0", "method": "notifications/initialized"}, operator, h)[0] == 202, "MCP initialized notification")
result = request("/mcp", "POST", {"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}, operator, h)[2]
names = [x["name"] for x in result["result"]["tools"]]
check(len(names) == 10 and not any("approv" in n or "authoriz" in n for n in names), "ten real MCP tools and no approval tool")
call = {"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "inventory.list_devices", "arguments": {"tenant_id": B}}}
result = request("/mcp", "POST", call, operator, h)[2]["result"]
check(result["isError"] and result["structuredContent"]["code"] == "NOT_FOUND", "MCP enforces same tenant boundary")
call["params"] = {"name": "jobs.get", "arguments": {"tenant_id": A, "operation_id": pending["operation_id"]}}
result = request("/mcp", "POST", call, operator, h)[2]["result"]["structuredContent"]
check(result["approval_state"] == "pending_approval" and result["job_count"] == 0, "MCP jobs.get sees same persisted pending request")
call["params"]={"name":"operations.plan","arguments":{"tenant_id":A,"input":plan_input}}
mcp_plan=request("/mcp","POST",call,operator,h)[2]["result"]["structuredContent"]
check(mcp_plan["jobs"][0]["asset_id"]==asset,"MCP planning uses same exact DTO and fixed job contract")
call["params"]={"name":"operations.submit","arguments":{"tenant_id":A,"plan_id":mcp_plan["plan_id"],"idempotency_key":"mcp-"+uuid.uuid4().hex}}
mcp_pending=request("/mcp","POST",call,operator,h)[2]["result"]["structuredContent"]
check(mcp_pending["approval_state"]=="pending_approval" and mcp_pending["job_count"]==0,"MCP submit cannot enqueue executable work")
check(request("/mcp", "POST", init, extra=h)[0] == 401, "MCP always requires bearer, not session ID")
# Exercise the actual mTLS job gateway without approving any operation.
with tempfile.TemporaryDirectory(prefix="backend-device-", dir=STATE) as folder:
folder=Path(folder)
subprocess.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-backend-contract"],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
enrolled=request("/agent/v1/enroll","POST",{"token":enrollment_token(A),"csr_pem":(folder/"device.csr").read_text()})[2]
(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(enrolled["agent_id"],1,0,10,field(1,"backend-contract")+field(2,"run_script")))
check(ws.receive()[0]==2,"job-aware mTLS gateway negotiates and reads durable outbox")
generation=int(sql(f"SELECT connection_generation FROM agent_installations WHERE agent_id='{enrolled['agent_id']}';"))
plan_input["targets"]["asset_ids"]=[enrolled["asset_id"]]
target_plan=request(base+"/operation-plans","POST",plan_input,operator)[2]
target_pending=request(base+f"/operation-plans/{target_plan['plan_id']}/submit","POST",{"idempotency_key":"gateway-"+uuid.uuid4().hex},operator)[2]
check(target_pending["job_count"]==0,"connected capable device still has no unapproved jobs")
target_job=target_plan["jobs"][0]
permit_request=field(1,target_job["job_id"])+field(2,target_job["attempt_id"])+field(3,target_job["job_payload_hash"])+field(4,1)
ws.send(envelope(enrolled["agent_id"],2,generation,22,permit_request))
opcode,reply=ws.receive()
check(opcode==2 and b"NOT_FOUND" in reply,"preapproval start request denied with no signed grant")
check(sql(f"SELECT count(*) FROM execution_permits WHERE job_id='{target_job['job_id']}';")=="0","denied grant request leaves no start reservation")
ws.send(envelope(enrolled["agent_id"],3,generation,12,field(1,str(uuid.uuid4()))))
check(ws.receive()[0]==2,"fresh heartbeat remains available after denied start")
finally:
ws.close()
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{enrolled['agent_id']}';")
finally:
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print("Backend local contracts passed; no browser approval, VM job, result replay or native execution acceptance performed.")
if __name__ == "__main__":
main()
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Real PostgreSQL/API cursor, scope and catalog-reference tests; no approvals."""
import datetime
import hashlib
import json
import uuid
from local_integration import STATE, A, B, sql, check
from backend_contract import request
def main():
operator=(STATE/"operator-a.token").read_text().strip()
viewer=(STATE/"viewer-a.token").read_text().strip()
other=(STATE/"operator-b.token").read_text().strip()
devices=[(str(uuid.uuid4()),str(uuid.uuid4())) for _ in range(2)]
base=f"/api/v1/tenants/{A}"
for asset,agent in devices:
sql(f"INSERT INTO assets VALUES('{A}','{asset}'); INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256,capabilities) VALUES('{A}','{agent}','{asset}','{uuid.uuid4().hex+uuid.uuid4().hex}','[\"ensure_package\",\"apply_updates\"]');")
try:
status,_,first=request(base+"/devices?limit=1",token=operator)
check(status==200 and len(first["items"])==1 and first["truncated"] and first["next_cursor"],"device list has bounded signed next cursor")
cursor=first["next_cursor"]
status,_,second=request(base+"/devices?cursor="+cursor,token=operator)
check(status==200 and first["as_of"]==second["as_of"] and first["items"][0]["agent_id"]!=second["items"][0]["agent_id"],"cursor preserves as-of and stable nonoverlapping order")
tampered=cursor[:-1]+("0" if cursor[-1]!="0" else "1")
check(request(base+"/devices?cursor="+tampered,token=operator)[0]==400,"cursor signature rejects tampering")
check(request(base+"/devices?cursor="+cursor,token=viewer)[0]==400,"cursor bound to authenticated principal")
check(request(base+"/operations?cursor="+cursor,token=operator)[0]==400,"cursor bound to resource/sort context")
check(request(base+"/devices?limit=501",token=operator)[0]==400,"page size maximum enforced")
check(request(f"/api/v1/tenants/{B}/devices?cursor="+cursor,token=operator)[0]==404,"foreign tenant does not expose cursor or inventory")
check(request(base+"/operations",token=viewer)[0]==403,"viewer has inventory scope but not operation-output scope")
check(sql("SELECT count(*) FROM memberships;","rmm_app")=="0","membership registry fails closed without tenant context")
# Metadata fixture only: never approve or install this synthetic package.
package="rmm-fixture-"+uuid.uuid4().hex
recipe={"schema_version":1,"provider":"apt","package":package,"version":"1.0+test1","architecture":"amd64","recipe_version":"fixture-1","installation_context":"system","reboot_policy":"never"}
payload=json.dumps(recipe,separators=(",",":")).encode(); digest=hashlib.sha256(payload).hexdigest(); recipe_id=str(uuid.uuid4())
sql(f"INSERT INTO package_recipes(tenant_id,recipe_id,package,package_version,architecture,recipe_version,payload,payload_hash) VALUES('{A}','{recipe_id}','{package}','1.0+test1','amd64','fixture-1',decode('{payload.hex()}','hex'),'{digest}');")
now=datetime.datetime.now(datetime.timezone.utc)
action={"action":"ensure_package","provider":"apt","package":package,"version":"1.0+test1","architecture":"amd64","recipe_version":"fixture-1","installation_context":"system"}
plan_input={"targets":{"asset_ids":[devices[0][0]]},"action":action,"execution_identity":"agent_service","schedule":{"not_before":now.isoformat(),"start_before":(now+datetime.timedelta(hours=1)).isoformat()},"max_runtime_seconds":60,"max_output_bytes":65536,"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"synthetic catalog binding; never install"}
status,_,plan=request(base+"/operation-plans","POST",plan_input,operator)
check(status==200 and plan["plan_payload"]["recipe_references"][0]["sha256"]==digest,"package plan hash binds immutable resolved recipe digest")
check(hashlib.sha256(bytes(plan["plan_payload_bytes"])).hexdigest()==plan["plan_hash"],"recipe references belong to exact approved-plan bytes")
check(plan["authorization_status"]=="unsupported_manifest_required","legacy catalog preview is explicitly not execution-ready")
legacy_submit=request(base+f"/operation-plans/{plan['plan_id']}/submit","POST",{"idempotency_key":"missing-manifest-"+uuid.uuid4().hex},operator)
check(legacy_submit[0]==409 and legacy_submit[2]["code"]=="APT_MANIFEST_REQUIRED","missing schema-2 manifest cannot become an approval request/execution path")
check(request(base+"/package-recipes?limit=1",token=operator)[0]==200,"immutable catalog exposed through scoped bounded list")
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); UPDATE package_recipes SET payload='changed' WHERE recipe_id='{recipe_id}';","rmm_app",fail=True)
print("PASS application cannot mutate immutable recipe version")
plan_input["action"]["recipe_version"]="different"
check(request(base+"/operation-plans","POST",plan_input,operator)[2]["code"]=="RECIPE_BINDING_MISMATCH","wrong recipe version cannot silently resolve")
plan_input["action"]={"action":"apply_updates","provider":"apt","packages":[package+"=1.0+test1"],"reboot_policy":"never"}
status,_,updates=request(base+"/operation-plans","POST",plan_input,operator)
check(status==200 and updates["plan_payload"]["recipe_references"][0]["sha256"]==digest,"narrow version-pinned update selection resolves same immutable recipe")
plan_input["action"]["packages"]=[package]
check(request(base+"/operation-plans","POST",plan_input,operator)[0]==400,"unversioned broad update selection rejected")
check(request(base+"/package-recipes","POST",recipe,operator)[0]==403,"automation cannot import privileged catalog content")
check(request(base+"/artifacts","POST",{"bytes":[1,2],"media_type":"application/octet-stream"},operator)[0]==403,"automation cannot use generic artifact upload")
for path,body in [(f"/devices/{devices[0][0]}/revocation",{"reason":"deny automation"}),(f"/operation-plans/{plan['plan_id']}/revocation",{"reason":"deny automation"}),("/execution-stop",{"stopped":True,"reason":"deny automation"})]:
check(request(base+path,"POST",body,operator)[0]==403,"administrative control requires separate interactive human context")
check(request(base+f"/operation-plans/{plan['plan_id']}",token=other)[0]==404,"catalog-backed plan remains tenant isolated")
check(sql(f"SELECT count(*) FROM jobs WHERE agent_id IN ('{devices[0][1]}','{devices[1][1]}');")=="0","catalog preparation creates no executable work")
finally:
for _,agent in devices:sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print("Backend paging/membership/catalog tests passed locally; no approvals or native package effects performed.")
if __name__ == "__main__":main()
+226
View File
@@ -0,0 +1,226 @@
//! Pure backend policy/permission requirements: no DB memberships, approval,
//! journald reads, helper invocation or root effects are manufactured here.
use super::*;
fn fixture() -> (RootScope, JobPayload, Vec<String>) {
let id = Uuid::new_v4();
let now = Utc::now();
let job = JobPayload {
schema_version: 1,
tenant_id: id,
asset_id: id,
agent_id: id,
enrollment_generation: 1,
assignment_version: 1,
job_id: Uuid::new_v4(),
attempt_id: Uuid::new_v4(),
action: Action::QueryLogs {
unit: "rmm-acceptance.service".into(),
since: now - chrono::Duration::minutes(5),
until: now,
max_lines: 100,
},
execution_identity: "root".into(),
not_before: now,
start_before: now + chrono::Duration::hours(1),
max_runtime_seconds: 20,
max_output_bytes: 65536,
};
let scope = RootScope {
tenant_id: id,
asset_id: id,
agent_id: id,
enrollment_generation: 1,
assignment_version: 1,
principals: vec!["selected".into()],
helper_policy_sha256: "ab".repeat(32),
acceptance_evidence_id: "reviewed-helper-record".into(),
allowed_services: vec!["mutation-only.service".into()],
allow_root_scripts: false,
allow_reboot: false,
apt: None,
root_logs: Some(RootLogPolicy {
schema_version: 1,
allowed_units: vec!["rmm-acceptance.service".into()],
sensitive_units: vec![],
}),
};
let caps = vec![
"rmm.sync.v1".into(),
"helper.root.v1".into(),
format!("helper.policy.sha256:{}", "ab".repeat(32)),
"root:query_logs".into(),
"query_logs".into(),
];
(scope, job, caps)
}
#[test]
fn exact_root_log_plan_shape_and_selected_scope() {
let (scope, job, caps) = fixture();
assert!(job.action.validate());
assert!(scope.permits("selected", &job, &caps, None));
assert!(!scope.permits("viewer", &job, &caps, None));
let mut foreign = job.clone();
foreign.tenant_id = Uuid::new_v4();
assert!(!scope.permits("selected", &foreign, &caps, None));
foreign = job.clone();
foreign.asset_id = Uuid::new_v4();
assert!(!scope.permits("selected", &foreign, &caps, None));
foreign = job.clone();
foreign.assignment_version += 1;
assert!(!scope.permits("selected", &foreign, &caps, None));
foreign = job.clone();
foreign.execution_identity = "agent_service".into();
assert!(!scope.permits("selected", &foreign, &caps, None));
assert!(!scope.permits("selected", &job, &caps[..4], None));
}
#[test]
fn default_empty_and_service_mutation_list_never_authorize_logs() {
let (mut scope, job, caps) = fixture();
scope.root_logs = None;
scope.allowed_services.push("rmm-acceptance.service".into());
assert!(!scope.permits("selected", &job, &caps, None));
let units = vec!["rmm-acceptance.service".into()];
assert!(scope.permits("selected", &job, &caps, Some(&units)));
scope.root_logs = Some(RootLogPolicy {
schema_version: 1,
allowed_units: vec![],
sensitive_units: vec![],
});
assert!(!scope.permits("selected", &job, &caps, Some(&units)));
}
#[test]
fn scoped_allowlist_and_global_ceiling_are_exact() {
let (scope, mut job, caps) = fixture();
assert!(!scope.permits("selected", &job, &caps, Some(&[])));
let foreign = vec!["other.service".into()];
assert!(!scope.permits("selected", &job, &caps, Some(&foreign)));
if let Action::QueryLogs { unit, .. } = &mut job.action {
*unit = "mutation-only.service".into();
}
assert!(!scope.permits("selected", &job, &caps, None));
}
#[test]
fn exact_log_bounds_not_rounded_hours() {
let (scope, job, caps) = fixture();
for lines in [0, 1001] {
let mut bad = job.clone();
if let Action::QueryLogs { max_lines, .. } = &mut bad.action {
*max_lines = lines;
}
assert!(!scope.permits("selected", &bad, &caps, None));
}
for runtime in [0, 19, 3601] {
let mut bad = job.clone();
bad.max_runtime_seconds = runtime;
assert!(!scope.permits("selected", &bad, &caps, None));
}
for output in [0, 65537] {
let mut bad = job.clone();
bad.max_output_bytes = output;
assert!(!scope.permits("selected", &bad, &caps, None));
}
let mut bad = job.clone();
if let Action::QueryLogs { since, until, .. } = &mut bad.action {
*since = *until - chrono::Duration::hours(24) - chrono::Duration::nanoseconds(1);
}
assert!(!scope.permits("selected", &bad, &caps, None));
if let Action::QueryLogs { since, until, .. } = &mut bad.action {
*since = chrono::DateTime::from_timestamp(-1, 0).unwrap();
*until = chrono::DateTime::from_timestamp(1, 0).unwrap();
}
assert!(!scope.permits("selected", &bad, &caps, None));
if let Action::QueryLogs { since, until, .. } = &mut bad.action {
*since = *until;
}
assert!(!scope.permits("selected", &bad, &caps, None));
}
#[test]
fn env_lists_are_bounded_data_not_paths_wildcards_or_bools() {
assert!(parse_log_units(None).unwrap().is_none());
assert_eq!(parse_log_units(Some("")).unwrap(), Some(vec![]));
assert_eq!(
parse_log_units(Some(r#"["rmm-acceptance.service"]"#)).unwrap(),
Some(vec!["rmm-acceptance.service".into()])
);
for raw in [
"true",
r#""/tmp/units""#,
r#"["*.service"]"#,
r#"["../ssh.service"]"#,
r#"["-x.service"]"#,
r#"["x.service","x.service"]"#,
] {
assert!(parse_log_units(Some(raw)).is_err());
}
let too_many = serde_json::to_string(
&(0..129)
.map(|i| format!("u{i}.service"))
.collect::<Vec<_>>(),
)
.unwrap();
assert!(parse_log_units(Some(&too_many)).is_err());
}
#[test]
fn classification_cannot_be_bypassed_by_another_scope_or_empty_env() {
let (mut scope, job, _) = fixture();
scope
.root_logs
.as_mut()
.unwrap()
.sensitive_units
.push("rmm-acceptance.service".into());
let mut policy = RootPolicy {
schema_version: 1,
profile: "development".into(),
expires_at: Utc::now() + chrono::Duration::hours(1),
scopes: vec![scope],
};
let (mut other, _, _) = fixture();
other.tenant_id = job.tenant_id;
other.asset_id = job.asset_id;
other.agent_id = job.agent_id;
other.principals = vec!["another-actor".into()];
policy.scopes.push(other);
assert!(policy.sensitive_log_unit(&job, "rmm-acceptance.service", Some(&[])));
for unit in [
"ssh.service",
"sshd.service",
"sshd@fixture.service",
"auditd.service",
"systemd-logind.service",
] {
assert!(policy.sensitive_log_unit(&job, unit, Some(&[])));
}
assert!(policy.sensitive_log_unit(&job, "custom.service", Some(&["custom.service".into()])));
assert!(!policy.sensitive_log_unit(&job, "ordinary.service", None));
}
#[test]
fn permissions_are_separate_root_read_and_sensitive_requirements() {
let (_, job, _) = fixture();
let normal = required_permissions(&job, false);
assert_eq!(normal, vec!["root:execute", "logs:read"]);
let sensitive = required_permissions(&job, true);
assert_eq!(
sensitive,
vec!["root:execute", "logs:read", "logs:read_sensitive"]
);
for rights in [
vec!["inventory:read"],
vec!["logs:read"],
vec!["root:execute"],
] {
assert!(!normal.iter().all(|p| rights.contains(p)));
}
let operator = ["root:execute", "logs:read"];
assert!(normal.iter().all(|p| operator.contains(p)));
assert!(!sensitive.iter().all(|p| operator.contains(p)));
let privileged = ["root:execute", "logs:read", "logs:read_sensitive"];
assert!(sensitive.iter().all(|p| privileged.contains(p)));
}
#[test]
fn unknown_log_profile_version_denies() {
let (mut scope, job, caps) = fixture();
scope.root_logs.as_mut().unwrap().schema_version = 2;
assert!(!scope.permits("selected", &job, &caps, None));
}
+164
View File
@@ -0,0 +1,164 @@
#!/usr/bin/env python3
"""Real mTLS sync/signature/checkpoint/key/inventory denials, no approvals/effects."""
import datetime
import hashlib
import json
import os
from pathlib import Path
import ssl
import subprocess
import tempfile
import uuid
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
from local_integration import LocalHTTPS, CA, STATE, A, B, field, envelope, token, sql, check
from backend_transport import fields, batch, csr, context, exchange
from backend_contract import request
def get(path, ctx, headers=None):
c=LocalHTTPS("rmm-dev.internal",18443,context=ctx,timeout=10)
c.request("GET",path,headers=headers or {})
r=c.getresponse(); result=(r.status,r.read());c.close();return result
def verify(_folder, key, payload, signature):
# Host CLI is OpenSSL 1.1.1, whose pkeyutl lacks -rawin. Verify with the
# installed independent Python Ed25519 implementation, not a skipped check.
try:
Ed25519PublicKey.from_public_bytes(bytes.fromhex(key["public_key_hex"])).verify(signature,payload)
return True
except (InvalidSignature,ValueError):
return False
class Flow:
def __init__(self, agent, ctx):
self.agent,self.ctx,self.sequence,self.generation=agent,ctx,0,0
def send(self, tag, body):
self.sequence+=1
return exchange("/agent/v1/messages",batch(envelope(self.agent,self.sequence,self.generation,tag,body)),self.ctx)
def hello(self):
self.generation=0
status,raw=self.send(10,field(1,"sync-fixture")+field(2,"rmm.sync.v1")+field(2,"run_script"))
assert status==200,"sync-aware hello failed"
self.generation=fields(fields(fields(raw)[2][0])[11][0])[1][0]
def sync(self, body):
status,raw=self.send(27,body)
if status!=200:return status,None,None
result=fields(fields(fields(raw)[2][0])[28][0])
return status,result[1][0],result[2][0]
def sync_body(journal, checkpoint, previous="", request_id=None, previous_request="",helper=None,helper_previous="",attempts=()):
request_id=request_id or str(uuid.uuid4())
body=field(1,request_id)+field(2,journal)+field(3,checkpoint)
if previous:body+=field(4,previous)
body+=field(5,1)+b"".join(field(6,attempt) for attempt in attempts)+field(8,1)
if helper:body+=field(9,helper)+field(10,checkpoint)
if helper_previous:body+=field(11,helper_previous)
if previous_request:body+=field(12,previous_request)
return request_id,body
def snapshot(value):
raw=json.dumps(value,separators=(",",":")).encode()
return b"".join([field(1,str(uuid.uuid4())),field(2,"linux.base"),field(3,1),field(5,1),field(6,raw),field(7,hashlib.sha256(raw).digest()),field(8,"partial")])
def main():
with tempfile.TemporaryDirectory(prefix="backend-sync-",dir=STATE) as folder:
folder=Path(folder)
_,_,enrolled=request("/agent/v1/enroll","POST",{"token":token(A),"csr_pem":csr(folder,"device")})
agent,asset=enrolled["agent_id"],enrolled["asset_id"]
ctx=context(folder,"device",enrolled["certificate_pem"])
flow=Flow(agent,ctx)
try:
status,raw=get("/api/v1/job-signing-key",ctx);key=json.loads(raw)
check(status==200 and set(key)=={"key_id","algorithm","schema_version","public_key_hex"} and key["algorithm"]=="Ed25519" and key["schema_version"]==1 and hashlib.sha256(bytes.fromhex(key["public_key_hex"])).hexdigest()==key["key_id"],"registered device mTLS bootstrap gets exact stable signing-key profile")
check(get("/api/v1/job-signing-key",ctx,{"Authorization":"Bearer invalid"})[0]==401,"explicit invalid bearer cannot fall back to mTLS key identity")
flow.hello()
check(sql(f"SELECT sync_required AND NOT sync_ready FROM agent_installations WHERE agent_id='{agent}';")=="t","negotiated sync gate closes on connection establishment")
journal,helper=str(uuid.uuid4()),str(uuid.uuid4())
rid,body=sync_body(journal,1,helper=helper)
status,payload,signature=flow.sync(body);assert status==200
decision=json.loads(payload)
check(decision["ready"] and decision["helper_ready"] and decision["tenant_id"]==A and decision["asset_id"]==asset and decision["agent_id"]==agent and decision["connection_generation"]==flow.generation and decision["request_hash"]==hashlib.sha256(body).hexdigest(),"authoritative empty-journal sync opens only bound current generation")
check(verify(folder,key,payload,signature),"independent Ed25519 verification of exact reconcile decision bytes")
check(not verify(folder,key,payload+b" ",signature),"modified reconcile decision bytes fail independent verification")
status,replayed,replayed_signature=flow.sync(body)
check(status==200 and replayed==payload and replayed_signature==signature,"same sync ID/content returns one persisted signed checkpoint")
_,changed=sync_body(journal,2,request_id=rid,helper=helper)
check(flow.sync(changed)[0]==409,"same sync ID with changed checkpoint denied")
prior_generation=flow.generation;flow.hello()
check(flow.generation>prior_generation and sql(f"SELECT sync_ready FROM agent_installations WHERE agent_id='{agent}';")=="f","clean reconnect fences old readiness until a fresh decision")
check(flow.sync(body)[0]==409,"old-generation signed checkpoint cannot be replayed as current readiness")
_,body2=sync_body(journal,2,decision["decision_id"],helper=helper,helper_previous=decision["decision_id"])
status,payload2,sig2=flow.sync(body2);assert status==200
decision2=json.loads(payload2)
check(decision2["ready"] and decision2["helper_ready"] and verify(folder,key,payload2,sig2),"consistent agent/helper journals regain readiness after clean reconnect")
lost_id,lost=sync_body(journal,3,decision2["decision_id"],helper=helper,helper_previous=decision2["decision_id"])
status,lost_payload,_=flow.sync(lost);assert status==200
lost_decision=json.loads(lost_payload)
flow.hello()
_,recovery=sync_body(journal,3,previous_request=lost_id,helper=helper,helper_previous=decision2["decision_id"])
status,payload3,_=flow.sync(recovery);assert status==200
recovered=json.loads(payload3)
check(recovered["ready"],"lost response recovery uses durable previous request without replacement grant")
now=datetime.datetime.now(datetime.timezone.utc).isoformat()
inventory={"os":"Linux fixture","hostname":"synthetic-sync","architecture":"x86_64","root_total_bytes":2,"root_available_bytes":1,"root_available_inodes":1,"collection_started_at":now,"collection_completed_at":now,"observations":[{"scope":"linux.smart","source_id":"linux.smart","observed_at":now,"quality":"unsupported","completeness":"unknown","truncated":False,"error_code":"NOT_SUPPORTED","data":None}]}
check(flow.send(13,snapshot(inventory))[0]==200,"additive RFC3339 observation metadata accepted through durable mTLS ingest")
operator=(STATE/"operator-a.token").read_text().strip()
stored=request(f"/api/v1/tenants/{A}/devices/{asset}",token=operator)[2]["inventory"]
check(stored["observations"]==inventory["observations"],"stored extended metadata retains unsupported/unknown distinctions exactly")
inventory["observations"][0]["data"]="x"*32768
check(flow.send(13,snapshot(inventory))[0]==409,"oversized individual observation fails before durable ACK")
inventory["observations"][0]["data"]=None;inventory["observations"][0]["received_at"]=now
check(flow.send(13,snapshot(inventory))[0]==409,"device cannot assert server receipt metadata")
artifact=str(uuid.uuid4());job=str(uuid.uuid4());attempt=str(uuid.uuid4())
sql(f"INSERT INTO artifacts(tenant_id,artifact_id,sha256,size_bytes,media_type) VALUES('{B}','{artifact}','{hashlib.sha256(b'foreign fixture').hexdigest()}',15,'application/octet-stream');")
path=f"/agent/v1/artifacts/{artifact}?job_id={job}&attempt_id={attempt}"
check(get(path,ctx)[0]==404,"device cannot retrieve an unapproved or foreign artifact reference")
check(get(path,ctx,{"Authorization":"Bearer "+operator})[0]==401,"agent artifact route cannot switch to bearer authority")
check(flow.send(23,field(1,b'{"execution_identity":"root"}')+field(2,b'x'*64))[0]==409,"agent-supplied unsigned execution permit is never server authority")
archive=b"synthetic sealed archive bytes "+uuid.uuid4().hex.encode();archive_hash=hashlib.sha256(archive).hexdigest();own_artifact=str(uuid.uuid4())
archive_dir=STATE/"jobs"/"artifacts"/A;archive_dir.mkdir(mode=0o700,parents=True,exist_ok=True)
with open(os.open(archive_dir/archive_hash,os.O_CREAT|os.O_EXCL|os.O_WRONLY,0o600),"wb") as f:f.write(archive)
sql(f"INSERT INTO artifacts(tenant_id,artifact_id,sha256,size_bytes,media_type) VALUES('{A}','{own_artifact}','{archive_hash}',{len(archive)},'application/octet-stream');")
when=datetime.datetime.now(datetime.timezone.utc)
plan_input={"targets":{"asset_ids":[asset]},"action":{"action":"run_script","interpreter":"posix_sh","script_utf8":"true\n","arguments":[],"expected_stdout":""},"execution_identity":"agent_service","schedule":{"not_before":when.isoformat(),"start_before":(when+datetime.timedelta(hours=1)).isoformat()},"max_runtime_seconds":60,"max_output_bytes":1024,"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"artifact reference fixture; never execute","artifact_ids":[own_artifact]}
status,_,plan=request(f"/api/v1/tenants/{A}/operation-plans","POST",plan_input,operator);assert status==200
check(plan["plan_payload"]["artifact_references"][0]["artifact"]["sha256"]==archive_hash and hashlib.sha256(bytes(plan["plan_payload_bytes"])).hexdigest()==plan["plan_hash"],"plan binds verified actual artifact bytes to fixed job/attempt")
reserved=plan["jobs"][0]
scoped_path=f"/agent/v1/artifacts/{own_artifact}?job_id={reserved['job_id']}&attempt_id={reserved['attempt_id']}"
check(get(scoped_path,ctx)[0]==404,"reserved plan IDs do not authorize artifact access before approval/job creation")
_,_,missing_device=request("/agent/v1/enroll","POST",{"token":token(A),"csr_pem":csr(folder,"missing")})
missing_agent=missing_device["agent_id"]
try:
missing=Flow(missing_agent,context(folder,"missing",missing_device["certificate_pem"]));missing.hello();missing_journal=str(uuid.uuid4())
fake=field(1,reserved["job_id"])+field(2,reserved["attempt_id"])+field(3,reserved["job_payload_hash"])+field(5,"unconsumed")+field(6,"accepted")
_,report=sync_body(missing_journal,1,attempts=[fake]);status,fault,_=missing.sync(report);assert status==200;fault=json.loads(fault)
check(not fault["ready"] and sql(f"SELECT sync_recovery_required FROM agent_installations WHERE agent_id='{missing_agent}';")=="t","missing/unapproved server job creates durable non-executable reconciliation fault")
_,omit=sync_body(missing_journal,2,fault["decision_id"]);status,blocked,_=missing.sync(omit)
check(status==200 and not json.loads(blocked)["ready"],"omitting an unresolved missing attempt cannot reset readiness")
finally:sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{missing_agent}';")
# Only own synchronization metadata is removed to simulate a restored
# server checkpoint. No jobs, approvals, grants, or VM records touched.
sql(f"DELETE FROM agent_sync_state WHERE agent_id='{agent}';")
_,rollback=sync_body(journal,4,recovered["decision_id"],helper=helper,helper_previous=recovered["decision_id"])
status,denied,_=flow.sync(rollback);assert status==200
check(not json.loads(denied)["ready"] and sql(f"SELECT restore_blocked FROM agent_installations WHERE agent_id='{agent}';")=="t","missing authoritative server checkpoint persists fail-closed restore gate")
_,blank=sync_body(str(uuid.uuid4()),1)
check(not json.loads(flow.sync(blank)[1])["ready"],"fresh empty journal cannot bypass known server checkpoint history")
flow.sequence=0;flow.hello()
_,regressed=sync_body(journal,5)
check(not json.loads(flow.sync(regressed)[1])["ready"] and sql(f"SELECT sequence_regressed FROM agent_installations WHERE agent_id='{agent}';")=="t","journal sequence rollback never yields blind readiness")
check(sql(f"SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE j.agent_id='{agent}';")=="0","sync never creates execution grants")
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
check(get("/api/v1/job-signing-key",ctx)[0]==401,"revoked device cannot bootstrap the signing key")
finally:
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print("Backend sync/key/inventory/restore tests passed locally; no approvals or VM/root effects executed.")
if __name__=="__main__":main()
+216
View File
@@ -0,0 +1,216 @@
#!/usr/bin/env python3
"""Actual local mTLS HTTPS/WSS, rotation/fencing and independent housekeeping.
No VM access, approval, execution, or mutable existing-device fixtures.
"""
import datetime
import hashlib
import json
from pathlib import Path
import ssl
import subprocess
import tempfile
import time
import uuid
from local_integration import LocalHTTPS, WebSocket, field, envelope, token, sql, check, CA, STATE, A, B
from backend_contract import request
def fields(data):
result = {}
offset = 0
def integer():
nonlocal offset
value = shift = 0
while True:
assert offset < len(data) and shift < 70, "malformed protobuf integer"
byte = data[offset]; offset += 1
value |= (byte & 127) << shift
if byte < 128:
return value
shift += 7
while offset < len(data):
key = integer(); tag, kind = key >> 3, key & 7
if kind == 0:
value = integer()
else:
assert kind == 2, "unsupported protobuf fixture field"
length = integer(); value = data[offset:offset+length]; offset += length
assert offset <= len(data), "malformed protobuf fixture length"
result.setdefault(tag, []).append(value)
return result
def exchange(path, body, context, content_type="application/x-protobuf"):
c = LocalHTTPS("rmm-dev.internal", 18443, context=context, timeout=30)
c.request("POST", path, body=body, headers={"Content-Type": content_type})
r = c.getresponse(); status, raw = r.status, r.read(); c.close()
return status, raw
def batch(e):
return field(1, 1) + field(2, e)
def csr(folder, name):
subprocess.run(["openssl", "req", "-new", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:prime256v1", "-nodes", "-keyout", str(folder/(name+".key")), "-out", str(folder/(name+".csr")), "-subj", "/CN=backend-transport-fixture"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return (folder/(name+".csr")).read_text()
def context(folder, name, pem):
(folder/(name+".pem")).write_text(pem)
ctx = ssl.create_default_context(cafile=str(CA))
ctx.load_cert_chain(folder/(name+".pem"), folder/(name+".key"))
return ctx
def hello(agent, sequence, ctx):
status, raw = exchange("/agent/v1/messages", batch(envelope(agent, sequence, 0, 10, field(1, "backend-transport")+field(2, "run_script"))), ctx)
assert status == 200, "HTTPS hello failed"
response = fields(fields(raw)[2][0])
return fields(response[11][0])[1][0]
def mtls_get(path, ctx):
client=LocalHTTPS("rmm-dev.internal",18443,context=ctx,timeout=10)
try:
client.request("GET",path)
response=client.getresponse();response.read();return response.status
finally:
client.close()
def certificate_hash(pem):
return hashlib.sha256(ssl.PEM_cert_to_DER_cert(pem)).hexdigest()
def public_device(asset, viewer):
status,_,device=request(f"/api/v1/tenants/{A}/devices/{asset}",token=viewer)
assert status==200,"own public device detail unavailable"
forbidden={"private_key","private_key_pem","csr_pem","certificate_pem","token","enrollment_token","renewal_csr_sha256","password"}
assert not forbidden.intersection(device),"private enrollment/credential fields leaked into device DTO"
return device
def main():
trust = ssl.create_default_context(cafile=str(CA))
with tempfile.TemporaryDirectory(prefix="backend-https-", dir=STATE) as folder:
folder = Path(folder)
initial = csr(folder, "old")
status, _, enrolled = request("/agent/v1/enroll", "POST", {"token": token(A), "csr_pem": initial})
assert status == 200
agent, asset = enrolled["agent_id"], enrolled["asset_id"]
viewer=(STATE/"viewer-a.token").read_text().strip()
viewer_b=(STATE/"viewer-b.token").read_text().strip()
old_hash=certificate_hash(enrolled["certificate_pem"])
old = context(folder, "old", enrolled["certificate_pem"])
ws = None
try:
initial_device=public_device(asset,viewer)
check(initial_device["certificate_sha256"]==old_hash and initial_device["last_authenticated_certificate_sha256"] is None and initial_device["last_authenticated_at"] is None,"issued certificate pin is public but issuance does not invent TLS-use evidence")
cursor=None;listed=None
for _ in range(100):
path=f"/api/v1/tenants/{A}/devices?limit=500"+("&cursor="+cursor if cursor else "")
list_status,_,page=request(path,token=viewer);assert list_status==200
listed=next((item for item in page["items"] if item["agent_id"]==agent),None)
if listed or not page["next_cursor"]:break
cursor=page["next_cursor"]
check(listed is not None and listed["certificate_sha256"]==old_hash and listed["last_authenticated_certificate_sha256"] is None,"device list exposes the same public certificate evidence contract")
check(request(f"/api/v1/tenants/{A}/devices/{asset}",token=viewer_b)[0]==404 and request(f"/api/v1/tenants/{B}/devices/{asset}",token=viewer_b)[0]==404,"certificate evidence remains tenant-scoped and foreign assets are 404")
check(exchange("/agent/v1/messages", batch(envelope(agent,1,0,10,field(1,"x"))),trust)[0] == 401, "HTTPS messages require real mTLS identity")
check(public_device(asset,viewer)["last_authenticated_at"] is None,"anonymous/forged device claim creates no authentication evidence")
generation = hello(agent, 1, old)
check(generation > 0, "HTTPS hello selects fenced connection generation")
authenticated=public_device(asset,viewer)
check(authenticated["last_authenticated_certificate_sha256"]==old_hash and authenticated["last_authenticated_at"] is not None and authenticated["last_contact_at"] is None,"actual old-certificate mTLS Hello records authentication, not heartbeat presence")
heartbeat = envelope(agent, 2, generation, 12, field(1, str(uuid.uuid4())))
status, raw = exchange("/agent/v1/messages", batch(heartbeat), old)
check(status == 200 and 14 in fields(fields(raw)[2][0]), "HTTPS heartbeat durable contract returns concrete ACK")
before = sql(f"SELECT last_contact_at FROM agent_installations WHERE agent_id='{agent}';")
check(exchange("/agent/v1/messages", batch(heartbeat), old)[0] == 409, "HTTPS fresh heartbeat replay denied")
check(sql(f"SELECT last_contact_at FROM agent_installations WHERE agent_id='{agent}';") == before, "replayed HTTPS heartbeat never refreshes presence")
check(exchange("/agent/v1/messages", b"{}", old, "application/json")[0] == 415, "HTTPS protobuf content type enforced")
check(exchange("/agent/v1/messages", field(1,1)+b"".join(field(2,heartbeat) for _ in range(17)), old)[0] == 400, "HTTPS batch count bounded before ingest")
fresh = envelope(agent,3,generation,12,field(1,str(uuid.uuid4())))
started = time.monotonic()
status, raw = exchange("/agent/v1/poll",field(1,fresh)+field(2,1),old)
check(status == 200 and len(fields(raw)[2]) == 1 and time.monotonic()-started >= .8, "bounded long poll returns an empty-work ACK after wait")
# A real WSS connection takes ownership from HTTPS.
ws = WebSocket(old); assert ws.status == 101
ws.send(envelope(agent,4,0,10,field(1,"backend-transport")))
_, wire = ws.receive(); wss_generation = fields(fields(wire)[11][0])[1][0]
check(wss_generation > generation, "WSS transport takeover increments generation")
check(exchange("/agent/v1/messages",batch(envelope(agent,5,generation,12,field(1,str(uuid.uuid4())))),old)[0] == 409, "old HTTPS channel is fenced by WSS takeover")
generation = hello(agent,6,old)
ws.send(envelope(agent,7,wss_generation,12,field(1,str(uuid.uuid4()))))
check(ws.receive()[0] == 8, "HTTPS takeover fences stale WSS control traffic")
ws.close(); ws = None
# The scheduler must expire a pending request without any agent poll.
operator=(STATE/"operator-a.token").read_text().strip()
now=datetime.datetime.now(datetime.timezone.utc)
plan_input={"targets":{"asset_ids":[asset]},"action":{"action":"run_script","interpreter":"posix_sh","script_utf8":"true\n","arguments":[],"expected_stdout":""},"execution_identity":"agent_service","schedule":{"not_before":now.isoformat(),"start_before":(now+datetime.timedelta(seconds=4)).isoformat()},"max_runtime_seconds":1,"max_output_bytes":1024,"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"offline housekeeping fixture; never execute"}
base=f"/api/v1/tenants/{A}"
status,_,plan=request(base+"/operation-plans","POST",plan_input,operator); assert status == 200
status,_,pending=request(base+f"/operation-plans/{plan['plan_id']}/submit","POST",{"idempotency_key":"housekeeping-"+uuid.uuid4().hex},operator); assert status == 200
deadline=time.monotonic()+8
while time.monotonic()<deadline:
expired=sql(f"SELECT approval_state FROM approval_requests WHERE operation_id='{pending['operation_id']}';")
if expired == "expired": break
time.sleep(.25)
check(expired == "expired", "background scheduler expires offline pending request without read/poll side effects")
check(sql(f"SELECT count(*) FROM jobs WHERE operation_id='{pending['operation_id']}';") == "0", "expiry never creates executable jobs")
# Renewing keys does not replace asset or enrollment generation.
contact_before_renewal=public_device(asset,viewer)["last_contact_at"]
check(mtls_get("/api/v1/job-signing-key",old)==200,"device key bootstrap authenticates the actual TLS peer")
check(public_device(asset,viewer)["last_contact_at"]==contact_before_renewal,"key GET does not refresh heartbeat presence")
missing_artifact=f"/agent/v1/artifacts/{uuid.uuid4()}?job_id={uuid.uuid4()}&attempt_id={uuid.uuid4()}"
check(mtls_get(missing_artifact,old)==404 and public_device(asset,viewer)["last_contact_at"]==contact_before_renewal,"artifact authentication does not refresh heartbeat presence")
renewal=csr(folder,"new")
status,raw=exchange("/agent/v1/certificates/renew",json.dumps({"csr_pem":renewal}).encode(),old,"application/json")
check(status == 200, "current mTLS identity can rotate a valid new CSR")
renewed=json.loads(raw); new=context(folder,"new",renewed["certificate_pem"])
new_hash=certificate_hash(renewed["certificate_pem"])
issued_new=public_device(asset,viewer)
check(new_hash!=old_hash and issued_new["certificate_sha256"]==new_hash and issued_new["last_authenticated_certificate_sha256"]==old_hash,"renewal updates issued pin while actual authenticated peer remains the old certificate")
check(issued_new["last_contact_at"]==contact_before_renewal,"certificate renewal does not refresh heartbeat presence")
status,repeated=exchange("/agent/v1/certificates/renew",json.dumps({"csr_pem":renewal}).encode(),old,"application/json")
check(status == 200 and json.loads(repeated)["certificate_pem"] == renewed["certificate_pem"], "lost renewal response can be recovered idempotently during overlap")
overlap=public_device(asset,viewer)
check(overlap["certificate_sha256"]==new_hash and overlap["last_authenticated_certificate_sha256"]==old_hash,"lost-response recovery using old overlap is not misreported as new-certificate use")
check(sql(f"SELECT asset_id::text||':'||enrollment_generation FROM agent_installations WHERE agent_id='{agent}';") == asset+":1", "certificate rotation preserves device assignment/identity generation")
generation=hello(agent,8,new)
used_new=public_device(asset,viewer)
check(used_new["certificate_sha256"]==new_hash and used_new["last_authenticated_certificate_sha256"]==new_hash and used_new["enrollment_generation"]==1 and used_new["agent_id"]==agent,"verified new-certificate Hello changes actual peer evidence without identity replacement")
check(datetime.datetime.fromisoformat(used_new["last_authenticated_at"].replace("Z","+00:00"))>=datetime.datetime.fromisoformat(overlap["last_authenticated_at"].replace("Z","+00:00")) and used_new["last_contact_at"]==contact_before_renewal,"new peer authentication has a separate time and cannot fake a heartbeat")
sql(f"UPDATE agent_installations SET previous_certificate_until=now()-interval '1 second' WHERE agent_id='{agent}';")
check(exchange("/agent/v1/messages",batch(envelope(agent,9,0,10,field(1,"old"))),old)[0] == 401, "expired previous certificate binding rejected without disabling TLS")
check(public_device(asset,viewer)["last_authenticated_at"]==used_new["last_authenticated_at"],"rejected expired-overlap certificate cannot change authentication evidence")
subprocess.run(["openssl","req","-x509","-newkey","ec","-pkeyopt","ec_paramgen_curve:prime256v1","-nodes","-keyout",str(folder/"invalid.key"),"-out",str(folder/"invalid.pem"),"-days","1","-subj","/CN=untrusted-client-fixture"],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
invalid=ssl.create_default_context(cafile=str(CA));invalid.load_cert_chain(folder/"invalid.pem",folder/"invalid.key")
try:
invalid_status=mtls_get("/api/v1/job-signing-key",invalid)
assert invalid_status==401,"untrusted client certificate was accepted"
except (ssl.SSLError,ConnectionResetError,ConnectionAbortedError,BrokenPipeError):
pass
check(public_device(asset,viewer)["last_authenticated_at"]==used_new["last_authenticated_at"],"TLS-invalid client certificate cannot create authentication evidence")
hello(agent,1,new)
check(sql(f"SELECT restore_blocked FROM agent_installations WHERE agent_id='{agent}';") == "t", "regressed journal sequence persistently blocks new effects for reconciliation")
ws=WebSocket(new); assert ws.status == 101
ws.send(envelope(agent,10,0,10,field(1,"revocation-fixture"))); assert ws.receive()[0] == 2
before_revocation=public_device(asset,viewer)
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
ws.sock.settimeout(4)
check(ws.receive()[0] == 8, "persistent revocation actively closes idle WSS channel")
check(exchange("/agent/v1/messages",batch(envelope(agent,11,0,10,field(1,"revoked"))),new)[0] == 401, "revoked identity cannot reconnect using rotated certificate")
after_revocation=public_device(asset,viewer)
check(after_revocation["last_authenticated_at"]==before_revocation["last_authenticated_at"] and after_revocation["last_authenticated_certificate_sha256"]==new_hash,"revoked peer cannot overwrite retained public authentication evidence")
check(sql(f"SELECT count(*) FROM agent_installations WHERE agent_id='{agent}';","rmm_app")=="0","certificate evidence remains RLS-protected without tenant context")
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); UPDATE agent_installations SET last_authenticated_at=now() WHERE agent_id='{agent}';","rmm_app",fail=True)
print("PASS application has no general UPDATE grant on authentication-evidence columns")
finally:
if ws: ws.close()
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
print("Backend transport/rotation/housekeeping passed locally; no approval, execution or VM acceptance performed.")
if __name__ == "__main__":
main()
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
"""Fresh private configuration/PKI test, independent of running containers."""
import importlib.util
import os
from pathlib import Path
import ssl
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("rmm_dev", ROOT / "deploy/dev.py")
dev = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dev)
os.umask(0o077)
with tempfile.TemporaryDirectory(prefix="rmm-bootstrap-", dir=ROOT / "devstate") as folder:
dev.STATE = Path(folder) / "state"
# A broken host configuration must not influence the generated PKI.
hostile = Path(folder) / "host.cnf"
hostile.write_text("this is not an OpenSSL configuration\n")
os.environ["OPENSSL_CONF"] = str(hostile)
dev.init()
for name in (".env", "viewer-a.token", "viewer-b.token", "pki/ca.key", "pki/server.key"):
assert (dev.STATE / name).stat().st_mode & 0o777 == 0o600
assert dev.STATE.stat().st_mode & 0o777 == 0o700
assert (dev.STATE / "init.sql").stat().st_mode & 0o777 == 0o444
# Both OpenSSL (including 1.1.1 hosts) and the system TLS parser must accept CA.
env = dict(os.environ, OPENSSL_CONF=str(dev.OPENSSL_CONFIG))
subprocess.run(["openssl", "verify", "-purpose", "sslserver", "-CAfile", str(dev.STATE / "pki/ca.pem"), "-verify_hostname", "rmm-dev.internal", str(dev.STATE / "pki/server.pem")], env=env, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
for name in ("ca", "server"):
text = subprocess.check_output(["openssl", "x509", "-in", str(dev.STATE / f"pki/{name}.pem"), "-noout", "-text"], env=env, text=True)
assert text.count("X509v3 Basic Constraints:") == 1
assert text.count("X509v3 Key Usage:") == 1
# Exercise the isolated client role as well; runtime enrollment is tested separately.
for args in (
["req", "-new", "-config", str(dev.OPENSSL_CONFIG), "-key", str(dev.STATE / "pki/server.key"), "-out", str(Path(folder) / "client.csr"), "-subj", "/CN=bootstrap-client"],
["x509", "-req", "-in", str(Path(folder) / "client.csr"), "-CA", str(dev.STATE / "pki/ca.pem"), "-CAkey", str(dev.STATE / "pki/ca.key"), "-set_serial", "42", "-out", str(Path(folder) / "client.pem"), "-days", "1", "-extfile", str(dev.OPENSSL_CONFIG), "-extensions", "client"],
["verify", "-purpose", "sslclient", "-CAfile", str(dev.STATE / "pki/ca.pem"), str(Path(folder) / "client.pem")],
):
subprocess.run(["openssl", *args], env=env, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
ssl.create_default_context(cafile=str(dev.STATE / "pki/ca.pem"))
try:
dev.init()
except SystemExit:
pass
else:
raise AssertionError("must not overwrite private development state")
print("PASS fresh configuration/CA/SAN verification, permissions and overwrite refusal")
+38
View File
@@ -0,0 +1,38 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { readFileSync, statSync } from 'node:fs';
test('dev/test browser simulation: login → fixed plan → pending → explicit click → result', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'execute', 'Explicit dev/test execution opt-in required; never run against production');
const file = process.env.RMM_E2E_LOGIN_FILE;
if (!file) throw new Error('Protected login JSON file required');
const stat = statSync(file);
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.()) throw new Error('Login file must be owned by current user, mode 0600');
const value: unknown = JSON.parse(readFileSync(file, 'utf8'));
if (!value || typeof value !== 'object' || !('username' in value) || !('password' in value) || typeof value.username !== 'string' || typeof value.password !== 'string') throw new Error('Invalid login file');
const asset = process.env.RMM_E2E_ASSET_ID;
if (!asset) throw new Error('Explicit approved test asset required');
// No APIRequestContext, direct approval request, storageState, screenshots or traces.
await page.goto('/');
await page.getByLabel('Benutzername').fill(value.username);
await page.getByLabel('Passwort', { exact: true }).fill(value.password);
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
await expect(page.getByRole('status')).toContainText('SIMULIERT');
await expect(page.getByRole('status')).toContainText('kein menschlicher Produktions-Freigabenachweis');
await page.getByRole('checkbox', { name: `Ziel ${asset}`, exact: true }).check();
await page.getByLabel('Exakte UTF-8 Skriptbytes', { exact: false }).fill("printf '%s\\n' 'Hello World'\n");
await page.getByLabel('Erwartete stdout-Ausgabe', { exact: false }).fill('Hello World\n');
await page.getByLabel('Begründung', { exact: true }).fill('Dev/test browser simulation · Hello World');
await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click();
const approve = page.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen' });
await expect(approve).toBeDisabled();
await page.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click();
await expect(page.getByText('Antrag: pending_approval', { exact: false })).toBeVisible();
await page.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
await expect(approve).toBeEnabled();
await approve.click(); // The only approval path: visible, interactive UI action.
await expect(page.getByText('Antrag: approved', { exact: false })).toBeVisible();
await expect(page.getByText('Ausführung: succeeded', { exact: false })).toBeVisible({ timeout: 90000 });
await page.getByRole('button', { name: /^Job / }).first().click();
await expect(page.getByRole('heading', { name: /^Job / })).toBeVisible();
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
});
+7
View File
@@ -0,0 +1,7 @@
export const diagnosticSteps = ['management:login-banner', 'management:catalog-response', 'management:catalog-heading', 'management:membership-heading', 'management:unknown-stop', 'management:action-option', 'management:select-native', 'management:native-hint', 'management:identity-unchanged', 'management:inert-controls', 'management:logout'] as const;
export function diagnostic(label: string, error: { name?: string; message?: string } | undefined, line: number | undefined): string | null {
if (!error || !diagnosticSteps.some(step => step === label)) return null;
const name = ['Error', 'TimeoutError', 'TypeError', 'AssertionError', 'TestTimeoutError'].includes(error.name ?? '') ? error.name : 'Error';
const kind = /Timeout|timed out|timeout/i.test(error.message ?? '') ? 'timeout' : /expect\(|Assertion/i.test(error.message ?? '') ? 'assertion' : 'exception';
return `failure_step=${label} exception=${name} kind=${kind} source_line=${Number.isSafeInteger(line) && Number(line) > 0 ? line : 'unknown'}`;
}
+23
View File
@@ -0,0 +1,23 @@
import type { Page } from '../../web/node_modules/@playwright/test/index.js';
import { expect } from '../../web/node_modules/@playwright/test/index.js';
import { readFileSync, statSync } from 'node:fs';
export function loginData(): { username: string; password: string } {
if (process.env.RMM_E2E_PROFILE !== 'dev/test') throw new Error('Explicit dev/test profile required');
const file = process.env.RMM_E2E_LOGIN_FILE;
if (!file) throw new Error('Protected login JSON file required');
const stat = statSync(file);
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.()) throw new Error('Login file must be owned by current user, mode 0600');
const value: unknown = JSON.parse(readFileSync(file, 'utf8'));
if (!value || typeof value !== 'object' || !('username' in value) || !('password' in value) || typeof value.username !== 'string' || typeof value.password !== 'string') throw new Error('Invalid login file');
return { username: value.username, password: value.password };
}
export async function login(page: Page) {
const value = loginData();
await page.goto('/');
await page.getByLabel('Benutzername').fill(value.username);
await page.getByLabel('Passwort', { exact: true }).fill(value.password);
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
await expect(page.getByRole('status')).toContainText('SIMULIERT');
await expect(page.getByRole('status')).toContainText('kein menschlicher Produktions-Freigabenachweis');
}
+22
View File
@@ -0,0 +1,22 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { login } from './helpers';
test('real TLS login follows announced configuration and restores session without invented simulation identity', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'management-read');
const configResponse = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/config');
await page.goto('/');
const response = await configResponse; expect(response.status()).toBe(200);
const config: unknown = await response.json();
if (!config || typeof config !== 'object' || !('oidc_configured' in config) || !('dev_login_enabled' in config) || !('oidc_login_url' in config)) throw new Error('Auth configuration schema missing');
expect(typeof config.oidc_configured).toBe('boolean'); expect(typeof config.dev_login_enabled).toBe('boolean');
if (config.oidc_configured) {
expect(config.oidc_login_url).toBe('/auth/oidc/login'); await expect(page.getByRole('link', { name: 'Mit SSO anmelden', exact: true })).toHaveAttribute('href', '/auth/oidc/login');
} else {
expect(config.oidc_login_url).toBeNull(); await expect(page.getByText('OIDC: not_configured', { exact: false })).toBeVisible(); await expect(page.getByRole('link', { name: 'Mit SSO anmelden', exact: true })).toHaveCount(0);
}
if (!config.dev_login_enabled) { await expect(page.locator('input[type="password"]')).toHaveCount(0); return; }
await expect(page.getByText('DEV/TEST · SIMULIERTER TESTPRINCIPAL', { exact: false })).toBeVisible();
await login(page); await page.reload(); await expect(page.getByRole('status')).toContainText('SIMULIERT');
await expect(page.getByRole('heading', { name: 'Mitgliedschaft und Ausführungsstopp' })).toBeVisible();
await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); await expect(page.getByRole('button', { name: 'Anmelden', exact: true })).toBeVisible();
});
+20
View File
@@ -0,0 +1,20 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { login } from './helpers';
test('real dev browser membership/catalog read and administrative controls stay inert', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'management-read');
let mutations = 0;
page.on('request', r => { if (r.method() === 'POST' && !['/auth/dev/login', '/auth/logout'].includes(new URL(r.url()).pathname)) mutations++; });
const catalog = page.waitForResponse(r => new URL(r.url()).pathname.endsWith('/package-recipes'));
await test.step('management:login-banner', async () => { await login(page); await expect(page.getByRole('status')).toContainText('SIMULIERT'); });
await test.step('management:catalog-response', async () => { expect((await catalog).status()).toBe(200); });
await test.step('management:catalog-heading', async () => { await expect(page.getByRole('heading', { name: 'Unveränderlicher Paketkatalog und Artefakte' })).toBeVisible(); });
await test.step('management:membership-heading', async () => { await expect(page.getByRole('heading', { name: 'Mitgliedschaft und Ausführungsstopp' })).toBeVisible(); });
await test.step('management:unknown-stop', async () => { await expect(page.getByText('unknown — kein lesender Stopstatus-Endpunkt angekündigt', { exact: false })).toBeVisible(); });
await test.step('management:action-option', async () => { await expect(page.getByLabel('Aktion', { exact: true }).locator('option[value="apt_native_acceptance"]')).toHaveCount(1); });
await test.step('management:select-native', async () => { await page.getByLabel('Aktion', { exact: true }).selectOption('apt_native_acceptance'); });
await test.step('management:native-hint', async () => { await expect(page.getByText('NUR SIMULIERTE DEV/TEST-NATIVABNAHME.', { exact: false })).toBeVisible(); });
await test.step('management:identity-unchanged', async () => { await expect(page.getByLabel('Ausführungsidentität', { exact: true })).toHaveValue('agent_service'); });
await test.step('management:inert-controls', async () => { expect(mutations).toBe(0); });
await test.step('management:logout', async () => { await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); await expect(page.getByRole('button', { name: 'Anmelden', exact: true })).toBeVisible(); });
});
+182
View File
@@ -0,0 +1,182 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { readFileSync, lstatSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { createHash } from 'node:crypto';
import { login } from './helpers';
function object(value: unknown): Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Object required');
return value as Record<string, unknown>;
}
function bytes(value: unknown): Buffer {
if (!Array.isArray(value) || !value.every(n => Number.isInteger(n) && n >= 0 && n <= 255)) throw new Error('Exact byte array required');
return Buffer.from(value);
}
function text(value: unknown): string { if (typeof value !== 'string' || !value) throw new Error('Text required'); return value; }
test('simulated human prepares root request through UI only, without approving', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'mvp-prepare');
const file = process.env.RMM_E2E_CASE_FILE;
if (!file) throw new Error('Protected root preparation file required');
const metadata = lstatSync(file);
if (!metadata.isFile() || metadata.isSymbolicLink() || metadata.uid !== process.getuid?.() || (metadata.mode & 0o777) !== 0o600 || metadata.size > 1048576) throw new Error('Unsafe root preparation file');
const expected = object(JSON.parse(readFileSync(file, 'utf8')));
expect(expected.phase).toBe('prepare'); expect(expected.profile).toBe('dev/test');
expect(expected.expected_principal).toBe('simulated-testprincipal-a');
const request = object(expected.request), params = object(request.parameters);
expect(params.execution_identity).toBe('root');
expect(object(request.targets).asset_ids).toEqual([expected.asset_id]);
expect(['run_script', 'set_service_state', 'apt_native_acceptance', 'ensure_package', 'apply_updates', 'reboot']).toContain(request.action);
let authorizationRequests = 0;
page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) authorizationRequests++; });
const sessionResponse = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200);
await login(page);
const session = object(await (await sessionResponse).json());
expect(session.testsimulation).toBe(true); expect(session.principal).toBe(expected.expected_principal);
expect(session.approval_origin).toBe('dev_test_browser_simulation');
await expect(page.getByLabel('Mandant', { exact: true })).toHaveValue(text(expected.tenant_id));
await page.getByRole('checkbox', { name: `Ziel ${text(expected.asset_id)}`, exact: true }).check();
await page.getByLabel('Aktion', { exact: true }).selectOption(text(request.action));
if (request.action === 'run_script') {
const variants = params.variants;
if (!Array.isArray(variants) || variants.length !== 1) throw new Error('One exact script variant required');
const variant = object(variants[0]);
await page.getByLabel('Interpreter', { exact: true }).selectOption(text(variant.interpreter));
await page.getByLabel('Exakte UTF-8 Skriptbytes', { exact: false }).fill(text(variant.script_utf8));
await page.getByLabel('Erwartete stdout-Ausgabe', { exact: false }).fill(text(params.expected_stdout));
} else {
await page.getByLabel('Aktionsparameter', { exact: true }).fill(JSON.stringify(params));
}
await page.getByLabel('Ausführungsidentität', { exact: true }).selectOption('root');
await page.getByLabel('Laufzeitlimit (s)', { exact: true }).fill(String(params.timeout_seconds));
await page.getByLabel('Ausgabelimit (Bytes)', { exact: true }).fill(String(params.max_output_bytes));
// Runner fixes browser/Node TZ to UTC. The actual UI's minute-precision form
// selects an explicit future minute; all resulting exact bytes are reviewed
// again in the separate approval test before any executable job can exist.
const start = new Date(Math.ceil(Date.now() / 60000) * 60000);
await page.getByLabel('Frühester Start (lokale Zeit)', { exact: true }).fill(start.toISOString().slice(0, 16));
await page.getByLabel('Startfrist / Ablauf (lokale Zeit)', { exact: true }).fill(new Date(start.getTime() + 15 * 60000).toISOString().slice(0, 16));
await page.getByLabel('Maximal parallel', { exact: true }).fill('1');
await page.getByLabel('Stopp nach Fehlern', { exact: true }).fill('1');
await page.getByLabel('Begründung', { exact: true }).fill(text(request.reason));
await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click();
const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) });
await expect(detail.locator('pre')).toBeVisible();
const plan = object(JSON.parse(await detail.locator('pre').innerText()));
expect(createHash('sha256').update(bytes(plan.plan_payload_bytes)).digest('hex')).toBe(plan.plan_hash);
await detail.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click();
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) });
await expect(op.locator('pre').first()).toContainText('"job_count": 0');
const operation = object(JSON.parse(await op.locator('pre').first().innerText()));
expect(operation.approval_state).toBe('pending_approval'); expect(operation.execution_state).toBeNull();
expect(authorizationRequests).toBe(0);
const output = resolve(text(expected.output_file));
if (dirname(output) !== dirname(resolve(file))) throw new Error('Private same-directory output required');
writeFileSync(output, JSON.stringify({ plan, operation }), { mode: 0o600, flag: 'wx' });
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
});
test('one opted-in immutable dev/test action: inspect all fixed bytes → UI click only', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'mvp-actions', 'Explicit dev/test simulation only');
const file = process.env.RMM_E2E_CASE_FILE;
if (!file) throw new Error('Protected expected case file required');
const metadata = lstatSync(file);
if (!metadata.isFile() || metadata.isSymbolicLink() || metadata.uid !== process.getuid?.() || (metadata.mode & 0o777) !== 0o600 || metadata.size > 1048576) throw new Error('Unsafe expected case file');
const expected = object(JSON.parse(readFileSync(file, 'utf8')));
expect(expected.profile).toBe('dev/test');
expect(expected.expected_principal).toBe('simulated-testprincipal-a');
expect(['hello', 'refresh_inventory', 'query_logs', 'service_setup', 'service_start', 'service_stop',
'apt_native_install', 'apt_native_update', 'ensure_package', 'apply_updates', 'cancel_queued', 'expired_offline', 'reboot_last']).toContain(expected.case);
const sessionResponse = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200);
await login(page);
const session = object(await (await sessionResponse).json());
expect(session.testsimulation).toBe(true);
expect(session.approval_origin).toBe('dev_test_browser_simulation');
expect(session.principal).toBe(expected.expected_principal);
await expect(page.getByLabel('Mandant', { exact: true })).toHaveValue(text(expected.tenant_id));
const operation = text(expected.operation_id);
await page.getByRole('button', { name: operation, exact: true }).click();
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: `Antrag / Ausführung ${operation}`, exact: true }) });
await expect(op.locator('p.warning')).toContainText('Antrag: pending_approval');
await expect(op.locator('pre').first()).toContainText('"job_count": 0');
await expect(op.locator('pre').first()).toContainText('"execution_state": null');
await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click();
const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) });
await expect(detail.locator('pre')).toBeVisible();
const plan = object(JSON.parse(await detail.locator('pre').innerText()));
expect(plan.plan_hash).toBe(expected.plan_hash);
expect(bytes(plan.plan_payload_bytes)).toEqual(bytes(expected.plan_payload_bytes));
expect(createHash('sha256').update(bytes(plan.plan_payload_bytes)).digest('hex')).toBe(expected.plan_hash);
const targets = plan.resolved_targets;
const jobs = plan.jobs;
if (!Array.isArray(targets) || !Array.isArray(jobs) || targets.length !== 1 || jobs.length !== 1) throw new Error('Exactly one fixed VM target/job required');
expect(object(targets[0])).toEqual({ agent_id: expected.agent_id, asset_id: expected.asset_id });
const job = object(jobs[0]);
const payload = object(job.payload);
expect(payload).toEqual(expected.job_payload);
expect(job.job_payload_hash).toBe(expected.job_payload_hash);
expect(payload.tenant_id).toBe(expected.tenant_id);
expect(payload.agent_id).toBe(expected.agent_id);
expect(payload.asset_id).toBe(expected.asset_id);
expect(Number.isInteger(payload.enrollment_generation) && Number(payload.enrollment_generation) > 0).toBe(true);
expect(Number.isInteger(payload.assignment_version) && Number(payload.assignment_version) > 0).toBe(true);
expect(Date.parse(text(payload.start_before))).toBeGreaterThan(Date.now());
expect(Number(payload.max_output_bytes)).toBeLessThanOrEqual(65536);
const action = object(payload.action);
if (expected.case === 'hello' || expected.case === 'cancel_queued' || expected.case === 'expired_offline') {
expect(payload.execution_identity).toBe('agent_service');
expect(action).toEqual({ action: 'run_script', interpreter: 'posix_sh', script_utf8: "printf '%s\\n' 'Hello World'\n", arguments: [], expected_stdout: 'Hello World\n' });
} else if (expected.case === 'service_setup') {
expect(payload.execution_identity).toBe('root');
expect(action.action).toBe('run_script'); expect(action.interpreter).toBe('posix_sh');
expect(action.arguments).toEqual([]); expect(action.expected_stdout).toBe('RMM test unit prepared\n');
expect(createHash('sha256').update(text(action.script_utf8)).digest('hex')).toBe('159ff240504b8a350b46a56fdf9cd50e5768816de7e6683829f05144978449f0');
expect(payload.max_runtime_seconds).toBe(60);
} else if (expected.case === 'refresh_inventory') {
expect(payload.execution_identity).toBe('agent_service');
expect(action).toEqual({ action: 'refresh_inventory', scope: 'linux.base' });
} else if (expected.case === 'query_logs') {
expect(payload.execution_identity).toBe('agent_service');
expect(action.action).toBe('query_logs'); expect(action.unit).toBe('rmm-mvp-test.service');
expect(action.max_lines).toBe(50);
expect(Date.parse(text(action.until)) - Date.parse(text(action.since))).toBe(300000);
} else if (['apt_native_install', 'apt_native_update', 'ensure_package', 'apply_updates'].includes(text(expected.case))) {
expect(payload.execution_identity).toBe('root');
const native = expected.case === 'apt_native_install' || expected.case === 'apt_native_update';
const upgrade = expected.case === 'apt_native_update' || expected.case === 'apply_updates' || expected.case === 'ensure_package';
expect(action.action).toBe(native ? 'apt_native_acceptance' : expected.case);
const manifest = object(action.apt_manifest);
expect(manifest.type).toBe('rmm.apt.helper-action'); expect(manifest.schema_version).toBe(2);
expect(manifest.action).toBe(native ? 'apt-native-acceptance' : 'apt-execute');
expect(manifest.execution_profile).toBe('dev-test');
const transaction = object(manifest.resolved_apt_transaction);
expect(JSON.parse(bytes(manifest.transaction_bytes).toString('utf8'))).toEqual(transaction);
expect(createHash('sha256').update(bytes(manifest.transaction_bytes)).digest('hex')).toBe(manifest.transaction_sha256);
expect(transaction.recipe_id).toBe(upgrade ? 'rmm-apt-fixture-update-1.1' : 'rmm-apt-fixture-install-1.0');
expect(transaction.recipe_version).toBe('1'); expect(transaction.platform).toBe('ubuntu-24.04-amd64');
expect(transaction.no_reboot).toBe(true);
const pin = upgrade ? 'ef67c9fe0ba1443fcfe465768e1204763063c369a2a1da04af6027d8a9c00491' : '74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867';
expect(transaction.packages).toEqual([{ name: 'rmm-mvp-test', architecture: 'all', old_version: upgrade ? '1.0' : null,
new_version: upgrade ? '1.1' : '1.0', origin: 'rmm-test', sha256: pin, size: 20672,
archive_path: `/var/lib/rmm-apt-transaction/archives/${pin}.deb` }]);
for (const key of ['provider_sha256', 'hook_sha256', 'apt_config_sha256', 'runtime_sha256']) expect(text(manifest[key])).toMatch(/^[a-f0-9]{64}$/);
if (expected.case === 'apply_updates') { expect(action.packages).toEqual(['rmm-mvp-test']); expect(action.reboot_policy).toBe('never'); }
if (expected.case === 'ensure_package') { expect(action.package).toBe('rmm-mvp-test'); expect(action.version).toBe('1.1'); }
} else if (expected.case === 'reboot_last') {
expect(payload.execution_identity).toBe('root');
expect(action).toEqual({ action: 'reboot', reason: 'Explicit final Linux VM dev/test acceptance', dependency_group: 'single-test-vm', reconnect_timeout_seconds: 180 });
expect(payload.max_runtime_seconds).toBe(240);
} else {
expect(payload.execution_identity).toBe('root');
expect(action).toEqual({ action: 'set_service_state', unit: 'rmm-mvp-test.service', state: expected.case === 'service_start' ? 'running' : 'stopped' });
expect(payload.max_runtime_seconds).toBe(180);
}
const approve = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true });
await expect(approve).toBeDisabled();
await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
await expect(approve).toBeEnabled();
await approve.click(); // Deliberately no API client, injected fetch, route or direct approval request.
await expect(op.locator('p.warning')).toContainText('Antrag: approved');
await expect(op.locator('pre').first()).toContainText('"job_count": 1');
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
});
+84
View File
@@ -0,0 +1,84 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { mkdirSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { mkdtempSync, rmSync } from 'node:fs';
import { chromium } from '../../web/node_modules/@playwright/test/index.js';
import { login } from './helpers';
test('real TLS UI: login, tenant, fixed preview, pending, reload, CSRF denial and logout', async ({ page, context }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only', 'Explicit pending-only dev/test run required');
const asset = process.env.RMM_E2E_ASSET_ID;
if (!asset) throw new Error('Authenticated local fixture asset required');
const statuses: { path: string; status: number }[] = [];
page.on('response', r => {
const path = new URL(r.url()).pathname;
if (path === '/auth/dev/login' || path.endsWith('/operation-plans') || path.endsWith('/submit')) statuses.push({ path: path === '/auth/dev/login' ? 'login' : path.endsWith('/submit') ? 'submit' : 'plan', status: r.status() });
});
let authorizationRequests = 0;
page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) authorizationRequests++; });
await login(page);
await expect(page.getByLabel('Mandant', { exact: true })).not.toHaveValue('');
const cookies = await context.cookies();
const authCookie = cookies.find(c => c.name === '__Host-rmm_session');
expect(authCookie?.httpOnly).toBe(true); expect(authCookie?.secure).toBe(true); expect(authCookie?.sameSite).toBe('Strict');
await page.getByRole('checkbox', { name: `Ziel ${asset}`, exact: true }).check();
await page.getByLabel('Exakte UTF-8 Skriptbytes', { exact: false }).fill("printf '%s\\n' 'Hello World'\n");
await page.getByLabel('Erwartete stdout-Ausgabe', { exact: false }).fill('Hello World\n');
await page.getByLabel('Begründung', { exact: true }).fill('Authenticated synthetic local fixture · pending-only browser contract; do not execute');
await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click();
const approve = page.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen' });
await expect(approve).toBeDisabled();
await expect(page.getByText('Nicht genehmigungsfähig:', { exact: false })).toHaveCount(0);
await page.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click();
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) });
await expect(op.locator('p.warning')).toContainText('Antrag: pending_approval');
await expect(op.locator('pre')).toContainText('"job_count": 0');
await expect(op.locator('pre')).toContainText('"execution_state": null');
const operationHeading = await op.getByRole('heading', { name: /^Antrag \/ Ausführung / }).innerText();
const operationId = operationHeading.replace('Antrag / Ausführung ', '').trim();
await page.reload();
await expect(page.getByRole('status')).toContainText('SIMULIERT');
await page.getByRole('button', { name: operationId, exact: true }).click();
await expect(op.locator('p.warning')).toContainText('Antrag: pending_approval');
await page.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click();
await page.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
await expect(approve).toBeEnabled(); // We intentionally do NOT click approval in this run.
expect(authorizationRequests).toBe(0);
// Negative CSRF check uses a real UI logout click, never an authorization request.
await page.route('**/auth/logout', async route => {
const headers = { ...route.request().headers() }; delete headers['x-csrf-token'];
await route.continue({ headers });
});
const denied = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/logout');
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
expect((await denied).status()).toBe(403);
await expect(page.getByRole('alert').first()).toBeVisible();
await expect(page.getByRole('status')).toContainText('SIMULIERT');
await page.unroute('**/auth/logout');
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
await expect(page.getByRole('button', { name: 'Anmelden', exact: true })).toBeVisible();
expect(statuses.some(s => s.path === 'login' && s.status === 200)).toBe(true);
expect(statuses.some(s => s.path === 'plan' && s.status === 200)).toBe(true);
expect(statuses.some(s => s.path === 'submit' && s.status === 200)).toBe(true);
const directory = resolve('node_modules/.cache/browser-evidence'); mkdirSync(directory, { recursive: true, mode: 0o700 });
writeFileSync(resolve(directory, 'pending.json'), JSON.stringify({ profile: 'dev/test', testsimulation: true, tls_verification_enabled: true, synthetic_authenticated_local_fixture: true, simulation_banner: true, secure_cookie: true, pending_job_count: 0, pending_execution_state: null, reload_pending_review: true, csrf_logout_denied_status: 403, authorization_requests: authorizationRequests, executed_jobs: false, vm_evidence: false, statuses, checked_at: new Date().toISOString() }, null, 2), { mode: 0o600 });
});
test('real browser TLS rejects a certificate for a different DNS name', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only');
let rejected = false;
try { await page.goto('https://rmm-dev-wrong-name.internal:18443/'); } catch (e) { rejected = e instanceof Error && /ERR_CERT_COMMON_NAME_INVALID/.test(e.message); }
expect(rejected).toBe(true);
});
test('real browser without the isolated development CA rejects the server chain', async () => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only');
const home = mkdtempSync(resolve('node_modules/.cache/browser-untrusted-'));
const browser = await chromium.launch({ env: { ...process.env, HOME: home }, args: ['--host-resolver-rules=MAP rmm-dev.internal 127.0.0.1', '--no-proxy-server'] });
try {
const page = await browser.newPage();
let rejected = false;
try { await page.goto('https://rmm-dev.internal:18443/'); } catch (e) { rejected = e instanceof Error && /ERR_CERT_AUTHORITY_INVALID/.test(e.message); }
expect(rejected).toBe(true);
} finally { await browser.close(); rmSync(home, { recursive: true, force: true }); }
});
+17
View File
@@ -0,0 +1,17 @@
import type { FullResult, Reporter, TestCase, TestResult, TestStep } from '../../web/node_modules/@playwright/test/reporter';
import { diagnostic } from './diagnostics';
/** Playwright action error call logs can contain locator.fill(password) arguments.
* Never print raw test errors/steps/stdout/stderr, even on a failed login. */
export default class SafeReporter implements Reporter {
private executed = 0;
onBegin() { console.log('Dev/test browser simulation suite. Secret-bearing step/error logs suppressed.'); }
onTestEnd(_test: TestCase, result: TestResult) { if (result.status !== 'skipped') this.executed++; console.log(`Browser simulation: ${result.status}.`); }
onStepEnd(_test: TestCase, _result: TestResult, step: TestStep) {
if (step.category !== 'test.step') return;
const safe = diagnostic(step.title, step.error, step.location?.line);
if (safe) console.error(safe);
}
onError() { console.error('Browser simulation infrastructure error; details suppressed to protect login secrets.'); }
onEnd(result: FullResult) { console.log(this.executed ? `Browser simulation suite: ${result.status}. No production approval evidence.` : 'No browser test executed (discovery or skipped). No acceptance evidence.'); }
}
+46
View File
@@ -0,0 +1,46 @@
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
import { createHash } from 'node:crypto';
import { login } from './helpers';
test('preserved real VM: pending fixed Hello World → UI click → confirmed result', async ({ page }) => {
test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'vm-hello', 'Explicit VM Hello-World simulation required');
const asset = process.env.RMM_E2E_ASSET_ID;
const operation = process.env.RMM_E2E_OPERATION_ID;
if (!asset || !operation) throw new Error('Fixed VM asset and existing pending operation required');
await login(page);
await page.getByRole('button', { name: operation, exact: true }).click();
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: `Antrag / Ausführung ${operation}`, exact: true }) });
await expect(op.locator('p.warning')).toContainText('Antrag: pending_approval');
await expect(op.locator('pre').first()).toContainText('"job_count": 0');
await expect(op.locator('pre').first()).toContainText('"execution_state": null');
await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click();
const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) });
await expect(detail.locator('pre')).toBeVisible();
// Inspect the complete rendered immutable preview, not a separately fetched
// or caller-supplied payload. Refuse all root/package/reboot/extra-target jobs.
const plan = JSON.parse(await detail.locator('pre').innerText());
expect(plan.resolved_targets).toHaveLength(1);
expect(plan.resolved_targets[0].asset_id).toBe(asset);
expect(plan.jobs).toHaveLength(1);
const payload = plan.jobs[0].payload;
expect(payload.asset_id).toBe(asset);
expect(payload.execution_identity).toBe('agent_service');
expect(payload.action).toEqual({ action: 'run_script', interpreter: 'posix_sh', script_utf8: "printf '%s\\n' 'Hello World'\n", arguments: [], expected_stdout: 'Hello World\n' });
expect(payload.max_runtime_seconds).toBeLessThanOrEqual(60);
expect(payload.max_output_bytes).toBeLessThanOrEqual(65536);
expect(Date.parse(payload.start_before)).toBeGreaterThan(Date.now());
expect(createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex')).toBe(plan.plan_hash);
await expect(detail).toContainText(plan.plan_hash);
const approve = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true });
await expect(approve).toBeDisabled();
await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
await expect(approve).toBeEnabled();
await approve.click(); // Only actual UI interaction; no request/API approval.
await expect(op.locator('p.warning')).toContainText('Antrag: approved');
await expect(op.locator('pre').first()).toContainText('"job_count": 1');
await expect(op.locator('p.warning')).toContainText('Ausführung: succeeded', { timeout: 90000 });
await op.getByRole('button', { name: /^Job / }).click();
await expect(op.getByRole('heading', { name: /^Job / })).toBeVisible();
await expect(op.locator('article pre')).toContainText('"postconditions_passed": true');
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
});
+284
View File
@@ -0,0 +1,284 @@
#!/usr/bin/env python3
"""Real local TLS/PostgreSQL/agent checks. Does not read VM credentials."""
import concurrent.futures
import base64
import hashlib
import http.client
import json
import os
from pathlib import Path
import secrets
import socket
import ssl
import struct
import subprocess
import tempfile
import time
ROOT = Path(__file__).resolve().parents[1]
STATE = ROOT / "devstate"
CA = STATE / "pki/ca.pem"
A = "11111111-1111-4111-8111-111111111111"
B = "22222222-2222-4222-8222-222222222222"
def sql(query, user="postgres", fail=False):
result = subprocess.run(["docker", "compose", "--env-file", str(STATE / ".env"), "-f", str(ROOT / "deploy/compose.yaml"), "-p", "rust-discovery-dev", "exec", "-T", "postgres", "psql", "-X", "-qAt", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", "rmm"], input=query, text=True, capture_output=True)
assert (result.returncode != 0) if fail else (result.returncode == 0), "database assertion failed (details suppressed)"
return result.stdout.strip()
class LocalHTTPS(http.client.HTTPSConnection):
def connect(self):
tcp = socket.create_connection(("127.0.0.1", 18443), timeout=10)
self.sock = self._context.wrap_socket(tcp, server_hostname=self.host)
def request(path, token=None, body=None, context=None, hostname="rmm-dev.internal"):
client = LocalHTTPS(hostname, 18443, context=context or ssl.create_default_context(cafile=str(CA)), timeout=10)
headers = {}
if token:
headers["Authorization"] = "Bearer " + token
if body is not None:
headers["Content-Type"] = "application/json"
client.request("POST" if body is not None else "GET", path, body=json.dumps(body) if body is not None else None, headers=headers)
response = client.getresponse()
data = response.read()
client.close()
return response.status, data
def token(tenant):
raw = secrets.token_hex(32)
digest = hashlib.sha256(raw.encode()).hexdigest()
sql(f"INSERT INTO enrollment_tokens VALUES('{digest}','{tenant}',now()+interval '15 minutes',NULL);")
return raw
def check(condition, message):
assert condition, message
print("PASS", message)
def varint(number):
value = bytearray()
while number > 127:
value.append((number & 127) | 128)
number >>= 7
value.append(number)
return bytes(value)
def field(tag, value):
if isinstance(value, int):
return varint(tag << 3) + varint(value)
value = value.encode() if isinstance(value, str) else value
return varint((tag << 3) | 2) + varint(len(value)) + value
def envelope(agent, sequence, generation, payload_tag, payload, major=1, observed=None):
import uuid
return b"".join([field(1, major), field(3, str(uuid.uuid4())), field(4, agent), field(5, 1), field(6, generation), field(7, "33333333-3333-4333-8333-333333333333"), field(8, sequence), field(9, observed if observed is not None else int(time.time() * 1000)), field(payload_tag, payload)])
class WebSocket:
def __init__(self, context):
tcp = socket.create_connection(("127.0.0.1", 18443), timeout=10)
self.sock = context.wrap_socket(tcp, server_hostname="rmm-dev.internal")
key = base64.b64encode(secrets.token_bytes(16)).decode()
self.sock.sendall(("GET /agent/v1/connect HTTP/1.1\r\nHost: rmm-dev.internal:18443\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: " + key + "\r\nX-Client-Cert: forged\r\n\r\n").encode())
headers = b""
while not headers.endswith(b"\r\n\r\n"):
byte = self.sock.recv(1)
assert byte and len(headers) < 16384
headers += byte
self.status = int(headers.split(b" ")[1])
def send(self, payload):
mask = secrets.token_bytes(4)
size = len(payload)
length = bytes([size | 128]) if size < 126 else (bytes([126 | 128]) + struct.pack("!H", size)) if size < 65536 else (bytes([127 | 128]) + struct.pack("!Q", size))
self.sock.sendall(b"\x82" + length + mask + bytes(x ^ mask[i % 4] for i, x in enumerate(payload)))
def exact(self, count):
result = b""
while len(result) < count:
chunk = self.sock.recv(count - len(result))
if not chunk:
raise EOFError()
result += chunk
return result
def receive(self):
opcode, size = self.exact(2)
size &= 127
if size == 126:
size = struct.unpack("!H", self.exact(2))[0]
elif size == 127:
size = struct.unpack("!Q", self.exact(8))[0]
assert size <= 1048576
return opcode & 15, self.exact(size)
def close(self):
self.sock.close()
def rejected_frame(context, payload):
ws = WebSocket(context)
try:
assert ws.status == 101
try:
# The server may reject an oversized frame from its header before
# the client has finished sending the body.
ws.send(payload)
opcode, _ = ws.receive()
assert opcode == 8, "invalid protocol input was not closed"
except (EOFError, ssl.SSLError, ConnectionResetError, BrokenPipeError):
pass
finally:
ws.close()
def main():
production = subprocess.run([str(ROOT / "target/debug/rmm-server")], env=dict(os.environ, RMM_PROFILE="production", RMM_DEV_AUTH="true"), capture_output=True)
check(production.returncode != 0 and b"production forbids dev auth" in production.stderr, "production rejects explicit dev auth before connecting")
viewer_a = (STATE / "viewer-a.token").read_text().strip()
viewer_b = (STATE / "viewer-b.token").read_text().strip()
deadline = time.monotonic() + 15
while True:
try:
request("/healthz")
break
except (ConnectionRefusedError, ConnectionResetError):
if time.monotonic() >= deadline:
raise
time.sleep(0.25)
check(request("/healthz")[0] == 200, "trusted TLS health")
for context, hostname in [(ssl.create_default_context(), "rmm-dev.internal"), (ssl.create_default_context(cafile=str(CA)), "wrong.internal")]:
try:
request("/healthz", context=context, hostname=hostname)
except ssl.SSLCertVerificationError:
pass
else:
raise AssertionError("TLS validation did not reject invalid trust/name")
print("PASS untrusted CA and wrong DNS name rejected")
check(request("/api/v1/tenants")[0] == 401, "anonymous API denied")
check(request("/api/v1/tenants", "invalid")[0] == 401, "invalid bearer denied")
status, error = request("/api/v1/tenants", "invalid")
check(status == 401 and json.loads(error)["code"] == "UNAUTHENTICATED" and "correlation_id" in json.loads(error), "structured errors without request echo")
for bearer, tenant, foreign in [(viewer_a, A, B), (viewer_b, B, A)]:
status, data = request("/api/v1/tenants", bearer)
check(status == 200 and [x["tenant_id"] for x in json.loads(data)["items"]] == [tenant], "viewer sees exactly assigned tenant")
check(request(f"/api/v1/tenants/{foreign}/devices", bearer)[0] == 404, "foreign tenant denied")
check(sql("SELECT count(*) FROM tenants;", "rmm_app") == "0", "RLS denies missing context")
result = sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); SELECT count(*) FROM tenants; ROLLBACK; SELECT count(*) FROM tenants;", "rmm_app")
check(result.splitlines()[-2:] == ["1", "0"], "transaction-local tenant reset after rollback")
sql(f"BEGIN; SELECT set_config('app.tenant_id','{A}',true); INSERT INTO assets VALUES('{B}','{secrets.token_hex(16)}');", "rmm_app", fail=True)
print("PASS RLS rejects cross-tenant insert")
role = sql("SELECT rolsuper OR rolbypassrls FROM pg_roles WHERE rolname='rmm_app';")
check(role == "f", "application role has neither superuser nor BYPASSRLS")
check(sql("SELECT count(*) FROM pg_tables WHERE schemaname='public' AND tableowner='rmm_app';") == "0", "application owns no tables")
with tempfile.TemporaryDirectory(prefix="rmm-e2e-", dir=STATE) as folder:
folder = Path(folder)
subprocess.run(["openssl", "req", "-new", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:prime256v1", "-nodes", "-keyout", str(folder / "device.key"), "-out", str(folder / "device.csr"), "-subj", "/CN=e2e"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
csr = (folder / "device.csr").read_text()
value = token(A)
# Invalid CSR rolls consumption back.
check(request("/agent/v1/enroll", body={"token": value, "csr_pem": "invalid"})[0] == 400, "invalid CSR rejected")
body = {"token": value, "csr_pem": csr}
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
replies = list(pool.map(lambda _: request("/agent/v1/enroll", body=body), range(2)))
check(sorted(x[0] for x in replies) == [200, 401], "enrollment token consumed atomically exactly once")
enrolled = json.loads(next(data for status, data in replies if status == 200))
(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")
check(request("/healthz", context=context)[0] == 200, "issued client certificate passes TLS validation")
anonymous = WebSocket(ssl.create_default_context(cafile=str(CA)))
check(anonymous.status == 401, "WSS denies missing peer identity despite forged certificate header")
anonymous.close()
rejected_frame(context, envelope(enrolled["agent_id"], 1, 0, 10, field(1, "test"), major=2))
print("PASS unknown protocol major closes channel")
rejected_frame(context, envelope("44444444-4444-4444-8444-444444444444", 1, 0, 10, field(1, "test")))
print("PASS TLS device cannot claim another agent ID")
ws = WebSocket(context)
try:
check(ws.status == 101, "registered certificate upgrades to WSS")
ws.send(envelope(enrolled["agent_id"], 1, 0, 10, field(1, "test")))
check(ws.receive()[0] == 2, "server hello is binary protobuf")
generation = int(sql(f"SELECT connection_generation FROM agent_installations WHERE agent_id='{enrolled['agent_id']}';"))
ws.send(envelope(enrolled["agent_id"], 2, generation, 12, field(1, "55555555-5555-4555-8555-555555555555"), observed=int(time.time() * 1000) - 600000))
check(ws.receive()[0] == 8, "stale buffered heartbeat closes channel")
check(sql(f"SELECT last_contact_at IS NULL FROM agent_installations WHERE agent_id='{enrolled['agent_id']}';") == "t", "stale heartbeat cannot mark presence online")
finally:
ws.close()
rejected_frame(context, b"x" * (1048576 + 1))
print("PASS oversized WebSocket message rejected")
# Actual agent enrollment, WSS, inventory and heartbeat, with bounded wait.
agent_state = folder / "agent"
token_path = folder / "enroll.token"
token_path.write_text(token(A))
token_path.chmod(0o600)
env = dict(os.environ, RMM_AGENT_STATE=str(agent_state), RMM_CA_FILE=str(CA), RMM_ENROLLMENT_TOKEN_FILE=str(token_path),RMM_EXTENDED_INVENTORY="1")
with open(folder / "agent.log", "wb") as log:
agent = subprocess.Popen([str(ROOT / "target/debug/rmm-agent")], env=env, stdout=log, stderr=log)
try:
deadline = time.monotonic() + 55
observed = None
while time.monotonic() < deadline:
status, data = request(f"/api/v1/tenants/{A}/devices", viewer_a)
assert status == 200
items = json.loads(data)["items"]
# Never select/revoke a concurrently running VM or another
# integration worker's device merely because it is online.
own_identity = agent_state / "identity.json"
own = json.loads(own_identity.read_text()) if own_identity.exists() else None
observed = None
if own:
detail_status, detail = request(f"/api/v1/tenants/{A}/devices/{own['asset_id']}", viewer_a)
candidate = json.loads(detail) if detail_status == 200 else None
if candidate and candidate["agent_id"] == own["agent_id"] and candidate["inventory"] and candidate["presence"] == "online":
observed = candidate
if observed:
break
assert agent.poll() is None, "agent exited (logs remain private)"
time.sleep(1)
check(observed is not None, "real Linux agent enrolled, mTLS WSS inventory stored and heartbeat online")
check(observed["inventory"]["root_available_bytes"] > 0 and observed["observed_at"] and observed["received_at"], "real disk value and observation timestamps")
check(isinstance(observed["inventory"].get("observations"),list) and len(observed["inventory"]["observations"])>0 and observed["inventory"].get("collection_completed_at"),"real native extended collectors stored with bounded per-source metadata")
status, data = request(f"/api/v1/tenants/{B}/devices", viewer_b)
check(status == 200 and observed["agent_id"] not in data.decode(), "tenant B cannot see tenant A telemetry")
check((agent_state / "device.key").stat().st_mode & 0o777 == 0o600, "local private device key is 0600")
agent_id = observed["agent_id"]
generation = sql(f"SELECT connection_generation FROM agent_installations WHERE agent_id='{agent_id}';")
agent.terminate()
agent.wait(timeout=10)
agent = subprocess.Popen([str(ROOT / "target/debug/rmm-agent")], env=env, stdout=log, stderr=log)
deadline = time.monotonic() + 15
while time.monotonic() < deadline:
current = sql(f"SELECT connection_generation FROM agent_installations WHERE agent_id='{agent_id}';")
if int(current) > int(generation):
break
time.sleep(1)
check(int(current) > int(generation), "restart reuses local identity and fences old channel")
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent_id}';")
before = sql(f"SELECT count(*) FROM observations WHERE agent_id='{agent_id}';")
agent.terminate()
agent.wait(timeout=10)
agent = subprocess.Popen([str(ROOT / "target/debug/rmm-agent")], env=env, stdout=log, stderr=log)
time.sleep(3)
check(sql(f"SELECT count(*) FROM observations WHERE agent_id='{agent_id}';") == before, "revoked identity cannot ingest after reconnect")
finally:
agent.terminate()
agent.wait(timeout=10)
if (agent_state / "identity.json").exists():
identity = json.loads((agent_state / "identity.json").read_text())
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{identity['agent_id']}';")
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{enrolled['agent_id']}';")
check(sql("SELECT count(*) FROM information_schema.tables WHERE table_name IN ('jobs','outbox','execution_permits');") == "3", "durable job/outbox/grant schema present")
check(sql("SELECT count(*) FROM jobs j LEFT JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id LEFT JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id WHERE a.authorization_id IS NULL;") == "0", "no executable job exists without durable human authorization")
print("All local integration checks passed. VM/systemd acceptance NOT performed.")
if __name__ == "__main__":
main()
+42
View File
@@ -0,0 +1,42 @@
# Integrated server OIDC test (explicit local development fixture)
Prerequisites: native `cargo build -p rmm-server --locked`, normal migrations through
0011 applied to the existing development database, existing protected development
configuration/CA/job key, Python cryptography, existing web Playwright/Chromium and
the isolated certutil tool used by the browser harness. No external IdP is required.
Run `python3 tests/oidc/server_integration.py`. It starts its own ephemeral loopback
TLS provider and native API on an unused loopback port, and reaches the existing
development PostgreSQL container through its bridge address. It does not publish
PostgreSQL or restart the existing server/database. Private configuration is parsed
internally as data; no shell sourcing, credential stdout/argv or browser artifacts.
The only administrative fixtures are its own random restricted auth-state login
role, synthetic external identity routing row and explicit `human` membership row
in development tenant A. It grants no membership from OIDC groups/email. Those
fixtures and its own encrypted state rows are removed afterwards. It does **not**
write authorization/job/permit rows or positively invoke a plan approval endpoint.
The only approval-endpoint test uses a bearer and must be denied before plan lookup.
Actual latest standalone run: **35 assertions passed**, including a real Chromium
provider button/cross-site callback with normal CA validation and a temporary NSS
store. Browser interaction is visibly synthetic and uses explicit dev simulation.
The configured production-profile start/login check uses simulation false solely
to test configuration behavior against the mock; this is **not** external-provider,
human production-approval or Linux-VM acceptance.
Covered: code/PKCE/nonce/state, binding non-burning and callback replay, stable
mapping, final session/CSRF/logout, JWT resource audience/ID-token separation,
issuer/expiry/kid/rotation/scopes, actual MCP lifecycle/inventory call and insufficient
scope denial, persistent membership revocation, disabled external identity, foreign
tenant not-found, bearer+cookie approval denial, production dev-route disabling.
Harness corrections during implementation: avoid duplicate Python environment
keywords; do not route the dev-login JSON extractor in production; read MCP SSE to
its concrete JSON-RPC response instead of waiting for a long-lived stream to close.
No auth/approval check was disabled to make the harness pass.
Current backend configuration/routes/migration/provisioning contract is
`crates/server/OIDC.md`. Earlier `proto/BACKEND_CONTRACT.md` statements about absent
production auth describe the pre-OIDC backend increment, not this additive adapter.
The other Sync/root/APT contracts and native-execution gates are unchanged.
+31
View File
@@ -0,0 +1,31 @@
// Real Chromium navigation with an isolated NSS truststore, no TLS bypass/artifacts.
const { chromium } = require('../../web/node_modules/playwright');
(async () => {
const browser = await chromium.launch({
headless: true,
env: { ...process.env, HOME: process.env.RMM_OIDC_TEST_BROWSER_HOME },
args: ['--host-resolver-rules=MAP rmm-dev.internal 127.0.0.1', '--no-proxy-server'],
});
try {
const page = await browser.newPage();
await page.goto(process.env.RMM_OIDC_TEST_ORIGIN + '/auth/oidc/login');
await page.getByRole('button', { name: 'Authorize simulated test login' }).click();
await page.waitForURL(process.env.RMM_OIDC_TEST_ORIGIN + '/');
const result = await page.evaluate(async () => {
const session = await fetch('/auth/session');
const value = await session.json();
return { status: session.status, simulation: value.testsimulation, principal: value.principal, csrf: Boolean(value.csrf_token) };
});
if (result.status !== 200 || result.simulation !== true || !result.csrf || result.principal !== process.env.RMM_OIDC_TEST_PRINCIPAL) throw new Error('browser contract failed');
const cookies = await page.context().cookies();
const session = cookies.find(c => c.name === '__Host-rmm_session');
if (!session || !session.httpOnly || !session.secure || session.sameSite !== 'Strict' || cookies.some(c => c.name === '__Host-rmm_oidc_binding')) throw new Error('cookie contract failed');
const logout = await page.evaluate(async () => {
const s = await (await fetch('/auth/session')).json();
const response = await fetch('/auth/logout', { method: 'POST', headers: { 'X-CSRF-Token': s.csrf_token } });
return { logout: response.status, after: (await fetch('/auth/session')).status };
});
if (logout.logout !== 204 || logout.after !== 401) throw new Error('logout contract failed');
console.log('PASS real Chromium OIDC code/PKCE/nonce/state + cross-site Lax callback, strict final session, simulated-test label and logout; no job approval');
} finally { await browser.close(); }
})().catch(() => { console.error('FAIL OIDC browser test (private details suppressed)'); process.exitCode = 1; });
+317
View File
@@ -0,0 +1,317 @@
#!/usr/bin/env python3
"""Actual TLS mock IdP + native server + existing dev PostgreSQL, no approvals/VM effects.
Only creates/removes its own identity/membership and separate login role fixture.
No direct API/DB approval is performed. Requires normal migrations already applied.
"""
import base64
import datetime
import hashlib
import http.client
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import os
from pathlib import Path
import secrets
import shutil
import socket
import ssl
import subprocess
import sys
import tempfile
import threading
import time
import urllib.parse
import uuid
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, rsa
from cryptography.x509.oid import NameOID
ROOT = Path(__file__).resolve().parents[2]
STAGE = 'initial setup'
sys.path.insert(0, str(ROOT / 'tests'))
from local_integration import sql, STATE, CA, A, B
def encoded(value):
return base64.urlsafe_b64encode(value).decode().rstrip('=')
def private(path, data):
path.write_bytes(data if isinstance(data, bytes) else data.encode())
path.chmod(0o600)
return str(path)
def quiet(args, **kwargs):
result = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **kwargs)
if result.returncode:
raise RuntimeError('private subprocess failed')
class Idp:
def __init__(self, folder):
self.lock = threading.Lock()
self.key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
self.kid = 'initial'
self.subject = 'synthetic-oidc-' + uuid.uuid4().hex
self.codes, self.requests = {}, {}
self.jwks, self.exchanges = 0, 0
self.bad_nonce = False
ca_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, 'Isolated OIDC mock CA')])
now = datetime.datetime.now(datetime.timezone.utc)
ca = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(ca_key.public_key())
.serial_number(x509.random_serial_number()).not_valid_before(now-datetime.timedelta(minutes=1))
.not_valid_after(now+datetime.timedelta(days=1)).add_extension(x509.BasicConstraints(ca=True,path_length=0),critical=True)
.sign(ca_key,hashes.SHA256()))
leaf_key = rsa.generate_private_key(public_exponent=65537,key_size=2048)
leaf = (x509.CertificateBuilder().subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'localhost')]))
.issuer_name(name).public_key(leaf_key.public_key()).serial_number(x509.random_serial_number())
.not_valid_before(now-datetime.timedelta(minutes=1)).not_valid_after(now+datetime.timedelta(days=1))
.add_extension(x509.SubjectAlternativeName([x509.DNSName('localhost')]),critical=False)
.add_extension(x509.ExtendedKeyUsage([x509.oid.ExtendedKeyUsageOID.SERVER_AUTH]),critical=False).sign(ca_key,hashes.SHA256()))
self.ca = private(folder/'mock-ca.pem',ca.public_bytes(serialization.Encoding.PEM))
cert = private(folder/'mock.pem',leaf.public_bytes(serialization.Encoding.PEM))
key = private(folder/'mock.key',leaf_key.private_bytes(serialization.Encoding.PEM,serialization.PrivateFormat.PKCS8,serialization.NoEncryption()))
idp = self
class Handler(BaseHTTPRequestHandler):
def log_message(self, *_): pass
def respond(self, status, value=None, location=None, html=None):
body = html.encode() if html else json.dumps(value or {}).encode()
self.send_response(status)
self.send_header('Content-Type','text/html' if html else 'application/json')
self.send_header('Content-Length',str(len(body)))
self.send_header('Cache-Control','no-store')
if location: self.send_header('Location',location)
self.end_headers();self.wfile.write(body)
def do_GET(self):
u = urllib.parse.urlsplit(self.path)
if u.path == '/.well-known/openid-configuration':
self.respond(200,{'issuer':idp.issuer,'authorization_endpoint':idp.issuer+'authorize','token_endpoint':idp.issuer+'token','jwks_uri':idp.issuer+'jwks','response_types_supported':['code'],'id_token_signing_alg_values_supported':['RS256'],'code_challenge_methods_supported':['S256'],'token_endpoint_auth_methods_supported':['client_secret_post']})
elif u.path == '/jwks':
with idp.lock:
idp.jwks += 1
key = idp.key.public_key().public_numbers()
self.respond(200,{'keys':[{'kty':'RSA','alg':'RS256','use':'sig','kid':idp.kid,'n':encoded(key.n.to_bytes((key.n.bit_length()+7)//8,'big')),'e':encoded(key.e.to_bytes(3,'big'))}]})
elif u.path == '/authorize':
q = dict(urllib.parse.parse_qsl(u.query))
if q.get('client_id')!='rmm-browser' or q.get('redirect_uri')!=idp.redirect or q.get('code_challenge_method')!='S256': return self.respond(400)
request_id = secrets.token_hex(24)
with idp.lock: idp.requests[request_id] = q
self.respond(200,html=f'<h1>SIMULATED local OIDC test principal, no human acceptance</h1><form action="/approve" method="post"><input type="hidden" name="request" value="{request_id}"><button>Authorize simulated test login</button></form>')
else: self.respond(404)
def do_POST(self):
size = int(self.headers.get('Content-Length','0'))
if size>16384: return self.respond(413)
q = dict(urllib.parse.parse_qsl(self.rfile.read(size).decode()))
if self.path == '/approve':
with idp.lock:
request = idp.requests.pop(q.get('request'),None)
if request is None: return self.respond(400)
code = secrets.token_hex(32); idp.codes[code] = request
return self.respond(303,location=idp.redirect+'?'+urllib.parse.urlencode({'code':code,'state':request['state']}))
if self.path != '/token': return self.respond(404)
with idp.lock:
idp.exchanges += 1
request = idp.codes.pop(q.get('code'),None)
if request is None or q.get('client_secret')!=idp.secret or q.get('redirect_uri')!=idp.redirect or q.get('client_id')!='rmm-browser' or q.get('grant_type')!='authorization_code' or encoded(hashlib.sha256(q.get('code_verifier','').encode()).digest())!=request['code_challenge']: return self.respond(400)
value = idp.token(aud='rmm-browser',typ='JWT',nonce='invalid' if idp.bad_nonce else request['nonce'])
self.respond(200,{'id_token':value,'token_type':'Bearer','access_token':'unused'})
self.server = ThreadingHTTPServer(('127.0.0.1',0),Handler)
self.issuer = f'https://localhost:{self.server.server_port}/'
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER);ctx.load_cert_chain(cert,key)
self.server.socket = ctx.wrap_socket(self.server.socket,server_side=True)
self.thread = threading.Thread(target=self.server.serve_forever,daemon=True);self.thread.start()
self.secret = secrets.token_hex(32)
def token(self, aud='https://rmm.example/mcp', typ='at+jwt', **overrides):
claims = {'iss':self.issuer,'sub':self.subject,'aud':aud,'exp':int(time.time())+300,'iat':int(time.time()),'nbf':int(time.time())-1,'jti':uuid.uuid4().hex,'client_id':'rmm-mcp','scope':'inventory:read operations:read plans:approve'}
claims.update(overrides)
header = {'alg':'RS256','kid':self.kid,'typ':typ}
body = encoded(json.dumps(header,separators=(',',':')).encode())+'.'+encoded(json.dumps(claims,separators=(',',':')).encode())
return body+'.'+encoded(self.key.sign(body.encode(),padding.PKCS1v15(),hashes.SHA256()))
def main():
global STAGE
env = {}
for line in (STATE/'.env').read_text().splitlines():
if line and not line.startswith('#'):
name,value=line.split('=',1);env[name]=value
result = subprocess.run(['docker','inspect','--format','{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}','rust-discovery-dev-postgres-1'],capture_output=True,text=True,check=True)
address = result.stdout.strip()
socket.inet_aton(address)
role = 'oidc_test_' + secrets.token_hex(8)
password = secrets.token_hex(32)
principal = None
process = None
idp = None
state_key_ref = None
assertions = 0
def check(value, label):
nonlocal assertions
if not value: raise RuntimeError(label)
assertions += 1;print('PASS',label)
with tempfile.TemporaryDirectory(prefix='oidc-server-',dir=STATE) as folder_name:
folder = Path(folder_name);folder.chmod(0o700)
try:
idp = Idp(folder)
with socket.socket() as sock:
sock.bind(('127.0.0.1',0));port=sock.getsockname()[1]
origin = f'https://rmm-dev.internal:{port}'
idp.redirect = origin+'/auth/oidc/callback'
principal = 'oidc:'+hashlib.sha256(json.dumps([idp.issuer,idp.subject],separators=(',',':')).encode()).hexdigest()
# Only identity routing/membership fixtures; never authorization/job writes.
sql(f"CREATE ROLE {role} LOGIN PASSWORD '{password}' NOSUPERUSER NOBYPASSRLS; GRANT USAGE ON SCHEMA rmm_oidc_auth TO {role}; GRANT SELECT,INSERT,DELETE ON rmm_oidc_auth.login_states TO {role}; INSERT INTO oidc_principals VALUES('{principal}','{idp.issuer}','{idp.subject}',ARRAY['{A}']::uuid[],false); INSERT INTO memberships VALUES('{A}','{principal}','human',ARRAY['inventory:read','operations:read','plans:approve'],NULL);")
app = urllib.parse.urlsplit(env['DATABASE_URL'])
def database_url(user, pwd): return urllib.parse.urlunsplit(('postgres',f'{user}:{pwd}@{address}:5432','/rmm','',''))
cfg = {'issuer':idp.issuer,'browser_client_id':'rmm-browser','client_secret_ref':private(folder/'client-secret',idp.secret),'redirect_uri':idp.redirect,'mcp_audience':'https://rmm.example/mcp','mcp_client_ids':['rmm-mcp'],'auth_database_url_ref':private(folder/'auth-db',database_url(role,password)),'state_key_ref':private(folder/'state-key',encoded(secrets.token_bytes(32))),'extra_ca_file':idp.ca,'testsimulation':True}
config = private(folder/'oidc.json',json.dumps(cfg))
state_key_ref = cfg['state_key_ref']
server_env = dict(os.environ)
server_env.update(env)
server_env.update(RMM_PROFILE='development',RMM_DEV_AUTH='true',DATABASE_URL=database_url(app.username,app.password),RMM_TLS_DIR=str(STATE/'pki'),RMM_JOB_KEY_FILE=str(STATE/'jobs/ed25519.pem'),RMM_WEB_DIR=str(ROOT/'web/dist'),RMM_UI_ORIGIN=origin,RMM_OIDC_CONFIG_FILE=config,RMM_LISTEN_ADDR=f'127.0.0.1:{port}',RMM_SERVER_ROLE='api')
for name in ('RUST_LOG','RUST_BACKTRACE'): server_env.pop(name,None)
def start():
nonlocal process
process=subprocess.Popen([str(ROOT/'target/debug/rmm-server')],env=server_env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
for _ in range(100):
if process.poll() is not None: raise RuntimeError('configured server failed startup')
try:
if request('/healthz')[0]==200:return
except (OSError,ssl.SSLError):pass
time.sleep(.1)
raise RuntimeError('configured server did not become ready')
def request(path, method='GET', body=None, headers=None):
connection=http.client.HTTPSConnection('rmm-dev.internal',port,context=ssl.create_default_context(cafile=str(CA)),timeout=15)
def connect():
connection.sock=connection._context.wrap_socket(socket.create_connection(('127.0.0.1',port),timeout=15),server_hostname='rmm-dev.internal')
connection.connect=connect
h=dict(headers or {})
if body is not None:h['Content-Type']='application/json'
connection.request(method,path,body=None if body is None else json.dumps(body),headers=h)
response=connection.getresponse();items=response.getheaders();status=response.status
if 'text/event-stream' in response.getheader('Content-Type',''):
data=b''
while True:
line=response.readline()
if not line:break
if line.startswith(b'data:') and line[5:].strip():
value=json.loads(line[5:])
if 'result' in value or 'error' in value:
data=json.dumps(value).encode();break
else:data=response.read()
connection.close()
return status,items,data
def flow():
status,headers,_=request('/auth/oidc/login');check(status==303,'configured login starts real OIDC flow')
h=dict(headers);binding=h['set-cookie'].split(';')[0]
q=dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(h['location']).query))
with idp.lock:
code=secrets.token_hex(32);idp.codes[code]=q
return '/auth/oidc/callback?'+urllib.parse.urlencode({'code':code,'state':q['state']}),binding
STAGE = 'configured native server startup'
start()
STAGE = 'HTTP OIDC contracts'
check(json.loads(request('/auth/config')[2])['oidc_login_url']=='/auth/oidc/login','UI discovery exposes nonempty OIDC login URL')
callback,binding=flow()
check(request(callback,headers={'Cookie':'__Host-rmm_oidc_binding=wrong'})[0]==401,'wrong binding rejected without burning correct state')
status,headers,_=request(callback,headers={'Cookie':binding})
check(status==303,'real token exchange validates signature nonce and PKCE')
cookies=[v.split(';')[0] for k,v in headers if k.lower()=='set-cookie' and v.startswith('__Host-rmm_session=')]
cookie=cookies[0]
check(request(callback,headers={'Cookie':binding})[0]==401,'consumed callback state cannot be replayed')
status,_,raw=request('/auth/session',headers={'Cookie':cookie});session=json.loads(raw)
check(status==200 and session['principal']==principal and session['testsimulation'] is True,'OIDC identity uses stable mapped principal and explicit dev simulation')
csrf={'Cookie':cookie,'Origin':origin,'X-CSRF-Token':session['csrf_token']}
check(request('/auth/logout','POST',{},headers={'Cookie':cookie})[0]==403,'OIDC cookie mutations require Origin and CSRF')
access=idp.token()
bearer={'Authorization':'Bearer '+access}
check(request(f'/api/v1/tenants/{A}/devices',headers=bearer)[0]==200,'resource JWT combines scopes with current persistent membership')
check(request(f'/api/v1/tenants/{B}/devices',headers=bearer)[0]==404,'foreign tenant uses same not-found boundary')
approval=f'/api/v1/tenants/{A}/operation-plans/{uuid.uuid4()}/authorizations'
check(request(approval,'POST',{'plan_hash':'0'*64},headers={**csrf,**bearer})[0]==403,'bearer cannot borrow valid browser cookie to approve even with membership and scope')
for token,label,expected in [(idp.token(aud='rmm-browser',typ='JWT'),'ID token',401),(idp.token(aud='rmm-browser'),'browser audience access token',401),(idp.token(iss='https://attacker.example/'),'wrong issuer',401),(idp.token(exp=int(time.time())-30),'expired token',401),(idp.token(scope='operations:read'),'missing scope',403),(idp.token(sub='unmapped-subject'),'unmapped identity',403)]:
check(request(f'/api/v1/tenants/{A}/devices',headers={'Authorization':'Bearer '+token})[0]==expected,label+' rejected')
oldkid=idp.kid;idp.kid='unknown-kid'
unknown=idp.token();idp.kid=oldkid
check(request(f'/api/v1/tenants/{A}/devices',headers={'Authorization':'Bearer '+unknown})[0]==401,'unknown kid rejected')
with idp.lock:idp.key=rsa.generate_private_key(public_exponent=65537,key_size=2048);idp.kid='rotated'
time.sleep(1.1)
check(request(f'/api/v1/tenants/{A}/devices',headers={'Authorization':'Bearer '+idp.token()})[0]==200,'real JWKS rotation refreshes configured issuer keys')
mcp={'Authorization':'Bearer '+idp.token(),'Accept':'application/json, text/event-stream','MCP-Protocol-Version':'2025-11-25'}
initialize={'jsonrpc':'2.0','id':1,'method':'initialize','params':{'protocolVersion':'2025-11-25','capabilities':{},'clientInfo':{'name':'oidc-test','version':'1'}}}
status,headers,_=request('/mcp','POST',initialize,headers=mcp)
check(status==200,'real MCP transport accepts targeted OAuth JWT')
session_id = next((v for k,v in headers if k.lower()=='mcp-session-id'),None)
if session_id:mcp['Mcp-Session-Id']=session_id
check(request('/mcp','POST',{'jsonrpc':'2.0','method':'notifications/initialized'},headers=mcp)[0]==202,'OIDC MCP initialized lifecycle')
call={'jsonrpc':'2.0','id':2,'method':'tools/call','params':{'name':'inventory.list_devices','arguments':{'tenant_id':A}}}
def result_json(raw):
text=raw.decode()
if text.startswith('event:') or text.startswith('data:'):
text=next(line[5:].strip() for line in text.splitlines() if line.startswith('data:'))
return json.loads(text)
status,_,raw=request('/mcp','POST',call,headers=mcp)
check(status==200 and not result_json(raw)['result'].get('isError',False),'actual OIDC MCP inventory tool uses current scoped identity')
restricted={**mcp,'Authorization':'Bearer '+idp.token(scope='operations:read')}
status,_,raw=request('/mcp','POST',call,headers=restricted)
check(status==200 and result_json(raw)['result'].get('isError',False),'MCP tool rejects insufficient access scope despite membership')
sql(f"UPDATE memberships SET revoked_at=now() WHERE principal='{principal}';")
check(request(f'/api/v1/tenants/{A}/devices',headers={'Authorization':'Bearer '+idp.token()})[0]==403 and request('/auth/session',headers={'Cookie':cookie})[0]==403,'membership revocation immediately affects JWT and browser session')
sql(f"UPDATE memberships SET revoked_at=NULL WHERE principal='{principal}'; UPDATE oidc_principals SET disabled=true WHERE principal='{principal}';")
check(request(f'/api/v1/tenants/{A}/devices',headers={'Authorization':'Bearer '+idp.token()})[0]==403,'disabled persistent external identity is denied')
sql(f"UPDATE oidc_principals SET disabled=false WHERE principal='{principal}';")
check(request('/mcp','POST',initialize,headers={'Authorization':'Bearer '+idp.token(aud='rmm-browser',typ='JWT'),'Accept':'application/json, text/event-stream'})[0]==401,'MCP rejects browser ID token')
check(request('/auth/logout','POST',{},headers=csrf)[0]==204 and request('/auth/session',headers={'Cookie':cookie})[0]==401,'local logout invalidates opaque RMM session')
idp.bad_nonce=True
callback,binding=flow()
check(request(callback,headers={'Cookie':binding})[0]==401,'nonce mismatch rejects signed ID token')
idp.bad_nonce=False
callback,binding=flow()
with idp.lock:
for value in idp.codes.values():value['code_challenge']='invalid-pkce'
check(request(callback,headers={'Cookie':binding})[0]==401,'provider-enforced PKCE failure rejects callback')
# Isolated real-browser truststore: never disable TLS or modify host roots.
home=folder/'browser-home';nss=home/'.pki/nssdb';nss.mkdir(parents=True,mode=0o700)
cache=ROOT/'web/node_modules/.cache'
certutil=shutil.which('certutil') or str(cache/'nss-tools/root/usr/bin/certutil')
quiet([certutil,'-N','-d','sql:'+str(nss),'--empty-password'])
for label,path in [('RMM server CA',str(CA)),('OIDC ephemeral CA',idp.ca)]:quiet([certutil,'-A','-d','sql:'+str(nss),'-n',label,'-t','C,,','-i',path])
browser_env={k:v for k,v in os.environ.items() if k not in ('DEBUG','PWDEBUG','NODE_DEBUG','RUST_LOG')}
browser_env.update(PLAYWRIGHT_BROWSERS_PATH=str(cache/'playwright-browsers'),RMM_OIDC_TEST_BROWSER_HOME=str(home),RMM_OIDC_TEST_ORIGIN=origin,RMM_OIDC_TEST_PRINCIPAL=principal)
STAGE = 'isolated Chromium OIDC flow'
quiet(['node',str(ROOT/'tests/oidc/browser.cjs')],env=browser_env,timeout=90)
check(True,'real Chromium UI OIDC authorization and cross-site callback, verified TLS and logout passed')
check(sql(f"SELECT count(*) FROM authorizations WHERE principal='{principal}';")=='0','no direct API/DB or browser plan authorization performed')
process.terminate();process.wait(timeout=10);process=None
cfg['testsimulation']=False;private(folder/'oidc.json',json.dumps(cfg))
server_env.update(RMM_PROFILE='production',RMM_DEV_AUTH='false')
for name in ('RMM_VIEWER_A','RMM_VIEWER_B','RMM_DEV_USERNAME','RMM_DEV_PASSWORD','RMM_OPERATOR_A','RMM_OPERATOR_B'):server_env.pop(name,None)
STAGE = 'configured production profile'
start()
STAGE = 'production dev-route denial'
check(request('/auth/dev/login','POST',{},headers={'Origin':origin})[0]==404,'configured production OIDC starts while dev login stays disabled')
STAGE = 'production non-simulated session'
callback,binding=flow()
status,headers,_=request(callback,headers={'Cookie':binding})
cookies=[v.split(';')[0] for k,v in headers if k.lower()=='set-cookie' and v.startswith('__Host-rmm_session=')]
check(status==303 and json.loads(request('/auth/session',headers={'Cookie':cookies[0]})[2])['testsimulation'] is False,'production OIDC session is non-simulated by server configuration, no role/group auto-grant')
print(f'OIDC server integration: {assertions} assertions passed; actual mock TLS/PG/Chromium only, no external IdP or production acceptance')
finally:
if process is not None:
process.terminate();process.wait(timeout=10)
if idp is not None:idp.server.shutdown();idp.server.server_close()
if principal is not None:
if state_key_ref is not None:
sql("DELETE FROM rmm_oidc_auth.login_states WHERE encryption_key_ref='"+state_key_ref.replace("'","''")+"';")
sql(f"DELETE FROM memberships WHERE principal='{principal}'; DELETE FROM oidc_principals WHERE principal='{principal}'; DROP OWNED BY {role}; DROP ROLE IF EXISTS {role};")
if __name__=='__main__':
try:main()
except Exception:
print('FAIL OIDC integration: '+STAGE+' (private details suppressed)',file=sys.stderr);sys.exit(1)
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
"""Local parser tests; no VM credentials, SSH connections, or remote changes."""
import importlib.util
from pathlib import Path
import tempfile
import unittest
SPEC = importlib.util.spec_from_file_location(
"vm_access_check", Path(__file__).resolve().parents[1] / "deploy/vm_access_check.py"
)
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
class ConfigTests(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.root = Path(self.directory.name)
self.previous_root = MODULE.ROOT
MODULE.ROOT = self.root
self.addCleanup(setattr, MODULE, "ROOT", self.previous_root)
(self.root / ".gitignore").write_text("credentials/\n")
credentials = self.root / "credentials"
credentials.mkdir(mode=0o700)
self.config = credentials / "linux-vm.conf"
def write(self, extra=""):
self.config.write_text("address=127.0.0.1\nusername=test\nport=22\n" + extra)
self.config.chmod(0o600)
def test_password_is_literal_data(self):
self.write("password='$(touch NOT_EXECUTED);`id`'\n")
self.assertEqual(MODULE.load_config(self.config)["password"], "$(touch NOT_EXECUTED);`id`")
self.assertFalse((self.root / "NOT_EXECUTED").exists())
def test_unknown_and_duplicate_fields_rejected(self):
for extra in ("unexpected=value\n", "username=other\n"):
with self.subTest(extra=extra):
self.write(extra)
with self.assertRaises(ValueError):
MODULE.load_config(self.config)
def test_insecure_file_rejected(self):
self.write()
self.config.chmod(0o644)
with self.assertRaises(ValueError):
MODULE.load_config(self.config)
def test_invalid_port_rejected(self):
self.write()
self.config.write_text("address=127.0.0.1\nusername=test\nport=65536\n")
with self.assertRaises(ValueError):
MODULE.load_config(self.config)
def test_missing_ignore_rule_rejected(self):
self.write()
(self.root / ".gitignore").write_text("")
with self.assertRaises(ValueError):
MODULE.load_config(self.config)
if __name__ == "__main__":
unittest.main()
+186
View File
@@ -0,0 +1,186 @@
#!/usr/bin/env python3
"""Local fail-closed readiness tests; no credentials, SSH or effects."""
from pathlib import Path
import json
import sys
import unittest
import sqlite3
import tempfile
from contextlib import redirect_stdout
from unittest.mock import patch
from types import SimpleNamespace
import io
import base64
import hashlib
import subprocess
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'deploy'))
import vm_job_test as vm
import vm_job_bootstrap as bootstrap
import vm_job_live as live
import vm_root_bootstrap as root_bootstrap
class ReadinessTests(unittest.TestCase):
def test_root_kernel_readiness_rejects_systemd_preexec_race(self):
mask = format(sum(1 << n for n in (0, 1, 3, 4, 6, 7, 8, 31)), 'x')
valid = dict(Uid='0 0 0 0', Gid='984 984 984 984', NoNewPrivs='1', Seccomp='2', CapEff=mask, CapBnd=mask)
self.assertTrue(root_bootstrap.confined_runtime(valid, 984))
self.assertTrue(root_bootstrap.confined_runtime(dict(valid, CapEff=format(int(mask, 16) & ~(1 << 7), 'x')), 984))
self.assertFalse(root_bootstrap.confined_runtime(dict(valid, CapEff=format(1 << 21, 'x')), 984))
for change in (dict(NoNewPrivs='0'), dict(Seccomp='0'), dict(Gid='0 0 0 0'), dict(CapBnd='1ffffffffff')):
self.assertFalse(root_bootstrap.confined_runtime(dict(valid, **change), 984))
def test_root_readiness_config_does_not_authorize_apt_reboot_or_root_scripts(self):
binding = dict(tenant_id='test', agent_id='test-agent', asset_id='test-asset', public_key_hex='public-test-key')
config = root_bootstrap.helper_config(binding, 993, 984)
self.assertEqual(config['allowed_services'], ['rmm-mvp-test.service'])
self.assertIsNone(config['apt_provider'])
self.assertFalse(config['allow_reboot'])
self.assertFalse(config['allow_root_scripts'])
self.assertEqual(config['binding']['public_key_hex'], '')
self.assertEqual(binding['public_key_hex'], 'public-test-key')
with self.assertRaises(RuntimeError):
root_bootstrap.helper_config(binding, 0, 984)
def test_root_cleanup_requires_proven_empty_consumption_ledger(self):
self.assertTrue(root_bootstrap.helper_can_stop({'helper_started': True, 'helper_consumed_count': 0}))
for count in (None, 1, 2):
self.assertFalse(root_bootstrap.helper_can_stop({'helper_started': True, 'helper_consumed_count': count}))
self.assertFalse(root_bootstrap.helper_can_stop({'helper_started': True}))
def test_actual_generated_root_program_never_echoes_exception_canary(self):
script = root_bootstrap.program("(_ for _ in ()).throw(ValueError('SECRET-ROOT-CANARY'))")
output = io.StringIO()
with redirect_stdout(output):
exec(compile(script, '<generated-root-bootstrap>', 'exec'), {})
response = json.loads(output.getvalue())
self.assertEqual(response['error']['exception_class'], 'ValueError')
self.assertTrue(response['error']['traceback_lines'])
self.assertNotIn('SECRET-ROOT-CANARY', output.getvalue())
def test_generated_root_program_has_real_manifest_and_hash_dependencies(self):
with tempfile.TemporaryDirectory() as folder:
path = Path(folder)
(path / 'identity').write_text('SECRET-ROOT-FILE-CANARY')
script = root_bootstrap.program('(sha(b"test"),manifest(Path(' + repr(folder) + ')))')
output = io.StringIO()
with redirect_stdout(output):
exec(compile(script, '<generated-root-bootstrap>', 'exec'), {})
response = json.loads(output.getvalue())
self.assertEqual(response['result'], [root_bootstrap.sha(b'test'), bootstrap.manifest(path)])
self.assertNotIn('SECRET-ROOT-FILE-CANARY', output.getvalue())
def test_lifecycle_never_stops_active_or_unknown_jobs(self):
self.assertTrue(live.can_stop([]))
self.assertTrue(live.can_stop(['succeeded', 'failed', 'expired', 'cancelled']))
for state in ('queued', 'accepted', 'running', 'verifying', 'unknown', 'awaiting_reboot', 'unrecognized'):
self.assertFalse(live.can_stop(['succeeded', state]))
self.assertFalse(live.can_stop(None))
def facts(self, **changes):
return dict(dict(identity_present=True, ssh_uid=1000, state_owner_uid=1000,
dedicated_account_present=True, agent_count=0,
port_occupied=False), **changes)
def test_existing_unprivileged_identity_is_only_readiness(self):
self.assertEqual(vm.readiness(self.facts()), [])
def test_root_is_not_agent_service_even_with_an_account(self):
self.assertIn('root_ssh_requires_existing_unprivileged_account_deployment',
vm.readiness(self.facts(ssh_uid=0)))
def test_root_without_account_and_root_state_blocks(self):
self.assertEqual(len(vm.readiness(self.facts(ssh_uid=0, state_owner_uid=0,
dedicated_account_present=False))), 3)
def test_missing_identity_never_requests_reenrollment(self):
self.assertEqual(vm.readiness(self.facts(identity_present=False)),
['preserved_device_identity_missing'])
def test_existing_agent_or_listener_not_stopped(self):
for change in ({'agent_count': 1}, {'port_occupied': True}):
self.assertEqual(vm.readiness(self.facts(**change)),
['existing_agent_or_tunnel_requires_lifecycle_review'])
def test_manifest_preserves_exact_bytes_and_sqlite_schema_rows(self):
with tempfile.TemporaryDirectory() as folder:
path = Path(folder)
(path / 'device.key').write_bytes(b'PRIVATE TEST BYTES\n')
with sqlite3.connect(path / 'journal.sqlite') as db:
db.execute('CREATE TABLE safety(key TEXT PRIMARY KEY,value TEXT)')
db.execute("INSERT INTO safety VALUES('connected','1')")
before = bootstrap.manifest(path)
self.assertEqual(before, bootstrap.manifest(path))
self.assertEqual(before['databases']['journal.sqlite']['tables']['safety']['rows'], 1)
self.assertNotIn('PRIVATE', json.dumps(before))
(path / 'device.key').write_bytes(b'CHANGED')
self.assertNotEqual(before, bootstrap.manifest(path))
def test_manifest_refuses_symlinks_and_external_hardlinks(self):
import os
with tempfile.TemporaryDirectory() as folder:
path = Path(folder)
(path / 'key').write_bytes(b'test')
(path / 'link').symlink_to(path / 'key')
with self.assertRaises(RuntimeError):
bootstrap.manifest(path)
(path / 'link').unlink()
os.link(path / 'key', path / 'link')
with self.assertRaises(RuntimeError):
bootstrap.manifest(path)
def run_generated(self, folder, fail_loader=False):
root = Path(folder)
for name in ('var/lib', 'etc/systemd/system', 'usr/local', 'original/state'):
(root / name).mkdir(parents=True, exist_ok=True)
state = root / 'original/state'
(state / 'agent.lock').touch()
(state / 'identity.json').write_text(json.dumps({'agent_id': 'test-agent', 'asset_id': 'test-asset'}))
(state / 'device.key').write_text('SECRET-CANARY-DEVICE-KEY')
with sqlite3.connect(state / 'journal.sqlite') as db:
db.execute('CREATE TABLE safety(key TEXT PRIMARY KEY,value TEXT)')
db.execute("INSERT INTO safety VALUES('connected','1')")
db.close()
elf = bytearray(64); elf[:6] = b'\x7fELF\x02\x01'; elf[18:20] = b'\x3e\x00'
payload = dict(source=str(root / 'original'), binding={'agent_id': 'test-agent', 'asset_id': 'test-asset'},
binary=base64.b64encode(elf).decode(), binary_sha256=hashlib.sha256(elf).hexdigest(),
ca='-----BEGIN CERTIFICATE-----\ntest\n', unit='test unit', previous_binary_sha256='unused')
script = bootstrap.remote_script(payload, staging_root=root)
account = SimpleNamespace(pw_uid=993, pw_gid=984, pw_shell='/usr/sbin/nologin')
def command(args, **kwargs):
if fail_loader and '--list' in args:
raise subprocess.CalledProcessError(127, ['SECRET-CANARY-ARGV'], stderr=b'SECRET-CANARY-STDERR')
return subprocess.CompletedProcess(args, 0, b'', b'')
output = io.StringIO()
with patch('pwd.getpwnam', side_effect=[KeyError(), account]), patch('grp.getgrnam', side_effect=KeyError()), \
patch('grp.getgrall', return_value=[]), patch('os.chown'), patch('os.fchown'), \
patch('subprocess.run', side_effect=command), redirect_stdout(output):
exec(compile(script, '<generated-bootstrap>', 'exec'), {})
self.assertNotIn('SECRET-CANARY', output.getvalue())
return root, json.loads(output.getvalue())
def test_real_generated_script_missing_binary_parent_reports_and_migrates(self):
with tempfile.TemporaryDirectory() as folder:
root, response = self.run_generated(folder)
self.assertIn('result', response)
report = json.loads((root / 'etc/rmm/vm-bootstrap-report.json').read_text())
self.assertTrue(report['migration_complete'])
self.assertTrue(report['byte_and_sqlite_preservation'])
self.assertFalse((root / 'original/state').exists())
self.assertEqual(report['before'], report['after'])
self.assertEqual((root / 'usr/local/libexec').stat().st_mode & 0o777, 0o755)
def test_generated_script_exception_is_sanitized_and_partial_report_persists(self):
with tempfile.TemporaryDirectory() as folder:
root, response = self.run_generated(folder, fail_loader=True)
self.assertEqual(response['error']['exception_class'], 'CalledProcessError')
self.assertTrue(all(isinstance(n, int) for n in response['error']['traceback_lines']))
report = json.loads((root / 'etc/rmm/vm-bootstrap-report.json').read_text())
self.assertFalse(report['migration_complete'])
self.assertEqual(report['attempts'][-1]['error']['exception_class'], 'CalledProcessError')
self.assertTrue((root / 'original/state/identity.json').is_file())
if __name__ == '__main__':
unittest.main()
+343
View File
@@ -0,0 +1,343 @@
#!/usr/bin/env python3
"""Pure acceptance-harness guards; no VM, credentials, API mutations or approval."""
import ast
import copy
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import ssl
import sys
import tempfile
import unittest
from unittest.mock import patch
from types import SimpleNamespace
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'deploy'))
import vm_mvp_acceptance as m
import vm_mvp_adapters as advanced
import vm_mvp_runtime as native
import vm_runtime_upgrade as upgrade
class Guards(unittest.TestCase):
def settings(self):
return m.config(dict(profile='dev/test', origin=m.ORIGIN, connect_address='127.0.0.1:18443', ca_file=str(m.CA),
tenant_id='11111111-1111-4111-8111-111111111111', asset_id='22222222-2222-4222-8222-222222222222',
agent_id='33333333-3333-4333-8333-333333333333', agent_uid=993))
def artifact(self):
return dict(profile='dev/test', **{k:v for k,v in self.settings().items() if k in ('tenant_id','asset_id','agent_id')},
approved_cases=['hello'], expires_at=(m.now()+dt.timedelta(minutes=5)).isoformat(),
permission_origin='explicit_chat_user_permission',chat_date='2026-11-12',testsimulation=True,
expected_principal='simulated-testprincipal-a')
def test_only_exact_loopback_tls_origin_and_nonroot_network_identity(self):
for field, value in [('profile','production'), ('origin','https://127.0.0.1:18443'),
('origin','https://user:secret@rmm-dev.internal:18443'),
('connect_address','0.0.0.0:18443'), ('ca_file','/not/trusted.pem'), ('agent_uid',0)]:
with self.assertRaises(m.GuardError): m.config(dict(self.settings(), **{field:value}))
def test_protected_files_duplicate_fields_modes_and_symlinks(self):
with tempfile.TemporaryDirectory() as folder:
path=Path(folder)/'input.json'; path.write_text('{"a":1,"a":2}'); path.chmod(0o600)
with self.assertRaises(m.GuardError): m.protected_json(path)
path.write_text('{}'); self.assertEqual(m.protected_json(path), {})
path.chmod(0o644)
with self.assertRaises(m.GuardError): m.protected_json(path)
link=Path(folder)/'link'; link.symlink_to(path)
with self.assertRaises(m.GuardError): m.protected_json(link)
def test_invalid_ca_cannot_establish_a_trust_context(self):
with tempfile.TemporaryDirectory() as folder:
path=Path(folder)/'invalid-ca.pem'; path.write_text('NOT A CERTIFICATE')
with self.assertRaises(ssl.SSLError): ssl.create_default_context(cafile=str(path))
def test_fixed_hello_bytes_identity_and_deadline(self):
case=m.case_input('hello',self.settings())
self.assertEqual(case['parameters']['variants'][0]['script_utf8'], "printf '%s\\n' 'Hello World'\n")
self.assertEqual(case['parameters']['expected_stdout'],'Hello World\n')
self.assertEqual(case['targets']['asset_ids'],[self.settings()['asset_id']])
self.assertEqual(case['parameters']['execution_identity'],'agent_service')
self.assertEqual(m.timestamp(case['schedule']['start_before'])-m.timestamp(case['schedule']['not_before']),dt.timedelta(minutes=15))
def test_bounded_logs_and_exact_service_unit_only(self):
logs=m.case_input('query_logs',self.settings())['parameters']
self.assertEqual(logs['unit'],'rmm-mvp-test.service');self.assertEqual(logs['max_lines'],50)
self.assertEqual(m.timestamp(logs['until'])-m.timestamp(logs['since']),dt.timedelta(minutes=5))
for name,state in [('service_start','running'),('service_stop','stopped')]:
case=m.case_input(name,self.settings())['parameters']
self.assertEqual((case['unit'],case['state'],case['execution_identity'],case['timeout_seconds']),
('rmm-mvp-test.service',state,'root',180))
setup=m.case_input('service_setup',self.settings())['parameters']
self.assertEqual(setup['execution_identity'],'root')
self.assertEqual(hashlib.sha256(setup['variants'][0]['script_utf8'].encode()).hexdigest(),
'159ff240504b8a350b46a56fdf9cd50e5768816de7e6683829f05144978449f0')
artifact=dict(self.artifact(),approved_cases=['service_setup','service_start','service_stop'])
m.validate_run_artifact(artifact,self.settings(),['service_setup','service_start','service_stop'])
with self.assertRaises(m.GuardError):m.validate_run_artifact(artifact,self.settings(),['service_start','service_setup','service_stop'])
def test_expired_or_unbounded_schedules_rejected(self):
for start,end in [(m.now()-dt.timedelta(minutes=2),m.now()-dt.timedelta(minutes=1)),
(m.now(),m.now()+dt.timedelta(hours=2))]:
with self.assertRaises(m.GuardError):m.schedule(start,end)
def test_explicit_scoped_artifact_no_pending_adapter_can_be_opted_in(self):
m.validate_run_artifact(self.artifact(),self.settings(),['hello'])
for changes,cases in [(dict(testsimulation=False),['hello']), (dict(profile='production'),['hello']),
(dict(expected_principal='real-person'),['hello']), ({},[]), ({},['hello','hello']),
(dict(approved_cases=['reboot_last','hello']),['reboot_last','hello']),
(dict(expires_at=(m.now()-dt.timedelta(seconds=1)).isoformat()),['hello'])]:
with self.assertRaises(m.GuardError):m.validate_run_artifact(dict(self.artifact(),**changes),self.settings(),cases)
def test_outstanding_unknown_or_replay_does_not_create_new_attempt(self):
m.safe_states(['succeeded','failed'])
for state in ('queued','accepted','running','verifying','unknown','awaiting_reboot'):
with self.assertRaises(m.GuardError):m.safe_states([state])
self.assertEqual(len(m.EXECUTABLE),15)
for name in ('duplicate_offer','lost_result_ack'):
with self.assertRaises(m.GuardError):m.case_input(name,self.settings())
def manifest(self):
pin='74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867'
transaction=dict(schema_version=1, recipe_id='rmm-apt-fixture-install-1.0',recipe_version='1',platform='ubuntu-24.04-amd64',
resolver_config_version='rmm-apt-local-v1',no_reboot=True,status_fingerprint='a'*64,baseline_sha256='b'*64,
packages=[dict(name='rmm-mvp-test',architecture='all',old_version=None,new_version='1.0',origin='rmm-test',sha256=pin,
size=20672,archive_path='/var/lib/rmm-apt-transaction/archives/'+pin+'.deb')])
raw=json.dumps(transaction,separators=(',',':')).encode()
return dict(type='rmm.apt.helper-action',schema_version=2,action='apt-native-acceptance',execution_profile='dev-test',
transaction_sha256=hashlib.sha256(raw).hexdigest(),resolved_apt_transaction=transaction,transaction_bytes=list(raw),
provider_sha256='c'*64,hook_sha256='d'*64,apt_config_sha256='e'*64,runtime_sha256='f'*64,test_result_id='fixture-only')
def test_complete_apt_fixture_format_is_not_execution_or_acceptance(self):
value=self.manifest();self.assertEqual(m.validate_apt_manifest(value)['packages'][0]['name'],'rmm-mvp-test')
for field,bad in [('schema_version',1),('transaction_bytes',[True]),('runtime_sha256','missing'),('extra','unsigned')]:
with self.assertRaises(m.GuardError):m.validate_apt_manifest(dict(value,**{field:bad}))
changed=copy.deepcopy(value);changed['resolved_apt_transaction']['no_reboot']=False
with self.assertRaises(m.GuardError):m.validate_apt_manifest(changed)
def test_reboot_same_boot_or_exit_code_is_not_success(self):
ids=[str(__import__('uuid').uuid4()) for _ in range(7)]
checkpoint=dict(zip(('tenant_id','agent_id','asset_id','operation_id','job_id','attempt_id','old_boot_id'),ids),plan_hash='a'*64)
job=dict(job_id=checkpoint['job_id'],attempt_id=checkpoint['attempt_id'],state='succeeded',result=dict(exit_code=0,evidence={}))
with self.assertRaises(m.GuardError):m.validate_reboot_checkpoint(checkpoint,checkpoint['old_boot_id'],job)
with self.assertRaises(m.GuardError):m.validate_reboot_checkpoint(checkpoint,str(__import__('uuid').uuid4()),job)
def test_no_direct_approval_or_database_mutation_in_harness(self):
source=Path(m.__file__).read_text();tree=ast.parse(source)
for call in ast.walk(tree):
if isinstance(call,ast.Call) and isinstance(call.func,ast.Attribute) and call.func.attr=='request':
self.assertIsInstance(call.args[0],ast.Constant)
self.assertIn(call.args[0].value,('/mcp','/auth/config'))
for value in ast.walk(tree):
if isinstance(value,ast.Constant) and isinstance(value.value,str):
self.assertFalse(value.value.upper().startswith(('INSERT INTO ','UPDATE ','DELETE FROM ','DROP TABLE ')))
browser=(m.ROOT/'tests/browser/mvp-actions.spec.ts').read_text()
self.assertNotRegex(browser,r'page\.request|request\.(post|put)\(|evaluate\(')
def test_readonly_preflight_calls_only_read_tools(self):
settings=self.settings()
class Fake:
def __init__(self):self.calls=[]
def request(self,path):return 200,{},dict(dev_login_enabled=True)
def outstanding_guard(self,settings):pass
def call(self,name,args):
self.calls.append(name)
if name=='tenants.list':return dict(items=[dict(tenant_id=settings['tenant_id'])])
if name=='inventory.list_devices':return dict(truncated=True)
return dict(agent_id=settings['agent_id'],asset_id=settings['asset_id'],presence='unreachable',capabilities=[])
fake=Fake();_,report=m.preflight(fake,settings)
self.assertEqual(fake.calls,['tenants.list','inventory.list_devices','inventory.get_device','monitoring.get_health'])
self.assertFalse(report['effects_performed'])
def planning(self,upgrade=False):
baseline={} if not upgrade else {'rmm-mvp-test:all':dict(version='1.0',status='install ok installed')}
runtime=dict(apt_config_sha256='e'*64)
return dict(fixture_package=baseline.get('rmm-mvp-test:all'),baseline=baseline,baseline_sha256=advanced.digest(advanced.canonical(baseline)),
sealed_baseline_sha256=advanced.digest(advanced.canonical(baseline)),status_records={},status_fingerprint=advanced.digest(b'{}'),
dpkg_updates_empty=True,pending_triggers_empty=True,runtime=runtime,runtime_sha256=advanced.digest(advanced.canonical(runtime)),
provider_sha256='c'*64,hook_sha256='d'*64,apt_config_sha256='e'*64,
prefetch={pin:dict(schema_version=1,sha256=pin,size=20672,sealed_at_ms=1) for pin in advanced.PINS.values()})
def test_all_four_apt_adapters_preserve_complete_typed_manifests(self):
for name in advanced.APT_CASES:
data=self.planning(name in ('apt_native_update','apply_updates','ensure_package'))
case=m.case_input(name,self.settings(),planning=data)
value=case['parameters']['apt_manifest'];m.validate_apt_manifest(value)
self.assertEqual(value['resolved_apt_transaction'],json.loads(bytes(value['transaction_bytes'])))
self.assertEqual(case['parameters']['execution_identity'],'root')
self.assertEqual(value['action'],'apt-native-acceptance' if name.startswith('apt_native_') else 'apt-execute')
self.assertEqual(case['parameters']['timeout_seconds'],300)
def test_no_live_ssh_planning_fallback_when_passive_data_missing_or_dirty(self):
with self.assertRaisesRegex(m.GuardError,'stored_linux.apt.planning'):advanced.planning_from_device(dict(inventory={}))
data=self.planning()
observation=dict(scope='linux.apt.planning',quality='valid',completeness='complete',truncated=False,observed_at=m.now().isoformat(),data=data)
self.assertEqual(advanced.planning_from_device(dict(inventory=dict(observations=[observation]))),data)
for changes in (dict(dpkg_updates_empty=False),dict(sealed_baseline_sha256='0'*64),dict(runtime_sha256='0'*64)):
with self.assertRaises(m.GuardError):advanced.planning_from_device(dict(inventory=dict(observations=[dict(observation,data=dict(data,**changes))])))
def test_no_fake_baseline_or_implicit_fixture_removal_to_repeat_acceptance(self):
data=self.planning();data['fixture_package']=dict(version='1.0',status='install ok installed')
with self.assertRaisesRegex(m.GuardError,'no_approved_removal'):m.case_input('apt_native_install',self.settings(),planning=data)
data=self.planning(True);data['fixture_package']=dict(version='1.1',status='install ok installed')
data['baseline']['rmm-mvp-test:all']=data['fixture_package']
data['baseline_sha256']=advanced.digest(advanced.canonical(data['baseline']))
data['sealed_baseline_sha256']=data['baseline_sha256']
self.assertEqual(m.case_input('apply_updates',self.settings(),planning=data)['parameters']['apt_manifest']['resolved_apt_transaction']['packages'][0]['new_version'],'1.1')
self.assertEqual(m.case_input('ensure_package',self.settings(),planning=data)['parameters']['version'],'1.1')
def test_final_upgrade_generated_program_has_real_dependencies_and_sanitized_errors(self):
import io
from contextlib import redirect_stdout
with tempfile.TemporaryDirectory() as folder:
path=Path(folder);(path/'identity').write_text('PRIVATE-UPGRADE-CANARY')
code=upgrade.program('(sha(b"known"),manifest(Path('+repr(folder)+')))')
output=io.StringIO()
with redirect_stdout(output):exec(compile(code,'<final-upgrade>','exec'),{})
result=json.loads(output.getvalue())
self.assertEqual(result['result'],[upgrade.sha(b'known'),upgrade.previous.manifest(path)])
self.assertNotIn('PRIVATE-UPGRADE-CANARY',output.getvalue())
output=io.StringIO()
with redirect_stdout(output):exec(compile(upgrade.program("(_ for _ in ()).throw(ValueError('PRIVATE-UPGRADE-ERROR'))"),'<final-upgrade>','exec'),{})
result=json.loads(output.getvalue());self.assertEqual(result['error']['exception_class'],'ValueError')
self.assertNotIn('PRIVATE-UPGRADE-ERROR',output.getvalue())
def test_real_cancel_expiry_and_reboot_plans_have_fixed_noop_or_explicit_root(self):
start=m.now()
cancel=m.case_input('cancel_queued',self.settings(),start)
self.assertEqual(m.timestamp(cancel['schedule']['not_before'])-start,dt.timedelta(minutes=3))
expiry=m.case_input('expired_offline',self.settings(),start)
self.assertEqual(m.timestamp(expiry['schedule']['start_before'])-start,dt.timedelta(seconds=60))
reboot=m.case_input('reboot_last',self.settings(),start)
self.assertEqual(reboot['action'],'reboot');self.assertEqual(reboot['parameters']['execution_identity'],'root')
self.assertEqual(reboot['parameters']['reconnect_timeout_seconds'],180)
def test_no_native_actor_or_server_restart_from_readonly_default(self):
with patch.object(sys,'argv',['vm_mvp_acceptance.py']),patch.object(m,'default_config',return_value=self.settings()), \
patch.object(m,'Backend') as backend,patch.object(m,'preflight',return_value=({},{})), \
patch.object(native,'OwnedRuntime') as actor:
m.main();actor.assert_not_called();backend.assert_called_once()
def test_native_action_requires_independent_helper_policy_no_capability_override(self):
actor=object.__new__(native.OwnedRuntime);actor.settings=self.settings()
request=m.case_input('reboot_last',self.settings())
capabilities=['reboot','rmm.sync.v1','helper.root.v1','root:reboot','helper.policy.sha256:'+'a'*64]
actor.backend=SimpleNamespace(call=lambda *args:dict(capabilities=capabilities))
actor.wait_sync=lambda:dict(helper_ready=True)
actor.control_probe=lambda *args:dict(policy_sha256='a'*64,policy=dict(allow_reboot=False))
with self.assertRaisesRegex(m.GuardError,'allow_reboot_false'):actor.require_action(request)
def test_cancel_adapter_orders_pause_approve_cancel_resume_without_new_attempt(self):
events=[];checkpoint=dict(job_id='44444444-4444-4444-8444-444444444444',operation_id='55555555-5555-4555-8555-555555555555',
attempt_id='66666666-6666-4666-8666-666666666666',plan_hash='a'*64)
class Backend:
def outstanding_guard(self,settings):events.append('guard')
def call(self,name,args):
events.append(name)
return {} if name=='inventory.get_device' else dict(state='cancelled')
def counts(self,*args):return [1,1,0]
actor=SimpleNamespace(require_action=lambda request:events.append('require_action'),server_state=lambda:dict(boot_id='old'),
pause_idle=lambda:events.append('pause'),resume_after_offline=lambda job:events.append('resume'))
with tempfile.TemporaryDirectory() as folder,patch.object(advanced,'authorize',side_effect=lambda *a,**k:events.append('ui_approve') or checkpoint), \
patch.object(advanced,'wait_job',return_value=dict(state='cancelled')),patch.object(advanced,'audit',return_value={}):
advanced.execute_advanced(Backend(),self.settings(),self.artifact(),'cancel_queued',Path(folder),actor)
self.assertLess(events.index('pause'),events.index('ui_approve'))
self.assertLess(events.index('ui_approve'),events.index('jobs.cancel'))
self.assertLess(events.index('jobs.cancel'),events.index('resume'))
def test_fault_cases_never_create_an_extra_plan_or_claim_injection_passed(self):
backend=SimpleNamespace(outstanding_guard=lambda settings:None,call=lambda *args:(_ for _ in ()).throw(AssertionError('no new MCP plan')))
actor=SimpleNamespace(replay_original_result=lambda checkpoint:dict(regression_verified=True))
with tempfile.TemporaryDirectory() as folder:
folder=Path(folder);m.write_private(folder/'hello-result.json',dict(job_id='test'))
advanced.execute_advanced(backend,self.settings(),self.artifact(),'lost_result_ack',folder,actor)
result=m.protected_json(folder/'lost_result_ack-result.json')
self.assertFalse(result['native_fault_test_passed'])
self.assertEqual(result['fault_injection'],'unsupported_native_fault_injection')
def test_chat_permission_is_never_a_native_readiness_boolean(self):
artifact=self.artifact()
self.assertNotIn('wiring_verified',artifact)
with self.assertRaises(m.GuardError):m.validate_run_artifact(dict(artifact,wiring_verified=True),self.settings(),['hello'])
with self.assertRaises(m.GuardError):native.OwnedRuntime(self.settings(),None,dict(agent_id=self.settings()['agent_id'],ready=True))
def test_bootstrap_cannot_start_foreign_units_or_sample_business_baseline(self):
actor=object.__new__(native.OwnedRuntime)
actor.verdict=dict(helper_units={'sshd.service':'a'*64})
actor.remote=lambda code:(_ for _ in ()).throw(AssertionError('must not dispatch'))
with self.assertRaises(m.GuardError):actor.start_helpers()
source=Path(native.__file__).read_text()
self.assertNotIn('prepare-baseline',source)
self.assertNotIn('provision-fixture-store',source)
self.assertNotIn('dpkg-query',source)
self.assertNotIn('os.kill(',source)
def test_planning_projection_fits_bounded_observation_without_raw_inventory(self):
data=self.planning();data.pop('baseline');data.pop('status_records')
observation=dict(scope='linux.apt.planning',quality='valid',completeness='complete',truncated=False,observed_at=m.now().isoformat(),data=data)
self.assertLess(len(advanced.canonical(data)),32768)
self.assertEqual(advanced.planning_from_device(dict(inventory=dict(observations=[observation]))),data)
def test_native_replay_program_compiles_and_keeps_same_result_attempt_and_grant(self):
actor=object.__new__(native.OwnedRuntime);actor.settings=self.settings()
checkpoint=dict(job_id='44444444-4444-4444-8444-444444444444',attempt_id='55555555-5555-4555-8555-555555555555')
actor.backend=SimpleNamespace(call=lambda *a:dict(state='succeeded'),counts=lambda *a:[1,1,1],sql=lambda *a:'1')
actor.server_state=lambda:dict(fingerprint='a'*64)
def capture(body):
compile(body,'<native-original-result-replay>','exec')
self.assertIn('SELECT envelope FROM reconcile_results',body)
self.assertIn('result=fields(outer[25][0])',body)
self.assertIn("server_hostname='rmm-dev.internal'",body)
self.assertNotIn('UPDATE ',body)
return dict(message_id='66666666-6666-4666-8666-666666666666')
actor.remote=capture
result=actor.replay_original_result(checkpoint)
self.assertTrue(result['regression_verified']);self.assertEqual(result['server_receipts'],1)
def test_runtime_readiness_script_is_effect_free_until_explicit_acquire(self):
actor=object.__new__(native.OwnedRuntime)
def capture(body):
compile(body,'<native-technical-inspect>','exec')
self.assertNotIn('prepare-baseline',body);self.assertNotIn('dpkg-query',body)
self.assertNotIn('systemctl\',\'start',body)
return dict(pid=0)
actor.remote=capture
self.assertEqual(actor.inspect(),dict(pid=0))
def test_root_requester_uses_human_UI_preparation_not_automation_scope_override(self):
settings=self.settings();job='44444444-4444-4444-8444-444444444444';attempt='55555555-5555-4555-8555-555555555555'
operation='66666666-6666-4666-8666-666666666666';raw=b'{"fixture":true}';hash_value=hashlib.sha256(raw).hexdigest()
plan=dict(plan_hash=hash_value,plan_payload_bytes=list(raw),jobs=[dict(job_id=job,attempt_id=attempt,
agent_id=settings['agent_id'],asset_id=settings['asset_id'],job_payload_hash='a'*64,payload=dict(execution_identity='root'))])
pending=dict(operation_id=operation,approval_state='pending_approval',job_count=0,execution_state=None)
calls=[]
def call(name,args):
calls.append(name);self.assertEqual(name,'jobs.get')
return dict(plan_hash=hash_value,approval_state='pending_approval')
backend=SimpleNamespace(call=call,counts=lambda *a:[0,0,0])
with tempfile.TemporaryDirectory() as folder:
folder=Path(folder)
def browser(argv,**kwargs):
if '--prepare-only' in argv:
m.write_private(folder/'service_start-prepared.json',dict(plan=plan,operation=pending))
else:
checkpoint=m.protected_json(folder/'service_start-checkpoint.json')
self.assertEqual(checkpoint['job_id'],job)
return SimpleNamespace(returncode=0)
with patch.object(advanced.subprocess,'run',side_effect=browser):
result=advanced.authorize(backend,settings,self.artifact(),'service_start',folder,m.case_input('service_start',settings))
self.assertEqual(result['operation_id'],operation)
self.assertEqual(calls,['jobs.get'])
def test_apt_profile_cannot_hide_consumption_in_a_private_ledger_copy(self):
actor=object.__new__(native.OwnedRuntime)
common=dict(ledger_device=1,ledger_inode=2,lock_device=1,lock_inode=3)
actor.control_probe=lambda profile:dict(common,ledger_inode=4) if profile=='apt_only' else common
with self.assertRaisesRegex(m.GuardError,'exact_ledger'):actor.apt_probe()
actor.control_probe=lambda profile:common
self.assertEqual(actor.apt_probe(),common)
if __name__=='__main__':unittest.main()
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""Local orchestration guard tests; no Docker, credentials, or SSH access."""
from pathlib import Path
import subprocess
import sys
import unittest
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "deploy"))
import vm_passive_test as vm
class OrchestrationTests(unittest.TestCase):
def test_require_fails_closed(self):
vm.require(True, "valid")
with self.assertRaisesRegex(RuntimeError, "blocked"):
vm.require(False, "blocked")
def test_failed_command_does_not_echo_private_diagnostics(self):
result = subprocess.CompletedProcess(["tool"], 1, b"PRIVATE STDOUT", b"PRIVATE STDERR")
with patch.object(vm.subprocess, "run", return_value=result):
with self.assertRaises(RuntimeError) as error:
vm.execute(["tool"])
self.assertNotIn("PRIVATE", str(error.exception))
def test_remote_code_uses_stdin_not_shell_interpolation(self):
code = "print('literal $(not_a_shell_command)')"
with patch.object(vm, "execute", return_value=b"ok") as execute:
result = vm.remote_python(["ssh", "-T"], "test-target", {"TEST": "value"}, code)
self.assertEqual(result, b"ok")
execute.assert_called_once_with(
["ssh", "-T", "test-target", "python3 -"],
env={"TEST": "value"}, input=code.encode(),
)
if __name__ == "__main__":
unittest.main()