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.
199 lines
7.1 KiB
PHP
199 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Services\Cloud\CloudAbuseObservationIngestService;
|
|
use App\Services\Cloud\CloudAbuseProjectionService;
|
|
use App\Services\Cloud\CloudAbuseRetentionService;
|
|
use App\Services\Cloud\CloudRuntimeHeartbeat;
|
|
|
|
/**
|
|
* Independent, observe-only network-abuse monitor.
|
|
*
|
|
* Collection and projection deliberately stay outside cloud_jobs: every host
|
|
* call is read-only and every database write is an idempotent telemetry or
|
|
* read-model update. No lifecycle, billing, firewall or task mutation belongs
|
|
* in this loop.
|
|
*/
|
|
final class CloudAbuseMonitor
|
|
{
|
|
private const PROJECTION_BURST = 64;
|
|
private const RETENTION_INTERVAL_SECONDS = 300;
|
|
private const IDLE_SLEEP_MICROSECONDS = 1_000_000;
|
|
private const SAFE_PROJECTION_DIAGNOSTICS = [
|
|
'instance_deleted',
|
|
'instance_owner_changed',
|
|
'projection_receipt_baseline_reset',
|
|
];
|
|
|
|
/**
|
|
* Injectable objects and clock keep the command testable without turning
|
|
* the production services into interfaces solely for the command layer.
|
|
*
|
|
* @param null|object $ingest must expose tick() and observationErrors()
|
|
* @param null|object $projection must expose tick() and observationErrors()
|
|
* @param null|object $retention must expose prune()
|
|
* @param null|callable():int $clock
|
|
* @param null|callable(int):void $sleep
|
|
*/
|
|
public static function run(
|
|
array $argv = [],
|
|
?CloudRuntimeHeartbeat $heartbeat = null,
|
|
?object $ingest = null,
|
|
?object $projection = null,
|
|
?object $retention = null,
|
|
?callable $clock = null,
|
|
?callable $sleep = null
|
|
): int {
|
|
$once = in_array('--once', $argv, true);
|
|
$heartbeat ??= CloudRuntimeHeartbeat::start(
|
|
'abuse_monitor',
|
|
$once
|
|
);
|
|
$ingest ??= new CloudAbuseObservationIngestService();
|
|
$projection ??= new CloudAbuseProjectionService();
|
|
$retention ??= new CloudAbuseRetentionService();
|
|
$clock ??= static fn (): int => time();
|
|
$sleep ??= static function (int $microseconds): void {
|
|
usleep($microseconds);
|
|
};
|
|
self::assertWorker($ingest, ['tick', 'observationErrors']);
|
|
self::assertWorker($projection, ['tick', 'observationErrors']);
|
|
self::assertWorker($retention, ['prune']);
|
|
|
|
$nextRetentionAt = 0;
|
|
$unresolved = [
|
|
'collection' => '',
|
|
'projection' => '',
|
|
'retention' => '',
|
|
];
|
|
try {
|
|
do {
|
|
$worked = false;
|
|
|
|
$heartbeat->busy('abuse_monitor_ingest');
|
|
try {
|
|
$worked = $ingest->tick() === true || $worked;
|
|
$unresolved['collection'] =
|
|
$ingest->observationErrors() === []
|
|
? '' : 'abuse_monitor_collection_failed';
|
|
} catch (\Throwable $error) {
|
|
$unresolved['collection'] =
|
|
'abuse_monitor_collection_failed';
|
|
self::writeFailure('collection', $error);
|
|
}
|
|
|
|
$heartbeat->busy('abuse_monitor_projection');
|
|
try {
|
|
$projectionFailed = false;
|
|
for ($projected = 0;
|
|
$projected < self::PROJECTION_BURST;
|
|
$projected++
|
|
) {
|
|
if ($projection->tick() !== true) {
|
|
break;
|
|
}
|
|
$worked = true;
|
|
$projectionFailed = self::hasProjectionFailure(
|
|
$projection->observationErrors()
|
|
) || $projectionFailed;
|
|
}
|
|
$unresolved['projection'] = $projectionFailed
|
|
? 'abuse_monitor_projection_failed' : '';
|
|
} catch (\Throwable $error) {
|
|
$unresolved['projection'] =
|
|
'abuse_monitor_projection_failed';
|
|
self::writeFailure('projection', $error);
|
|
}
|
|
|
|
$now = $clock();
|
|
if (!is_int($now) || $now < 1) {
|
|
throw new \LogicException(
|
|
'Abuse monitor command clock is invalid'
|
|
);
|
|
}
|
|
if ($now >= $nextRetentionAt) {
|
|
$heartbeat->busy('abuse_monitor_retention');
|
|
try {
|
|
$pruned = $retention->prune();
|
|
if (!is_array($pruned)) {
|
|
throw new \RuntimeException(
|
|
'Abuse retention returned an invalid result'
|
|
);
|
|
}
|
|
$worked = array_sum(array_map(
|
|
static fn (mixed $value): int =>
|
|
is_int($value) && $value > 0 ? $value : 0,
|
|
$pruned
|
|
)) > 0 || $worked;
|
|
$unresolved['retention'] = '';
|
|
} catch (\Throwable $error) {
|
|
$unresolved['retention'] =
|
|
'abuse_monitor_retention_failed';
|
|
self::writeFailure('retention', $error);
|
|
}
|
|
$nextRetentionAt = $now
|
|
+ self::RETENTION_INTERVAL_SECONDS;
|
|
}
|
|
|
|
$errors = array_values(array_filter($unresolved));
|
|
if ($errors !== []) {
|
|
$heartbeat->error($errors[0]);
|
|
} else {
|
|
$heartbeat->success();
|
|
}
|
|
if ($once) {
|
|
return $errors === [] ? 0 : 1;
|
|
}
|
|
if (!$worked) {
|
|
$sleep(self::IDLE_SLEEP_MICROSECONDS);
|
|
}
|
|
} while (true);
|
|
} catch (\Throwable $error) {
|
|
$heartbeat->error('abuse_monitor_runtime_failed');
|
|
throw $error;
|
|
} finally {
|
|
$heartbeat->stopped();
|
|
}
|
|
}
|
|
|
|
/** @param list<string> $methods */
|
|
private static function assertWorker(object $worker, array $methods): void
|
|
{
|
|
foreach ($methods as $method) {
|
|
if (!is_callable([$worker, $method])) {
|
|
throw new \LogicException(
|
|
'Abuse monitor dependency is invalid'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function writeFailure(
|
|
string $stage,
|
|
\Throwable $error
|
|
): void {
|
|
fwrite(
|
|
STDERR,
|
|
'abuse monitor ' . $stage . ' temporarily unavailable: '
|
|
. get_class($error) . PHP_EOL
|
|
);
|
|
}
|
|
|
|
/** @param list<mixed> $diagnostics */
|
|
private static function hasProjectionFailure(array $diagnostics): bool
|
|
{
|
|
foreach ($diagnostics as $diagnostic) {
|
|
if (!is_string($diagnostic)
|
|
|| !in_array(
|
|
$diagnostic,
|
|
self::SAFE_PROJECTION_DIAGNOSTICS,
|
|
true
|
|
)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|