Files
rust-discovery/deploy/browser/cloud_apt_diagnostic.cjs
T
2026-11-13 13:49:11 +01:00

95 lines
7.6 KiB
JavaScript

// 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;
});