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/app/Services/Cloud/CloudAgentHostClientResolve...

103 lines
3.6 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Services\Config;
/**
* Production assembly boundary for the panel's Go Agent resolver.
*
* The factory deliberately has only two host-side inputs: an explicit public
* address allowlist and a root-only secret directory. It never reads the
* legacy Engine settings, SSH credentials, or key material from a database.
* `fromConfig()` is the application entry point; `create()` keeps the same
* boundary deterministic for contract tests and deployment tooling.
*/
final class CloudAgentHostClientResolverFactory
{
private const DEFAULT_SECRET_DIRECTORY = '/run/virt-cloud/agents';
private const DEFAULT_TIMEOUT_SECONDS = 15.0;
/**
* Build a resolver from the panel configuration.
*
* The optional settings array is intentionally explicit and is useful for
* a fresh deployment/bootstrap test. When omitted, values are read via
* App\\Services\\Config only; no model or environment fallback is used.
*
* @param array<string,mixed>|null $settings
*/
public static function fromConfig(
?array $settings = null,
?callable $registry = null,
?callable $clientFactory = null,
?callable $certificateFingerprint = null,
?callable $credentialPathValidator = null,
): CloudAgentHostClientResolver {
if ($settings === null) {
$settings = [
'cloud_agent_secret_dir' => Config::get(
'cloud_agent_secret_dir', self::DEFAULT_SECRET_DIRECTORY,
),
'cloud_agent_address_allowlist' => Config::get(
'cloud_agent_address_allowlist', [],
),
'cloud_agent_timeout' => Config::get(
'cloud_agent_timeout', self::DEFAULT_TIMEOUT_SECONDS,
),
];
}
return self::create(
$settings,
$registry,
$clientFactory,
$certificateFingerprint,
$credentialPathValidator,
);
}
/**
* Create the resolver from already-decoded, non-secret deployment values.
*
* @param array<string,mixed> $settings
*/
public static function create(
array $settings,
?callable $registry = null,
?callable $clientFactory = null,
?callable $certificateFingerprint = null,
?callable $credentialPathValidator = null,
): CloudAgentHostClientResolver {
$directory = $settings['cloud_agent_secret_dir'] ?? self::DEFAULT_SECRET_DIRECTORY;
if (!is_string($directory)) {
throw new \InvalidArgumentException('Agent secret directory must be a string');
}
$allowlist = $settings['cloud_agent_address_allowlist'] ?? [];
if (!is_array($allowlist)) {
throw new \InvalidArgumentException('Agent address allowlist must be an array');
}
$timeout = $settings['cloud_agent_timeout'] ?? self::DEFAULT_TIMEOUT_SECONDS;
if (is_string($timeout) && is_numeric($timeout)) {
$timeout = (float) $timeout;
}
if ((!is_int($timeout) && !is_float($timeout)) || !is_finite((float) $timeout)) {
throw new \InvalidArgumentException('Agent timeout must be a finite number');
}
$secrets = new CloudAgentFileSecretResolver($directory);
$hostResolver = new CloudAgentStaticHostResolver($allowlist);
return new CloudAgentHostClientResolver(
$registry,
$secrets,
$hostResolver,
$clientFactory,
(float) $timeout,
$certificateFingerprint,
$credentialPathValidator,
);
}
}