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/CloudNetworkAbuseScaleTest.php

352 lines
12 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAbuseDetectionPolicy;
use App\Services\Cloud\CloudAbusePolicySnapshot;
use App\Services\Cloud\CloudNetworkAbuseLedger;
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');
});
$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');
$instanceCount = 1000;
$instanceIds = range(1, $instanceCount);
foreach (array_chunk($instanceIds, 200) as $chunk) {
$rows = [];
foreach ($chunk as $instanceId) {
$rows[] = [
'id' => $instanceId,
'host_id' => 1 + (($instanceId - 1) % 10),
'user_id' => 1 + (($instanceId - 1) % 250),
'uuid' => sprintf(
'00000000-0000-4000-8000-%012d',
$instanceId
),
];
}
$db->table('cloud_instances')->insert($rows);
}
$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 $bucketStart,
int $counter,
array $outbound = []
) use ($flow): array {
return [
'bucket_start' => $bucketStart,
'complete' => true,
'quality_flags' => [],
'metrics' => [
'interface' => [
'rx_bytes' => $counter * 1000,
'tx_bytes' => $counter * 2000,
'rx_packets' => $counter * 10,
'tx_packets' => $counter * 20,
'rx_errors' => 0,
'tx_errors' => 0,
'rx_drops' => 0,
'tx_drops' => 0,
],
'ipv4' => [
'inbound' => $flow(),
'outbound' => $flow($outbound),
],
'ipv6' => null,
],
];
};
$ledger = new CloudNetworkAbuseLedger();
$apply = static function (
array $ids,
int $bucketStart,
int $counter,
array $outbound = []
) use ($db, $ledger, $sample, $snapshot): array {
return $db->transaction(static function () use (
$ids,
$bucketStart,
$counter,
$outbound,
$db,
$ledger,
$sample,
$snapshot
): array {
$states = ['applied' => 0, 'ignored' => 0];
foreach ($ids as $instanceId) {
$result = $ledger->apply(
$db,
$instanceId,
$sample($bucketStart, $counter, $outbound),
$snapshot,
$bucketStart + 60
);
++$states[$result['state']];
}
return $states;
});
};
$dayStart = strtotime('2026-11-08 00:00:00 UTC');
$assert($apply($instanceIds, $dayStart, 1)
=== ['applied' => 1000, 'ignored' => 0]
&& $apply($instanceIds, $dayStart + 60, 2)
=== ['applied' => 1000, 'ignored' => 0],
'The 1000-instance ledger did not accept normal minute windows.');
$replayedAttack = [
'tcp_active_flows' => 5000,
'tcp_new_flows' => 5000,
'tcp_syn_flows' => 5000,
'tcp_unreplied_flows' => 5000,
'unique_remote_ips' => 700,
];
$assert($apply(
$instanceIds,
$dayStart + 60,
999999,
$replayedAttack
) === ['applied' => 0, 'ignored' => 1000]
&& $db->table('cloud_network_findings')->count() === 0,
'Replayed windows changed a ledger or created sparse findings.');
// Exercise all 1,440 minute updates for one busy instance. The other 999
// instances still prove that cardinality scales with instances, not windows.
$db->transaction(static function () use (
$dayStart,
$db,
$ledger,
$sample,
$snapshot
): void {
for ($minute = 2; $minute < 1440; ++$minute) {
$result = $ledger->apply(
$db,
1,
$sample($dayStart + ($minute * 60), $minute + 1),
$snapshot,
$dayStart + (($minute + 1) * 60)
);
if ($result['state'] !== 'applied') {
throw new RuntimeException('Busy-instance minute was not applied.');
}
}
});
$nextDay = $dayStart + 86400;
$assert($apply($instanceIds, $nextDay, 1441)
=== ['applied' => 1000, 'ignored' => 0]
&& $db->table('cloud_network_instance_ledger')->count() === 1000
&& $db->table('cloud_network_daily_summaries')->count() === 1000
&& (int) $db->table('cloud_network_daily_summaries')
->where('instance_id', 1)->value('sample_count') === 1440
&& (int) $db->table('cloud_network_daily_summaries')
->where('instance_id', 1000)->value('sample_count') === 2
&& (int) $db->table('cloud_network_daily_summaries')
->sum('sample_count') === 3438,
'Minute input expanded beyond one ledger row and one instance/day row.');
$alertedIds = range(1, 10);
$assert($apply(
$alertedIds,
$nextDay + 60,
1442,
$replayedAttack
) === ['applied' => 10, 'ignored' => 0]
&& $apply(
$alertedIds,
$nextDay + 120,
1443,
$replayedAttack
) === ['applied' => 10, 'ignored' => 0]
&& $apply($alertedIds, $nextDay + 180, 1444)
=== ['applied' => 10, 'ignored' => 0]
&& $db->table('cloud_network_findings')->count() === 10
&& $db->table('cloud_network_findings')
->where('state', 'resolved')->count() === 10
&& $db->table('cloud_network_alert_events')->count() === 20,
'Sparse finding transitions grew per minute instead of per state change.');
$beforeRollback = $db->table('cloud_network_instance_ledger')
->whereIn('instance_id', range(11, 60))->orderBy('instance_id')
->pluck('last_bucket_start')->map('intval')->all();
try {
$db->transaction(static function () use (
$nextDay,
$db,
$ledger,
$sample,
$snapshot
): void {
foreach (range(11, 60) as $instanceId) {
$ledger->apply(
$db,
$instanceId,
$sample($nextDay + 240, 1445),
$snapshot,
$nextDay + 300
);
}
throw new RuntimeException('scale rollback probe');
});
throw new RuntimeException('Scale rollback probe did not throw.');
} catch (RuntimeException $error) {
$assert($error->getMessage() === 'scale rollback probe',
'Scale rollback probe failed for an unrelated reason.');
}
$afterRollback = $db->table('cloud_network_instance_ledger')
->whereIn('instance_id', range(11, 60))->orderBy('instance_id')
->pluck('last_bucket_start')->map('intval')->all();
$assert($afterRollback === $beforeRollback
&& $db->table('cloud_network_instance_ledger')->count() === 1000
&& $db->table('cloud_network_daily_summaries')->count() === 1000
&& $db->table('cloud_network_findings')->count() === 10
&& $db->table('cloud_network_alert_events')->count() === 20,
'A failed multi-instance transaction leaked partial compact-ledger rows.');
$firstCurrentDay = json_decode(
(string) $db->table('cloud_network_instance_ledger')
->where('instance_id', 1)->value('current_day_json'),
true,
32,
JSON_THROW_ON_ERROR
);
$ordinaryCurrentDay = json_decode(
(string) $db->table('cloud_network_instance_ledger')
->where('instance_id', 1000)->value('current_day_json'),
true,
32,
JSON_THROW_ON_ERROR
);
$assert($firstCurrentDay['sample_count'] === 4
&& $ordinaryCurrentDay['sample_count'] === 1,
'Current UTC-day aggregates are not bounded inside instance ledgers.');
echo "Compact network abuse 1000-instance scale checks passed.\n";