Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

return new class extends Migration
{
private const INDEX_NAME = 'webhook_logs_service_idempotency_key_unique';

public function up(): void
{
Schema::table('webhook_logs', function (Blueprint $table) {
$table->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();
}
};
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Database/WebhookLogSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Persistence\Models\WebhookLog;

it('allows multiple webhook logs with same service and idempotency key', function () {
WebhookLog::factory()->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);
});
40 changes: 40 additions & 0 deletions tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
15 changes: 9 additions & 6 deletions tests/Testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Audit/Application/UseCases/RecordWebhookLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
28 changes: 24 additions & 4 deletions tests/Unit/Services/WebhookLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down