Skip to content

feat: Add webhook event system with HMAC-SHA256 signing and retry logic#346

Open
i010542 wants to merge 2 commits intorohitdash08:mainfrom
i010542:webhook-system
Open

feat: Add webhook event system with HMAC-SHA256 signing and retry logic#346
i010542 wants to merge 2 commits intorohitdash08:mainfrom
i010542:webhook-system

Conversation

@i010542
Copy link

@i010542 i010542 commented Mar 9, 2026

Summary

Implemented a complete webhook event system for FinMind satisfying all acceptance criteria from Issue #77.

Changes

  • New: Webhook and WebhookDelivery ORM models
  • New: HMAC-SHA256 signature verification
  • New: 3-attempt retry logic with exponential backoff (0s, 5s, 30s)
  • New: REST API endpoints (CRUD + test-ping)
  • New: Event types: `expense.`, `bill.`, `reminder.*`, `user.registered`, `user.deleted`
  • New: 20 unit tests for all acceptance criteria

API Endpoints

```
GET /webhooks List endpoints
POST /webhooks Create (returns secret once)
GET /webhooks/:id Get endpoint (no secret)
PATCH /webhooks/:id Update url/events/active
DELETE /webhooks/:id Delete
GET /webhooks/:id/deliveries Last 50 delivery logs
POST /webhooks/:id/test Send test ping
```

Features

  • Event filtering - specify which events to receive
  • Request tracing - unique trace_id for debugging
  • Code deduplication - extracted _emit_expense common method
  • Type safety - complete type annotations
  • Error handling - distinguish timeout, connection errors, etc.
  • Test coverage - 20 comprehensive test cases

Acceptance Criteria

  • Signed delivery via HMAC-SHA256
  • Retry & failure handling with 3-attempt schedule
  • Event types documented in code and route validation

Testing

  • 20 unit tests covering all acceptance criteria
  • Full test suite requires Docker Compose (PostgreSQL + Redis)

Resolves

#77

- Add Webhook and WebhookDelivery ORM models
- Implement HMAC-SHA256 signature verification
- Add 3-attempt retry logic with exponential backoff (0s, 5s, 30s)
- Create REST API endpoints for webhook CRUD operations
- Add test-ping endpoint for webhook testing
- Emit events: expense.*, bill.*, reminder.*, user.registered, user.deleted
- Add 20 unit tests for all acceptance criteria
- Fix: WebhookDelivery model, test file, code duplication, event filtering
- Improve: type annotations, logging, error handling

Features:
- Event filtering - specify which events to receive
- Request tracing - unique trace_id for debugging
- Code deduplication - extracted _emit_expense common method
- Type safety - complete type annotations
- Error handling - distinguish timeout, connection errors, etc.
- Test coverage - 20 comprehensive test cases

Acceptance Criteria:
- Signed delivery via HMAC-SHA256
- Retry & failure handling with 3-attempt schedule
- Event types documented in code and route validation

Resolves rohitdash08#77
@i010542 i010542 requested a review from rohitdash08 as a code owner March 9, 2026 05:17
修复了PR审查发现的关键问题:

P0严重问题修复:
- 修复乐观锁实现:添加版本冲突检查,防止并发更新导致的数据丢失
- 当客户端版本与数据库版本不匹配时返回409错误

P1重要问题修复:
- 集成Flask-Limiter速率限制:为create_webhook端点添加每分钟10次限制
- 启用WebhookAuditLog审计日志:在创建、更新、删除webhook时记录操作历史
- 添加审计日志辅助函数_log_webhook_audit(),记录IP地址和User-Agent

P2改进问题修复:
- 完善配置文档:在.env.example中添加webhook、速率限制、Celery配置示例

其他修复和增强:
- 创建Celery任务文件tasks.py,实现异步webhook重试机制
- 移除test_webhooks.py中的重复测试函数
- 添加依赖项:flask-babel, flask-limiter, celery
- 在_webhook_to_dict()中返回version字段以支持乐观锁
- 修复handle_delivery_error中的会话管理问题
- 优化代码注释和文档

影响范围:
- packages/backend/app/models.py - 添加索引、version字段、WebhookAuditLog模型
- packages/backend/app/routes/webhooks.py - 乐观锁、速率限制、审计日志
- packages/backend/app/services/webhooks.py - URL验证、配置验证、错误处理
- packages/backend/app/tasks.py - 新增Celery异步任务
- packages/backend/tests/test_webhooks.py - 移除重复测试
- packages/backend/requirements.txt - 添加新依赖
- .env.example - 添加配置示例
@i010542
Copy link
Author

i010542 commented Mar 9, 2026

✅ PR审核优化完成

已应用所有审查发现的修复:

🔴 P0 - 严重问题修复

  • 乐观锁实现 - 添加版本冲突检查,防止并发数据丢失

🟡 P1 - 重要问题修复

  • 速率限制 - Flask-Limiter集成,防止API滥用
  • 审计日志 - WebhookAuditLog启用,记录所有操作

🟢 P2 - 改进问题修复

  • 文档完善 - .env.example配置示例
  • 异步任务 - Celery任务队列实现
  • 依赖更新 - flask-babel, flask-limiter, celery

📊 审查结果

  • 总问题数: 10个
  • 已修复: 10个
  • 可以提交: ✅ 是

📝 提交信息

  • Commit 1: feat: Add webhook event system (bf390db)
  • Commit 2: fix: webhook system optimizations (ee3e11c)

所有修复已推送到 webhook-system 分支。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant