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

554 lines
21 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->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'log');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$mainSchema = $capsule->schema('default');
$mainSchema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name');
});
$mainSchema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region');
$table->boolean('enabled');
});
$mainSchema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$mainSchema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
});
$mainSchema->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');
});
$mainSchema->create('cloud_abuse_instance_states',
static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('host_id');
$table->integer('rule_version')->default(1);
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$table->string('risk_state');
$table->string('collection_state');
$table->string('latest_direction');
$table->string('latest_rule_code');
$table->string('latest_severity');
$table->text('summary_json');
$table->integer('last_bucket_start')->nullable();
$table->integer('last_sample_at')->nullable();
$table->integer('last_evaluated_at')->nullable();
$table->integer('updated_at');
});
$mainSchema->create('cloud_abuse_findings',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('finding_key');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('host_id');
$table->string('rule_code');
$table->integer('rule_version');
$table->integer('policy_revision')->default(1);
$table->string('policy_hash')->default(
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
);
$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->text('evidence_json');
$table->integer('resolved_at')->nullable();
$table->string('resolution_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
$logSchema = $capsule->schema('log');
$logSchema->create('cloud_vm_network_observation_samples',
static function (Blueprint $table): void {
$table->string('sample_key')->primary();
$table->integer('instance_id');
$table->integer('bucket_start');
$table->boolean('complete');
$table->text('metrics_json');
$table->text('quality_flags_json');
});
$logSchema->create('cloud_vm_network_monitor_cursors',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->string('collection_state');
$table->integer('last_success_at')->nullable();
});
$main = $capsule->connection('default');
$log = $capsule->connection('log');
$now = 2_000_000;
$actor = ['user_id' => 9, 'is_admin' => true];
$defaultPolicyHash =
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae';
$findingKey = static function (
int $instanceId,
string $ruleCode,
string $family,
string $direction
) use ($defaultPolicyHash): string {
return hash('sha256', implode("\n", [
$instanceId,
1,
$defaultPolicyHash,
$ruleCode,
$family,
$direction,
]));
};
$main->table('user')->insert([
['id' => 11, 'user_name' => 'alpha'],
['id' => 12, 'user_name' => 'beta'],
]);
$main->table('cloud_hosts')->insert([
['id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong', 'enabled' => 1],
['id' => 2, 'name' => 'HKG-B', 'region' => 'HongKong', 'enabled' => 1],
]);
$main->table('cloud_network_lines')->insert([
['id' => 3, 'name' => 'International'],
]);
$main->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]) {
$main->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],
];
};
$summary = 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);
};
$rate = ['rx_bps' => 800, 'tx_bps' => 1600,
'rx_pps' => 10, 'tx_pps' => 20];
$main->table('cloud_abuse_instance_states')->insert([
['instance_id' => 1, 'user_id' => 11, 'host_id' => 1,
'risk_state' => 'normal', 'collection_state' => 'healthy',
'latest_direction' => 'none', 'latest_rule_code' => '',
'latest_severity' => 'none',
'summary_json' => $summary($metrics(1000), $rate),
'last_bucket_start' => $now - 60,
'last_sample_at' => $now - 1, 'last_evaluated_at' => $now - 1,
'updated_at' => $now - 1],
['instance_id' => 2, 'user_id' => 12, 'host_id' => 1,
'risk_state' => 'high_risk', 'collection_state' => 'healthy',
'latest_direction' => 'outbound',
'latest_rule_code' => 'outbound_tcp_syn_sweep',
'latest_severity' => 'high',
'summary_json' => $summary($metrics(2000, 6000), $rate),
'last_bucket_start' => $now - 60,
'last_sample_at' => $now - 2, 'last_evaluated_at' => $now - 2,
'updated_at' => $now - 2],
['instance_id' => 3, 'user_id' => 11, 'host_id' => 2,
'risk_state' => 'high_risk', 'collection_state' => 'healthy',
'latest_direction' => 'inbound',
'latest_rule_code' => 'inbound_tcp_syn_attack',
'latest_severity' => 'high',
'summary_json' => $summary($metrics(3000), $rate),
'last_bucket_start' => $now - 600,
'last_sample_at' => $now - 540,
'last_evaluated_at' => $now - 540,
'updated_at' => $now - 540],
['instance_id' => 4, 'user_id' => 11, 'host_id' => 1,
'risk_state' => 'normal', 'collection_state' => 'healthy',
'latest_direction' => 'none', 'latest_rule_code' => '',
'latest_severity' => 'none', 'summary_json' => '{broken',
'last_bucket_start' => $now - 60,
'last_sample_at' => $now - 1, 'last_evaluated_at' => $now - 1,
'updated_at' => $now - 1],
]);
$main->table('cloud_abuse_findings')->insert([
'finding_key' => $findingKey(
2, 'outbound_tcp_syn_sweep', 'ipv6', 'outbound'
),
'instance_id' => 2, 'user_id' => 12, 'host_id' => 1,
'rule_code' => 'outbound_tcp_syn_sweep', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'ipv6',
'severity' => 'high', 'state' => 'open',
'first_seen_at' => $now - 120, 'last_seen_at' => $now - 60,
'consecutive_windows' => 2,
'evidence_json' => json_encode([
'tcp_syn_flows' => 6000, 'unique_remote_ips' => 700,
], JSON_THROW_ON_ERROR),
'resolved_at' => null, 'created_at' => $now - 120,
'updated_at' => $now - 60,
]);
$log->table('cloud_vm_network_monitor_cursors')->insert([
'host_id' => 1, 'collection_state' => 'healthy',
'last_success_at' => $now - 1,
]);
for ($index = 0; $index < 61; ++$index) {
$bucket = $now - (60 * (60 - $index));
$log->table('cloud_vm_network_observation_samples')->insert([
'sample_key' => hash('sha256', 'sample-' . $index),
'instance_id' => 2, 'bucket_start' => $bucket,
'complete' => 1,
'metrics_json' => json_encode(
$metrics(1000 + $index * 600, 100 + $index),
JSON_THROW_ON_ERROR
),
'quality_flags_json' => '[]',
]);
}
$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 did not use the same stale/corrupt boundaries as the list.');
$assert($page['health']['worker_state'] === 'degraded'
&& $page['health']['error_code'] === 'partial_collection_failure',
'Missing enabled-host coverage was incorrectly 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
&& $broken['metrics']['tcp_active_flows'] === null,
'Corrupt summary JSON became healthy zero data or caused a page failure.');
$high = $service->page($actor, ['risk' => 'high_risk']);
$assert($high['pagination']['total'] === 1
&& $high['items'][0]['instance_id'] === '2',
'Stale risk state leaked into the verified high-risk filter.');
$ipv6 = $service->page($actor, ['ip_version' => 'ipv6']);
$assert($ipv6['pagination']['total'] === 1
&& $ipv6['items'][0]['instance_id'] === '2'
&& $ipv6['summary']['high_risk'] === 1,
'IP-family filter and summary scope diverged.');
$search = $service->page($actor, ['query' => 'alpha']);
$assert($search['pagination']['total'] === 3,
'Owner/name query did not retain its bounded exact scope.');
$insertState = static function (
int $id,
string $name,
string $risk,
string $summaryJson
) use ($main, $uuid, $now): void {
$main->table('cloud_instances')->insert([
'id' => $id, 'uuid' => $uuid($id), 'host_id' => 1,
'user_id' => 11, 'resource_pool_id' => 4,
'name' => $name, 'status' => 'active',
]);
$main->table('cloud_abuse_instance_states')->insert([
'instance_id' => $id, 'user_id' => 11, 'host_id' => 1,
'risk_state' => $risk, 'collection_state' => 'healthy',
'latest_direction' => 'none', 'latest_rule_code' => '',
'latest_severity' => 'none', 'summary_json' => $summaryJson,
'last_bucket_start' => $now - 60,
'last_sample_at' => $now - 1,
'last_evaluated_at' => $now - 1,
'updated_at' => $now - 1,
]);
};
$insertState(5, 'missing-summary-shape', 'normal', '{}');
$insertState(6, 'oversized-summary', 'normal', json_encode([
'rates' => $rate,
'sample_count' => 80,
'warmup_remaining' => 0,
'quality_flags' => [],
'metrics' => $metrics(6000),
'padding' => str_repeat('x', 140000),
], JSON_THROW_ON_ERROR));
$insertState(7, 'junk-metrics', 'normal', json_encode([
'rates' => $rate,
'sample_count' => 80,
'warmup_remaining' => 0,
'quality_flags' => [],
'metrics' => ['junk' => 1],
], JSON_THROW_ON_ERROR));
$insertState(8, 'null-rate-warmup', 'observing', json_encode([
'rates' => [
'rx_bps' => null, 'tx_bps' => null,
'rx_pps' => null, 'tx_pps' => null,
],
'sample_count' => 1,
'warmup_remaining' => 79,
'quality_flags' => [],
'metrics' => $metrics(8000),
], JSON_THROW_ON_ERROR));
$adversarialPage = $service->page($actor, ['per_page' => 100]);
$adversarialAnomalies = $service->page($actor, [
'risk' => 'collection_anomaly', 'per_page' => 100,
]);
$adversarialNormal = $service->page($actor, [
'risk' => 'normal', 'per_page' => 100,
]);
$adversarialObserving = $service->page($actor, [
'risk' => 'observing', 'per_page' => 100,
]);
$anomalyIds = array_column($adversarialAnomalies['items'], 'instance_id');
$normalIds = array_column($adversarialNormal['items'], 'instance_id');
$assert($adversarialPage['pagination']['total'] === 8
&& $adversarialPage['summary']['total'] === 8
&& $adversarialPage['summary']['collection_anomaly'] === 5
&& $adversarialAnomalies['pagination']['total'] === 5
&& array_diff(['5', '6', '7'], $anomalyIds) === []
&& array_intersect(['5', '6', '7'], $normalIds) === []
&& $adversarialObserving['pagination']['total'] === 1
&& $adversarialObserving['items'][0]['instance_id'] === '8'
&& $adversarialObserving['items'][0]['sample']['quality'] === 'complete',
'Strict summary classification diverged for missing, oversized, junk or nullable-rate data.');
$detail = $service->detail($actor, 2);
$encoded = json_encode($detail, JSON_THROW_ON_ERROR);
$assert(count($detail['series']) === 60
&& $detail['series'][0]['tx_bps'] === 160
&& count($detail['findings']) === 1
&& str_contains($detail['findings'][0]['evidence_summary'],
'TCP SYN Flow 6,000')
&& !str_contains($encoded, 'evidence_json')
&& !str_contains($encoded, 'metrics_json')
&& !str_contains($encoded, 'quality_flags_json')
&& !str_contains($encoded, 'remote_addresses')
&& !str_contains($encoded, '"remote_ports"'),
'Detail response leaked raw evidence or failed its bounded trend contract.');
$main->table('cloud_abuse_findings')->insert([
'finding_key' => $findingKey(
1, 'outbound_udp_fanout', 'mixed', 'outbound'
),
'instance_id' => 1, 'user_id' => 11, 'host_id' => 1,
'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'mixed',
'severity' => 'suspicious', 'state' => 'open',
'first_seen_at' => $now - 120, 'last_seen_at' => $now - 60,
'consecutive_windows' => 2, 'evidence_json' => '{}',
'resolved_at' => null, 'created_at' => $now - 120,
'updated_at' => $now - 60,
]);
$mixedIpv4 = $service->page($actor, [
'ip_version' => 'ipv4', 'per_page' => 100,
]);
$mixedIpv6 = $service->page($actor, [
'ip_version' => 'ipv6', 'per_page' => 100,
]);
$assert(array_column($mixedIpv4['items'], 'instance_id') === ['1']
&& array_diff(
['1', '2'],
array_column($mixedIpv6['items'], 'instance_id')
) === [],
'Mixed-family open findings were not visible from both IP-family filters.');
$main->table('cloud_abuse_findings')->insert([
[
'finding_key' => $findingKey(
2, 'outbound_udp_fanout', 'ipv4', 'outbound'
),
'instance_id' => 2, 'user_id' => 11, 'host_id' => 1,
'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1,
'direction' => 'outbound', 'ip_family' => 'ipv4',
'severity' => 'suspicious', 'state' => 'resolved',
'first_seen_at' => $now - 180,
'last_seen_at' => $now - 30,
'consecutive_windows' => 2,
'evidence_json' => '{"udp_new_flows":200}',
'resolved_at' => $now - 20, 'created_at' => $now - 180,
'updated_at' => $now - 20,
],
[
'finding_key' => $findingKey(
2, 'inbound_tcp_port_scan', 'ipv4', 'inbound'
),
'instance_id' => 2, 'user_id' => 12, 'host_id' => 1,
'rule_code' => 'inbound_tcp_port_scan', 'rule_version' => 1,
'direction' => 'inbound', 'ip_family' => 'ipv4',
'severity' => 'suspicious', 'state' => 'open',
'first_seen_at' => $now - 50,
'last_seen_at' => $now - 10,
'consecutive_windows' => 2,
'evidence_json' => json_encode([
'tcp_new_flows' => 20,
'padding' => str_repeat('y', 140000),
], JSON_THROW_ON_ERROR),
'resolved_at' => null, 'created_at' => $now - 50,
'updated_at' => $now - 10,
],
]);
$log->table('cloud_vm_network_observation_samples')->insert([
[
'sample_key' => hash('sha256', 'junk-series'),
'instance_id' => 2, 'bucket_start' => $now + 60,
'complete' => 1, 'metrics_json' => '{"junk":1}',
'quality_flags_json' => '[]',
],
[
'sample_key' => hash('sha256', 'oversized-series-metrics'),
'instance_id' => 2, 'bucket_start' => $now + 120,
'complete' => 1, 'metrics_json' => json_encode([
'interface' => $metrics(9000)['interface'],
'ipv4' => $metrics(9000)['ipv4'],
'ipv6' => $metrics(9000)['ipv6'],
'padding' => str_repeat('z', 140000),
], JSON_THROW_ON_ERROR),
'quality_flags_json' => '[]',
],
[
'sample_key' => hash('sha256', 'oversized-series-flags'),
'instance_id' => 2, 'bucket_start' => $now + 180,
'complete' => 1,
'metrics_json' => json_encode($metrics(9600), JSON_THROW_ON_ERROR),
'quality_flags_json' => json_encode([
str_repeat('q', 140000),
], JSON_THROW_ON_ERROR),
],
]);
$adversarialDetail = $service->detail($actor, 2);
$adversarialEncoded = json_encode(
$adversarialDetail,
JSON_THROW_ON_ERROR
);
$recentSeries = array_slice($adversarialDetail['series'], -3);
$assert(count($adversarialDetail['series']) === 60
&& array_column($recentSeries, 'quality') === [
'incomplete', 'incomplete', 'incomplete',
]
&& count($adversarialDetail['findings']) === 2
&& !in_array(
'outbound_udp_fanout',
array_column($adversarialDetail['findings'], 'rule_code'),
true
)
&& str_contains(
$adversarialDetail['findings'][0]['evidence_summary'],
'聚合证据不可用'
)
&& !str_contains($adversarialEncoded, str_repeat('y', 1000))
&& !str_contains($adversarialEncoded, str_repeat('z', 1000)),
'Detail admitted malformed samples, oversized JSON or a previous owner finding.');
$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 cursors were used to invent 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 class.');
}
}
$source = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudAdminAbuseMonitoringService.php');
$controller = file_get_contents(dirname(__DIR__)
. '/app/Controllers/CloudAdminAbuseMonitoringController.php');
$assert(is_string($source) && is_string($controller)
&& !str_contains($source, 'cloud_jobs')
&& !str_contains($source, 'service_status')
&& !str_contains($source, 'network_status')
&& !str_contains($source, 'remote_addresses')
&& str_contains($controller, "'Cache-Control', 'private, no-store'")
&& !preg_match('/->(?:insert|update|delete|lockForUpdate)\s*\(/', $source),
'Administrator monitor gained a mutation, enforcement or raw-flow path.');
echo "Administrator abuse-monitoring service tests passed.\n";