|
|
|
|
@ -24,13 +24,16 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
private \Closure $registry;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var \Closure(string,string,string,array,float,string,list<string>):CloudAgentClient
|
|
|
|
|
* @var \Closure(string,string,string,array,float,array,string):CloudAgentClient
|
|
|
|
|
*/
|
|
|
|
|
private \Closure $clientFactory;
|
|
|
|
|
|
|
|
|
|
/** @var \Closure(string):string|null */
|
|
|
|
|
private \Closure $certificateFingerprint;
|
|
|
|
|
|
|
|
|
|
/** @var \Closure(string,bool):bool */
|
|
|
|
|
private \Closure $credentialPathValidator;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
?callable $registry = null,
|
|
|
|
|
private readonly ?CloudAgentSecretResolver $secrets = null,
|
|
|
|
|
@ -38,6 +41,7 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
?callable $clientFactory = null,
|
|
|
|
|
private readonly float $timeoutSeconds = 15.0,
|
|
|
|
|
?callable $certificateFingerprint = null,
|
|
|
|
|
?callable $credentialPathValidator = null,
|
|
|
|
|
) {
|
|
|
|
|
if ($timeoutSeconds < 1.0 || $timeoutSeconds > 900.0) {
|
|
|
|
|
throw new \InvalidArgumentException('Agent timeout is outside the supported range');
|
|
|
|
|
@ -57,6 +61,7 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
array $tls,
|
|
|
|
|
float $timeoutSeconds,
|
|
|
|
|
array $resolvedAddresses,
|
|
|
|
|
string $expectedCertificateFingerprint,
|
|
|
|
|
): CloudAgentClient {
|
|
|
|
|
return new CloudAgentClient(
|
|
|
|
|
$endpoint,
|
|
|
|
|
@ -66,10 +71,14 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
$timeoutSeconds,
|
|
|
|
|
$agentId,
|
|
|
|
|
$resolvedAddresses,
|
|
|
|
|
$expectedCertificateFingerprint,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
$this->certificateFingerprint = \Closure::fromCallable(
|
|
|
|
|
$certificateFingerprint ?? [self::class, 'readCertificateFingerprint'],
|
|
|
|
|
$certificateFingerprint ?? [CloudAgentClient::class, 'certificateFingerprint'],
|
|
|
|
|
);
|
|
|
|
|
$this->credentialPathValidator = \Closure::fromCallable(
|
|
|
|
|
$credentialPathValidator ?? [self::class, 'validateCredentialPath'],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -80,17 +89,17 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
*/
|
|
|
|
|
public function resolve(int $hostId): CloudAgentHandle
|
|
|
|
|
{
|
|
|
|
|
return $this->resolveWithStatuses($hostId, ['active']);
|
|
|
|
|
return $this->resolveWithStatuses($hostId, ['active'], false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Resolve an Agent for a read-only observation while it is degraded. */
|
|
|
|
|
public function resolveReadOnly(int $hostId): CloudAgentHandle
|
|
|
|
|
{
|
|
|
|
|
return $this->resolveWithStatuses($hostId, self::ACTIVE_STATUSES);
|
|
|
|
|
return $this->resolveWithStatuses($hostId, self::ACTIVE_STATUSES, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param list<string> $allowedStatuses */
|
|
|
|
|
private function resolveWithStatuses(int $hostId, array $allowedStatuses): CloudAgentHandle
|
|
|
|
|
private function resolveWithStatuses(int $hostId, array $allowedStatuses, bool $readOnly): CloudAgentHandle
|
|
|
|
|
{
|
|
|
|
|
if ($hostId < 1) {
|
|
|
|
|
throw new CloudError('宿主机编号无效', 422);
|
|
|
|
|
@ -115,28 +124,43 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = $rows[0];
|
|
|
|
|
if ((int) ($row['host_id'] ?? 0) !== $hostId) {
|
|
|
|
|
$rawHostId = $row['host_id'] ?? null;
|
|
|
|
|
if ((is_int($rawHostId) && $rawHostId !== $hostId)
|
|
|
|
|
|| (is_string($rawHostId) && (!preg_match('/^[1-9][0-9]*$/D', $rawHostId)
|
|
|
|
|
|| (int) $rawHostId !== $hostId))
|
|
|
|
|
|| (!is_int($rawHostId) && !is_string($rawHostId))) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 注册身份与宿主机不匹配', 503);
|
|
|
|
|
}
|
|
|
|
|
$agentId = trim((string) ($row['agent_id'] ?? ''));
|
|
|
|
|
if (!in_array((string) ($row['status'] ?? ''), $allowedStatuses, true)) {
|
|
|
|
|
$rawAgentId = $row['agent_id'] ?? null;
|
|
|
|
|
if (!is_string($rawAgentId) || $rawAgentId === trim($rawAgentId)) {
|
|
|
|
|
$agentId = is_string($rawAgentId) ? $rawAgentId : '';
|
|
|
|
|
} else {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 身份格式无效', 503);
|
|
|
|
|
}
|
|
|
|
|
$status = $row['status'] ?? null;
|
|
|
|
|
if (!is_string($status) || !in_array($status, $allowedStatuses, true)) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 当前不可用', 503);
|
|
|
|
|
}
|
|
|
|
|
if ((string) ($row['protocol_version'] ?? '') !== 'v1') {
|
|
|
|
|
$protocolVersion = $row['protocol_version'] ?? null;
|
|
|
|
|
if (!is_string($protocolVersion) || $protocolVersion !== 'v1') {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 协议版本不受支持', 503);
|
|
|
|
|
}
|
|
|
|
|
if (preg_match('/^[A-Za-z0-9][A-Za-z0-9_.:-]{0,95}$/D', $agentId) !== 1) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 身份无效', 503);
|
|
|
|
|
}
|
|
|
|
|
$fingerprint = (string) ($row['client_certificate_fingerprint'] ?? '');
|
|
|
|
|
if (preg_match('/^[0-9a-f]{64}$/D', $fingerprint) !== 1) {
|
|
|
|
|
$fingerprint = $row['client_certificate_fingerprint'] ?? null;
|
|
|
|
|
if (!is_string($fingerprint) || preg_match('/^[0-9a-f]{64}$/D', $fingerprint) !== 1) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 客户端证书指纹无效', 503);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$resolvedAddresses = [];
|
|
|
|
|
try {
|
|
|
|
|
$rawEndpoint = $row['endpoint'] ?? null;
|
|
|
|
|
if (!is_string($rawEndpoint) || $rawEndpoint !== trim($rawEndpoint)) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent 地址无效', 422);
|
|
|
|
|
}
|
|
|
|
|
$endpoint = CloudAgentEndpointPolicy::validate(
|
|
|
|
|
(string) ($row['endpoint'] ?? ''),
|
|
|
|
|
$rawEndpoint,
|
|
|
|
|
function (string $host) use (&$resolvedAddresses): array {
|
|
|
|
|
$resolvedAddresses = $this->hostResolver->resolve($host);
|
|
|
|
|
return $resolvedAddresses;
|
|
|
|
|
@ -161,10 +185,12 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
$tls = [];
|
|
|
|
|
foreach (['ca_file', 'cert_file', 'key_file'] as $key) {
|
|
|
|
|
$value = $material[$key] ?? null;
|
|
|
|
|
if (!is_string($value) || trim($value) === '' || preg_match('/[\x00-\x1F\x7F]/', $value) === 1) {
|
|
|
|
|
if (!is_string($value) || $value === '' || $value !== trim($value)
|
|
|
|
|
|| preg_match('/[\x00-\x1F\x7F]/', $value) === 1
|
|
|
|
|
|| !$this->isCredentialPathValid($value, $key === 'key_file')) {
|
|
|
|
|
throw new CloudError('宿主机 Go Agent mTLS 未配置', 503);
|
|
|
|
|
}
|
|
|
|
|
$tls[$key] = trim($value);
|
|
|
|
|
$tls[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$observedFingerprint = ($this->certificateFingerprint)($tls['cert_file']);
|
|
|
|
|
@ -185,6 +211,7 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
$tls,
|
|
|
|
|
$this->timeoutSeconds,
|
|
|
|
|
array_values($resolvedAddresses),
|
|
|
|
|
$fingerprint,
|
|
|
|
|
);
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
throw $error;
|
|
|
|
|
@ -195,12 +222,13 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
throw new CloudError('宿主机 Go Agent client 类型无效', 503);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new CloudAgentHandle(
|
|
|
|
|
return CloudAgentHandle::fromResolved(
|
|
|
|
|
$hostId,
|
|
|
|
|
$agentId,
|
|
|
|
|
'v1',
|
|
|
|
|
$fingerprint,
|
|
|
|
|
$client,
|
|
|
|
|
$readOnly,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -213,20 +241,59 @@ final class CloudAgentHostClientResolver
|
|
|
|
|
return is_array($row) ? $row : [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function readCertificateFingerprint(string $path): ?string
|
|
|
|
|
private static function validateCredentialPath(string $path, bool $privateKey = false): bool
|
|
|
|
|
{
|
|
|
|
|
if (!function_exists('openssl_x509_read') || !function_exists('openssl_x509_fingerprint')) {
|
|
|
|
|
return null;
|
|
|
|
|
if ($path === '' || $path[0] !== '/' || trim($path) !== $path
|
|
|
|
|
|| preg_match('/[\x00-\x1F\x7F]/', $path) === 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$contents = @file_get_contents($path);
|
|
|
|
|
if (!is_string($contents) || $contents === '') {
|
|
|
|
|
return null;
|
|
|
|
|
$stat = @lstat($path);
|
|
|
|
|
$mode = is_array($stat) ? (int) ($stat['mode'] ?? 0) : 0;
|
|
|
|
|
if (!is_array($stat)
|
|
|
|
|
|| ($mode & 0170000) !== 0100000
|
|
|
|
|
|| ($mode & 006000) !== 0
|
|
|
|
|
|| ($mode & 0022) !== 0
|
|
|
|
|
|| !isset($stat['size']) || (int) $stat['size'] < 1
|
|
|
|
|
|| (int) $stat['size'] > 1024 * 1024) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$certificate = @openssl_x509_read($contents);
|
|
|
|
|
if ($certificate === false) {
|
|
|
|
|
return null;
|
|
|
|
|
if ($privateKey && ($mode & 0077) !== 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($privateKey && function_exists('posix_geteuid')
|
|
|
|
|
&& isset($stat['uid']) && (int) $stat['uid'] !== 0
|
|
|
|
|
&& (int) $stat['uid'] !== (int) posix_geteuid()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do not trust a safe-looking leaf below a group/world-writable or
|
|
|
|
|
// symlinked directory. This keeps a later Guzzle open from being
|
|
|
|
|
// redirected by a replaceable parent during certificate rotation.
|
|
|
|
|
$directory = dirname($path);
|
|
|
|
|
while ($directory !== '/' && $directory !== '.') {
|
|
|
|
|
$directoryStat = @lstat($directory);
|
|
|
|
|
$directoryMode = is_array($directoryStat) ? (int) ($directoryStat['mode'] ?? 0) : 0;
|
|
|
|
|
if (!is_array($directoryStat)
|
|
|
|
|
|| ($directoryMode & 0170000) !== 0040000
|
|
|
|
|
|| ($directoryMode & 006000) !== 0
|
|
|
|
|
|| ($directoryMode & 0022) !== 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$parent = dirname($directory);
|
|
|
|
|
if ($parent === $directory) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$directory = $parent;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function isCredentialPathValid(string $path, bool $privateKey): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return ($this->credentialPathValidator)($path, $privateKey);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$fingerprint = @openssl_x509_fingerprint($certificate, 'sha256');
|
|
|
|
|
return is_string($fingerprint) ? strtolower($fingerprint) : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|