diff --git a/web/app/Services/Cloud/CloudAdminHostNetworkService.php b/web/app/Services/Cloud/CloudAdminHostNetworkService.php index a2c54ffa..dbbd7647 100644 --- a/web/app/Services/Cloud/CloudAdminHostNetworkService.php +++ b/web/app/Services/Cloud/CloudAdminHostNetworkService.php @@ -260,6 +260,12 @@ final class CloudAdminHostNetworkService ); } + self::assertActiveAttachmentDnsContract( + $pool, + $desired, + $poolRows + ); + self::assertNoOverlap( $poolKey, $desired['address_family'], @@ -1024,6 +1030,38 @@ final class CloudAdminHostNetworkService ]; } + /** + * Every active pool on one attachment can participate in a dual-stack + * bundle. Keep resolver order and identity identical so any IPv4/IPv6 + * pairing produces one deterministic network snapshot. Disabled or + * draining pools may be edited first and are checked when reactivated. + */ + private static function assertActiveAttachmentDnsContract( + ?CloudPublicIpPool $currentPool, + array $desired, + $poolRows + ): void { + if ($desired['state'] !== 'active') { + return; + } + foreach ($poolRows as $row) { + if (($currentPool !== null + && (int) $row->id === (int) $currentPool->id) + || (int) $row->network_attachment_id + !== $desired['network_attachment_id'] + || (string) $row->state !== 'active') { + continue; + } + if (self::decodeIpList($row->dns_servers_json) + !== $desired['dns_servers']) { + throw new CloudError( + '同一网络接入的启用地址池必须使用完全相同且顺序一致的 DNS;请先停用相关地址池再分步调整', + 409 + ); + } + } + } + private static function runtimeChanged(array $current, array $desired): bool { diff --git a/web/tests/CloudAdminHostNetworkServiceTest.php b/web/tests/CloudAdminHostNetworkServiceTest.php index c27fb6ba..5429c98e 100644 --- a/web/tests/CloudAdminHostNetworkServiceTest.php +++ b/web/tests/CloudAdminHostNetworkServiceTest.php @@ -453,7 +453,7 @@ $createBody = [ 'name' => 'Second public pool', 'cidr' => '8.8.4.0/29', 'gateway' => '8.8.4.1', - 'dns_servers' => ['1.0.0.1'], + 'dns_servers' => ['1.1.1.1'], 'reserved_ips' => [], 'guest_prefix_length' => 29, 'gateway_on_link' => false, @@ -521,6 +521,15 @@ $ipv6Body = [ 'state' => 'active', 'request_key' => str_repeat('6', 32), ]; +$expectCloudError( + static fn () => $service->create($hostId, $actor, $ipv6Body), + 409, + '启用地址池必须使用完全相同且顺序一致的 DNS' +); +$assert(CloudPublicIpPool::where('host_id', $hostId) + ->where('address_family', 6)->count() === 0, + 'A mismatched active IPv6 DNS contract created a partial pool.'); +$ipv6Body['dns_servers'] = ['1.1.1.1']; $ipv6Created = $service->create($hostId, $actor, $ipv6Body); $ipv6Pool = CloudPublicIpPool::where( 'pool_key', (string) $ipv6Created['saved_pool_key'] @@ -542,15 +551,29 @@ $assert($ipv6Pool !== null && $ipv6Item['dual_stack_ready'] === true && $ipv6Item['attachment']['dual_stack_ready'] === true, 'IPv6 /64 was materialized or its bounded sparse capacity was not exposed.'); +$expectCloudError( + static fn () => $service->update( + $hostId, + (string) $ipv6Created['saved_pool_key'], + $actor, + array_replace($ipv6Body, [ + 'dns_servers' => ['9.9.9.9'], + 'expected_revision' => 1, + ]) + ), + 409, + '启用地址池必须使用完全相同且顺序一致的 DNS' +); +$ipv6Pool->refresh(); +$assert((int) $ipv6Pool->desired_revision === 1 + && (string) $ipv6Pool->dns_servers_json === '["1.1.1.1"]', + 'Rejecting a mismatched paired DNS update changed the IPv6 pool.'); $blockedRangeBody = array_replace($ipv6Body, [ 'name' => 'Blocked sparse range', 'cidr' => '2404:c140:156:3b::/120', 'gateway' => '2404:c140:156:3b::10', - 'dns_servers' => [ - '2404:c140:156:3b::10', - '2404:c140:156:3b:0:0:0:10', - ], + 'dns_servers' => ['1.1.1.1'], 'reserved_ips' => ['2404:c140:156:3b::10'], 'allocation_start' => '2404:c140:156:3b::10', 'allocation_end' => '2404:c140:156:3b::13',