feat: deploy cloud-only RMM and verify audited APT diagnostics
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { defineConfig } from '../../web/node_modules/@playwright/test/index.js';
|
||||
import { resolve } from 'node:path';
|
||||
import { readdirSync, existsSync } from 'node:fs';
|
||||
|
||||
const root = resolve(__dirname, '../..');
|
||||
const browsers = resolve(root, 'web/node_modules/.cache/playwright-browsers');
|
||||
const version = readdirSync(browsers).find(x => /^chromium-/.test(x));
|
||||
if (!version) throw new Error('Installed Chromium required');
|
||||
const executablePath = ['chrome-linux/chrome', 'chrome-linux64/chrome']
|
||||
.map(x => resolve(browsers, version, x)).find(x => existsSync(x));
|
||||
if (!executablePath) throw new Error('Installed Chromium required');
|
||||
export default defineConfig({
|
||||
testDir: '../../tests/browser', testMatch: 'mvp-actions.spec.ts', workers: 1,
|
||||
fullyParallel: false, retries: 0, timeout: 120000,
|
||||
reporter: [['../../tests/browser/safe-reporter.ts']],
|
||||
outputDir: '../../web/node_modules/.cache/cloud-acceptance/results',
|
||||
use: { browserName: 'chromium', baseURL: 'https://localhost:18443',
|
||||
actionTimeout: 15000, navigationTimeout: 20000,
|
||||
ignoreHTTPSErrors: false, timezoneId: 'UTC', trace: 'off', screenshot: 'off', video: 'off',
|
||||
launchOptions: { executablePath, env: { ...process.env,
|
||||
HOME: resolve(root, 'web/node_modules/.cache/cloud-acceptance/home') } } },
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
// Actual UI only. No page.request, fetch injection, approval API or DB access.
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const crypto = require('node:crypto');
|
||||
const { chromium } = require('../../web/node_modules/playwright');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
const assert = (value) => { if (!value) throw new Error('UI contract mismatch'); };
|
||||
(async () => {
|
||||
const file = path.resolve(process.argv[2]);
|
||||
const stat = fs.lstatSync(file);
|
||||
assert(stat.isFile() && !stat.isSymbolicLink() && stat.uid === process.getuid() && (stat.mode & 0o777) === 0o600);
|
||||
const expected = JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||
assert(expected.profile === 'dev/test' && ['hello', 'refresh_inventory', 'cancel_queued'].includes(expected.case));
|
||||
assert(expected.expected_principal === 'simulated-testprincipal-a');
|
||||
const loginFile = path.join(root, 'credentials/server-browser-login.json');
|
||||
const loginStat = fs.lstatSync(loginFile);
|
||||
assert(loginStat.isFile() && !loginStat.isSymbolicLink() && loginStat.uid === process.getuid() && (loginStat.mode & 0o777) === 0o600);
|
||||
const login = JSON.parse(fs.readFileSync(loginFile, 'utf8'));
|
||||
const cache = path.join(root, 'web/node_modules/.cache/cloud-acceptance');
|
||||
const browserRoot = path.join(root, 'web/node_modules/.cache/playwright-browsers');
|
||||
const version = fs.readdirSync(browserRoot).find(x => /^chromium-/.test(x));
|
||||
const candidates = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browserRoot, version, x));
|
||||
const browser = await chromium.launch({ headless: true, executablePath: candidates.find(x => fs.existsSync(x)),
|
||||
env: { ...process.env, HOME: path.join(cache, 'home') } });
|
||||
try {
|
||||
const page = await browser.newPage({ ignoreHTTPSErrors: false });
|
||||
await page.goto('https://localhost:18443/');
|
||||
await page.getByLabel('Benutzername').fill(login.username);
|
||||
await page.getByLabel('Passwort', { exact: true }).fill(login.password);
|
||||
const sessionResponse = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200);
|
||||
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
|
||||
const session = await (await sessionResponse).json();
|
||||
assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' && session.principal === expected.expected_principal);
|
||||
await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor();
|
||||
await page.getByRole('button', { name: expected.operation_id, exact: true }).click();
|
||||
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Antrag / Ausführung ' + expected.operation_id, exact: true }) });
|
||||
const pending = JSON.parse(await op.locator('pre').first().innerText());
|
||||
assert(pending.approval_state === 'pending_approval' && pending.job_count === 0 && pending.execution_state === null);
|
||||
await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen', exact: true }).click();
|
||||
const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) });
|
||||
const rendered = JSON.parse(await detail.locator('pre').first().innerText());
|
||||
assert(rendered.plan_hash === expected.plan.plan_hash && crypto.createHash('sha256').update(Buffer.from(rendered.plan_payload_bytes)).digest('hex') === expected.plan.plan_hash);
|
||||
assert(JSON.stringify(rendered.plan_payload_bytes) === JSON.stringify(expected.plan.plan_payload_bytes));
|
||||
assert(rendered.jobs.length === 1 && rendered.resolved_targets.length === 1);
|
||||
const job = rendered.jobs[0]; const payload = job.payload;
|
||||
assert(job.job_payload_hash === expected.job.job_payload_hash && JSON.stringify(payload) === JSON.stringify(expected.job.payload));
|
||||
assert(payload.agent_id === expected.agent_id && payload.asset_id === expected.asset_id && payload.tenant_id === expected.tenant_id);
|
||||
assert(payload.execution_identity === 'agent_service' && payload.max_runtime_seconds === 60 && payload.max_output_bytes <= 65536);
|
||||
assert(Date.parse(payload.start_before) > Date.now());
|
||||
if (expected.case === 'hello' || expected.case === 'cancel_queued') assert(payload.action.action === 'run_script' && payload.action.interpreter === 'posix_sh' && payload.action.script_utf8 === "printf '%s\\n' 'Hello World'\n" && payload.action.expected_stdout === 'Hello World\n');
|
||||
else assert(payload.action.action === 'refresh_inventory' && payload.action.scope === 'linux.base');
|
||||
const approve = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true });
|
||||
assert(await approve.isDisabled());
|
||||
await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
|
||||
await approve.click();
|
||||
await op.locator('p.warning').filter({ hasText: 'Antrag: approved' }).waitFor();
|
||||
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
|
||||
} finally { await browser.close(); }
|
||||
})().catch(error => { console.log('CLOUD_UI_INCOMPLETE:' + error.constructor.name); process.exitCode = 1; });
|
||||
@@ -0,0 +1,94 @@
|
||||
// Actual UI interactions only; credentials remain in the protected login file.
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const crypto = require('node:crypto');
|
||||
const { isDeepStrictEqual } = require('node:util');
|
||||
const { chromium } = require('../../web/node_modules/playwright');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
const assert = value => { if (!value) throw new Error('Diagnostic UI contract mismatch'); };
|
||||
function readPrivate(file) {
|
||||
const s = fs.lstatSync(file);
|
||||
assert(s.isFile() && !s.isSymbolicLink() && s.uid === process.getuid() && (s.mode & 0o777) === 0o600 && s.size < 1048576);
|
||||
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||
}
|
||||
(async () => {
|
||||
const file = path.resolve(process.argv[2]); const expected = readPrivate(file);
|
||||
assert(expected.profile === 'dev/test' && expected.case === 'apt_diagnostic_v1' && expected.expected_principal === 'simulated-testprincipal-a');
|
||||
const source = fs.readFileSync(path.join(root, 'deploy/linux/apt-diagnostic.py'), 'utf8');
|
||||
const script = "exec /usr/bin/python3 -I - <<'RMM_APT_DIAGNOSTIC'\n" + source + '\nRMM_APT_DIAGNOSTIC\n';
|
||||
const login = readPrivate(path.join(root, 'credentials/server-browser-login.json'));
|
||||
const browsers = path.join(root, 'web/node_modules/.cache/playwright-browsers');
|
||||
const version = fs.readdirSync(browsers).find(x => /^chromium-/.test(x));
|
||||
const executablePath = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browsers, version, x)).find(x => fs.existsSync(x));
|
||||
const browser = await chromium.launch({ headless: true, executablePath,
|
||||
env: { ...process.env, HOME: path.join(root, 'web/node_modules/.cache/cloud-acceptance/home') } });
|
||||
try {
|
||||
const page = await browser.newPage({ ignoreHTTPSErrors: false, timezoneId: 'UTC' });
|
||||
page.setDefaultTimeout(15000);
|
||||
let approvals = 0;
|
||||
page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) approvals++; });
|
||||
await page.goto('https://localhost:18443/');
|
||||
await page.getByLabel('Benutzername').fill(login.username);
|
||||
await page.getByLabel('Passwort', { exact: true }).fill(login.password);
|
||||
const response = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200);
|
||||
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
|
||||
const session = await (await response).json();
|
||||
assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' && session.principal === expected.expected_principal);
|
||||
await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor();
|
||||
if (expected.phase === 'prepare') {
|
||||
assert(expected.request.parameters.variants[0].script_utf8 === script && expected.request.parameters.expected_stdout === null);
|
||||
await page.getByRole('checkbox', { name: 'Ziel ' + expected.asset_id, exact: true }).check();
|
||||
await page.getByLabel('Aktion', { exact: true }).selectOption('run_script');
|
||||
await page.getByRole('combobox', { name: 'Interpreter', exact: true }).selectOption('posix_sh');
|
||||
await page.getByLabel('Exakte UTF-8 Skriptbytes', { exact: false }).fill(script);
|
||||
await page.getByLabel('Erwartete stdout-Ausgabe', { exact: false }).fill('');
|
||||
await page.getByLabel('Ausführungsidentität', { exact: true }).selectOption('root');
|
||||
await page.getByLabel('Laufzeitlimit (s)', { exact: true }).fill('90');
|
||||
await page.getByLabel('Ausgabelimit (Bytes)', { exact: true }).fill('16384');
|
||||
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(expected.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 }) });
|
||||
const plan = JSON.parse(await detail.locator('pre').innerText());
|
||||
assert(crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === 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 op.locator('pre').first().filter({ hasText: '"job_count": 0' }).waitFor();
|
||||
const operation = JSON.parse(await op.locator('pre').first().innerText());
|
||||
assert(operation.approval_state === 'pending_approval' && operation.execution_state === null && approvals === 0);
|
||||
assert(path.dirname(path.resolve(expected.output_file)) === path.dirname(file));
|
||||
fs.writeFileSync(expected.output_file, JSON.stringify({ plan, operation }), { flag: 'wx', mode: 0o600 });
|
||||
} else {
|
||||
assert(expected.phase === 'approve');
|
||||
await page.getByRole('button', { name: expected.operation_id, exact: true }).click();
|
||||
const op = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Antrag / Ausführung ' + expected.operation_id, exact: true }) });
|
||||
const pending = JSON.parse(await op.locator('pre').first().innerText());
|
||||
assert(pending.approval_state === 'pending_approval' && pending.job_count === 0 && pending.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 }) });
|
||||
const plan = JSON.parse(await detail.locator('pre').innerText());
|
||||
assert(plan.plan_hash === expected.plan.plan_hash && crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === plan.plan_hash);
|
||||
assert(JSON.stringify(plan.plan_payload_bytes) === JSON.stringify(expected.plan.plan_payload_bytes));
|
||||
assert(plan.jobs.length === 1 && plan.resolved_targets.length === 1);
|
||||
const job = plan.jobs[0]; const p = job.payload;
|
||||
assert(isDeepStrictEqual(job, expected.plan.jobs[0]));
|
||||
assert(p.agent_id === expected.agent_id && p.asset_id === expected.asset_id && p.tenant_id === expected.tenant_id);
|
||||
assert(p.execution_identity === 'root' && p.max_runtime_seconds === 90 && p.max_output_bytes === 16384 && Date.parse(p.start_before) > Date.now());
|
||||
assert(isDeepStrictEqual(p.action, { action: 'run_script', interpreter: 'posix_sh', script_utf8: script, arguments: [], expected_stdout: null }));
|
||||
const button = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true });
|
||||
assert(await button.isDisabled());
|
||||
await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check();
|
||||
await button.click();
|
||||
await op.locator('p.warning').filter({ hasText: 'Antrag: approved' }).waitFor();
|
||||
}
|
||||
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
|
||||
} finally { await browser.close(); }
|
||||
})().catch(error => {
|
||||
const line = error.stack?.match(/cloud_apt_diagnostic\.cjs:(\d+):\d+/)?.[1] || 'unknown';
|
||||
console.log('CLOUD_APT_DIAGNOSTIC_UI_INCOMPLETE:' + error.constructor.name + ':source_line=' + line);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Cloud-only existing UI contracts; never starts a server, agent or fixture."""
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
import cloud_mvp_acceptance as cloud
|
||||
|
||||
CASES=('service_setup','service_start','service_stop','apt_native_install','apt_native_update','ensure_package','apply_updates')
|
||||
|
||||
|
||||
def run(file,prepare=False):
|
||||
value=cloud.contract.protected_json(file)
|
||||
cloud.require(value.get('profile')=='dev/test' and value.get('case') in CASES and
|
||||
value.get('expected_principal')=='simulated-testprincipal-a','fixed cloud root cases only')
|
||||
if prepare:
|
||||
cloud.require(value.get('phase')=='prepare' and value['request']['parameters']['execution_identity']=='root','root preparation only')
|
||||
cloud.prepare_browser()
|
||||
env={k:v for k,v in os.environ.items() if k not in ('DEBUG','PWDEBUG','NODE_DEBUG')}
|
||||
env.update(RMM_E2E_PROFILE='dev/test',RMM_E2E_MODE='mvp-prepare' if prepare else 'mvp-actions',
|
||||
RMM_E2E_CASE_FILE=str(Path(file).resolve()),RMM_E2E_LOGIN_FILE=str(cloud.ROOT/'credentials/server-browser-login.json'),TZ='UTC')
|
||||
result=subprocess.run(['node',str(cloud.ROOT/'web/node_modules/@playwright/test/cli.js'),'test',
|
||||
'--config',str(cloud.ROOT/'deploy/browser/cloud-root.config.ts')],cwd=cloud.ROOT,env=env,
|
||||
capture_output=True,timeout=150)
|
||||
# The dedicated reporter emits only generic statuses. Never publish raw
|
||||
# browser failures, request headers, page state or entered credentials.
|
||||
if result.returncode!=0:
|
||||
lines=re.findall(rb'Cloud/root UI failure source_line=(\d+)',result.stdout)
|
||||
raise cloud.contract.GuardError('cloud_root_UI_incomplete_no_automatic_repeat; source_lines='+','.join(x.decode('ascii') for x in lines))
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--case-file',required=True)
|
||||
parser.add_argument('--prepare-only',action='store_true');args=parser.parse_args()
|
||||
try:run(args.case_file,args.prepare_only)
|
||||
except Exception:
|
||||
print('CLOUD_ROOT_UI_INCOMPLETE; no automatic repeat');raise SystemExit(2) from None
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Cloud APT diagnosis: no install/retry, actual pending + separate UI approval."""
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
import cloud_mvp_acceptance as cloud
|
||||
import cloud_root_policy
|
||||
|
||||
ROOT=cloud.ROOT
|
||||
require=cloud.require
|
||||
STATE=ROOT/'devstate/cloud-apt-followup'
|
||||
|
||||
|
||||
def narrow_policy(payload):
|
||||
import datetime as dt
|
||||
p=Path('/opt/rust-discovery/devstate/root-policy/policy.json')
|
||||
require(not p.is_symlink() and p.stat().st_uid==10001 and p.stat().st_mode&0o777==0o600,'unsafe policy')
|
||||
old=p.read_bytes();require(hashlib.sha256(old).hexdigest()==payload['expected_sha256'],'existing policy changed')
|
||||
require(dt.datetime.fromisoformat(json.loads(old)['expires_at'])<dt.datetime.now(dt.timezone.utc),'previous scope still active')
|
||||
policy=payload['policy'];require(len(policy['scopes'])==1,'one diagnosis scope')
|
||||
scope=policy['scopes'][0]
|
||||
require(scope['allow_root_scripts'] and not scope['allow_reboot'] and not scope['allowed_services'] and
|
||||
scope['principals']==['simulated-testprincipal-a'] and 'apt' not in scope,'diagnosis-only root limits')
|
||||
backup=p.parents[1]/('root-policy-bootstrap-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
fd=os.open(backup/'previous-policy.json',os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'wb') as f:f.write(old);f.flush();os.fsync(f.fileno())
|
||||
next_path=p.with_suffix('.diagnosis-next')
|
||||
fd=os.open(next_path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as f:json.dump(policy,f);f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),10001,10001)
|
||||
os.replace(next_path,p)
|
||||
return dict(policy_sha256=hashlib.sha256(p.read_bytes()).hexdigest(),backup=str(backup),expires_at=policy['expires_at'],approvals_created=False)
|
||||
|
||||
|
||||
def identity():
|
||||
return dict(tenant_id=cloud.TENANT,**cloud.contract.protected_json(ROOT/'devstate/remote-vm.json')['identity'])
|
||||
|
||||
|
||||
def evidence(kind,**extra):
|
||||
target=identity()
|
||||
return cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind=kind,tenant_id=target['tenant_id'],agent_id=target['agent_id'],**extra))
|
||||
|
||||
|
||||
def activate():
|
||||
require(evidence('gate')['unresolved']==0,'unresolved central job')
|
||||
closed=list((ROOT/'devstate/cloud-root').glob('policy-closed-*.json'));require(len(closed)==1,'one prior closed policy')
|
||||
previous=cloud.contract.protected_json(closed[0])
|
||||
original=[cloud.contract.protected_json(p) for p in (ROOT/'devstate/cloud-root').glob('policy-*.json')]
|
||||
original=next(p for p in original if 'policy' in p)['policy']
|
||||
scope=next(s for s in original['scopes'] if s['allow_root_scripts'])
|
||||
scope=dict(scope,allowed_services=[],acceptance_evidence_id='cloud-apt-readonly-diagnostic-v1')
|
||||
policy=dict(schema_version=1,profile='development',expires_at=(dt.datetime.now(dt.timezone.utc)+dt.timedelta(hours=1)).isoformat(),scopes=[scope])
|
||||
result=cloud.bootstrap.rpc('server.conf',narrow_policy,dict(id=str(uuid.uuid4()),expected_sha256=previous['policy_sha256'],policy=policy))
|
||||
cloud.contract.write_private(STATE/'diagnostic-policy.json',result)
|
||||
print('Diagnostic scope active; no APT/service/reboot permission or job approval')
|
||||
|
||||
|
||||
def diagnose():
|
||||
m=cloud.contract;settings=identity();require(evidence('gate')['unresolved']==0,'unresolved central job')
|
||||
backend=cloud.Backend();device=backend.call('inventory.get_device',{k:settings[k] for k in ('tenant_id','asset_id')})
|
||||
require(device['presence']=='online' and {'root:run_script','rmm.sync.v1','helper.root.v1'}<=set(device['capabilities']),'online root helper required')
|
||||
source=(ROOT/'deploy/linux/apt-diagnostic.py').read_text()
|
||||
script="exec /usr/bin/python3 -I - <<'RMM_APT_DIAGNOSTIC'\n"+source+'\nRMM_APT_DIAGNOSTIC\n'
|
||||
request=m.case_input('service_setup',settings)
|
||||
request['parameters'].update(variants=[dict(os_family='linux',interpreter='posix_sh',encoding='utf-8',script_utf8=script)],
|
||||
timeout_seconds=90,max_output_bytes=16384,expected_stdout=None)
|
||||
request['reason']='APT Exit 100 diagnosis: two dry-run solver checks; no package transaction; preserve failed attempt'
|
||||
folder=STATE/'diagnosis';folder.mkdir(mode=0o700,exist_ok=False)
|
||||
prepare=folder/'prepare.json';output=folder/'prepared.json'
|
||||
common=dict(profile='dev/test',case='apt_diagnostic_v1',expected_principal='simulated-testprincipal-a',**settings)
|
||||
m.write_private(prepare,dict(common,phase='prepare',request=request,output_file=str(output)))
|
||||
cloud.prepare_browser()
|
||||
def browser(file):
|
||||
process=subprocess.run(['node',str(ROOT/'deploy/browser/cloud_apt_diagnostic.cjs'),str(file)],capture_output=True,timeout=120)
|
||||
require(process.returncode==0,'UI incomplete; preserve checkpoint; no automatic repeat')
|
||||
print('Diagnostic: actual UI preparation',flush=True);browser(prepare)
|
||||
prepared=m.protected_json(output);plan=prepared['plan'];operation=prepared['operation'];job=plan['jobs'][0]
|
||||
require(len(plan['jobs'])==1 and hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest()==plan['plan_hash'],'fixed immutable plan')
|
||||
stored=backend.call('jobs.get',dict(tenant_id=settings['tenant_id'],operation_id=operation['operation_id']))
|
||||
before=evidence('gate',job_id=job['job_id'])
|
||||
require(stored['approval_state']=='pending_approval' and stored['plan_hash']==plan['plan_hash'] and stored['job_count']==0
|
||||
and stored['execution_state'] is None and all(before[k]==0 for k in ('jobs','outbox','permits')),'pending zero artifacts')
|
||||
checkpoint=dict(common,plan=plan,operation_id=operation['operation_id'],preapproval=before,stage='pending_approval')
|
||||
path=folder/'checkpoint.json'
|
||||
def save():path.write_text(json.dumps(checkpoint,indent=2));path.chmod(0o600)
|
||||
save();approve=folder/'approve.json'
|
||||
m.write_private(approve,dict(checkpoint,phase='approve'))
|
||||
print('Diagnostic: pending 0/0/0 verified; separate actual UI approval',flush=True);browser(approve)
|
||||
finish_diagnosis()
|
||||
|
||||
|
||||
def finish_diagnosis():
|
||||
"""Read/audit the existing fixed job only; never prepare or approve again."""
|
||||
m=cloud.contract;settings=identity();folder=STATE/'diagnosis';path=folder/'checkpoint.json'
|
||||
checkpoint=m.protected_json(path);plan=checkpoint['plan'];job=plan['jobs'][0]
|
||||
require(all(checkpoint[k]==settings[k] for k in settings),'diagnostic target changed')
|
||||
operation=dict(operation_id=checkpoint['operation_id']);common=checkpoint;backend=cloud.Backend()
|
||||
def save():path.write_text(json.dumps(checkpoint,indent=2));path.chmod(0o600)
|
||||
end=time.monotonic()+200
|
||||
while True:
|
||||
result=backend.call('jobs.get',dict(tenant_id=settings['tenant_id'],job_id=job['job_id']))
|
||||
if result['state'] in m.TERMINAL or result['state']=='unknown':break
|
||||
require(time.monotonic()<end,'diagnostic outcome unconfirmed; no repeat');time.sleep(3)
|
||||
checkpoint.update(stage=result['state'],result=result);save()
|
||||
require(result['state']=='succeeded' and result['result']['evidence']['postconditions_passed'],'diagnostic job failed; preserve result')
|
||||
data=json.loads(bytes(result['result']['stdout']))
|
||||
require(data['diagnostic_only'] is True and data['transaction_executed'] is False and data['preserved_state'] is True,'diagnostic preservation proof')
|
||||
audit=evidence('audit',operation_id=operation['operation_id']);after=evidence('gate',job_id=job['job_id'])
|
||||
require(audit==dict(origin='dev_test_browser_simulation',simulation=True,principal=common['expected_principal'],plan_hash=plan['plan_hash']),'simulation audit')
|
||||
require([after[k] for k in ('jobs','outbox','permits')]==[1,1,1],'one diagnostic attempt')
|
||||
checkpoint.update(verified=True,data=data,audit=audit,postapproval=after);save();print(json.dumps(data))
|
||||
|
||||
|
||||
def close():
|
||||
require(evidence('gate')['unresolved']==0,'unresolved central job')
|
||||
previous=cloud.contract.protected_json(STATE/'diagnostic-policy.json')
|
||||
result=cloud.bootstrap.rpc('server.conf',cloud_root_policy.close_policy,dict(expected_sha256=previous['policy_sha256'],backup=previous['backup']))
|
||||
cloud.contract.write_private(STATE/'diagnostic-policy-closed.json',result);print('Diagnostic scope closed')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('activate','diagnose','finish','close'));args=parser.parse_args()
|
||||
try:
|
||||
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True)
|
||||
dict(activate=activate,diagnose=diagnose,finish=finish_diagnosis,close=close)[args.action]()
|
||||
except Exception as error:
|
||||
print('APT_FOLLOWUP_INCOMPLETE:'+type(error).__name__);raise SystemExit(2) from None
|
||||
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Cloud-only MCP → pending → actual dev/test WebUI → VM result acceptance.
|
||||
|
||||
No local executor/server, enrollment, direct approval API/SQL or retry exists.
|
||||
Credentials stay on the cloud server; the browser reads its protected login
|
||||
file. All SQL is fixed read-only evidence, scoped to the one cloud VM.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import socket
|
||||
import ssl
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import remote_client_bootstrap as bootstrap
|
||||
import vm_mvp_acceptance as contract
|
||||
|
||||
ROOT = bootstrap.ROOT
|
||||
TENANT = bootstrap.TENANT
|
||||
require = bootstrap.require
|
||||
|
||||
|
||||
def prepare_browser():
|
||||
cache=ROOT/'web/node_modules/.cache/cloud-acceptance'
|
||||
nss=cache/'home/.pki/nssdb';nss.mkdir(mode=0o700,parents=True,exist_ok=True)
|
||||
for directory in (cache,cache/'home',cache/'home/.pki',nss):directory.chmod(0o700)
|
||||
ca=ROOT/'credentials/server-ca.pem';ssl.create_default_context(cafile=str(ca))
|
||||
tool=shutil.which('certutil') or str(ROOT/'web/node_modules/.cache/nss-tools/root/usr/bin/certutil')
|
||||
commands=[]
|
||||
if not (nss/'cert9.db').exists():commands.append([tool,'-N','-d','sql:'+str(nss),'--empty-password'])
|
||||
commands.extend(([tool,'-A','-d','sql:'+str(nss),'-n','RMM cloud acceptance CA','-t','C,,','-i',str(ca)],
|
||||
[tool,'-V','-d','sql:'+str(nss),'-n','RMM cloud acceptance CA','-u','L']))
|
||||
for cmd in commands:
|
||||
contract.require(subprocess.run(cmd,capture_output=True,timeout=15).returncode==0,'isolated_browser_trust_unavailable')
|
||||
|
||||
|
||||
def vm_transport(payload):
|
||||
import sqlite3
|
||||
mode=payload['mode'];require(mode in ('wss','https'),'transport_allowlist')
|
||||
state=Path('/var/lib/rmm-agent-remote')
|
||||
identity=json.loads((state/'identity.json').read_text())
|
||||
require(identity['agent_id']==payload['agent_id'],'fixed_cloud_vm_identity')
|
||||
binary=Path('/usr/local/libexec/rmm-agent-remote')
|
||||
require(hashlib.sha256(binary.read_bytes()).hexdigest()==payload['binary_sha256'],'fixed_artifact')
|
||||
with sqlite3.connect('file:'+str(state/'journal.sqlite')+'?mode=ro',uri=True) as db:
|
||||
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'unresolved_native_job')
|
||||
require('RMM_TRANSPORT=' not in Path('/etc/rmm-remote/agent.env').read_text(),'environment_override_conflict')
|
||||
directory=Path('/etc/systemd/system/rmm-agent-remote.service.d')
|
||||
require(not directory.is_symlink(),'unsafe_dropin_directory')
|
||||
directory.mkdir(mode=0o755,exist_ok=True);directory.chmod(0o755)
|
||||
require(directory.stat().st_uid==0 and directory.stat().st_mode&0o022==0,'unsafe_dropin_owner')
|
||||
path=directory/'95-cloud-acceptance-transport.conf'
|
||||
allowed=['[Service]\nEnvironment=RMM_TRANSPORT='+m+'\n' for m in ('wss','https')]
|
||||
require(not path.is_symlink() and (not path.exists() or path.stat().st_uid==0 and path.read_text() in allowed),'unknown_transport_dropin')
|
||||
if mode=='wss':
|
||||
if path.exists():path.unlink()
|
||||
else:
|
||||
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_TRUNC|os.O_NOFOLLOW,0o644)
|
||||
with os.fdopen(fd,'w') as stream:stream.write(allowed[1]);stream.flush();os.fsync(stream.fileno())
|
||||
path.chmod(0o644)
|
||||
subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True)
|
||||
subprocess.run(['systemctl','restart','rmm-agent-remote.service'],capture_output=True,check=True)
|
||||
return dict(requested=mode,only_cloud_vm=True)
|
||||
|
||||
|
||||
def server(payload):
|
||||
root = Path('/opt/rust-discovery')
|
||||
if payload['kind'] == 'http':
|
||||
require(payload['path'] in ('/mcp', '/auth/config'), 'no administrative mutation endpoint')
|
||||
class HTTPS(http.client.HTTPSConnection):
|
||||
def connect(self):
|
||||
self.sock = self._context.wrap_socket(socket.create_connection(('127.0.0.1',18443),timeout=10), server_hostname=self.host)
|
||||
client = HTTPS('localhost',18443,context=ssl.create_default_context(cafile=str(root/'devstate/pki/ca.pem')))
|
||||
headers = {'Content-Type':'application/json', **payload.get('headers',{})}
|
||||
require(not any(k.lower() in ('authorization','cookie','x-csrf-token') for k in headers), 'caller credential injection')
|
||||
if payload['path'] == '/mcp':
|
||||
principal=payload.get('principal','a'); require(principal in ('a','b'),'fixed_test_principal')
|
||||
headers['Authorization'] = 'Bearer '+(root/('devstate/operator-'+principal+'.token')).read_text().strip()
|
||||
client.request(payload['method'],payload['path'],body=json.dumps(payload['body']) if payload.get('body') is not None else None,headers=headers)
|
||||
response = client.getresponse(); status = response.status; returned = dict(response.getheaders())
|
||||
if 'text/event-stream' in response.getheader('Content-Type',''):
|
||||
data = None
|
||||
for _ in range(128):
|
||||
line = response.readline(1024*1024)
|
||||
if not line: break
|
||||
if line.startswith(b'data: ') and line[6:].strip():
|
||||
item = json.loads(line[6:])
|
||||
if 'result' in item or 'error' in item: data=item; break
|
||||
else:
|
||||
raw=response.read(1024*1024); data=json.loads(raw) if raw else None
|
||||
client.close()
|
||||
return dict(status=status,headers=returned,data=data)
|
||||
tenant = str(uuid.UUID(payload['tenant_id'])); agent = str(uuid.UUID(payload['agent_id']))
|
||||
require(tenant == '11111111-1111-4111-8111-111111111111', 'fixed test tenant required')
|
||||
scope = "tenant_id='"+tenant+"' AND agent_id='"+agent+"'"
|
||||
if payload['kind'] == 'gate':
|
||||
job = str(uuid.UUID(payload['job_id'])) if payload.get('job_id') else None
|
||||
selected = scope + (" AND job_id='"+job+"'" if job else '')
|
||||
query = ("SELECT json_build_object('jobs',(SELECT count(*) FROM jobs WHERE "+selected+"),"
|
||||
"'outbox',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+
|
||||
selected.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=').replace('job_id=', 'j.job_id=')+"),"
|
||||
"'permits',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+
|
||||
selected.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=').replace('job_id=', 'j.job_id=')+"),"
|
||||
"'unresolved',(SELECT count(*) FROM jobs WHERE "+scope+" AND state NOT IN ('succeeded','failed','cancelled','expired')));")
|
||||
else:
|
||||
require(payload['kind'] == 'audit', 'unknown evidence query')
|
||||
operation = str(uuid.UUID(payload['operation_id']))
|
||||
query = ("SELECT json_build_object('origin',approval_origin,'simulation',testsimulation,'principal',principal,'plan_hash',plan_hash) "
|
||||
"FROM authorizations WHERE tenant_id='"+tenant+"' AND operation_id='"+operation+"' "
|
||||
"AND EXISTS(SELECT 1 FROM jobs WHERE "+scope+" AND operation_id='"+operation+"');")
|
||||
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),
|
||||
'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
|
||||
result=subprocess.run(cmd,input=query.encode(),capture_output=True,check=True).stdout.strip()
|
||||
return json.loads(result) if result else {}
|
||||
|
||||
|
||||
class Backend:
|
||||
def __init__(self,principal='a'):
|
||||
contract.require(principal in ('a','b'),'fixed_test_principal')
|
||||
self.principal=principal
|
||||
self.sequence=1
|
||||
self.headers={'Accept':'application/json, text/event-stream','MCP-Protocol-Version':'2025-11-25'}
|
||||
response=self.request(dict(jsonrpc='2.0',id=1,method='initialize',params=dict(protocolVersion='2025-11-25',capabilities={},clientInfo=dict(name='cloud-vm-mvp',version='1'))))
|
||||
contract.require(response['status']==200 and response['data']['result']['protocolVersion']=='2025-11-25','mcp_initialize')
|
||||
session=next(v for k,v in response['headers'].items() if k.lower()=='mcp-session-id')
|
||||
self.headers['Mcp-Session-Id']=session
|
||||
contract.require(self.request(dict(jsonrpc='2.0',method='notifications/initialized'))['status']==202,'mcp_initialized')
|
||||
|
||||
def request(self,body):
|
||||
return bootstrap.rpc('server.conf',server,dict(kind='http',path='/mcp',method='POST',headers=self.headers,body=body,principal=self.principal))
|
||||
|
||||
def call(self,name,args):
|
||||
contract.require(name in ('tenants.list','inventory.get_device','operations.plan','operations.submit','jobs.get','jobs.cancel'),'tool_allowlist')
|
||||
self.sequence+=1
|
||||
response=self.request(dict(jsonrpc='2.0',id=self.sequence,method='tools/call',params=dict(name=name,arguments=args)))
|
||||
data=response.get('data',{}) or {}
|
||||
contract.require(response['status']==200 and 'result' in data and not data['result'].get('isError'),'mcp_tool_failed')
|
||||
return data['result']['structuredContent']
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--execute-dev-test-ui',action='store_true',required=True)
|
||||
parser.add_argument('--case',choices=('hello','refresh_inventory','cancel_queued'),default='hello')
|
||||
parser.add_argument('--transport',choices=('wss','https'))
|
||||
args=parser.parse_args();os.umask(0o077)
|
||||
contract.require(not (Path.home()/'.local/share/rust-discovery/remote-agent').exists(),'notebook_must_not_have_client')
|
||||
prepare_browser()
|
||||
installed=contract.protected_json(ROOT/'devstate/remote-vm.json')
|
||||
identity=installed['identity']
|
||||
settings=dict(tenant_id=TENANT,**identity)
|
||||
def evidence(kind,**extra):
|
||||
return bootstrap.rpc('server.conf',server,dict(kind=kind,tenant_id=TENANT,agent_id=identity['agent_id'],**extra))
|
||||
contract.require(evidence('gate')['unresolved']==0,'unresolved_job_no_new_attempt')
|
||||
if args.transport:
|
||||
bootstrap.rpc('linux-vm.conf',vm_transport,dict(mode=args.transport,agent_id=identity['agent_id'],binary_sha256=installed['binary_sha256']))
|
||||
end=time.monotonic()+90
|
||||
while True:
|
||||
observed=bootstrap.rpc('server.conf',bootstrap.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
if observed.get('transport')==args.transport and observed.get('sync_ready') and not observed.get('restore_blocked'):break
|
||||
contract.require(time.monotonic()<end,'native_transport_sync_not_ready');time.sleep(2)
|
||||
backend=Backend()
|
||||
device=backend.call('inventory.get_device',dict(tenant_id=TENANT,asset_id=identity['asset_id']))
|
||||
contract.require(device['agent_id']==identity['agent_id'] and device['presence']=='online'
|
||||
and 0 <= (contract.now()-contract.timestamp(device['last_contact_at'])).total_seconds()<90,'fresh_cloud_vm_required')
|
||||
observed=bootstrap.rpc('server.conf',bootstrap.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
contract.require(observed['sync_ready'] and not observed['restore_blocked'],'signed_sync_gate_required')
|
||||
needed={'refresh_inventory' if args.case=='refresh_inventory' else 'run_script','rmm.sync.v1'}
|
||||
contract.require(needed<=set(device['capabilities']),'actual_vm_capability_required')
|
||||
request=contract.case_input(args.case,settings)
|
||||
plan=backend.call('operations.plan',dict(tenant_id=TENANT,input=request))
|
||||
contract.require(hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest()==plan['plan_hash'] and len(plan['jobs'])==1,'immutable_plan')
|
||||
job=plan['jobs'][0]
|
||||
folder=ROOT/'devstate/cloud-acceptance'/uuid.uuid4().hex;folder.mkdir(mode=0o700,parents=True)
|
||||
checkpoint=dict(profile='dev/test',case=args.case,expected_principal='simulated-testprincipal-a',**settings,
|
||||
plan=plan,job=job,stage='planned',no_retry=True,transport=observed['transport'],server_connection=observed)
|
||||
path=folder/'checkpoint.json'
|
||||
def save():
|
||||
path.write_text(json.dumps(checkpoint,indent=2));path.chmod(0o600)
|
||||
save()
|
||||
pending=backend.call('operations.submit',dict(tenant_id=TENANT,plan_id=plan['plan_id'],idempotency_key='cloud-'+folder.name))
|
||||
gate=evidence('gate',job_id=job['job_id'])
|
||||
contract.require(pending['approval_state']=='pending_approval' and pending['job_count']==0 and pending['execution_state'] is None
|
||||
and all(gate[k]==0 for k in ('jobs','outbox','permits')),'pending_has_zero_execution_artifacts')
|
||||
checkpoint.update(operation_id=pending['operation_id'],preapproval=gate,stage='pending_approval');save()
|
||||
process=subprocess.run(['node',str(ROOT/'deploy/browser/cloud_approve.cjs'),str(path)],capture_output=True,timeout=120)
|
||||
contract.require(process.returncode==0,'browser_incomplete_preserve_checkpoint_never_retry')
|
||||
if args.case=='cancel_queued':
|
||||
backend.call('jobs.cancel',dict(tenant_id=TENANT,job_id=job['job_id'],reason='Explicit cloud dev/test cancellation before fixed future start'))
|
||||
deadline=time.monotonic()+105
|
||||
while True:
|
||||
result=backend.call('jobs.get',dict(tenant_id=TENANT,job_id=job['job_id']))
|
||||
if result['state'] in contract.TERMINAL or result['state'] in ('unknown','awaiting_reboot'):break
|
||||
contract.require(time.monotonic()<deadline,'result_timeout_no_retry');time.sleep(2)
|
||||
checkpoint.update(result=result,stage=result['state']);save()
|
||||
if args.case=='cancel_queued':
|
||||
contract.require(result['state']=='cancelled','confirmed_cancellation_required')
|
||||
else:
|
||||
contract.require(result['state']=='succeeded' and result['result']['evidence']['postconditions_passed'],'native_postconditions_required')
|
||||
if args.case=='hello':
|
||||
contract.require(result['result']['exit_code']==0 and bytes(result['result']['stdout'])==b'Hello World\n','exact_stdout_required')
|
||||
elif args.case=='refresh_inventory':
|
||||
contract.require(contract.timestamp(result['result']['evidence']['observed_at'])>=contract.timestamp(request['schedule']['not_before']),'fresh_inventory_required')
|
||||
gate=evidence('gate',job_id=job['job_id']);audit=evidence('audit',operation_id=pending['operation_id'])
|
||||
expected_counts=[1,1,0] if args.case=='cancel_queued' else [1,1,1]
|
||||
contract.require([gate[k] for k in ('jobs','outbox','permits')]==expected_counts,'exact_job_outbox_grant_counts')
|
||||
contract.require(audit==dict(origin='dev_test_browser_simulation',simulation=True,principal=checkpoint['expected_principal'],plan_hash=plan['plan_hash']),'interactive_simulation_audit')
|
||||
checkpoint.update(verified=True,postapproval=gate,audit=audit);save()
|
||||
print('PASS cloud VM '+args.case+': MCP pending 0/0/0, actual simulated UI approval, verified counts '+str(expected_counts))
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print('CLOUD_ACCEPTANCE_INCOMPLETE:'+ (str(error) if isinstance(error,contract.GuardError) else type(error).__name__))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One cloud-only Root/APT UI acceptance, never a retry or direct approval.
|
||||
|
||||
Preparation and approval are separate real browser sessions. MCP/SQL only read
|
||||
the root request and prove zero execution artifacts before the UI approval.
|
||||
Existing case directories are never reused; unknown effects stop progress.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
import cloud_mvp_acceptance as cloud
|
||||
import cloud_root_evidence
|
||||
import vm_mvp_adapters as adapters
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parent/'browser'))
|
||||
import run_cloud_root as browser
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--case',choices=browser.CASES,required=True)
|
||||
parser.add_argument('--resume-prepared',action='store_true',help='Only an existing still-pending UI request with zero executable artifacts')
|
||||
args=parser.parse_args()
|
||||
os.umask(0o077);m=cloud.contract;name=args.case
|
||||
m.require(not (Path.home()/'.local/share/rust-discovery/remote-agent').exists(),'notebook must remain development only')
|
||||
installed=m.protected_json(cloud.ROOT/'devstate/remote-vm.json');identity=installed['identity']
|
||||
settings=dict(tenant_id=cloud.TENANT,**identity);backend=cloud.Backend()
|
||||
def evidence(kind,**extra):return cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind=kind,tenant_id=cloud.TENANT,agent_id=identity['agent_id'],**extra))
|
||||
m.require(evidence('gate')['unresolved']==0,'unresolved central job; no new attempt')
|
||||
observed=cloud.bootstrap.rpc('server.conf',cloud.bootstrap.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
m.require(observed['sync_ready'] and not observed['restore_blocked'],'signed sync required')
|
||||
device=backend.call('inventory.get_device',dict(tenant_id=cloud.TENANT,asset_id=identity['asset_id']))
|
||||
m.require(device['presence']=='online' and device['agent_id']==identity['agent_id'],'online cloud VM required')
|
||||
peers=cloud.bootstrap.rpc('linux-vm.conf',cloud_root_evidence.inspect_helpers,{k:settings[k] for k in ('tenant_id','asset_id','agent_id')})
|
||||
m.require(peers['ledger']['integrity']=='ok' and peers['ledger']['faults']==0,'healthy shared helper ledger required')
|
||||
if name in adapters.APT_CASES:
|
||||
apt_peer=peers['apt'];recipe=adapters.RECIPES[name]
|
||||
m.require(apt_peer['fixture_acceptance_ready'] and recipe in apt_peer['bootstrap_recipes'] if name.startswith('apt_native_')
|
||||
else recipe in apt_peer['accepted_recipes'],'actual native bootstrap/acceptance gate required')
|
||||
planning=adapters.planning_from_device(device) if name in adapters.APT_CASES else None
|
||||
request=m.case_input(name,settings,planning=planning)
|
||||
m.require({'rmm.sync.v1','helper.root.v1','root:'+request['action'],request['action']}<=set(device['capabilities']),'native root capability required')
|
||||
folder=cloud.ROOT/'devstate/cloud-root/cases'/name
|
||||
if not args.resume_prepared:folder.mkdir(mode=0o700,parents=True,exist_ok=False)
|
||||
prepared=folder/'prepared.json';preparation=folder/'prepare.json'
|
||||
if args.resume_prepared:
|
||||
previous=m.protected_json(preparation)
|
||||
m.require(previous['case']==name and all(previous[k]==settings[k] for k in settings),'existing preparation target mismatch')
|
||||
request=previous['request']
|
||||
else:
|
||||
m.write_private(preparation,dict(profile='dev/test',case=name,phase='prepare',request=request,**settings,
|
||||
expected_principal='simulated-testprincipal-a',output_file=str(prepared)))
|
||||
print(name+': prepare through actual UI',flush=True);browser.run(preparation,prepare=True)
|
||||
response=m.protected_json(prepared);plan=response['plan'];pending=response['operation']
|
||||
m.require(hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest()==plan['plan_hash'] and len(plan['jobs'])==1,'immutable plan bytes')
|
||||
job=plan['jobs'][0];payload=job['payload']
|
||||
m.require(payload['execution_identity']=='root' and all(payload[k]==settings[k] for k in ('tenant_id','asset_id','agent_id')),'fixed cloud root target')
|
||||
stored=backend.call('jobs.get',dict(tenant_id=cloud.TENANT,operation_id=pending['operation_id']))
|
||||
gate=evidence('gate',job_id=job['job_id'])
|
||||
m.require(stored['plan_hash']==plan['plan_hash'] and stored['approval_state']=='pending_approval' and stored['job_count']==0
|
||||
and stored['execution_state'] is None and all(gate[k]==0 for k in ('jobs','outbox','permits')),'pending must have zero executable artifacts')
|
||||
checkpoint=dict(case=name,plan=plan,job=job,request=request,operation_id=pending['operation_id'],preapproval=gate,helper_readiness=peers,stage='pending_approval')
|
||||
path=folder/'checkpoint.json'
|
||||
def save():
|
||||
# Checkpoint belongs to this exclusive invocation; retain result records
|
||||
# separately and never submit/approve again after an interruption.
|
||||
path.write_text(json.dumps(checkpoint,indent=2));path.chmod(0o600)
|
||||
save()
|
||||
approval=folder/'approve.json'
|
||||
m.write_private(approval,dict(profile='dev/test',case=name,**settings,expected_principal='simulated-testprincipal-a',
|
||||
operation_id=pending['operation_id'],plan_hash=plan['plan_hash'],plan_payload_bytes=plan['plan_payload_bytes'],
|
||||
job_payload=payload,job_payload_hash=job['job_payload_hash']))
|
||||
print(name+': pending verified 0/0/0; separate actual UI approval',flush=True);browser.run(approval)
|
||||
end=time.monotonic()+420
|
||||
while True:
|
||||
result=backend.call('jobs.get',dict(tenant_id=cloud.TENANT,job_id=job['job_id']))
|
||||
if result['state'] in m.TERMINAL or result['state'] in ('unknown','awaiting_reboot'):break
|
||||
m.require(time.monotonic()<end,'result timeout; unknown effect, no retry');time.sleep(3)
|
||||
checkpoint.update(stage=result['state'],result=result);save()
|
||||
m.require(result['state']=='succeeded' and result['result']['evidence']['postconditions_passed'],'native success/postcondition required; no retry')
|
||||
if name=='service_setup':
|
||||
m.require(result['result']['exit_code']==0 and bytes(result['result']['stdout'])==b'RMM test unit prepared\n','exact root script result')
|
||||
if name in adapters.APT_CASES:
|
||||
apt=result['result']['evidence']['apt'];manifest=request['parameters']['apt_manifest']
|
||||
package=manifest['resolved_apt_transaction']['packages'][0]
|
||||
m.require(apt['packages']==[dict(name=package['name'],architecture=package['architecture'],version=package['new_version'])]
|
||||
and apt['fixture_payload_sha256']==hashlib.sha256((package['new_version']+'\n').encode()).hexdigest(),'package and payload proof')
|
||||
if name.startswith('apt_native_'):
|
||||
m.require(apt['test_acceptance'] is True and apt['noProductionEvidence'] is True
|
||||
and apt['test_result_id']==manifest['test_result_id'] and len(apt['native_record_sha256'])==64,'real native acceptance proof')
|
||||
elif planning['fixture_package']==dict(version=package['new_version'],status='install ok installed'):
|
||||
m.require(apt['changed'] is False and apt['already_desired'] is True and apt['postcondition_already_met'] is True
|
||||
and apt['transaction_executed'] is False,'desired-state noop proof')
|
||||
counts=evidence('gate',job_id=job['job_id']);audit=evidence('audit',operation_id=pending['operation_id'])
|
||||
m.require([counts[k] for k in ('jobs','outbox','permits')]==[1,1,1],'one job/outbox/grant only')
|
||||
m.require(audit==dict(origin='dev_test_browser_simulation',simulation=True,principal='simulated-testprincipal-a',plan_hash=plan['plan_hash']),'simulation audit binding')
|
||||
checkpoint.update(verified=True,postapproval=counts,audit=audit);save()
|
||||
print('PASS '+name+': real cloud VM result, actual simulated UI, 0/0/0 → 1/1/1')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print('CLOUD_ROOT_ACCEPTANCE_INCOMPLETE:'+ (str(error) if isinstance(error,cloud.contract.GuardError) else type(error).__name__))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,210 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Versioned cloud-VM helper upgrade and native readiness, never job approval.
|
||||
|
||||
Preserves existing device identities, journals, ledger inodes, archives and
|
||||
acceptance history. Refuses consumed/bound old helper state, active root helpers,
|
||||
unknown artifacts and partial previous installations. No package mutation,
|
||||
business test service setup, reboot, enrollment or database approval exists.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
|
||||
import remote_client_bootstrap as transport
|
||||
import cloud_mvp_acceptance as acceptance
|
||||
|
||||
ROOT=transport.ROOT
|
||||
|
||||
|
||||
def install(payload):
|
||||
import sqlite3
|
||||
import fcntl
|
||||
import time
|
||||
os.umask(0o077)
|
||||
require(os.geteuid()==0,'root bootstrap required')
|
||||
require(str(uuid.UUID(payload['id']))==payload['id'],'fixed bootstrap id')
|
||||
account=pwd.getpwnam('rmm-agent');require((account.pw_uid,account.pw_gid)==(993,984),'agent account drift')
|
||||
unit='rmm-agent-remote.service'
|
||||
state=Path('/var/lib/rmm-agent-remote');ledger=Path('/var/lib/rmm-helper/consumption.sqlite')
|
||||
def run(args,**kwargs):
|
||||
return subprocess.run(args,capture_output=True,check=True,timeout=90,**kwargs).stdout
|
||||
def digest(data):return hashlib.sha256(data).hexdigest()
|
||||
def ledger_count(require_unbound=False):
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','helper ledger corrupt')
|
||||
count=db.execute('SELECT count(*) FROM consumed').fetchone()[0]
|
||||
tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
if require_unbound:
|
||||
require('helper_sync_meta' not in tables or db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==0,
|
||||
'existing helper identity binding; no rebind or reset')
|
||||
return count
|
||||
def manifest(path):
|
||||
values={}
|
||||
for item in path.rglob('*'):
|
||||
require(not item.is_symlink(),'state symlink')
|
||||
if item.is_file():values[str(item.relative_to(path))]=digest(item.read_bytes())
|
||||
return values
|
||||
def create(path,data,mode,uid=0,gid=0):
|
||||
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode)
|
||||
with os.fdopen(fd,'wb') as stream:
|
||||
stream.write(data);stream.flush();os.fsync(stream.fileno());os.fchown(stream.fileno(),uid,gid);os.fchmod(stream.fileno(),mode)
|
||||
for name in ('rmm-agent.service','rmm-helper-dev-test.service','rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
require(run(['systemctl','show',name,'-p','ActiveState','--value']).strip()==b'inactive','old/root service not idle')
|
||||
require(ledger_count(require_unbound=True)==0,'consumed helper ledger; no implicit recovery')
|
||||
require(json.loads((state/'binding.json').read_text())==payload['binding'],'actual cloud binding mismatch')
|
||||
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'nonterminal native jobs')
|
||||
base=Path('/var/lib/rmm-apt-transaction')
|
||||
require(not (base/'native-readiness.json').exists() and not list(base.glob('acceptance-*.json'))
|
||||
and not list(base.glob('bootstrap-attempt-*.json')) and not (base/'active').exists(),'existing probe/effect history requires reconciliation')
|
||||
old={
|
||||
'/usr/local/libexec/rmm-privileged-helper':'f867ac51e82fc7d0f0050cb7629e1ff92d8af9b493d6cb8ae4839accb5694e30',
|
||||
'/etc/systemd/system/rmm-apt-helper.service':'87c2fa242a604fbb9edb5612dd0ef1e703a362d8b465cb97fb3b7ccb5f781f36',
|
||||
'/usr/lib/rmm-apt-transaction/apt_transaction.py':payload['provider_sha256'],
|
||||
'/usr/lib/rmm-apt-transaction/hook':payload['hook_sha256'],
|
||||
}
|
||||
for name,pin in old.items():
|
||||
p=Path(name);require(not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0
|
||||
and digest(p.read_bytes())==pin,'unexpected installed artifact')
|
||||
metadata=base/'install-metadata.json'
|
||||
old_metadata=json.loads(metadata.read_text())
|
||||
require(old_metadata==dict(schema_version=2,provider_sha256=payload['provider_sha256'],hook_sha256=payload['hook_sha256'],
|
||||
unit_sha256=old['/etc/systemd/system/rmm-apt-helper.service']),'unexpected provider metadata')
|
||||
destination={
|
||||
'key':'/etc/rmm-remote/helper-signing-key.json','general_config':'/etc/rmm-remote/helper-general.json',
|
||||
'general_pin':'/etc/rmm-remote/general-helper-proof.json','apt_pin':'/etc/rmm-remote/apt-helper-proof.json',
|
||||
'general_unit':'/etc/systemd/system/rmm-helper-cloud.service',
|
||||
'agent_dropin':'/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf',
|
||||
'log_units':'/etc/rmm-remote/log-units.json',
|
||||
}
|
||||
for value in destination.values():require(not Path(value).exists() and not Path(value).is_symlink(),'existing cloud bootstrap path')
|
||||
test_unit=Path('/etc/systemd/system/rmm-mvp-test.service')
|
||||
require(test_unit.is_file() and not test_unit.is_symlink() and test_unit.stat().st_uid==0
|
||||
and test_unit.stat().st_size==0 and test_unit.stat().st_mode&0o777==0o600 and test_unit.stat().st_nlink==1,
|
||||
'expected precreated empty test unit required; no business setup over SSH')
|
||||
history=Path('/var/lib/rmm-bootstrap-history');require(history.is_dir() and not history.is_symlink()
|
||||
and history.stat().st_uid==0 and history.stat().st_mode&0o777==0o700,'unsafe bootstrap history')
|
||||
backup=history/('cloud-root-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
report=dict(stage='validated',backups={},created=[],package_effects=False,rmm_jobs_executed=False)
|
||||
def save():
|
||||
p=backup/'.report-next.json'
|
||||
create(p,json.dumps(report,indent=2).encode(),0o600);os.replace(p,backup/'report.json')
|
||||
def retain(path):
|
||||
p=Path(path);target=backup/str(p).lstrip('/').replace('/','__')
|
||||
require(p.stat().st_dev==backup.stat().st_dev and not target.exists(),'backup must be exclusive same-filesystem')
|
||||
os.rename(p,target);report['backups'][str(p)]=str(target);save()
|
||||
agent_stopped=False
|
||||
try:
|
||||
save()
|
||||
raw=base64.b64decode(payload['binary'],validate=True)
|
||||
require(digest(raw)==payload['helper_sha256'] and raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','helper artifact mismatch')
|
||||
staged=backup/'rmm-privileged-helper';create(staged,raw,0o755)
|
||||
run(['/lib64/ld-linux-x86-64.so.2','--list',str(staged)])
|
||||
run(['systemctl','stop',unit]);agent_stopped=True
|
||||
report['agent_before']=manifest(state);report['old_agent_before']=manifest(Path('/var/lib/rmm-agent'))
|
||||
root_before=manifest(ledger.parent);ledger_inode=(ledger.stat().st_dev,ledger.stat().st_ino)
|
||||
for path in ('/usr/local/libexec/rmm-privileged-helper','/etc/systemd/system/rmm-apt-helper.service',
|
||||
'/etc/rmm/apt-helper.json',str(metadata)):retain(path)
|
||||
os.rename(staged,'/usr/local/libexec/rmm-privileged-helper')
|
||||
create(Path('/etc/systemd/system/rmm-apt-helper.service'),payload['apt_unit'].encode(),0o644)
|
||||
create(metadata,json.dumps(dict(old_metadata,unit_sha256=digest(payload['apt_unit'].encode()))).encode(),0o600)
|
||||
create(Path(destination['key']),json.dumps(payload['key']).encode(),0o600)
|
||||
create(Path(destination['general_unit']),payload['general_unit'].encode(),0o644)
|
||||
cmd=['/usr/bin/python3','-I','/usr/lib/rmm-apt-transaction/apt_transaction.py']
|
||||
runtime=json.loads(run(cmd+['inspect-runtime']));config_bytes=run(cmd+['print-config'])
|
||||
runtime_sha=digest(json.dumps(runtime,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode())
|
||||
general=dict(profile='general',binding=dict(payload['binding'],public_key_hex=''),signing_key_file=destination['key'],
|
||||
agent_uid=993,socket_gid=984,socket='/run/rmm-helper-cloud/control.sock',state='/var/lib/rmm-helper',
|
||||
allowed_services=['rmm-mvp-test.service'],allow_root_scripts=True,allow_reboot=False,apt_provider=None)
|
||||
apt=dict(general,profile='apt_only',socket='/run/rmm-apt-helper/helper.sock',allowed_services=[],allow_root_scripts=False,
|
||||
apt_provider=dict(schema_version=1,execution_profile='dev-test',allow_fixture_bootstrap=True,
|
||||
allowed_recipes=['rmm-apt-fixture-install-1.0','rmm-apt-fixture-update-1.1'],runtime_sha256=runtime_sha,apt_config_sha256=digest(config_bytes)))
|
||||
create(Path(destination['general_config']),json.dumps(general).encode(),0o600)
|
||||
create(Path('/etc/rmm/apt-helper.json'),json.dumps(apt).encode(),0o600)
|
||||
pins={}
|
||||
for name,config,mode in [('general',destination['general_config'],'--inspect-general-profile'),('apt','/etc/rmm/apt-helper.json','--inspect-apt-profile')]:
|
||||
data=run(['/usr/local/libexec/rmm-privileged-helper',mode],env=dict(os.environ,RMM_HELPER_CONFIG=config))
|
||||
pin=json.loads(data);require(pin['identity']['agent_id']==payload['binding']['agent_id'] and
|
||||
pin['identity']['signing_key_id']==payload['key']['key_id'],'inspected authority differs')
|
||||
create(Path(destination[name+'_pin']),data,0o640,0,984);pins[name]=pin
|
||||
require(manifest(state)==report['agent_before'] and manifest(Path('/var/lib/rmm-agent'))==report['old_agent_before']
|
||||
and manifest(ledger.parent)==root_before and (ledger.stat().st_dev,ledger.stat().st_ino)==ledger_inode,
|
||||
'installation altered preserved identity/journals/ledger')
|
||||
report.update(stage='installed_preserved',pins=pins,runtime=runtime,runtime_sha256=runtime_sha,apt_config_sha256=digest(config_bytes),
|
||||
preserved_before_start=True);save()
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
run(['systemd-analyze','verify','/etc/systemd/system/'+name])
|
||||
run(['systemctl','daemon-reload'])
|
||||
report['stage']='native_apt_probe';save()
|
||||
start=subprocess.run(['systemctl','start','rmm-apt-helper.service'],capture_output=True,timeout=90)
|
||||
report['apt_start_exit']=start.returncode;report['native_readiness']=(base/'native-readiness.json').exists();save()
|
||||
require(start.returncode==0 and report['native_readiness'],'native APT readiness failed; no root jobs')
|
||||
run(['systemctl','start','rmm-helper-cloud.service'])
|
||||
# Type=simple acknowledges spawn before clock/sync initialization. A
|
||||
# returned start command is not daemon readiness or acceptance evidence.
|
||||
for _ in range(10):
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
status=run(['systemctl','show',name,'-p','ActiveState','-p','SubState','-p','NRestarts']).decode()
|
||||
require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status,
|
||||
'helper daemon not stably ready')
|
||||
time.sleep(1)
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==1,'root ledger binding not initialized')
|
||||
dropin=('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n'
|
||||
'Environment=RMM_HELPER_POLICY_FILE='+destination['general_pin']+'\n'
|
||||
'Environment=RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock\n'
|
||||
'Environment=RMM_APT_HELPER_PROOF_FILE='+destination['apt_pin']+'\n'
|
||||
'Environment=RMM_LOG_UNITS_FILE='+destination['log_units']+'\n')
|
||||
create(Path(destination['log_units']),b'["rmm-mvp-test.service"]\n',0o600,993,984)
|
||||
create(Path(destination['agent_dropin']),dropin.encode(),0o644)
|
||||
run(['systemctl','daemon-reload'])
|
||||
report.update(stage='helpers_started_no_jobs',completed=True);save()
|
||||
except Exception as error:
|
||||
report.update(error_class=type(error).__name__,error_stage=report['stage'])
|
||||
# No effects have been authorized here. Stop only own helpers while the
|
||||
# ledger is still empty. Preserve every backup/probe/acceptance file.
|
||||
if ledger_count()==0:
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):
|
||||
subprocess.run(['systemctl','stop',name],capture_output=True,timeout=30)
|
||||
save()
|
||||
finally:
|
||||
if agent_stopped:run(['systemctl','start',unit])
|
||||
return dict(completed=report.get('completed',False),stage=report['stage'],error_class=report.get('error_class'),
|
||||
native_readiness=report.get('native_readiness',False),backup=str(backup),pins=report.get('pins'),
|
||||
runtime_sha256=report.get('runtime_sha256'),apt_config_sha256=report.get('apt_config_sha256'),
|
||||
preserved_before_start=report.get('preserved_before_start',False),root_jobs_executed=False)
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--install-reviewed-helper',action='store_true',required=True);parser.parse_args()
|
||||
os.umask(0o077)
|
||||
installed=acceptance.contract.protected_json(ROOT/'devstate/remote-vm.json');identity=installed['identity']
|
||||
gate=transport.rpc('server.conf',acceptance.server,dict(kind='gate',tenant_id=transport.TENANT,agent_id=identity['agent_id']))
|
||||
require(gate['unresolved']==0,'unresolved central job')
|
||||
metadata=transport.rpc('server.conf',transport.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
material=transport.rpc('server.conf',transport.server,dict(action='material'))
|
||||
binding={k:metadata[k] for k in ('agent_id','asset_id','enrollment_generation','assignment_version')}
|
||||
binding.update(tenant_id=transport.TENANT,public_key_hex=material['key']['public_key_hex'])
|
||||
data=(ROOT/'devstate/cloud-root/rmm-privileged-helper').read_bytes()
|
||||
payload=dict(id=str(uuid.uuid4()),binding=binding,key=material['key'],binary=base64.b64encode(data).decode(),
|
||||
helper_sha256=hashlib.sha256(data).hexdigest(),
|
||||
provider_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes()).hexdigest(),
|
||||
hook_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/hook').read_bytes()).hexdigest(),
|
||||
apt_unit=(ROOT/'deploy/linux/rmm-apt-helper.service').read_text(),
|
||||
general_unit=(ROOT/'deploy/linux/rmm-helper-cloud.service').read_text())
|
||||
result=transport.rpc('linux-vm.conf',install,payload)
|
||||
p=ROOT/'devstate/cloud-root'/('install-'+payload['id']+'.json');p.write_text(json.dumps(result,indent=2));p.chmod(0o600)
|
||||
print(json.dumps({k:v for k,v in result.items() if k not in ('pins','backup')}))
|
||||
require(result['completed'],'bootstrap incomplete; preserve report and diagnose, never repeat installation')
|
||||
|
||||
|
||||
require=transport.require
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('CLOUD_ROOT_BOOTSTRAP_INCOMPLETE; private diagnostic report retained')
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,225 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One guarded upgrade of the unused cloud helper; no jobs or package effects.
|
||||
|
||||
Retains the previous probe, binary, profiles and reports. A successful systemctl
|
||||
start is insufficient: both daemons must remain running and bind the shared
|
||||
ledger before this upgrade reports readiness. Never clears consumed/history.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import cloud_root_bootstrap as initial
|
||||
|
||||
transport=initial.transport
|
||||
ROOT=initial.ROOT
|
||||
require=transport.require
|
||||
|
||||
|
||||
def upgrade(payload):
|
||||
import sqlite3
|
||||
import time
|
||||
os.umask(0o077)
|
||||
require(os.geteuid()==0,'root required')
|
||||
require(str(uuid.UUID(payload['id']))==payload['id'],'upgrade id')
|
||||
previous=Path(payload['previous']['backup'])
|
||||
require(previous.parent==Path('/var/lib/rmm-bootstrap-history') and not previous.is_symlink(),'known previous receipt')
|
||||
receipt=json.loads((previous/'report.json').read_text())
|
||||
require(receipt.get('stage')=='helpers_started_no_jobs' and receipt.get('preserved_before_start') is True,'previous stage mismatch')
|
||||
ledger=Path('/var/lib/rmm-helper/consumption.sqlite');state=Path('/var/lib/rmm-agent-remote')
|
||||
base=Path('/var/lib/rmm-apt-transaction')
|
||||
def run(args,**kwargs):return subprocess.run(args,capture_output=True,check=True,timeout=90,**kwargs).stdout
|
||||
def sha(data):return hashlib.sha256(data).hexdigest()
|
||||
def count():
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','ledger integrity')
|
||||
return db.execute('SELECT count(*) FROM consumed').fetchone()[0]
|
||||
def manifest(path):
|
||||
result={}
|
||||
for item in path.rglob('*'):
|
||||
require(not item.is_symlink(),'preserved state symlink')
|
||||
if item.is_file():result[str(item.relative_to(path))]=sha(item.read_bytes())
|
||||
return result
|
||||
def create(path,data,mode=0o600,gid=0):
|
||||
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode)
|
||||
with os.fdopen(fd,'wb') as f:
|
||||
f.write(data);f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),0,gid);os.fchmod(f.fileno(),mode)
|
||||
require(count()==0,'no upgrade after consumed root attempts')
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
tables={r[0] for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
require('helper_sync_meta' not in tables or db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==0,'already bound ledger')
|
||||
require(not list(base.glob('acceptance-*.json')) and not list(base.glob('bootstrap-attempt-*.json'))
|
||||
and not (base/'active').exists(),'existing effects require reconciliation')
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service','rmm-helper-dev-test.service','rmm-agent.service'):
|
||||
active=run(['systemctl','show',name,'-p','ActiveState','--value']).strip()
|
||||
require(active in (b'inactive',b'failed') and
|
||||
run(['systemctl','show',name,'-p','MainPID','--value']).strip()==b'0','helpers must be stopped')
|
||||
require(json.loads((state/'binding.json').read_text())==payload['binding'],'cloud binding drift')
|
||||
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'unresolved agent job')
|
||||
runtime=receipt['runtime']
|
||||
for path,pin in runtime['binaries'].items():
|
||||
p=Path(path).resolve(strict=True)
|
||||
require(p.stat().st_uid==0 and p.stat().st_mode&0o022==0 and sha(p.read_bytes())==pin['sha256'],'installed runtime drift')
|
||||
apt_unit=Path('/etc/systemd/system/rmm-apt-helper.service')
|
||||
general_unit=Path('/etc/systemd/system/rmm-helper-cloud.service')
|
||||
require(sha(apt_unit.read_bytes())==runtime['unit_profile_sha256'],'APT unit drift')
|
||||
require(general_unit.read_text()==payload['old_general_unit'],'general unit drift')
|
||||
config=Path('/etc/rmm/apt-helper.json');old_config=json.loads(config.read_text())
|
||||
require(old_config['apt_provider']['runtime_sha256']==receipt['runtime_sha256'],'old runtime pin drift')
|
||||
metadata=base/'install-metadata.json';old_meta=json.loads(metadata.read_text())
|
||||
require(old_meta==dict(schema_version=2,provider_sha256=payload['provider_sha256'],hook_sha256=payload['hook_sha256'],
|
||||
unit_sha256=runtime['unit_profile_sha256']),'metadata drift')
|
||||
proof=Path('/etc/rmm-remote/apt-helper-proof.json')
|
||||
require(json.loads(proof.read_text())==payload['previous']['pins']['apt'],'independent proof drift')
|
||||
binary=base64.b64decode(payload['binary'],validate=True)
|
||||
require(sha(binary)==payload['helper_sha256'] and binary[:6]==b'\x7fELF\x02\x01' and binary[18:20]==b'\x3e\x00','binary mismatch')
|
||||
backup=previous.parent/('cloud-clock-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
report=dict(stage='validated',completed=False,package_effects=False,rmm_jobs_executed=False,backups={})
|
||||
def save():
|
||||
p=backup/'.next.json';create(p,json.dumps(report,indent=2).encode());os.replace(p,backup/'report.json')
|
||||
def retain(p):
|
||||
require(not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0,'unsafe retained path')
|
||||
target=backup/str(p).lstrip('/').replace('/','__')
|
||||
require(not target.exists() and p.stat().st_dev==backup.stat().st_dev,'exclusive same filesystem backup')
|
||||
os.rename(p,target);report['backups'][str(p)]=str(target);save()
|
||||
stopped=False
|
||||
try:
|
||||
save();staged=backup/'helper-next';create(staged,binary,0o755)
|
||||
run(['/lib64/ld-linux-x86-64.so.2','--list',str(staged)])
|
||||
run(['systemctl','stop','rmm-agent-remote.service']);stopped=True
|
||||
preserved={str(p):manifest(p) for p in (state,Path('/var/lib/rmm-agent'),ledger.parent)}
|
||||
inode=(ledger.stat().st_dev,ledger.stat().st_ino)
|
||||
for p in (Path('/usr/local/libexec/rmm-privileged-helper'),apt_unit,general_unit,config,metadata,proof,base/'native-readiness.json'):retain(p)
|
||||
os.rename(staged,'/usr/local/libexec/rmm-privileged-helper')
|
||||
create(apt_unit,payload['apt_unit'].encode(),0o644);create(general_unit,payload['general_unit'].encode(),0o644)
|
||||
create(metadata,json.dumps(dict(old_meta,unit_sha256=sha(payload['apt_unit'].encode()))).encode())
|
||||
cmd=['/usr/bin/python3','-I','/usr/lib/rmm-apt-transaction/apt_transaction.py']
|
||||
new_runtime=json.loads(run(cmd+['inspect-runtime']))
|
||||
runtime_sha=sha(json.dumps(new_runtime,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode())
|
||||
old_config['apt_provider']['runtime_sha256']=runtime_sha
|
||||
create(config,json.dumps(old_config).encode())
|
||||
data=run(['/usr/local/libexec/rmm-privileged-helper','--inspect-apt-profile'],env=dict(os.environ,RMM_HELPER_CONFIG=str(config)))
|
||||
pin=json.loads(data);require(pin['identity']==payload['previous']['pins']['apt']['identity'],'authority changed')
|
||||
create(proof,data,0o640,984)
|
||||
require(all(manifest(Path(p))==v for p,v in preserved.items()) and (ledger.stat().st_dev,ledger.stat().st_ino)==inode,'preserved journals changed')
|
||||
report.update(stage='installed_preserved',preserved_before_start=True,pins=dict(payload['previous']['pins'],apt=pin),
|
||||
runtime_sha256=runtime_sha,runtime=new_runtime,apt_config_sha256=receipt['apt_config_sha256']);save()
|
||||
for p in (apt_unit,general_unit):run(['systemd-analyze','verify',str(p)])
|
||||
run(['systemctl','daemon-reload'])
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
if run(['systemctl','show',name,'-p','ActiveState','--value']).strip()==b'failed':
|
||||
run(['systemctl','reset-failed',name])
|
||||
report['stage']='native_probe_and_stable_start';save()
|
||||
run(['systemctl','start','rmm-apt-helper.service','rmm-helper-cloud.service'])
|
||||
for _ in range(10):
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
status=run(['systemctl','show',name,'-p','ActiveState','-p','SubState','-p','NRestarts']).decode()
|
||||
require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status,'daemon not stably ready')
|
||||
time.sleep(1)
|
||||
require((base/'native-readiness.json').is_file() and count()==0,'native readiness or zero effects absent')
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==1,'helper ledger not bound')
|
||||
report.update(stage='stable_helpers_no_jobs',completed=True,native_readiness=True);save()
|
||||
except Exception as error:
|
||||
report.update(error_class=type(error).__name__,completed=False)
|
||||
if count()==0:
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):subprocess.run(['systemctl','stop',name],capture_output=True,timeout=30)
|
||||
save()
|
||||
finally:
|
||||
if stopped:run(['systemctl','start','rmm-agent-remote.service'])
|
||||
return dict(report,backup=str(backup))
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
previous=initial.acceptance.contract.protected_json(ROOT/'devstate/cloud-root/install-121352fd-8078-48cd-9185-439e29420c56.json')
|
||||
identity=previous['pins']['general']['identity']
|
||||
gate=transport.rpc('server.conf',initial.acceptance.server,dict(kind='gate',tenant_id=identity['tenant_id'],agent_id=identity['agent_id']))
|
||||
require(gate['unresolved']==0,'central unresolved job')
|
||||
binding=transport.rpc('server.conf',transport.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
material=transport.rpc('server.conf',transport.server,dict(action='material'))
|
||||
binding={k:binding[k] for k in ('agent_id','asset_id','enrollment_generation','assignment_version')}
|
||||
binding.update(tenant_id=identity['tenant_id'],public_key_hex=material['key']['public_key_hex'])
|
||||
general=(ROOT/'deploy/linux/rmm-helper-cloud.service').read_text()
|
||||
old=general.replace('# Read-only trusted clock query; glibc may use clock_adjtime. No CAP_SYS_TIME.\nSystemCallFilter=adjtimex clock_adjtime\n',
|
||||
'SystemCallFilter=adjtimex\n')
|
||||
binary=(ROOT/'devstate/cloud-root/rmm-privileged-helper-clock-fixed').read_bytes()
|
||||
payload=dict(id=str(uuid.uuid4()),previous=previous,binding=binding,old_general_unit=old,general_unit=general,
|
||||
apt_unit=(ROOT/'deploy/linux/rmm-apt-helper.service').read_text(),binary=base64.b64encode(binary).decode(),
|
||||
helper_sha256=hashlib.sha256(binary).hexdigest(),
|
||||
provider_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes()).hexdigest(),
|
||||
hook_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/hook').read_bytes()).hexdigest())
|
||||
result=transport.rpc('linux-vm.conf',upgrade,payload)
|
||||
initial.acceptance.contract.write_private(ROOT/'devstate/cloud-root'/('clock-'+payload['id']+'.json'),result)
|
||||
print(json.dumps({k:result.get(k) for k in ('completed','stage','error_class','native_readiness','preserved_before_start','runtime_sha256')}))
|
||||
require(result['completed'],'upgrade incomplete; inspect retained report, never reset')
|
||||
|
||||
|
||||
def resume_start(payload):
|
||||
"""Resume only the installed, unused profile; never reinstall or erase proof."""
|
||||
import sqlite3
|
||||
import time
|
||||
import traceback
|
||||
backup=Path(payload['backup'])
|
||||
require(backup.parent==Path('/var/lib/rmm-bootstrap-history') and backup.name.startswith('cloud-clock-'),'fixed retained upgrade')
|
||||
report=json.loads((backup/'report.json').read_text())
|
||||
require((report['stage']=='installed_preserved' or report['stage']=='stable_start_resume' and report.get('step')=='start_helpers')
|
||||
and report.get('completed') is False and report.get('preserved_before_start') is True,'not a resumable start')
|
||||
def run(args):return subprocess.run(args,capture_output=True,check=True,timeout=90).stdout
|
||||
def count():
|
||||
with sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True) as db:
|
||||
return db.execute('SELECT count(*) FROM consumed').fetchone()[0]
|
||||
def save():
|
||||
p=backup/'.start-next.json'
|
||||
fd=os.open(p,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as f:json.dump(report,f,indent=2);f.flush();os.fsync(f.fileno())
|
||||
os.replace(p,backup/'report.json')
|
||||
require(count()==0,'no start recovery after consumed attempt')
|
||||
base=Path('/var/lib/rmm-apt-transaction')
|
||||
require(not list(base.glob('acceptance-*.json')) and not list(base.glob('bootstrap-attempt-*.json'))
|
||||
and not (base/'active').exists() and not (base/'native-readiness.json').exists(),'existing evidence requires inspection')
|
||||
for kind in ('apt','general'):
|
||||
unit=Path('/etc/systemd/system/rmm-'+('apt-helper' if kind=='apt' else 'helper-cloud')+'.service')
|
||||
require(unit.read_text()==payload[kind+'_unit'],'unit differs from reviewed source')
|
||||
cmd=['/usr/bin/python3','-I','/usr/lib/rmm-apt-transaction/apt_transaction.py']
|
||||
runtime=json.loads(run(cmd+['inspect-runtime']))
|
||||
require(runtime==report['runtime'],'installed runtime drift')
|
||||
pin=json.loads(Path('/etc/rmm-remote/apt-helper-proof.json').read_text())
|
||||
require(pin==report['pins']['apt'],'independent proof drift')
|
||||
try:
|
||||
report['stage']='stable_start_resume';save()
|
||||
run(['systemctl','daemon-reload'])
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):
|
||||
report['step']='verify_'+name;save()
|
||||
run(['systemd-analyze','verify','/etc/systemd/system/'+name])
|
||||
report['step']='start_helpers';save()
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):
|
||||
if run(['systemctl','show',name,'-p','ActiveState','--value']).strip()==b'failed':
|
||||
run(['systemctl','reset-failed',name])
|
||||
run(['systemctl','start','rmm-apt-helper.service','rmm-helper-cloud.service'])
|
||||
for _ in range(10):
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):
|
||||
status=run(['systemctl','show',name,'-p','ActiveState','-p','SubState','-p','NRestarts']).decode()
|
||||
require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status,'daemon failed stable start')
|
||||
time.sleep(1)
|
||||
with sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True) as db:
|
||||
require(db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==1,'helper ledger not bound')
|
||||
require((base/'native-readiness.json').is_file() and count()==0,'missing native readiness or unexpected root effect')
|
||||
previous_error={k:report.pop(k) for k in ('error_class','frames') if k in report}
|
||||
if previous_error:report['previous_start_failure']=previous_error
|
||||
report.update(stage='stable_helpers_no_jobs',completed=True,native_readiness=True);save()
|
||||
run(['systemctl','restart','rmm-agent-remote.service'])
|
||||
except Exception as error:
|
||||
report.update(completed=False,error_class=type(error).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(error.__traceback__)])
|
||||
if count()==0:
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):subprocess.run(['systemctl','stop',name],capture_output=True,timeout=30)
|
||||
save()
|
||||
return dict(report,backup=str(backup))
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('CLOUD_CLOCK_UPGRADE_INCOMPLETE; no automatic repeat');raise SystemExit(2) from None
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Read-only nonce-bound helper readiness as the actual unprivileged VM peer."""
|
||||
|
||||
|
||||
def inspect_helpers(payload):
|
||||
import sqlite3
|
||||
program=r'''import json,os,socket,struct,uuid
|
||||
from pathlib import Path
|
||||
result={}
|
||||
for kind,pin_path,socket_path in (
|
||||
('general','/etc/rmm-remote/general-helper-proof.json','/run/rmm-helper-cloud/control.sock'),
|
||||
('apt','/etc/rmm-remote/apt-helper-proof.json','/run/rmm-apt-helper/helper.sock')):
|
||||
pin_path=Path(pin_path);socket_path=Path(socket_path)
|
||||
for path in list(pin_path.parents)+[pin_path]+list(socket_path.parents):
|
||||
meta=path.lstat();assert not path.is_symlink() and meta.st_uid==0 and meta.st_mode&0o022==0
|
||||
meta=socket_path.lstat();assert meta.st_uid==0 and meta.st_mode&0o007==0
|
||||
pin=json.loads(pin_path.read_text());challenge=str(uuid.uuid4())
|
||||
request=dict(operation='apt_readiness_v1' if kind=='apt' else 'general_policy_v1',challenge=challenge,identity=pin['identity'])
|
||||
field='provider_stack_sha256' if kind=='apt' else 'policy_sha256'
|
||||
if kind=='apt':request['expected_stack_sha256']=pin[field]
|
||||
s=socket.socket(socket.AF_UNIX);s.settimeout(10);s.connect(str(socket_path))
|
||||
assert struct.unpack('3i',s.getsockopt(socket.SOL_SOCKET,socket.SO_PEERCRED,12))[1]==0
|
||||
raw=json.dumps(request,separators=(',',':')).encode();s.sendall(struct.pack('!I',len(raw))+raw)
|
||||
def receive(n):
|
||||
b=b''
|
||||
while len(b)<n:
|
||||
c=s.recv(n-len(b));assert c;b+=c
|
||||
return b
|
||||
n=struct.unpack('!I',receive(4))[0];assert 0<n<=1024*1024
|
||||
response=json.loads(receive(n));s.close()
|
||||
assert response['schema_version']==1 and response['challenge']==challenge and response['identity']==pin['identity'] and response['profile']==pin['profile']
|
||||
assert response[field]==pin[field] and response['ledger_inode'] and response['lock_inode']
|
||||
result[kind]=response
|
||||
assert all(result['general'][k]==result['apt'][k] for k in ('ledger_device','ledger_inode','lock_device','lock_inode'))
|
||||
print(json.dumps(result))
|
||||
'''
|
||||
process=subprocess.run(['/usr/sbin/runuser','-u','rmm-agent','--','/usr/bin/python3','-I','-c',program],capture_output=True,check=True,timeout=25)
|
||||
result=json.loads(process.stdout)
|
||||
for peer in result.values():
|
||||
require(all(peer['identity'][key]==payload[key] for key in ('tenant_id','asset_id','agent_id')),'helper target identity drift')
|
||||
with sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True) as db:
|
||||
result['ledger']=dict(integrity=db.execute('PRAGMA integrity_check').fetchone()[0],
|
||||
consumed=db.execute('SELECT count(*) FROM consumed').fetchone()[0],
|
||||
faults=db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0])
|
||||
return result
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Pause the unused APT endpoint without resetting its failed native attempt."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import cloud_mvp_acceptance as cloud
|
||||
|
||||
|
||||
def pause(payload):
|
||||
import sqlite3
|
||||
os.umask(0o077)
|
||||
require(hashlib.sha256(Path('/usr/local/libexec/rmm-privileged-helper').read_bytes()).hexdigest()==payload['binary_sha256'],'installed helper drift')
|
||||
state=Path('/var/lib/rmm-agent-remote')
|
||||
require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'],'cloud client drift')
|
||||
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'unresolved native attempt')
|
||||
ledger=Path('/var/lib/rmm-helper/consumption.sqlite');inode=(ledger.stat().st_dev,ledger.stat().st_ino)
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('SELECT count(*) FROM consumed').fetchone()[0]==4 and
|
||||
db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0 and
|
||||
db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'shared ledger not final')
|
||||
path=Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf')
|
||||
expected=('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n'
|
||||
'Environment=RMM_HELPER_POLICY_FILE=/etc/rmm-remote/general-helper-proof.json\n'
|
||||
'Environment=RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock\n'
|
||||
'Environment=RMM_APT_HELPER_PROOF_FILE=/etc/rmm-remote/apt-helper-proof.json\n'
|
||||
'Environment=RMM_LOG_UNITS_FILE=/etc/rmm-remote/log-units.json\n')
|
||||
require(not path.is_symlink() and path.stat().st_uid==0 and path.read_text()==expected,'own drop-in drift')
|
||||
marker=Path('/var/lib/rmm-apt-transaction/bootstrap-attempt-rmm-apt-fixture-install-1.0.json')
|
||||
marker_hash=hashlib.sha256(marker.read_bytes()).hexdigest()
|
||||
backup=Path('/var/lib/rmm-bootstrap-history')/('cloud-apt-paused-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
target=backup/'30-cloud-root.conf';target.write_text(expected);target.chmod(0o600)
|
||||
subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,check=True,timeout=30)
|
||||
subprocess.run(['systemctl','disable','rmm-apt-helper.service'],capture_output=True,check=True,timeout=30)
|
||||
updated=''.join(line+'\n' for line in expected.splitlines() if not line.startswith('Environment=RMM_APT_HELPER_'))
|
||||
next_path=path.with_suffix('.apt-paused-next')
|
||||
fd=os.open(next_path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644)
|
||||
with os.fdopen(fd,'w') as f:f.write(updated);f.flush();os.fsync(f.fileno())
|
||||
os.replace(next_path,path)
|
||||
subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True,timeout=30)
|
||||
subprocess.run(['systemctl','restart','rmm-agent-remote.service'],capture_output=True,check=True,timeout=30)
|
||||
require((ledger.stat().st_dev,ledger.stat().st_ino)==inode and hashlib.sha256(marker.read_bytes()).hexdigest()==marker_hash,'ledger or attempt marker changed')
|
||||
report=dict(apt_paused=True,agent_restarted=True,ledger_inode_preserved=True,attempt_marker_preserved=True,backup=str(backup),package_effects=False)
|
||||
p=backup/'report.json';p.write_text(json.dumps(report));p.chmod(0o600)
|
||||
return report
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
require(len(list((cloud.ROOT/'devstate/cloud-root').glob('policy-closed-*.json')))==1,'close root policy first')
|
||||
identity=cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')['identity']
|
||||
gate=cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind='gate',tenant_id=cloud.TENANT,agent_id=identity['agent_id']))
|
||||
require(gate['unresolved']==0,'unresolved central attempt')
|
||||
payload=dict(id=str(uuid.uuid4()),agent_id=identity['agent_id'],binary_sha256=hashlib.sha256((cloud.ROOT/'devstate/cloud-root/rmm-privileged-helper-clock-fixed').read_bytes()).hexdigest())
|
||||
result=cloud.bootstrap.rpc('linux-vm.conf',pause,payload)
|
||||
cloud.contract.write_private(cloud.ROOT/'devstate/cloud-root'/('apt-paused-'+payload['id']+'.json'),result)
|
||||
print({k:v for k,v in result.items() if k!='backup'})
|
||||
|
||||
|
||||
require=cloud.require
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('CLOUD_APT_PAUSE_INCOMPLETE; inspect retained state');raise SystemExit(2) from None
|
||||
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install an expiring deny/limit policy. This never approves a job."""
|
||||
import datetime as dt
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import cloud_mvp_acceptance as cloud
|
||||
|
||||
|
||||
def install_policy(payload):
|
||||
root=Path('/opt/rust-discovery');compose=root/'deploy/server.compose.yaml';env=root/'devstate/.env'
|
||||
require(hashlib.sha256(compose.read_bytes()).hexdigest()==payload['previous_compose_sha256'],'compose drift')
|
||||
require(not compose.is_symlink() and not env.is_symlink(),'unsafe server config')
|
||||
directory=root/'devstate/root-policy';require(not directory.exists(),'existing root policy requires separate reviewed update')
|
||||
backup=root/'devstate'/('root-policy-bootstrap-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
def create(path,data,mode=0o600,uid=0):
|
||||
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode)
|
||||
with os.fdopen(fd,'wb') as f:
|
||||
f.write(data);f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),uid,uid);os.fchmod(f.fileno(),mode)
|
||||
create(backup/'server.compose.yaml',compose.read_bytes());create(backup/'server.env',env.read_bytes())
|
||||
directory.mkdir(mode=0o700);os.chown(directory,10001,10001)
|
||||
create(directory/'policy.json',json.dumps(payload['policy']).encode(),uid=10001)
|
||||
text=env.read_text();require(not any(line.startswith('RMM_DEV_ROOT_') for line in text.splitlines()),'existing root environment')
|
||||
create(env.with_suffix('.root-next'),(text.rstrip()+'\nRMM_DEV_ROOT_ENABLED=true\n').encode())
|
||||
create(compose.with_suffix('.root-next'),payload['compose'].encode(),0o644)
|
||||
os.replace(env.with_suffix('.root-next'),env);os.replace(compose.with_suffix('.root-next'),compose)
|
||||
cmd=['docker','compose','--env-file',str(env),'-f',str(compose)]
|
||||
subprocess.run(cmd+['config','-q'],capture_output=True,check=True,timeout=30)
|
||||
result=subprocess.run(cmd+['up','-d','--no-deps','api','gateway'],capture_output=True,timeout=60)
|
||||
return dict(deployed=result.returncode==0,expires_at=payload['policy']['expires_at'],policy_sha256=hashlib.sha256((directory/'policy.json').read_bytes()).hexdigest(),
|
||||
approvals_created=False,backup=str(backup))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--close-reviewed-test-scope',action='store_true');args=parser.parse_args()
|
||||
if args.close_reviewed_test_scope:
|
||||
records=list((cloud.ROOT/'devstate/cloud-root').glob('policy-*.json'))
|
||||
cloud.require(len(records)==1,'one known policy deployment required')
|
||||
previous=cloud.contract.protected_json(records[0]);identity=previous['policy']['scopes'][0]
|
||||
gate=cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind='gate',tenant_id=identity['tenant_id'],agent_id=identity['agent_id']))
|
||||
cloud.require(gate['unresolved']==0,'unresolved central job; preserve policy and reconcile')
|
||||
result=cloud.bootstrap.rpc('server.conf',close_policy,dict(expected_sha256=previous['policy_sha256'],backup=previous['backup']))
|
||||
cloud.contract.write_private(cloud.ROOT/'devstate/cloud-root'/('policy-closed-'+uuid.uuid4().hex+'.json'),result)
|
||||
print(dict(test_scope_closed=result['closed'],expires_at=result['expires_at']));return
|
||||
os.umask(0o077)
|
||||
reports=[cloud.contract.protected_json(p) for p in (cloud.ROOT/'devstate/cloud-root').glob('health-*.json')]
|
||||
ready=next(r for r in reports if r['completed'] is True and r['readiness_present'] is True)
|
||||
cloud.require(ready['consumed']==0 and ready['ledger_faults']==0,'unused healthy root runtime required')
|
||||
identity=ready['pins']['general']['identity'];settings={k:identity[k] for k in ('tenant_id','agent_id','asset_id')}
|
||||
gate=cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind='gate',**{k:settings[k] for k in ('tenant_id','agent_id')}))
|
||||
cloud.require(gate['unresolved']==0,'central unresolved job')
|
||||
device=cloud.Backend().call('inventory.get_device',{k:settings[k] for k in ('tenant_id','asset_id')})
|
||||
scopes=[]
|
||||
for name,pin in ready['pins'].items():
|
||||
policy_hash=pin['policy_sha256' if name=='general' else 'provider_stack_sha256']
|
||||
cloud.require('helper.policy.sha256:'+policy_hash in device['capabilities'],'measured helper capability missing')
|
||||
scope={k:identity[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')}
|
||||
scope.update(principals=['simulated-testprincipal-a'],helper_policy_sha256=policy_hash,
|
||||
acceptance_evidence_id='cloud-stable-readiness:'+ready['runtime_sha256'],
|
||||
allowed_services=['rmm-mvp-test.service'] if name=='general' else [],
|
||||
allow_root_scripts=name=='general',allow_reboot=False)
|
||||
if name=='apt':
|
||||
scope['apt']=dict(allow_native_acceptance=True,allow_execute=True,runtime_sha256=ready['runtime_sha256'],
|
||||
apt_config_sha256=ready['apt_config_sha256'],
|
||||
provider_sha256=ready['runtime']['binaries']['/usr/lib/rmm-apt-transaction/apt_transaction.py']['sha256'],
|
||||
hook_sha256=ready['runtime']['binaries']['/usr/lib/rmm-apt-transaction/hook']['sha256'])
|
||||
scopes.append(scope)
|
||||
policy=dict(schema_version=1,profile='development',expires_at=(dt.datetime.now(dt.timezone.utc)+dt.timedelta(hours=2)).isoformat(),scopes=scopes)
|
||||
previous=(cloud.ROOT/'devstate/cloud-root/compose-before-root.yaml').read_bytes()
|
||||
payload=dict(id=str(uuid.uuid4()),policy=policy,compose=(cloud.ROOT/'deploy/server.compose.yaml').read_text(),
|
||||
previous_compose_sha256=hashlib.sha256(previous).hexdigest())
|
||||
result=cloud.bootstrap.rpc('server.conf',install_policy,payload)
|
||||
cloud.contract.write_private(cloud.ROOT/'devstate/cloud-root'/('policy-'+payload['id']+'.json'),dict(result,policy=policy))
|
||||
print({k:result[k] for k in ('deployed','expires_at','approvals_created')})
|
||||
cloud.require(result['deployed'],'deployment unconfirmed; inspect retained server config, never repeat bootstrap')
|
||||
|
||||
|
||||
def close_policy(payload):
|
||||
import datetime as dt
|
||||
p=Path('/opt/rust-discovery/devstate/root-policy/policy.json')
|
||||
require(not p.is_symlink() and p.stat().st_uid==10001 and p.stat().st_mode&0o777==0o600,'unsafe policy')
|
||||
raw=p.read_bytes();require(hashlib.sha256(raw).hexdigest()==payload['expected_sha256'],'policy drift')
|
||||
backup=Path(payload['backup'])
|
||||
require(backup.parent==Path('/opt/rust-discovery/devstate') and backup.name.startswith('root-policy-bootstrap-')
|
||||
and not backup.is_symlink() and backup.stat().st_mode&0o777==0o700,'known private backup required')
|
||||
retained=backup/'policy-before-close.json'
|
||||
fd=os.open(retained,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'wb') as f:f.write(raw);f.flush();os.fsync(f.fileno())
|
||||
policy=json.loads(raw);policy['expires_at']=dt.datetime.now(dt.timezone.utc).isoformat()
|
||||
next_path=p.with_suffix('.closed-next')
|
||||
fd=os.open(next_path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as f:
|
||||
json.dump(policy,f);f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),10001,10001)
|
||||
os.replace(next_path,p)
|
||||
return dict(closed=True,expires_at=policy['expires_at'],policy_sha256=hashlib.sha256(p.read_bytes()).hexdigest(),approvals_created=False)
|
||||
|
||||
|
||||
require=cloud.require
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('CLOUD_ROOT_POLICY_INCOMPLETE; no automatic repeat');raise SystemExit(2) from None
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Exact dev/test RMM diagnostic payload: simulation only, no host APT writes."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
def digest(data):
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def preserved():
|
||||
base = Path('/var/lib/rmm-apt-transaction')
|
||||
files = [Path('/var/lib/dpkg/status'), base / 'bootstrap-attempt-rmm-apt-fixture-install-1.0.json']
|
||||
files.extend(p for p in (base / 'active').rglob('*') if p.is_file())
|
||||
return {str(p): digest(p.read_bytes()) for p in files}
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
base = Path('/var/lib/rmm-apt-transaction')
|
||||
archive = base / 'archives/74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867.deb'
|
||||
assert archive.stat().st_size == 20672 and digest(archive.read_bytes()) == archive.stem
|
||||
assert not Path('/usr/share/rmm-mvp-test/version').exists()
|
||||
before = preserved()
|
||||
config = (base / 'active/apt.conf').read_bytes()
|
||||
results = []
|
||||
with tempfile.TemporaryDirectory(prefix='rmm-apt-diagnostic-') as temp:
|
||||
workspace = Path(temp)
|
||||
for name in ('empty', 'lists', 'cache', 'log', 'state'):
|
||||
(workspace / name).mkdir(mode=0o700)
|
||||
# Preserve source/hook/solver configuration. Only diagnostic scratch
|
||||
# directories move into PrivateTmp; host dpkg status is read-only.
|
||||
copied = config.replace(str(base / 'active').encode(), str(workspace).encode())
|
||||
copied = copied.replace(str(base / 'log').encode(), str(workspace / 'log').encode())
|
||||
copied = copied.replace(str(base / 'state').encode(), str(workspace / 'state').encode())
|
||||
path = workspace / 'apt.conf'; path.write_bytes(copied)
|
||||
env = dict(PATH='/usr/sbin:/usr/bin:/sbin:/bin', LC_ALL='C', HOME='/root',
|
||||
DEBIAN_FRONTEND='noninteractive', APT_CONFIG=str(path))
|
||||
for name, delimiter in (('original_argv_simulated', ['--']), ('without_separator_simulated', [])):
|
||||
command = ['/usr/bin/apt-get', '--simulate', '--yes', '--no-remove', '--no-install-recommends',
|
||||
'--no-download', 'install'] + delimiter + [str(archive)]
|
||||
with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err:
|
||||
process = subprocess.run(command, env=env, stdin=subprocess.DEVNULL,
|
||||
stdout=out, stderr=err, timeout=20)
|
||||
lengths = (out.tell(), err.tell()); out.seek(0); err.seek(0)
|
||||
stdout = out.read(16384); stderr = err.read(16384)
|
||||
messages = []
|
||||
for line in stderr.decode('utf-8', errors='replace').splitlines():
|
||||
if line.startswith(('E:', 'W:')):
|
||||
line = re.sub(r'[A-Za-z][A-Za-z0-9+.-]*://\S+', '<uri>', line)
|
||||
line = re.sub(r'/[^\s\x27\x22,;()]+', '<path>', line)
|
||||
line = re.sub(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', '<address>', line)
|
||||
messages.append(line[:512])
|
||||
results.append(dict(case=name,exit_code=process.returncode,messages=messages[:12],
|
||||
stdout_sha256=digest(stdout),stderr_sha256=digest(stderr),truncated=any(n>16384 for n in lengths),
|
||||
solver_selected_fixture=b'rmm-mvp-test' in stdout))
|
||||
assert preserved() == before and not Path('/usr/share/rmm-mvp-test/version').exists()
|
||||
print(json.dumps(dict(schema_version=1,diagnostic_only=True,transaction_executed=False,
|
||||
preserved_state=True,original_config_sha256=digest(config),results=results)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
[Unit]
|
||||
Description=RMM unprivileged client for discovery.ease-technologies.de
|
||||
After=network-online.target time-sync.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=rmm-agent
|
||||
Group=rmm-agent
|
||||
StateDirectory=rmm-agent-remote
|
||||
StateDirectoryMode=0700
|
||||
UMask=0077
|
||||
Environment=RMM_AGENT_STATE=/var/lib/rmm-agent-remote
|
||||
Environment=RMM_CA_FILE=/etc/rmm-remote/ca.pem
|
||||
Environment=RMM_SERVER_NAME=discovery.ease-technologies.de
|
||||
Environment=RMM_CONNECT_ADDR=88.99.80.202:443
|
||||
Environment=RMM_TRANSPORT=auto
|
||||
Environment=RMM_EXTENDED_INVENTORY=1
|
||||
EnvironmentFile=/etc/rmm-remote/agent.env
|
||||
ExecStart=/usr/local/libexec/rmm-agent-remote
|
||||
Restart=on-failure
|
||||
RestartSec=10
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelModules=true
|
||||
ProtectControlGroups=true
|
||||
RestrictSUIDSGID=true
|
||||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
|
||||
CapabilityBoundingSet=
|
||||
AmbientCapabilities=
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -4,7 +4,10 @@ After=local-fs.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
# System services default to root. Avoid explicit User=root: systemd 255's
|
||||
# seccomp setup otherwise drops CAP_SETUID and retains setup inheritable caps.
|
||||
# Controller verification still requires euid 0, the exact capability sets,
|
||||
# zero inherited/ambient caps, NNP and the full sandbox below.
|
||||
Group=rmm-agent
|
||||
Environment=RMM_HELPER_CONFIG=/etc/rmm/apt-helper.json
|
||||
ExecStartPre=/usr/local/libexec/rmm-privileged-helper --probe-native
|
||||
@@ -38,9 +41,10 @@ CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE CAP_FOWNER CAP_FSETID CAP_SETGI
|
||||
# Trusted setup needs mount/unshare/fork/capset/prctl/seccomp. There is deliberately
|
||||
# no outer @mount deny; the irreversible worker filter denies it after setup.
|
||||
SystemCallFilter=~@reboot @module @raw-io @swap @clock @debug
|
||||
# Existing helper clock trust reads adjtimex(modes=0). Mutating adjtimex still
|
||||
# requires CAP_SYS_TIME, absent from the bounding set; other @clock calls denied.
|
||||
SystemCallFilter=adjtimex
|
||||
# glibc implements the read-only adjtimex(modes=0) query via clock_adjtime on
|
||||
# Ubuntu. Neither syscall can change time without CAP_SYS_TIME (not bounded).
|
||||
# Keep all other @clock operations denied; never bypass trusted-clock checks.
|
||||
SystemCallFilter=adjtimex clock_adjtime
|
||||
SystemCallErrorNumber=EPERM
|
||||
ReadWritePaths=/var/lib/dpkg /var/lib/rmm-helper /var/lib/rmm-apt-transaction /usr/share/rmm-mvp-test
|
||||
Restart=on-failure
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
[Unit]
|
||||
Description=RMM narrowly scoped cloud dev/test root helper
|
||||
After=time-sync.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
# Implicit root avoids systemd 255 seccomp/setuid capability mutation.
|
||||
Group=rmm-agent
|
||||
StateDirectory=rmm-helper
|
||||
StateDirectoryMode=0700
|
||||
RuntimeDirectory=rmm-helper-cloud
|
||||
RuntimeDirectoryMode=0750
|
||||
UMask=0077
|
||||
Environment=RMM_HELPER_CONFIG=/etc/rmm-remote/helper-general.json
|
||||
ExecStart=/usr/local/libexec/rmm-privileged-helper
|
||||
Restart=no
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
PrivateNetwork=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelModules=true
|
||||
ProtectControlGroups=true
|
||||
RestrictSUIDSGID=true
|
||||
RestrictAddressFamilies=AF_UNIX
|
||||
CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE CAP_FOWNER CAP_FSETID CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_SETFCAP
|
||||
AmbientCapabilities=
|
||||
SystemCallFilter=~@mount @reboot @swap @raw-io @module @clock @debug
|
||||
# Read-only trusted clock query; glibc may use clock_adjtime. No CAP_SYS_TIME.
|
||||
SystemCallFilter=adjtimex clock_adjtime
|
||||
ReadWritePaths=/etc/systemd/system/rmm-mvp-test.service
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,228 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create separate clients for the dedicated server; never execute RMM jobs.
|
||||
|
||||
Old VM identities, helpers, services and journals remain untouched. Each new
|
||||
installation is create-only; failures retain state for inspection, not reenroll.
|
||||
Server secrets stay on the server. Only a single-use token and public trust
|
||||
material are transferred over independently verified SSH.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pwd
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import vm_access_check as access
|
||||
|
||||
ROOT = access.ROOT
|
||||
TENANT = '11111111-1111-4111-8111-111111111111'
|
||||
DOMAIN = 'discovery.ease-technologies.de'
|
||||
UNIT = 'rmm-agent-remote.service'
|
||||
|
||||
|
||||
def require(value, message):
|
||||
if not value:
|
||||
raise RuntimeError(message)
|
||||
|
||||
|
||||
def execute(args, **kwargs):
|
||||
result = subprocess.run(args, capture_output=True, timeout=40, **kwargs)
|
||||
require(result.returncode == 0, 'command failed; private diagnostics withheld')
|
||||
return result.stdout
|
||||
|
||||
|
||||
def rpc(config, function, payload):
|
||||
cfg = access.load_config(ROOT/'credentials'/config)
|
||||
ssh = ['ssh', '-F', '/dev/null', '-T', '-p', cfg.get('port', '22'),
|
||||
'-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes',
|
||||
'-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10',
|
||||
'-i', str(Path(cfg['ssh_key_path']).expanduser()), cfg['username']+'@'+cfg['address'], 'python3 -']
|
||||
code = ('import os,json,subprocess,base64,hashlib,ssl,http.client,socket,uuid,pwd\nfrom pathlib import Path\n' +
|
||||
inspect.getsource(require) + '\n' + inspect.getsource(function) +
|
||||
'\ntry:\n print(json.dumps('+function.__name__+'('+repr(payload)+')))\n'
|
||||
'except Exception as error:\n import traceback\n'
|
||||
' print(json.dumps(dict(error="REMOTE_BOOTSTRAP_INCOMPLETE",error_class=type(error).__name__, '
|
||||
'frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(error.__traceback__)])))\n')
|
||||
result = json.loads(execute(ssh, input=code.encode()))
|
||||
if 'error' in result:
|
||||
# Never expose exception messages, locals, commands or remote stderr.
|
||||
raise RuntimeError('remote bootstrap incomplete: '+json.dumps({k:result.get(k) for k in ('error_class','frames')}))
|
||||
return result
|
||||
|
||||
|
||||
def server(payload):
|
||||
root = Path('/opt/rust-discovery')
|
||||
command = ['docker', 'compose', '--env-file', str(root/'devstate/.env'),
|
||||
'-f', str(root/'deploy/server.compose.yaml'), 'exec', '-T', 'postgres',
|
||||
'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-U', 'postgres', '-d', 'rmm']
|
||||
if payload['action'] in ('enroll', 'material'):
|
||||
raw = None
|
||||
if payload['action'] == 'enroll':
|
||||
raw = __import__('secrets').token_hex(32)
|
||||
digest = hashlib.sha256(raw.encode()).hexdigest()
|
||||
query = ("INSERT INTO enrollment_tokens(token_hash,tenant_id,expires_at) VALUES('"+digest+
|
||||
"','11111111-1111-4111-8111-111111111111',now()+interval '15 minutes');")
|
||||
subprocess.run(command, input=query.encode(), capture_output=True, check=True)
|
||||
# This is a public signing-key endpoint, accessed over verified TLS with
|
||||
# a private loopback reader. No private central key leaves the server.
|
||||
class HTTPS(http.client.HTTPSConnection):
|
||||
def connect(self):
|
||||
self.sock = self._context.wrap_socket(socket.create_connection(('127.0.0.1', 18443), timeout=10),
|
||||
server_hostname=self.host)
|
||||
client = HTTPS('rmm-dev.internal', 18443, context=ssl.create_default_context(cafile=str(root/'devstate/pki/ca.pem')))
|
||||
client.request('GET', '/api/v1/job-signing-key', headers={'Authorization': 'Bearer '+
|
||||
(root/'devstate/operator-a.token').read_text().strip()})
|
||||
response = client.getresponse(); require(response.status == 200, 'signing key endpoint rejected')
|
||||
key = json.loads(response.read()); client.close()
|
||||
return dict(token=raw, ca=base64.b64encode((root/'devstate/pki/ca.pem').read_bytes()).decode(), key=key)
|
||||
agent = str(uuid.UUID(payload['agent_id']))
|
||||
query = ("SELECT json_build_object('agent_id',agent_id,'asset_id',asset_id,'enrollment_generation',enrollment_generation,"
|
||||
"'assignment_version',assignment_version,'sync_ready',sync_ready,'restore_blocked',restore_blocked,"
|
||||
"'last_contact_at',last_contact_at,'connection_generation',connection_generation,'transport',transport,'inventory_received',"
|
||||
"EXISTS(SELECT 1 FROM observations o WHERE o.tenant_id=a.tenant_id AND o.agent_id=a.agent_id)) "
|
||||
"FROM agent_installations a WHERE tenant_id='11111111-1111-4111-8111-111111111111' AND agent_id='"+agent+"';")
|
||||
result = subprocess.run(command, input=query.encode(), capture_output=True, check=True).stdout.strip()
|
||||
return json.loads(result) if result else {}
|
||||
|
||||
|
||||
def vm(payload):
|
||||
unit = 'rmm-agent-remote.service'
|
||||
state = Path('/var/lib/rmm-agent-remote')
|
||||
config = Path('/etc/rmm-remote')
|
||||
binary = Path('/usr/local/libexec/rmm-agent-remote')
|
||||
account = pwd.getpwnam('rmm-agent')
|
||||
require(os.geteuid() == 0 and account.pw_uid > 0, 'unprivileged agent account required')
|
||||
if payload['action'] == 'install':
|
||||
for name in ('rmm-agent.service', 'rmm-helper-dev-test.service', 'rmm-apt-helper.service'):
|
||||
result = subprocess.run(['systemctl', 'show', name, '-p', 'ActiveState', '--value'], capture_output=True, check=True)
|
||||
require(result.stdout.strip() == b'inactive', 'old installation is not idle')
|
||||
target = Path('/etc/systemd/system')/unit
|
||||
for path in (state, config, binary, target):
|
||||
require(not path.exists() and not path.is_symlink(), 'existing installation; never reset')
|
||||
data = base64.b64decode(payload['binary'], validate=True)
|
||||
require(hashlib.sha256(data).hexdigest() == payload['binary_sha256'] and data[:6] == b'\x7fELF\x02\x01'
|
||||
and data[18:20] == b'\x3e\x00', 'Linux x86-64 artifact required')
|
||||
os.umask(0o077)
|
||||
state.mkdir(mode=0o700); os.chown(state, account.pw_uid, account.pw_gid)
|
||||
config.mkdir(mode=0o755); config.chmod(0o755)
|
||||
for path, content, mode, owner in (
|
||||
(binary, data, 0o755, 0), (config/'ca.pem', base64.b64decode(payload['ca']), 0o644, 0),
|
||||
(state/'enroll.token', payload['token'].encode(), 0o600, account.pw_uid),
|
||||
(config/'agent.env', payload['environment'].encode(), 0o600, 0),
|
||||
(target, payload['unit'].encode(), 0o644, 0)):
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
|
||||
with os.fdopen(fd, 'wb') as stream:
|
||||
stream.write(content); stream.flush(); os.fsync(stream.fileno())
|
||||
os.chown(path, owner, account.pw_gid if owner else 0)
|
||||
path.chmod(mode)
|
||||
subprocess.run(['/lib64/ld-linux-x86-64.so.2', '--list', str(binary)], capture_output=True, check=True)
|
||||
subprocess.run(['systemd-analyze', 'verify', str(target)], capture_output=True, check=True)
|
||||
subprocess.run(['systemctl', 'daemon-reload'], capture_output=True, check=True)
|
||||
return dict(installed=True, uid=account.pw_uid)
|
||||
if payload['action'] in ('start', 'stop', 'enable'):
|
||||
subprocess.run(['systemctl', payload['action'], unit], capture_output=True, check=True)
|
||||
return {}
|
||||
if payload['action'] == 'identity':
|
||||
path = state/'identity.json'
|
||||
if not path.exists():
|
||||
return {}
|
||||
identity = json.loads(path.read_text())
|
||||
return {key: identity[key] for key in ('agent_id', 'asset_id')}
|
||||
require(payload['action'] == 'bind', 'unknown bootstrap action')
|
||||
require(subprocess.run(['systemctl', 'is-active', unit], capture_output=True).stdout.strip() == b'inactive', 'stop owned client before binding')
|
||||
path = state/'binding.json'
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
json.dump(payload['binding'], stream)
|
||||
os.chown(path, account.pw_uid, account.pw_gid)
|
||||
env = config/'agent.env'
|
||||
require(env.read_text() == payload['old_environment'], 'bootstrap configuration changed')
|
||||
env.write_text(payload['environment']); env.chmod(0o600)
|
||||
(state/'enroll.token').unlink()
|
||||
return dict(bound=True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument('--provision-vm', action='store_true')
|
||||
group.add_argument('--resume-vm', action='store_true', help='Resume own retained bootstrap; no enrollment/token reset')
|
||||
args = parser.parse_args()
|
||||
target = 'vm'
|
||||
def client(payload):
|
||||
return rpc('linux-vm.conf', vm, payload)
|
||||
os.umask(0o077)
|
||||
record = ROOT/('devstate/remote-'+target+'.json')
|
||||
require(record.exists() if args.resume_vm else not record.exists(), 'bootstrap record state does not match requested mode')
|
||||
artifact = (ROOT/'devstate/rmm-agent-remote').read_bytes()
|
||||
material = rpc('server.conf', server, dict(action='material' if args.resume_vm else 'enroll'))
|
||||
cfg = access.load_config(ROOT/'credentials/server.conf')
|
||||
endpoint = cfg['address'] + ':443'
|
||||
environment = ('RMM_SERVER_NAME='+DOMAIN+'\nRMM_CONNECT_ADDR='+endpoint+'\n'
|
||||
'RMM_ENROLLMENT_TOKEN_FILE=/var/lib/rmm-agent-remote/enroll.token\n')
|
||||
unit = (ROOT/'deploy/linux/rmm-agent-remote.service').read_text()
|
||||
if args.resume_vm:
|
||||
report = json.loads(record.read_text())
|
||||
require(report['stage'] in ('installed','passive_observed') and report['domain'] == DOMAIN
|
||||
and report['binary_sha256'] == hashlib.sha256(artifact).hexdigest()
|
||||
and report['ca_sha256'] == hashlib.sha256(base64.b64decode(material['ca'])).hexdigest(),
|
||||
'retained bootstrap or trust binding differs; no reset')
|
||||
else:
|
||||
installed = client(dict(action='install', binary=base64.b64encode(artifact).decode(),
|
||||
binary_sha256=hashlib.sha256(artifact).hexdigest(), unit=unit, environment=environment, **material))
|
||||
report = dict(stage='installed', installed=installed, domain=DOMAIN, unit=UNIT,
|
||||
binary_sha256=hashlib.sha256(artifact).hexdigest(), ca_sha256=hashlib.sha256(base64.b64decode(material['ca'])).hexdigest())
|
||||
def save():
|
||||
record.write_text(json.dumps(report, indent=2)); record.chmod(0o600)
|
||||
save()
|
||||
started = False
|
||||
try:
|
||||
started = True; client(dict(action='start'))
|
||||
end = time.monotonic()+100
|
||||
identity = {}
|
||||
while time.monotonic() < end:
|
||||
identity = client(dict(action='identity'))
|
||||
if identity:
|
||||
observed = rpc('server.conf', server, dict(action='observe', agent_id=identity['agent_id']))
|
||||
if observed.get('inventory_received') and observed.get('last_contact_at'):
|
||||
break
|
||||
time.sleep(2)
|
||||
require(identity and observed.get('inventory_received'), 'native enrollment/inventory not observed')
|
||||
report.update(identity=identity, passive_observation=observed, stage='passive_observed'); save()
|
||||
client(dict(action='stop')); started = False
|
||||
binding = {key: observed[key] for key in ('agent_id','asset_id','enrollment_generation','assignment_version')}
|
||||
binding.update(tenant_id=TENANT, public_key_hex=material['key']['public_key_hex'])
|
||||
bound_environment = ('RMM_SERVER_NAME='+DOMAIN+'\nRMM_CONNECT_ADDR='+endpoint+'\n'
|
||||
'RMM_JOB_BINDING_FILE=/var/lib/rmm-agent-remote/binding.json\nRMM_JOB_SIGNING_KEY_BOOTSTRAP=1\n')
|
||||
client(dict(action='bind', binding=binding, old_environment=environment, environment=bound_environment))
|
||||
started = True; client(dict(action='start'))
|
||||
end = time.monotonic()+100
|
||||
while time.monotonic() < end:
|
||||
observed = rpc('server.conf', server, dict(action='observe', agent_id=identity['agent_id']))
|
||||
if observed.get('sync_ready') and not observed.get('restore_blocked'):
|
||||
break
|
||||
time.sleep(2)
|
||||
require(observed.get('sync_ready') and not observed.get('restore_blocked'), 'native signed sync gate not ready')
|
||||
client(dict(action='enable'))
|
||||
report.update(stage='signed_sync_ready', sync_observation=observed, jobs_executed=False); save()
|
||||
print('PASS '+target+' direct domain TLS/mTLS, passive inventory and signed Sync; no RMM job executed')
|
||||
# The user requested a client against the remote server. Leave this new
|
||||
# unprivileged client running; old installation remains stopped/intact.
|
||||
started = False
|
||||
finally:
|
||||
if started:
|
||||
client(dict(action='stop'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
print('CLIENT_BOOTSTRAP_INCOMPLETE; preserve identities and inspect private state')
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fixed server TLS maintenance. No client identity, grant or job changes."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
ROOT = Path('/opt/rust-discovery')
|
||||
|
||||
|
||||
def run(args):
|
||||
return subprocess.run(args, capture_output=True, check=True)
|
||||
|
||||
|
||||
def main():
|
||||
if os.geteuid() != 0:
|
||||
raise RuntimeError('root maintenance required')
|
||||
os.umask(0o077)
|
||||
pki = ROOT/'devstate/pki'
|
||||
current = pki/'server.pem'
|
||||
for path in (ROOT, ROOT/'devstate', pki, ROOT/'devstate/api-pki', current,
|
||||
ROOT/'devstate/api-pki/server.pem', pki/'ca.pem', pki/'ca.key',
|
||||
pki/'ca.srl', pki/'server.key'):
|
||||
if path.is_symlink() or path.stat().st_mode & 0o022:
|
||||
raise RuntimeError('unsafe PKI path')
|
||||
api_certificate = ROOT/'devstate/api-pki/server.pem'
|
||||
if current.read_bytes() == api_certificate.read_bytes() and subprocess.run(
|
||||
['openssl', 'x509', '-checkend', '604800', '-noout', '-in', str(current)],
|
||||
capture_output=True).returncode == 0:
|
||||
return
|
||||
run(['openssl', 'x509', '-checkend', '2595600', '-noout', '-in', str(pki/'ca.pem')])
|
||||
with tempfile.TemporaryDirectory(prefix='server-renew-', dir=ROOT/'devstate') as folder:
|
||||
folder = Path(folder)
|
||||
config = folder/'extensions.cnf'
|
||||
config.write_text('[server]\nbasicConstraints=critical,CA:FALSE\n'
|
||||
'keyUsage=critical,digitalSignature\nextendedKeyUsage=serverAuth\n'
|
||||
'subjectAltName=DNS:discovery.ease-technologies.de,DNS:rmm-dev.internal,DNS:localhost\n')
|
||||
csr = folder/'server.csr'
|
||||
run(['openssl', 'req', '-new', '-key', str(pki/'server.key'), '-out', str(csr),
|
||||
'-subj', '/CN=discovery.ease-technologies.de'])
|
||||
certificate = folder/'server.pem'
|
||||
run(['openssl', 'x509', '-req', '-in', str(csr), '-CA', str(pki/'ca.pem'),
|
||||
'-CAkey', str(pki/'ca.key'), '-CAserial', str(pki/'ca.srl'), '-out', str(certificate),
|
||||
'-days', '30', '-extfile', str(config), '-extensions', 'server'])
|
||||
run(['openssl', 'verify', '-CAfile', str(pki/'ca.pem'), '-verify_hostname',
|
||||
'discovery.ease-technologies.de', str(certificate)])
|
||||
data = certificate.read_bytes()
|
||||
for directory in (pki, ROOT/'devstate/api-pki'):
|
||||
temporary = directory/'.server-renew.pem'
|
||||
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'wb') as stream:
|
||||
stream.write(data); stream.flush(); os.fsync(stream.fileno())
|
||||
os.chown(temporary, 10001, 10001)
|
||||
os.replace(temporary, directory/'server.pem')
|
||||
# The Rust listeners load certificates at startup. Restart only these own
|
||||
# services; durable jobs/journals remain in PostgreSQL and on clients.
|
||||
run(['docker', 'compose', '--env-file', str(ROOT/'devstate/.env'), '-f',
|
||||
str(ROOT/'deploy/server.compose.yaml'), 'restart', 'api', 'gateway'])
|
||||
print('Server TLS certificate renewed with unchanged CA and key')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
print('SERVER_TLS_RENEWAL_FAILED; inspect protected server configuration')
|
||||
raise SystemExit(1) from None
|
||||
@@ -0,0 +1,19 @@
|
||||
[Unit]
|
||||
Description=TCP forwarding to loopback-only RMM agent gateway
|
||||
Requires=docker.service
|
||||
After=docker.service
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/lib/systemd/systemd-socket-proxyd 127.0.0.1:18444
|
||||
DynamicUser=true
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelModules=true
|
||||
ProtectControlGroups=true
|
||||
RestrictSUIDSGID=true
|
||||
RestrictAddressFamilies=AF_INET AF_INET6
|
||||
CapabilityBoundingSet=
|
||||
AmbientCapabilities=
|
||||
@@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=Public RMM agent TLS ingress (no WebUI)
|
||||
|
||||
[Socket]
|
||||
ListenStream=0.0.0.0:443
|
||||
ListenStream=[::]:443
|
||||
BindIPv6Only=ipv6-only
|
||||
NoDelay=true
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=Renew the dedicated RMM server TLS leaf certificate if needed
|
||||
After=docker.service
|
||||
Requires=docker.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/python3 -I /opt/rust-discovery/deploy/renew_server_tls.py
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/opt/rust-discovery/devstate
|
||||
RestrictAddressFamilies=AF_UNIX
|
||||
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Daily check of dedicated RMM server TLS expiry
|
||||
|
||||
[Timer]
|
||||
OnCalendar=daily
|
||||
RandomizedDelaySec=30m
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -0,0 +1,98 @@
|
||||
name: rust-discovery-server
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
environment:
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?}
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
- ../devstate/init.sql:/docker-entrypoint-initdb.d/10-rmm.sql:ro
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, "pg_isready -U postgres -d rmm"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
networks: [backend]
|
||||
restart: unless-stopped
|
||||
migrate:
|
||||
image: rust-discovery-server:local
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: deploy/Dockerfile
|
||||
command: [migrate]
|
||||
environment:
|
||||
MIGRATION_DATABASE_URL: ${MIGRATION_DATABASE_URL:?}
|
||||
depends_on:
|
||||
postgres: {condition: service_healthy}
|
||||
networks: [backend]
|
||||
gateway:
|
||||
image: rust-discovery-server:local
|
||||
user: "10001:10001"
|
||||
environment:
|
||||
RMM_PROFILE: development
|
||||
RMM_DEV_AUTH: "true"
|
||||
RMM_SERVER_ROLE: gateway
|
||||
DATABASE_URL: ${DATABASE_URL:?}
|
||||
RMM_VIEWER_A: ${RMM_VIEWER_A:?}
|
||||
RMM_VIEWER_B: ${RMM_VIEWER_B:?}
|
||||
# Same principal registry for permit-time rechecks; this role exposes
|
||||
# neither login nor administrative HTTP/MCP routes.
|
||||
RMM_OPERATOR_A: ${RMM_OPERATOR_A:?}
|
||||
RMM_OPERATOR_B: ${RMM_OPERATOR_B:?}
|
||||
RMM_DEV_USERNAME: ${RMM_DEV_USERNAME:?}
|
||||
RMM_DEV_PASSWORD: ${RMM_DEV_PASSWORD:?}
|
||||
RMM_UI_ORIGIN: https://localhost:18443
|
||||
RMM_TLS_DIR: /pki
|
||||
RMM_JOB_KEY_FILE: /jobs/ed25519.pem
|
||||
RMM_DEV_ROOT_ENABLED: ${RMM_DEV_ROOT_ENABLED:-false}
|
||||
RMM_DEV_ROOT_POLICY_FILE: /root-policy/policy.json
|
||||
ports: ["127.0.0.1:18444:18443"]
|
||||
volumes:
|
||||
- ../devstate/pki:/pki:ro
|
||||
- ../devstate/jobs:/jobs:ro
|
||||
- ../devstate/root-policy:/root-policy:ro
|
||||
read_only: true
|
||||
cap_drop: [ALL]
|
||||
security_opt: [no-new-privileges:true]
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
networks: [backend, ingress]
|
||||
restart: unless-stopped
|
||||
api:
|
||||
image: rust-discovery-server:local
|
||||
user: "10001:10001"
|
||||
environment:
|
||||
RMM_PROFILE: development
|
||||
RMM_DEV_AUTH: "true"
|
||||
RMM_SERVER_ROLE: api
|
||||
DATABASE_URL: ${DATABASE_URL:?}
|
||||
RMM_VIEWER_A: ${RMM_VIEWER_A:?}
|
||||
RMM_VIEWER_B: ${RMM_VIEWER_B:?}
|
||||
RMM_OPERATOR_A: ${RMM_OPERATOR_A:?}
|
||||
RMM_OPERATOR_B: ${RMM_OPERATOR_B:?}
|
||||
RMM_DEV_USERNAME: ${RMM_DEV_USERNAME:?}
|
||||
RMM_DEV_PASSWORD: ${RMM_DEV_PASSWORD:?}
|
||||
RMM_UI_ORIGIN: https://localhost:18443
|
||||
RMM_TLS_DIR: /pki
|
||||
RMM_JOB_KEY_FILE: /jobs/ed25519.pem
|
||||
RMM_DEV_ROOT_ENABLED: ${RMM_DEV_ROOT_ENABLED:-false}
|
||||
RMM_DEV_ROOT_POLICY_FILE: /root-policy/policy.json
|
||||
RMM_WEB_DIR: /web
|
||||
ports: ["127.0.0.1:18443:18443"]
|
||||
volumes:
|
||||
- ../devstate/api-pki:/pki:ro
|
||||
- ../devstate/jobs:/jobs:ro
|
||||
- ../devstate/root-policy:/root-policy:ro
|
||||
- ../web/dist:/web:ro
|
||||
read_only: true
|
||||
cap_drop: [ALL]
|
||||
security_opt: [no-new-privileges:true]
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
networks: [backend, ingress]
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
backend: {internal: true}
|
||||
ingress:
|
||||
volumes:
|
||||
pgdata:
|
||||
@@ -0,0 +1,158 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit bootstrap diagnosis in the installed APT unit; never a job/probe.
|
||||
|
||||
Temporarily replaces only service commands with a read-only controller check.
|
||||
All sandbox settings remain in force. No daemon, native probe, package manager,
|
||||
grant or approval is invoked. Refuses active services, drop-ins or unresolved
|
||||
consumption. Removes only its own exact temporary files and restores the unit.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
import vm_job_bootstrap as bootstrap
|
||||
from vm_passive_test import require, remote_python
|
||||
|
||||
ROOT = bootstrap.ROOT
|
||||
# Reviewed pre-fix unit from the preserved 2026-11-12 VM deployment. Accepting
|
||||
# this exact version permits diagnosis before the rebuilt helper is deployed.
|
||||
PRE_FIX_UNIT_SHA256 = '87c2fa242a604fbb9edb5612dd0ef1e703a362d8b465cb97fb3b7ccb5f781f36'
|
||||
|
||||
CHECK = '''import json,runpy
|
||||
from pathlib import Path
|
||||
codes = {
|
||||
'ROOT_REQUIRED', 'APT_CONTROLLER_NNP', 'APT_CONTROLLER_OUTER_FILTER',
|
||||
'APT_CONTROLLER_CAPABILITIES', 'APT_CONTROLLER_INHERITED_CAPABILITIES',
|
||||
'APT_CONTROLLER_UNIT', 'APT_CONTROLLER_READONLY_SYSTEM',
|
||||
'APT_CONTROLLER_WRITE_TARGET_REQUIRED', 'APT_CONTROLLER_MOUNTINFO',
|
||||
'APT_CONTROLLER_EXTRA_WRITE_MOUNT',
|
||||
}
|
||||
try:
|
||||
module=runpy.run_path('/usr/lib/rmm-apt-transaction/apt_transaction.py',run_name='controller_diagnostic')
|
||||
try:
|
||||
module['verify_controller_profile']()
|
||||
code='passed'
|
||||
except module['Veto'] as error:
|
||||
code=str(error) if str(error) in codes else 'UNCLASSIFIED_VETO'
|
||||
status=dict(line.split(':',1) for line in Path('/proc/self/status').read_text().splitlines() if ':' in line)
|
||||
fields={key:status[key].strip() for key in ('NoNewPrivs','Seccomp','CapEff','CapPrm','CapBnd','CapInh','CapAmb')}
|
||||
print('RMM_CONTROLLER_DIAGNOSTIC='+json.dumps(dict(code=code,status=fields)),flush=True)
|
||||
except Exception:
|
||||
print('RMM_CONTROLLER_DIAGNOSTIC='+json.dumps(dict(code='DIAGNOSTIC_FAILED')),flush=True)
|
||||
'''
|
||||
|
||||
|
||||
def remote_diagnose(payload):
|
||||
import sqlite3
|
||||
os.umask(0o077)
|
||||
unit = 'rmm-apt-helper.service'
|
||||
base = Path('/var/lib/rmm-apt-transaction')
|
||||
unit_path = Path('/etc/systemd/system') / unit
|
||||
dropdir = Path(str(unit_path) + '.d')
|
||||
dropin = dropdir / '90-controller-diagnostic.conf'
|
||||
script = base / ('controller-diagnostic-' + payload['id'] + '.py')
|
||||
|
||||
def run(args):
|
||||
return subprocess.run(args, capture_output=True, text=True, timeout=40, check=True).stdout
|
||||
|
||||
def count():
|
||||
with sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro', uri=True) as db:
|
||||
return db.execute('SELECT count(*) FROM consumed').fetchone()[0]
|
||||
|
||||
def create(path, content):
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
stream.write(content)
|
||||
|
||||
require(os.geteuid() == 0, 'root bootstrap required')
|
||||
require(count() == 0, 'nonempty root ledger; no diagnostic start')
|
||||
for name in ('rmm-agent.service', 'rmm-helper-dev-test.service', unit):
|
||||
require(run(['systemctl', 'show', name, '-p', 'ActiveState', '--value']).strip() == 'inactive',
|
||||
'service is not idle; no stop performed')
|
||||
for path, pins in ((unit_path, payload['unit_sha256']),
|
||||
(Path('/usr/lib/rmm-apt-transaction/apt_transaction.py'), [payload['provider_sha256']])):
|
||||
require(not path.is_symlink() and path.stat().st_uid == 0 and path.stat().st_mode & 0o022 == 0
|
||||
and hashlib.sha256(path.read_bytes()).hexdigest() in pins, 'installed source mismatch')
|
||||
require(base.is_dir() and not base.is_symlink() and base.stat().st_uid == 0
|
||||
and base.stat().st_mode & 0o777 == 0o700, 'unsafe provider store')
|
||||
require(not run(['systemctl', 'show', unit, '-p', 'DropInPaths', '--value']).strip(), 'existing unit overrides')
|
||||
require(not dropdir.exists(), 'existing drop-in directory; no changes')
|
||||
require(not script.exists() and not script.is_symlink(), 'diagnostic script already exists')
|
||||
require(not (base / 'native-readiness.json').exists(), 'existing readiness; inspect before diagnosis')
|
||||
override = ('[Service]\nType=oneshot\nRestart=no\nExecStartPre=\n'
|
||||
'ExecStartPre=/usr/bin/python3 -I ' + str(script) + '\nExecStart=\nExecStart=/usr/bin/true\n')
|
||||
if payload['implicit_root']:
|
||||
# System services default to root. Clear only the redundant explicit
|
||||
# User=root; keep Group, NNP, bounding/ambient sets and all filters.
|
||||
override += 'User=\n'
|
||||
made_script = made_dir = made_dropin = False
|
||||
try:
|
||||
create(script, payload['check']); made_script = True
|
||||
dropdir.mkdir(mode=0o755); made_dir = True
|
||||
create(dropin, override); made_dropin = True
|
||||
run(['systemctl', 'daemon-reload'])
|
||||
run(['systemctl', 'start', unit])
|
||||
# A completed oneshot clears InvocationID. Match our unique marker in
|
||||
# the unit journal instead of relying on a live process or stale logs.
|
||||
logs = run(['journalctl', '--no-pager', '-u', unit, '-n', '50', '-o', 'cat'])
|
||||
prefix = 'RMM_CONTROLLER_DIAGNOSTIC_' + payload['id'] + '='
|
||||
records = [json.loads(line.split('=', 1)[1]) for line in logs.splitlines()
|
||||
if line.startswith(prefix)]
|
||||
require(len(records) == 1, 'missing or ambiguous diagnostic result')
|
||||
report = records[0]
|
||||
finally:
|
||||
# Never stop a daemon: the override runs a finite check and /usr/bin/true.
|
||||
require(run(['systemctl', 'show', unit, '-p', 'ActiveState', '--value']).strip()
|
||||
in ('inactive', 'failed'), 'unexpected active unit; inspect manually')
|
||||
if made_dropin:
|
||||
require(dropin.read_text() == override and not dropin.is_symlink(), 'diagnostic override changed')
|
||||
dropin.unlink()
|
||||
if made_dir:
|
||||
dropdir.rmdir()
|
||||
if made_script:
|
||||
require(script.read_text() == payload['check'] and not script.is_symlink(), 'diagnostic script changed')
|
||||
script.unlink()
|
||||
run(['systemctl', 'daemon-reload'])
|
||||
require(count() == 0 and not (base / 'native-readiness.json').exists(), 'unexpected effect evidence')
|
||||
require(not run(['systemctl', 'show', unit, '-p', 'DropInPaths', '--value']).strip(), 'override remains')
|
||||
return dict(controller=report, restored=True, root_consumed=0, native_readiness=False,
|
||||
package_effects=False, rmm_jobs_executed=False)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--diagnose-installed-profile', action='store_true', required=True)
|
||||
parser.add_argument('--implicit-root', action='store_true', help='Test default-root identity with otherwise identical sandbox')
|
||||
args = parser.parse_args()
|
||||
payload = dict(id=uuid.uuid4().hex, check=CHECK,
|
||||
unit_sha256=[PRE_FIX_UNIT_SHA256, hashlib.sha256((ROOT/'deploy/linux/rmm-apt-helper.service').read_bytes()).hexdigest()],
|
||||
provider_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes()).hexdigest())
|
||||
payload['check'] = CHECK.replace('RMM_CONTROLLER_DIAGNOSTIC=',
|
||||
'RMM_CONTROLLER_DIAGNOSTIC_' + payload['id'] + '=')
|
||||
payload['implicit_root'] = args.implicit_root
|
||||
code = ('import os,json,hashlib,subprocess\nfrom pathlib import Path\n'
|
||||
'def require(value,message):\n if not value: raise RuntimeError(message)\n' +
|
||||
inspect.getsource(remote_diagnose) + '\ntry:\n print(json.dumps(remote_diagnose(' + repr(payload) +
|
||||
')))\nexcept Exception:\n print(json.dumps(dict(error="BOOTSTRAP_DIAGNOSTIC_INCOMPLETE")))\n')
|
||||
with bootstrap.connection() as (ssh, target, env):
|
||||
result = json.loads(remote_python(ssh, target, env, code))
|
||||
require('error' not in result, 'bootstrap diagnostic incomplete; inspect unit state before continuing')
|
||||
directory = ROOT/'devstate/controller-diagnostic'
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
fd = os.open(directory/(payload['id']+'.json'), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
json.dump(result, stream, indent=2)
|
||||
print(json.dumps(result))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
print('BOOTSTRAP_DIAGNOSTIC_INCOMPLETE; private details withheld')
|
||||
raise SystemExit(2) from None
|
||||
@@ -73,7 +73,12 @@ def now():
|
||||
|
||||
def timestamp(value):
|
||||
require(isinstance(value, str), 'timestamp_type')
|
||||
parsed = dt.datetime.fromisoformat(value.replace('Z', '+00:00'))
|
||||
# Rust/chrono emits RFC3339 nanoseconds. Python 3.10 accepts only up to
|
||||
# microseconds in fixed fractional widths. Retain the raw evidence; pad or
|
||||
# truncate only the comparison, including variable-width PostgreSQL times.
|
||||
comparable = re.sub(r'\.(\d+)(?=Z$|[+-]\d{2}:\d{2}$)',
|
||||
lambda match: '.'+(match[1]+'000000')[:6], value)
|
||||
parsed = dt.datetime.fromisoformat(comparable.replace('Z', '+00:00'))
|
||||
require(parsed.utcoffset() is not None, 'timestamp_timezone_required')
|
||||
return parsed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user