fix(cloud): anonymize retained compensation scopes

go
codex 3 weeks ago
parent 09ebbd51a1
commit a0b70fc30d

@ -151,9 +151,11 @@ final class CloudServiceCompensationPlanner
return [
'type' => $scopeType,
'id' => $scopeId,
'label' => (string) $instance->name,
// The per-instance rows already freeze the tenant-owned name
// and owner. Keep the long-lived batch scope free of tenant
// PII so account deletion can retain aggregate audit safely.
'label' => '单实例 #' . $scopeId,
'instance_id' => $scopeId,
'user_id' => (int) $instance->user_id,
'host_id' => (int) $instance->host_id,
];
}

@ -192,6 +192,7 @@ class UserDataDeletionService
// jobs before their parent rows so encrypted owner/VM payloads do
// not survive as orphaned administrator audit records.
$resourceJobRows = $this->deleteCloudResourceJobs($connection, $ids);
$this->anonymizeCompensationScopes($connection, $ids);
$trafficInstanceIds = $this->pluckByUsers(
$connection,
'cloud_instances',
@ -412,6 +413,49 @@ class UserDataDeletionService
})->delete();
}
/**
* Batch audit survives account deletion, but a historical single-instance
* scope may have frozen the tenant's chosen instance name and user ID.
* Replace that snapshot while its item rows still identify the affected
* batches. Host and public-pool scopes contain provider metadata only.
*/
private function anonymizeCompensationScopes(
$connection,
array $userIds
): void {
if (!$this->hasColumns($connection,
'cloud_service_compensation_items', ['batch_id', 'user_id'])
|| !$this->hasColumns($connection,
'cloud_service_compensations', [
'id', 'scope_type', 'scope_id', 'scope_snapshot_json',
])) {
return;
}
$batchIds = $connection->table('cloud_service_compensation_items')
->whereIn('user_id', $userIds)->orderBy('batch_id')
->pluck('batch_id')->map(static fn ($id): int => (int) $id)
->unique()->values()->all();
if ($batchIds === []) {
return;
}
$batches = $connection->table('cloud_service_compensations')
->whereIn('id', $batchIds)->where('scope_type', 'instance')
->orderBy('id')->lockForUpdate()->get(['id', 'scope_id']);
foreach ($batches as $batch) {
$scopeId = (int) $batch->scope_id;
$snapshot = json_encode([
'type' => 'instance',
'id' => $scopeId,
'label' => '单实例 #' . $scopeId,
'instance_id' => $scopeId,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE);
$connection->table('cloud_service_compensations')
->where('id', (int) $batch->id)
->update(['scope_snapshot_json' => $snapshot]);
}
}
private function deleteCloudTrafficScanFailures(
$connection,
array $instanceIds,

@ -18,6 +18,11 @@ $assert(str_contains($deletion,
$assert(!str_contains($deletion,
"['cloud_service_compensations', 'requested_by_user_id']"),
'Account deletion must retain compensation batch audit.');
$assert(str_contains($deletion, 'anonymizeCompensationScopes(')
&& str_contains($deletion, "->where('scope_type', 'instance')")
&& str_contains($deletion, "'label' => '单实例 #' . \$scopeId")
&& str_contains($deletion, "['scope_snapshot_json' => \$snapshot]"),
'Retained single-instance compensation audit is not anonymized.');
$assert(str_contains($deletion,
"'cloud_service_compensations as compensation_batch'")
&& str_contains($deletion,

@ -311,9 +311,15 @@ $assert($preview['recipients'][0]['user_id'] === 1
$instancePreview = $planner->preview('instance', 101);
$assert($instancePreview['scope']['type'] === 'instance'
&& $instancePreview['scope']['instance_id'] === 101
&& $instancePreview['scope']['label'] === '单实例 #101'
&& !array_key_exists('user_id', $instancePreview['scope'])
&& !str_contains(
json_encode($instancePreview['scope'], JSON_THROW_ON_ERROR),
'vm-101'
)
&& $instancePreview['instance_count'] === 1
&& $instancePreview['eligible_count'] === 1,
'Instance scope must produce one eligible preview.');
'Instance scope must produce one eligible preview without tenant PII.');
$deletedPreview = $planner->preview('instance', 111);
$assert($deletedPreview['scope']['instance_id'] === 111
&& $deletedPreview['instance_count'] === 0

@ -126,7 +126,9 @@ $schema->create('cloud_mail_outbox', static function (Blueprint $table) {
});
$schema->create('cloud_service_compensations', static function (Blueprint $table) {
$table->increments('id'); $table->integer('requested_by_user_id');
$table->string('state');
$table->string('state'); $table->string('scope_type')->nullable();
$table->integer('scope_id')->nullable();
$table->text('scope_snapshot_json')->nullable();
});
$schema->create('cloud_service_compensation_items', static function (Blueprint $table) {
$table->increments('id'); $table->integer('batch_id');
@ -251,6 +253,11 @@ Capsule::table('cloud_mail_outbox')->insert([
]);
Capsule::table('cloud_service_compensations')->insert([
'id' => 1, 'requested_by_user_id' => 2, 'state' => 'completed',
'scope_type' => 'instance', 'scope_id' => 501,
'scope_snapshot_json' => json_encode([
'type' => 'instance', 'id' => 501, 'label' => 'tenant-secret-vm',
'instance_id' => 501, 'user_id' => 1, 'host_id' => 5,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_service_compensation_items')->insert([
['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'state' => 'applied'],
@ -308,6 +315,14 @@ $assert(Capsule::table('cloud_mail_outbox')->pluck('invoice_id')->all() === [303
'Cloud invoice mail history deletion was not isolated.');
$assert(Capsule::table('cloud_service_compensations')->pluck('id')->all() === [1],
'Account deletion removed the immutable compensation batch audit.');
$retainedCompensationScope = json_decode((string) Capsule::table(
'cloud_service_compensations'
)->where('id', 1)->value('scope_snapshot_json'), true, 16,
JSON_THROW_ON_ERROR);
$assert($retainedCompensationScope === [
'type' => 'instance', 'id' => 501, 'label' => '单实例 #501',
'instance_id' => 501,
], 'Account deletion retained tenant PII in compensation batch audit.');
$assert(Capsule::table('cloud_service_compensation_items')->pluck('user_id')->all()
=== [3]
&& Capsule::table('cloud_service_compensation_recipients')

Loading…
Cancel
Save