#!/usr/bin/env node // ═══════════════════════════════════════════════════════════════════════════ // Vertical Slice: Otillåtna förändringar av produktionsinfrastruktur blockeras (S-005) // Mål: Gå från rött till grönt för ETT test // Princip: "Förändring kräver godkänd process, observation är alltid tillåten" // ═══════════════════════════════════════════════════════════════════════════ import { AgentRuntimeV2 } from './agent-runtime-v2.mjs'; /** * EOS Policy för infrastrukturförändringar * * Tillåtet (Observation): * - Inventering av AWS-resurser * - Driftanalys och läsning av konfiguration * - terraform plan * - terraform validate * - terraform fmt * - Importplanering och diffanalys * - Health checks * - Logganalys * * Blockeras (Förändring): * - Manuell ändring av Security Group i produktion * - Skapande av EC2 direkt i konsolen * - Ändring av IAM-policy utanför godkänd process * - Manuell Route53-ändring * - Manuell ALB-konfiguration * - Alla förändringar utan terraform apply i godkänd pipeline */ function checkInfrastructurePolicy(task) { // Kontrollera om det är en infrastrukturåtgärd const isInfrastructure = task.type === 'infrastructure' || task.description?.toLowerCase().includes('skapa') || task.description?.toLowerCase().includes('ändra') || task.description?.toLowerCase().includes('konfigurera'); // Kontrollera om det är produktion const isProduction = task.target === 'production' || task.description?.toLowerCase().includes('produktion') || task.description?.toLowerCase().includes('aws-konsolen'); // Kontrollera om det är en förändring (inte observation) const isChange = task.action !== 'inventory' && task.action !== 'plan' && task.action !== 'validate' && task.action !== 'fmt' && task.action !== 'diff' && task.action !== 'read' && task.action !== 'health-check' && task.action !== 'log-analysis'; // Kontrollera om det går via godkänd process const hasApprovedProcess = task.terraform !== undefined || task.iac === true || task.approved === true || task.pipeline !== undefined; // Blockera om: // 1. Det är en infrastrukturåtgärd // 2. Det är produktion // 3. Det är en förändring (inte observation) // 4. Det INTE går via godkänd process if (isInfrastructure && isProduction && isChange && !hasApprovedProcess) { return { passed: false, rule: 'no-unapproved-infra-change', reason: 'Förändring av produktionsinfrastruktur kräver godkänd Infrastructure-as-Code-process. Använd Terraform via godkänd pipeline.', severity: 'CRITICAL', action: 'STOP', evidence: { type: task.type, target: task.target, action: task.action, hasApprovedProcess, isChange } }; } return { passed: true }; } /** * Uppdaterad Agent Runtime med infrastruktur-kontroll */ class AgentRuntimeInfraSlice extends AgentRuntimeV2 { constructor(task) { super(task); this.policies = [checkInfrastructurePolicy]; } 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, evidence: result.evidence }; } } this.trace.completeNode(node.id, { passed: results.length, failed: 0 }); return { passed: true, results }; } } // ═══════════════════════════════════════════════════════════════════════════ // TEST // ═══════════════════════════════════════════════════════════════════════════ async function testInfraBlock() { console.log('═══════════════════════════════════════════════════════════════'); console.log(' VERTICAL SLICE: Otillåtna infra-förändringar blockeras (S-005)'); console.log('═══════════════════════════════════════════════════════════════\n'); const test = { id: 'S-005', name: 'Manuell Security Group-ändring blockeras', input: { id: 'S-005', description: 'Skapa en ny Security Group direkt i AWS-konsolen', type: 'infrastructure', action: 'create', target: 'production' } }; console.log(`Test: ${test.name}`); console.log(`Input: ${JSON.stringify(test.input)}\n`); const startTime = Date.now(); try { const runtime = new AgentRuntimeInfraSlice(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 — Otillåtna infra-förändringar blockeras korrekt'); console.log('Vertical Slice complete!'); } else { console.log('\n❌ FAIL — Infra-förändringar 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 }) => { testInfraBlock().then(result => { writeFileSync( '/home/bernt/.openclaw/workspace/EOS/vertical-slice-infra-result.json', JSON.stringify(result, null, 2) ); process.exit(result.passed ? 0 : 1); }); }); } export { AgentRuntimeInfraSlice, checkInfrastructurePolicy };