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.
166 lines
7.1 KiB
PHP
166 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Development-only generator. Production PHP uses the generated constant and
|
|
// never needs the Agent source tree, JSON parsing, or a writable deployment.
|
|
$root = dirname(__DIR__);
|
|
$source = $root . '/agent/operation/recovery_contract.json';
|
|
$target = $root . '/web/app/Services/Cloud/CloudOperationRecoveryContract.php';
|
|
$check = ($argv[1] ?? '') === '--check';
|
|
if (count($argv) > 2 || (isset($argv[1]) && !$check)) {
|
|
fwrite(STDERR, "Usage: php tools/generate-recovery-contract.php [--check]\n");
|
|
exit(2);
|
|
}
|
|
$contract = json_decode(file_get_contents($source), true, 512,
|
|
JSON_THROW_ON_ERROR);
|
|
$hasKeys = static function (array $value, array $keys): bool {
|
|
$actual = array_keys($value);
|
|
sort($actual);
|
|
sort($keys);
|
|
return $actual === $keys;
|
|
};
|
|
if (!$hasKeys($contract, ['schema_version', 'operations'])
|
|
|| ($contract['schema_version'] ?? null) !== 1
|
|
|| !is_array($contract['operations'] ?? null)
|
|
|| !array_is_list($contract['operations'])
|
|
|| $contract['operations'] === []) {
|
|
throw new RuntimeException('Invalid recovery contract envelope.');
|
|
}
|
|
$operations = [];
|
|
foreach ($contract['operations'] as $entry) {
|
|
if (!is_array($entry) || !$hasKeys($entry, [
|
|
'kind', 'recovery_mode', 'identity_scope', 'actions',
|
|
'action_patterns', 'system_resolutions',
|
|
])) {
|
|
throw new RuntimeException('Invalid recovery operation fields.');
|
|
}
|
|
$kind = $entry['kind'] ?? null;
|
|
$mode = $entry['recovery_mode'] ?? null;
|
|
$scope = $entry['identity_scope'] ?? null;
|
|
if (!is_string($kind) || preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $kind) !== 1
|
|
|| isset($operations[$kind])
|
|
|| !in_array($mode, ['generic', 'create', 'storage', 'aggregate', 'payment'], true)
|
|
|| !in_array($scope, ['instance', 'catalog', 'create', 'storage', 'none'], true)) {
|
|
throw new RuntimeException('Invalid or duplicate recovery operation.');
|
|
}
|
|
foreach (['actions', 'action_patterns', 'system_resolutions'] as $field) {
|
|
if (!is_array($entry[$field] ?? null) || !array_is_list($entry[$field])
|
|
|| count(array_unique($entry[$field])) !== count($entry[$field])) {
|
|
throw new RuntimeException('Invalid recovery list: ' . $kind . '.' . $field);
|
|
}
|
|
foreach ($entry[$field] as $value) {
|
|
if (!is_string($value) || $value === '') {
|
|
throw new RuntimeException('Invalid recovery entry: ' . $kind . '.' . $field);
|
|
}
|
|
}
|
|
}
|
|
$resolutions = match ($mode) {
|
|
'generic' => ['applied', 'not_applied'],
|
|
'create' => ['not_applied'],
|
|
default => [],
|
|
};
|
|
$validScope = match ($mode) {
|
|
'generic' => in_array($scope, ['instance', 'catalog'], true),
|
|
'create' => in_array($scope, ['create', 'instance'], true),
|
|
'storage' => $scope === 'storage',
|
|
default => $scope === 'none',
|
|
};
|
|
if (!$validScope || $entry['system_resolutions'] !== $resolutions
|
|
|| (($scope === 'none') !== ($entry['actions'] === [] && $entry['action_patterns'] === []))) {
|
|
throw new RuntimeException('Unsafe recovery mode boundary: ' . $kind);
|
|
}
|
|
foreach ($entry['actions'] as $action) {
|
|
if (strlen($action) > 128
|
|
|| preg_match('/^[a-z][a-z0-9]*(?:[-:][a-z0-9]+)*$/D', $action) !== 1) {
|
|
throw new RuntimeException('Invalid recovery action: ' . $kind);
|
|
}
|
|
}
|
|
foreach ($entry['action_patterns'] as $pattern) {
|
|
if (!str_starts_with($pattern, '^') || !str_ends_with($pattern, '$')
|
|
|| str_contains($pattern, '~') || @preg_match('~' . $pattern . '~D', '') === false) {
|
|
throw new RuntimeException('Invalid anchored recovery pattern: ' . $kind);
|
|
}
|
|
}
|
|
unset($entry['kind']);
|
|
$operations[$kind] = $entry;
|
|
}
|
|
// The Agent authorizes by action, not PHP kind. Aliases such as delete and
|
|
// expiry_delete are legal only when they grant the same resolution capability.
|
|
$actionPermissions = [];
|
|
$patterns = [];
|
|
$literalPatternPrefix = static function (string $pattern): string {
|
|
$prefix = '';
|
|
for ($index = 1, $length = strlen($pattern); $index < $length; $index++) {
|
|
$character = $pattern[$index];
|
|
if (preg_match('/^[A-Za-z0-9_:-]$/D', $character) !== 1) {
|
|
break;
|
|
}
|
|
$prefix .= $character;
|
|
}
|
|
return $prefix;
|
|
};
|
|
foreach ($operations as $entry) {
|
|
foreach ($entry['actions'] as $action) {
|
|
if (isset($actionPermissions[$action])
|
|
&& $actionPermissions[$action] !== $entry['system_resolutions']) {
|
|
throw new RuntimeException('Conflicting recovery permissions: ' . $action);
|
|
}
|
|
$actionPermissions[$action] = $entry['system_resolutions'];
|
|
}
|
|
foreach ($entry['action_patterns'] as $pattern) {
|
|
// Different grants are safe only when anchored literal prefixes prove
|
|
// that the regular-expression languages cannot intersect.
|
|
foreach ($patterns as $existingPattern => $permissions) {
|
|
if ($permissions === $entry['system_resolutions']) {
|
|
continue;
|
|
}
|
|
$prefix = $literalPatternPrefix($pattern);
|
|
$existingPrefix = $literalPatternPrefix($existingPattern);
|
|
if ($prefix === '' || $existingPrefix === ''
|
|
|| str_starts_with($prefix, $existingPrefix)
|
|
|| str_starts_with($existingPrefix, $prefix)) {
|
|
throw new RuntimeException('Potentially conflicting patterned recovery permissions.');
|
|
}
|
|
}
|
|
$patterns[$pattern] = $entry['system_resolutions'];
|
|
}
|
|
}
|
|
foreach ($actionPermissions as $action => $permissions) {
|
|
foreach ($patterns as $pattern => $patternPermissions) {
|
|
if (preg_match('~' . $pattern . '~D', $action) === 1
|
|
&& $permissions !== $patternPermissions) {
|
|
throw new RuntimeException('Recovery pattern overrides permissions: ' . $action);
|
|
}
|
|
}
|
|
}
|
|
$lines = [];
|
|
foreach ($operations as $kind => $entry) {
|
|
$lines[] = " '$kind' => [";
|
|
foreach ($entry as $key => $value) {
|
|
$encoded = is_array($value)
|
|
? '[' . implode(', ', array_map(static fn (string $item): string => var_export($item, true), $value)) . ']'
|
|
: var_export($value, true);
|
|
$lines[] = " '$key' => $encoded,";
|
|
}
|
|
$lines[] = ' ],';
|
|
}
|
|
$output = "<?php\n\nnamespace App\\Services\\Cloud;\n\n"
|
|
. "// Generated from agent/operation/recovery_contract.json. Do not edit.\n"
|
|
. "// Regenerate: php tools/generate-recovery-contract.php\n"
|
|
. "final class CloudOperationRecoveryContract\n{\n"
|
|
. " public const OPERATIONS = [\n" . implode("\n", $lines)
|
|
. "\n ];\n}\n";
|
|
if ($check) {
|
|
if (!is_file($target) || file_get_contents($target) !== $output) {
|
|
fwrite(STDERR, "Recovery contract is stale; run php tools/generate-recovery-contract.php\n");
|
|
exit(1);
|
|
}
|
|
echo "Generated recovery contract is current.\n";
|
|
exit(0);
|
|
}
|
|
if (file_put_contents($target, $output) !== strlen($output)) {
|
|
throw new RuntimeException('Could not write PHP recovery contract.');
|
|
}
|
|
echo "Generated PHP recovery contract.\n";
|