|
|
|
|
@ -23,8 +23,9 @@ use App\Models\User;
|
|
|
|
|
* the public network mutation lane. It stores only typed tenant intent,
|
|
|
|
|
* serialises each VPC with its own row lock, and advances observed state only after the engine
|
|
|
|
|
* reports an exact desired revision/digest for every participating host.
|
|
|
|
|
* Tenant NICs, routes and nftables are data-plane work owned by the engine's
|
|
|
|
|
* later VPC phase; a VPC remains unusable while that phase is pending.
|
|
|
|
|
* Tenant NICs, routes and nftables are applied by the engine's independent
|
|
|
|
|
* VPC dataplane lane; a VPC remains unusable until typed host readback proves
|
|
|
|
|
* the requested revision on every participating host.
|
|
|
|
|
*/
|
|
|
|
|
final class CloudVpcService
|
|
|
|
|
{
|
|
|
|
|
@ -41,6 +42,10 @@ final class CloudVpcService
|
|
|
|
|
private const DIRECTIONS = ['ingress', 'egress'];
|
|
|
|
|
private const ACTIONS = ['allow', 'deny'];
|
|
|
|
|
private const PROTOCOLS = ['all', 'tcp', 'udp', 'icmp'];
|
|
|
|
|
/** Stable private NIC/dataplane contract shared with the engine. */
|
|
|
|
|
private const PRIVATE_INTERFACE = 'private0';
|
|
|
|
|
private const FABRIC_INTERFACE = 'wg-fabric0';
|
|
|
|
|
private const PRIVATE_DATAPLANE_OWNER = 'virt-cloud-vpc-dataplane-v1';
|
|
|
|
|
|
|
|
|
|
/** Return VPCs visible to an actor without contacting a host. */
|
|
|
|
|
public static function listing(array $actor): array
|
|
|
|
|
@ -750,8 +755,9 @@ final class CloudVpcService
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send the latest desired document to the private engine VPC lane. The
|
|
|
|
|
* engine rejects it unless the referenced Fabric is active on every host.
|
|
|
|
|
* No general VM-job or public-network mutation row is created here.
|
|
|
|
|
* engine rejects it unless the referenced Fabric is active on every host,
|
|
|
|
|
* then applies/readbacks the private dataplane. No general VM-job or
|
|
|
|
|
* public-network mutation row is created here.
|
|
|
|
|
*/
|
|
|
|
|
public static function reconcile(string $vpcKey, array $actor, bool $allowDeferredRetry = false): array
|
|
|
|
|
{
|
|
|
|
|
@ -839,6 +845,25 @@ final class CloudVpcService
|
|
|
|
|
throw new CloudError('引擎 VPC 结果与操作编号不一致,正在核对', 502);
|
|
|
|
|
}
|
|
|
|
|
$observed = is_array($result['observed'] ?? null) ? $result['observed'] : $result;
|
|
|
|
|
// Some engine versions expose a compact `observed` projection and
|
|
|
|
|
// the full typed host readback beside it. Merge the latter into
|
|
|
|
|
// the bounded projection before validating; never discard
|
|
|
|
|
// member_nics/routes/WireGuard evidence merely because the
|
|
|
|
|
// transport envelope used a compact view.
|
|
|
|
|
if (is_array($result['hosts'] ?? null)) {
|
|
|
|
|
if (!is_array($observed['hosts'] ?? null)) {
|
|
|
|
|
$observed['hosts'] = [];
|
|
|
|
|
}
|
|
|
|
|
foreach ($result['hosts'] as $hostKey => $hostItem) {
|
|
|
|
|
if (!is_array($hostItem)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$hostKey = (string) ($hostItem['host_id'] ?? $hostKey);
|
|
|
|
|
$compact = is_array($observed['hosts'][$hostKey] ?? null)
|
|
|
|
|
? $observed['hosts'][$hostKey] : [];
|
|
|
|
|
$observed['hosts'][$hostKey] = array_replace($compact, $hostItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isset($observed['operation_id'])
|
|
|
|
|
&& (string) $observed['operation_id'] !== (string) $operation->operation_id) {
|
|
|
|
|
throw new CloudError('引擎 VPC 观测与操作编号不一致,正在核对', 502);
|
|
|
|
|
@ -941,6 +966,86 @@ final class CloudVpcService
|
|
|
|
|
$hostKey = (string) ($item['host_id'] ?? $key);
|
|
|
|
|
$hosts[$hostKey] = $item;
|
|
|
|
|
}
|
|
|
|
|
// Validate typed tenant dataplane evidence before evaluating the
|
|
|
|
|
// all-host gate. Fabric transport alone can only leave this
|
|
|
|
|
// independent VPC lane waiting/uncertain.
|
|
|
|
|
$expectedMembersByHost = [];
|
|
|
|
|
$allExpectedMembers = [];
|
|
|
|
|
$expectedDestinations = [];
|
|
|
|
|
$desiredRoutes = [];
|
|
|
|
|
$desiredContractValid = true;
|
|
|
|
|
try {
|
|
|
|
|
$desired = self::desiredDocument($vpc);
|
|
|
|
|
// The host adapter installs only member /32 routes and the
|
|
|
|
|
// explicitly configured VPC routes. The VPC/subnet CIDRs
|
|
|
|
|
// themselves are control-plane address ranges, not an
|
|
|
|
|
// allowance for a broad host route in the data plane.
|
|
|
|
|
foreach (is_array($desired['routes'] ?? null) ? $desired['routes'] : [] as $route) {
|
|
|
|
|
if (is_array($route) && isset($route['destination_cidr'])) {
|
|
|
|
|
$desiredRoutes[] = $route;
|
|
|
|
|
$expectedDestinations[] = (string) $route['destination_cidr'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (is_array($desired['members'] ?? null) ? $desired['members'] : [] as $member) {
|
|
|
|
|
if (!is_array($member) || !isset($member['member_id'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$memberId = (string) $member['member_id'];
|
|
|
|
|
$memberHost = (string) ($member['host_id'] ?? '');
|
|
|
|
|
$member['private_mac'] = strtolower((string) ($member['private_mac'] ?? ''));
|
|
|
|
|
$expectedMembersByHost[$memberHost][$memberId] = $member;
|
|
|
|
|
$allExpectedMembers[$memberId] = $member;
|
|
|
|
|
if (isset($member['private_ip_cidr'])) {
|
|
|
|
|
$expectedDestinations[] = (string) $member['private_ip_cidr'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
// A malformed desired ledger is itself a closed gate.
|
|
|
|
|
$desiredContractValid = false;
|
|
|
|
|
$expectedMembersByHost = [];
|
|
|
|
|
$allExpectedMembers = [];
|
|
|
|
|
$expectedDestinations = [];
|
|
|
|
|
$desiredRoutes = [];
|
|
|
|
|
}
|
|
|
|
|
$expectedDestinations = array_values(array_unique(array_filter(
|
|
|
|
|
$expectedDestinations,
|
|
|
|
|
static fn ($value): bool => is_string($value) && $value !== ''
|
|
|
|
|
)));
|
|
|
|
|
foreach ($hosts as $hostKey => $item) {
|
|
|
|
|
if (($item['private_dataplane_verified'] ?? false) !== true) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$errorCode = $desiredContractValid
|
|
|
|
|
? self::validateHostDataplaneEvidence(
|
|
|
|
|
$item,
|
|
|
|
|
(string) $hostKey,
|
|
|
|
|
$expectedMembersByHost[(string) $hostKey] ?? [],
|
|
|
|
|
array_values($allExpectedMembers),
|
|
|
|
|
$expectedDestinations,
|
|
|
|
|
$desiredRoutes,
|
|
|
|
|
$vpcKey,
|
|
|
|
|
$revision,
|
|
|
|
|
$digest
|
|
|
|
|
)
|
|
|
|
|
: 'vpc_private_readback_mismatch';
|
|
|
|
|
if ($errorCode !== null) {
|
|
|
|
|
$item['private_dataplane_verified'] = false;
|
|
|
|
|
$item['last_error_code'] = $errorCode;
|
|
|
|
|
$hosts[$hostKey] = $item;
|
|
|
|
|
$observedHosts = is_array($observed['hosts'] ?? null)
|
|
|
|
|
? $observed['hosts'] : [];
|
|
|
|
|
$observedHosts[$hostKey] = $item;
|
|
|
|
|
$observed['hosts'] = $observedHosts;
|
|
|
|
|
$observed['private_dataplane_verified'] = false;
|
|
|
|
|
} elseif (($item['removed'] ?? false) === true) {
|
|
|
|
|
$item['removal_verified'] = true;
|
|
|
|
|
$hosts[$hostKey] = $item;
|
|
|
|
|
$observedHosts = is_array($observed['hosts'] ?? null)
|
|
|
|
|
? $observed['hosts'] : [];
|
|
|
|
|
$observedHosts[$hostKey] = $item;
|
|
|
|
|
$observed['hosts'] = $observedHosts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$required = CloudVpcHostDeployment::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereNotIn('state', ['deleted'])
|
|
|
|
|
->lockForUpdate()->get()->keyBy(static fn ($row): int => (int) $row->host_id);
|
|
|
|
|
@ -992,10 +1097,10 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
$liveRequired = $required->filter(static fn ($row): bool =>
|
|
|
|
|
(string) $row->state !== 'deleting');
|
|
|
|
|
// This phase has no private-NIC/route/nft readback. A Fabric or
|
|
|
|
|
// host control-plane digest therefore cannot prove that a
|
|
|
|
|
// A Fabric or host control-plane digest cannot prove that a
|
|
|
|
|
// deleting child/host is gone from the data plane. Keep every
|
|
|
|
|
// tombstone closed until that later evidence is introduced.
|
|
|
|
|
// tombstone closed until the typed private-NIC/route/nft evidence
|
|
|
|
|
// is introduced by the engine.
|
|
|
|
|
$pendingChildTombstone = false;
|
|
|
|
|
foreach ([
|
|
|
|
|
CloudVpcSubnet::class,
|
|
|
|
|
@ -1014,6 +1119,8 @@ final class CloudVpcService
|
|
|
|
|
(string) $row->state === 'deleting');
|
|
|
|
|
$allActive = $globalActive && $liveRequired->count() > 0
|
|
|
|
|
&& !$pendingChildTombstone && !$pendingHostTombstone;
|
|
|
|
|
$allRemovalProof = true;
|
|
|
|
|
$hasRemovalTarget = false;
|
|
|
|
|
foreach ($hosts as $hostKey => $_item) {
|
|
|
|
|
if (!$required->has((int) $hostKey)) {
|
|
|
|
|
$allActive = false;
|
|
|
|
|
@ -1044,17 +1151,36 @@ final class CloudVpcService
|
|
|
|
|
&& $itemRevision <= $revision
|
|
|
|
|
&& $itemRevision === $revision && hash_equals($digest, $itemDigest);
|
|
|
|
|
$deleting = (string) $deployment->state === 'deleting';
|
|
|
|
|
$removalActive = $deleting
|
|
|
|
|
&& ($item['removed'] ?? false) === true
|
|
|
|
|
&& ($item['removal_verified'] ?? false) === true
|
|
|
|
|
&& ($item['state'] ?? null) === 'active'
|
|
|
|
|
&& ($item['transport_verified'] ?? false) === true
|
|
|
|
|
&& ($item['private_dataplane_verified'] ?? false) === true
|
|
|
|
|
&& $itemRevision === $revision
|
|
|
|
|
&& hash_equals($digest, $itemDigest);
|
|
|
|
|
if ($deleting) {
|
|
|
|
|
$hasRemovalTarget = true;
|
|
|
|
|
if (!$removalActive) {
|
|
|
|
|
$allRemovalProof = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$deployment->observed_revision = $itemRevision;
|
|
|
|
|
$deployment->observed_digest = $itemDigest !== '' ? $itemDigest : null;
|
|
|
|
|
// An empty-host target is only a desired control-plane
|
|
|
|
|
// document. Without a data-plane removal readback, preserve
|
|
|
|
|
// the deleting state even when the Fabric digest is exact.
|
|
|
|
|
if (!$deleting) {
|
|
|
|
|
if ($removalActive) {
|
|
|
|
|
$deployment->state = 'deleted';
|
|
|
|
|
$deployment->last_error_code = null;
|
|
|
|
|
} elseif (!$deleting) {
|
|
|
|
|
$deployment->state = $active ? 'active' : 'degraded';
|
|
|
|
|
$deployment->last_error_code = $active ? null : 'vpc_host_observation_mismatch';
|
|
|
|
|
} else {
|
|
|
|
|
$deployment->last_error_code = 'vpc_host_removal_unverified';
|
|
|
|
|
}
|
|
|
|
|
$deployment->last_error_code = $active ? null : 'vpc_host_observation_mismatch';
|
|
|
|
|
$deployment->updated_at = time();
|
|
|
|
|
if ($active) {
|
|
|
|
|
if ($active || $removalActive) {
|
|
|
|
|
$deployment->last_success_at = time();
|
|
|
|
|
} else {
|
|
|
|
|
$deployment->last_failure_at = time();
|
|
|
|
|
@ -1062,12 +1188,36 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
$deployment->save();
|
|
|
|
|
}
|
|
|
|
|
$removalOnlyComplete = $hasRemovalTarget && $allRemovalProof
|
|
|
|
|
&& $liveRequired->count() === 0 && !$pendingChildTombstone;
|
|
|
|
|
if ($hasRemovalTarget && $allRemovalProof && $liveRequired->count() === 0) {
|
|
|
|
|
$observedAt = time();
|
|
|
|
|
foreach ([
|
|
|
|
|
CloudVpcSubnet::class,
|
|
|
|
|
CloudVpcPrivateIp::class,
|
|
|
|
|
CloudVpcMember::class,
|
|
|
|
|
CloudVpcRoute::class,
|
|
|
|
|
CloudVpcAclRule::class,
|
|
|
|
|
] as $childModel) {
|
|
|
|
|
$childModel::where('vpc_id', $vpc->id)
|
|
|
|
|
->where('state', 'deleting')
|
|
|
|
|
->update([
|
|
|
|
|
'observed_revision' => $revision,
|
|
|
|
|
'observed_digest' => $digest,
|
|
|
|
|
'state' => 'deleted',
|
|
|
|
|
'last_error_code' => null,
|
|
|
|
|
'updated_at' => $observedAt,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$removalOnlyComplete = true;
|
|
|
|
|
}
|
|
|
|
|
$operationSucceeded = $allActive || $removalOnlyComplete;
|
|
|
|
|
|
|
|
|
|
// The engine's current observed contract is host-scoped: an
|
|
|
|
|
// active exact digest means the complete typed document for this
|
|
|
|
|
// revision was accepted by every participating host. Mirror that
|
|
|
|
|
// gate onto live child rows. Deletion tombstones intentionally
|
|
|
|
|
// remain untouched until the later data-plane readback phase.
|
|
|
|
|
// remain untouched until a typed removal readback proves absence.
|
|
|
|
|
if ($allActive) {
|
|
|
|
|
$observedAt = time();
|
|
|
|
|
// Persist a proof marker on each member only after the full
|
|
|
|
|
@ -1101,27 +1251,27 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
$operation->observed_json = json_encode(self::boundedObserved($observed),
|
|
|
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
|
|
|
|
|
$operation->status = $allActive ? 'succeeded'
|
|
|
|
|
$operation->status = $operationSucceeded ? 'succeeded'
|
|
|
|
|
: ($dataPlaneDeferred ? 'waiting_observed'
|
|
|
|
|
: ($terminalGate ? 'failed' : 'uncertain'));
|
|
|
|
|
$operation->error_code = $allActive ? null
|
|
|
|
|
$operation->error_code = $operationSucceeded ? null
|
|
|
|
|
: ($dataPlaneDeferred ? 'vpc_data_plane_deferred'
|
|
|
|
|
: ($terminalGate ? $gateError : 'vpc_observation_incomplete'));
|
|
|
|
|
$operation->updated_at = time();
|
|
|
|
|
$operation->finished_at = ($allActive || $dataPlaneDeferred || $terminalGate)
|
|
|
|
|
$operation->finished_at = ($operationSucceeded || $dataPlaneDeferred || $terminalGate)
|
|
|
|
|
? time() : null;
|
|
|
|
|
$operation->lease_until = 0;
|
|
|
|
|
$operation->save();
|
|
|
|
|
$vpc->observed_revision = $allActive ? $revision : (int) $vpc->observed_revision;
|
|
|
|
|
$vpc->observed_digest = $allActive ? $digest : $vpc->observed_digest;
|
|
|
|
|
$vpc->state = $allActive ? 'active' : 'degraded';
|
|
|
|
|
$vpc->sync_state = $allActive ? 'applied'
|
|
|
|
|
$vpc->observed_revision = $operationSucceeded ? $revision : (int) $vpc->observed_revision;
|
|
|
|
|
$vpc->observed_digest = $operationSucceeded ? $digest : $vpc->observed_digest;
|
|
|
|
|
$vpc->state = $operationSucceeded ? 'active' : 'degraded';
|
|
|
|
|
$vpc->sync_state = $operationSucceeded ? 'applied'
|
|
|
|
|
: ($dataPlaneDeferred ? 'waiting' : ($terminalGate ? 'failed' : 'uncertain'));
|
|
|
|
|
$vpc->last_error_code = $allActive ? null
|
|
|
|
|
$vpc->last_error_code = $operationSucceeded ? null
|
|
|
|
|
: ($dataPlaneDeferred ? 'vpc_data_plane_deferred'
|
|
|
|
|
: ($terminalGate ? $gateError : 'vpc_observation_incomplete'));
|
|
|
|
|
$vpc->last_success_at = $allActive ? time() : (int) $vpc->last_success_at;
|
|
|
|
|
$vpc->last_failure_at = $allActive ? (int) $vpc->last_failure_at : time();
|
|
|
|
|
$vpc->last_success_at = $operationSucceeded ? time() : (int) $vpc->last_success_at;
|
|
|
|
|
$vpc->last_failure_at = $operationSucceeded ? (int) $vpc->last_failure_at : time();
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
}, 3);
|
|
|
|
|
@ -1156,6 +1306,7 @@ final class CloudVpcService
|
|
|
|
|
->whereIn('member_id', $members->pluck('id')->map(static fn ($id): int => (int) $id)->all())
|
|
|
|
|
->get()->keyBy('member_id');
|
|
|
|
|
$memberKeys = [];
|
|
|
|
|
$privateMacs = [];
|
|
|
|
|
$memberRows = [];
|
|
|
|
|
foreach ($members as $row) {
|
|
|
|
|
$instance = $instances[(int) $row->instance_id] ?? null;
|
|
|
|
|
@ -1174,12 +1325,22 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
$memberKey = (string) $row->member_key;
|
|
|
|
|
$memberKeys[(int) $row->id] = $memberKey;
|
|
|
|
|
// The MAC is derived from the immutable VPC/member identities so
|
|
|
|
|
// retries, host migration and engine restarts keep the same NIC
|
|
|
|
|
// identity. It is locally administered and unicast; no schema
|
|
|
|
|
// column is needed and the value remains part of the signed DTO.
|
|
|
|
|
$privateMac = self::privateMacForMember((string) $vpc->vpc_key, $memberKey);
|
|
|
|
|
if ($privateMac === null || isset($privateMacs[$privateMac])) {
|
|
|
|
|
throw new CloudError('VPC 成员私网 MAC 无效,无法生成引擎目标', 409);
|
|
|
|
|
}
|
|
|
|
|
$privateMacs[$privateMac] = $memberKey;
|
|
|
|
|
$memberRows[] = [
|
|
|
|
|
'member_id' => $memberKey,
|
|
|
|
|
'instance_uuid' => strtolower((string) $instance->uuid),
|
|
|
|
|
'host_id' => (string) $row->host_id,
|
|
|
|
|
'private_ip_cidr' => (string) $privateIp->private_ip . '/32',
|
|
|
|
|
'subnet_id' => $subnetKey,
|
|
|
|
|
'private_mac' => $privateMac,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$hostMembers = [];
|
|
|
|
|
@ -1399,13 +1560,28 @@ final class CloudVpcService
|
|
|
|
|
'reason_code' => 'vpc_private_expectation_invalid',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$privateMac = self::privateMacForMember(
|
|
|
|
|
(string) $vpc->vpc_key, (string) $member->member_key
|
|
|
|
|
);
|
|
|
|
|
if ($privateMac === null) {
|
|
|
|
|
return [
|
|
|
|
|
'required' => true, 'state' => 'uncertain',
|
|
|
|
|
'desired_state' => $desiredState,
|
|
|
|
|
'vpc_id' => (string) $vpc->vpc_key,
|
|
|
|
|
'member_id' => (string) $member->member_key,
|
|
|
|
|
'private_ip_cidr' => $validIp ? $member->private_ip . '/32' : null,
|
|
|
|
|
'private_mac' => null, 'desired_revision' => max(0, $revision),
|
|
|
|
|
'desired_digest' => $validDigest ? $digest : null,
|
|
|
|
|
'reason_code' => 'vpc_private_expectation_invalid',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'required' => true, 'state' => $state,
|
|
|
|
|
'desired_state' => $desiredState,
|
|
|
|
|
'vpc_id' => (string) $vpc->vpc_key,
|
|
|
|
|
'member_id' => (string) $member->member_key,
|
|
|
|
|
'private_ip_cidr' => $member->private_ip . '/32',
|
|
|
|
|
'private_mac' => null,
|
|
|
|
|
'private_mac' => $privateMac,
|
|
|
|
|
'desired_revision' => $revision,
|
|
|
|
|
'desired_digest' => $digest,
|
|
|
|
|
'reason_code' => null,
|
|
|
|
|
@ -1436,7 +1612,7 @@ final class CloudVpcService
|
|
|
|
|
// target is bound by desired_state, identity, IP, revision and digest;
|
|
|
|
|
// removal remains strict because it changes desired_state to removed.
|
|
|
|
|
foreach (['required', 'desired_state', 'vpc_id', 'member_id',
|
|
|
|
|
'private_ip_cidr', 'desired_revision', 'desired_digest'] as $key) {
|
|
|
|
|
'private_ip_cidr', 'private_mac', 'desired_revision', 'desired_digest'] as $key) {
|
|
|
|
|
if ((string) ($captured[$key] ?? null) !== (string) ($current[$key] ?? null)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
@ -1461,7 +1637,7 @@ final class CloudVpcService
|
|
|
|
|
'observed_digest' => null, 'readback' => $expectation['required'] ? 'missing' : 'confirmed',
|
|
|
|
|
'link_state' => $expectation['required'] ? 'absent' : 'absent',
|
|
|
|
|
'private_ip_cidr' => $expectation['private_ip_cidr'] ?? null,
|
|
|
|
|
'private_mac' => null,
|
|
|
|
|
'private_mac' => $expectation['private_mac'] ?? null,
|
|
|
|
|
'reason_code' => $expectation['reason_code']
|
|
|
|
|
?? ($expectation['required'] ? 'vpc_private_observation_missing' : null),
|
|
|
|
|
'observed_at' => 0,
|
|
|
|
|
@ -1580,6 +1756,10 @@ final class CloudVpcService
|
|
|
|
|
&& $memberExact && $privateExact;
|
|
|
|
|
$base['observed_revision'] = $observedRevision;
|
|
|
|
|
$base['observed_digest'] = $observedDigest;
|
|
|
|
|
// Keep the stable desired identity in the projection even before
|
|
|
|
|
// a host readback is confirmed. The observation validator below
|
|
|
|
|
// still requires the engine to echo the exact same MAC.
|
|
|
|
|
$base['private_mac'] = $expectation['private_mac'] ?? null;
|
|
|
|
|
$base['state'] = $confirmed ? 'active'
|
|
|
|
|
: ((string) $member->state === 'degraded'
|
|
|
|
|
|| (string) $privateIp->state === 'degraded'
|
|
|
|
|
@ -1627,9 +1807,8 @@ final class CloudVpcService
|
|
|
|
|
* Validate one engine private_state at a mutation commit boundary.
|
|
|
|
|
*
|
|
|
|
|
* The engine's XML observation is necessary but not sufficient for an
|
|
|
|
|
* attached VPC: the member identity, desired revision and digest must
|
|
|
|
|
* also be echoed by a future data-plane reconciler. Until then this
|
|
|
|
|
* method intentionally returns an uncertain gate for VPC members.
|
|
|
|
|
* attached VPC: the member identity, stable MAC, desired revision and
|
|
|
|
|
* digest must be echoed by the VPC data-plane readback.
|
|
|
|
|
*/
|
|
|
|
|
public static function validatePrivateObservation(
|
|
|
|
|
array $expectation, ?array $observation, string $kind
|
|
|
|
|
@ -1645,7 +1824,7 @@ final class CloudVpcService
|
|
|
|
|
'desired_digest' => $expectation['desired_digest'] ?? null,
|
|
|
|
|
'observed_digest' => null, 'readback' => 'missing',
|
|
|
|
|
'link_state' => 'absent', 'private_ip_cidr' => null,
|
|
|
|
|
'private_mac' => null,
|
|
|
|
|
'private_mac' => $expectation['private_mac'] ?? null,
|
|
|
|
|
'reason_code' => 'vpc_private_observation_missing',
|
|
|
|
|
'observed_at' => time(),
|
|
|
|
|
];
|
|
|
|
|
@ -1745,11 +1924,17 @@ final class CloudVpcService
|
|
|
|
|
$ip = $expectedState === 'removed'
|
|
|
|
|
|| (is_string($expectation['private_ip_cidr'])
|
|
|
|
|
&& hash_equals((string) $expectation['private_ip_cidr'], (string) $state['private_ip_cidr']));
|
|
|
|
|
$mac = $expectedState === 'removed'
|
|
|
|
|
|| (is_string($expectation['private_mac'] ?? null)
|
|
|
|
|
&& is_string($observation['private_mac'] ?? null)
|
|
|
|
|
&& is_string($state['private_mac'] ?? null)
|
|
|
|
|
&& hash_equals(strtolower((string) $expectation['private_mac']),
|
|
|
|
|
strtolower((string) $state['private_mac'])));
|
|
|
|
|
$desired = $expectedState === 'removed'
|
|
|
|
|
? in_array($state['state'], ['disabled', 'removed'], true)
|
|
|
|
|
: in_array($state['state'], ['active', 'suspended'], true);
|
|
|
|
|
$link = $linkExpected === null || $state['link_state'] === $linkExpected;
|
|
|
|
|
$ok = $shape && $identity && $revision && $digest && $ip && $desired && $link
|
|
|
|
|
$ok = $shape && $identity && $revision && $digest && $ip && $mac && $desired && $link
|
|
|
|
|
&& $state['readback'] === 'confirmed';
|
|
|
|
|
if (!$ok) {
|
|
|
|
|
$state['state'] = $state['state'] === 'degraded' ? 'degraded' : 'uncertain';
|
|
|
|
|
@ -2450,7 +2635,12 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
CloudInput::only($item, ['route_key', 'destination_cidr', 'next_hop_ip', 'metric']);
|
|
|
|
|
$key = self::assertKey($item['route_key'] ?? null, '路由');
|
|
|
|
|
$destination = self::cidr($item['destination_cidr'] ?? null, 8, 32, '路由目标');
|
|
|
|
|
// The host data plane is intentionally routed as exact /32s.
|
|
|
|
|
// Accepting a wider prefix here would let a tenant's route
|
|
|
|
|
// replace the per-member cryptokey and nft boundary, even though
|
|
|
|
|
// the engine later rejects it. Reject it at the PHP boundary so
|
|
|
|
|
// the API and engine cannot disagree about a usable target.
|
|
|
|
|
$destination = self::cidr($item['destination_cidr'] ?? null, 32, 32, '路由目标');
|
|
|
|
|
$next = self::ipv4($item['next_hop_ip'] ?? null, '路由下一跳');
|
|
|
|
|
$metric = self::positiveInt($item['metric'] ?? 100, 1, 65535, '路由 metric 无效');
|
|
|
|
|
$result[] = ['route_key' => $key, 'destination' => $destination,
|
|
|
|
|
@ -2519,6 +2709,401 @@ final class CloudVpcService
|
|
|
|
|
&& hash_equals($left, $right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Derive the stable locally-administered MAC used by one VPC member.
|
|
|
|
|
*
|
|
|
|
|
* Member keys are scoped to a VPC, so binding both identities prevents a
|
|
|
|
|
* key reused in another tenant from receiving the same NIC identity. The
|
|
|
|
|
* first octet is forced to unicast/local-administered (02/06/0a/0e
|
|
|
|
|
* families) and the remaining bytes are cryptographic hash material.
|
|
|
|
|
*/
|
|
|
|
|
private static function privateMacForMember(string $vpcKey, string $memberKey): ?string
|
|
|
|
|
{
|
|
|
|
|
if (preg_match(self::KEY_PATTERN, $vpcKey) !== 1
|
|
|
|
|
|| preg_match(self::KEY_PATTERN, $memberKey) !== 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$digest = hash('sha256', 'virt-cloud:vpc-private0:' . $vpcKey . ':' . $memberKey);
|
|
|
|
|
$octets = [];
|
|
|
|
|
for ($index = 0; $index < 6; $index++) {
|
|
|
|
|
$octets[] = hexdec(substr($digest, $index * 2, 2));
|
|
|
|
|
}
|
|
|
|
|
$octets[0] = ($octets[0] & 0xfc) | 0x02;
|
|
|
|
|
return implode(':', array_map(
|
|
|
|
|
static fn (int $octet): string => sprintf('%02x', $octet), $octets
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function privateBridgeForVpc(string $vpcKey): ?string
|
|
|
|
|
{
|
|
|
|
|
return preg_match(self::KEY_PATTERN, $vpcKey) === 1
|
|
|
|
|
? 'vcv-' . substr(hash('sha256', $vpcKey), 0, 11) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function privateNftTableForVpc(string $vpcKey): ?string
|
|
|
|
|
{
|
|
|
|
|
return preg_match(self::KEY_PATTERN, $vpcKey) === 1
|
|
|
|
|
? 'virt_cloud_vpc_' . substr(hash('sha256', $vpcKey), 0, 12) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function privateRouteTableForVpc(string $vpcKey): ?int
|
|
|
|
|
{
|
|
|
|
|
if (preg_match(self::KEY_PATTERN, $vpcKey) !== 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$digest = hash('sha256', $vpcKey);
|
|
|
|
|
return 10000 + (hexdec(substr($digest, 12, 8)) % 10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate typed host-side private dataplane evidence. Raw command output
|
|
|
|
|
* is deliberately ignored: only bounded identity, route, ACL and XML
|
|
|
|
|
* readback fields can open the PHP VPC gate.
|
|
|
|
|
*
|
|
|
|
|
* A null return means the evidence is exact; otherwise the returned code
|
|
|
|
|
* is safe to persist as a diagnostic and keeps the host degraded.
|
|
|
|
|
*/
|
|
|
|
|
private static function validateHostDataplaneEvidence(
|
|
|
|
|
array $item,
|
|
|
|
|
string $hostId,
|
|
|
|
|
array $expectedMembers,
|
|
|
|
|
array $allMembers,
|
|
|
|
|
array $expectedDestinations,
|
|
|
|
|
array $desiredRoutes,
|
|
|
|
|
string $vpcKey,
|
|
|
|
|
int $revision,
|
|
|
|
|
string $digest
|
|
|
|
|
): ?string {
|
|
|
|
|
// These fields are identity evidence, not optional status hints. A
|
|
|
|
|
// compact/legacy host projection must therefore stay closed until a
|
|
|
|
|
// full typed readback is supplied by the engine.
|
|
|
|
|
if (!array_key_exists('vpc_id', $item)
|
|
|
|
|
|| !array_key_exists('host_id', $item)
|
|
|
|
|
|| !array_key_exists('desired_revision', $item)
|
|
|
|
|
|| !array_key_exists('desired_digest', $item)
|
|
|
|
|
|| !array_key_exists('observed_revision', $item)
|
|
|
|
|
|| !array_key_exists('observed_digest', $item)
|
|
|
|
|
|| (string) $item['vpc_id'] !== $vpcKey
|
|
|
|
|
|| !self::digestMatches($digest, (string) $item['desired_digest'])
|
|
|
|
|
|| (string) $item['host_id'] !== $hostId
|
|
|
|
|
|| (string) ($item['state'] ?? '') !== 'active'
|
|
|
|
|
|| (int) $item['desired_revision'] !== $revision
|
|
|
|
|
|| (int) $item['observed_revision'] !== $revision
|
|
|
|
|
|| !self::digestMatches($digest, (string) $item['observed_digest'])) {
|
|
|
|
|
return 'vpc_private_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
// A host removal is a distinct typed proof. It must not be treated
|
|
|
|
|
// as an empty successful apply: all panel-owned resources and
|
|
|
|
|
// tenant-facing evidence must be absent before a deleting deployment
|
|
|
|
|
// can be finalized.
|
|
|
|
|
if (($item['removed'] ?? false) === true) {
|
|
|
|
|
$readback = $item['readback'] ?? null;
|
|
|
|
|
$readbackState = is_string($readback) ? strtolower($readback)
|
|
|
|
|
: (is_array($readback)
|
|
|
|
|
? strtolower((string) ($readback['state'] ?? $readback['status'] ?? ''))
|
|
|
|
|
: '');
|
|
|
|
|
foreach (['member_nics', 'routes', 'wg_allowed_ips', 'proxy_ips'] as $field) {
|
|
|
|
|
if (!is_array($item[$field] ?? null) || $item[$field] !== []) {
|
|
|
|
|
return 'vpc_private_removal_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($expectedMembers !== []
|
|
|
|
|
|| $readbackState !== 'confirmed'
|
|
|
|
|
|| ($item['bridge'] ?? null) !== null
|
|
|
|
|
|| ($item['bridge_owner'] ?? null) !== null
|
|
|
|
|
|| ($item['nft_table'] ?? null) !== null
|
|
|
|
|
|| !in_array((string) ($item['rollback'] ?? ''), ['none', 'ok'], true)) {
|
|
|
|
|
return 'vpc_private_removal_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$bridge = self::privateBridgeForVpc($vpcKey);
|
|
|
|
|
$nftTable = self::privateNftTableForVpc($vpcKey);
|
|
|
|
|
$routeTable = self::privateRouteTableForVpc($vpcKey);
|
|
|
|
|
if ($bridge === null || $nftTable === null
|
|
|
|
|
|| $routeTable === null
|
|
|
|
|
|| (string) ($item['bridge'] ?? '') !== $bridge
|
|
|
|
|
|| (string) ($item['bridge_owner'] ?? '') !== self::PRIVATE_DATAPLANE_OWNER
|
|
|
|
|
|| (string) ($item['nft_table'] ?? '') !== $nftTable
|
|
|
|
|
|| ($item['anti_spoof'] ?? false) !== true
|
|
|
|
|
|| (int) ($item['rp_filter'] ?? -1) !== 2
|
|
|
|
|
|| (int) ($item['proxy_arp'] ?? -1) !== 0
|
|
|
|
|
|| (int) ($item['proxy_arp_pvlan'] ?? -1) !== 1
|
|
|
|
|
|| (int) ($item['acl_revision'] ?? 0) !== $revision
|
|
|
|
|
|| !array_key_exists('rollback', $item)
|
|
|
|
|
|| !in_array((string) $item['rollback'], ['none', 'ok'], true)) {
|
|
|
|
|
return 'vpc_private_dataplane_unverified';
|
|
|
|
|
}
|
|
|
|
|
$readback = $item['readback'] ?? null;
|
|
|
|
|
$readbackState = is_string($readback) ? strtolower($readback)
|
|
|
|
|
: (is_array($readback)
|
|
|
|
|
? strtolower((string) ($readback['state'] ?? $readback['status'] ?? ''))
|
|
|
|
|
: '');
|
|
|
|
|
if ($readbackState !== 'confirmed') {
|
|
|
|
|
return 'vpc_private_readback_missing';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The bridge uses routed guest /32s. Generic proxy ARP stays off;
|
|
|
|
|
// only exact panel-owned proxy-neighbour entries (with pvlan enabled
|
|
|
|
|
// for same-bridge next hops) may answer. A missing or broadened
|
|
|
|
|
// proxy set keeps the host degraded.
|
|
|
|
|
$proxyIps = $item['proxy_ips'] ?? null;
|
|
|
|
|
if (!is_array($proxyIps)) {
|
|
|
|
|
return 'vpc_private_proxy_arp_readback_missing';
|
|
|
|
|
}
|
|
|
|
|
$expectedProxyIps = [];
|
|
|
|
|
foreach ($expectedDestinations as $destination) {
|
|
|
|
|
if (!is_string($destination)
|
|
|
|
|
|| !preg_match('/^([0-9]{1,3}(?:\.[0-9]{1,3}){3})\/32$/D', $destination, $match)
|
|
|
|
|
|| filter_var($match[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return 'vpc_private_proxy_arp_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$expectedProxyIps[] = $match[1];
|
|
|
|
|
}
|
|
|
|
|
$expectedProxyIps = array_values(array_unique($expectedProxyIps));
|
|
|
|
|
sort($expectedProxyIps, SORT_STRING);
|
|
|
|
|
$observedProxyIps = [];
|
|
|
|
|
foreach ($proxyIps as $proxyIp) {
|
|
|
|
|
if (!is_string($proxyIp)
|
|
|
|
|
|| filter_var($proxyIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return 'vpc_private_proxy_arp_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$observedProxyIps[] = $proxyIp;
|
|
|
|
|
}
|
|
|
|
|
$observedProxyIps = array_values(array_unique($observedProxyIps));
|
|
|
|
|
sort($observedProxyIps, SORT_STRING);
|
|
|
|
|
if ($observedProxyIps !== $expectedProxyIps) {
|
|
|
|
|
return 'vpc_private_proxy_arp_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nics = $item['member_nics'] ?? null;
|
|
|
|
|
if (!is_array($nics) || count($nics) !== count($expectedMembers)) {
|
|
|
|
|
return 'vpc_private_nic_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$seen = [];
|
|
|
|
|
foreach ($nics as $nic) {
|
|
|
|
|
if (!is_array($nic)) {
|
|
|
|
|
return 'vpc_private_nic_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$memberId = (string) ($nic['member_id'] ?? '');
|
|
|
|
|
if ($memberId === '' || isset($seen[$memberId])
|
|
|
|
|
|| !isset($expectedMembers[$memberId])) {
|
|
|
|
|
return 'vpc_private_nic_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$expected = $expectedMembers[$memberId];
|
|
|
|
|
$observedMac = strtolower((string) ($nic['private_mac'] ?? ''));
|
|
|
|
|
if ((string) ($nic['interface'] ?? '') !== self::PRIVATE_INTERFACE
|
|
|
|
|
|| strtolower((string) ($nic['instance_uuid'] ?? ''))
|
|
|
|
|
!== strtolower((string) $expected['instance_uuid'])
|
|
|
|
|
|| (string) ($nic['private_ip_cidr'] ?? '')
|
|
|
|
|
!== (string) $expected['private_ip_cidr']
|
|
|
|
|
|| $observedMac !== strtolower((string) $expected['private_mac'])
|
|
|
|
|
|| ($nic['xml_verified'] ?? false) !== true) {
|
|
|
|
|
return 'vpc_private_nic_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$seen[$memberId] = true;
|
|
|
|
|
}
|
|
|
|
|
if (count($seen) !== count($expectedMembers)) {
|
|
|
|
|
return 'vpc_private_nic_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Routes are checked against the exact host-specific next hop and
|
|
|
|
|
// metric. A dataplane readback must never install a default or
|
|
|
|
|
// otherwise broad route owned by a different subsystem.
|
|
|
|
|
$routes = $item['routes'] ?? null;
|
|
|
|
|
if (!is_array($routes)) {
|
|
|
|
|
return 'vpc_private_route_readback_missing';
|
|
|
|
|
}
|
|
|
|
|
$memberById = [];
|
|
|
|
|
foreach ($allMembers as $member) {
|
|
|
|
|
if (is_array($member) && isset($member['member_id'])) {
|
|
|
|
|
$memberById[(string) $member['member_id']] = $member;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteRows = [];
|
|
|
|
|
foreach ($allMembers as $member) {
|
|
|
|
|
if (!is_array($member) || !isset($member['private_ip_cidr'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteRows[] = [
|
|
|
|
|
'destination_cidr' => (string) $member['private_ip_cidr'],
|
|
|
|
|
'device' => (string) (($member['host_id'] ?? '') === $hostId
|
|
|
|
|
? $bridge : self::FABRIC_INTERFACE),
|
|
|
|
|
'via' => null,
|
|
|
|
|
'metric' => 100,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
foreach ($desiredRoutes as $desiredRoute) {
|
|
|
|
|
if (!is_array($desiredRoute)
|
|
|
|
|
|| !isset($desiredRoute['destination_cidr'])) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$nextMemberId = isset($desiredRoute['next_hop_member_id'])
|
|
|
|
|
? (string) $desiredRoute['next_hop_member_id'] : '';
|
|
|
|
|
$nextMember = $nextMemberId !== ''
|
|
|
|
|
? ($memberById[$nextMemberId] ?? null) : null;
|
|
|
|
|
$nextHost = is_array($nextMember)
|
|
|
|
|
? (string) ($nextMember['host_id'] ?? '')
|
|
|
|
|
: (string) ($desiredRoute['next_hop_host_id'] ?? '');
|
|
|
|
|
if ($nextHost === '') {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteRows[] = [
|
|
|
|
|
'destination_cidr' => (string) $desiredRoute['destination_cidr'],
|
|
|
|
|
'device' => $nextHost === $hostId ? $bridge : self::FABRIC_INTERFACE,
|
|
|
|
|
'via' => $nextHost === $hostId && is_array($nextMember)
|
|
|
|
|
? (string) preg_replace(
|
|
|
|
|
'/\/32$/D', '',
|
|
|
|
|
(string) ($nextMember['private_ip']
|
|
|
|
|
?? $nextMember['private_ip_cidr'] ?? '')
|
|
|
|
|
)
|
|
|
|
|
: null,
|
|
|
|
|
'metric' => (int) ($desiredRoute['metric'] ?? 100),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteMap = [];
|
|
|
|
|
foreach ($expectedRouteRows as $expectedRoute) {
|
|
|
|
|
$via = (string) ($expectedRoute['via'] ?? '');
|
|
|
|
|
$key = $expectedRoute['destination_cidr'] . '|'
|
|
|
|
|
. $expectedRoute['device'] . '|' . $via . '|'
|
|
|
|
|
. (string) $expectedRoute['metric'];
|
|
|
|
|
if (isset($expectedRouteMap[$key])) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteMap[$key] = true;
|
|
|
|
|
}
|
|
|
|
|
$seenRoutes = [];
|
|
|
|
|
foreach ($routes as $route) {
|
|
|
|
|
if (!is_array($route)
|
|
|
|
|
|| !is_string($route['destination_cidr'] ?? null)
|
|
|
|
|
|| !isset($route['device'])
|
|
|
|
|
|| !is_string($route['device'])
|
|
|
|
|
|| $route['device'] === ''
|
|
|
|
|
|| !array_key_exists('table', $route)
|
|
|
|
|
|| !in_array($route['device'], [$bridge, self::FABRIC_INTERFACE], true)
|
|
|
|
|
|| (string) ($route['owner'] ?? '') !== self::PRIVATE_DATAPLANE_OWNER) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$destination = (string) $route['destination_cidr'];
|
|
|
|
|
if (in_array($destination, ['0.0.0.0/0', '::/0'], true)
|
|
|
|
|
|| !in_array($destination, $expectedDestinations, true)) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$via = $route['via'] ?? null;
|
|
|
|
|
if ($via !== null && !is_string($via)) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$metric = $route['metric'] ?? null;
|
|
|
|
|
if ((is_string($metric) && !ctype_digit($metric))
|
|
|
|
|
|| (!is_int($metric) && !is_string($metric))) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$metric = (int) $metric;
|
|
|
|
|
$key = $destination . '|' . (string) $route['device'] . '|'
|
|
|
|
|
. (string) ($via ?? '') . '|' . (string) $metric;
|
|
|
|
|
if (isset($seenRoutes[$key])) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
if (!isset($expectedRouteMap[$key])) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$seenRoutes[$key] = true;
|
|
|
|
|
if ((!is_int($route['table'])
|
|
|
|
|
&& !(is_string($route['table']) && is_numeric($route['table'])))
|
|
|
|
|
|| (int) $route['table'] !== $routeTable) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$expectedRouteSet = array_keys($expectedRouteMap);
|
|
|
|
|
$observedRouteSet = array_keys($seenRoutes);
|
|
|
|
|
sort($expectedRouteSet, SORT_STRING);
|
|
|
|
|
sort($observedRouteSet, SORT_STRING);
|
|
|
|
|
if ($observedRouteSet !== $expectedRouteSet) {
|
|
|
|
|
return 'vpc_private_route_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The engine reports tenant AllowedIPs separately from Fabric overlay
|
|
|
|
|
// addresses. Require an exact remote-member /32 set, preventing a
|
|
|
|
|
// broad prefix from crossing a host or VPC boundary.
|
|
|
|
|
$expectedPeers = [];
|
|
|
|
|
foreach ($allMembers as $member) {
|
|
|
|
|
if ((string) ($member['host_id'] ?? '') === $hostId) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$peerHost = (string) ($member['host_id'] ?? '');
|
|
|
|
|
$expectedPeers[$peerHost][] = (string) $member['private_ip_cidr'];
|
|
|
|
|
}
|
|
|
|
|
foreach ($desiredRoutes as $route) {
|
|
|
|
|
if (!is_array($route)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$target = null;
|
|
|
|
|
if (isset($route['next_hop_member_id'])) {
|
|
|
|
|
$target = $memberById[(string) $route['next_hop_member_id']] ?? null;
|
|
|
|
|
}
|
|
|
|
|
$peerHost = is_array($target)
|
|
|
|
|
? (string) ($target['host_id'] ?? '')
|
|
|
|
|
: (string) ($route['next_hop_host_id'] ?? '');
|
|
|
|
|
if ($peerHost !== '' && $peerHost !== $hostId
|
|
|
|
|
&& isset($route['destination_cidr'])) {
|
|
|
|
|
$expectedPeers[$peerHost][] = (string) $route['destination_cidr'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ($expectedPeers as &$ips) {
|
|
|
|
|
$ips = array_values(array_unique($ips));
|
|
|
|
|
sort($ips, SORT_STRING);
|
|
|
|
|
}
|
|
|
|
|
unset($ips);
|
|
|
|
|
$wg = $item['wg_allowed_ips'] ?? null;
|
|
|
|
|
if (!is_array($wg)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_missing';
|
|
|
|
|
}
|
|
|
|
|
$seenPeers = [];
|
|
|
|
|
foreach ($wg as $peer) {
|
|
|
|
|
if (!is_array($peer)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$peerHost = (string) ($peer['peer_host_id'] ?? '');
|
|
|
|
|
$tenantIps = $peer['tenant_allowed_ips'] ?? $peer['allowed_ips'] ?? null;
|
|
|
|
|
$publicKey = $peer['public_key'] ?? null;
|
|
|
|
|
if ($peerHost === '' || isset($seenPeers[$peerHost])
|
|
|
|
|
|| !array_key_exists($peerHost, $expectedPeers)
|
|
|
|
|
|| !is_array($tenantIps)
|
|
|
|
|
|| !is_string($publicKey)
|
|
|
|
|
|| preg_match('/^[A-Za-z0-9+\/]{42}[A-Za-z0-9+\/]==$|^[A-Za-z0-9+\/]{43}=$/D',
|
|
|
|
|
$publicKey) !== 1) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$decodedKey = base64_decode($publicKey, true);
|
|
|
|
|
if ($decodedKey === false || strlen($decodedKey) !== 32
|
|
|
|
|
|| trim($decodedKey, "\0") === '') {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$normalizedIps = [];
|
|
|
|
|
foreach ($tenantIps as $ip) {
|
|
|
|
|
if (!is_string($ip)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$normalizedIps[] = $ip;
|
|
|
|
|
}
|
|
|
|
|
$normalizedIps = array_values(array_unique($normalizedIps));
|
|
|
|
|
sort($normalizedIps, SORT_STRING);
|
|
|
|
|
foreach ($normalizedIps as $ip) {
|
|
|
|
|
if (!preg_match('/^[0-9.]+\/32$/D', $ip)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($normalizedIps !== $expectedPeers[$peerHost]) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
$seenPeers[$peerHost] = true;
|
|
|
|
|
}
|
|
|
|
|
if (count($seenPeers) !== count($expectedPeers)) {
|
|
|
|
|
return 'vpc_private_wireguard_readback_mismatch';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function boundedObserved(array $value): array
|
|
|
|
|
{
|
|
|
|
|
$hosts = [];
|
|
|
|
|
@ -2526,7 +3111,14 @@ final class CloudVpcService
|
|
|
|
|
if (!is_array($host) || count($hosts) >= 256) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$hosts[(string) $key] = [
|
|
|
|
|
$bounded = [
|
|
|
|
|
'host_id' => is_string($host['host_id'] ?? null)
|
|
|
|
|
? substr($host['host_id'], 0, 64) : (string) $key,
|
|
|
|
|
'vpc_id' => is_string($host['vpc_id'] ?? null)
|
|
|
|
|
? substr($host['vpc_id'], 0, 64) : null,
|
|
|
|
|
'desired_revision' => max(0, (int) ($host['desired_revision'] ?? 0)),
|
|
|
|
|
'desired_digest' => is_string($host['desired_digest'] ?? null)
|
|
|
|
|
? substr($host['desired_digest'], 0, 64) : null,
|
|
|
|
|
'state' => is_string($host['state'] ?? null) ? substr($host['state'], 0, 16) : 'unknown',
|
|
|
|
|
'observed_revision' => max(0, (int) ($host['observed_revision'] ?? $host['revision'] ?? 0)),
|
|
|
|
|
'observed_digest' => is_string($host['observed_digest'] ?? $host['digest'] ?? null)
|
|
|
|
|
@ -2535,8 +3127,102 @@ final class CloudVpcService
|
|
|
|
|
'private_dataplane_verified' => (
|
|
|
|
|
($host['private_dataplane_verified'] ?? false) === true
|
|
|
|
|
),
|
|
|
|
|
'removed' => ($host['removed'] ?? false) === true,
|
|
|
|
|
'retiring' => ($host['retiring'] ?? false) === true,
|
|
|
|
|
'next_stage_allowed' => ($host['next_stage_allowed'] ?? false) === true,
|
|
|
|
|
];
|
|
|
|
|
if (is_string($host['last_error_code'] ?? null)) {
|
|
|
|
|
$bounded['last_error_code'] = substr($host['last_error_code'], 0, 64);
|
|
|
|
|
}
|
|
|
|
|
foreach (['bridge', 'bridge_owner', 'nft_table', 'readback', 'rollback'] as $field) {
|
|
|
|
|
if (is_string($host[$field] ?? null)) {
|
|
|
|
|
$bounded[$field] = substr($host[$field], 0, 128);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isset($host['anti_spoof'])) {
|
|
|
|
|
$bounded['anti_spoof'] = $host['anti_spoof'] === true;
|
|
|
|
|
}
|
|
|
|
|
if (isset($host['acl_revision'])) {
|
|
|
|
|
$bounded['acl_revision'] = max(0, (int) $host['acl_revision']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($host['rp_filter'])) {
|
|
|
|
|
$bounded['rp_filter'] = max(0, (int) $host['rp_filter']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($host['proxy_arp'])) {
|
|
|
|
|
$bounded['proxy_arp'] = max(0, (int) $host['proxy_arp']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($host['proxy_arp_pvlan'])) {
|
|
|
|
|
$bounded['proxy_arp_pvlan'] = max(0, (int) $host['proxy_arp_pvlan']);
|
|
|
|
|
}
|
|
|
|
|
if (is_array($host['proxy_ips'] ?? null)) {
|
|
|
|
|
$bounded['proxy_ips'] = array_values(array_slice(array_map(
|
|
|
|
|
static fn ($ip): string => substr((string) $ip, 0, 64),
|
|
|
|
|
$host['proxy_ips']
|
|
|
|
|
), 0, 4096));
|
|
|
|
|
}
|
|
|
|
|
if (is_array($host['member_nics'] ?? null)) {
|
|
|
|
|
$bounded['member_nics'] = [];
|
|
|
|
|
foreach (array_slice($host['member_nics'], 0, 4096) as $nic) {
|
|
|
|
|
if (!is_array($nic)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$bounded['member_nics'][] = [
|
|
|
|
|
'member_id' => is_string($nic['member_id'] ?? null)
|
|
|
|
|
? substr($nic['member_id'], 0, 64) : null,
|
|
|
|
|
'instance_uuid' => is_string($nic['instance_uuid'] ?? null)
|
|
|
|
|
? substr($nic['instance_uuid'], 0, 64) : null,
|
|
|
|
|
'interface' => is_string($nic['interface'] ?? null)
|
|
|
|
|
? substr($nic['interface'], 0, 32) : null,
|
|
|
|
|
'private_ip_cidr' => is_string($nic['private_ip_cidr'] ?? null)
|
|
|
|
|
? substr($nic['private_ip_cidr'], 0, 64) : null,
|
|
|
|
|
'private_mac' => is_string($nic['private_mac'] ?? null)
|
|
|
|
|
? substr(strtolower($nic['private_mac']), 0, 32) : null,
|
|
|
|
|
'xml_verified' => ($nic['xml_verified'] ?? false) === true,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (is_array($host['routes'] ?? null)) {
|
|
|
|
|
$bounded['routes'] = [];
|
|
|
|
|
foreach (array_slice($host['routes'], 0, 256) as $route) {
|
|
|
|
|
if (!is_array($route)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$bounded['routes'][] = [
|
|
|
|
|
'destination_cidr' => is_string($route['destination_cidr'] ?? null)
|
|
|
|
|
? substr($route['destination_cidr'], 0, 64) : null,
|
|
|
|
|
'device' => is_string($route['device'] ?? null)
|
|
|
|
|
? substr($route['device'], 0, 64) : null,
|
|
|
|
|
'table' => is_scalar($route['table'] ?? null)
|
|
|
|
|
? substr((string) $route['table'], 0, 32) : null,
|
|
|
|
|
'owner' => is_string($route['owner'] ?? null)
|
|
|
|
|
? substr($route['owner'], 0, 64) : null,
|
|
|
|
|
'via' => is_string($route['via'] ?? null)
|
|
|
|
|
? substr($route['via'], 0, 64) : null,
|
|
|
|
|
'metric' => is_scalar($route['metric'] ?? null)
|
|
|
|
|
? substr((string) $route['metric'], 0, 16) : null,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (is_array($host['wg_allowed_ips'] ?? null)) {
|
|
|
|
|
$bounded['wg_allowed_ips'] = [];
|
|
|
|
|
foreach (array_slice($host['wg_allowed_ips'], 0, 256) as $peer) {
|
|
|
|
|
if (!is_array($peer)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$ips = $peer['tenant_allowed_ips'] ?? $peer['allowed_ips'] ?? [];
|
|
|
|
|
$bounded['wg_allowed_ips'][] = [
|
|
|
|
|
'peer_host_id' => is_string($peer['peer_host_id'] ?? null)
|
|
|
|
|
? substr($peer['peer_host_id'], 0, 64) : null,
|
|
|
|
|
'public_key' => is_string($peer['public_key'] ?? null)
|
|
|
|
|
? substr($peer['public_key'], 0, 64) : null,
|
|
|
|
|
'tenant_allowed_ips' => is_array($ips)
|
|
|
|
|
? array_values(array_slice(array_map(
|
|
|
|
|
static fn ($ip): string => substr((string) $ip, 0, 64), $ips
|
|
|
|
|
), 0, 4096)) : [],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$hosts[(string) $key] = $bounded;
|
|
|
|
|
}
|
|
|
|
|
return ['hosts' => $hosts];
|
|
|
|
|
}
|
|
|
|
|
|