useStoragePath($root.'/audit-worker-storage'); $app->make(Kernel::class)->bootstrap(); if (! $app->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::connection()->getConfig('host') !== '127.0.0.1' || (string) DB::connection()->getConfig('port') !== '55439' || DB::connection()->getDatabaseName() !== $database) { throw new LogicException('Only the owned synthetic PostgreSQL recovery is permitted.'); } require_once __DIR__.'/../Support/RequisitionLedgerQaRuntime.php'; $source = requisitionLedgerQaSource(); if ($source === null || getenv('RQ01_LEDGER_INTEGRATED_SCHEMA') !== '1') { throw new LogicException('Complete private composition required.'); } require_once $source.'/bootstrap.php'; LedgerSchema::assertSynthetic(DB::connection()); config(['private-audit.qa_enabled' => true]); $files = $root.($mode === 'prepare' ? '/audit-source-files' : '/audit-restored-files'); foreach (['local', 'supplier_documents', 'receipt_documents'] as $disk) { config(['filesystems.disks.'.$disk => ['driver' => 'local', 'root' => $files.'/'.$disk, 'throw' => true]]); Storage::forgetDisk($disk); } $snapshot = static function (): array { return DB::transaction(function () { DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); $frames = []; foreach (DB::select("select tablename from pg_tables where schemaname='public' order by tablename") as $table) { $name = str_replace('"', '""', $table->tablename); $rows = DB::select('select row_to_json(t)::text as row from "'.$name.'" t order by row_to_json(t)::text'); $frames[$table->tablename] = hash('sha256', json_encode([DB::getSchemaBuilder()->getColumnListing($table->tablename), array_column($rows, 'row')], JSON_THROW_ON_ERROR)); } return $frames; }); }; $service = app(PrivateAuditExporter::class); if ($mode === 'prepare') { LedgerSchema::install(DB::connection()); require_once __DIR__.'/../Support/IntegratedPurchasingQaSchema.php'; integratedPurchasingQaSchema(); require_once __DIR__.'/../Support/ReceiptFixture.php'; require_once __DIR__.'/../Support/SupplyFixture.php'; $f = receiptFixture(); $actor = $f['issuer']; auth()->guard()->setUser($actor); supplyGrant($actor, array_column(PrivateAuditAccess::FAMILIES, 'permission')); $bytes = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl6E4sAAAAASUVORK5CYII='); $path = $actor->tenant_id.'/'.$f['order']->supplier_id.'/synthetic-recovery.png'; Storage::disk('supplier_documents')->put($path, $bytes); $document = SupplierDocument::query()->create(['tenant_id' => $actor->tenant_id, 'supplier_id' => $f['order']->supplier_id, 'uploaded_by' => $actor->id, 'document_type' => 'other', 'original_name' => 'synthetic-recovery.png', 'path' => $path, 'mime_type' => 'image/png', 'size' => strlen($bytes), 'sha256' => hash('sha256', $bytes)]); $input = ['operation_key' => (string) Str::uuid(), 'families' => array_keys(PrivateAuditAccess::FAMILIES), 'purpose' => 'Synthetic SQL and private files recovery verification']; $export = $service->generate($actor, $input); $service->verifiedPath($actor, $export); echo json_encode(['actor' => $actor->id, 'export' => $export->id, 'input' => $input, 'sha256' => $export->sha256, 'size' => $export->size, 'document' => $document->id, 'source_sha256' => hash('sha256', $bytes), 'snapshot' => $snapshot()], JSON_THROW_ON_ERROR); exit; } $fixture = json_decode($argv[3], true, 64, JSON_THROW_ON_ERROR); if ($snapshot() !== $fixture['snapshot']) { throw new RuntimeException('Restored audit database differs in tables, columns or rows.'); } $actor = User::query()->findOrFail($fixture['actor']); auth()->guard()->setUser($actor); $export = PrivateAuditExport::query()->findOrFail($fixture['export']); $path = $service->verifiedPath($actor, $export); $document = SupplierDocument::query()->findOrFail($fixture['document']); if (hash_file('sha256', $path) !== $fixture['sha256'] || filesize($path) !== $fixture['size'] || hash('sha256', Storage::disk('supplier_documents')->get($document->path)) !== $fixture['source_sha256'] || $service->generate($actor, $fixture['input'])->id !== $export->id) { throw new RuntimeException('Restored archive, original or replay differs.'); } $zip = new ZipArchive; if ($zip->open($path, ZipArchive::RDONLY) !== true) { throw new RuntimeException('Restored archive cannot be read.'); } try { if (hash('sha256', $zip->getFromName('originais/purchasing/supplier_documents/'.$document->id.'.png')) !== $fixture['source_sha256']) { throw new RuntimeException('Original inside restored archive differs.'); } } finally { $zip->close(); } DB::beginTransaction(); try { DB::table('organization_memberships')->where('user_id', $actor->id)->update(['expires_at' => now()->subMinute()]); try { $service->verifiedPath($actor, $export); throw new RuntimeException('Restored archive ignored current revoked authority.'); } catch (HttpExceptionInterface $error) { if ($error->getStatusCode() !== 403) { throw $error; } } } finally { DB::rollBack(); } if ($snapshot() !== $fixture['snapshot']) { throw new RuntimeException('Audit recovery verification changed restored facts.'); } echo json_encode(['tables_equal' => count($fixture['snapshot']), 'archive_bytes_and_hash_equal' => true, 'original_file_and_archived_copy_equal' => true, 'replay_without_effects' => true, 'current_revocation_refused' => true, 'sql_and_separate_private_files_restored' => true, 'source_file_tree_absent_during_verification' => ! is_dir($root.'/audit-source-files'), 'operational_changes' => false], JSON_THROW_ON_ERROR).PHP_EOL;