You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudAbuseObservationIngest...

400 lines
14 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Services\Cloud\CloudAbuseDetectionPolicy;
use App\Services\Cloud\CloudAbuseObservationIngestService;
use App\Services\Cloud\CloudAbusePolicySnapshot;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->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');
$table->string('name')->default('vm');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_network_monitor_hosts',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->string('collector_generation')->nullable();
$table->integer('cursor_value')->default(0);
$table->integer('oldest_cursor')->default(0);
$table->integer('gap_before')->default(0);
$table->integer('poll_revision')->default(0);
$table->integer('next_poll_at')->default(0);
$table->string('poll_lease_token')->nullable();
$table->integer('poll_lease_until')->default(0);
$table->string('collection_state');
$table->integer('consecutive_failures')->default(0);
$table->integer('last_window_start')->nullable();
$table->integer('last_success_at')->nullable();
$table->string('last_error_code')->default('');
$table->integer('updated_at');
});
$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']);
});
$instance = new CloudInstance();
$instance->host_id = 7;
$instance->user_id = 44;
$instance->uuid = '123e4567-e89b-42d3-a456-426614174000';
$instance->name = 'abuse-test';
$instance->created_at = $instance->updated_at = 100;
$instance->save();
$flow = static fn (): array => [
'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,
];
$batch = static function (
string $generation,
int $cursor,
string $uuid,
string $bucket,
bool $gap = false
) use ($flow): array {
return [
'contract_version' => 1,
'collector_generation' => $generation,
'oldest_cursor' => $cursor,
'gap_before' => $gap,
'next_cursor' => $cursor,
'has_more' => false,
'capability' => ['available' => true],
'windows' => [[
'cursor' => $cursor,
'window_id' => str_repeat('a', 32),
'vm_uuid' => $uuid,
'bucket_start' => $bucket,
'bucket_seconds' => 60,
'complete' => true,
'quality_flags' => [],
'interface' => [
'rx_bytes' => $cursor * 100,
'tx_bytes' => $cursor * 200,
'rx_packets' => $cursor * 10,
'tx_packets' => $cursor * 20,
'rx_errors' => 0,
'tx_errors' => 0,
'rx_drops' => 0,
'tx_drops' => 0,
],
'ipv4' => [
'inbound' => $flow(),
'outbound' => $flow(),
],
'ipv6' => null,
]],
];
};
$empty = static fn (string $generation, int $oldest = 0): array => [
'contract_version' => 1,
'collector_generation' => $generation,
'oldest_cursor' => $oldest,
'gap_before' => false,
'next_cursor' => 0,
'has_more' => false,
'capability' => ['available' => true],
'windows' => [],
];
$client = new class() {
public array $responses = [];
public array $calls = [];
public function networkObservations(
array $actor,
?string $generation,
int $after,
int $limit
): array {
$this->calls[] = [$actor, $generation, $after, $limit];
$response = array_shift($this->responses);
if ($response instanceof Throwable) {
throw $response;
}
return $response;
}
};
$now = 1000;
$policy = CloudAbuseDetectionPolicy::defaults();
$snapshot = new CloudAbusePolicySnapshot(
1,
$policy->hash(),
$policy,
null,
0
);
$worker = new CloudAbuseObservationIngestService(
static fn (): array => [7],
static fn (int $hostId): object => $client,
static function () use (&$now): int { return $now; },
static fn (): CloudAbusePolicySnapshot => $snapshot
);
$db = $capsule->connection('default');
$generation = '0123456789abcdef0123456789abcdef';
$client->responses[] = $batch(
$generation,
1,
$instance->uuid,
'2026-11-08T01:02:00Z'
);
$assert($worker->tick(), 'Due host was not polled.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$ledger = $db->table('cloud_network_instance_ledger')->first();
$assert($cursor !== null && (int) $cursor->cursor_value === 1
&& $cursor->collection_state === 'healthy'
&& $ledger !== null && (int) $ledger->instance_id === $instance->id
&& (int) $ledger->policy_revision === 1
&& (int) $ledger->last_bucket_start
=== strtotime('2026-11-08 01:02:00 UTC'),
'Instance ledger and host cursor did not commit together.');
$assert(($client->calls[0][0]['is_super_admin'] ?? false) === true
&& $client->calls[0][1] === null
&& $client->calls[0][2] === 0,
'Collector did not use the signed inspection actor and cursor.');
$now += 15;
$client->responses[] = $empty($generation, 1);
$assert($worker->tick(), 'Empty same-generation poll did not run.');
$assert((int) $db->table('cloud_network_monitor_hosts')
->value('cursor_value') === 1,
'An empty same-generation page reset the durable cursor.');
$now += 15;
$gap = $batch(
$generation,
3,
$instance->uuid,
'2026-11-08T01:04:00Z',
true
);
$gap['has_more'] = true;
$client->responses[] = $gap;
$assert($worker->tick(), 'Retained-ring gap poll did not run.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$latest = json_decode((string) $db
->table('cloud_network_instance_ledger')->value('latest_metrics_json'),
true, 32, JSON_THROW_ON_ERROR);
$assert((int) $cursor->cursor_value === 3
&& (int) $cursor->gap_before === 1
&& $cursor->collection_state === 'degraded'
&& in_array('panel_ring_gap', $latest['quality_flags'], true)
&& in_array('incomplete', $latest['quality_flags'], true),
'Ring-gap evidence remained eligible or was not carried forward.');
$continued = $batch(
$generation,
4,
$instance->uuid,
'2026-11-08T01:05:00Z'
);
$continued['oldest_cursor'] = 3;
$continued['has_more'] = true;
$client->responses[] = $continued;
$assert($worker->tick(), 'Pending ring gap did not continue immediately.');
$latest = json_decode((string) $db
->table('cloud_network_instance_ledger')->value('latest_metrics_json'),
true, 32, JSON_THROW_ON_ERROR);
$assert(in_array('panel_ring_gap', $latest['quality_flags'], true)
&& (int) $db->table('cloud_network_monitor_hosts')
->value('gap_before') === 1,
'A ring gap was not propagated across retained pages.');
$resetGeneration = 'fedcba9876543210fedcba9876543210';
$reset = $batch(
$resetGeneration,
1,
$instance->uuid,
'2026-11-08T01:06:00Z'
);
$client->responses[] = $reset;
$assert($worker->tick(), 'Generation reset did not settle.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$assert($cursor->collector_generation === $resetGeneration
&& (int) $cursor->cursor_value === 1
&& (int) $cursor->gap_before === 0
&& $cursor->collection_state === 'healthy',
'Old-generation state contaminated the replacement generation.');
$now += 15;
$missing = $batch(
$resetGeneration,
2,
'123e4567-e89b-42d3-a456-426614174099',
'2026-11-08T01:07:00Z'
);
$client->responses[] = $missing;
$assert($worker->tick(), 'Unknown placement poll did not settle.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$assert((int) $cursor->cursor_value === 2
&& $cursor->collection_state === 'degraded'
&& $cursor->last_error_code === 'instance_mapping_missing'
&& $db->table('cloud_network_instance_ledger')->count() === 1,
'Unknown VM was guessed, persisted or blocked the host cursor.');
$now += 15;
$client->responses[] = new RuntimeException('temporary transport failure');
$assert($worker->tick(), 'Failed poll did not consume its bounded attempt.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$assert($cursor->poll_lease_token === null
&& (int) $cursor->poll_lease_until === 0
&& (int) $cursor->consecutive_failures === 1
&& $cursor->collection_state === 'degraded'
&& $worker->observationErrors()
=== ['network_observation_collection_failed'],
'Transient failure left a permanent host lease.');
$now += 15;
$blocked = $batch(
$resetGeneration,
3,
$instance->uuid,
'2026-11-08T01:08:00Z'
);
$beforeBucket = (int) $db->table('cloud_network_instance_ledger')
->value('last_bucket_start');
$db->unprepared(
'CREATE TRIGGER reject_network_ledger_update '
. 'BEFORE UPDATE ON cloud_network_instance_ledger '
. 'BEGIN SELECT RAISE(FAIL, "ledger failure"); END'
);
$client->responses[] = $blocked;
$assert($worker->tick(), 'Atomic ledger failure was not exercised.');
$db->unprepared('DROP TRIGGER reject_network_ledger_update');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$assert((int) $cursor->cursor_value === 2
&& (int) $cursor->consecutive_failures === 2
&& (int) $db->table('cloud_network_instance_ledger')
->value('last_bucket_start') === $beforeBucket,
'A failed ledger update advanced the cursor or leaked state.');
$now += 30;
$client->responses[] = $blocked;
$assert($worker->tick(), 'Exact failed page did not recover.');
$cursor = $db->table('cloud_network_monitor_hosts')->first();
$assert((int) $cursor->cursor_value === 3
&& (int) $cursor->consecutive_failures === 0
&& $cursor->collection_state === 'healthy'
&& (int) $db->table('cloud_network_instance_ledger')
->value('last_bucket_start')
=== strtotime('2026-11-08 01:08:00 UTC'),
'Atomic retry did not apply exactly once and advance the cursor.');
$source = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudAbuseObservationIngestService.php');
$assert(is_string($source)
&& !str_contains($source, "connection('log')")
&& !str_contains($source, 'cloud_vm_network_observation_samples')
&& !str_contains($source, 'cloud_vm_network_observation_rollups')
&& !str_contains($source, 'cloud_vm_network_projection_outbox')
&& !str_contains($source, 'sample_key')
&& !str_contains($source, 'placement_key')
&& !str_contains($source, 'cloud_jobs')
&& !str_contains($source, 'service_status')
&& !str_contains($source, 'network_status'),
'Direct collector retained an old pipeline or enforcement dependency.');
echo "Direct network abuse collection tests passed.\n";