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.
virt/web/tests/CloudAgentOperationEnvelope...

294 lines
11 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAgentOperationEnvelope;
use App\Services\Cloud\CloudError;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$operationId = '11111111-1111-4111-8111-111111111111';
$idempotencyKey = 'idem-envelope-001';
$requestHash = str_repeat('a', 64);
$response = [
'operation_id' => $operationId,
'idempotency_key' => $idempotencyKey,
'request_hash' => $requestHash,
'kind' => 'domain_create_validate',
'target' => '00000000-0000-4000-8000-000000000001',
'status' => 'succeeded',
'version' => 3,
'commit_boundary' => 'domain_create_validate_confirmed',
'result' => [
'seed_artifact_id' => '00000000-0000-4000-8000-000000000002',
'domain_xml_hash' => str_repeat('b', 64),
'normalized_xml' => '<domain><uuid>must not enter the journal</uuid></domain>',
],
'error_code' => '',
'error_message' => 'local diagnostic detail must not cross the journal boundary',
];
$envelope = CloudAgentOperationEnvelope::fromResponse(
$response,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
$journal = $envelope->toJournalArray();
$assert($envelope->status === 'succeeded' && $envelope->version === 3,
'Valid Agent operation envelope was not decoded.');
$assert(!array_key_exists('error_message', $journal),
'Agent diagnostic error_message crossed the journal boundary.');
$assert(($journal['result']['domain_xml_hash'] ?? null) === str_repeat('b', 64),
'Safe typed operation result was not retained.');
$assert(!array_key_exists('normalized_xml', $journal['result'] ?? []),
'Normalized XML crossed the operation journal projection.');
$cases = [
'operation mismatch' => static function () use ($response, $idempotencyKey, $requestHash): void {
CloudAgentOperationEnvelope::fromResponse(
$response,
'22222222-2222-4222-8222-222222222222',
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'invalid version' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['version'] = 0;
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'sensitive result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['startup_script' => 'echo should never persist'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'camelCase sensitive result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['startupScript' => 'echo should never persist'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'secret ref result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['secretRef' => 'provider-secret-id'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'secret hash with raw value' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['secret_hash' => 'raw-secret-not-a-digest'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'invalid digest shape' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['plan_hash' => 'not-a-sha256'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'list result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['unexpected-list-item'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'empty result object' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = [];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'nested diagnostic result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['details' => ['errorDetail' => 'host diagnostic']];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'embedded XML result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['fooXmlBar' => 'raw XML'];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'result control character' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['result'] = ['status' => "ready\nunsafe"];
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'result too deep' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$nested = ['leaf' => true];
for ($depth = 0; $depth < 18; $depth++) {
$nested = ['nested' => $nested];
}
$invalid = $response;
$invalid['result'] = $nested;
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'pending with result' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['status'] = 'pending';
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'failed without error code' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['status'] = 'failed';
$invalid['result'] = null;
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'succeeded with error code' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['error_code'] = 'unexpected_error';
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
'succeeded without proven boundary' => static function () use ($response, $operationId, $idempotencyKey, $requestHash): void {
$invalid = $response;
$invalid['commit_boundary'] = 'unknown';
CloudAgentOperationEnvelope::fromResponse(
$invalid,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
'00000000-0000-4000-8000-000000000001',
);
},
];
foreach ($cases as $name => $case) {
try {
$case();
throw new RuntimeException('Envelope accepted invalid case: ' . $name);
} catch (CloudError $error) {
$assert($error->httpStatus === 502, 'Invalid Agent response returned an unsafe status for ' . $name);
}
}
try {
CloudAgentOperationEnvelope::fromResponse(
$response,
$operationId,
$idempotencyKey,
$requestHash,
'domain_create_validate',
' target',
);
throw new RuntimeException('Envelope accepted a whitespace-padded target identity.');
} catch (CloudError $error) {
$assert($error->httpStatus === 422, 'Invalid panel target identity returned an unsafe status.');
}
$source = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentOperationEnvelope.php');
$assert($source !== false
&& str_contains($source, 'version')
&& str_contains($source, 'assertSafeResult')
&& str_contains($source, 'array_is_list($response[\'result\'])')
&& !str_contains($source, 'CloudEngineClient')
&& !str_contains($source, 'CloudSecrets'),
'Operation envelope crossed the legacy engine or secret boundary.');
echo "Cloud Agent operation envelope contract checks passed.\n";