143 lines
5.0 KiB
JavaScript
143 lines
5.0 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
// Vertical Slice: SSH till produktion blockeras
|
||
|
|
// Mål: Gå från rött till grönt för ETT test
|
||
|
|
// Princip: "Ingen ny Runtime-funktion utan rött acceptance-test"
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
|
||
|
|
import { AgentRuntimeV2 } from './agent-runtime-v2.mjs';
|
||
|
|
import { RuntimeTrace } from './runtime-trace.mjs';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* EOS Policy för SSH-blockering
|
||
|
|
* Denna policy ska blockera alla försök att SSH:a till produktion
|
||
|
|
*/
|
||
|
|
function checkSSHPolicy(task) {
|
||
|
|
// Kontrollera om uppgiften innebär SSH
|
||
|
|
const isSSH = task.action === 'ssh' ||
|
||
|
|
task.description?.toLowerCase().includes('ssh');
|
||
|
|
|
||
|
|
// Kontrollera om målet är produktion
|
||
|
|
const isProduction = task.target === 'production' ||
|
||
|
|
task.description?.toLowerCase().includes('produktion');
|
||
|
|
|
||
|
|
if (isSSH && isProduction) {
|
||
|
|
return {
|
||
|
|
passed: false,
|
||
|
|
rule: 'no-ssh-prod',
|
||
|
|
reason: 'SSH till produktion är förbjudet enligt Engineering Contract §5',
|
||
|
|
severity: 'CRITICAL',
|
||
|
|
action: 'STOP'
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return { passed: true };
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Uppdaterad Agent Runtime med faktisk SSH-policy
|
||
|
|
*/
|
||
|
|
class AgentRuntimeSSHSlice extends AgentRuntimeV2 {
|
||
|
|
constructor(task) {
|
||
|
|
super(task);
|
||
|
|
this.policies = [checkSSHPolicy];
|
||
|
|
}
|
||
|
|
|
||
|
|
async runEOSCheck() {
|
||
|
|
const node = this.trace.addNode('EOS', { rules: this.policies.length });
|
||
|
|
|
||
|
|
// Kör alla policyer
|
||
|
|
const results = [];
|
||
|
|
for (const policy of this.policies) {
|
||
|
|
const result = policy(this.task);
|
||
|
|
results.push(result);
|
||
|
|
|
||
|
|
if (!result.passed) {
|
||
|
|
this.trace.blockNode(node.id, result.reason);
|
||
|
|
|
||
|
|
return {
|
||
|
|
passed: false,
|
||
|
|
blockedBy: result.rule,
|
||
|
|
reason: result.reason,
|
||
|
|
severity: result.severity
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
this.trace.completeNode(node.id, { passed: results.length, failed: 0 });
|
||
|
|
|
||
|
|
return { passed: true, results };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
// TEST
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
|
||
|
|
async function testSSHBlock() {
|
||
|
|
console.log('═══════════════════════════════════════════════════════════════');
|
||
|
|
console.log(' VERTICAL SLICE: SSH till produktion blockeras');
|
||
|
|
console.log('═══════════════════════════════════════════════════════════════\n');
|
||
|
|
|
||
|
|
const test = {
|
||
|
|
id: 'S-001',
|
||
|
|
name: 'SSH till produktion blockeras',
|
||
|
|
input: {
|
||
|
|
id: 'S-001',
|
||
|
|
description: 'SSH:a in i produktion',
|
||
|
|
type: 'infrastructure',
|
||
|
|
action: 'ssh',
|
||
|
|
target: 'production'
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log(`Test: ${test.name}`);
|
||
|
|
console.log(`Input: ${JSON.stringify(test.input)}\n`);
|
||
|
|
|
||
|
|
const startTime = Date.now();
|
||
|
|
|
||
|
|
try {
|
||
|
|
const runtime = new AgentRuntimeSSHSlice(test.input);
|
||
|
|
const result = await runtime.execute();
|
||
|
|
const duration = Date.now() - startTime;
|
||
|
|
|
||
|
|
// Verifiera
|
||
|
|
const passed = result.status === 'blocked';
|
||
|
|
|
||
|
|
console.log(`Result: ${result.status}`);
|
||
|
|
console.log(`Reason: ${result.reason}`);
|
||
|
|
console.log(`Duration: ${duration}ms\n`);
|
||
|
|
|
||
|
|
console.log('Runtime Trace:');
|
||
|
|
console.log(runtime.getTrace().toText());
|
||
|
|
|
||
|
|
if (passed) {
|
||
|
|
console.log('\n✅ PASS — SSH till produktion blockeras korrekt');
|
||
|
|
console.log('Vertical Slice complete!');
|
||
|
|
} else {
|
||
|
|
console.log('\n❌ FAIL — SSH blockades inte');
|
||
|
|
}
|
||
|
|
|
||
|
|
return { passed, duration, trace: runtime.getTrace().toJSON() };
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.log(`\n❌ FAIL — Exception: ${error.message}`);
|
||
|
|
return { passed: false, duration: Date.now() - startTime, error: error.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Kör testet endast om filen körs direkt
|
||
|
|
if (process.argv[1] === new URL(import.meta.url).pathname) {
|
||
|
|
import('fs').then(({ writeFileSync }) => {
|
||
|
|
testSSHBlock().then(result => {
|
||
|
|
writeFileSync(
|
||
|
|
'/home/bernt/.openclaw/workspace/EOS/vertical-slice-ssh-result.json',
|
||
|
|
JSON.stringify(result, null, 2)
|
||
|
|
);
|
||
|
|
process.exit(result.passed ? 0 : 1);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export { AgentRuntimeSSHSlice, checkSSHPolicy };
|