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
6.8 KiB
PHP
199 lines
6.8 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAbuseAlertEventCoordinator;
|
|
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->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$main = $capsule->connection('default');
|
|
$main->statement('PRAGMA foreign_keys = ON');
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
});
|
|
$schema->create('cloud_abuse_alert_events',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('event_key')->unique();
|
|
$table->string('sample_key')->unique();
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->integer('host_id');
|
|
$table->integer('bucket_start');
|
|
$table->integer('rule_version');
|
|
$table->integer('policy_revision');
|
|
$table->string('policy_hash');
|
|
$table->string('transition_digest');
|
|
$table->integer('transition_count');
|
|
$table->string('highest_severity');
|
|
$table->integer('created_at');
|
|
});
|
|
$schema->create('cloud_abuse_alert_event_items',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('event_id');
|
|
$table->string('item_key')->unique();
|
|
$table->string('finding_key');
|
|
$table->integer('occurrence_generation');
|
|
$table->integer('transition_revision');
|
|
$table->string('rule_code');
|
|
$table->string('direction');
|
|
$table->string('ip_family');
|
|
$table->string('transition_type');
|
|
$table->string('from_severity');
|
|
$table->string('to_severity');
|
|
$table->text('evidence_json');
|
|
$table->string('resolution_reason')->default('');
|
|
$table->integer('occurred_at');
|
|
$table->integer('created_at');
|
|
$table->foreign('event_id')->references('id')
|
|
->on('cloud_abuse_alert_events')->cascadeOnDelete();
|
|
});
|
|
|
|
$main->table('cloud_instances')->insert(['id' => 41]);
|
|
$policyHash = str_repeat('a', 64);
|
|
$claim = [
|
|
'sample_key' => str_repeat('b', 64),
|
|
'instance_id' => 41,
|
|
'user_id' => 9,
|
|
'host_id' => 3,
|
|
'bucket_start' => 120,
|
|
'rule_version' => 1,
|
|
'policy_revision' => 7,
|
|
'policy_hash' => $policyHash,
|
|
];
|
|
$transitions = [[
|
|
'finding_key' => str_repeat('c', 64),
|
|
'occurrence_generation' => 1,
|
|
'transition_revision' => 1,
|
|
'rule_code' => 'outbound_tcp_syn_sweep',
|
|
'direction' => 'outbound',
|
|
'ip_family' => 'ipv4',
|
|
'transition_type' => 'opened',
|
|
'from_severity' => 'none',
|
|
'to_severity' => 'suspicious',
|
|
'evidence' => ['tcp_syn_flows' => 900, 'unique_remote_ips' => 140],
|
|
'resolution_reason' => '',
|
|
'occurred_at' => 120,
|
|
], [
|
|
'finding_key' => str_repeat('d', 64),
|
|
'occurrence_generation' => 2,
|
|
'transition_revision' => 4,
|
|
'rule_code' => 'inbound_udp_flood',
|
|
'direction' => 'inbound',
|
|
'ip_family' => 'ipv6',
|
|
'transition_type' => 'resolved',
|
|
'from_severity' => 'high',
|
|
'to_severity' => 'none',
|
|
'evidence' => ['udp_active_flows' => 0, 'udp_new_flows' => 0],
|
|
'resolution_reason' => 'evidence_clear',
|
|
'occurred_at' => 120,
|
|
]];
|
|
|
|
$coordinator = new CloudAbuseAlertEventCoordinator();
|
|
$main->transaction(static function () use (
|
|
$coordinator,
|
|
$main,
|
|
$claim,
|
|
$transitions
|
|
): void {
|
|
$coordinator->record($main, $claim, array_reverse($transitions), 1000);
|
|
});
|
|
$event = $main->table('cloud_abuse_alert_events')->first();
|
|
$items = $main->table('cloud_abuse_alert_event_items')
|
|
->orderBy('finding_key')->get();
|
|
$assert($event !== null
|
|
&& (int) $event->transition_count === 2
|
|
&& $event->highest_severity === 'high'
|
|
&& count($items) === 2
|
|
&& $items[0]->finding_key === str_repeat('c', 64)
|
|
&& $items[0]->evidence_json
|
|
=== '{"tcp_syn_flows":900,"unique_remote_ips":140}',
|
|
'Multiple transitions were not frozen in one canonical event batch.');
|
|
|
|
$coordinator->record($main, $claim, $transitions, 1001);
|
|
$assert($main->table('cloud_abuse_alert_events')->count() === 1
|
|
&& $main->table('cloud_abuse_alert_event_items')->count() === 2,
|
|
'An identical event replay duplicated the durable ledger.');
|
|
|
|
$invalidEvidence = $transitions;
|
|
$invalidEvidence[0]['evidence'] = ['remote_ip' => '203.0.113.4'];
|
|
$threw = false;
|
|
try {
|
|
$coordinator->record($main, $claim + [
|
|
'sample_key' => str_repeat('e', 64),
|
|
], $invalidEvidence, 1002);
|
|
} catch (LogicException) {
|
|
$threw = true;
|
|
}
|
|
$assert($threw && $main->table('cloud_abuse_alert_events')->count() === 1,
|
|
'Non-aggregate evidence was accepted by the alert ledger.');
|
|
|
|
$wrongBucket = $transitions;
|
|
$wrongBucket[0]['occurred_at'] = 60;
|
|
$threw = false;
|
|
try {
|
|
$coordinator->record($main, $claim + [
|
|
'sample_key' => str_repeat('f', 64),
|
|
], $wrongBucket, 1003);
|
|
} catch (LogicException) {
|
|
$threw = true;
|
|
}
|
|
$assert($threw, 'A transition from another sample bucket was accepted.');
|
|
|
|
$rollbackClaim = $claim;
|
|
$rollbackClaim['sample_key'] = str_repeat('1', 64);
|
|
$rolledBack = false;
|
|
try {
|
|
$main->transaction(static function () use (
|
|
$coordinator,
|
|
$main,
|
|
$rollbackClaim,
|
|
$transitions
|
|
): void {
|
|
$coordinator->record(
|
|
$main,
|
|
$rollbackClaim,
|
|
$transitions,
|
|
1004
|
|
);
|
|
throw new RuntimeException('force rollback');
|
|
});
|
|
} catch (RuntimeException $error) {
|
|
$rolledBack = $error->getMessage() === 'force rollback';
|
|
}
|
|
$assert($rolledBack
|
|
&& !$main->table('cloud_abuse_alert_events')
|
|
->where('sample_key', $rollbackClaim['sample_key'])->exists(),
|
|
'A rolled-back alert event left a partial durable ledger.');
|
|
|
|
$main->table('cloud_instances')->where('id', 41)->delete();
|
|
$assert($main->table('cloud_abuse_alert_events')->count() === 1
|
|
&& $main->table('cloud_abuse_alert_event_items')->count() === 2,
|
|
'Deleting an instance erased an alert before delivery finality exists.');
|
|
$main->table('cloud_abuse_alert_events')->where('id', $event->id)->delete();
|
|
$assert($main->table('cloud_abuse_alert_event_items')->count() === 0,
|
|
'Deleting an alert event did not cascade its immutable items.');
|
|
$source = file_get_contents(dirname(__DIR__)
|
|
. '/app/Services/Cloud/CloudAbuseAlertEventCoordinator.php');
|
|
$assert(is_string($source)
|
|
&& !str_contains($source, 'Telegram')
|
|
&& !str_contains($source, 'TicketTelegram')
|
|
&& !str_contains($source, 'Http::'),
|
|
'Phase-6 event recording gained an external delivery dependency.');
|
|
|
|
echo "Cloud abuse alert event coordinator tests passed.\n";
|