File size: 4,021 Bytes
04bb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Webhook endpoints for receiving A2A protocol callbacks from provider agents."""

import hashlib
import hmac
import logging
import time
import uuid
from uuid import UUID

from fastapi import APIRouter, Depends, Header, HTTPException, Request
from sqlalchemy.ext.asyncio import AsyncSession

from src.config import settings
from src.database import get_db
from src.schemas.a2a import JsonRpcRequest, JsonRpcResponse
from src.services.task_broker import TaskBrokerService

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/webhooks", tags=["webhooks"])


async def _verify_webhook_signature(
    request: Request,
    x_webhook_signature: str | None = Header(None, alias="X-Webhook-Signature"),
) -> None:
    """Verify the HMAC-SHA256 webhook signature if a webhook secret is configured."""
    if not settings.webhook_secret:
        if settings.debug:
            return  # No secret configured — skip validation (debug mode only)
        raise HTTPException(
            status_code=503,
            detail="Webhook verification not configured",
        )

    if x_webhook_signature is None:
        raise HTTPException(status_code=401, detail="Missing X-Webhook-Signature header")

    body = await request.body()
    expected = hmac.new(
        settings.webhook_secret.encode(), body, hashlib.sha256
    ).hexdigest()

    if not hmac.compare_digest(expected, x_webhook_signature):
        raise HTTPException(status_code=401, detail="Invalid webhook signature")


@router.post("/a2a/{agent_id}", response_model=JsonRpcResponse)
async def a2a_callback(
    agent_id: UUID,
    payload: JsonRpcRequest,
    db: AsyncSession = Depends(get_db),
    _sig: None = Depends(_verify_webhook_signature),
) -> JsonRpcResponse:
    """Receive A2A JSON-RPC callbacks from provider agents."""
    start = time.monotonic()
    service = TaskBrokerService(db)
    response: JsonRpcResponse | None = None
    error_msg: str | None = None
    success = True

    try:
        if payload.method == "tasks/statusUpdate":
            result = await service.handle_status_update(
                agent_id=agent_id,
                params=payload.params,
            )
            response = JsonRpcResponse(id=payload.id, result=result)

        elif payload.method == "tasks/artifactUpdate":
            result = await service.handle_artifact_update(
                agent_id=agent_id,
                params=payload.params,
            )
            response = JsonRpcResponse(id=payload.id, result=result)

        else:
            success = False
            error_msg = f"Method not found: {payload.method}"
            response = JsonRpcResponse(
                id=payload.id,
                error={"code": -32601, "message": error_msg},
            )

    except Exception as exc:
        success = False
        error_msg = str(exc)[:500]
        raise

    finally:
        latency_ms = int((time.monotonic() - start) * 1000)
        task_id_str = payload.params.get("id") if payload.params else None
        task_id = None
        try:
            task_id = uuid.UUID(task_id_str) if task_id_str else None
        except (ValueError, TypeError):
            pass

        try:
            from src.models.webhook_log import WebhookLog
            log = WebhookLog(
                agent_id=agent_id,
                task_id=task_id,
                direction="inbound",
                method=payload.method,
                request_body=payload.model_dump(),
                response_body=response.model_dump() if response else None,
                status_code=200 if success else 400,
                success=success,
                error_message=error_msg,
                latency_ms=latency_ms,
            )
            db.add(log)
            await db.flush()
        except Exception:
            logger.debug("Failed to persist inbound webhook log", exc_info=True)

    return response