feat(cloud): bound abuse monitoring retention
parent
62a7d4adf3
commit
733d66847f
@ -0,0 +1,405 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* Bounded retention for observe-only network-abuse telemetry.
|
||||
*
|
||||
* The log and main databases deliberately remain separate. Each database is
|
||||
* pruned in its own transaction and every pass is idempotent; no lifecycle,
|
||||
* billing, firewall or provider-operation state is touched here.
|
||||
*/
|
||||
final class CloudAbuseRetentionService
|
||||
{
|
||||
public const TELEMETRY_RETENTION_SECONDS = 90 * 86400;
|
||||
public const READ_MODEL_RETENTION_SECONDS = 180 * 86400;
|
||||
public const DEFAULT_BATCH_SIZE = 200;
|
||||
|
||||
/** @var \Closure():int */
|
||||
private readonly \Closure $clock;
|
||||
|
||||
/** Last baseline position inspected by this worker process. */
|
||||
private ?int $baselineScanAfter = null;
|
||||
|
||||
/** @var null|array{projected_at:int,sample_key:string} */
|
||||
private ?array $receiptScanAfter = null;
|
||||
|
||||
public function __construct(?callable $clock = null)
|
||||
{
|
||||
$this->clock = \Closure::fromCallable(
|
||||
$clock ?? static fn (): int => time()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run one bounded, restart-safe retention pass.
|
||||
*
|
||||
* @return array{
|
||||
* terminal_outboxes:int,
|
||||
* samples:int,
|
||||
* baselines:int,
|
||||
* receipts:int,
|
||||
* findings:int
|
||||
* }
|
||||
*/
|
||||
public function prune(int $limit = self::DEFAULT_BATCH_SIZE): array
|
||||
{
|
||||
if ($limit < 1 || $limit > self::DEFAULT_BATCH_SIZE) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Abuse retention batch size must be between 1 and '
|
||||
. self::DEFAULT_BATCH_SIZE
|
||||
);
|
||||
}
|
||||
$now = ($this->clock)();
|
||||
if (!is_int($now) || $now < self::READ_MODEL_RETENTION_SECONDS) {
|
||||
throw new \LogicException('Abuse retention clock is invalid');
|
||||
}
|
||||
|
||||
$telemetryCutoff = $now - self::TELEMETRY_RETENTION_SECONDS;
|
||||
$readModelCutoff = $now - self::READ_MODEL_RETENTION_SECONDS;
|
||||
$terminal = $this->pruneTerminalTelemetry(
|
||||
$telemetryCutoff,
|
||||
$limit
|
||||
);
|
||||
|
||||
return [
|
||||
'terminal_outboxes' => $terminal['outboxes'],
|
||||
'samples' => $terminal['samples']
|
||||
+ $this->pruneOrphanSamples($telemetryCutoff, $limit),
|
||||
'baselines' => $this->pruneOrphanBaselines($limit, $now),
|
||||
'receipts' => $this->pruneReceipts(
|
||||
$readModelCutoff,
|
||||
$limit,
|
||||
$now
|
||||
),
|
||||
'findings' => $this->pruneFindings($readModelCutoff, $limit),
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array{outboxes:int,samples:int} */
|
||||
private function pruneTerminalTelemetry(int $cutoff, int $limit): array
|
||||
{
|
||||
$log = User::getConnectionResolver()->connection('log');
|
||||
|
||||
return $log->transaction(static function () use (
|
||||
$log,
|
||||
$cutoff,
|
||||
$limit
|
||||
): array {
|
||||
$rows = $log->table(
|
||||
'cloud_vm_network_projection_outbox as outbox'
|
||||
)->join(
|
||||
'cloud_vm_network_observation_samples as sample',
|
||||
'sample.sample_key',
|
||||
'=',
|
||||
'outbox.sample_key'
|
||||
)->whereIn('outbox.state', ['done', 'discarded'])
|
||||
->where('outbox.terminal_at', '<=', $cutoff)
|
||||
->where('sample.bucket_start', '<=', $cutoff)
|
||||
->orderBy('outbox.terminal_at')
|
||||
->orderBy('outbox.sample_key')
|
||||
->limit($limit)
|
||||
->lockForUpdate()
|
||||
->get(['outbox.sample_key']);
|
||||
$keys = [];
|
||||
foreach ($rows as $row) {
|
||||
$keys[] = (string) $row->sample_key;
|
||||
}
|
||||
if ($keys === []) {
|
||||
return ['outboxes' => 0, 'samples' => 0];
|
||||
}
|
||||
|
||||
// The foreign key is RESTRICT: terminal intent must be removed
|
||||
// before its immutable sample, within the same log transaction.
|
||||
$outboxes = $log->table('cloud_vm_network_projection_outbox')
|
||||
->whereIn('sample_key', $keys)
|
||||
->whereIn('state', ['done', 'discarded'])
|
||||
->where('terminal_at', '<=', $cutoff)
|
||||
->delete();
|
||||
if ($outboxes !== count($keys)) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse retention terminal outbox changed during pruning'
|
||||
);
|
||||
}
|
||||
$samples = $log->table(
|
||||
'cloud_vm_network_observation_samples'
|
||||
)->whereIn('sample_key', $keys)
|
||||
->where('bucket_start', '<=', $cutoff)
|
||||
->delete();
|
||||
if ($samples !== count($keys)) {
|
||||
throw new \RuntimeException(
|
||||
'Abuse retention sample changed during pruning'
|
||||
);
|
||||
}
|
||||
|
||||
return ['outboxes' => $outboxes, 'samples' => $samples];
|
||||
});
|
||||
}
|
||||
|
||||
private function pruneOrphanSamples(int $cutoff, int $limit): int
|
||||
{
|
||||
$log = User::getConnectionResolver()->connection('log');
|
||||
|
||||
return $log->transaction(static function () use (
|
||||
$log,
|
||||
$cutoff,
|
||||
$limit
|
||||
): int {
|
||||
$rows = $log->table(
|
||||
'cloud_vm_network_observation_samples as sample'
|
||||
)->leftJoin(
|
||||
'cloud_vm_network_projection_outbox as outbox',
|
||||
'outbox.sample_key',
|
||||
'=',
|
||||
'sample.sample_key'
|
||||
)->whereNull('outbox.sample_key')
|
||||
->where('sample.bucket_start', '<=', $cutoff)
|
||||
->orderBy('sample.bucket_start')
|
||||
->orderBy('sample.sample_key')
|
||||
->limit($limit)
|
||||
->lockForUpdate()
|
||||
->get(['sample.sample_key']);
|
||||
$keys = [];
|
||||
foreach ($rows as $row) {
|
||||
$keys[] = (string) $row->sample_key;
|
||||
}
|
||||
if ($keys === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $log->table('cloud_vm_network_observation_samples')
|
||||
->whereIn('sample_key', $keys)
|
||||
->where('bucket_start', '<=', $cutoff)
|
||||
->whereNotExists(static function ($query): void {
|
||||
$query->selectRaw('1')->from(
|
||||
'cloud_vm_network_projection_outbox as outbox'
|
||||
)->whereColumn(
|
||||
'outbox.sample_key',
|
||||
'cloud_vm_network_observation_samples.sample_key'
|
||||
);
|
||||
})->delete();
|
||||
});
|
||||
}
|
||||
|
||||
private function pruneOrphanBaselines(int $limit, int $now): int
|
||||
{
|
||||
$log = User::getConnectionResolver()->connection('log');
|
||||
$main = User::getConnectionResolver()->connection();
|
||||
$candidateIds = $this->baselineCandidates($log, $limit, $now);
|
||||
if ($candidateIds === []) {
|
||||
return 0;
|
||||
}
|
||||
$live = $main->table('cloud_instances')
|
||||
->whereIn('id', $candidateIds)->pluck('id')->all();
|
||||
$liveIds = array_fill_keys(array_map('intval', $live), true);
|
||||
$orphans = array_values(array_filter(
|
||||
$candidateIds,
|
||||
static fn (int $id): bool => !isset($liveIds[$id])
|
||||
));
|
||||
if ($orphans === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $log->transaction(static function () use (
|
||||
$log,
|
||||
$orphans
|
||||
): int {
|
||||
// Pending or leased work owns the baseline even after the main
|
||||
// row disappears. Projection must decide/discard it first.
|
||||
return $log->table('cloud_vm_network_baselines')
|
||||
->whereIn('instance_id', $orphans)
|
||||
->whereNotExists(static function ($query): void {
|
||||
$query->selectRaw('1')->from(
|
||||
'cloud_vm_network_projection_outbox as outbox'
|
||||
)->whereColumn(
|
||||
'outbox.instance_id',
|
||||
'cloud_vm_network_baselines.instance_id'
|
||||
)->whereIn('outbox.state', ['pending', 'leased']);
|
||||
})->delete();
|
||||
});
|
||||
}
|
||||
|
||||
private function pruneReceipts(int $cutoff, int $limit, int $now): int
|
||||
{
|
||||
$main = User::getConnectionResolver()->connection();
|
||||
$log = User::getConnectionResolver()->connection('log');
|
||||
$candidateKeys = $this->receiptCandidates(
|
||||
$main,
|
||||
$cutoff,
|
||||
$limit,
|
||||
$now
|
||||
);
|
||||
if ($candidateKeys === []) {
|
||||
return 0;
|
||||
}
|
||||
$retained = $log->table('cloud_vm_network_observation_samples')
|
||||
->whereIn('sample_key', $candidateKeys)
|
||||
->pluck('sample_key')->all();
|
||||
$retainedKeys = array_fill_keys(array_map('strval', $retained), true);
|
||||
$orphans = array_values(array_filter(
|
||||
$candidateKeys,
|
||||
static fn (string $key): bool => !isset($retainedKeys[$key])
|
||||
));
|
||||
if ($orphans === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $main->transaction(static function () use (
|
||||
$main,
|
||||
$orphans,
|
||||
$cutoff
|
||||
): int {
|
||||
return $main->table('cloud_abuse_projection_receipts')
|
||||
->whereIn('sample_key', $orphans)
|
||||
->where('projected_at', '<=', $cutoff)
|
||||
->delete();
|
||||
});
|
||||
}
|
||||
|
||||
/** @return list<int> */
|
||||
private function baselineCandidates(
|
||||
object $log,
|
||||
int $limit,
|
||||
int $now
|
||||
): array {
|
||||
$maximum = (int) ($log->table('cloud_vm_network_baselines')
|
||||
->max('instance_id') ?? 0);
|
||||
if ($maximum < 1) {
|
||||
$this->baselineScanAfter = null;
|
||||
return [];
|
||||
}
|
||||
if ($this->baselineScanAfter === null
|
||||
|| $this->baselineScanAfter > $maximum) {
|
||||
// A time-derived first anchor prevents repeated --once workers
|
||||
// from always starting at the same live-ID prefix after restart.
|
||||
$this->baselineScanAfter = intdiv($now, 60)
|
||||
% ($maximum + 1);
|
||||
}
|
||||
$anchor = $this->baselineScanAfter;
|
||||
$ids = $log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', '>', $anchor)
|
||||
->orderBy('instance_id')->limit($limit)
|
||||
->pluck('instance_id')->map(static fn ($id): int => (int) $id)
|
||||
->all();
|
||||
$remaining = $limit - count($ids);
|
||||
if ($remaining > 0) {
|
||||
$wrapped = $log->table('cloud_vm_network_baselines')
|
||||
->where('instance_id', '<=', $anchor)
|
||||
->orderBy('instance_id')->limit($remaining)
|
||||
->pluck('instance_id')
|
||||
->map(static fn ($id): int => (int) $id)->all();
|
||||
$ids = array_merge($ids, $wrapped);
|
||||
}
|
||||
if ($ids !== []) {
|
||||
$this->baselineScanAfter = $ids[array_key_last($ids)];
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function receiptCandidates(
|
||||
object $main,
|
||||
int $cutoff,
|
||||
int $limit,
|
||||
int $now
|
||||
): array {
|
||||
$base = static fn () => $main
|
||||
->table('cloud_abuse_projection_receipts')
|
||||
->where('projected_at', '<=', $cutoff);
|
||||
if ($this->receiptScanAfter === null) {
|
||||
$bounds = $base()->selectRaw(
|
||||
'MIN(projected_at) AS oldest, MAX(projected_at) AS newest'
|
||||
)->first();
|
||||
if ($bounds === null || $bounds->oldest === null
|
||||
|| $bounds->newest === null) {
|
||||
return [];
|
||||
}
|
||||
$oldest = (int) $bounds->oldest;
|
||||
$newest = (int) $bounds->newest;
|
||||
$span = max(1, $newest - $oldest + 2);
|
||||
$this->receiptScanAfter = [
|
||||
'projected_at' => $oldest - 1
|
||||
+ (intdiv($now, 60) % $span),
|
||||
// sample_key values are SHA-256 identities. Varying this
|
||||
// anchor by minute keeps repeated --once workers from
|
||||
// selecting the same protected prefix after every restart.
|
||||
'sample_key' => hash(
|
||||
'sha256',
|
||||
'abuse-receipt-retention:' . intdiv($now, 60)
|
||||
),
|
||||
];
|
||||
}
|
||||
$anchor = $this->receiptScanAfter;
|
||||
$rows = $base()->where(static function ($query) use ($anchor): void {
|
||||
$query->where('projected_at', '>', $anchor['projected_at'])
|
||||
->orWhere(static function ($same) use ($anchor): void {
|
||||
$same->where(
|
||||
'projected_at',
|
||||
$anchor['projected_at']
|
||||
)->where('sample_key', '>', $anchor['sample_key']);
|
||||
});
|
||||
})->orderBy('projected_at')->orderBy('sample_key')
|
||||
->limit($limit)->get(['projected_at', 'sample_key'])->all();
|
||||
$remaining = $limit - count($rows);
|
||||
if ($remaining > 0) {
|
||||
$wrapped = $base()->where(
|
||||
static function ($query) use ($anchor): void {
|
||||
$query->where(
|
||||
'projected_at',
|
||||
'<',
|
||||
$anchor['projected_at']
|
||||
)->orWhere(static function ($same) use ($anchor): void {
|
||||
$same->where(
|
||||
'projected_at',
|
||||
$anchor['projected_at']
|
||||
)->where('sample_key', '<=', $anchor['sample_key']);
|
||||
});
|
||||
}
|
||||
)->orderBy('projected_at')->orderBy('sample_key')
|
||||
->limit($remaining)
|
||||
->get(['projected_at', 'sample_key'])->all();
|
||||
$rows = array_merge($rows, $wrapped);
|
||||
}
|
||||
if ($rows === []) {
|
||||
return [];
|
||||
}
|
||||
$last = $rows[array_key_last($rows)];
|
||||
$this->receiptScanAfter = [
|
||||
'projected_at' => (int) $last->projected_at,
|
||||
'sample_key' => (string) $last->sample_key,
|
||||
];
|
||||
return array_map(
|
||||
static fn (object $row): string => (string) $row->sample_key,
|
||||
$rows
|
||||
);
|
||||
}
|
||||
|
||||
private function pruneFindings(int $cutoff, int $limit): int
|
||||
{
|
||||
$main = User::getConnectionResolver()->connection();
|
||||
|
||||
return $main->transaction(static function () use (
|
||||
$main,
|
||||
$cutoff,
|
||||
$limit
|
||||
): int {
|
||||
$ids = $main->table('cloud_abuse_findings')
|
||||
->where('state', 'resolved')
|
||||
->whereNotNull('resolved_at')
|
||||
->where('resolved_at', '<=', $cutoff)
|
||||
->orderBy('resolved_at')->orderBy('id')
|
||||
->limit($limit)->lockForUpdate()->pluck('id')->all();
|
||||
if ($ids === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $main->table('cloud_abuse_findings')
|
||||
->whereIn('id', array_map('intval', $ids))
|
||||
->where('state', 'resolved')
|
||||
->where('resolved_at', '<=', $cutoff)
|
||||
->delete();
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,371 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
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:',
|
||||
], 'default');
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
], 'log');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
|
||||
$mainSchema = $capsule->schema('default');
|
||||
$mainSchema->create('cloud_instances', 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('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->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->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');
|
||||
}
|
||||
);
|
||||
$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');
|
||||
}
|
||||
);
|
||||
|
||||
$main = $capsule->connection('default');
|
||||
$log = $capsule->connection('log');
|
||||
$now = 20_000_000;
|
||||
$telemetryCutoff = $now
|
||||
- CloudAbuseRetentionService::TELEMETRY_RETENTION_SECONDS;
|
||||
$readCutoff = $now
|
||||
- CloudAbuseRetentionService::READ_MODEL_RETENTION_SECONDS;
|
||||
$oldBucket = intdiv($telemetryCutoff - 120, 60) * 60;
|
||||
$recentBucket = intdiv($telemetryCutoff + 120, 60) * 60;
|
||||
|
||||
$key = static fn (string $name): string => hash('sha256', $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, $key, $now): void {
|
||||
$main->table('cloud_abuse_findings')->insert([
|
||||
'finding_key' => $key('finding-' . $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],
|
||||
]);
|
||||
|
||||
$doneOld = $sample('done-old', 1, $oldBucket);
|
||||
$outbox($doneOld, 1, $oldBucket, 'done', $telemetryCutoff - 1);
|
||||
$receipt($doneOld, 1, $readCutoff - 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', $telemetryCutoff + 1);
|
||||
$orphanOld = $sample('orphan-old', 1, $oldBucket);
|
||||
|
||||
$receiptGone = $key('receipt-gone');
|
||||
$receipt($receiptGone, 1, $readCutoff - 1);
|
||||
$receiptRecent = $key('receipt-recent');
|
||||
$receipt($receiptRecent, 1, $readCutoff + 1);
|
||||
$receiptWithSample = $sample('receipt-with-sample', 1, $recentBucket);
|
||||
$receipt($receiptWithSample, 1, $readCutoff - 1);
|
||||
|
||||
$finding('resolved-old', 'resolved', $readCutoff - 1);
|
||||
$finding('resolved-recent', 'resolved', $readCutoff + 1);
|
||||
$finding('open-old', 'open', null);
|
||||
|
||||
$service = new CloudAbuseRetentionService(static fn (): int => $now);
|
||||
$result = $service->prune();
|
||||
|
||||
$assert($result === [
|
||||
'terminal_outboxes' => 1,
|
||||
'samples' => 2,
|
||||
'baselines' => 1,
|
||||
'receipts' => 2,
|
||||
'findings' => 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])->exists()
|
||||
&& $main->table('cloud_abuse_projection_receipts')
|
||||
->whereIn('sample_key', [$receiptRecent, $receiptWithSample])
|
||||
->count() === 2,
|
||||
'Receipt retention ignored age or retained-sample protection.');
|
||||
$assert($main->table('cloud_abuse_findings')->count() === 2
|
||||
&& !$main->table('cloud_abuse_findings')
|
||||
->where('finding_key', $key('finding-resolved-old'))->exists(),
|
||||
'Finding retention removed an open/recent finding or kept an old one.');
|
||||
|
||||
// A storage failure after terminal outbox deletion must roll the log
|
||||
// transaction back, retaining both records for a later retry.
|
||||
$rollbackKey = $sample('rollback-terminal', 1, $oldBucket);
|
||||
$outbox(
|
||||
$rollbackKey,
|
||||
1,
|
||||
$oldBucket,
|
||||
'discarded',
|
||||
$telemetryCutoff - 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(),
|
||||
'Terminal outbox deletion was not rolled back with sample failure.');
|
||||
$log->statement('DROP TRIGGER fail_abuse_sample_delete');
|
||||
|
||||
// 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', [
|
||||
$key('finding-batch-one'), $key('finding-batch-two'),
|
||||
])->count() === 1,
|
||||
'Retention batch size did not bound the indexed finding pass.');
|
||||
$assert($service->prune() === [
|
||||
'terminal_outboxes' => 0,
|
||||
'samples' => 0,
|
||||
'baselines' => 0,
|
||||
'receipts' => 0,
|
||||
'findings' => 1,
|
||||
], '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::READ_MODEL_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')
|
||||
->where('sample_key', $receiptKeys[3])->exists()
|
||||
&& $main->table('cloud_abuse_projection_receipts')
|
||||
->whereIn('sample_key', [$receiptKeys[1], $receiptKeys[2]])
|
||||
->count() === 2,
|
||||
'A protected receipt prefix starved a later orphan receipt.');
|
||||
$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.');
|
||||
|
||||
foreach ([0, CloudAbuseRetentionService::DEFAULT_BATCH_SIZE + 1] as $limit) {
|
||||
try {
|
||||
$service->prune($limit);
|
||||
throw new RuntimeException('Invalid retention batch size was accepted.');
|
||||
} catch (InvalidArgumentException) {
|
||||
// Expected.
|
||||
}
|
||||
}
|
||||
|
||||
echo "Cloud abuse retention service tests passed.\n";
|
||||
Loading…
Reference in New Issue