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.
99 lines
2.8 KiB
PHP
99 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Canonicalize the only portable restore platform contract supported by this
|
|
* clean-break release. Callers never compare loosely decoded Agent payloads or
|
|
* construct hashes themselves.
|
|
*/
|
|
final class CloudPortableRestoreCompatibility
|
|
{
|
|
private const CONTRACT = [
|
|
'contract' => 1,
|
|
'architecture' => 'x86_64',
|
|
'machine' => 'q35',
|
|
'firmware' => 'uefi',
|
|
'secure_boot' => false,
|
|
'disk_format' => 'qcow2',
|
|
'system_disk_only' => true,
|
|
'public_network_contract' => 1,
|
|
'vpc_inherited' => false,
|
|
];
|
|
|
|
private function __construct()
|
|
{
|
|
}
|
|
|
|
/** The one fixed platform contract supported by this clean-break release. */
|
|
public static function supported(): self
|
|
{
|
|
return new self();
|
|
}
|
|
|
|
public static function fromPayload(mixed $value): self
|
|
{
|
|
if (!is_array($value) || array_is_list($value)
|
|
|| count($value) !== count(self::CONTRACT)) {
|
|
throw new \InvalidArgumentException(
|
|
'Portable restore contract has an invalid shape'
|
|
);
|
|
}
|
|
foreach (self::CONTRACT as $name => $expected) {
|
|
if (!array_key_exists($name, $value) || $value[$name] !== $expected) {
|
|
throw new \InvalidArgumentException(
|
|
'Portable restore contract is unsupported'
|
|
);
|
|
}
|
|
}
|
|
return self::supported();
|
|
}
|
|
|
|
public static function fromStored(string $json, string $sha256): self
|
|
{
|
|
if (preg_match('/^[a-f0-9]{64}$/D', $sha256) !== 1) {
|
|
throw new \InvalidArgumentException(
|
|
'Portable restore contract digest is invalid'
|
|
);
|
|
}
|
|
try {
|
|
$value = json_decode($json, true, 8, JSON_THROW_ON_ERROR);
|
|
} catch (\JsonException $error) {
|
|
throw new \InvalidArgumentException(
|
|
'Portable restore contract JSON is invalid', 0, $error
|
|
);
|
|
}
|
|
$contract = self::fromPayload($value);
|
|
if (!hash_equals($contract->json(), $json)
|
|
|| !hash_equals($contract->sha256(), $sha256)) {
|
|
throw new \InvalidArgumentException(
|
|
'Portable restore contract evidence does not match'
|
|
);
|
|
}
|
|
return $contract;
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return self::CONTRACT;
|
|
}
|
|
|
|
public function json(): string
|
|
{
|
|
return json_encode(
|
|
self::CONTRACT,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
);
|
|
}
|
|
|
|
public function sha256(): string
|
|
{
|
|
return hash('sha256', $this->json());
|
|
}
|
|
|
|
public function matches(self $other): bool
|
|
{
|
|
return hash_equals($this->sha256(), $other->sha256());
|
|
}
|
|
}
|