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.
106 lines
4.0 KiB
PHP
106 lines
4.0 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAbuseDetectionPolicy;
|
|
|
|
$root = dirname(__DIR__);
|
|
$read = static function (string $path) use ($root): string {
|
|
$value = file_get_contents($root . '/' . ltrim($path, '/'));
|
|
if (!is_string($value)) {
|
|
throw new RuntimeException('Unable to read ' . $path);
|
|
}
|
|
return $value;
|
|
};
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$tableSql = static function (string $schema, string $table) use ($assert):
|
|
string {
|
|
$matched = preg_match(
|
|
'/CREATE TABLE ' . preg_quote($table, '/')
|
|
. ' \(([\s\S]*?)\n\) ENGINE=InnoDB/',
|
|
$schema,
|
|
$match
|
|
) === 1;
|
|
$assert($matched, 'Missing abuse policy table: ' . $table);
|
|
return $match[1];
|
|
};
|
|
|
|
$schema = $read('sql/cloud.sql');
|
|
$setup = $read('app/Command/CloudSetup.php');
|
|
$service = $read('app/Services/Cloud/CloudAbusePolicyService.php');
|
|
$revision = $tableSql($schema, 'cloud_abuse_policy_revisions');
|
|
$rules = $tableSql($schema, 'cloud_abuse_policy_rules');
|
|
|
|
foreach ([
|
|
'id', 'revision', 'detector_version', 'policy_hash', 'active_slot',
|
|
'warmup_windows', 'suspicious_windows', 'high_windows',
|
|
'clear_windows', 'published_by', 'published_at', 'created_at',
|
|
] as $column) {
|
|
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
|
|
$revision) === 1 && str_contains($setup, "'" . $column . "'"),
|
|
'Missing abuse policy revision column: ' . $column);
|
|
}
|
|
foreach ([
|
|
'id', 'policy_revision_id', 'rule_code', 'direction', 'protocol',
|
|
'enabled', 'suspicious_thresholds_json', 'high_thresholds_json',
|
|
'created_at',
|
|
] as $column) {
|
|
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
|
|
$rules) === 1 && str_contains($setup, "'" . $column . "'"),
|
|
'Missing abuse policy rule column: ' . $column);
|
|
}
|
|
foreach ([
|
|
'cloud_abuse_policy_revision', 'cloud_abuse_policy_active',
|
|
'cloud_abuse_policy_hash', 'chk_cloud_abuse_policy_hash',
|
|
'chk_cloud_abuse_policy_active', 'chk_cloud_abuse_policy_windows',
|
|
] as $object) {
|
|
$assert(str_contains($revision, $object)
|
|
&& str_contains($setup, "'" . $object . "'"),
|
|
'Missing abuse policy revision object: ' . $object);
|
|
}
|
|
foreach ([
|
|
'cloud_abuse_policy_rule_revision', 'cloud_abuse_policy_rule_code',
|
|
'chk_cloud_abuse_policy_rule_direction',
|
|
'chk_cloud_abuse_policy_rule_protocol',
|
|
'chk_cloud_abuse_policy_rule_enabled',
|
|
'fk_cloud_abuse_policy_rule_revision',
|
|
] as $object) {
|
|
$assert(str_contains($rules, $object)
|
|
&& str_contains($setup, "'" . $object . "'"),
|
|
'Missing abuse policy rule object: ' . $object);
|
|
}
|
|
$defaults = CloudAbuseDetectionPolicy::defaults();
|
|
$compactSchema = preg_replace('/\s+/', ' ', $schema);
|
|
$assert(is_string($compactSchema)
|
|
&& str_contains($compactSchema, "'" . $defaults->hash() . "'"),
|
|
'The fresh schema default policy hash drifted from the PHP catalog.');
|
|
foreach ($defaults->rules() as $code => $rule) {
|
|
$signals = json_encode($rule['signals'], JSON_THROW_ON_ERROR
|
|
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
$high = json_encode($rule['high'], JSON_THROW_ON_ERROR
|
|
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
$seed = sprintf(
|
|
"(1, '%s', '%s', '%s', %d, '%s', '%s', 0)",
|
|
$code,
|
|
$rule['direction'],
|
|
$rule['protocol'],
|
|
$rule['enabled'] ? 1 : 0,
|
|
$signals,
|
|
$high
|
|
);
|
|
$assert(str_contains($compactSchema, $seed),
|
|
'The fresh schema rule seed drifted from PHP: ' . $code);
|
|
}
|
|
$assert(str_contains($setup,
|
|
"SCHEMA_VERSION = 'go-agent-wss-v47-offer-billing-cycles'")
|
|
&& str_contains($service, "->where('active_slot', 1)->lockForUpdate()")
|
|
&& str_contains($service, "->where('active_slot', 1)->update")
|
|
&& str_contains($service, 'insertGetId'),
|
|
'The version boundary or optimistic publication fence is incomplete.');
|
|
|
|
echo "Versioned network abuse policy schema contracts passed.\n";
|