|
|
|
|
@ -41,7 +41,7 @@ try {
|
|
|
|
|
public int $ticks = 0;
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
$this->ticks++;
|
|
|
|
|
++$this->ticks;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public function observationErrors(): array
|
|
|
|
|
@ -49,25 +49,13 @@ try {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$projection = new class {
|
|
|
|
|
public int $ticks = 0;
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
$this->ticks++;
|
|
|
|
|
return $this->ticks <= 2;
|
|
|
|
|
}
|
|
|
|
|
public function observationErrors(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$retention = new class {
|
|
|
|
|
public int $runs = 0;
|
|
|
|
|
public function prune(): array
|
|
|
|
|
{
|
|
|
|
|
$this->runs++;
|
|
|
|
|
return ['terminal_outboxes' => 0, 'samples' => 1,
|
|
|
|
|
'baselines' => 0, 'receipts' => 0, 'findings' => 0];
|
|
|
|
|
++$this->runs;
|
|
|
|
|
return ['daily_summaries' => 1, 'resolved_findings' => 0,
|
|
|
|
|
'alert_events' => 0, 'deliveries' => 0];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$heartbeat = CloudRuntimeHeartbeat::start(
|
|
|
|
|
@ -80,7 +68,6 @@ try {
|
|
|
|
|
['cloudAbuseMonitor', '--once'],
|
|
|
|
|
$heartbeat,
|
|
|
|
|
$ingest,
|
|
|
|
|
$projection,
|
|
|
|
|
$retention,
|
|
|
|
|
static fn (): int => $now,
|
|
|
|
|
static function (int $_microseconds): void {
|
|
|
|
|
@ -96,13 +83,12 @@ try {
|
|
|
|
|
);
|
|
|
|
|
$assert($status === 0
|
|
|
|
|
&& $ingest->ticks === 1
|
|
|
|
|
&& $projection->ticks === 3
|
|
|
|
|
&& $retention->runs === 1
|
|
|
|
|
&& $record['kind'] === 'abuse_monitor'
|
|
|
|
|
&& $record['state'] === 'stopped'
|
|
|
|
|
&& $record['last_success'] === $now
|
|
|
|
|
&& $record['unresolved_error'] === false,
|
|
|
|
|
'Observe-only worker did not drain its bounded burst cleanly.');
|
|
|
|
|
'Direct observe-only worker did not complete cleanly.');
|
|
|
|
|
|
|
|
|
|
$failureDirectory = $temporary . '-failure';
|
|
|
|
|
mkdir($failureDirectory, 0700, true);
|
|
|
|
|
@ -116,21 +102,11 @@ try {
|
|
|
|
|
return ['network_observation_collection_failed'];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$idleProjection = new class {
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public function observationErrors(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$idleRetention = new class {
|
|
|
|
|
public function prune(): array
|
|
|
|
|
{
|
|
|
|
|
return ['terminal_outboxes' => 0, 'samples' => 0,
|
|
|
|
|
'baselines' => 0, 'receipts' => 0, 'findings' => 0];
|
|
|
|
|
return ['daily_summaries' => 0, 'resolved_findings' => 0,
|
|
|
|
|
'alert_events' => 0, 'deliveries' => 0];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$failedHeartbeat = CloudRuntimeHeartbeat::start(
|
|
|
|
|
@ -143,7 +119,6 @@ try {
|
|
|
|
|
['cloudAbuseMonitor', '--once'],
|
|
|
|
|
$failedHeartbeat,
|
|
|
|
|
$failedIngest,
|
|
|
|
|
$idleProjection,
|
|
|
|
|
$idleRetention,
|
|
|
|
|
static fn (): int => $now
|
|
|
|
|
);
|
|
|
|
|
@ -161,11 +136,12 @@ try {
|
|
|
|
|
&& $failedRecord['last_error']
|
|
|
|
|
=== 'abuse_monitor_collection_failed'
|
|
|
|
|
&& $failedRecord['unresolved_error'] === true,
|
|
|
|
|
'A caught collection failure was falsely projected healthy.');
|
|
|
|
|
'A caught collection failure was falsely reported healthy.');
|
|
|
|
|
$cleanup($failureDirectory);
|
|
|
|
|
|
|
|
|
|
$safeDirectory = $temporary . '-safe-projection';
|
|
|
|
|
mkdir($safeDirectory, 0700, true);
|
|
|
|
|
// A successful zero-work retry must clear a prior retention warning.
|
|
|
|
|
$recoveryDirectory = $temporary . '-retention-recovery';
|
|
|
|
|
mkdir($recoveryDirectory, 0700, true);
|
|
|
|
|
$idleIngest = new class {
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
@ -176,92 +152,6 @@ try {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$safeProjection = new class {
|
|
|
|
|
public int $ticks = 0;
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
return ++$this->ticks === 1;
|
|
|
|
|
}
|
|
|
|
|
public function observationErrors(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->ticks === 1
|
|
|
|
|
? ['instance_deleted'] : [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$safeHeartbeat = CloudRuntimeHeartbeat::start(
|
|
|
|
|
'abuse_monitor',
|
|
|
|
|
true,
|
|
|
|
|
$safeDirectory,
|
|
|
|
|
static fn (): int => $now
|
|
|
|
|
);
|
|
|
|
|
$safeStatus = CloudAbuseMonitor::run(
|
|
|
|
|
['cloudAbuseMonitor', '--once'],
|
|
|
|
|
$safeHeartbeat,
|
|
|
|
|
$idleIngest,
|
|
|
|
|
$safeProjection,
|
|
|
|
|
$idleRetention,
|
|
|
|
|
static fn (): int => $now
|
|
|
|
|
);
|
|
|
|
|
$safeFiles = iterator_to_array(new FilesystemIterator($safeDirectory));
|
|
|
|
|
$safeRecord = json_decode(
|
|
|
|
|
file_get_contents(reset($safeFiles)->getPathname()),
|
|
|
|
|
true,
|
|
|
|
|
16,
|
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
|
);
|
|
|
|
|
$assert($safeStatus === 0
|
|
|
|
|
&& $safeRecord['unresolved_error'] === false,
|
|
|
|
|
'A safely converged projection discard raised a worker failure.');
|
|
|
|
|
$cleanup($safeDirectory);
|
|
|
|
|
|
|
|
|
|
$conflictDirectory = $temporary . '-projection-conflict';
|
|
|
|
|
mkdir($conflictDirectory, 0700, true);
|
|
|
|
|
$conflictProjection = new class {
|
|
|
|
|
public int $ticks = 0;
|
|
|
|
|
public function tick(): bool
|
|
|
|
|
{
|
|
|
|
|
return ++$this->ticks === 1;
|
|
|
|
|
}
|
|
|
|
|
public function observationErrors(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->ticks === 1
|
|
|
|
|
? ['projection_order_conflict'] : [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$conflictHeartbeat = CloudRuntimeHeartbeat::start(
|
|
|
|
|
'abuse_monitor',
|
|
|
|
|
true,
|
|
|
|
|
$conflictDirectory,
|
|
|
|
|
static fn (): int => $now
|
|
|
|
|
);
|
|
|
|
|
$conflictStatus = CloudAbuseMonitor::run(
|
|
|
|
|
['cloudAbuseMonitor', '--once'],
|
|
|
|
|
$conflictHeartbeat,
|
|
|
|
|
$idleIngest,
|
|
|
|
|
$conflictProjection,
|
|
|
|
|
$idleRetention,
|
|
|
|
|
static fn (): int => $now
|
|
|
|
|
);
|
|
|
|
|
$conflictFiles = iterator_to_array(
|
|
|
|
|
new FilesystemIterator($conflictDirectory)
|
|
|
|
|
);
|
|
|
|
|
$conflictRecord = json_decode(
|
|
|
|
|
file_get_contents(reset($conflictFiles)->getPathname()),
|
|
|
|
|
true,
|
|
|
|
|
16,
|
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
|
);
|
|
|
|
|
$assert($conflictStatus === 1
|
|
|
|
|
&& $conflictRecord['last_error']
|
|
|
|
|
=== 'abuse_monitor_projection_failed'
|
|
|
|
|
&& $conflictRecord['unresolved_error'] === true,
|
|
|
|
|
'A projection ordering conflict was silently treated as safe.');
|
|
|
|
|
$cleanup($conflictDirectory);
|
|
|
|
|
|
|
|
|
|
// A stage clears its own warning after a successful zero-work retry; it
|
|
|
|
|
// must not depend on unrelated collection or projection work.
|
|
|
|
|
$recoveryDirectory = $temporary . '-retention-recovery';
|
|
|
|
|
mkdir($recoveryDirectory, 0700, true);
|
|
|
|
|
$recoveringRetention = new class {
|
|
|
|
|
public int $runs = 0;
|
|
|
|
|
public function prune(): array
|
|
|
|
|
@ -269,8 +159,8 @@ try {
|
|
|
|
|
if (++$this->runs === 1) {
|
|
|
|
|
throw new RuntimeException('simulated retention outage');
|
|
|
|
|
}
|
|
|
|
|
return ['terminal_outboxes' => 0, 'samples' => 0,
|
|
|
|
|
'baselines' => 0, 'receipts' => 0, 'findings' => 0];
|
|
|
|
|
return ['daily_summaries' => 0, 'resolved_findings' => 0,
|
|
|
|
|
'alert_events' => 0, 'deliveries' => 0];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$recoveryNow = $now;
|
|
|
|
|
@ -288,7 +178,6 @@ try {
|
|
|
|
|
['cloudAbuseMonitor'],
|
|
|
|
|
$recoveryHeartbeat,
|
|
|
|
|
$idleIngest,
|
|
|
|
|
$idleProjection,
|
|
|
|
|
$recoveringRetention,
|
|
|
|
|
static function () use (&$recoveryNow): int {
|
|
|
|
|
return $recoveryNow;
|
|
|
|
|
@ -316,7 +205,7 @@ try {
|
|
|
|
|
$assert($row['state'] === 'idle'
|
|
|
|
|
&& $row['unresolved_error'] === false
|
|
|
|
|
&& $row['last_success'] === $recoveryNow - 301,
|
|
|
|
|
'Successful zero-work retention did not clear its warning.');
|
|
|
|
|
'Successful zero-work retention did not clear warning.');
|
|
|
|
|
throw new RuntimeException('stop daemon fixture');
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
@ -340,18 +229,19 @@ try {
|
|
|
|
|
$deployment = $read('docs/DEPLOYMENT.md');
|
|
|
|
|
$assert(str_contains($xcat, "'cloudAbuseMonitor'")
|
|
|
|
|
&& str_contains($xcat, 'CloudAbuseMonitor::run')
|
|
|
|
|
&& str_contains($command, 'PROJECTION_BURST = 64')
|
|
|
|
|
&& str_contains($command, 'CloudAbuseObservationIngestService')
|
|
|
|
|
&& str_contains($command, 'CloudAbuseProjectionService')
|
|
|
|
|
&& str_contains($command, 'CloudAbuseRetentionService')
|
|
|
|
|
&& !str_contains($command, 'CloudAbuseProjectionService')
|
|
|
|
|
&& !str_contains($command, 'PROJECTION_BURST')
|
|
|
|
|
&& !str_contains($command, "table('cloud_jobs')")
|
|
|
|
|
&& !str_contains($command, 'CloudJob'),
|
|
|
|
|
'Monitor command is unreachable, unbounded, or coupled to jobs.');
|
|
|
|
|
'Monitor command retained projection or job-queue coupling.');
|
|
|
|
|
$assert(str_contains($heartbeatSource, "'abuse_monitor'")
|
|
|
|
|
&& str_contains($heartbeatSource, "'abuse_monitor_ingest'")
|
|
|
|
|
&& !str_contains($heartbeatSource, 'abuse_monitor_projection')
|
|
|
|
|
&& str_contains($runtime,
|
|
|
|
|
"'abuse_monitor' => '网络滥用监测'"),
|
|
|
|
|
'Runtime observation does not expose the independent monitor.');
|
|
|
|
|
'Runtime observation retained a projection phase.');
|
|
|
|
|
$assert(str_contains($unit,
|
|
|
|
|
'ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudAbuseMonitor')
|
|
|
|
|
&& str_contains($unit, 'NoNewPrivileges=true')
|
|
|
|
|
@ -359,9 +249,9 @@ try {
|
|
|
|
|
&& str_contains($unit, 'LimitCORE=0')
|
|
|
|
|
&& str_contains($deployment,
|
|
|
|
|
'virt-cloud-abuse-monitor.service'),
|
|
|
|
|
'Monitor systemd isolation or deployment documentation is incomplete.');
|
|
|
|
|
'Monitor systemd isolation or deployment docs are incomplete.');
|
|
|
|
|
} finally {
|
|
|
|
|
$cleanup($temporary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "Observe-only abuse monitor worker contract tests passed.\n";
|
|
|
|
|
echo "Direct abuse monitor worker contract tests passed.\n";
|
|
|
|
|
|