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.
79 lines
5.2 KiB
PHP
79 lines
5.2 KiB
PHP
<?php
|
|
namespace App\Services {
|
|
final class Config {
|
|
public static function get($name, $default = null) {
|
|
return $name === 'cloud_engine_hmac_key' ? base64_encode(str_repeat('k', 32)) : $default;
|
|
}
|
|
}
|
|
}
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
use App\Services\Cloud\CloudEngineClient;
|
|
use App\Services\Cloud\CloudInput;
|
|
use App\Services\Cloud\CloudMoney;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudWorkerService;
|
|
use App\Controllers\CloudInternalController;
|
|
$assert = function ($condition, $message) { if (!$condition) { throw new RuntimeException($message); } };
|
|
$assert(CloudMoney::cents('9.99') + CloudMoney::cents('0.01') === 1000, 'Money arithmetic must be exact.');
|
|
$assert(CloudMoney::decimal(999999999999) === '9999999999.99', 'Maximum decimal value changed.');
|
|
foreach ([1.1, '-1', '1e3', '1.001', '01.00', '10000000000.00'] as $bad) {
|
|
try { CloudMoney::cents($bad); throw new RuntimeException('Invalid amount accepted.'); } catch (CloudError $expected) {}
|
|
}
|
|
$actor = ['user_id' => 9, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => true, 'session_id' => str_repeat('a', 64)];
|
|
$body = '{"session_id":"example"}';
|
|
$path = '/api/internal/cloud/session-introspection?a=1%2F2';
|
|
$headers = CloudEngineClient::headers('POST', $path, $body, $actor, 1800000000, 'PythonUrlSafeNonce_ab-CD_123456789');
|
|
$named = ['timestamp' => $headers['X-Cloud-Timestamp'], 'nonce' => $headers['X-Cloud-Nonce'],
|
|
'actor' => $headers['X-Cloud-Actor'], 'signature' => $headers['X-Cloud-Signature']];
|
|
$assert(CloudInternalController::validSignature('POST', $path, $body, $named, str_repeat('k', 32), 1800000000), 'Signed request rejected.');
|
|
foreach ([['POST', $path . '0', $body, 1800000000], ['GET', $path, $body, 1800000000],
|
|
['POST', $path, $body . ' ', 1800000000], ['POST', $path, $body, 1800000061]] as [$method, $target, $raw, $now]) {
|
|
$assert(!CloudInternalController::validSignature($method, $target, $raw, $named, str_repeat('k', 32), $now), 'Tampered or stale signature accepted.');
|
|
}
|
|
$digest = hash('sha256', $body);
|
|
$assert(CloudEngineClient::canonicalDigest('POST', $path, $digest, '1800000000', $named['nonce'], $named['actor'])
|
|
=== CloudEngineClient::canonical('POST', $path, $body, '1800000000', $named['nonce'], $named['actor']), 'Upload digest contract differs.');
|
|
$assert(CloudWorkerService::disposition(['status' => 'failed'], true) === 'uncertain', 'Unknown creation failure must not refund.');
|
|
$assert(CloudWorkerService::disposition(['status' => 'failed', 'safe_to_refund' => false], true) === 'uncertain', 'Explicitly unsafe creation failure must not refund.');
|
|
$assert(CloudWorkerService::disposition(['status' => 'failed', 'safe_to_refund' => true], true) === 'failed', 'Confirmed absent creation must refund.');
|
|
foreach (['shortA1!', 'longwithoutupper1!', 'ALLUPPERCASE!123', 'LongWithoutSymbol123',
|
|
'NoDigitsLong!Aa', "StrongPassword!1\n", 'Strong Password!123',
|
|
'StrongPassword!123含中文', str_repeat('a', 126) . 'A1!'] as $bad) {
|
|
try { CloudInput::password($bad); throw new RuntimeException('Weak guest password accepted.'); } catch (CloudError $expected) {}
|
|
}
|
|
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%*-_=+?';
|
|
$assert(CloudInput::ROOT_PASSWORD_ALPHABET === $alphabet
|
|
&& CloudInput::ROOT_PASSWORD_LENGTH === 32
|
|
&& CloudInput::ROOT_PASSWORD_MIN_LENGTH === 12
|
|
&& CloudInput::ROOT_PASSWORD_MAX_LENGTH === 128,
|
|
'PHP/Python guest password contract drifted.');
|
|
$generated = [];
|
|
for ($index = 0; $index < 50; $index++) {
|
|
$password = CloudInput::password(null);
|
|
$generated[] = $password;
|
|
$assert(strlen($password) === 32
|
|
&& strspn($password, $alphabet) === 32
|
|
&& preg_match('/[A-Z]/', $password) === 1
|
|
&& preg_match('/[a-z]/', $password) === 1
|
|
&& preg_match('/[0-9]/', $password) === 1
|
|
&& preg_match('/[^A-Za-z0-9]/', $password) === 1,
|
|
'Generated guest password violates the uniform 32-character policy.');
|
|
}
|
|
$assert(count(array_unique($generated)) === 50
|
|
&& count(array_unique(array_map(static fn ($value) => substr($value, 0, 4), $generated))) > 1,
|
|
'Generated guest passwords repeat or retain a fixed prefix.');
|
|
$assert(strlen(CloudInput::password('Aa1!' . str_repeat('x', 124))) === 128,
|
|
'Guest manual password bounds wrong.');
|
|
$automaticName = CloudInput::generateInstanceName();
|
|
$assert(preg_match('/^vm-[abcdefghjkmnpqrstuvwxyz23456789]{6}$/D',
|
|
$automaticName) === 1
|
|
&& CloudInput::optionalName(null) === null
|
|
&& CloudInput::optionalName('') === null
|
|
&& CloudInput::optionalName('customer-vm') === 'customer-vm',
|
|
'Optional or generated instance-name policy drifted.');
|
|
$safe = CloudInput::safeResult(['uuid' => 'test', 'generated_password' => 'never-save', 'nested' => ['private_key' => 'secret', 'name' => 'safe']]);
|
|
$assert($safe === ['uuid' => 'test', 'nested' => ['name' => 'safe']], 'Secret entered durable job output.');
|
|
echo "Cloud money, password, HMAC and ambiguity checks passed.\n";
|
|
}
|