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/CloudAdminAbuseMonitoringSe...

384 lines
13 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminAbuseMonitoringService;
use App\Services\Cloud\CloudError;
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('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region');
$table->boolean('enabled');
});
$schema->create('cloud_network_lines',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$schema->create('cloud_resource_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('uuid');
$table->integer('host_id');
$table->integer('user_id');
$table->integer('resource_pool_id')->nullable();
$table->string('name');
$table->string('status');
});
$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');
});
$schema->create('cloud_network_monitor_hosts',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->string('collection_state');
$table->integer('last_success_at')->nullable();
});
$db = $capsule->connection('default');
$now = 2_000_000;
$actor = ['user_id' => 9, 'is_admin' => true];
$db->table('user')->insert([
['id' => 11, 'user_name' => 'alpha'],
['id' => 12, 'user_name' => 'beta'],
]);
$db->table('cloud_hosts')->insert([
['id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong', 'enabled' => 1],
['id' => 2, 'name' => 'HKG-B', 'region' => 'HongKong', 'enabled' => 1],
]);
$db->table('cloud_network_lines')->insert([
'id' => 3, 'name' => 'International',
]);
$db->table('cloud_resource_pools')->insert(['id' => 4, 'line_id' => 3]);
$uuid = static fn (int $id): string => sprintf(
'123e4567-e89b-42d3-a456-%012d',
$id
);
foreach ([
[1, 11, 1, 'normal-vm'],
[2, 12, 1, 'high-vm'],
[3, 11, 2, 'stale-vm'],
[4, 11, 1, 'broken-vm'],
] as [$id, $user, $host, $name]) {
$db->table('cloud_instances')->insert([
'id' => $id,
'uuid' => $uuid($id),
'host_id' => $host,
'user_id' => $user,
'resource_pool_id' => 4,
'name' => $name,
'status' => 'active',
]);
}
$flow = array_fill_keys([
'tcp_active_flows', 'tcp_new_flows', 'tcp_syn_flows',
'tcp_unreplied_flows', 'udp_active_flows', 'udp_new_flows',
'other_active_flows', 'unique_remote_ips',
'unique_remote_ports', 'unique_local_ports',
'max_remote_ports_per_remote_ip',
'max_remote_ips_per_remote_port',
'max_local_ports_per_remote_ip',
'max_remote_ips_per_local_port',
], 0);
$metrics = static function (int $counter, int $flows = 0) use ($flow): array {
$outbound = $flow;
$outbound['tcp_active_flows'] = $flows;
$outbound['tcp_new_flows'] = $flows;
return [
'interface' => [
'rx_bytes' => $counter,
'tx_bytes' => $counter * 2,
'rx_packets' => intdiv($counter, 10),
'tx_packets' => intdiv($counter, 5),
'rx_errors' => 0,
'tx_errors' => 0,
'rx_drops' => 0,
'tx_drops' => 0,
],
'ipv4' => ['inbound' => $flow, 'outbound' => $outbound],
'ipv6' => ['inbound' => $flow, 'outbound' => $flow],
];
};
$latest = static function (array $metrics, array $rates): string {
return json_encode([
'rates' => $rates,
'sample_count' => 80,
'warmup_remaining' => 0,
'quality_flags' => [],
'metrics' => $metrics,
], JSON_THROW_ON_ERROR);
};
$day = static function (int $dayStart, int $peak = 10): array {
return [
'sample_count' => 10,
'complete_count' => 10,
'quality_mask' => 0,
'rx_bps_peak' => $peak,
'tx_bps_peak' => $peak * 2,
'rx_pps_peak' => $peak,
'tx_pps_peak' => $peak * 2,
'tcp_active_peak' => $peak,
'tcp_new_peak' => $peak,
'tcp_syn_peak' => 0,
'tcp_unreplied_peak' => 0,
'udp_active_peak' => 0,
'udp_new_peak' => 0,
'peer_ip_peak' => 2,
'peer_port_peak' => 3,
'first_sample_at' => $dayStart,
'last_sample_at' => $dayStart + 540,
];
};
$rate = [
'rx_bps' => 800,
'tx_bps' => 1600,
'rx_pps' => 10,
'tx_pps' => 20,
];
$currentDay = intdiv($now, 86400) * 86400;
$insertLedger = static function (
int $id,
string $risk,
string $collection,
string $summary,
int $sampleAt,
string $direction = 'none',
string $rule = '',
string $severity = 'none'
) use ($db, $now, $currentDay, $day): void {
$db->table('cloud_network_instance_ledger')->insert([
'instance_id' => $id,
'policy_revision' => 1,
'risk_state' => $risk,
'collection_state' => $collection,
'latest_direction' => $direction,
'latest_rule_code' => $rule,
'latest_severity' => $severity,
'detector_state_json' => '{}',
'latest_metrics_json' => $summary,
'current_day_start' => $currentDay,
'current_day_json' => json_encode(
$day($currentDay),
JSON_THROW_ON_ERROR
),
'last_bucket_start' => $sampleAt - 60,
'last_sample_at' => $sampleAt,
'last_evaluated_at' => $sampleAt,
'created_at' => $now - 1000,
'updated_at' => $sampleAt,
]);
};
$insertLedger(1, 'normal', 'healthy',
$latest($metrics(1000), $rate), $now - 1);
$insertLedger(2, 'high_risk', 'healthy',
$latest($metrics(2000, 6000), $rate), $now - 2,
'outbound', 'outbound_tcp_syn_sweep', 'high');
$insertLedger(3, 'high_risk', 'healthy',
$latest($metrics(3000), $rate), $now - 540,
'inbound', 'inbound_tcp_syn_attack', 'high');
$insertLedger(4, 'normal', 'healthy', '{broken', $now - 1);
$db->table('cloud_network_findings')->insert([
'id' => 1,
'instance_id' => 2,
'policy_revision' => 1,
'rule_code' => 'outbound_tcp_syn_sweep',
'direction' => 'outbound',
'ip_family' => 'ipv6',
'severity' => 'high',
'state' => 'open',
'first_seen_at' => $now - 120,
'last_seen_at' => $now - 60,
'consecutive_windows' => 2,
'occurrence_generation' => 1,
'transition_revision' => 1,
'evidence_json' => json_encode([
'tcp_syn_flows' => 6000,
'unique_remote_ips' => 700,
], JSON_THROW_ON_ERROR),
'resolved_at' => null,
'resolution_reason' => '',
'created_at' => $now - 120,
'updated_at' => $now - 60,
]);
for ($index = 3; $index >= 1; --$index) {
$start = $currentDay - ($index * 86400);
$db->table('cloud_network_daily_summaries')->insert(
['instance_id' => 2, 'day_start' => $start]
+ $day($start, $index * 100)
+ ['created_at' => $now, 'updated_at' => $now]
);
}
$db->table('cloud_network_monitor_hosts')->insert([
'host_id' => 1,
'collection_state' => 'healthy',
'last_success_at' => $now - 1,
]);
$heartbeat = static fn (): array => [[
'key' => 'abuse_monitor',
'status' => 'ok',
'last_success_at' => $now - 1,
]];
$service = new CloudAdminAbuseMonitoringService(
static fn (): int => $now,
$heartbeat
);
$page = $service->page($actor);
$assert($page['summary']['normal'] === 1
&& $page['summary']['high_risk'] === 1
&& $page['summary']['collection_anomaly'] === 2
&& $page['summary']['total'] === 4,
'Risk summary diverged from stale/corrupt list boundaries.');
$assert($page['health']['worker_state'] === 'degraded'
&& $page['health']['error_code'] === 'partial_collection_failure',
'Missing enabled-host coverage was projected healthy.');
$broken = array_values(array_filter(
$page['items'],
static fn (array $row): bool => $row['instance_id'] === '4'
))[0] ?? null;
$assert($broken !== null
&& $broken['risk_state'] === 'collection_anomaly'
&& $broken['metrics']['rx_bps'] === null,
'Corrupt latest metrics became healthy zero data.');
$high = $service->page($actor, ['risk' => 'high_risk']);
$ipv6 = $service->page($actor, ['ip_version' => 'ipv6']);
$search = $service->page($actor, ['query' => 'alpha']);
$assert($high['pagination']['total'] === 1
&& $high['items'][0]['instance_id'] === '2'
&& $ipv6['pagination']['total'] === 1
&& $ipv6['items'][0]['instance_id'] === '2'
&& $search['pagination']['total'] === 3,
'Risk, family or owner filters drifted.');
$detail = $service->detail($actor, 2);
$encoded = json_encode($detail, JSON_THROW_ON_ERROR);
$assert(count($detail['series']) === 4
&& $detail['series'][0]['rx_bps'] === 300
&& $detail['series'][3]['rx_bps'] === 10
&& count($detail['findings']) === 1
&& str_contains(
$detail['findings'][0]['evidence_summary'],
'TCP SYN Flow 6,000'
)
&& !str_contains($encoded, 'evidence_json')
&& !str_contains($encoded, 'current_day_json')
&& !str_contains($encoded, 'remote_addresses'),
'Detail response lost daily trend bounds or leaked raw evidence.');
$noHeartbeat = new CloudAdminAbuseMonitoringService(
static fn (): int => $now,
static fn (): array => []
);
$unavailable = $noHeartbeat->page($actor);
$assert($unavailable['health']['worker_state'] === 'unavailable'
&& $unavailable['health']['error_code'] === 'monitor_worker_unavailable',
'Historical host state invented a healthy worker.');
foreach ([
['actor' => ['user_id' => 1, 'is_admin' => false], 'input' => []],
['actor' => $actor, 'input' => ['risk' => 'invented']],
['actor' => $actor, 'input' => ['unknown' => '1']],
['actor' => $actor, 'input' => ['per_page' => '101']],
] as $case) {
try {
$service->page($case['actor'], $case['input']);
throw new RuntimeException('Invalid admin request was accepted.');
} catch (CloudError $error) {
$assert(in_array($error->httpStatus, [400, 403], true),
'Invalid request returned an unexpected error.');
}
}
$source = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudAdminAbuseMonitoringService.php');
$assert(is_string($source)
&& !str_contains($source, 'cloud_abuse_instance_states')
&& !str_contains($source, 'cloud_abuse_findings')
&& !str_contains($source, 'cloud_vm_network_')
&& !str_contains($source, "connection('log')")
&& !str_contains($source, 'cloud_jobs')
&& !str_contains($source, 'service_status')
&& !str_contains($source, 'network_status')
&& !preg_match('/->(?:insert|update|delete|lockForUpdate)\s*\(/', $source),
'Administrator monitor retained a legacy, mutation or enforcement path.');
echo "Administrator compact abuse-monitoring tests passed.\n";