refactor(abuse): compact alert delivery ledger
parent
f9ae55d21d
commit
300e4c8ba8
@ -1,510 +1,144 @@
|
||||
<?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;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
|
||||
], 'default');
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
], 'log');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
User::setConnectionResolver($capsule->getDatabaseManager());
|
||||
$db = $capsule->getConnection('default');
|
||||
$schema = $db->getSchemaBuilder();
|
||||
|
||||
$mainSchema = $capsule->schema('default');
|
||||
$mainSchema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
});
|
||||
$mainSchema->create(
|
||||
'cloud_abuse_projection_receipts',
|
||||
static function (Blueprint $table): void {
|
||||
$table->string('sample_key')->primary();
|
||||
$table->integer('instance_id');
|
||||
$table->integer('rule_version');
|
||||
$table->integer('policy_revision')->default(1);
|
||||
$table->string('policy_hash')->default(
|
||||
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
|
||||
);
|
||||
$table->integer('bucket_start');
|
||||
$table->integer('projected_at');
|
||||
}
|
||||
);
|
||||
$mainSchema->create('cloud_abuse_findings',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('finding_key')->unique();
|
||||
$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->integer('occurrence_generation')->default(1);
|
||||
$table->integer('transition_revision')->default(1);
|
||||
$table->text('evidence_json');
|
||||
$table->integer('resolved_at')->nullable();
|
||||
$table->string('resolution_reason')->default('');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$mainSchema->create('cloud_abuse_alert_events',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('sample_key')->unique();
|
||||
$table->integer('instance_id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('created_at');
|
||||
});
|
||||
$mainSchema->create('cloud_abuse_alert_event_items',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('event_id');
|
||||
$table->string('finding_key');
|
||||
$table->foreign('event_id')->references('id')
|
||||
->on('cloud_abuse_alert_events')->cascadeOnDelete();
|
||||
});
|
||||
$mainSchema->create('cloud_abuse_alert_deliveries',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('event_id')->unique();
|
||||
$table->string('state');
|
||||
$table->integer('terminal_at')->nullable();
|
||||
$table->foreign('event_id')->references('id')
|
||||
->on('cloud_abuse_alert_events')->cascadeOnDelete();
|
||||
});
|
||||
$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();
|
||||
});
|
||||
|
||||
$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->integer('rule_version')->default(1);
|
||||
$table->integer('policy_revision')->default(1);
|
||||
$table->string('policy_hash')->default(
|
||||
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
|
||||
);
|
||||
}
|
||||
);
|
||||
$logSchema->create(
|
||||
'cloud_vm_network_observation_rollups',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('instance_id');
|
||||
$table->integer('interval_seconds');
|
||||
$table->integer('bucket_start');
|
||||
$table->primary(['instance_id', 'interval_seconds', 'bucket_start']);
|
||||
}
|
||||
);
|
||||
$logSchema->create(
|
||||
'cloud_vm_network_projection_outbox',
|
||||
static function (Blueprint $table): void {
|
||||
$table->string('sample_key')->primary();
|
||||
$table->integer('instance_id');
|
||||
$table->integer('bucket_start');
|
||||
$table->string('state');
|
||||
$table->integer('terminal_at')->nullable();
|
||||
}
|
||||
);
|
||||
$logSchema->create(
|
||||
'cloud_vm_network_baselines',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('instance_id')->primary();
|
||||
$table->integer('user_id');
|
||||
$table->integer('rule_version')->default(1);
|
||||
$table->integer('policy_revision')->default(1);
|
||||
$table->string('policy_hash')->default(
|
||||
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae'
|
||||
);
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
);
|
||||
|
||||
$main = $capsule->connection('default');
|
||||
$log = $capsule->connection('log');
|
||||
$main->statement('PRAGMA foreign_keys = ON');
|
||||
$now = 20_000_000;
|
||||
$sampleCutoff = $now
|
||||
- CloudAbuseRetentionService::RAW_TELEMETRY_RETENTION_SECONDS;
|
||||
$outboxCutoff = $now
|
||||
- CloudAbuseRetentionService::TERMINAL_OUTBOX_RETENTION_SECONDS;
|
||||
$receiptCutoff = $now
|
||||
- CloudAbuseRetentionService::RECEIPT_RETENTION_SECONDS;
|
||||
$readCutoff = $now
|
||||
- CloudAbuseRetentionService::READ_MODEL_RETENTION_SECONDS;
|
||||
$oldBucket = intdiv($sampleCutoff - 120, 60) * 60;
|
||||
$recentBucket = intdiv($sampleCutoff + 120, 60) * 60;
|
||||
$defaultPolicyHash =
|
||||
'511828bde950c504f92bb7a0603ca8b14453f575ead276c1889baa764f384dae';
|
||||
|
||||
$key = static fn (string $name): string => hash('sha256', $name);
|
||||
$fixtureFindingKey = static fn (string $name): string => $key(
|
||||
'finding-' . $defaultPolicyHash . '-' . $name
|
||||
);
|
||||
$sample = static function (
|
||||
string $name,
|
||||
int $instanceId,
|
||||
int $bucket
|
||||
) use ($log, $key): string {
|
||||
$sampleKey = $key($name);
|
||||
$log->table('cloud_vm_network_observation_samples')->insert([
|
||||
'sample_key' => $sampleKey,
|
||||
'instance_id' => $instanceId,
|
||||
'bucket_start' => $bucket,
|
||||
]);
|
||||
|
||||
return $sampleKey;
|
||||
};
|
||||
$outbox = static function (
|
||||
string $sampleKey,
|
||||
int $instanceId,
|
||||
int $bucket,
|
||||
string $state,
|
||||
?int $terminalAt = null
|
||||
) use ($log): void {
|
||||
$log->table('cloud_vm_network_projection_outbox')->insert([
|
||||
'sample_key' => $sampleKey,
|
||||
'instance_id' => $instanceId,
|
||||
'bucket_start' => $bucket,
|
||||
'state' => $state,
|
||||
'terminal_at' => $terminalAt,
|
||||
]);
|
||||
};
|
||||
$receipt = static function (
|
||||
string $sampleKey,
|
||||
int $instanceId,
|
||||
int $projectedAt
|
||||
) use ($main): void {
|
||||
$main->table('cloud_abuse_projection_receipts')->insert([
|
||||
'sample_key' => $sampleKey,
|
||||
'instance_id' => $instanceId,
|
||||
'rule_version' => 1,
|
||||
'bucket_start' => 60,
|
||||
'projected_at' => $projectedAt,
|
||||
]);
|
||||
};
|
||||
$finding = static function (
|
||||
string $name,
|
||||
string $state,
|
||||
?int $resolvedAt
|
||||
) use ($main, $fixtureFindingKey, $now): void {
|
||||
$main->table('cloud_abuse_findings')->insert([
|
||||
'finding_key' => $fixtureFindingKey($name),
|
||||
'instance_id' => 1,
|
||||
'user_id' => 1,
|
||||
'host_id' => 1,
|
||||
'rule_code' => 'test_rule',
|
||||
'rule_version' => 1,
|
||||
'direction' => 'outbound',
|
||||
'ip_family' => 'ipv4',
|
||||
'severity' => 'info',
|
||||
'state' => $state,
|
||||
'first_seen_at' => 60,
|
||||
'last_seen_at' => 60,
|
||||
'consecutive_windows' => 1,
|
||||
'evidence_json' => '{}',
|
||||
'resolved_at' => $resolvedAt,
|
||||
'created_at' => 60,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
};
|
||||
|
||||
$main->table('cloud_instances')->insert(['id' => 1]);
|
||||
$log->table('cloud_vm_network_baselines')->insert([
|
||||
['instance_id' => 1, 'user_id' => 1],
|
||||
['instance_id' => 2, 'user_id' => 2],
|
||||
['instance_id' => 3, 'user_id' => 3],
|
||||
$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],
|
||||
]);
|
||||
|
||||
$doneOld = $sample('done-old', 1, $oldBucket);
|
||||
$outbox($doneOld, 1, $oldBucket, 'done', $outboxCutoff - 1);
|
||||
$receipt($doneOld, 1, $receiptCutoff - 1);
|
||||
|
||||
$pendingOld = $sample('pending-old', 3, $oldBucket);
|
||||
$outbox($pendingOld, 3, $oldBucket, 'pending');
|
||||
$leasedOld = $sample('leased-old', 1, $oldBucket);
|
||||
$outbox($leasedOld, 1, $oldBucket, 'leased');
|
||||
$doneRecent = $sample('done-recent', 1, $recentBucket);
|
||||
$outbox($doneRecent, 1, $recentBucket, 'done', $outboxCutoff + 1);
|
||||
$orphanOld = $sample('orphan-old', 1, $oldBucket);
|
||||
|
||||
$receiptGone = $key('receipt-gone');
|
||||
$receipt($receiptGone, 1, $receiptCutoff - 1);
|
||||
$receiptRecent = $key('receipt-recent');
|
||||
$receipt($receiptRecent, 1, $receiptCutoff + 1);
|
||||
$receiptWithSample = $sample('receipt-with-sample', 1, $recentBucket);
|
||||
$receipt($receiptWithSample, 1, $receiptCutoff - 1);
|
||||
|
||||
$fiveMinuteCutoff = $now
|
||||
- CloudAbuseRetentionService::FIVE_MINUTE_ROLLUP_RETENTION_SECONDS;
|
||||
$hourlyCutoff = $now
|
||||
- CloudAbuseRetentionService::HOURLY_ROLLUP_RETENTION_SECONDS;
|
||||
$log->table('cloud_vm_network_observation_rollups')->insert([
|
||||
['instance_id' => 1, 'interval_seconds' => 300,
|
||||
'bucket_start' => intdiv($fiveMinuteCutoff - 300, 300) * 300],
|
||||
['instance_id' => 1, 'interval_seconds' => 300,
|
||||
'bucket_start' => intdiv($fiveMinuteCutoff + 300, 300) * 300],
|
||||
['instance_id' => 1, 'interval_seconds' => 3600,
|
||||
'bucket_start' => intdiv($hourlyCutoff - 3600, 3600) * 3600],
|
||||
['instance_id' => 1, 'interval_seconds' => 3600,
|
||||
'bucket_start' => intdiv($hourlyCutoff + 3600, 3600) * 3600],
|
||||
$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],
|
||||
]);
|
||||
|
||||
$finding('resolved-old', 'resolved', $readCutoff - 1);
|
||||
$finding('resolved-recent', 'resolved', $readCutoff + 1);
|
||||
$finding('open-old', 'open', null);
|
||||
$oldEventFindingKey = $fixtureFindingKey('resolved-old');
|
||||
$main->table('cloud_abuse_alert_events')->insert([
|
||||
'id' => 1,
|
||||
'sample_key' => $key('old-alert-event'),
|
||||
'instance_id' => 1,
|
||||
'user_id' => 1,
|
||||
'created_at' => $readCutoff - 1,
|
||||
$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],
|
||||
]);
|
||||
$main->table('cloud_abuse_alert_event_items')->insert([
|
||||
'id' => 1,
|
||||
'event_id' => 1,
|
||||
'finding_key' => $oldEventFindingKey,
|
||||
$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],
|
||||
]);
|
||||
foreach ([2, 3, 4] as $eventId) {
|
||||
$main->table('cloud_abuse_alert_events')->insert([
|
||||
'id' => $eventId,
|
||||
'sample_key' => $key('delivery-alert-' . $eventId),
|
||||
'instance_id' => 1,
|
||||
'user_id' => 1,
|
||||
'created_at' => $readCutoff - 1,
|
||||
]);
|
||||
$main->table('cloud_abuse_alert_event_items')->insert([
|
||||
'id' => $eventId,
|
||||
'event_id' => $eventId,
|
||||
'finding_key' => $key('delivery-finding-' . $eventId),
|
||||
]);
|
||||
}
|
||||
$main->table('cloud_abuse_alert_deliveries')->insert([
|
||||
['event_id' => 2, 'state' => 'sent',
|
||||
'terminal_at' => $readCutoff - 1],
|
||||
['event_id' => 3, 'state' => 'unknown',
|
||||
'terminal_at' => $readCutoff - 1],
|
||||
$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 === [
|
||||
'terminal_outboxes' => 1,
|
||||
'samples' => 2,
|
||||
'five_minute_rollups' => 1,
|
||||
'hourly_rollups' => 1,
|
||||
'baselines' => 1,
|
||||
'receipts' => 3,
|
||||
'findings' => 1,
|
||||
'daily_summaries' => 1,
|
||||
'alert_events' => 1,
|
||||
], 'Retention pass returned unexpected bounded deletion counts.');
|
||||
$assert(!$log->table('cloud_vm_network_projection_outbox')
|
||||
->where('sample_key', $doneOld)->exists()
|
||||
&& !$log->table('cloud_vm_network_observation_samples')
|
||||
->where('sample_key', $doneOld)->exists(),
|
||||
'Expired terminal projection and sample were not removed together.');
|
||||
$assert(!$log->table('cloud_vm_network_observation_samples')
|
||||
->where('sample_key', $orphanOld)->exists(),
|
||||
'Expired orphan sample was not removed.');
|
||||
foreach ([$pendingOld, $leasedOld, $doneRecent] as $protectedKey) {
|
||||
$assert($log->table('cloud_vm_network_projection_outbox')
|
||||
->where('sample_key', $protectedKey)->exists()
|
||||
&& $log->table('cloud_vm_network_observation_samples')
|
||||
->where('sample_key', $protectedKey)->exists(),
|
||||
'Pending, leased or recent telemetry was removed.');
|
||||
}
|
||||
$assert($log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', 1)->exists()
|
||||
&& !$log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', 2)->exists()
|
||||
&& $log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', 3)->exists(),
|
||||
'Orphan baseline cleanup ignored live ownership or active projection.');
|
||||
$assert(!$main->table('cloud_abuse_projection_receipts')
|
||||
->whereIn('sample_key', [
|
||||
$doneOld, $receiptGone, $receiptWithSample,
|
||||
])->exists()
|
||||
&& $main->table('cloud_abuse_projection_receipts')
|
||||
->where('sample_key', $receiptRecent)->exists(),
|
||||
'Receipt retention ignored the bounded replay horizon.');
|
||||
$assert($log->table('cloud_vm_network_observation_rollups')->count() === 2,
|
||||
'Observation rollups did not keep the correct bounded horizons.');
|
||||
$assert($main->table('cloud_abuse_findings')->count() === 2
|
||||
&& !$main->table('cloud_abuse_findings')
|
||||
->where(
|
||||
'finding_key',
|
||||
$fixtureFindingKey('resolved-old')
|
||||
)->exists(),
|
||||
'Finding retention removed an open/recent finding or kept an old one.');
|
||||
$assert($main->table('cloud_abuse_alert_events')->where('id', 1)->exists()
|
||||
&& $main->table('cloud_abuse_alert_event_items')
|
||||
->where('event_id', 1)->exists(),
|
||||
'Phase-6 retention removed an alert before delivery finality exists.');
|
||||
$assert(!$main->table('cloud_abuse_alert_events')->where('id', 2)->exists()
|
||||
&& !$main->table('cloud_abuse_alert_event_items')
|
||||
->where('event_id', 2)->exists()
|
||||
&& $main->table('cloud_abuse_alert_events')
|
||||
->whereIn('id', [3, 4])->count() === 2,
|
||||
'Alert retention removed pending/unknown work or retained known terminal work.');
|
||||
|
||||
// Terminal projection state and raw evidence have independent horizons. A raw
|
||||
// deletion failure must leave only the sample for a later retry; it must not
|
||||
// resurrect already-finalized projection work.
|
||||
$rollbackKey = $sample('rollback-terminal', 1, $oldBucket);
|
||||
$outbox(
|
||||
$rollbackKey,
|
||||
1,
|
||||
$oldBucket,
|
||||
'discarded',
|
||||
$outboxCutoff - 1
|
||||
);
|
||||
$quotedRollback = $log->getPdo()->quote($rollbackKey);
|
||||
$log->statement(
|
||||
'CREATE TRIGGER fail_abuse_sample_delete '
|
||||
. 'BEFORE DELETE ON cloud_vm_network_observation_samples '
|
||||
. 'WHEN OLD.sample_key = ' . $quotedRollback . ' '
|
||||
. "BEGIN SELECT RAISE(ABORT, 'injected retention failure'); END"
|
||||
);
|
||||
$failed = false;
|
||||
try {
|
||||
$service->prune();
|
||||
} catch (Throwable) {
|
||||
$failed = true;
|
||||
}
|
||||
$assert($failed
|
||||
&& !$log->table('cloud_vm_network_projection_outbox')
|
||||
->where('sample_key', $rollbackKey)->exists()
|
||||
&& $log->table('cloud_vm_network_observation_samples')
|
||||
->where('sample_key', $rollbackKey)->exists(),
|
||||
'Independent terminal intent and sample retention was not restart-safe.');
|
||||
$log->statement('DROP TRIGGER fail_abuse_sample_delete');
|
||||
$retry = $service->prune();
|
||||
$assert($retry['samples'] === 1
|
||||
&& !$log->table('cloud_vm_network_observation_samples')
|
||||
->where('sample_key', $rollbackKey)->exists(),
|
||||
'Raw sample cleanup did not resume after a transient storage failure.');
|
||||
|
||||
// The configured bound applies to each independently indexed retention set.
|
||||
$finding('batch-one', 'resolved', $readCutoff - 2);
|
||||
$finding('batch-two', 'resolved', $readCutoff - 2);
|
||||
$bounded = $service->prune(1);
|
||||
$assert($bounded['findings'] === 1
|
||||
&& $main->table('cloud_abuse_findings')
|
||||
->whereIn('finding_key', [
|
||||
$fixtureFindingKey('batch-one'),
|
||||
$fixtureFindingKey('batch-two'),
|
||||
])->count() === 1,
|
||||
'Retention batch size did not bound the indexed finding pass.');
|
||||
'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() === [
|
||||
'terminal_outboxes' => 0,
|
||||
'samples' => 0,
|
||||
'five_minute_rollups' => 0,
|
||||
'hourly_rollups' => 0,
|
||||
'baselines' => 0,
|
||||
'receipts' => 0,
|
||||
'findings' => 1,
|
||||
'daily_summaries' => 0,
|
||||
'alert_events' => 0,
|
||||
], 'A restart-safe retention pass was not idempotent.');
|
||||
|
||||
// Protected rows at the front of a bounded scan must not permanently starve
|
||||
// an eligible row beyond the limit. The process cursor advances and wraps;
|
||||
// a time-derived first anchor also varies repeated --once worker starts.
|
||||
$log->table('cloud_vm_network_projection_outbox')->delete();
|
||||
$log->table('cloud_vm_network_observation_samples')->delete();
|
||||
$log->table('cloud_vm_network_baselines')->delete();
|
||||
$main->table('cloud_abuse_projection_receipts')->delete();
|
||||
$main->table('cloud_instances')->insert(['id' => 2]);
|
||||
$log->table('cloud_vm_network_baselines')->insert([
|
||||
['instance_id' => 1, 'user_id' => 1],
|
||||
['instance_id' => 2, 'user_id' => 2],
|
||||
['instance_id' => 3, 'user_id' => 3],
|
||||
]);
|
||||
$scanMinute = intdiv($now, 60);
|
||||
$scanMinute -= $scanMinute % 4;
|
||||
$scanNow = $scanMinute * 60;
|
||||
$scanService = new CloudAbuseRetentionService(
|
||||
static fn (): int => $scanNow
|
||||
);
|
||||
$firstBaselineScan = $scanService->prune(2);
|
||||
$assert($firstBaselineScan['baselines'] === 0
|
||||
&& $log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', 3)->exists(),
|
||||
'A protected baseline prefix was not exercised.');
|
||||
$secondBaselineScan = $scanService->prune(2);
|
||||
$assert($secondBaselineScan['baselines'] === 1
|
||||
&& !$log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', 3)->exists(),
|
||||
'A high-ID orphan baseline remained starved after scan wraparound.');
|
||||
|
||||
$log->table('cloud_vm_network_projection_outbox')->delete();
|
||||
$log->table('cloud_vm_network_observation_samples')->delete();
|
||||
$main->table('cloud_abuse_projection_receipts')->delete();
|
||||
$receiptKeys = [];
|
||||
$scanReadCutoff = $scanNow
|
||||
- CloudAbuseRetentionService::RECEIPT_RETENTION_SECONDS;
|
||||
foreach ([1, 2, 3] as $number) {
|
||||
$receiptKeys[$number] = str_pad(
|
||||
dechex($number),
|
||||
64,
|
||||
'0',
|
||||
STR_PAD_LEFT
|
||||
);
|
||||
$receipt($receiptKeys[$number], 1, $scanReadCutoff - 1);
|
||||
if ($number <= 2) {
|
||||
$log->table('cloud_vm_network_observation_samples')->insert([
|
||||
'sample_key' => $receiptKeys[$number],
|
||||
'instance_id' => 1,
|
||||
'bucket_start' => $oldBucket,
|
||||
]);
|
||||
$outbox(
|
||||
$receiptKeys[$number],
|
||||
1,
|
||||
$oldBucket,
|
||||
'pending'
|
||||
);
|
||||
}
|
||||
}
|
||||
$receiptScanService = new CloudAbuseRetentionService(
|
||||
static fn (): int => $scanNow
|
||||
);
|
||||
$receiptScanService->prune(2);
|
||||
$receiptScanService->prune(2);
|
||||
$assert($main->table('cloud_abuse_projection_receipts')->count() === 0,
|
||||
'Receipt tombstones outlived the bounded Agent replay horizon.');
|
||||
$assert($log->table('cloud_vm_network_projection_outbox')->count() === 2
|
||||
&& $log->table('cloud_vm_network_observation_samples')->count() === 2,
|
||||
'Receipt scan removed pending or leased projection evidence.');
|
||||
'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);
|
||||
throw new RuntimeException('Invalid retention batch size was accepted.');
|
||||
} catch (InvalidArgumentException) {
|
||||
// Expected.
|
||||
$threw = true;
|
||||
}
|
||||
$assert($threw, 'Invalid retention batch size was accepted.');
|
||||
}
|
||||
|
||||
echo "Cloud abuse retention service tests passed.\n";
|
||||
echo "Compact abuse retention tests passed.\n";
|
||||
|
||||
Loading…
Reference in New Issue