Files
compras/scripts/check-standalone-pilot-activation.php
T
nala 96b30368bc
tests / ci (push) Canceled after 0s
commit compras5 2911
2026-11-29 19:49:19 +02:00

107 lines
7.1 KiB
PHP

<?php
use App\Services\StandaloneRuntime;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
require dirname(__DIR__).'/vendor/autoload.php';
$installed = in_array('--installed-read-only', $argv, true);
if (! in_array('--read-only', $argv, true) && ! $installed) {
throw new LogicException('READ_ONLY_ACTIVATION_PREFLIGHT_REQUIRED');
}
$root = dirname(__DIR__);
$plan = json_decode(file_get_contents($root.'/docs/proofs/compras-standalone-pilot-activation-proposal.json'), true, 512, JSON_THROW_ON_ERROR);
$sourcePath = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
$source = json_decode(file_get_contents($sourcePath), true, 32, JSON_THROW_ON_ERROR);
$expectedSettings = $plan['active_settings_sha256'];
$expectedManifest = $plan['frontend']['previous_manifest_sha256'];
$frontend = $plan['frontend'];
if ($installed) {
$activation = json_decode(file_get_contents($root.'/docs/proofs/compras-standalone-pilot-activation.json'), true, 32, JSON_THROW_ON_ERROR);
$previous = json_decode(file_get_contents($activation['backup_directory'].'/settings.before.private.json'), true, 32, JSON_THROW_ON_ERROR);
$component = [];
foreach (['inventory-conversion', 'order-cancellation'] as $componentName) {
if (! is_file($root.'/docs/proofs/'.$componentName.'-pilot-installation.json')) {
continue;
}
$inventory = json_decode(file_get_contents($root.'/docs/proofs/'.$componentName.'-pilot-installation.json'), true, 32, JSON_THROW_ON_ERROR);
$inventoryPlanPath = $root.'/docs/proofs/'.$componentName.'-pilot-proposal.json';
$inventoryPlan = json_decode(file_get_contents($inventoryPlanPath), true, 32, JSON_THROW_ON_ERROR);
$inventoryApproval = json_decode(file_get_contents($root.'/docs/proofs/'.$componentName.'-pilot-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
if ($inventory['state'] !== 'passed' || $inventoryApproval['state'] !== 'approved'
|| ! hash_equals($inventoryApproval['proposal_sha256'], hash_file('sha256', $inventoryPlanPath))) {
throw new LogicException('ACTIVATION_POSTCHECK_COMPONENT_AUTHORIZATION');
}
$component = [...$component, ...$inventoryPlan['proposed_settings_only']];
}
if ($activation['state'] !== 'passed' || $source !== [...$previous, ...$plan['proposed_runtime_settings'], ...$component]) {
throw new LogicException('ACTIVATION_POSTCHECK_SETTINGS_DIFFER');
}
$expectedSettings = hash_file('sha256', $sourcePath);
$expectedManifest = $plan['frontend']['manifest_sha256'];
$interfacePlanPath = $root.'/docs/proofs/analytics-print-publication-proposal.json';
if (is_file($root.'/docs/proofs/analytics-print-publication.json')) {
$interfacePlan = json_decode(file_get_contents($interfacePlanPath), true, 512, JSON_THROW_ON_ERROR);
$interfaceApproval = json_decode(file_get_contents($root.'/docs/proofs/analytics-print-publication-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
$interfaceResult = json_decode(file_get_contents($root.'/docs/proofs/analytics-print-publication.json'), true, 32, JSON_THROW_ON_ERROR);
if ($interfaceApproval['state'] !== 'approved' || ($interfaceResult['pilot_state'] ?? $interfaceResult['state']) !== 'passed'
|| $interfaceApproval['scope'] !== 'analytics_print_and_attribution_menu_shared_frontend_delta'
|| ! hash_equals($interfaceApproval['proposal_sha256'], hash_file('sha256', $interfacePlanPath))
|| $interfacePlan['previous_manifest_sha256'] !== $expectedManifest
|| $interfaceResult['manifest_sha256'] !== $interfacePlan['manifest_sha256']) {
throw new LogicException('ACTIVATION_POSTCHECK_INTERFACE_AUTHORIZATION');
}
$frontend = $interfacePlan;
$expectedManifest = $interfacePlan['manifest_sha256'];
}
}
if (! hash_equals($expectedSettings, hash_file('sha256', $sourcePath))
|| $source['APP_ENV'] !== 'payable-testing' || $source['DB_HOST'] !== '127.0.0.1'
|| realpath($source['HOST_ROOT']) !== realpath($root)
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $source['DB_DATABASE'], $match)
|| $source['DB_USERNAME'] !== 'payable_qa_'.$match[1]
|| trim($source['PAYABLE_SOURCE_INSTANCE'] ?? '') === '') {
throw new LogicException('ACTIVATION_PREFLIGHT_SOURCE_CHANGED');
}
$assets = $root.'/'.$frontend['candidate_root'];
if (! hash_equals($frontend['manifest_sha256'], hash_file('sha256', $assets.'/manifest.json'))
|| ! hash_equals($expectedManifest, hash_file('sha256', $root.'/public/build/manifest.json'))) {
throw new LogicException('ACTIVATION_PREFLIGHT_MANIFEST_CHANGED');
}
foreach ($frontend['files'] as $file => $hash) {
if (! str_starts_with($file, 'assets/') || str_contains($file, '..') || ! hash_equals($hash, hash_file('sha256', $assets.'/'.$file))
|| (is_file($root.'/public/build/'.$file) && ! hash_equals($hash, hash_file('sha256', $root.'/public/build/'.$file)))) {
throw new LogicException('ACTIVATION_PREFLIGHT_ASSET_MISMATCH');
}
}
$environment = [...$source, ...$plan['proposed_runtime_settings'], 'APP_ENV' => 'pilot-readonly', 'APP_DEBUG' => 'false',
'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'PGOPTIONS' => '-c default_transaction_read_only=on',
'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'array', 'QUEUE_CONNECTION' => 'null', 'MAIL_MAILER' => 'array',
'APP_CONFIG_CACHE' => $root.'/storage/framework/absent-pilot-readonly-config.php', 'LOG_CHANNEL' => 'stderr'];
foreach ($environment as $key => $value) {
putenv($key.'='.$value);
$_ENV[$key] = $_SERVER[$key] = $value;
}
$app = require $root.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$db = DB::connection();
if ($db->selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
throw new LogicException('ACTIVATION_PREFLIGHT_DATABASE_NOT_READ_ONLY');
}
$runtime = app(StandaloneRuntime::class);
$runtime->configure($db);
$runtime->assertLoadedPackages();
if (! $runtime->enabled() || count(array_filter(StandaloneRuntime::FEATURES, fn ($name) => $runtime->requested($name))) !== 24) {
throw new LogicException('ACTIVATION_PREFLIGHT_CAPABILITIES_INCOMPLETE');
}
if (! hash_equals($expectedSettings, hash_file('sha256', $sourcePath))) {
throw new LogicException('ACTIVATION_PREFLIGHT_SETTINGS_CHANGED');
}
$result = ['date' => gmdate('c'), 'state' => 'passed', 'database_enforced_read_only' => true,
'all_24_capabilities_start_with_pinned_installed_packages' => true, 'source_instance_preserved' => true,
'frontend_assets_verified' => count($frontend['files']), 'active_settings_changed' => false,
'mode' => $installed ? 'installed_configuration_read_only_postcheck' : 'prospective_read_only_preflight',
'assets_published' => $installed, 'new_grants_or_business_commands' => false];
file_put_contents($root.'/docs/proofs/'.($installed ? 'compras-standalone-pilot-activation-check.json' : 'compras-standalone-pilot-activation-preflight.json'), json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;