diff --git a/database/migrations/004_remove_webhook_logs_idempotency_unique_index.php b/database/migrations/004_remove_webhook_logs_idempotency_unique_index.php new file mode 100644 index 0000000..678912e --- /dev/null +++ b/database/migrations/004_remove_webhook_logs_idempotency_unique_index.php @@ -0,0 +1,40 @@ +dropUnique(self::INDEX_NAME); + }); + } + + public function down(): void + { + if ($this->hasDuplicateIdempotencyKeys()) { + throw new RuntimeException('Cannot restore webhook_logs unique index because duplicate service + idempotency_key rows exists. ' + .'processed_webhook_events is the deduplication source of truth.'); + } + + Schema::table('webhook_logs', function (Blueprint $table) { + $table->unique(['service', 'idempotency_key'], self::INDEX_NAME); + }); + } + + private function hasDuplicateIdempotencyKeys(): bool + { + return DB::table('webhook_logs') + ->select('service', 'idempotency_key') + ->whereNotNull('idempotency_key') + ->groupBy('service', 'idempotency_key') + ->havingRaw('COUNT(*) > 1') + ->exists(); + } +}; diff --git a/docs/architecture.md b/docs/architecture.md index 8718e27..89d1831 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -62,7 +62,7 @@ Deduplication is backed by the `processed_webhook_events` projection. `processed_webhook_events` owns the uniqueness constraint on `service + idempotency_key`. -For compatibility, `webhook_logs` may still contain a legacy uniqueness constraint on `service + idempotency_key`, but it is no longer used as the source of truth for deduplication. +`webhook_logs` does not own any deduplication constraint. It is an audit trail and may contain multiple rows for the same webhook across receive, retry, replay or manual audit writes. ### Historical migration diff --git a/tests/Feature/Database/WebhookLogSchemaTest.php b/tests/Feature/Database/WebhookLogSchemaTest.php new file mode 100644 index 0000000..4b0efa4 --- /dev/null +++ b/tests/Feature/Database/WebhookLogSchemaTest.php @@ -0,0 +1,26 @@ +create([ + 'service' => 'github', + 'event' => 'opened', + 'status' => 'success', + 'external_id' => 'delivery_123', + 'idempotency_key' => 'delivery_123', + ]); + + WebhookLog::factory()->create([ + 'service' => 'github', + 'event' => 'opened', + 'status' => 'failed', + 'external_id' => 'delivery_123', + 'idempotency_key' => 'delivery_123', + 'error_message' => 'Retry failed', + ]); + + expect(WebhookLog::query()->count())->toBe(2); +}); diff --git a/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php b/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php index 9708699..1c5e2c0 100644 --- a/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php +++ b/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php @@ -1104,3 +1104,43 @@ expect(WebhookLog::query()->count())->toBe(1) ->and(ProcessedWebhookEvent::query()->count())->toBe(1); }); + +it('does not use webhook logs as duplicate source anymore', function () { + WebhookLog::factory()->create([ + 'service' => 'github', + 'event' => 'opened', + 'status' => 'success', + 'external_id' => 'delivery_123', + 'idempotency_key' => 'delivery_123', + ]); + + expect(WebhookLog::query()->count())->toBe(1) + ->and(ProcessedWebhookEvent::query()->count())->toBe(0); + + $payload = json_encode([ + 'action' => 'opened', + 'pull_request' => [ + 'id' => 123, + ], + ], JSON_THROW_ON_ERROR); + + $signature = githubSignature($payload, 'test_github_secret'); + + $this->call( + 'POST', + 'test-github-webhook', + [], + [], + [], + [ + 'HTTP_X_HUB_SIGNATURE_256' => $signature->value(), + 'HTTP_X_GITHUB_EVENT' => 'ping', + 'HTTP_X_GITHUB_DELIVERY' => 'delivery_123', + 'CONTENT_TYPE' => 'application/json', + ], + $payload + )->assertOk(); + + expect(WebhookLog::query()->count())->toBe(2) + ->and(ProcessedWebhookEvent::query()->count())->toBe(1); +}); diff --git a/tests/Testcase.php b/tests/Testcase.php index 3b62651..fd2ee53 100644 --- a/tests/Testcase.php +++ b/tests/Testcase.php @@ -34,13 +34,16 @@ public function getEnvironmentSetUp($app) ]); // Run migrations - $migration1 = include __DIR__.'/../database/migrations/001_create_webhook_logs_table.php'; - $migration1->up(); + $createWebhookLogsTable = include __DIR__.'/../database/migrations/001_create_webhook_logs_table.php'; + $createWebhookLogsTable->up(); - $migration2 = include __DIR__.'/../database/migrations/002_add_idempotency_key_to_webhook_logs_table.php'; - $migration2->up(); + $addIdempotencyKey = include __DIR__.'/../database/migrations/002_add_idempotency_key_to_webhook_logs_table.php'; + $addIdempotencyKey->up(); - $migration3 = include __DIR__.'/../database/migrations/003_create_processed_webhook_events_table.php'; - $migration3->up(); + $createProcessedWebhookEventsTable = include __DIR__.'/../database/migrations/003_create_processed_webhook_events_table.php'; + $createProcessedWebhookEventsTable->up(); + + $createWebhookLogsTable = include __DIR__.'/../database/migrations/004_remove_webhook_logs_idempotency_unique_index.php'; + $createWebhookLogsTable->up(); } } diff --git a/tests/Unit/Audit/Application/UseCases/RecordWebhookLogTest.php b/tests/Unit/Audit/Application/UseCases/RecordWebhookLogTest.php index 6c74665..8b4e44a 100644 --- a/tests/Unit/Audit/Application/UseCases/RecordWebhookLogTest.php +++ b/tests/Unit/Audit/Application/UseCases/RecordWebhookLogTest.php @@ -95,3 +95,31 @@ public function record(RecordWebhookLogCommand $command): WebhookLog ->and($fakeWriter->commands[0]->valid)->toBeFalse() ->and($fakeWriter->commands[0]->errorMessage)->toBe('Invalid GitHub webhook signature.'); }); + +it('records multiple audit logs with the same idempotency key', function () { + $useCase = app(RecordWebhookLog::class); + + $useCase->handle(new RecordWebhookLogCommand( + service: 'github', + event: 'opened', + valid: true, + payload: ['action' => 'opened'], + attempt: 0, + externalId: 'delivery_123', + idempotencyKey: 'delivery_123', + errorMessage: null, + )); + + $useCase->handle(new RecordWebhookLogCommand( + service: 'github', + event: 'opened', + valid: false, + payload: ['action' => 'opened'], + attempt: 1, + externalId: 'delivery_123', + idempotencyKey: 'delivery_123', + errorMessage: 'Retry failed', + )); + + expect(WebhookLog::query()->count())->toBe(2); +}); diff --git a/tests/Unit/Processing/Infrastructure/Persistence/EloquentProcessedWebhookRecorderTest.php b/tests/Unit/Processing/Infrastructure/Persistence/EloquentProcessedWebhookRecorderTest.php index 0c1136d..608e4ac 100644 --- a/tests/Unit/Processing/Infrastructure/Persistence/EloquentProcessedWebhookRecorderTest.php +++ b/tests/Unit/Processing/Infrastructure/Persistence/EloquentProcessedWebhookRecorderTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Illuminate\Database\QueryException; use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\EloquentProcessedWebhookRecorder; use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\Models\ProcessedWebhookEvent; @@ -32,3 +33,21 @@ expect(ProcessedWebhookEvent::query()->count())->toBe(1); }); + +it('enforces unique constraint on processed webhook events service and idempotency key', function () { + ProcessedWebhookEvent::query()->create([ + 'service' => 'stripe', + 'idempotency_key' => 'idem_unique', + 'external_id' => 'evt_unique', + 'event' => 'event1', + 'processed_at' => now(), + ]); + + expect(fn () => ProcessedWebhookEvent::query()->create([ + 'service' => 'stripe', + 'idempotency_key' => 'idem_unique', + 'external_id' => 'evt_other', + 'event' => 'event2', + 'processed_at' => now(), + ]))->toThrow(QueryException::class); +}); diff --git a/tests/Unit/Services/WebhookLoggerTest.php b/tests/Unit/Services/WebhookLoggerTest.php index 1444144..3992cc4 100644 --- a/tests/Unit/Services/WebhookLoggerTest.php +++ b/tests/Unit/Services/WebhookLoggerTest.php @@ -256,11 +256,31 @@ ->not->toThrow(QueryException::class); }); - it('enforces unique constraint on service + idempotency_key', function () { - $this->logger->logSuccess('stripe', 'event1', ['id' => '1'], 0, 'evt_unique', 'idem_unique'); + it('allows multiple logs with same service and idempotency_key', function () { + $firstLog = $this->logger->logSuccess( + 'stripe', + 'event1', + ['id' => '1'], + 0, + 'evt_unique', + 'idem_unique', + ); + + $secondLog = $this->logger->logSuccess( + 'stripe', + 'event2', + ['id' => '2'], + 0, + 'evt_other', + 'idem_unique', + ); - expect(fn () => $this->logger->logSuccess('stripe', 'event2', ['id' => '2'], 0, 'evt_other', 'idem_unique')) - ->toThrow(QueryException::class); + expect($firstLog->id)->not->toBe($secondLog->id) + ->and($firstLog->service)->toBe('stripe') + ->and($secondLog->service)->toBe('stripe') + ->and($firstLog->idempotency_key)->toBe('idem_unique') + ->and($secondLog->idempotency_key)->toBe('idem_unique') + ->and(WebhookLog::query()->count())->toBe(2); }); it('allows same external_id for different services', function () {