addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('user_id'); $table->string('uuid'); }); $schema->create('cloud_network_instance_ledger', static function (Blueprint $table): void { $table->integer('instance_id')->primary(); $table->integer('policy_revision'); $table->string('risk_state'); $table->string('collection_state'); $table->string('latest_direction'); $table->string('latest_rule_code'); $table->string('latest_severity'); $table->text('detector_state_json'); $table->text('latest_metrics_json'); $table->integer('current_day_start'); $table->text('current_day_json'); $table->integer('last_bucket_start')->nullable(); $table->integer('last_sample_at')->nullable(); $table->integer('last_evaluated_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_network_daily_summaries', static function (Blueprint $table): void { $table->integer('instance_id'); $table->integer('day_start'); foreach (['sample_count', 'complete_count', 'quality_mask', 'rx_bps_peak', 'tx_bps_peak', 'rx_pps_peak', 'tx_pps_peak', 'tcp_active_peak', 'tcp_new_peak', 'tcp_syn_peak', 'tcp_unreplied_peak', 'udp_active_peak', 'udp_new_peak', 'peer_ip_peak', 'peer_port_peak', 'first_sample_at', 'last_sample_at', 'created_at', 'updated_at'] as $column) { $table->integer($column); } $table->primary(['instance_id', 'day_start']); }); $schema->create('cloud_network_findings', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('policy_revision'); $table->string('rule_code'); $table->string('direction'); $table->string('ip_family'); $table->string('severity'); $table->string('state'); $table->integer('first_seen_at'); $table->integer('last_seen_at'); $table->integer('consecutive_windows'); $table->integer('occurrence_generation'); $table->integer('transition_revision'); $table->text('evidence_json'); $table->integer('resolved_at')->nullable(); $table->string('resolution_reason'); $table->integer('created_at'); $table->integer('updated_at'); $table->unique([ 'instance_id', 'policy_revision', 'rule_code', 'direction', 'ip_family', ]); }); $schema->create('cloud_network_alert_events', static function (Blueprint $table): void { $table->increments('id'); $table->integer('finding_id'); $table->integer('transition_revision'); $table->string('transition_type'); $table->string('from_severity'); $table->string('to_severity'); $table->text('evidence_json'); $table->string('resolution_reason'); $table->integer('occurred_at'); $table->integer('created_at'); $table->unique(['finding_id', 'transition_revision']); }); $db = $capsule->connection('default'); $db->table('cloud_instances')->insert([ 'id' => 1, 'host_id' => 7, 'user_id' => 44, 'uuid' => '123e4567-e89b-42d3-a456-426614174000', ]); $definition = CloudAbuseDetectionPolicy::defaults()->definition(); $definition['windows'] = [ 'warmup' => 1, 'suspicious' => 1, 'high' => 1, 'clear' => 1, ]; foreach ($definition['rules'] as $code => &$rule) { $rule['enabled'] = $code === 'outbound_tcp_syn_sweep'; } unset($rule); $policy = CloudAbuseDetectionPolicy::fromDefinition($definition); $snapshot = new CloudAbusePolicySnapshot( 1, $policy->hash(), $policy, null, 0 ); $flow = static fn (array $overrides = []): array => array_merge([ 'tcp_active_flows' => 1, 'tcp_new_flows' => 0, 'tcp_syn_flows' => 0, 'tcp_unreplied_flows' => 0, 'udp_active_flows' => 0, 'udp_new_flows' => 0, 'other_active_flows' => 0, 'unique_remote_ips' => 1, 'unique_remote_ports' => 1, 'unique_local_ports' => 1, 'max_remote_ports_per_remote_ip' => 1, 'max_remote_ips_per_remote_port' => 1, 'max_local_ports_per_remote_ip' => 1, 'max_remote_ips_per_local_port' => 1, ], $overrides); $sample = static function ( int $bucket, array $outbound = [], bool $complete = true, array $quality = [] ) use ($flow): array { return [ 'bucket_start' => $bucket, 'complete' => $complete, 'quality_flags' => $quality, 'metrics' => [ 'interface' => [ 'rx_bytes' => $bucket * 10, 'tx_bytes' => $bucket * 20, 'rx_packets' => $bucket, 'tx_packets' => $bucket * 2, 'rx_errors' => 0, 'tx_errors' => 0, 'rx_drops' => 0, 'tx_drops' => 0, ], 'ipv4' => [ 'inbound' => $flow(), 'outbound' => $flow($outbound), ], 'ipv6' => null, ], ]; }; $attack = [ 'tcp_active_flows' => 5000, 'tcp_new_flows' => 5000, 'tcp_syn_flows' => 5000, 'tcp_unreplied_flows' => 5000, 'unique_remote_ips' => 700, ]; $ledger = new CloudNetworkAbuseLedger(); $first = strtotime('2026-11-08 23:58:00 UTC'); $normal = $db->transaction(static fn (): array => $ledger->apply( $db, 1, $sample($first), $snapshot, 1000 )); $assert($normal === ['state' => 'applied', 'transitions' => 0], 'First minute did not initialize the compact ledger.'); $opened = $db->transaction(static fn (): array => $ledger->apply( $db, 1, $sample($first + 60, $attack), $snapshot, 1060 )); $state = $db->table('cloud_network_instance_ledger')->first(); $day = json_decode((string) $state->current_day_json, true, 32, JSON_THROW_ON_ERROR); $assert($opened === ['state' => 'applied', 'transitions' => 1] && $state->risk_state === 'high_risk' && $state->latest_direction === 'outbound' && (int) $state->last_bucket_start === $first + 60 && $day['sample_count'] === 2 && $day['complete_count'] === 2 && $day['tcp_syn_peak'] === 5000 && $db->table('cloud_network_findings')->count() === 1 && $db->table('cloud_network_alert_events')->count() === 1, 'High-risk state, daily aggregate and sparse event did not commit.'); $replay = $db->transaction(static fn (): array => $ledger->apply( $db, 1, $sample($first + 60, ['tcp_syn_flows' => 999999]), $snapshot, 1061 )); $assert($replay === ['state' => 'ignored', 'transitions' => 0] && $db->table('cloud_network_alert_events')->count() === 1, 'A replayed minute mutated the compact ledger.'); $nextDay = $first + 120; $resolved = $db->transaction(static fn (): array => $ledger->apply( $db, 1, $sample($nextDay), $snapshot, 1120 )); $daily = $db->table('cloud_network_daily_summaries')->first(); $finding = $db->table('cloud_network_findings')->first(); $state = $db->table('cloud_network_instance_ledger')->first(); $currentDay = json_decode((string) $state->current_day_json, true, 32, JSON_THROW_ON_ERROR); $assert($resolved === ['state' => 'applied', 'transitions' => 1] && $daily !== null && (int) $daily->day_start === intdiv($first, 86400) * 86400 && (int) $daily->sample_count === 2 && (int) $daily->tcp_syn_peak === 5000 && $currentDay['sample_count'] === 1 && $finding->state === 'resolved' && $finding->resolution_reason === 'evidence_clear' && $db->table('cloud_network_alert_events')->count() === 2, 'UTC-day rollup or evidence-clear transition was not durable.'); $incomplete = $db->transaction(static fn (): array => $ledger->apply( $db, 1, $sample($nextDay + 60, [], false, ['incomplete']), $snapshot, 1180 )); $state = $db->table('cloud_network_instance_ledger')->first(); $currentDay = json_decode((string) $state->current_day_json, true, 32, JSON_THROW_ON_ERROR); $assert($incomplete['state'] === 'applied' && $state->collection_state === 'degraded' && $state->risk_state === 'normal' && $currentDay['sample_count'] === 2 && $currentDay['complete_count'] === 1, 'Incomplete evidence changed risk or entered trusted daily peaks.'); $beforeRollback = (int) $state->last_bucket_start; try { $db->transaction(static function () use ( $ledger, $db, $sample, $nextDay, $snapshot ): void { $ledger->apply( $db, 1, $sample($nextDay + 120), $snapshot, 1240 ); throw new RuntimeException('rollback probe'); }); throw new RuntimeException('Rollback probe did not throw.'); } catch (RuntimeException $error) { $assert($error->getMessage() === 'rollback probe', 'Rollback probe failed for an unrelated reason.'); } $assert((int) $db->table('cloud_network_instance_ledger') ->value('last_bucket_start') === $beforeRollback, 'A failed surrounding transaction leaked a ledger update.'); $source = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudNetworkAbuseLedger.php'); $assert(is_string($source) && !str_contains($source, 'vm_uuid') && !str_contains($source, 'sample_key') && !str_contains($source, 'placement_key') && !str_contains($source, 'window_id') && !str_contains($source, "connection('log')") && !str_contains($source, 'cloud_jobs') && !str_contains($source, 'network_status') && !str_contains($source, 'service_status'), 'Compact ledger gained a transport identity, log DB or enforcement side effect.'); echo "Compact network abuse ledger tests passed.\n";