Skip to content

feat: implement rate limiting, structured logging, webhook verification, and migration runner - #151

Merged
oraimoitel merged 1 commit into
cocor-tech:masterfrom
bitcoindev817-hue:master
Aug 2, 2026
Merged

feat: implement rate limiting, structured logging, webhook verification, and migration runner#151
oraimoitel merged 1 commit into
cocor-tech:masterfrom
bitcoindev817-hue:master

Conversation

@bitcoindev817-hue

@bitcoindev817-hue bitcoindev817-hue commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Closes #65
Closes #66
Closes #67
Closes #68

Changes Summary

#65 - Rate Limiting with Redis-backed Sliding Window Counter

  • Replaced simple counter-based rate limiting with a sliding window approach using Redis sorted sets (ZADD/ZREMRANGEBYSCORE/ZCARD) with timestamps as scores
  • Added in-memory fallback when Redis is unavailable instead of failing closed with 503
  • Updated internal/infrastructure/ratelimit/redis.go with sliding window implementation and InMemoryRateLimiter
  • Updated internal/api/middleware/ratelimit.go to use the new sliding window and in-memory fallback
  • Updated internal/api/middleware/rate_limit_test.go to reflect new fallback behavior

#66 - Structured Logging with Correlation IDs

  • Added logger.Ctx(ctx) utility in pkg/logger/logger.go for request-scoped loggers with correlation ID and userID
  • Enhanced internal/api/middleware/logging.go to include callerIdentity (userID) in request completion logs
  • Replaced ad hoc fmt.Printf in internal/domain/email/service.go with structured zerolog calls
  • Propagated X-Request-ID header to outbound HTTP calls in webhook dispatcher, Yellow Card client, and Brevo email service
  • Added GetUserIDFromGin() helper for reading userID from Gin context after authentication

#67 - Webhook Signature Verification with Constant-Time Comparison

  • Added SignWebhookPayload() and VerifyWebhookSignature() functions in webhook/service.go using HMAC-SHA256
  • Uses crypto/subtle.ConstantTimeCompare for timing-safe comparison to prevent timing attacks
  • Added POST /webhooks/incoming/:id endpoint for receiving incoming webhooks with signature verification
  • Added GetByID() method to WebhookRepository interface and PostgresRepository
  • Added IncomingWebhookHandler in internal/api/handler/webhook_handler.go
  • Added comprehensive tests in webhook/service_test.go covering valid, tampered, and missing signatures

#68 - Database Migration Runner with Rollback Support

  • Migration runner already exists and is complete in cmd/migrate/main.go
  • Tracks applied migrations in schema_migrations table
  • Supports up/down migrations with automatic transaction rollback on failure
  • Integrates with PostgreSQL with advisory locking for concurrent safety
  • Verified complete with existing tests in cmd/migrate/main_test.go

…on, and migration runner

Closes cocor-tech#65, cocor-tech#66, cocor-tech#67, cocor-tech#68

- cocor-tech#65: Implement Redis-backed sliding window rate limiter using sorted sets
  with timestamps as scores. Falls back to in-memory limiter when Redis is
  unavailable instead of failing closed with 503.

- cocor-tech#66: Enhance structured logging with correlation ID propagation through
  all service calls. Add caller identity (userID) to request logs. Replace
  ad hoc fmt.Printf in email service with structured zerolog calls. Propagate
  X-Request-ID header to outbound HTTP calls (webhooks, Yellow Card, Brevo).

- cocor-tech#67: Add webhook signature verification using HMAC-SHA256 with constant-time
  comparison (crypto/subtle.ConstantTimeCompare) to prevent timing attacks.
  Add incoming webhook endpoint at POST /webhooks/incoming/:id with tests for
  valid, tampered, and missing signatures.

- cocor-tech#68: Migration runner already exists with up/down support, automatic
  transaction rollback on failure, and PostgreSQL integration. Verified
  complete with schema_migrations tracking table and advisory locking.
@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@bitcoindev817-hue Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@oraimoitel
oraimoitel merged commit acd6baa into cocor-tech:master Aug 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants