addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); // Automatic allocation must search beyond the first MAX_MEMBERS candidates: // live custom-route /32s occupy the same address namespace without counting // as members. This models 3,840 leases followed by all 256 route slots while // avoiding thousands of database inserts in a control-plane regression test. $allocationSubnet = (object) [ 'network_address' => ip2long('10.60.0.0'), 'broadcast_address' => ip2long('10.60.255.255'), 'gateway_ip' => null, ]; $allocationLeases = []; $allocationReserved = []; $allocationStart = (int) $allocationSubnet->network_address + 1; for ($offset = 0; $offset < 3840; $offset++) { $allocationLeases[] = (object) ['private_ip_int' => $allocationStart + $offset]; } for ($offset = 3840; $offset < 4096; $offset++) { $allocationReserved[long2ip($allocationStart + $offset)] = true; } $firstFreeIp = new ReflectionMethod(CloudVpcService::class, 'firstFreeIp'); $assert($firstFreeIp->invoke(null, $allocationSubnet, $allocationLeases, $allocationReserved) === '10.60.16.1', 'Automatic allocation falsely exhausted after 4,096 member/route reservations.'); $schema->create('cloud_regions', static function (Blueprint $table): void { $table->increments('id'); $table->string('code', 32); $table->string('name', 80); $table->string('status', 16); }); $schema->create('cloud_network_lines', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedInteger('region_id'); }); $schema->create('cloud_resource_pools', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedInteger('line_id'); }); $schema->create('cloud_host_resource_pool_memberships', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedInteger('host_id'); $table->unsignedInteger('resource_pool_id'); $table->string('scheduler_state', 16); }); $schema->create('cloud_region_fabrics', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedInteger('region_id'); $table->string('fabric_id', 64); $table->string('fabric_region', 64); $table->unsignedBigInteger('fabric_revision'); $table->string('state', 16); }); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name', 80); $table->string('region', 64); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('uuid', 36); $table->unsignedInteger('host_id'); $table->unsignedInteger('resource_pool_id')->nullable(); $table->unsignedInteger('user_id'); $table->string('name', 63); $table->string('status', 32); $table->string('service_status', 32); $table->string('network_status', 32); $table->unsignedBigInteger('expires_at')->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); }); $schema->create('cloud_instance_runtime', static function (Blueprint $table): void { $table->unsignedInteger('instance_id')->primary(); $table->unsignedInteger('host_id'); $table->string('uuid', 36); $table->string('power_state', 16)->default('unknown'); $table->string('sync_state', 16)->default('never'); $table->unsignedBigInteger('last_success_at')->default(0); $table->unsignedBigInteger('last_attempt_at')->default(0); $table->unsignedBigInteger('revision')->default(0); $table->string('last_error_code', 64)->default(''); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unique(['host_id', 'uuid']); }); $schema->create('cloud_vpcs', static function (Blueprint $table): void { $table->increments('id'); $table->string('vpc_key', 32)->unique(); $table->unsignedInteger('user_id'); $table->unsignedInteger('region_id'); $table->unsignedInteger('private_network_id')->default(0); $table->string('name', 63); $table->string('cidr', 18); $table->string('fabric_id', 64); $table->string('fabric_region', 64); $table->unsignedBigInteger('fabric_revision'); $table->unsignedInteger('max_members'); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64); $table->string('observed_digest', 64)->nullable(); $table->string('sync_state', 16); $table->string('last_operation_id', 32)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('last_success_at')->default(0); $table->unsignedBigInteger('last_failure_at')->default(0); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); }); $schema->create('cloud_vpc_subnets', static function (Blueprint $table): void { $table->increments('id'); $table->string('subnet_key', 32); $table->unsignedInteger('vpc_id'); $table->string('cidr', 18); $table->unsignedBigInteger('network_address'); $table->unsignedBigInteger('broadcast_address'); $table->unsignedTinyInteger('prefix_length'); $table->string('gateway_ip', 15)->nullable(); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unique(['vpc_id', 'subnet_key'], 'cloud_vpc_subnet_key'); $table->unique(['vpc_id', 'cidr'], 'cloud_vpc_subnet_cidr'); }); $schema->create('cloud_vpc_private_ips', static function (Blueprint $table): void { $table->increments('id'); $table->string('ip_key', 32); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('subnet_id'); $table->unsignedInteger('member_id')->nullable(); $table->unsignedInteger('instance_id')->nullable(); $table->string('private_ip', 15); $table->unsignedBigInteger('private_ip_int'); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unique(['vpc_id', 'ip_key'], 'cloud_vpc_private_ip_key'); $table->unique(['vpc_id', 'private_ip'], 'cloud_vpc_private_ip_address'); $table->unique('member_id', 'cloud_vpc_private_ip_member'); }); $schema->create('cloud_vpc_members', static function (Blueprint $table): void { $table->increments('id'); $table->string('member_key', 32); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('subnet_id'); $table->unsignedInteger('private_ip_id')->nullable(); $table->unsignedInteger('instance_id'); $table->unsignedInteger('host_id'); $table->string('private_ip', 15); $table->unsignedBigInteger('private_ip_int'); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_operation_id', 32)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unique(['vpc_id', 'member_key'], 'cloud_vpc_member_key'); $table->unique(['vpc_id', 'instance_id'], 'cloud_vpc_member_instance'); $table->unique('instance_id', 'cloud_vpc_member_instance_global'); $table->unique(['vpc_id', 'private_ip'], 'cloud_vpc_member_ip'); $table->unique('private_ip_id', 'cloud_vpc_member_private_ip'); }); $schema->create('cloud_vpc_routes', static function (Blueprint $table): void { $table->increments('id'); $table->string('route_key', 32); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('member_id'); $table->string('destination_cidr', 18); $table->unsignedBigInteger('destination_int'); $table->unsignedBigInteger('broadcast_int'); $table->unsignedTinyInteger('prefix_length'); $table->string('next_hop_ip', 15); $table->unsignedSmallInteger('metric')->default(100); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); }); $schema->create('cloud_vpc_acl_rules', static function (Blueprint $table): void { $table->increments('id'); $table->string('rule_key', 32); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('priority')->default(1); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('updated_at'); }); $schema->create('cloud_vpc_host_deployments', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('host_id'); $table->unsignedBigInteger('fabric_revision')->default(0); $table->string('state', 16)->default('applying'); $table->unsignedBigInteger('desired_revision')->default(1); $table->unsignedBigInteger('observed_revision')->default(0); $table->string('desired_digest', 64)->nullable(); $table->string('observed_digest', 64)->nullable(); $table->string('last_operation_id', 32)->nullable(); $table->string('last_error_code', 64)->nullable(); $table->unsignedBigInteger('last_success_at')->default(0); $table->unsignedBigInteger('last_failure_at')->default(0); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unique(['vpc_id', 'host_id'], 'cloud_vpc_host_deployment'); }); $schema->create('cloud_vpc_operations', static function (Blueprint $table): void { $table->increments('id'); $table->string('operation_id', 32); $table->unsignedInteger('vpc_id'); $table->unsignedInteger('user_id'); $table->string('kind', 24); $table->unsignedBigInteger('desired_revision'); $table->string('idempotency_key', 128); $table->string('request_hash', 64); $table->string('status', 24); $table->text('payload_json'); $table->text('observed_json')->nullable(); $table->unsignedSmallInteger('evidence_version')->default(0); $table->string('error_code', 64)->nullable(); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); $table->unsignedBigInteger('finished_at')->nullable(); $table->string('lease_token', 32)->nullable(); $table->unsignedBigInteger('lease_until')->default(0); $table->unsignedInteger('attempts')->default(0); $table->unique('operation_id', 'cloud_vpc_operation_id'); $table->unique(['vpc_id', 'idempotency_key'], 'cloud_vpc_operation_idempotency'); $table->unique(['vpc_id', 'desired_revision'], 'cloud_vpc_operation_revision'); }); $now = time(); Capsule::table('cloud_regions')->insert([ ['id' => 1, 'code' => 'test-region', 'name' => 'Test Region', 'status' => 'active'], ['id' => 2, 'code' => 'other-region', 'name' => 'Other Region', 'status' => 'active'], ]); Capsule::table('cloud_network_lines')->insert([ ['id' => 11, 'region_id' => 1], ['id' => 12, 'region_id' => 2], ['id' => 13, 'region_id' => 999], ]); Capsule::table('cloud_resource_pools')->insert([ ['id' => 21, 'line_id' => 11], ['id' => 22, 'line_id' => 12], ['id' => 23, 'line_id' => 11], ['id' => 24, 'line_id' => 999], ['id' => 25, 'line_id' => 13], ]); Capsule::table('cloud_region_fabrics')->insert([ [ 'id' => 1, 'region_id' => 1, 'fabric_id' => 'fabric-test', 'fabric_region' => 'test-region', 'fabric_revision' => 7, 'state' => 'active', ], [ 'id' => 2, 'region_id' => 2, 'fabric_id' => 'fabric-other', 'fabric_region' => 'other-region', 'fabric_revision' => 3, 'state' => 'active', ], ]); foreach ([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140] as $hostId) { Capsule::table('cloud_hosts')->insert([ 'id' => $hostId, 'name' => 'host-' . $hostId, // A display label is intentionally not the Fabric region identity. 'region' => 'Display Region ' . $hostId, ]); Capsule::table('cloud_host_resource_pool_memberships')->insert([ 'host_id' => $hostId, 'resource_pool_id' => 21, 'scheduler_state' => 'active', ]); } $ipToInt = static function (string $ip): int { $unpacked = unpack('Nvalue', inet_pton($ip)); return (int) sprintf('%u', (int) $unpacked['value']); }; $uuidFor = static function (int $id): string { return sprintf('%08x-0000-4000-8000-%012x', $id, $id); }; $keyFor = static function (string $scope, int $id): string { return substr(hash('sha256', $scope . ':' . $id), 0, 32); }; $canonicalize = null; $canonicalize = static function ($value) use (&$canonicalize) { if (!is_array($value)) { return $value; } if (array_is_list($value)) { return array_map(static fn ($item) => $canonicalize($item), $value); } $keys = array_keys($value); sort($keys, SORT_STRING); $result = []; foreach ($keys as $key) { $result[$key] = $canonicalize($value[$key]); } return $result; }; $canonicalJson = static fn (array $value): string => json_encode( $canonicalize($value), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $bridgeMethod = new ReflectionMethod(CloudVpcService::class, 'privateBridgeForVpc'); $nftMethod = new ReflectionMethod(CloudVpcService::class, 'privateNftTableForVpc'); $routeTableMethod = new ReflectionMethod(CloudVpcService::class, 'privateRouteTableForVpc'); $privateMacMethod = new ReflectionMethod(CloudVpcService::class, 'privateMacForMember'); $activeHostEvidence = static function ( string $vpcKey, int $hostId, int $revision, string $digest, string $vpcCidr, array $members = [], array $desiredRoutes = [] ) use ($bridgeMethod, $nftMethod, $routeTableMethod): array { $bridge = $bridgeMethod->invoke(null, $vpcKey); $routeTable = $routeTableMethod->invoke(null, $vpcKey); $memberById = []; $memberNics = []; $routes = []; $proxyIps = []; $remoteIpsByHost = []; foreach ($members as $member) { $memberById[(string) $member['member_id']] = $member; $privateIp = preg_replace('/\/32$/D', '', (string) $member['private_ip_cidr']); $proxyIps[] = $privateIp; if ((string) $member['host_id'] === (string) $hostId) { $memberNics[] = [ 'member_id' => (string) $member['member_id'], 'instance_uuid' => (string) $member['instance_uuid'], 'interface' => 'private0', 'private_ip_cidr' => (string) $member['private_ip_cidr'], 'private_mac' => (string) $member['private_mac'], 'xml_verified' => true, 'config_verified' => true, 'seed_verified' => true, 'restart_required' => false, 'domain_running' => true, 'guest_network_verified' => true, 'guest_network_readback' => 'confirmed', 'guest_reason_code' => null, 'guest_private_mac' => (string) $member['private_mac'], 'guest_private_ip_cidr' => (string) $member['private_ip_cidr'], 'link_state' => 'up', 'guest_link_state' => 'up', 'guest_routes' => [$vpcCidr], ]; } else { $remoteIpsByHost[(string) $member['host_id']][] = (string) $member['private_ip_cidr']; } $routes[] = [ 'destination_cidr' => (string) $member['private_ip_cidr'], 'device' => (string) $member['host_id'] === (string) $hostId ? $bridge : 'wg-fabric0', 'via' => null, 'metric' => 100, 'table' => $routeTable, 'owner' => 'virt-cloud-vpc-dataplane-v1', ]; } foreach ($desiredRoutes as $route) { $nextMember = isset($route['next_hop_member_id']) ? ($memberById[(string) $route['next_hop_member_id']] ?? null) : null; $nextHost = is_array($nextMember) ? (string) $nextMember['host_id'] : (string) ($route['next_hop_host_id'] ?? ''); $destination = (string) $route['destination_cidr']; $proxyIps[] = preg_replace('/\/32$/D', '', $destination); $routes[] = [ 'destination_cidr' => $destination, 'device' => $nextHost === (string) $hostId ? $bridge : 'wg-fabric0', 'via' => $nextHost === (string) $hostId && is_array($nextMember) ? preg_replace('/\/32$/D', '', (string) $nextMember['private_ip_cidr']) : null, 'metric' => (int) ($route['metric'] ?? 100), 'table' => $routeTable, 'owner' => 'virt-cloud-vpc-dataplane-v1', ]; } $proxyIps = array_values(array_unique($proxyIps)); sort($proxyIps, SORT_STRING); $wgAllowedIps = []; foreach ($remoteIpsByHost as $peerHostId => $tenantIps) { $tenantIps = array_values(array_unique($tenantIps)); sort($tenantIps, SORT_STRING); $wgAllowedIps[] = [ 'peer_host_id' => (string) $peerHostId, 'public_key' => base64_encode(hash( 'sha256', 'fixture-peer:' . $peerHostId, true )), 'tenant_allowed_ips' => $tenantIps, ]; } return [ 'vpc_id' => $vpcKey, 'host_id' => (string) $hostId, 'desired_revision' => $revision, 'desired_digest' => $digest, 'observed_revision' => $revision, 'observed_digest' => $digest, 'state' => 'active', 'transport_verified' => true, 'private_dataplane_verified' => true, 'removed' => false, 'config_verified' => true, 'config_observed_revision' => $revision, 'config_observed_digest' => $digest, 'bridge' => $bridge, 'bridge_owner' => 'virt-cloud-vpc-dataplane-v1', 'nft_table' => $nftMethod->invoke(null, $vpcKey), 'anti_spoof' => true, 'rp_filter' => 2, 'proxy_arp' => 0, 'proxy_arp_pvlan' => 1, 'proxy_ips' => $proxyIps, 'acl_revision' => $revision, 'rollback' => 'none', 'readback' => 'confirmed', 'member_nics' => $memberNics, 'routes' => $routes, 'wg_allowed_ips' => $wgAllowedIps, ]; }; $seededInstanceIds = []; $seedInstance = static function (int $id, int $hostId, int $userId = 9) use ( $now, $uuidFor, &$seededInstanceIds ): void { if (isset($seededInstanceIds[$id])) { throw new RuntimeException('Duplicate fixture instance id ' . $id . '.'); } $seededInstanceIds[$id] = true; Capsule::table('cloud_instances')->insert([ 'id' => $id, 'uuid' => $uuidFor($id), 'host_id' => $hostId, 'resource_pool_id' => 21, 'user_id' => $userId, 'name' => 'vm-' . $id, 'status' => 'active', 'service_status' => 'exempt', 'network_status' => 'active', 'expires_at' => null, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_instance_runtime')->insert([ 'instance_id' => $id, 'host_id' => $hostId, 'uuid' => $uuidFor($id), 'power_state' => 'running', 'sync_state' => 'ok', 'last_success_at' => $now, 'last_attempt_at' => $now, 'revision' => 1, 'last_error_code' => '', 'created_at' => $now, 'updated_at' => $now, ]); }; $seedVpc = static function ( int $id, string $cidr, string $subnetCidr, int $hostId, string $proofMode = 'active', string $state = 'active', ?array $liveMember = null, array $desiredRoutes = [] ) use ( $now, $ipToInt, $uuidFor, $keyFor, $canonicalJson, $activeHostEvidence, $privateMacMethod ): array { [$networkIp] = explode('/', $cidr, 2); [$subnetIp, $prefix] = explode('/', $subnetCidr, 2); $subnetNetwork = $ipToInt($subnetIp); $subnetSize = 2 ** (32 - (int) $prefix); $vpcKey = $keyFor('vpc', $id); $subnetKey = $keyFor('subnet', $id); $operationId = $keyFor('operation', $id); $memberRows = []; if ($liveMember !== null) { $memberKey = $keyFor('member', (int) $liveMember['member_row_id']); $memberRows[] = [ 'member_id' => $memberKey, 'instance_uuid' => $uuidFor((int) $liveMember['instance_id']), 'host_id' => (string) $hostId, 'private_ip_cidr' => (string) $liveMember['private_ip'] . '/32', 'subnet_id' => $subnetKey, 'private_mac' => $privateMacMethod->invoke(null, $vpcKey, $memberKey), ]; } $hostMemberIds = array_map( static fn (array $member): string => (string) $member['member_id'], $memberRows ); $document = [ 'schema_version' => 1, 'vpc_id' => $vpcKey, 'region' => 'test-region', 'fabric_id' => 'fabric-test', 'fabric_revision' => 7, 'cidr' => $networkIp . '/24', 'desired_revision' => 1, 'hosts' => [['host_id' => (string) $hostId, 'members' => $hostMemberIds]], 'subnets' => [['subnet_id' => $subnetKey, 'cidr' => $subnetCidr]], 'members' => $memberRows, 'routes' => $desiredRoutes, 'acls' => [], ]; $digest = hash('sha256', $canonicalJson($document)); $payload = $document; $payload['desired_digest'] = $digest; $payload['operation_id'] = $operationId; $hostEvidence = $activeHostEvidence( $vpcKey, $hostId, 1, $digest, $networkIp . '/24', $memberRows, $desiredRoutes ); if ($proofMode === 'deleted') { $observed = [ 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'revision' => 0, 'digest' => null, 'state' => 'degraded', 'next_stage_allowed' => false, 'private_dataplane_verified' => false, 'fabric_gate' => ['active' => false, 'error_code' => 'vpc_no_members'], 'hosts' => [], 'no_data_plane_terminal_proof' => [ 'revision' => 1, 'digest' => $digest, 'gate' => 'vpc_no_members', ], ]; } else { if ($proofMode === 'incomplete') { $hostEvidence['private_dataplane_verified'] = false; unset($hostEvidence['nft_table']); } $observed = [ 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'revision' => 1, 'digest' => $digest, 'state' => 'active', 'next_stage_allowed' => true, 'private_dataplane_verified' => $proofMode === 'active', 'fabric_gate' => ['active' => true], 'hosts' => [(string) $hostId => $hostEvidence], ]; } Capsule::table('cloud_vpcs')->insert([ 'id' => $id, 'vpc_key' => $vpcKey, 'user_id' => 9, 'region_id' => 1, 'private_network_id' => $id, 'name' => 'reuse-vpc-' . $id, 'cidr' => $networkIp . '/24', 'fabric_id' => 'fabric-test', 'fabric_region' => 'test-region', 'fabric_revision' => 7, 'max_members' => 32, 'state' => $state, 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $digest, 'observed_digest' => $digest, 'sync_state' => 'applied', 'last_operation_id' => $operationId, 'last_error_code' => null, 'last_success_at' => $now, 'last_failure_at' => 0, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_vpc_subnets')->insert([ 'id' => 1000 + $id, 'subnet_key' => $subnetKey, 'vpc_id' => $id, 'cidr' => $subnetCidr, 'network_address' => $subnetNetwork, 'broadcast_address' => $subnetNetwork + $subnetSize - 1, 'prefix_length' => (int) $prefix, 'gateway_ip' => null, 'state' => 'active', 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $digest, 'observed_digest' => $digest, 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); $deploymentExact = $proofMode !== 'incomplete'; Capsule::table('cloud_vpc_host_deployments')->insert([ 'vpc_id' => $id, 'host_id' => $hostId, 'fabric_revision' => 7, 'state' => $proofMode === 'deleted' ? 'deleted' : 'active', 'desired_revision' => 1, 'observed_revision' => $deploymentExact ? 1 : 0, 'desired_digest' => $digest, 'observed_digest' => $deploymentExact ? $digest : null, 'last_operation_id' => $operationId, 'last_error_code' => $deploymentExact ? null : 'vpc_host_observation_missing', 'last_success_at' => $deploymentExact ? $now : 0, 'last_failure_at' => $deploymentExact ? 0 : $now, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_vpc_operations')->insert([ 'operation_id' => $operationId, 'vpc_id' => $id, 'user_id' => 9, 'kind' => 'member_detach', 'desired_revision' => 1, 'idempotency_key' => 'fixture-apply-' . $id, 'request_hash' => hash('sha256', 'fixture-request:' . $id), 'status' => 'succeeded', 'payload_json' => json_encode( $payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'observed_json' => json_encode( $observed, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'error_code' => null, 'created_at' => $now, 'updated_at' => $now, 'finished_at' => $now, 'lease_token' => null, 'lease_until' => 0, 'attempts' => 1, ]); return [ 'id' => $id, 'vpc_key' => $vpcKey, 'subnet_id' => 1000 + $id, 'subnet_key' => $subnetKey, 'digest' => $digest, 'operation_id' => $operationId, ]; }; $seedPair = static function ( int $memberId, int $privateId, array $vpc, int $instanceId, int $hostId, string $ip, string $state = 'deleted', bool $exact = true ) use ($now, $ipToInt, $keyFor): void { $ipInt = $ipToInt($ip); Capsule::table('cloud_vpc_members')->insert([ 'id' => $memberId, 'member_key' => $keyFor('member', $memberId), 'vpc_id' => $vpc['id'], 'subnet_id' => $vpc['subnet_id'], 'private_ip_id' => $privateId, 'instance_id' => $instanceId, 'host_id' => $hostId, 'private_ip' => $ip, 'private_ip_int' => $ipInt, 'state' => $state, 'desired_revision' => 1, 'observed_revision' => $exact ? 1 : 0, 'desired_digest' => $vpc['digest'], 'observed_digest' => $exact ? $vpc['digest'] : null, 'last_operation_id' => $vpc['operation_id'], 'last_error_code' => $exact ? null : 'vpc_private_observation_missing', 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_vpc_private_ips')->insert([ 'id' => $privateId, 'ip_key' => $keyFor('private-ip', $privateId), 'vpc_id' => $vpc['id'], 'subnet_id' => $vpc['subnet_id'], 'member_id' => $memberId, 'instance_id' => $instanceId, 'private_ip' => $ip, 'private_ip_int' => $ipInt, 'state' => $state, 'desired_revision' => 1, 'observed_revision' => $exact ? 1 : 0, 'desired_digest' => $vpc['digest'], 'observed_digest' => $exact ? $vpc['digest'] : null, 'last_error_code' => $exact ? null : 'vpc_private_observation_missing', 'created_at' => $now, 'updated_at' => $now, ]); }; $seedRoute = static function ( int $id, array $vpc, int $memberId, string $destinationCidr, string $nextHopIp, string $state = 'active' ) use ($now, $ipToInt, $keyFor): void { [$destinationIp, $prefix] = explode('/', $destinationCidr, 2); $destinationInt = $ipToInt($destinationIp); $size = 2 ** (32 - (int) $prefix); Capsule::table('cloud_vpc_routes')->insert([ 'id' => $id, 'route_key' => $keyFor('route', $id), 'vpc_id' => $vpc['id'], 'member_id' => $memberId, 'destination_cidr' => $destinationCidr, 'destination_int' => $destinationInt, 'broadcast_int' => $destinationInt + $size - 1, 'prefix_length' => (int) $prefix, 'next_hop_ip' => $nextHopIp, 'metric' => 100, 'state' => $state, 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $vpc['digest'], 'observed_digest' => $vpc['digest'], 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); }; $seedRemovedDeployment = static function (array $vpc, int $hostId) use ($now): void { Capsule::table('cloud_vpc_host_deployments')->insert([ 'vpc_id' => $vpc['id'], 'host_id' => $hostId, 'fabric_revision' => 7, 'state' => 'deleted', 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $vpc['digest'], 'observed_digest' => $vpc['digest'], 'last_operation_id' => $vpc['operation_id'], 'last_error_code' => null, 'last_success_at' => $now, 'last_failure_at' => 0, 'created_at' => $now - 1, 'updated_at' => $now, ]); }; $installCurrentProof = static function ( array $vpc, array $liveMembers, array $observedHostIds, int $revision = 2, string $operationScope = 'current-operation', int $timeOffset = 2 ) use ( $now, $uuidFor, $keyFor, $canonicalJson, $privateMacMethod, $activeHostEvidence ): array { $root = Capsule::table('cloud_vpcs')->where('id', $vpc['id'])->first(); $subnet = Capsule::table('cloud_vpc_subnets') ->where('id', $vpc['subnet_id'])->first(); if ($root === null || $subnet === null) { throw new RuntimeException('Current-proof fixture root/subnet is missing.'); } $operationId = $keyFor($operationScope, (int) $vpc['id']); $memberRows = []; $membersByHost = []; foreach ($liveMembers as $liveMember) { $memberKey = $keyFor('member', (int) $liveMember['member_row_id']); $memberRows[] = [ 'member_id' => $memberKey, 'instance_uuid' => $uuidFor((int) $liveMember['instance_id']), 'host_id' => (string) $liveMember['host_id'], 'private_ip_cidr' => (string) $liveMember['private_ip'] . '/32', 'subnet_id' => (string) $vpc['subnet_key'], 'private_mac' => $privateMacMethod->invoke( null, (string) $vpc['vpc_key'], $memberKey ), ]; $membersByHost[(string) $liveMember['host_id']][] = $memberKey; } $hostRows = []; foreach ($membersByHost as $hostId => $memberIds) { sort($memberIds, SORT_STRING); $hostRows[] = ['host_id' => (string) $hostId, 'members' => $memberIds]; } usort($hostRows, static fn (array $left, array $right): int => strcmp($left['host_id'], $right['host_id'])); $document = [ 'schema_version' => 1, 'vpc_id' => (string) $vpc['vpc_key'], 'region' => 'test-region', 'fabric_id' => 'fabric-test', 'fabric_revision' => 7, 'cidr' => (string) $root->cidr, 'desired_revision' => $revision, 'hosts' => $hostRows, 'subnets' => [[ 'subnet_id' => (string) $vpc['subnet_key'], 'cidr' => (string) $subnet->cidr, ]], 'members' => $memberRows, 'routes' => [], 'acls' => [], ]; $digest = hash('sha256', $canonicalJson($document)); $payload = $document; $payload['desired_digest'] = $digest; $payload['operation_id'] = $operationId; $observedHosts = []; foreach ($observedHostIds as $hostId) { $observedHosts[(string) $hostId] = $activeHostEvidence( (string) $vpc['vpc_key'], (int) $hostId, $revision, $digest, (string) $root->cidr, $memberRows, [] ); } $observed = [ 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'revision' => $revision, 'digest' => $digest, 'state' => 'active', 'next_stage_allowed' => true, 'private_dataplane_verified' => true, 'fabric_gate' => ['active' => true], 'hosts' => $observedHosts, ]; $currentTime = $now + $timeOffset; Capsule::table('cloud_vpc_operations')->insert([ 'operation_id' => $operationId, 'vpc_id' => $vpc['id'], 'user_id' => 9, 'kind' => 'reconcile', 'desired_revision' => $revision, 'idempotency_key' => 'fixture-current-' . $vpc['id'], 'request_hash' => hash('sha256', 'fixture-current:' . $vpc['id']), 'status' => 'succeeded', 'payload_json' => json_encode( $payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'observed_json' => json_encode( $observed, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'error_code' => null, 'created_at' => $currentTime, 'updated_at' => $currentTime, 'finished_at' => $currentTime, 'lease_token' => null, 'lease_until' => 0, 'attempts' => 1, ]); Capsule::table('cloud_vpcs')->where('id', $vpc['id'])->update([ 'state' => 'active', 'desired_revision' => $revision, 'observed_revision' => $revision, 'desired_digest' => $digest, 'observed_digest' => $digest, 'sync_state' => 'applied', 'last_operation_id' => $operationId, 'last_error_code' => null, 'last_success_at' => $currentTime, 'updated_at' => $currentTime, ]); foreach ($hostRows as $host) { Capsule::table('cloud_vpc_host_deployments') ->where('vpc_id', $vpc['id']) ->where('host_id', (int) $host['host_id'])->update([ 'state' => 'active', 'desired_revision' => $revision, 'observed_revision' => $revision, 'desired_digest' => $digest, 'observed_digest' => $digest, 'last_operation_id' => $operationId, 'last_error_code' => null, 'last_success_at' => $currentTime, 'last_failure_at' => 0, 'updated_at' => $currentTime, ]); } foreach ($liveMembers as $liveMember) { $memberId = (int) $liveMember['member_row_id']; Capsule::table('cloud_vpc_members')->where('id', $memberId)->update([ 'desired_revision' => $revision, 'observed_revision' => $revision, 'desired_digest' => $digest, 'observed_digest' => $digest, 'last_operation_id' => $operationId, 'last_error_code' => null, 'updated_at' => $currentTime, ]); Capsule::table('cloud_vpc_private_ips')->where('member_id', $memberId)->update([ 'desired_revision' => $revision, 'observed_revision' => $revision, 'desired_digest' => $digest, 'observed_digest' => $digest, 'last_error_code' => null, 'updated_at' => $currentTime, ]); } return ['revision' => $revision, 'digest' => $digest, 'operation_id' => $operationId]; }; $installRemovalProof = static function ( array $vpc, int $hostId, bool $leaveNewerDesiredUncertain = false ) use ($now, $keyFor, $canonicalJson): array { $root = Capsule::table('cloud_vpcs')->where('id', $vpc['id'])->first(); $subnet = Capsule::table('cloud_vpc_subnets') ->where('id', $vpc['subnet_id'])->first(); if ($root === null || $subnet === null) { throw new RuntimeException('Removal-proof fixture root/subnet is missing.'); } $revision = 2; $operationId = $keyFor('removal-operation', (int) $vpc['id']); $document = [ 'schema_version' => 1, 'vpc_id' => (string) $vpc['vpc_key'], 'region' => 'test-region', 'fabric_id' => 'fabric-test', 'fabric_revision' => 7, 'cidr' => (string) $root->cidr, 'desired_revision' => $revision, 'hosts' => [['host_id' => (string) $hostId, 'members' => []]], 'subnets' => [[ 'subnet_id' => (string) $vpc['subnet_key'], 'cidr' => (string) $subnet->cidr, ]], 'members' => [], 'routes' => [], 'acls' => [], ]; $digest = hash('sha256', $canonicalJson($document)); $payload = $document; $payload['desired_digest'] = $digest; $payload['operation_id'] = $operationId; $removed = [ 'vpc_id' => (string) $vpc['vpc_key'], 'host_id' => (string) $hostId, 'desired_revision' => $revision, 'desired_digest' => $digest, 'observed_revision' => $revision, 'observed_digest' => $digest, 'state' => 'active', 'transport_verified' => true, 'private_dataplane_verified' => true, 'removed' => true, 'bridge' => null, 'bridge_owner' => null, 'nft_table' => null, 'rollback' => 'none', 'readback' => 'confirmed', 'member_nics' => [], 'routes' => [], 'wg_allowed_ips' => [], 'proxy_ips' => [], ]; $observed = [ 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'revision' => $revision, 'digest' => $digest, 'state' => 'degraded', 'next_stage_allowed' => true, 'private_dataplane_verified' => true, 'fabric_gate' => ['active' => true], 'hosts' => [(string) $hostId => $removed], ]; $proofTime = $now + 2; Capsule::table('cloud_vpc_operations')->insert([ 'operation_id' => $operationId, 'vpc_id' => $vpc['id'], 'user_id' => 9, 'kind' => 'member_detach', 'desired_revision' => $revision, 'idempotency_key' => 'fixture-removal-' . $vpc['id'], 'request_hash' => hash('sha256', 'fixture-removal:' . $vpc['id']), 'status' => 'succeeded', 'payload_json' => json_encode( $payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'observed_json' => json_encode( $observed, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'error_code' => null, 'created_at' => $proofTime, 'updated_at' => $proofTime, 'finished_at' => $proofTime, 'lease_token' => null, 'lease_until' => 0, 'attempts' => 1, ]); Capsule::table('cloud_vpc_host_deployments') ->where('vpc_id', $vpc['id'])->where('host_id', $hostId)->update([ 'state' => 'deleted', 'desired_revision' => $revision, 'observed_revision' => $revision, 'desired_digest' => $digest, 'observed_digest' => $digest, 'last_operation_id' => $operationId, 'last_error_code' => null, 'last_success_at' => $proofTime, 'last_failure_at' => 0, 'updated_at' => $proofTime, ]); Capsule::table('cloud_vpcs')->where('id', $vpc['id'])->update([ 'state' => $leaveNewerDesiredUncertain ? 'degraded' : 'active', 'desired_revision' => $leaveNewerDesiredUncertain ? 3 : $revision, 'observed_revision' => $revision, 'desired_digest' => $leaveNewerDesiredUncertain ? hash('sha256', 'newer-uncertain:' . $vpc['id']) : $digest, 'observed_digest' => $digest, 'sync_state' => $leaveNewerDesiredUncertain ? 'uncertain' : 'applied', 'last_operation_id' => $leaveNewerDesiredUncertain ? $keyFor('newer-uncertain-operation', (int) $vpc['id']) : $operationId, 'last_error_code' => $leaveNewerDesiredUncertain ? 'vpc_observation_incomplete' : null, 'last_success_at' => $proofTime, 'updated_at' => $proofTime, ]); return ['revision' => $revision, 'digest' => $digest, 'operation_id' => $operationId]; }; $expectCloudError = static function (callable $callback, int $status) use ($assert): CloudError { try { $callback(); } catch (CloudError $error) { $assert($error->httpStatus === $status, 'Expected CloudError HTTP ' . $status . ', got ' . $error->httpStatus . '.'); return $error; } throw new RuntimeException('Expected CloudError HTTP ' . $status . '.'); }; $actor = ['user_id' => 9]; // Instance geography follows membership -> pool -> line -> CloudRegion. // CloudHost.region remains a display label and may differ from Fabric region. $instanceRegionMethod = new ReflectionMethod( CloudVpcService::class, 'assertInstanceRegion' ); $regionFixture = new CloudInstance(); $regionFixture->host_id = 10; $regionFixture->resource_pool_id = 21; $instanceRegionMethod->invoke(null, $regionFixture, 1); // An instance placement pin must match the host's one canonical live pool, // even when both pools belong to the same region. Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 23]); $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 409 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 21]); // A second active/draining placement is a first-release scheduling conflict; // filtering by the instance pool must not hide it from the VPC gate. Capsule::table('cloud_host_resource_pool_memberships')->insert([ 'host_id' => 10, 'resource_pool_id' => 23, 'scheduler_state' => 'draining', ]); $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 409 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->where('resource_pool_id', 23)->delete(); // A broken second live placement remains infrastructure corruption even // though the instance's own pinned placement is otherwise valid. Capsule::table('cloud_host_resource_pool_memberships')->insert([ 'host_id' => 10, 'resource_pool_id' => 999, 'scheduler_state' => 'draining', ]); $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 503 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->where('resource_pool_id', 999)->delete(); // Missing pool/line/region joins are infrastructure corruption, not an // alternate region guess. Preserve the live membership row in every case. Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 999]); $regionFixture->resource_pool_id = 999; $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 503 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 24]); $regionFixture->resource_pool_id = 24; $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 503 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 25]); $regionFixture->resource_pool_id = 25; $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 503 ); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 22]); $regionFixture->resource_pool_id = 22; $expectCloudError( static fn () => $instanceRegionMethod->invoke(null, $regionFixture, 1), 409 ); $instanceRegionMethod->invoke(null, $regionFixture, 2); Capsule::table('cloud_host_resource_pool_memberships') ->where('host_id', 10)->update(['resource_pool_id' => 21]); // Persistence bounds must follow the 4096-member VPC contract. A legacy 256 // cap silently dropped host, route and WireGuard absence evidence, making a // complete replay proof impossible for larger VPCs. $boundedObservedMethod = new ReflectionMethod(CloudVpcService::class, 'boundedObserved'); $boundedRoutes = []; $boundedPeers = []; for ($index = 1; $index <= 257; $index++) { $boundedRoutes[] = [ 'destination_cidr' => '10.200.' . intdiv($index - 1, 254) . '.' . ((($index - 1) % 254) + 1) . '/32', 'device' => 'wg-fabric0', 'table' => 10001, 'owner' => 'virt-cloud-vpc-dataplane-v1', 'via' => null, 'metric' => 100, ]; $boundedPeers[] = [ 'peer_host_id' => (string) (1000 + $index), 'public_key' => base64_encode(hash('sha256', 'bounded-peer:' . $index, true)), 'tenant_allowed_ips' => ['10.201.' . intdiv($index - 1, 254) . '.' . ((($index - 1) % 254) + 1) . '/32'], ]; } $boundedHosts = []; for ($index = 1; $index <= 257; $index++) { $boundedHosts[(string) $index] = [ 'host_id' => (string) $index, 'config_verified' => true, 'config_observed_revision' => 9, 'config_observed_digest' => str_repeat('a', 64), 'member_nics' => $index === 1 ? [[ 'member_id' => str_repeat('b', 32), 'instance_uuid' => 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'interface' => 'private0', 'private_ip_cidr' => '10.200.0.1/32', 'private_mac' => '02:00:00:00:00:01', 'xml_verified' => true, 'config_verified' => true, 'seed_verified' => true, 'restart_required' => false, 'domain_running' => true, 'guest_network_verified' => true, 'guest_network_readback' => 'confirmed', 'guest_reason_code' => null, 'guest_private_mac' => '02:00:00:00:00:01', 'guest_private_ip_cidr' => '10.200.0.1/32', 'link_state' => 'up', 'guest_link_state' => 'up', 'guest_routes' => ['10.200.0.0/24'], ]] : [], 'routes' => $index === 1 ? $boundedRoutes : [], 'wg_allowed_ips' => $index === 1 ? $boundedPeers : [], ]; } $boundedObserved = $boundedObservedMethod->invoke(null, [ 'evidence_version' => CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'hosts' => $boundedHosts, ]); $assert(count($boundedObserved['hosts'] ?? []) === 257 && count($boundedObserved['hosts']['1']['routes'] ?? []) === 257 && count($boundedObserved['hosts']['1']['wg_allowed_ips'] ?? []) === 257 && ($boundedObserved['hosts']['1']['config_verified'] ?? false) === true && ($boundedObserved['hosts']['1']['member_nics'][0]['guest_network_verified'] ?? false) === true && ($boundedObserved['hosts']['1']['member_nics'][0]['link_state'] ?? null) === 'up' && ($boundedObserved['hosts']['1']['member_nics'][0]['guest_routes'] ?? []) === ['10.200.0.0/24'] && ($boundedObserved['evidence_version'] ?? 0) === CloudVpcService::OBSERVATION_EVIDENCE_VERSION, 'Observed proof persistence still truncates hosts, routes or peers at 256.'); // A fully proven historical pair must release both uniqueness keys before a // same-VPC attach automatically reserves the lowest free address again. The host stays // active for a real sibling, so absence is proven by exact NIC/route/proxy // evidence rather than by treating an active empty bridge as a removal receipt. $same = $seedVpc(1, '10.41.0.0/24', '10.41.0.0/29', 10, 'active', 'active', [ 'member_row_id' => 10002, 'instance_id' => 1002, 'private_ip' => '10.41.0.2', ]); $seedInstance(1001, 10); $seedInstance(1002, 10); $seedPair(10001, 20001, $same, 1001, 10, '10.41.0.1'); $seedPair(10002, 20002, $same, 1002, 10, '10.41.0.2', 'active'); $sameInput = [ 'instance_id' => 1001, 'request_key' => 'reuse-same-vpc-0001', ]; CloudVpcService::attachMember($same['vpc_key'], $actor, $sameInput); $sameMember = Capsule::table('cloud_vpc_members')->where('instance_id', 1001)->first(); $sameLease = $sameMember === null ? null : Capsule::table('cloud_vpc_private_ips') ->where('member_id', (int) $sameMember->id)->first(); $assert(Capsule::table('cloud_vpc_members')->where('id', 10001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 20001)->count() === 0 && $sameMember !== null && (int) $sameMember->id !== 10001 && (string) $sameMember->private_ip === '10.41.0.1' && (string) $sameMember->state === 'applying' && $sameLease !== null && (int) $sameMember->private_ip_id === (int) $sameLease->id && (int) $sameLease->member_id === (int) $sameMember->id && (string) $sameLease->private_ip === '10.41.0.1' && Capsule::table('cloud_vpc_members')->where('id', 10002) ->where('state', 'active')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 1)->value('desired_revision') === 2, 'A proven same-VPC member/IP pair did not release its real database keys.'); // Replaying the attach request is read-only even though the first operation is // still queued for engine application. Idempotency must be resolved before // mutable instance preconditions: the VM may already be entering deletion // when a client retries after losing the original response. Capsule::table('cloud_instances')->where('id', 1001)->update([ 'status' => 'deleting', 'updated_at' => $now + 1, ]); CloudVpcService::attachMember($same['vpc_key'], $actor, $sameInput); Capsule::table('cloud_instances')->where('id', 1001)->update([ 'status' => 'active', 'updated_at' => $now + 2, ]); $assert(Capsule::table('cloud_vpc_members')->where('instance_id', 1001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('instance_id', 1001)->count() === 1 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 1) ->where('idempotency_key', 'reuse-same-vpc-0001')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 1)->value('desired_revision') === 2, 'An idempotent attach replay duplicated identities or advanced the VPC revision.'); // A pre-policy client could have supplied the selected address explicitly. // Its immutable operation remains replayable even if the automatic subnet is // now cleaning: replay identity is proven from the old signed desired payload, // before mutable subnet/instance preconditions are consulted. $legacyExplicitHash = hash('sha256', $canonicalJson([ 'kind' => 'member_attach', 'input' => [ 'subnet_key' => $same['subnet_key'], 'instance_id' => 1001, 'private_ip' => '10.41.0.1', ], ])); Capsule::table('cloud_vpc_operations')->where('vpc_id', 1) ->where('idempotency_key', 'reuse-same-vpc-0001') ->update(['request_hash' => $legacyExplicitHash]); Capsule::table('cloud_vpc_subnets')->where('id', $same['subnet_id']) ->update(['state' => 'deleting']); Capsule::table('cloud_instances')->where('id', 1001)->update([ 'status' => 'deleting', 'updated_at' => $now + 3, ]); CloudVpcService::attachMember($same['vpc_key'], $actor, $sameInput); CloudVpcService::attachMember($same['vpc_key'], $actor, [ ...$sameInput, 'subnet_key' => $same['subnet_key'], 'private_ip' => '10.41.0.1', ]); $expectCloudError(static fn () => CloudVpcService::attachMember( $same['vpc_key'], $actor, [ ...$sameInput, 'subnet_key' => $same['subnet_key'], 'private_ip' => '10.41.0.3', ] ), 409); $legacyWhitespaceIp = ' 10.41.0.1 '; $legacyWhitespaceHash = hash('sha256', $canonicalJson([ 'kind' => 'member_attach', 'input' => [ 'subnet_key' => $same['subnet_key'], 'instance_id' => 1001, 'private_ip' => $legacyWhitespaceIp, ], ])); Capsule::table('cloud_vpc_operations')->where('vpc_id', 1) ->where('idempotency_key', 'reuse-same-vpc-0001') ->update(['request_hash' => $legacyWhitespaceHash]); CloudVpcService::attachMember($same['vpc_key'], $actor, [ ...$sameInput, 'subnet_key' => $same['subnet_key'], 'private_ip' => $legacyWhitespaceIp, ]); Capsule::table('cloud_vpc_subnets')->where('id', $same['subnet_id']) ->update(['state' => 'active']); Capsule::table('cloud_instances')->where('id', 1001)->update([ 'status' => 'active', 'updated_at' => $now + 4, ]); $assert(Capsule::table('cloud_vpc_members')->where('instance_id', 1001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('instance_id', 1001)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 1)->value('desired_revision') === 2, 'A legacy explicit-IP attach did not replay exactly before mutable subnet checks.'); // Keep the production uniqueness barriers present and executable. The service // succeeds above because it physically deleted the old rows, not because the // schema or application merely stopped looking at them. $memberIndexes = array_map(static fn ($row): string => (string) $row->name, Capsule::select("PRAGMA index_list('cloud_vpc_members')")); $privateIndexes = array_map(static fn ($row): string => (string) $row->name, Capsule::select("PRAGMA index_list('cloud_vpc_private_ips')")); foreach (['cloud_vpc_member_instance_global', 'cloud_vpc_member_ip', 'cloud_vpc_member_private_ip'] as $index) { $assert(in_array($index, $memberIndexes, true), 'Missing member UNIQUE index ' . $index . '.'); } foreach (['cloud_vpc_private_ip_address', 'cloud_vpc_private_ip_member'] as $index) { $assert(in_array($index, $privateIndexes, true), 'Missing lease UNIQUE index ' . $index . '.'); } $memberUniqueRejected = false; try { Capsule::table('cloud_vpc_members')->insert([ 'member_key' => $keyFor('duplicate-member', 1), 'vpc_id' => 999, 'subnet_id' => 999, 'private_ip_id' => null, 'instance_id' => 1001, 'host_id' => 10, 'private_ip' => '10.255.0.1', 'private_ip_int' => $ipToInt('10.255.0.1'), 'state' => 'applying', 'desired_revision' => 1, 'observed_revision' => 0, 'desired_digest' => null, 'observed_digest' => null, 'last_operation_id' => null, 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); } catch (QueryException) { $memberUniqueRejected = true; } $leaseUniqueRejected = false; try { Capsule::table('cloud_vpc_private_ips')->insert([ 'ip_key' => $keyFor('duplicate-private-ip', 1), 'vpc_id' => 1, 'subnet_id' => $same['subnet_id'], 'member_id' => null, 'instance_id' => 1999, 'private_ip' => '10.41.0.1', 'private_ip_int' => $ipToInt('10.41.0.1'), 'state' => 'applying', 'desired_revision' => 2, 'observed_revision' => 0, 'desired_digest' => null, 'observed_digest' => null, 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); } catch (QueryException) { $leaseUniqueRejected = true; } $assert($memberUniqueRejected && $leaseUniqueRejected, 'SQLite did not enforce the real global-member/private-address UNIQUE keys.'); // A VM whose prior VPC has no live host can migrate only after the old root, // operation and removed-host ledger are all exact. Audit rows remain intact. $source = $seedVpc(2, '10.42.0.0/24', '10.42.0.0/29', 20, 'deleted', 'degraded'); $target = $seedVpc(3, '10.43.0.0/24', '10.43.0.0/29', 20, 'active', 'degraded'); $seedInstance(2001, 20); $seedPair(20001, 30001, $source, 2001, 20, '10.42.0.1'); CloudVpcService::attachMember($target['vpc_key'], $actor, [ 'instance_id' => 2001, 'request_key' => 'reuse-cross-vpc-0001', ]); $crossMember = Capsule::table('cloud_vpc_members')->where('instance_id', 2001)->first(); $assert($crossMember !== null && (int) $crossMember->vpc_id === 3 && (string) $crossMember->private_ip === '10.43.0.1' && Capsule::table('cloud_vpc_members')->where('id', 20001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 30001)->count() === 0 && Capsule::table('cloud_vpcs')->where('id', 2)->count() === 1 && Capsule::table('cloud_vpc_host_deployments')->where('vpc_id', 2) ->where('host_id', 20)->value('state') === 'deleted' && Capsule::table('cloud_vpc_operations')->where('operation_id', $source['operation_id']) ->value('status') === 'succeeded', 'A proven old VPC did not release a VM for cross-VPC reuse while retaining audit rows.'); // If the proven .1 tombstone remains allocated, firstFreeIp would choose .2. // Reclaiming it must therefore restore the lowest automatic address. $automatic = $seedVpc(4, '10.44.0.0/24', '10.44.0.0/30', 30, 'deleted', 'active'); $seedInstance(3001, 30); $seedInstance(3003, 30); $seedPair(30001, 40001, $automatic, 3001, 30, '10.44.0.1'); CloudVpcService::attachMember($automatic['vpc_key'], $actor, [ 'instance_id' => 3003, 'request_key' => 'reuse-auto-lowest-0001', ]); $assert(Capsule::table('cloud_vpc_members')->where('id', 30001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 40001)->count() === 0 && Capsule::table('cloud_vpc_members')->where('instance_id', 3003) ->value('private_ip') === '10.44.0.1', 'Automatic allocation did not reclaim the lowest proven private address.'); // A later current-root mutation may be pending or uncertain without erasing a // complete historical operation proof for the already deleted pair. $rootAdvanced = $seedVpc(5, '10.45.0.0/24', '10.45.0.0/29', 40, 'deleted', 'active'); $seedInstance(4001, 40); $seedPair(40001, 50001, $rootAdvanced, 4001, 40, '10.45.0.1'); Capsule::table('cloud_vpcs')->where('id', 5)->update([ 'desired_revision' => 2, 'observed_revision' => 1, 'desired_digest' => hash('sha256', 'newer-pending-root'), 'observed_digest' => $rootAdvanced['digest'], 'state' => 'degraded', 'sync_state' => 'uncertain', 'last_error_code' => 'vpc_observation_incomplete', 'last_operation_id' => $keyFor('newer-operation', 5), ]); CloudVpcService::attachMember($rootAdvanced['vpc_key'], $actor, [ 'instance_id' => 4001, 'request_key' => 'reuse-monotonic-root-0001', ]); $assert(Capsule::table('cloud_vpc_members')->where('id', 40001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 50001)->count() === 0 && Capsule::table('cloud_vpc_members')->where('instance_id', 4001) ->value('private_ip') === '10.45.0.1' && (int) Capsule::table('cloud_vpcs')->where('id', 5)->value('desired_revision') === 3, 'A newer uncertain root incorrectly erased a monotonic historical removal proof.'); // A deleted label and exact pair fields are insufficient when their own // historical operation never succeeded or has no durable observed evidence. $operationIncomplete = $seedVpc(6, '10.46.0.0/24', '10.46.0.0/29', 50, 'deleted', 'active'); $seedInstance(5001, 50); $seedPair(50001, 60001, $operationIncomplete, 5001, 50, '10.46.0.1'); Capsule::table('cloud_vpc_operations') ->where('operation_id', $operationIncomplete['operation_id'])->update([ 'status' => 'uncertain', 'observed_json' => null, 'finished_at' => null, 'error_code' => 'vpc_observation_incomplete', ]); $expectCloudError(static fn () => CloudVpcService::attachMember( $operationIncomplete['vpc_key'], $actor, [ 'instance_id' => 5001, 'request_key' => 'reuse-operation-block-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 50001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 60001)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 6)->value('desired_revision') === 1, 'A non-succeeded historical operation released its member/IP identities.'); // Every historical host is part of the absence proof; a stale host blocks // reuse even when the root and member pair claim to be exact. $hostIncomplete = $seedVpc(7, '10.47.0.0/24', '10.47.0.0/29', 60, 'incomplete', 'active', [ 'member_row_id' => 60002, 'instance_id' => 6002, 'private_ip' => '10.47.0.2', ]); $seedInstance(6001, 60); $seedInstance(6002, 60); $seedPair(60001, 70001, $hostIncomplete, 6001, 60, '10.47.0.1'); $seedPair(60002, 70002, $hostIncomplete, 6002, 60, '10.47.0.2', 'active'); $expectCloudError(static fn () => CloudVpcService::attachMember( $hostIncomplete['vpc_key'], $actor, [ 'instance_id' => 6001, 'request_key' => 'reuse-host-block-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 60001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 70001)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 7)->value('desired_revision') === 1 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 7)->count() === 1, 'An incomplete host proof released or mutated a member/IP tombstone.'); // An orphan lease is independent durable identity, not free capacity. It must // produce a controlled integrity error rather than a SQL unique-key failure. $orphanVpc = $seedVpc(8, '10.48.0.0/24', '10.48.0.0/29', 70); $seedInstance(7001, 70); Capsule::table('cloud_vpc_private_ips')->insert([ 'id' => 80001, 'ip_key' => $keyFor('orphan-private-ip', 80001), 'vpc_id' => 8, 'subnet_id' => $orphanVpc['subnet_id'], 'member_id' => null, 'instance_id' => 7001, 'private_ip' => '10.48.0.1', 'private_ip_int' => $ipToInt('10.48.0.1'), 'state' => 'deleted', 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $orphanVpc['digest'], 'observed_digest' => $orphanVpc['digest'], 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); $expectCloudError(static fn () => CloudVpcService::attachMember( $orphanVpc['vpc_key'], $actor, [ 'instance_id' => 7001, 'request_key' => 'reuse-orphan-block-0001', ] ), 503); $assert(Capsule::table('cloud_vpc_private_ips')->where('id', 80001)->count() === 1 && Capsule::table('cloud_vpc_members')->where('instance_id', 7001)->count() === 0 && (int) Capsule::table('cloud_vpcs')->where('id', 8)->value('desired_revision') === 1 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 8)->count() === 1, 'An orphan private-IP lease was reused, deleted or surfaced as a partial attach.'); // A historical operation cannot prove absence of identities created after it. // This closes a replay window where exact revision/digest fields were copied // onto a newer tombstone that the earlier host observation never contained. $createdLater = $seedVpc(9, '10.49.0.0/24', '10.49.0.0/29', 80, 'deleted', 'active'); $seedInstance(8001, 80); $seedPair(80001, 90001, $createdLater, 8001, 80, '10.49.0.1'); Capsule::table('cloud_vpc_members')->where('id', 80001)->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); Capsule::table('cloud_vpc_private_ips')->where('id', 90001)->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); $expectCloudError(static fn () => CloudVpcService::attachMember( $createdLater['vpc_key'], $actor, [ 'instance_id' => 8001, 'request_key' => 'reuse-created-later-block-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 80001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 90001)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 9)->value('desired_revision') === 1, 'A historical operation released identities created after its observation.'); // Both sides of the 1:1 ledger need a real creation timestamp. Accepting a // zero on either row would let a partially migrated identity borrow an older // operation merely because max(0, valid_timestamp) looked plausible. foreach ([ ['vpc_id' => 14, 'instance_id' => 14001, 'member_id' => 140001, 'private_id' => 140101, 'ip' => '10.54.0.1', 'zero' => 'member'], ['vpc_id' => 15, 'instance_id' => 15001, 'member_id' => 150001, 'private_id' => 150101, 'ip' => '10.55.0.1', 'zero' => 'private'], ] as $invalidCreatedAt) { $id = (int) $invalidCreatedAt['vpc_id']; $timestampVpc = $seedVpc( $id, '10.' . (40 + $id) . '.0.0/24', '10.' . (40 + $id) . '.0.0/29', 80, 'deleted', 'active' ); $seedInstance((int) $invalidCreatedAt['instance_id'], 80); $seedPair( (int) $invalidCreatedAt['member_id'], (int) $invalidCreatedAt['private_id'], $timestampVpc, (int) $invalidCreatedAt['instance_id'], 80, (string) $invalidCreatedAt['ip'] ); $table = $invalidCreatedAt['zero'] === 'member' ? 'cloud_vpc_members' : 'cloud_vpc_private_ips'; Capsule::table($table)->where( 'id', $invalidCreatedAt['zero'] === 'member' ? (int) $invalidCreatedAt['member_id'] : (int) $invalidCreatedAt['private_id'] )->update(['created_at' => 0]); $expectCloudError(static fn () => CloudVpcService::attachMember( $timestampVpc['vpc_key'], $actor, [ 'instance_id' => (int) $invalidCreatedAt['instance_id'], 'request_key' => 'reuse-zero-created-' . $invalidCreatedAt['zero'], ] ), 409); $assert(Capsule::table('cloud_vpc_members') ->where('id', (int) $invalidCreatedAt['member_id'])->count() === 1 && Capsule::table('cloud_vpc_private_ips') ->where('id', (int) $invalidCreatedAt['private_id'])->count() === 1, 'A zero creation timestamp released a partially migrated identity.'); } // A non-terminal route that points through the retired address keeps that // address semantically live even when another member owns the route row. $nextHopReferenced = $seedVpc(10, '10.50.0.0/24', '10.50.0.0/29', 90, 'active', 'active', [ 'member_row_id' => 90002, 'instance_id' => 9002, 'private_ip' => '10.50.0.2', ]); $seedInstance(9001, 90); $seedInstance(9002, 90); $seedPair(90001, 100001, $nextHopReferenced, 9001, 90, '10.50.0.1'); $seedPair(90002, 100002, $nextHopReferenced, 9002, 90, '10.50.0.2', 'active'); $seedRoute(100001, $nextHopReferenced, 90002, '10.50.0.3/32', '10.50.0.1'); $expectCloudError(static fn () => CloudVpcService::attachMember( $nextHopReferenced['vpc_key'], $actor, [ 'instance_id' => 9001, 'request_key' => 'reuse-next-hop-block-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 90001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 100001)->count() === 1 && Capsule::table('cloud_vpc_routes')->where('id', 100001) ->where('state', 'active')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 10)->value('desired_revision') === 1, 'A route next hop still using the retired address did not keep the pair reserved.'); // The old /32 is also reserved when a live route still targets it. Include the // same route in the signed historical desired document so both persisted SQL // state and operation replay independently demonstrate the unsafe reference. $destinationSiblingKey = $keyFor('member', 100002); $destinationPayloadRoute = [ 'route_id' => $keyFor('desired-route', 11), 'destination_cidr' => '10.51.0.1/32', 'next_hop_member_id' => $destinationSiblingKey, 'metric' => 100, ]; $destinationReferenced = $seedVpc(11, '10.51.0.0/24', '10.51.0.0/29', 100, 'active', 'active', [ 'member_row_id' => 100002, 'instance_id' => 10002, 'private_ip' => '10.51.0.2', ], [$destinationPayloadRoute]); $seedInstance(10001, 100); $seedInstance(10002, 100); $seedPair(100001, 110001, $destinationReferenced, 10001, 100, '10.51.0.1'); $seedPair(100002, 110002, $destinationReferenced, 10002, 100, '10.51.0.2', 'active'); $seedRoute(110001, $destinationReferenced, 100002, '10.51.0.1/32', '10.51.0.2'); $expectCloudError(static fn () => CloudVpcService::attachMember( $destinationReferenced['vpc_key'], $actor, [ 'instance_id' => 10001, 'request_key' => 'reuse-destination-block-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 100001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 110001)->count() === 1 && Capsule::table('cloud_vpc_routes')->where('id', 110001) ->where('destination_cidr', '10.51.0.1/32')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 11)->value('desired_revision') === 1, 'A live destination /32 reference released the retired member/IP pair.'); // A custom /32 is part of the same route namespace as implicit member /32s. // Automatic allocation must skip it, and route replacement must not create the // inverse conflict against a live member. $routeReservedMemberKey = $keyFor('member', 190002); $routeReservedPayload = [[ 'route_id' => $keyFor('route', 190001), 'destination_cidr' => '10.59.0.1/32', 'next_hop_member_id' => $routeReservedMemberKey, 'metric' => 100, ]]; $routeReserved = $seedVpc(19, '10.59.0.0/24', '10.59.0.0/29', 110, 'active', 'active', [ 'member_row_id' => 190002, 'instance_id' => 19002, 'private_ip' => '10.59.0.2', ], $routeReservedPayload); $seedInstance(19001, 110); $seedInstance(19002, 110); $seedInstance(19003, 110); $seedPair(190002, 190102, $routeReserved, 19002, 110, '10.59.0.2', 'active'); $seedRoute(190001, $routeReserved, 190002, '10.59.0.1/32', '10.59.0.2'); $expectCloudError(static fn () => CloudVpcService::replaceRoutes( $routeReserved['vpc_key'], $actor, [ 'routes' => [[ 'route_key' => $keyFor('route-conflict', 190002), 'destination_cidr' => '10.59.0.2/32', 'next_hop_ip' => '10.59.0.2', 'metric' => 100, ]], 'request_key' => 'route-member-conflict-0001', ] ), 409); CloudVpcService::attachMember($routeReserved['vpc_key'], $actor, [ 'instance_id' => 19003, 'request_key' => 'route-ip-auto-0001', ]); $assert(Capsule::table('cloud_vpc_members')->where('instance_id', 19001)->count() === 0 && Capsule::table('cloud_vpc_members')->where('instance_id', 19003) ->value('private_ip') === '10.59.0.3' && Capsule::table('cloud_vpc_routes')->where('id', 190001) ->where('state', 'active')->count() === 1, 'Attach or route replacement allowed an implicit/custom /32 collision.'); // H2 existed as an exact deleted deployment when the pair's revision-1 // removal operation omitted it, then later rejoined. Its mutable deployment // row is active now, so only the exact current root operation (covering H1 and // H2 with full typed evidence) may supersede the older incomplete host set. $rejoinedHost = $seedVpc(12, '10.52.0.0/24', '10.52.0.0/29', 110, 'deleted', 'active'); $seedRemovedDeployment($rejoinedHost, 120); $seedInstance(12001, 110); $seedInstance(12002, 110); $seedInstance(12003, 120); $seedPair(120001, 120101, $rejoinedHost, 12001, 110, '10.52.0.1'); $seedPair(120002, 120102, $rejoinedHost, 12002, 110, '10.52.0.2', 'active'); $seedPair(120003, 120103, $rejoinedHost, 12003, 120, '10.52.0.3', 'active'); Capsule::table('cloud_vpc_members')->whereIn('id', [120002, 120003])->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); Capsule::table('cloud_vpc_private_ips') ->whereIn('member_id', [120002, 120003])->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); $installCurrentProof($rejoinedHost, [ ['member_row_id' => 120002, 'instance_id' => 12002, 'host_id' => 110, 'private_ip' => '10.52.0.2'], ['member_row_id' => 120003, 'instance_id' => 12003, 'host_id' => 120, 'private_ip' => '10.52.0.3'], ], [110, 120]); CloudVpcService::attachMember($rejoinedHost['vpc_key'], $actor, [ 'instance_id' => 12001, 'request_key' => 'reuse-current-host-set-0001', ]); $assert(Capsule::table('cloud_vpc_members')->where('id', 120001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 120101)->count() === 0 && Capsule::table('cloud_vpc_members')->where('instance_id', 12001) ->where('private_ip', '10.52.0.1')->count() === 1 && Capsule::table('cloud_vpc_members')->where('id', 120003) ->where('state', 'active')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 12)->value('desired_revision') === 3, 'An exact current all-host proof did not supersede the older omitted-H2 operation.'); // The same rejoin history stays fail-closed if the current signed target names // H2 but its typed observed host map omits H2. $missingRejoinedHost = $seedVpc(13, '10.53.0.0/24', '10.53.0.0/29', 130, 'deleted', 'active'); $seedRemovedDeployment($missingRejoinedHost, 140); $seedInstance(13001, 130); $seedInstance(13002, 130); $seedInstance(13003, 140); $seedPair(130001, 130101, $missingRejoinedHost, 13001, 130, '10.53.0.1'); $seedPair(130002, 130102, $missingRejoinedHost, 13002, 130, '10.53.0.2', 'active'); $seedPair(130003, 130103, $missingRejoinedHost, 13003, 140, '10.53.0.3', 'active'); Capsule::table('cloud_vpc_members')->whereIn('id', [130002, 130003])->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); Capsule::table('cloud_vpc_private_ips') ->whereIn('member_id', [130002, 130003])->update([ 'created_at' => $now + 1, 'updated_at' => $now + 1, ]); $installCurrentProof($missingRejoinedHost, [ ['member_row_id' => 130002, 'instance_id' => 13002, 'host_id' => 130, 'private_ip' => '10.53.0.2'], ['member_row_id' => 130003, 'instance_id' => 13003, 'host_id' => 140, 'private_ip' => '10.53.0.3'], ], [130]); $expectCloudError(static fn () => CloudVpcService::attachMember( $missingRejoinedHost['vpc_key'], $actor, [ 'instance_id' => 13001, 'request_key' => 'reuse-current-host-missing-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('id', 130001)->count() === 1 && Capsule::table('cloud_vpc_private_ips')->where('id', 130101)->count() === 1 && Capsule::table('cloud_vpc_host_deployments')->where('vpc_id', 13) ->where('host_id', 140)->where('state', 'active')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 13)->value('desired_revision') === 2, 'A current proof missing rejoined H2 released the historical tombstone.'); // R can be only a VM-local deleted marker while its signed payload still // contains the member. Q is the later all-host operation that actually proves // the empty owner removed; U may already be a newer uncertain target that // omits the deleted deployment. Replay Q via the exact owner receipt instead // of trusting R or requiring U to finish first. $ownerRemoval = $seedVpc(16, '10.56.0.0/24', '10.56.0.0/29', 90, 'active', 'active', [ 'member_row_id' => 160001, 'instance_id' => 16001, 'private_ip' => '10.56.0.1', ]); $seedInstance(16001, 90); $seedPair(160001, 160101, $ownerRemoval, 16001, 90, '10.56.0.1'); $installRemovalProof($ownerRemoval, 90, true); CloudVpcService::attachMember($ownerRemoval['vpc_key'], $actor, [ 'instance_id' => 16001, 'request_key' => 'reuse-owner-proof-0001', ]); $assert(Capsule::table('cloud_vpc_members')->where('id', 160001)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('id', 160101)->count() === 0 && Capsule::table('cloud_vpc_members')->where('instance_id', 16001) ->where('private_ip', '10.56.0.1')->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 16) ->value('desired_revision') === 4, 'The exact owner-removal operation did not release a VM-local tombstone.'); // A partial R2 may persist H1's exact removal while H2 keeps the operation // uncertain. A later successful R3 is allowed to omit H1, but may combine its // global closure with that earlier owner receipt only when the pair carries // the exact same R2 revision/digest. foreach ([ ['vpc_id' => 17, 'mismatch' => false, 'created_late' => false], ['vpc_id' => 18, 'mismatch' => true, 'created_late' => false], ['vpc_id' => 20, 'mismatch' => false, 'created_late' => true], ] as $partialCase) { $id = (int) $partialCase['vpc_id']; $retiredMemberId = $id * 10000 + 1; $siblingMemberId = $id * 10000 + 2; $retiredPrivateId = $id * 10000 + 101; $siblingPrivateId = $id * 10000 + 102; $retiredInstanceId = $id * 1000 + 1; $siblingInstanceId = $id * 1000 + 2; $secondOctet = 40 + $id; $partialVpc = $seedVpc( $id, '10.' . $secondOctet . '.0.0/24', '10.' . $secondOctet . '.0.0/29', 100, 'active', 'active', [ 'member_row_id' => $siblingMemberId, 'instance_id' => $siblingInstanceId, 'private_ip' => '10.' . $secondOctet . '.0.2', ] ); $seedInstance($retiredInstanceId, 90); $seedInstance($siblingInstanceId, 100); $seedPair( $retiredMemberId, $retiredPrivateId, $partialVpc, $retiredInstanceId, 90, '10.' . $secondOctet . '.0.1' ); $seedPair( $siblingMemberId, $siblingPrivateId, $partialVpc, $siblingInstanceId, 100, '10.' . $secondOctet . '.0.2', 'active' ); $seedRemovedDeployment($partialVpc, 90); $partialOwnerProof = $installRemovalProof($partialVpc, 90); Capsule::table('cloud_vpc_operations') ->where('operation_id', $partialOwnerProof['operation_id'])->update([ 'status' => 'uncertain', 'error_code' => 'vpc_observation_incomplete', 'finished_at' => null, ]); foreach (['cloud_vpc_members', 'cloud_vpc_private_ips'] as $table) { Capsule::table($table) ->where('id', $table === 'cloud_vpc_members' ? $retiredMemberId : $retiredPrivateId) ->update([ 'desired_revision' => 2, 'observed_revision' => 2, 'desired_digest' => $partialOwnerProof['digest'], 'observed_digest' => $partialOwnerProof['digest'], 'last_error_code' => null, ]); } if ($partialCase['created_late']) { Capsule::table('cloud_vpc_members')->where('id', $retiredMemberId) ->update(['created_at' => $now + 3, 'updated_at' => $now + 3]); Capsule::table('cloud_vpc_private_ips')->where('id', $retiredPrivateId) ->update(['created_at' => $now + 3, 'updated_at' => $now + 3]); } $installCurrentProof($partialVpc, [[ 'member_row_id' => $siblingMemberId, 'instance_id' => $siblingInstanceId, 'host_id' => 100, 'private_ip' => '10.' . $secondOctet . '.0.2', ]], [100], 3, 'later-global-operation', $partialCase['created_late'] ? 4 : 2); if ($partialCase['mismatch'] || $partialCase['created_late']) { $reason = $partialCase['mismatch'] ? 'mismatch' : 'created-late'; $mismatchDigest = hash('sha256', 'owner-proof-mismatch:' . $id); if ($partialCase['mismatch']) { Capsule::table('cloud_vpc_members')->where('id', $retiredMemberId) ->update(['observed_digest' => $mismatchDigest]); Capsule::table('cloud_vpc_private_ips')->where('id', $retiredPrivateId) ->update(['observed_digest' => $mismatchDigest]); } $expectCloudError(static fn () => CloudVpcService::attachMember( $partialVpc['vpc_key'], $actor, [ 'instance_id' => $retiredInstanceId, 'request_key' => 'reuse-partial-' . $reason . '-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members') ->where('id', $retiredMemberId)->count() === 1 && Capsule::table('cloud_vpc_private_ips') ->where('id', $retiredPrivateId)->count() === 1, 'An invalid partial owner receipt released the tombstone.'); continue; } CloudVpcService::attachMember($partialVpc['vpc_key'], $actor, [ 'instance_id' => $retiredInstanceId, 'request_key' => 'reuse-partial-owner-0001', ]); $assert(Capsule::table('cloud_vpc_members') ->where('id', $retiredMemberId)->count() === 0 && Capsule::table('cloud_vpc_private_ips') ->where('id', $retiredPrivateId)->count() === 0 && Capsule::table('cloud_vpc_members') ->where('instance_id', $retiredInstanceId) ->where('private_ip', '10.' . $secondOctet . '.0.1')->count() === 1, 'A later global proof did not combine with the exact partial owner receipt.'); } // Tenant membership intent contains only the instance and idempotency key. // Subnet selection and private-IP allocation are platform policy, even when a // caller guesses the current automatically managed values. $strictAttach = $seedVpc( 22, '10.62.0.0/24', '10.62.0.0/29', 160, 'active', 'active' ); $seedInstance(22001, 160); foreach ([ 'subnet_key' => $strictAttach['subnet_key'], 'private_ip' => '10.62.0.1', ] as $field => $value) { $expectCloudError(static fn () => CloudVpcService::attachMember( $strictAttach['vpc_key'], $actor, [ 'instance_id' => 22001, 'request_key' => 'strict-attach-' . str_replace('_', '-', $field), $field => $value, ] ), 422); } $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 22)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 22)->count() === 0 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 22)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 22) ->value('desired_revision') === 1, 'Forbidden subnet/private-IP fields changed durable membership state.'); // A missing or cleaning automatic subnet is a retryable policy conflict. The // service must not fabricate a subnet or allocate an address while its only // durable subnet identity is still a tombstone. $cleaningSubnet = $seedVpc( 23, '10.63.0.0/24', '10.63.0.0/29', 170, 'active', 'active' ); $seedInstance(23001, 170); Capsule::table('cloud_vpc_subnets') ->where('id', $cleaningSubnet['subnet_id'])->update(['state' => 'deleting']); $expectCloudError(static fn () => CloudVpcService::attachMember( $cleaningSubnet['vpc_key'], $actor, [ 'instance_id' => 23001, 'request_key' => 'cleaning-subnet-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 23)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 23)->count() === 0 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 23)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 23) ->value('desired_revision') === 1, 'A cleaning automatic subnet allowed a partial member attachment.'); // The same conflict applies if a damaged/historical root has no remaining // subnet row at all. Absence is not permission to recreate policy state during // an unrelated membership request. $missingSubnet = $seedVpc( 25, '10.65.0.0/24', '10.65.0.0/29', 190, 'active', 'active' ); $seedInstance(25001, 190); Capsule::table('cloud_vpc_subnets') ->where('id', $missingSubnet['subnet_id'])->delete(); $expectCloudError(static fn () => CloudVpcService::attachMember( $missingSubnet['vpc_key'], $actor, [ 'instance_id' => 25001, 'request_key' => 'missing-subnet-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 25)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 25)->count() === 0 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 25)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 25) ->value('desired_revision') === 1, 'A missing automatic subnet allowed a partial member attachment.'); // Corrupted automatic-subnet identity must fail before desired-state signing; // otherwise the engine would reject the malformed subnet_id after local rows // had already been allocated. $invalidSubnetKey = $seedVpc( 26, '10.66.0.0/24', '10.66.0.0/29', 200, 'active', 'active' ); $seedInstance(26001, 200); Capsule::table('cloud_vpc_subnets') ->where('id', $invalidSubnetKey['subnet_id']) ->update(['subnet_key' => 'invalid-subnet-key']); $expectCloudError(static fn () => CloudVpcService::attachMember( $invalidSubnetKey['vpc_key'], $actor, [ 'instance_id' => 26001, 'request_key' => 'invalid-subnet-key-0001', ] ), 503); $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 26)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 26)->count() === 0 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 26)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 26) ->value('desired_revision') === 1, 'A malformed automatic subnet identity leaked partial member state.'); // Historical multi-subnet roots remain readable/deletable, but automatic IPAM // cannot choose between two live ranges. Fail closed before allocating any // member or operation rather than silently selecting the oldest row. $multipleSubnets = $seedVpc( 24, '10.64.0.0/24', '10.64.0.0/29', 180, 'active', 'active' ); $seedInstance(24001, 180); $secondSubnetNetwork = $ipToInt('10.64.0.8'); Capsule::table('cloud_vpc_subnets')->insert([ 'id' => 2024, 'subnet_key' => $keyFor('subnet-extra', 24), 'vpc_id' => 24, 'cidr' => '10.64.0.8/29', 'network_address' => $secondSubnetNetwork, 'broadcast_address' => $secondSubnetNetwork + 7, 'prefix_length' => 29, 'gateway_ip' => null, 'state' => 'active', 'desired_revision' => 1, 'observed_revision' => 1, 'desired_digest' => $multipleSubnets['digest'], 'observed_digest' => $multipleSubnets['digest'], 'last_error_code' => null, 'created_at' => $now, 'updated_at' => $now, ]); $expectCloudError(static fn () => CloudVpcService::attachMember( $multipleSubnets['vpc_key'], $actor, [ 'instance_id' => 24001, 'request_key' => 'multiple-subnets-0001', ] ), 503); $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 24)->count() === 0 && Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 24)->count() === 0 && Capsule::table('cloud_vpc_subnets')->where('vpc_id', 24) ->where('state', 'active')->count() === 2 && Capsule::table('cloud_vpc_operations')->where('vpc_id', 24)->count() === 1 && (int) Capsule::table('cloud_vpcs')->where('id', 24) ->value('desired_revision') === 1, 'A historical multi-subnet root was mutated or silently auto-selected.'); // A deleting member still owns its address and possible host-side device. // It must consume capacity until exact tombstone convergence removes it. $deletingCapacity = $seedVpc( 21, '10.61.0.0/24', '10.61.0.0/29', 150, 'active', 'active' ); Capsule::table('cloud_vpcs')->where('id', 21)->update(['max_members' => 1]); $seedInstance(21001, 150); $seedInstance(21002, 150); $seedPair( 210001, 210101, $deletingCapacity, 21001, 150, '10.61.0.1', 'deleting' ); $expectCloudError(static fn () => CloudVpcService::attachMember( $deletingCapacity['vpc_key'], $actor, [ 'instance_id' => 21002, 'request_key' => 'deleting-capacity-0001', ] ), 409); $assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 21)->count() === 1 && Capsule::table('cloud_vpc_members')->where('instance_id', 21002)->count() === 0 && (int) Capsule::table('cloud_vpcs')->where('id', 21) ->value('desired_revision') === 1, 'A deleting member stopped consuming capacity before tombstone convergence.'); echo "VPC member and private-IP reuse contracts passed.\n";