|
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Services\Cloud\CloudAbuseRetentionService;
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
User::setConnectionResolver($capsule->getDatabaseManager());
|
|
|
|
|
$db = $capsule->getConnection('default');
|
|
|
|
|
$schema = $db->getSchemaBuilder();
|
|
|
|
|
|
|
|
|
|
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_monitor_hosts', static function (Blueprint $table): void {
|
|
|
|
|
$table->integer('host_id')->primary();
|
|
|
|
|
$table->string('poll_lease_token')->nullable();
|
|
|
|
|
$table->integer('poll_lease_until')->default(0);
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_daily_summaries', static function (Blueprint $table): void {
|
|
|
|
|
$table->integer('instance_id');
|
|
|
|
|
$table->integer('day_start');
|
|
|
|
|
$table->primary(['instance_id', 'day_start']);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_findings', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('resolved_at')->nullable();
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_alert_events', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('finding_id');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_alert_deliveries', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('event_id')->unique();
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('terminal_at')->nullable();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$now = 2_000_000_000;
|
|
|
|
|
$historyCutoff = $now - CloudAbuseRetentionService::ALERT_HISTORY_RETENTION_SECONDS;
|
|
|
|
|
$dailyCutoff = $now - CloudAbuseRetentionService::DAILY_SUMMARY_RETENTION_SECONDS;
|
|
|
|
|
$hostCutoff = $now - CloudAbuseRetentionService::ORPHAN_HOST_RETENTION_SECONDS;
|
|
|
|
|
|
|
|
|
|
$db->table('cloud_hosts')->insert(['id' => 1]);
|
|
|
|
|
$db->table('cloud_instances')->insert([['id' => 1], ['id' => 2]]);
|
|
|
|
|
$db->table('cloud_network_daily_summaries')->insert([
|
|
|
|
|
['instance_id' => 1, 'day_start' => $dailyCutoff - 86400],
|
|
|
|
|
['instance_id' => 1, 'day_start' => $dailyCutoff + 86400],
|
|
|
|
|
]);
|
|
|
|
|
$db->table('cloud_network_monitor_hosts')->insert([
|
|
|
|
|
['host_id' => 1, 'poll_lease_token' => null, 'poll_lease_until' => 0,
|
|
|
|
|
'updated_at' => $hostCutoff - 1],
|
|
|
|
|
['host_id' => 2, 'poll_lease_token' => null, 'poll_lease_until' => 0,
|
|
|
|
|
'updated_at' => $hostCutoff - 1],
|
|
|
|
|
['host_id' => 3, 'poll_lease_token' => str_repeat('a', 32),
|
|
|
|
|
'poll_lease_until' => $now + 60, 'updated_at' => $hostCutoff - 1],
|
|
|
|
|
['host_id' => 4, 'poll_lease_token' => null, 'poll_lease_until' => 0,
|
|
|
|
|
'updated_at' => $hostCutoff + 60],
|
|
|
|
|
]);
|
|
|
|
|
$db->table('cloud_network_findings')->insert([
|
|
|
|
|
['id' => 1, 'state' => 'resolved', 'resolved_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 2, 'state' => 'resolved', 'resolved_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 3, 'state' => 'resolved', 'resolved_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 4, 'state' => 'open', 'resolved_at' => null],
|
|
|
|
|
['id' => 5, 'state' => 'resolved', 'resolved_at' => $historyCutoff + 60],
|
|
|
|
|
]);
|
|
|
|
|
$db->table('cloud_network_alert_events')->insert([
|
|
|
|
|
['id' => 1, 'finding_id' => 1, 'created_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 2, 'finding_id' => 2, 'created_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 3, 'finding_id' => 2, 'created_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 4, 'finding_id' => 2, 'created_at' => $historyCutoff - 10],
|
|
|
|
|
['id' => 5, 'finding_id' => 2, 'created_at' => $historyCutoff - 10],
|
|
|
|
|
]);
|
|
|
|
|
$db->table('cloud_network_alert_deliveries')->insert([
|
|
|
|
|
['event_id' => 1, 'state' => 'sent', 'terminal_at' => $historyCutoff - 5],
|
|
|
|
|
['event_id' => 2, 'state' => 'unknown', 'terminal_at' => $historyCutoff - 5],
|
|
|
|
|
['event_id' => 4, 'state' => 'pending', 'terminal_at' => null],
|
|
|
|
|
['event_id' => 5, 'state' => 'skipped', 'terminal_at' => $historyCutoff + 60],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new CloudAbuseRetentionService(static fn (): int => $now);
|
|
|
|
|
$result = $service->prune();
|
|
|
|
|
$assert($result === [
|
|
|
|
|
'daily_summaries' => 1,
|
|
|
|
|
'alert_events' => 1,
|
|
|
|
|
'resolved_findings' => 2,
|
|
|
|
|
'orphan_hosts' => 1,
|
|
|
|
|
], 'Compact retention returned an unexpected deletion ledger.');
|
|
|
|
|
$assert(!$db->table('cloud_network_daily_summaries')
|
|
|
|
|
->where('day_start', $dailyCutoff - 86400)->exists()
|
|
|
|
|
&& $db->table('cloud_network_daily_summaries')
|
|
|
|
|
->where('day_start', $dailyCutoff + 86400)->exists(),
|
|
|
|
|
'Daily summaries did not respect the retention cutoff.');
|
|
|
|
|
$assert(!$db->table('cloud_network_alert_events')->where('id', 1)->exists()
|
|
|
|
|
&& $db->table('cloud_network_alert_events')->whereIn('id', [2, 3, 4, 5])
|
|
|
|
|
->count() === 4,
|
|
|
|
|
'Unknown, pending, undelivered or recent-terminal events lost their fence.');
|
|
|
|
|
$assert(!$db->table('cloud_network_findings')->whereIn('id', [1, 3])->exists()
|
|
|
|
|
&& $db->table('cloud_network_findings')->whereIn('id', [2, 4, 5])
|
|
|
|
|
->count() === 3,
|
|
|
|
|
'Finding retention ignored live events, state or age.');
|
|
|
|
|
$assert(!$db->table('cloud_network_monitor_hosts')->where('host_id', 2)->exists()
|
|
|
|
|
&& $db->table('cloud_network_monitor_hosts')->whereIn('host_id', [1, 3, 4])
|
|
|
|
|
->count() === 3,
|
|
|
|
|
'Host retention removed a live, leased or recent cursor.');
|
|
|
|
|
$assert($service->prune() === [
|
|
|
|
|
'daily_summaries' => 0,
|
|
|
|
|
'alert_events' => 0,
|
|
|
|
|
'resolved_findings' => 0,
|
|
|
|
|
'orphan_hosts' => 0,
|
|
|
|
|
], 'Compact retention was not idempotent.');
|
|
|
|
|
|
|
|
|
|
foreach ([0, CloudAbuseRetentionService::DEFAULT_BATCH_SIZE + 1] as $limit) {
|
|
|
|
|
$threw = false;
|
|
|
|
|
try {
|
|
|
|
|
$service->prune($limit);
|
|
|
|
|
} catch (InvalidArgumentException) {
|
|
|
|
|
$threw = true;
|
|
|
|
|
}
|
|
|
|
|
$assert($threw, 'Invalid retention batch size was accepted.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "Compact abuse retention tests passed.\n";
|