From ac67a2de7b6e6d913993fd2c884b095dfda71a25 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 1 Nov 2026 00:07:57 +0800 Subject: [PATCH] feat(traffic): enforce aggregate network restrictions --- .../Cloud/CloudInstanceNetworkPolicy.php | 129 ++++++++- .../Services/Cloud/CloudLifecycleService.php | 132 +++++++-- .../Cloud/CloudPlanUpgradeService.php | 6 +- .../Services/Cloud/CloudRenewalService.php | 6 +- web/app/Services/Cloud/CloudWorkerService.php | 136 +++++++-- web/tests/CloudBillingLifecycleTest.php | 126 +++++++++ web/tests/CloudFinancialWorkflowTest.php | 2 + web/tests/CloudInstanceNetworkPolicyTest.php | 267 +++++++++++++++++- web/tests/CloudPlanUpgradeServiceTest.php | 25 ++ ...fficRestrictionIntegrationContractTest.php | 45 +++ 10 files changed, 812 insertions(+), 62 deletions(-) create mode 100644 web/tests/CloudTrafficRestrictionIntegrationContractTest.php diff --git a/web/app/Services/Cloud/CloudInstanceNetworkPolicy.php b/web/app/Services/Cloud/CloudInstanceNetworkPolicy.php index bbe93e4f..bca96c56 100644 --- a/web/app/Services/Cloud/CloudInstanceNetworkPolicy.php +++ b/web/app/Services/Cloud/CloudInstanceNetworkPolicy.php @@ -132,14 +132,17 @@ final class CloudInstanceNetworkPolicy $now ); $trafficDecision = $blocked ? 'asserted' : 'cleared'; - } elseif ($rows->firstWhere( - 'reason_code', self::REASON_TRAFFIC - ) === null && in_array((string) $instance->network_status, [ - 'blocking', 'blocked_traffic', 'block_failed', - ], true)) { - // A rollout can meet an already suspended instance while - // the shared projection is unavailable. Preserve that - // known-safe physical state instead of guessing a restore. + } elseif ((bool) $state->applied_suspended + || in_array((string) $instance->network_status, [ + 'blocking', 'blocked_traffic', 'blocked_expired', + 'block_failed', 'restore_pending', 'restoring', + 'restore_failed', + ], true)) { + // Missing/attention accounting is never authority to + // restore a known-blocked instance. Reassert the traffic + // fence even when a prior available projection had already + // cleared this row; a queued resume will then be skipped, + // while an already-dispatched resume is compensated. self::setReasonFact( $instance, $rows, @@ -150,6 +153,7 @@ final class CloudInstanceNetworkPolicy . ':untrusted', $now ); + $trafficDecision = 'fenced'; } } } @@ -250,9 +254,94 @@ final class CloudInstanceNetworkPolicy 409 ); } - return $kind === 'network_suspend' - ? (bool) $state->desired_suspended - : !(bool) $state->desired_suspended; + if ($kind === 'network_suspend') { + return (bool) $state->desired_suspended; + } + if ((bool) $state->desired_suspended) { + return false; + } + + // Accounting and instance convergence run in separate transactions. + // Revalidate the current enforced pool at the last pre-dispatch + // boundary so an old queued resume cannot cross a newly persisted + // exhausted/attention/missing projection before Lifecycle has copied + // that fact into the instance policy state. These are plain reads: + // the policy lock domain never takes a traffic-pool row lock. + return !self::currentResumeFence($instanceId); + } + + private static function currentResumeFence(int $instanceId): bool + { + $instance = CloudInstance::find($instanceId); + if ($instance === null) { + return true; + } + if ((string) $instance->service_status !== 'exempt' + && !$instance->serviceAvailable()) { + return true; + } + if ($instance->current_period_id === null) { + return false; + } + $period = CloudServicePeriod::where( + 'id', (int) $instance->current_period_id + )->where('instance_id', $instanceId)->where( + 'user_id', (int) $instance->user_id + )->first(); + if ($period === null || !self::isEnforcedPeriod($period)) { + return false; + } + if ((string) $period->traffic_quota_mode !== 'finite') { + return false; + } + + $now = time(); + if (!in_array((string) $period->status, + ['active', 'exhausted'], true) + || (int) $period->starts_at > $now + || (int) $period->ends_at <= $now) { + return true; + } + $pool = CloudTrafficPool::where( + 'user_id', (int) $instance->user_id + )->where( + 'traffic_pool_group_id', + (int) $period->traffic_pool_group_id + )->first(); + + return $pool === null + || (string) $pool->state !== 'available' + || (int) $pool->overage_bytes > 0; + } + + /** @return array|null */ + public static function payloadMarker( + array $payload, + int $instanceId, + string $operationId + ): ?array { + if (!array_key_exists('_network_policy', $payload)) { + return null; + } + $marker = $payload['_network_policy']; + if (!is_array($marker) || array_is_list($marker) + || ($marker['version'] ?? null) !== 1 + || ($marker['instance_id'] ?? null) !== $instanceId + || !is_string($marker['operation_id'] ?? null) + || !hash_equals($operationId, $marker['operation_id']) + || !is_int($marker['policy_revision'] ?? null) + || $marker['policy_revision'] < 1 + || !is_bool($marker['desired_suspended'] ?? null) + || !is_bool($marker['applied_suspended'] ?? null) + || !is_string($marker['suspend_reason'] ?? null) + || !in_array($marker['suspend_reason'], + ['traffic', 'expired'], true)) { + throw new CloudRecoveryPayloadUnavailable( + '网络策略任务缺少不可变执行快照', + 409 + ); + } + return $marker; } /** @@ -347,6 +436,17 @@ final class CloudInstanceNetworkPolicy return self::snapshot($state, $rows); } + public static function releaseAfterInstanceDeletedLocked( + CloudInstance $instance + ): void { + CloudInstanceNetworkPolicyState::where( + 'instance_id', (int) $instance->id + )->delete(); + CloudInstanceNetworkRestriction::where( + 'instance_id', (int) $instance->id + )->delete(); + } + private static function lockState( CloudInstance $instance, int $now @@ -491,10 +591,9 @@ final class CloudInstanceNetworkPolicy ? (self::hasDesired($rows, self::REASON_SERVICE) ? 'blocking_expired' : 'blocking') : 'restore_pending'; - if (!(bool) $state->desired_suspended) { - $instance->network_suspended_at = $instance->network_suspended_at - ?? $now; - } + $instance->network_suspended_at = + (bool) $state->applied_suspended + ? ($instance->network_suspended_at ?? $now) : null; } elseif ($policyState === 'applied') { if ((bool) $state->applied_suspended) { $instance->network_status = self::hasDesired( diff --git a/web/app/Services/Cloud/CloudLifecycleService.php b/web/app/Services/Cloud/CloudLifecycleService.php index 9c4bc418..64c9bd46 100644 --- a/web/app/Services/Cloud/CloudLifecycleService.php +++ b/web/app/Services/Cloud/CloudLifecycleService.php @@ -345,6 +345,7 @@ final class CloudLifecycleService $period = $instance->current_period_id === null ? null : CloudServicePeriod::where('id', $instance->current_period_id) ->where('instance_id', $instance->id)->lockForUpdate()->first(); + $policyPeriod = $period; if ($period !== null && in_array((string) $period->status, ['active', 'exhausted'], true)) { $this->updateUsage($instance, $period, $now); @@ -397,18 +398,30 @@ final class CloudLifecycleService $instance->traffic_quota_bytes = max(0, (int) $period->traffic_quota_bytes); $instance->traffic_download_bytes = (int) $period->download_used_bytes; $instance->traffic_upload_bytes = (int) $period->upload_used_bytes; + $policyPeriod = $period; + $aggregateNetworkPolicy = + CloudInstanceNetworkPolicy::isEnforcedPeriod($period) + || CloudInstanceNetworkPolicy::hasState( + (int) $instance->id + ); $this->convergePaidPeriodProjectionLocked( $instance, - $now + $now, + !$aggregateNetworkPolicy ); } } + $aggregateNetworkPolicy = + CloudInstanceNetworkPolicy::isEnforcedPeriod( + $period ?? $policyPeriod + ) || CloudInstanceNetworkPolicy::hasState((int) $instance->id); if ($period !== null) { $usageKnown = $this->updateUsage($instance, $period, $now); $used = (int) $period->download_used_bytes + (int) $period->upload_used_bytes; $quota = max(0, (int) $period->traffic_quota_bytes); - if ($usageKnown && $quota > 0 && $used >= $quota) { + if (!$aggregateNetworkPolicy + && $usageKnown && $quota > 0 && $used >= $quota) { if ((string) $period->status !== 'exhausted') { $period->status = 'exhausted'; $period->updated_at = $now; @@ -421,7 +434,8 @@ final class CloudLifecycleService } elseif ($usageKnown) { $this->convergePaidPeriodProjectionLocked( $instance, - $now + $now, + !$aggregateNetworkPolicy ); } } elseif ($instance->expires_at !== null && (int) $instance->expires_at <= $now) { @@ -467,18 +481,55 @@ final class CloudLifecycleService // down; a paid renewal restores those links. During an $instance->service_status = 'expired_grace'; $instance->suspended_at = null; - $network = (string) $instance->network_status; - if ($network === 'blocked_traffic') { - $instance->network_status = 'blocked_expired'; - } elseif (!in_array($network, - ['blocking', 'blocking_expired', 'blocked_expired', - 'block_failed'], true)) { - $instance->network_status = 'blocking_expired'; + if (!$aggregateNetworkPolicy) { + $network = (string) $instance->network_status; + if ($network === 'blocked_traffic') { + $instance->network_status = 'blocked_expired'; + } elseif (!in_array($network, + ['blocking', 'blocking_expired', 'blocked_expired', + 'block_failed'], true)) { + $instance->network_status = 'blocking_expired'; + } } } } + $networkPolicy = null; + if ($aggregateNetworkPolicy) { + $networkPolicy = CloudInstanceNetworkPolicy::synchronizeLocked( + $instance, + $period ?? $policyPeriod, + $now + ); + } + $policyDeferred = ($networkPolicy['deferred'] ?? false) === true; $network = (string) $instance->network_status; - if ($network === 'block_failed') { + if ($aggregateNetworkPolicy && !$policyDeferred + && ($networkPolicy['state'] ?? null) === 'failed') { + $retryKind = ($networkPolicy['desired_suspended'] ?? false) + === true && ($networkPolicy['applied_suspended'] ?? true) + === false ? 'network_suspend' + : ((($networkPolicy['desired_suspended'] ?? true) === false + && ($networkPolicy['applied_suspended'] ?? false) === true) + ? 'network_resume' : null); + if ($retryKind !== null + && $this->retryableNotAppliedJobLocked( + $instance, + $retryKind, + $now + ) !== null + && CloudInstanceNetworkPolicy::allowRetryLocked( + $instance, + $retryKind, + $now + )) { + $networkPolicy = + CloudInstanceNetworkPolicy::snapshotLocked( + $instance, + $now + ); + } + } elseif ((!$aggregateNetworkPolicy || $policyDeferred) + && $network === 'block_failed') { $reason = null; $nextState = null; if ($period !== null @@ -500,7 +551,8 @@ final class CloudLifecycleService ) !== null) { $instance->network_status = $nextState; } - } elseif ($network === 'restore_failed' + } elseif ((!$aggregateNetworkPolicy || $policyDeferred) + && $network === 'restore_failed' && $period !== null && (string) $period->status === 'active' && $this->retryableNotAppliedJobLocked( @@ -531,6 +583,33 @@ final class CloudLifecycleService CloudJobService::enqueueExpiryDelete($instance); return true; } + if ($aggregateNetworkPolicy && !$policyDeferred) { + $action = (string) ($networkPolicy['action'] ?? 'none'); + if ($action === 'suspend') { + return $this->enqueue( + $instance, + 'network_suspend', + 'network/suspend', + (string) ($networkPolicy['suspend_reason'] ?? 'traffic'), + [], + true + ); + } + if ($action === 'resume') { + $instance->network_status = 'restoring'; + $instance->updated_at = $now; + $instance->save(); + return $this->enqueue( + $instance, + 'network_resume', + 'network/resume', + null, + [], + true + ); + } + return false; + } if ($network === 'blocking_expired') { return $this->enqueue($instance, 'network_suspend', 'network/suspend', 'expired', ['reason' => 'expired']); @@ -564,7 +643,8 @@ final class CloudLifecycleService */ private function convergePaidPeriodProjectionLocked( CloudInstance $instance, - int $now + int $now, + bool $convergeNetwork = true ): void { $service = (string) $instance->service_status; if ($service === 'suspend_failed') { @@ -590,6 +670,9 @@ final class CloudLifecycleService $instance->suspended_at = null; } + if (!$convergeNetwork) { + return; + } $network = (string) $instance->network_status; if ($network === 'block_failed') { if ($this->retryableNotAppliedJobLocked( @@ -787,23 +870,40 @@ final class CloudLifecycleService } private function enqueue(CloudInstance $instance, string $kind, string $suffix, - ?string $reason = null, array $body = []): bool + ?string $reason = null, array $body = [], + bool $aggregateNetworkPolicy = false): bool { $job = new CloudJob(); $job->operation_id = bin2hex(random_bytes(16)); + $networkPolicyMarker = null; + if ($aggregateNetworkPolicy) { + $networkPolicyMarker = + CloudInstanceNetworkPolicy::prepareJobLocked( + $instance, + $kind, + (string) $job->operation_id + ); + if ($kind === 'network_suspend') { + $reason = (string) $networkPolicyMarker['suspend_reason']; + } + } $job->instance_id = $instance->id; $job->user_id = $instance->user_id; $job->host_id = $instance->host_id; $job->kind = $kind; $job->execution_mode = 'agent'; $job->status = 'queued'; - $job->sealed_payload = CloudSecrets::encrypt([ + $payload = [ 'method' => 'POST', 'request_target' => '/v1/vms/' . rawurlencode($instance->uuid) . '/' . $suffix, 'body' => $body, 'owner_id' => (int) $instance->user_id, 'system_policy' => true, 'suspend_reason' => $reason, - ], 'job-payload'); + ]; + if ($networkPolicyMarker !== null) { + $payload['_network_policy'] = $networkPolicyMarker; + } + $job->sealed_payload = CloudSecrets::encrypt($payload, 'job-payload'); $job->available_at = $job->created_at = $job->updated_at = time(); $job->save(); return true; diff --git a/web/app/Services/Cloud/CloudPlanUpgradeService.php b/web/app/Services/Cloud/CloudPlanUpgradeService.php index a939ea20..6076f977 100644 --- a/web/app/Services/Cloud/CloudPlanUpgradeService.php +++ b/web/app/Services/Cloud/CloudPlanUpgradeService.php @@ -570,7 +570,11 @@ final class CloudPlanUpgradeService if ((string) $period->status === 'exhausted' && ($quota === 0 || $used < $quota)) { $period->status = 'active'; - if (in_array((string) $instance->network_status, + $aggregateNetworkPolicy = + CloudInstanceNetworkPolicy::isEnforcedPeriod($period) + || CloudInstanceNetworkPolicy::hasState((int) $instance->id); + if (!$aggregateNetworkPolicy + && in_array((string) $instance->network_status, ['blocking', 'blocked_traffic', 'block_failed'], true)) { $instance->network_status = 'restore_pending'; $instance->network_suspended_at = null; diff --git a/web/app/Services/Cloud/CloudRenewalService.php b/web/app/Services/Cloud/CloudRenewalService.php index 0d2f0aca..cc647011 100644 --- a/web/app/Services/Cloud/CloudRenewalService.php +++ b/web/app/Services/Cloud/CloudRenewalService.php @@ -180,6 +180,9 @@ final class CloudRenewalService $user->save(); $locked->expires_at = $newExpiry; if ($period->status === 'active') { + $aggregateNetworkPolicy = + CloudInstanceNetworkPolicy::isEnforcedPeriod($period) + || CloudInstanceNetworkPolicy::hasState((int) $locked->id); $locked->current_period_id = $period->id; $locked->traffic_quota_bytes = $addedTraffic; $locked->traffic_download_bytes = 0; @@ -198,7 +201,8 @@ final class CloudRenewalService $locked->service_status = 'active'; $locked->suspended_at = null; } - if (!in_array((string) $locked->network_status, [ + if (!$aggregateNetworkPolicy + && !in_array((string) $locked->network_status, [ 'active', 'blocking', 'blocking_expired', diff --git a/web/app/Services/Cloud/CloudWorkerService.php b/web/app/Services/Cloud/CloudWorkerService.php index 169578cf..7b91c537 100644 --- a/web/app/Services/Cloud/CloudWorkerService.php +++ b/web/app/Services/Cloud/CloudWorkerService.php @@ -386,7 +386,20 @@ final class CloudWorkerService CloudVpcPortService::assertJobRunnable($job, $payload); if ($systemPolicy && $job->instance_id !== null) { $instance = CloudInstance::find($job->instance_id); - $desired = self::policyDesired($instance, (string) $job->kind); + $policyMarker = in_array((string) $job->kind, + ['network_suspend', 'network_resume'], true) + ? CloudInstanceNetworkPolicy::payloadMarker( + $payload, + (int) $job->instance_id, + (string) $job->operation_id + ) : null; + $desired = $policyMarker === null + ? self::policyDesired($instance, (string) $job->kind) + : CloudInstanceNetworkPolicy::jobStillDesired( + (int) $job->instance_id, + (string) $job->kind, + (string) $job->operation_id + ); if (!$desired) { $this->finish($job, ['status' => 'succeeded', 'result' => ['skipped' => true]], $payload); return true; @@ -2976,23 +2989,44 @@ final class CloudWorkerService '系统策略任务关联实例已缺失', 409 ); } - $expected = [ - 'service_suspend' => ['suspending_expired', 'purge_stopping'], - 'network_suspend' => ['blocking', 'blocking_expired'], - 'network_resume' => ['restore_pending', 'restoring'], - ][$job->kind]; - $already = match ((string) $job->kind) { - 'service_suspend' => in_array( - (string) $instance->service_status, - ['suspend_failed', 'purge_stop_failed'], true - ), - 'network_suspend' => (string) $instance->network_status - === 'block_failed', - 'network_resume' => (string) $instance->network_status - === 'restore_failed', - default => false, - }; - $transitioned = false; + $networkPolicyMarker = in_array((string) $job->kind, + ['network_suspend', 'network_resume'], true) + ? CloudInstanceNetworkPolicy::payloadMarker( + $payload, + (int) $job->instance_id, + (string) $job->operation_id + ) : null; + if ($networkPolicyMarker !== null) { + CloudInstanceNetworkPolicy::finishJobLocked( + $instance, + (string) $job->kind, + (string) $job->operation_id, + 'failed', + false, + $effectMayHaveApplied, + time() + ); + } else { + $expected = [ + 'service_suspend' => + ['suspending_expired', 'purge_stopping'], + 'network_suspend' => + ['blocking', 'blocking_expired'], + 'network_resume' => + ['restore_pending', 'restoring'], + ][$job->kind]; + $already = match ((string) $job->kind) { + 'service_suspend' => in_array( + (string) $instance->service_status, + ['suspend_failed', 'purge_stop_failed'], true + ), + 'network_suspend' => (string) $instance->network_status + === 'block_failed', + 'network_resume' => (string) $instance->network_status + === 'restore_failed', + default => false, + }; + $transitioned = false; if ($job->kind === 'service_suspend' && in_array((string) $instance->service_status, $expected, true)) { if ((string) $instance->status === 'deleting' @@ -3013,14 +3047,15 @@ final class CloudWorkerService $instance->network_status = 'restore_failed'; $transitioned = true; } - if (!$transitioned && !$already) { - throw new CloudRecoveryInstanceStateConflict( - '系统策略失败结论与实例当前状态矛盾', - 409 - ); + if (!$transitioned && !$already) { + throw new CloudRecoveryInstanceStateConflict( + '系统策略失败结论与实例当前状态矛盾', + 409 + ); + } } - $instance->updated_at = time(); - $instance->save(); + $instance->updated_at = time(); + $instance->save(); } elseif ($status === 'succeeded' && $job->instance_id !== null) { $instance = CloudInstance::where('id', $job->instance_id)->lockForUpdate()->first(); if ($instance === null) { @@ -3028,11 +3063,31 @@ final class CloudWorkerService '任务成功结论的关联实例已缺失', 409 ); } - if (in_array((string) $job->kind, + $networkPolicyMarker = in_array((string) $job->kind, + ['network_suspend', 'network_resume'], true) + ? CloudInstanceNetworkPolicy::payloadMarker( + $payload, + (int) $job->instance_id, + (string) $job->operation_id + ) : null; + if ($networkPolicyMarker !== null) { + CloudInstanceNetworkPolicy::finishJobLocked( + $instance, + (string) $job->kind, + (string) $job->operation_id, + 'succeeded', + is_array($result) + && ($result['skipped'] ?? false) === true, + false, + time() + ); + } elseif (in_array((string) $job->kind, ['delete', 'expiry_delete', 'delete_force'], true)) { CloudVpcPortService::releaseAfterInstanceDeletedLocked( $instance ); + CloudInstanceNetworkPolicy:: + releaseAfterInstanceDeletedLocked($instance); $instance->status = 'deleted'; $instance->sealed_credential = null; CloudBackupSubscription::where( @@ -3087,6 +3142,13 @@ final class CloudWorkerService $instance->spec_json = json_encode($decoded, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + } elseif (in_array((string) $job->kind, [ + 'service_suspend', 'network_suspend', 'network_resume', + ], true) && is_array($result) + && ($result['skipped'] ?? false) === true) { + // The desired state changed before dispatch. No remote + // effect occurred, so retain the current projection and + // let lifecycle derive the next generation if needed. } elseif ($job->kind === 'service_suspend' && in_array((string) $instance->service_status, array_merge( @@ -3133,11 +3195,29 @@ final class CloudWorkerService } elseif ($job->kind === 'network_resume' && in_array((string) $instance->network_status, array_merge( - ['restoring', 'restore_pending'], + [ + 'restoring', 'restore_pending', + 'blocking', 'blocking_expired', + ], $preparedRecoveryMarker === null ? [] : ['restore_failed'] ), true)) { - $instance->network_status = 'active'; + if (((string) $instance->service_status === 'expired_grace' + && $instance->expires_at !== null + && (int) $instance->expires_at <= time()) + || (string) $instance->network_status + === 'blocking_expired') { + // Expiry may race with a previously valid resume. + // The receipt proves the links are now up, so queue + // a fresh suspend generation instead of publishing + // a misleading active projection. + $instance->network_status = 'blocking_expired'; + } elseif ((string) $instance->network_status + === 'blocking') { + $instance->network_status = 'blocking'; + } else { + $instance->network_status = 'active'; + } $instance->network_suspended_at = null; } elseif ($job->kind === 'network_resume' && (string) $instance->network_status !== 'active') { diff --git a/web/tests/CloudBillingLifecycleTest.php b/web/tests/CloudBillingLifecycleTest.php index 2f3f56a3..83239c8f 100644 --- a/web/tests/CloudBillingLifecycleTest.php +++ b/web/tests/CloudBillingLifecycleTest.php @@ -125,6 +125,55 @@ namespace { $table->integer('updated_at'); $table->unique(['host_id', 'uuid']); }); + $schema->create('cloud_instance_network_policy_states', + static function (Blueprint $table): void { + $table->integer('instance_id')->primary(); + $table->integer('user_id'); + $table->boolean('desired_suspended')->default(false); + $table->boolean('applied_suspended')->default(false); + $table->integer('revision')->default(1); + $table->string('state')->default('applied'); + $table->string('operation_id')->nullable()->unique(); + $table->integer('created_at'); + $table->integer('updated_at'); + }); + $schema->create('cloud_instance_network_restrictions', + static function (Blueprint $table): void { + $table->integer('instance_id'); + $table->integer('user_id'); + $table->string('reason_code'); + $table->boolean('desired_active')->default(false); + $table->integer('revision')->default(1); + $table->string('source_type'); + $table->string('source_id'); + $table->integer('created_at'); + $table->integer('updated_at'); + $table->primary(['instance_id', 'reason_code']); + }); + $schema->create('cloud_traffic_pool_groups', + static function (Blueprint $table): void { + $table->increments('id'); + $table->string('code')->unique(); + $table->string('name'); + $table->string('mode'); + $table->integer('created_at'); + $table->integer('updated_at'); + }); + $schema->create('cloud_traffic_pools', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('user_id'); + $table->integer('traffic_pool_group_id'); + $table->string('state'); + $table->integer('available_bytes')->default(0); + $table->integer('overage_bytes')->default(0); + $table->integer('version')->default(0); + $table->integer('last_sequence')->default(0); + $table->integer('last_event_id')->nullable(); + $table->integer('created_at'); + $table->integer('updated_at'); + $table->unique(['user_id', 'traffic_pool_group_id']); + }); $schema->create('cloud_service_periods', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); @@ -1098,5 +1147,82 @@ namespace { && (int) $checkpointPeriod->upload_used_bytes === 300, 'Durable checkpoint did not reconstruct the independent period boundary.'); + // Enforced groups use the shared pool projection. An instance may exceed + // the quota contributed by its own period while another entitlement still + // has balance; only aggregate exhaustion schedules one policy mutation. + Capsule::table('cloud_traffic_pool_groups')->insert([ + 'id' => 2, + 'code' => 'shared-line', + 'name' => 'Shared line', + 'mode' => 'enforced', + 'created_at' => $now, + 'updated_at' => $now, + ]); + Capsule::table('cloud_traffic_pools')->insert([ + 'id' => 2, + 'user_id' => 1, + 'traffic_pool_group_id' => 2, + 'state' => 'available', + 'available_bytes' => 1000, + 'overage_bytes' => 0, + 'version' => 1, + 'last_sequence' => 0, + 'last_event_id' => null, + 'created_at' => $now, + 'updated_at' => $now, + ]); + $shared = $makeInstance( + 'dddddddd-dddd-4ddd-8ddd-dddddddddddd', + 'shared-pool-vm', + $now + 3600 + ); + $shared->lifetime_download_bytes = 1500; + $shared->lifetime_upload_bytes = 500; + $shared->traffic_synced_at = $now; + $shared->save(); + $sharedPeriod = $makePeriod( + $shared, + $now - 100, + $now + 3600, + 'active', + 1000 + ); + $sharedPeriod->traffic_pool_group_id = 2; + $sharedPeriod->traffic_quota_mode = 'finite'; + $sharedPeriod->download_base_bytes = 0; + $sharedPeriod->upload_base_bytes = 0; + $sharedPeriod->save(); + $shared->current_period_id = $sharedPeriod->id; + $shared->save(); + $reconcile = (new ReflectionClass(CloudLifecycleService::class)) + ->getMethod('reconcile'); + $assert($reconcile->invoke($lifecycle, (int) $shared->id) === false + && (string) $shared->fresh()->network_status === 'active' + && !CloudJob::where('instance_id', $shared->id) + ->where('kind', 'network_suspend')->exists(), + 'An instance-local quota disconnected an available shared pool.'); + + Capsule::table('cloud_traffic_pools')->where('id', 2)->update([ + 'state' => 'exhausted', + 'available_bytes' => 0, + 'overage_bytes' => 1, + 'version' => 2, + 'updated_at' => $now + 1, + ]); + $assert($reconcile->invoke($lifecycle, (int) $shared->id) === true, + 'Shared-pool exhaustion did not queue a network policy operation.'); + $sharedJob = CloudJob::where('instance_id', $shared->id) + ->where('kind', 'network_suspend')->latest('id')->firstOrFail(); + $sharedPayload = \App\Services\Cloud\CloudSecrets::decrypt( + (string) $sharedJob->sealed_payload, + 'job-payload' + ); + $assert((string) $shared->fresh()->network_status === 'blocking' + && ($sharedPayload['_network_policy']['version'] ?? null) === 1 + && ($sharedPayload['_network_policy']['operation_id'] ?? null) + === (string) $sharedJob->operation_id + && ($sharedPayload['suspend_reason'] ?? null) === 'traffic', + 'Shared-pool policy job lost its immutable aggregate operation binding.'); + echo "Cloud independent-period renewal, traffic quota, expiry grace and purge lifecycle checks passed.\n"; } diff --git a/web/tests/CloudFinancialWorkflowTest.php b/web/tests/CloudFinancialWorkflowTest.php index f0aebc27..12fa83e5 100644 --- a/web/tests/CloudFinancialWorkflowTest.php +++ b/web/tests/CloudFinancialWorkflowTest.php @@ -323,6 +323,8 @@ namespace { $t->unique(['host_id', 'operation_id']); }); $schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id'] as $v) { $t->integer($v); } foreach (['order_id', 'plan_id', 'offer_id', 'resource_pool_id', 'reservation_id', 'current_period_id', 'expires_at', 'suspended_at', 'network_suspended_at'] as $v) { $t->integer($v)->nullable(); } foreach (['renewal_days', 'renewal_traffic_bytes', 'traffic_quota_bytes', 'traffic_download_bytes', 'traffic_upload_bytes', 'lifetime_download_bytes', 'lifetime_upload_bytes', 'traffic_synced_at'] as $v) { $t->integer($v)->default(0); } $t->string('renewal_price')->nullable(); $t->string('uuid'); $t->string('name'); $t->string('status'); $t->string('service_status')->default('exempt'); $t->string('network_status')->default('active'); $t->text('spec_json'); $t->text('sealed_credential')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); }); + $schema->create('cloud_instance_network_policy_states', function (Blueprint $t) { $t->integer('instance_id')->primary(); $t->integer('user_id'); $t->boolean('desired_suspended')->default(false); $t->boolean('applied_suspended')->default(false); $t->integer('revision')->default(1); $t->string('state')->default('applied'); $t->string('operation_id')->nullable()->unique(); $t->integer('created_at'); $t->integer('updated_at'); }); + $schema->create('cloud_instance_network_restrictions', function (Blueprint $t) { $t->integer('instance_id'); $t->integer('user_id'); $t->string('reason_code'); $t->boolean('desired_active')->default(false); $t->integer('revision')->default(1); $t->string('source_type'); $t->string('source_id'); $t->integer('created_at'); $t->integer('updated_at'); $t->primary(['instance_id', 'reason_code']); }); $schema->create('cloud_vpc_ports', function (Blueprint $t) { $t->increments('id'); $t->integer('vpc_id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('active_claim')->nullable(); $t->string('state'); }); $schema->create('cloud_instance_snapshots', function (Blueprint $t) { $t->increments('id'); $t->string('snapshot_key')->unique(); foreach (['instance_id', 'user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('name'); $t->string('state'); $t->string('consistency'); $t->integer('size_bytes')->nullable(); $t->string('last_operation_id')->nullable(); foreach (['ready_at', 'last_restored_at', 'deleted_at'] as $v) { $t->integer($v)->nullable(); } $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_renewals', function (Blueprint $t) { $t->increments('id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('plan_id')->nullable(); $t->integer('period_id')->nullable(); $t->string('idempotency_key'); $t->string('request_hash'); $t->string('amount'); $t->integer('validity_days'); $t->integer('traffic_bytes_added'); $t->integer('previous_expires_at'); $t->integer('new_expires_at'); $t->integer('created_at'); $t->unique(['user_id', 'idempotency_key']); }); diff --git a/web/tests/CloudInstanceNetworkPolicyTest.php b/web/tests/CloudInstanceNetworkPolicyTest.php index 792fa21c..acf3884b 100644 --- a/web/tests/CloudInstanceNetworkPolicyTest.php +++ b/web/tests/CloudInstanceNetworkPolicyTest.php @@ -48,6 +48,7 @@ $schema->create('cloud_traffic_pools', static function (Blueprint $t): void { $schema->create('cloud_instances', static function (Blueprint $t): void { $t->increments('id'); $t->integer('user_id'); + $t->integer('current_period_id')->nullable(); $t->string('service_status'); $t->string('network_status'); $t->integer('network_suspended_at')->nullable(); @@ -410,9 +411,273 @@ $attention = $instance->getConnection()->transaction( return $result; } ); -$assert($attention['traffic_decision'] === 'preserved' +$assert($attention['traffic_decision'] === 'fenced' && in_array('traffic_pool_exhausted', $attention['desired_reasons'], true), 'An attention projection guessed that traffic access could be restored.'); +$fenceEndsAt = time() + 3600; +$fencedInstance = new CloudInstance(); +$fencedInstance->id = 11; +$fencedInstance->user_id = 20; +$fencedInstance->current_period_id = 31; +$fencedInstance->service_status = 'active'; +$fencedInstance->network_status = 'blocked_traffic'; +$fencedInstance->network_suspended_at = 900; +$fencedInstance->expires_at = $fenceEndsAt; +$fencedInstance->updated_at = 1000; +$fencedInstance->save(); +$fencedPeriod = new CloudServicePeriod(); +$fencedPeriod->id = 31; +$fencedPeriod->instance_id = 11; +$fencedPeriod->user_id = 20; +$fencedPeriod->traffic_pool_group_id = 2; +$fencedPeriod->traffic_quota_mode = 'finite'; +$fencedPeriod->status = 'active'; +$fencedPeriod->starts_at = 900; +$fencedPeriod->ends_at = $fenceEndsAt; +$fencedPeriod->save(); +$fenced = $fencedInstance->getConnection()->transaction( + static function () use ($fencedInstance, $fencedPeriod): array { + $locked = CloudInstance::where('id', $fencedInstance->id) + ->lockForUpdate()->firstOrFail(); + $result = CloudInstanceNetworkPolicy::synchronizeLocked( + $locked, + $fencedPeriod, + 1013 + ); + $locked->save(); + return $result; + } +); +$assert($fenced['traffic_decision'] === 'fenced' + && $fenced['desired_suspended'] === true + && $fenced['applied_suspended'] === true, + 'An attention pool restored an already blocked instance during takeover.'); + +Capsule::table('cloud_traffic_pools')->where('id', 40)->update([ + 'state' => 'available', + 'available_bytes' => 100, + 'version' => 5, +]); +$fencedResume = str_repeat('f', 32); +$fencedInstance->getConnection()->transaction( + static function () use ( + $fencedInstance, + $fencedPeriod, + $fencedResume + ): void { + $locked = CloudInstance::where('id', $fencedInstance->id) + ->lockForUpdate()->firstOrFail(); + CloudInstanceNetworkPolicy::synchronizeLocked( + $locked, + $fencedPeriod, + 1014 + ); + CloudInstanceNetworkPolicy::prepareJobLocked( + $locked, + 'network_resume', + $fencedResume, + 1014 + ); + $locked->save(); + } +); +$assert(CloudInstanceNetworkPolicy::jobStillDesired( + (int) $fencedInstance->id, + 'network_resume', + $fencedResume + ) === true, + 'An available current pool unexpectedly fenced a queued resume.'); +Capsule::table('cloud_traffic_pools')->where('id', 40)->update([ + 'state' => 'attention', + 'available_bytes' => 0, + 'version' => 6, +]); +$assert(CloudInstanceNetworkPolicy::jobStillDesired( + (int) $fencedInstance->id, + 'network_resume', + $fencedResume + ) === false, + 'A queued resume crossed an attention pool before instance convergence.'); +$fencedInstance->getConnection()->transaction( + static function () use ( + $fencedInstance, + $fencedPeriod, + $fencedResume + ): void { + $locked = CloudInstance::where('id', $fencedInstance->id) + ->lockForUpdate()->firstOrFail(); + $result = CloudInstanceNetworkPolicy::synchronizeLocked( + $locked, + $fencedPeriod, + 1015 + ); + if (($result['traffic_decision'] ?? null) !== 'fenced' + || CloudInstanceNetworkPolicy::jobStillDesired( + (int) $fencedInstance->id, + 'network_resume', + $fencedResume + ) !== false) { + throw new RuntimeException( + 'Attention did not invalidate a queued automatic resume.' + ); + } + CloudInstanceNetworkPolicy::finishJobLocked( + $locked, + 'network_resume', + $fencedResume, + 'succeeded', + true, + false, + 1015 + ); + $locked->save(); + } +); +$assert((string) CloudInstance::find(11)->network_status === 'blocked_traffic' + && (int) CloudInstance::find(11)->network_suspended_at === 900, + 'A queued resume crossed an attention fence or rewrote block timing.'); + +// Unknown effect retains the exact operation fence. A fresh generation is +// forbidden; resolving that same operation may then converge the projection. +$instance->getConnection()->transaction(static function () use ($instance): void { + $locked = CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail(); + foreach ([ + CloudInstanceNetworkPolicy::REASON_ADMIN, + CloudInstanceNetworkPolicy::REASON_TRAFFIC, + ] as $reason) { + CloudInstanceNetworkPolicy::setReasonLocked( + $locked, + $reason, + false, + $reason === CloudInstanceNetworkPolicy::REASON_ADMIN + ? 'administrator' : 'traffic_pool', + $reason === CloudInstanceNetworkPolicy::REASON_ADMIN + ? 'user:1' : 'pool:40:v4', + 1014 + ); + } + $locked->save(); +}); +$unknownOperation = str_repeat('d', 32); +$instance->getConnection()->transaction( + static function () use ($instance, $unknownOperation): void { + $locked = CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail(); + CloudInstanceNetworkPolicy::prepareJobLocked( + $locked, + 'network_resume', + $unknownOperation, + 1015 + ); + CloudInstanceNetworkPolicy::finishJobLocked( + $locked, + 'network_resume', + $unknownOperation, + 'failed', + false, + true, + 1016 + ); + $locked->save(); + } +); +$unknownState = CloudInstanceNetworkPolicyState::findOrFail(10); +$assert((string) $unknownState->state === 'unknown' + && (string) $unknownState->operation_id === $unknownOperation + && (string) CloudInstance::find(10)->network_status === 'restore_failed', + 'An ambiguous resume released its exact operation fence.'); +$assert($instance->getConnection()->transaction( + static function () use ($instance): bool { + return CloudInstanceNetworkPolicy::allowRetryLocked( + CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail(), + 'network_resume', + 1017 + ); + } +) === false, 'An unknown-effect operation was converted into a fresh retry.'); +$instance->getConnection()->transaction( + static function () use ($instance, $unknownOperation): void { + $locked = CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail(); + CloudInstanceNetworkPolicy::finishJobLocked( + $locked, + 'network_resume', + $unknownOperation, + 'succeeded', + false, + false, + 1018 + ); + $locked->save(); + } +); +$assert((string) CloudInstance::find(10)->network_status === 'active', + 'Resolving the original resume operation did not restore the projection.'); + +// A policy mutation skipped before dispatch changes no remote state. +$skippedOperation = str_repeat('e', 32); +$instance->getConnection()->transaction( + static function () use ($instance, $skippedOperation): void { + $locked = CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail(); + CloudInstanceNetworkPolicy::setReasonLocked( + $locked, + CloudInstanceNetworkPolicy::REASON_ADMIN, + true, + 'administrator', + 'user:1', + 1019 + ); + CloudInstanceNetworkPolicy::prepareJobLocked( + $locked, + 'network_suspend', + $skippedOperation, + 1019 + ); + CloudInstanceNetworkPolicy::setReasonLocked( + $locked, + CloudInstanceNetworkPolicy::REASON_ADMIN, + false, + 'administrator', + 'user:1', + 1020 + ); + CloudInstanceNetworkPolicy::finishJobLocked( + $locked, + 'network_suspend', + $skippedOperation, + 'succeeded', + true, + false, + 1021 + ); + $locked->save(); + } +); +$assert((string) CloudInstance::find(10)->network_status === 'active' + && (bool) CloudInstanceNetworkPolicyState::findOrFail(10) + ->applied_suspended === false, + 'A pre-dispatch skip was finalized as a real network suspension.'); +$instance->getConnection()->transaction(static function () use ($instance): void { + CloudInstanceNetworkPolicy::releaseAfterInstanceDeletedLocked( + CloudInstance::where('id', $instance->id) + ->lockForUpdate()->firstOrFail() + ); +}); +$fencedInstance->getConnection()->transaction( + static function () use ($fencedInstance): void { + CloudInstanceNetworkPolicy::releaseAfterInstanceDeletedLocked( + CloudInstance::where('id', $fencedInstance->id) + ->lockForUpdate()->firstOrFail() + ); + } +); +$assert(CloudInstanceNetworkPolicyState::count() === 0 + && CloudInstanceNetworkRestriction::count() === 0, + 'Deleting an instance left aggregate network-policy state behind.'); + echo "Cloud instance network policy checks passed.\n"; diff --git a/web/tests/CloudPlanUpgradeServiceTest.php b/web/tests/CloudPlanUpgradeServiceTest.php index e9a48aef..6f89da7a 100644 --- a/web/tests/CloudPlanUpgradeServiceTest.php +++ b/web/tests/CloudPlanUpgradeServiceTest.php @@ -249,6 +249,31 @@ namespace { $table->integer('created_at'); $table->integer('updated_at'); }); + $schema->create('cloud_instance_network_policy_states', + static function (Blueprint $table): void { + $table->integer('instance_id')->primary(); + $table->integer('user_id'); + $table->boolean('desired_suspended')->default(false); + $table->boolean('applied_suspended')->default(false); + $table->integer('revision')->default(1); + $table->string('state')->default('applied'); + $table->string('operation_id')->nullable()->unique(); + $table->integer('created_at'); + $table->integer('updated_at'); + }); + $schema->create('cloud_instance_network_restrictions', + static function (Blueprint $table): void { + $table->integer('instance_id'); + $table->integer('user_id'); + $table->string('reason_code'); + $table->boolean('desired_active')->default(false); + $table->integer('revision')->default(1); + $table->string('source_type'); + $table->string('source_id'); + $table->integer('created_at'); + $table->integer('updated_at'); + $table->primary(['instance_id', 'reason_code']); + }); $schema->create('cloud_resource_reservations', static function (Blueprint $table): void { $table->increments('id'); diff --git a/web/tests/CloudTrafficRestrictionIntegrationContractTest.php b/web/tests/CloudTrafficRestrictionIntegrationContractTest.php new file mode 100644 index 00000000..a76983bb --- /dev/null +++ b/web/tests/CloudTrafficRestrictionIntegrationContractTest.php @@ -0,0 +1,45 @@ += 2 + && str_contains($worker, + 'releaseAfterInstanceDeletedLocked($instance)'), + 'Worker lost pre-dispatch convergence, terminal convergence, or delete cleanup.'); +$assert(str_contains($renewal, '$aggregateNetworkPolicy') + && str_contains($renewal, '!$aggregateNetworkPolicy') + && str_contains($upgrade, '$aggregateNetworkPolicy') + && str_contains($upgrade, '!$aggregateNetworkPolicy'), + 'Renewal or upgrade can bypass another active network restriction.'); +$assert(str_contains($lifecycle, "'_network_policy'") + && str_contains($worker, 'payloadMarker('), + 'Policy jobs are not bound to an immutable aggregate execution snapshot.'); + +echo "Cloud traffic restriction integration contract passed.\n";