|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Services\Cloud\CloudAbuseDetectionPolicy;
|
|
|
|
|
use App\Services\Cloud\CloudAbusePolicyService;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$expectCloudError = static function (
|
|
|
|
|
callable $callback,
|
|
|
|
|
int $status,
|
|
|
|
|
string $message
|
|
|
|
|
) use ($assert): void {
|
|
|
|
|
try {
|
|
|
|
|
$callback();
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === $status,
|
|
|
|
|
$message . ' Unexpected status: ' . $error->httpStatus);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
$schema->create('cloud_abuse_policy_revisions', static function (
|
|
|
|
|
Blueprint $table
|
|
|
|
|
): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->unsignedBigInteger('revision')->unique();
|
|
|
|
|
$table->unsignedInteger('detector_version');
|
|
|
|
|
$table->string('policy_hash', 64);
|
|
|
|
|
$table->unsignedTinyInteger('active_slot')->nullable()->unique();
|
|
|
|
|
$table->unsignedSmallInteger('warmup_windows');
|
|
|
|
|
$table->unsignedSmallInteger('suspicious_windows');
|
|
|
|
|
$table->unsignedSmallInteger('high_windows');
|
|
|
|
|
$table->unsignedSmallInteger('clear_windows');
|
|
|
|
|
$table->unsignedBigInteger('published_by')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('published_at');
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_abuse_policy_rules', static function (
|
|
|
|
|
Blueprint $table
|
|
|
|
|
): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->unsignedBigInteger('policy_revision_id');
|
|
|
|
|
$table->string('rule_code', 48);
|
|
|
|
|
$table->string('direction', 16);
|
|
|
|
|
$table->string('protocol', 8);
|
|
|
|
|
$table->unsignedTinyInteger('enabled');
|
|
|
|
|
$table->text('suspicious_thresholds_json');
|
|
|
|
|
$table->text('high_thresholds_json');
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
$table->unique(['policy_revision_id', 'rule_code']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$connection = $capsule->getConnection('default');
|
|
|
|
|
$defaults = CloudAbuseDetectionPolicy::defaults();
|
|
|
|
|
$revisionId = $connection->table('cloud_abuse_policy_revisions')
|
|
|
|
|
->insertGetId([
|
|
|
|
|
'revision' => 1,
|
|
|
|
|
'detector_version' => $defaults->engineVersion(),
|
|
|
|
|
'policy_hash' => $defaults->hash(),
|
|
|
|
|
'active_slot' => 1,
|
|
|
|
|
'warmup_windows' => $defaults->windows()['warmup'],
|
|
|
|
|
'suspicious_windows' => $defaults->windows()['suspicious'],
|
|
|
|
|
'high_windows' => $defaults->windows()['high'],
|
|
|
|
|
'clear_windows' => $defaults->windows()['clear'],
|
|
|
|
|
'published_by' => null,
|
|
|
|
|
'published_at' => 0,
|
|
|
|
|
'created_at' => 0,
|
|
|
|
|
]);
|
|
|
|
|
foreach ($defaults->rules() as $code => $rule) {
|
|
|
|
|
$connection->table('cloud_abuse_policy_rules')->insert([
|
|
|
|
|
'policy_revision_id' => $revisionId,
|
|
|
|
|
'rule_code' => $code,
|
|
|
|
|
'direction' => $rule['direction'],
|
|
|
|
|
'protocol' => $rule['protocol'],
|
|
|
|
|
'enabled' => $rule['enabled'] ? 1 : 0,
|
|
|
|
|
'suspicious_thresholds_json' => json_encode(
|
|
|
|
|
$rule['signals'], JSON_THROW_ON_ERROR
|
|
|
|
|
),
|
|
|
|
|
'high_thresholds_json' => json_encode(
|
|
|
|
|
$rule['high'], JSON_THROW_ON_ERROR
|
|
|
|
|
),
|
|
|
|
|
'created_at' => 0,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$service = new CloudAbusePolicyService(static fn (): int => 1700000000);
|
|
|
|
|
$active = $service->activeSnapshot();
|
|
|
|
|
$assert($active->revision === 1
|
|
|
|
|
&& $active->publishedBy === null
|
|
|
|
|
&& $active->policy->definition() === $defaults->definition()
|
|
|
|
|
&& hash_equals($active->policyHash, $defaults->hash()),
|
|
|
|
|
'The seeded immutable policy could not be reconstructed exactly.');
|
|
|
|
|
|
|
|
|
|
$same = $service->publish($defaults->definition(), 1, 7);
|
|
|
|
|
$assert($same->revision === 1
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_revisions')->count() === 1,
|
|
|
|
|
'Publishing the unchanged active policy created a redundant revision.');
|
|
|
|
|
|
|
|
|
|
$changed = $defaults->definition();
|
|
|
|
|
$changed['windows']['suspicious'] = 4;
|
|
|
|
|
$changed['rules']['outbound_udp_fanout']['enabled'] = false;
|
|
|
|
|
$published = $service->publish($changed, 1, 7);
|
|
|
|
|
$assert($published->revision === 2
|
|
|
|
|
&& $published->publishedBy === 7
|
|
|
|
|
&& $published->publishedAt === 1700000000
|
|
|
|
|
&& $published->policy->definition() === $changed
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_revisions')
|
|
|
|
|
->where('active_slot', 1)->value('revision') === 2
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_revisions')
|
|
|
|
|
->where('revision', 1)->value('active_slot') === null
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_rules')->count() === 12,
|
|
|
|
|
'Publishing did not atomically preserve history and activate revision 2.');
|
|
|
|
|
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
static fn () => $service->publish($defaults->definition(), 1, 9),
|
|
|
|
|
409,
|
|
|
|
|
'A stale administrator revision overwrote the active abuse policy.'
|
|
|
|
|
);
|
|
|
|
|
$invalid = $published->policy->definition();
|
|
|
|
|
$invalid['rules']['outbound_tcp_syn_sweep']['high']['tcp_syn_flows'] = 10;
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
static fn () => $service->publish($invalid, 2, 9),
|
|
|
|
|
422,
|
|
|
|
|
'A high-risk threshold below its suspicious floor was published.'
|
|
|
|
|
);
|
|
|
|
|
$assert($service->activeSnapshot()->revision === 2,
|
|
|
|
|
'Rejected policy input changed the active revision.');
|
|
|
|
|
$historical = $service->snapshotByIdentity(1, $defaults->hash());
|
|
|
|
|
$assert($historical->revision === 1
|
|
|
|
|
&& $historical->policy->definition() === $defaults->definition(),
|
|
|
|
|
'Historical immutable policy lookup followed the current active slot.');
|
|
|
|
|
$expectCloudError(
|
|
|
|
|
static fn () => $service->snapshotByIdentity(
|
|
|
|
|
1,
|
|
|
|
|
$published->policyHash
|
|
|
|
|
),
|
|
|
|
|
409,
|
|
|
|
|
'Historical policy lookup accepted a mismatched immutable hash.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$connection->unprepared(
|
|
|
|
|
"CREATE TRIGGER abort_abuse_policy_rule BEFORE INSERT "
|
|
|
|
|
. "ON cloud_abuse_policy_rules "
|
|
|
|
|
. "WHEN NEW.rule_code = 'inbound_udp_flood' "
|
|
|
|
|
. "BEGIN SELECT RAISE(ABORT, 'injected failure'); END"
|
|
|
|
|
);
|
|
|
|
|
$atomicCandidate = $published->policy->definition();
|
|
|
|
|
$atomicCandidate['windows']['clear'] = 6;
|
|
|
|
|
$failed = false;
|
|
|
|
|
try {
|
|
|
|
|
$service->publish($atomicCandidate, 2, 11);
|
|
|
|
|
} catch (Throwable) {
|
|
|
|
|
$failed = true;
|
|
|
|
|
}
|
|
|
|
|
$assert($failed
|
|
|
|
|
&& $service->activeSnapshot()->revision === 2
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_revisions')->count() === 2
|
|
|
|
|
&& $connection->table('cloud_abuse_policy_rules')->count() === 12,
|
|
|
|
|
'A partial policy-rule insert deactivated the last valid revision.');
|
|
|
|
|
|
|
|
|
|
echo "Versioned network abuse policy publication tests passed.\n";
|