Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(webhook): kafka producer (#5024)

Will Hutchinson33e68186785a
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.json
index 6f7ff530df..62efe7c442 100644
--- a/.github/kafka-cluster-topics.json
+++ b/.github/kafka-cluster-topics.json
@@ -3,5 +3,6 @@
"macro.documents",
"macro.email",
"macro.example",
- "macro.projects"
+ "macro.projects",
+ "macro.webhooks"
]
diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rs
index 6fe2e3c91e..8014a720f6 100644
--- a/crates/macro_event_topics/src/lib.rs
+++ b/crates/macro_event_topics/src/lib.rs
@@ -57,4 +57,6 @@ topics! {
MacroChannelsTopic => "macro.channels",
/// Email lifecycle events (links, messages, threads, labels).
MacroEmailTopic => "macro.email",
+ /// Webhook configuration lifecycle events (created / updated / deleted / validated).
+ MacroWebhooksTopic => "macro.webhooks",
}
diff --git a/crates/macro_event_topics/src/test.rs b/crates/macro_event_topics/src/test.rs
index 826b241a42..5efa13e540 100644
--- a/crates/macro_event_topics/src/test.rs
+++ b/crates/macro_event_topics/src/test.rs
@@ -21,4 +21,5 @@ fn all_topic_names_includes_declared_topics() {
assert!(all_topic_names().contains(&MacroDocumentsTopic.as_str()));
assert!(all_topic_names().contains(&MacroProjectsTopic.as_str()));
assert!(all_topic_names().contains(&MacroChannelsTopic.as_str()));
+ assert!(all_topic_names().contains(&MacroWebhooksTopic.as_str()));
}
diff --git a/crates/webhook/Cargo.toml b/crates/webhook/Cargo.toml
index d4fe513425..3f50d3b740 100644
--- a/crates/webhook/Cargo.toml
+++ b/crates/webhook/Cargo.toml
@@ -7,7 +7,6 @@ version = "0.1.0"
[features]
consumer = [
"dep:macro_env",
- "dep:macro_event_topics",
"dep:rdkafka",
"dep:tokio",
"ingestion",
@@ -31,7 +30,6 @@ ingestion = [
"dep:documents",
"dep:entity_access",
"dep:futures",
- "dep:macro_event_broker",
"dep:uuid",
"ports",
]
@@ -47,7 +45,7 @@ outbound = [
"dep:url",
"ports",
]
-ports = ["dep:serde_json", "dep:url"]
+ports = ["dep:serde_json", "dep:url", "macro_event_broker/ports"]
worker = ["dep:tokio", "ports"]
[dependencies]
@@ -67,8 +65,8 @@ macro_authorization = { path = "../macro_authorization", optional = true, defaul
"axum",
] }
macro_env = { path = "../macro_env", optional = true }
-macro_event_broker = { path = "../macro_event_broker", default-features = false, optional = true }
-macro_event_topics = { path = "../macro_event_topics", optional = true }
+macro_event_broker = { path = "../macro_event_broker", default-features = false }
+macro_event_topics = { path = "../macro_event_topics" }
macro_user_id = { path = "../macro_user_id" }
macro_uuid = { path = "../macro_uuid", optional = true }
model-error-response = { path = "../model-error-response", optional = true }
diff --git a/crates/webhook/README.md b/crates/webhook/README.md
index 53dfff03d7..250604fe81 100644
--- a/crates/webhook/README.md
+++ b/crates/webhook/README.md
@@ -20,10 +20,63 @@ existing `rate_limit` crate with the key
`per-user-validate-webhook:{macro_user_id}:{webhook_id}` and a limit of 10
attempts per 3600-second window.
+### Management lifecycle events
+
+Successful authenticated management mutations publish schema-version-1 events
+to `macro.webhooks`. The Kafka key is the subject webhook ID for every event:
+
+| Event name | Emitted after | Sanitized metadata |
+| --- | --- | --- |
+| `webhook.created` | Creation | `webhook_id`, `workspace_id`, `created_by_user_id`, configuration snapshot, sorted `header_names`, and `created_at` |
+| `webhook.updated` | The final PATCH mutation, including any validity reset | `webhook_id`, `workspace_id`, `actor_user_id`, requested PATCH fields, `headers_updated`, status transition, final `is_valid`, and `updated_at` |
+| `webhook.deleted` | Soft deletion | `webhook_id`, `workspace_id`, and `actor_user_id` |
+| `webhook.validated` | Validation-result persistence, whether valid or invalid | `webhook_id`, `workspace_id`, `actor_user_id`, `is_valid`, optional response status, and a sanitized message |
+
+Created metadata exposes custom-header names but not their values. Updated
+metadata exposes only whether headers were replaced. No lifecycle event includes
+the webhook signing secret or custom-header values. A created event has this
+wire-envelope shape (the Kafka key is not part of the JSON):
+
+```json
+{
+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",
+ "schema_version": 1,
+ "event_type": "webhook.created",
+ "metadata": {
+ "webhook_id": "wh_01998a2f-aaaa-7bbb-8ccc-dddddddddddd",
+ "workspace_id": "team_0123",
+ "created_by_user_id": "macro|[email protected]",
+ "name": "Document updates",
+ "endpoint_url": "https://example.com/hooks/macro",
+ "status": "active",
+ "is_valid": false,
+ "filters": [
+ {
+ "events": ["document.updated"],
+ "ids": ["0197f776-6e7b-7c69-a251-780ae754d3e4"]
+ }
+ ],
+ "header_names": ["Authorization", "X-Customer"],
+ "created_at": "2026-07-20T17:01:02Z"
+ }
+}
+```
+
+Publication is post-mutation and fire-and-forget. The API result does not wait
+for Kafka delivery, and a scheduling or publication failure does not roll back
+or fail an otherwise successful mutation. There is no transaction, outbox, or
+retry coupling persistence to publication, so a persisted mutation can have no
+corresponding event; this accepted gap gives lifecycle production at-most-once
+semantics relative to management mutations.
+
+Only the authenticated create, patch, delete, and validate flows produce these
+lifecycle events. Event ingestion, queue workers, HTTP delivery, and delivery
+bookkeeping (including success/failure timestamp updates) do not produce them.
+
## Event ingestion and matching
-The `webhook-event-ingestion` Kafka consumer reads `macro.documents` and
-`macro.channels`. It supports these event names:
+The `webhook-event-ingestion` Kafka consumer reads `macro.documents`,
+`macro.channels`, and `macro.webhooks`. It supports these event names:
- Documents: `document.created`, `document.updated`, `document.deleted`, and
`document.copied`.
@@ -32,17 +85,27 @@ The `webhook-event-ingestion` Kafka consumer reads `macro.documents` and
`channel.message_deleted`, `channel.message_attachment_created`,
`channel.message_attachment_removed`, `channel.participant_added`, and
`channel.participant_removed`.
-
-For each event, ingestion asks `EntityAccessService` for the people who
-currently have access to its entity. The matching workspace set contains each
-person's Macro user ID, for personal webhooks, plus every team ID to which any
-of those people belongs. The set is deduplicated before matching, so one person
-or team is considered only once.
-
-A webhook matches only when it is active, valid, not soft-deleted, owned by one
-of those personal or team workspaces, and has one filter element that matches
-both the exact event name and entity ID. An event match in one filter element
-cannot be combined with an ID match in another element.
+- Webhooks: `webhook.created`, `webhook.updated`, `webhook.deleted`, and
+ `webhook.validated`.
+
+For document and channel events, ingestion asks `EntityAccessService` for the
+people who currently have access to the entity. The matching workspace set
+contains each person's Macro user ID, for personal webhooks, plus every team ID
+to which any of those people belongs. The set is deduplicated before matching,
+so one person or team is considered only once.
+
+Webhook events use `entity_type = "webhook"` and take the event metadata's
+`workspace_id` as the sole matching workspace. This is a strict owner-workspace
+rule: ingestion does not perform an entity-access lookup or personal/team
+workspace expansion, and actor membership does not make another workspace
+eligible.
+
+A webhook matches only when it is active, valid, not soft-deleted, owned by an
+eligible workspace, and has one filter element that matches both the exact
+event name and entity ID. An event match in one filter element cannot be
+combined with an ID match in another element. Because deletion is published
+after soft deletion, a webhook cannot receive its own `webhook.deleted` event;
+other eligible webhooks in the same owner workspace can receive it.
### Filter-ID semantics
@@ -61,7 +124,9 @@ matches every entity ID for that filter's events:
For document events, IDs always mean the event's `document_id`. For every
channel event, IDs mean `channel_id`. This includes message, attachment, and
participant events: their message, attachment, and participant IDs are not used
-for webhook filtering.
+for webhook filtering. For webhook events, IDs mean the subject `webhook_id`;
+an absent or `null` `ids` field matches every webhook ID in the strict owner
+workspace.
The matching query stores filters in the `webhook.filters` `JSONB NOT NULL`
column, protected by the `webhook_filters_is_array` constraint. It uses the
@@ -99,8 +164,8 @@ reuse the same delivery record.
FIFO preserves the order in which the current consumer enqueues events for one
webhook. It does not create a global order across Kafka partitions or across the
-`macro.documents` and `macro.channels` topics. Consumers must therefore treat
-the order as observed order, not total event order.
+`macro.documents`, `macro.channels`, and `macro.webhooks` topics. Consumers must
+therefore treat the order as observed order, not total event order.
## HTTP delivery contract
diff --git a/crates/webhook/src/domain/events.rs b/crates/webhook/src/domain/events.rs
new file mode 100644
index 0000000000..3bff1c8422
--- /dev/null
+++ b/crates/webhook/src/domain/events.rs
@@ -0,0 +1,179 @@
+//! Kafka event models for the `macro.webhooks` topic.
+//!
+//! Lifecycle events contain sanitized webhook configuration metadata. Signing
+//! secrets and custom-header values are deliberately excluded from every
+//! payload.
+
+#[cfg(test)]
+mod test;
+
+use chrono::{DateTime, Utc};
+use macro_event_broker::{Event, MacroEvent, TopicEvent};
+use macro_event_topics::MacroWebhooksTopic;
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::{Deserialize, Serialize};
+
+use super::models::{WebhookFilters, WebhookStatus};
+
+/// Metadata for [`WebhookTopicEvent::Created`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct WebhookCreatedMetadata {
+ /// Identifier of the created webhook.
+ pub webhook_id: String,
+ /// Identifier of the workspace that owns the webhook.
+ pub workspace_id: String,
+ /// Authenticated user who created the webhook.
+ pub created_by_user_id: MacroUserIdStr<'static>,
+ /// Display name of the webhook.
+ pub name: String,
+ /// URL that receives webhook deliveries.
+ pub endpoint_url: String,
+ /// Lifecycle status of the webhook.
+ pub status: WebhookStatus,
+ /// Whether the endpoint configuration has passed validation.
+ pub is_valid: bool,
+ /// Event and entity filters configured for the webhook.
+ pub filters: WebhookFilters,
+ /// Names of configured custom headers, with their values omitted.
+ pub header_names: Vec<String>,
+ /// Creation timestamp reported by the repository.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Metadata for [`WebhookTopicEvent::Updated`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct WebhookUpdatedMetadata {
+ /// Identifier of the updated webhook.
+ pub webhook_id: String,
+ /// Identifier of the workspace that owns the webhook.
+ pub workspace_id: String,
+ /// Authenticated user who updated the webhook.
+ pub actor_user_id: MacroUserIdStr<'static>,
+ /// Requested display name, or `None` when the PATCH omitted it.
+ pub name: Option<String>,
+ /// Requested endpoint URL, or `None` when the PATCH omitted it.
+ pub endpoint_url: Option<String>,
+ /// Requested event filters, or `None` when the PATCH omitted them.
+ pub filters: Option<WebhookFilters>,
+ /// Whether the PATCH replaced custom headers; names and values are omitted.
+ pub headers_updated: bool,
+ /// Requested lifecycle status, or `None` when the PATCH omitted it.
+ pub status: Option<WebhookStatus>,
+ /// Lifecycle status before the update, present when `status` is present.
+ pub previous_status: Option<WebhookStatus>,
+ /// Whether the final endpoint configuration has passed validation.
+ pub is_valid: bool,
+ /// Update timestamp reported by the repository after the final mutation.
+ pub updated_at: DateTime<Utc>,
+}
+
+/// Metadata for [`WebhookTopicEvent::Deleted`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct WebhookDeletedMetadata {
+ /// Identifier of the deleted webhook.
+ pub webhook_id: String,
+ /// Identifier of the workspace that owned the webhook.
+ pub workspace_id: String,
+ /// Authenticated user who deleted the webhook.
+ pub actor_user_id: MacroUserIdStr<'static>,
+}
+
+/// Metadata for [`WebhookTopicEvent::Validated`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct WebhookValidatedMetadata {
+ /// Identifier of the validated webhook.
+ pub webhook_id: String,
+ /// Identifier of the workspace that owns the webhook.
+ pub workspace_id: String,
+ /// Authenticated user who requested validation.
+ pub actor_user_id: MacroUserIdStr<'static>,
+ /// Persisted endpoint validation outcome.
+ pub is_valid: bool,
+ /// HTTP status returned by the endpoint, when it responded.
+ pub response_status: Option<u16>,
+ /// Sanitized validation result message, when available.
+ pub message: Option<String>,
+}
+
+/// Lifecycle events published to [`MacroWebhooksTopic`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "event_type", content = "metadata")]
+pub enum WebhookTopicEvent {
+ /// A webhook was created.
+ #[serde(rename = "webhook.created")]
+ Created(WebhookCreatedMetadata),
+ /// A webhook configuration was updated.
+ #[serde(rename = "webhook.updated")]
+ Updated(WebhookUpdatedMetadata),
+ /// A webhook was soft-deleted.
+ #[serde(rename = "webhook.deleted")]
+ Deleted(WebhookDeletedMetadata),
+ /// A webhook endpoint validation completed.
+ #[serde(rename = "webhook.validated")]
+ Validated(WebhookValidatedMetadata),
+}
+
+impl TopicEvent for WebhookTopicEvent {
+ type Topic = MacroWebhooksTopic;
+
+ fn schema_version(&self) -> u8 {
+ 1
+ }
+}
+
+/// Publishable lifecycle event for [`MacroWebhooksTopic`], keyed by webhook id.
+pub struct WebhookMacroEvent {
+ key: String,
+ event: Event<WebhookTopicEvent>,
+}
+
+impl WebhookMacroEvent {
+ /// Build a created event keyed by the created webhook id.
+ pub fn created(key: impl Into<String>, metadata: WebhookCreatedMetadata) -> Self {
+ Self::new(key, WebhookTopicEvent::Created(metadata))
+ }
+
+ /// Build an updated event keyed by the updated webhook id.
+ pub fn updated(key: impl Into<String>, metadata: WebhookUpdatedMetadata) -> Self {
+ Self::new(key, WebhookTopicEvent::Updated(metadata))
+ }
+
+ /// Build a deleted event keyed by the deleted webhook id.
+ pub fn deleted(key: impl Into<String>, metadata: WebhookDeletedMetadata) -> Self {
+ Self::new(key, WebhookTopicEvent::Deleted(metadata))
+ }
+
+ /// Build a validated event keyed by the validated webhook id.
+ pub fn validated(key: impl Into<String>, metadata: WebhookValidatedMetadata) -> Self {
+ Self::new(key, WebhookTopicEvent::Validated(metadata))
+ }
+
+ /// Build an event from a topic-specific lifecycle event.
+ pub fn new(key: impl Into<String>, event: WebhookTopicEvent) -> Self {
+ Self::with_event(key, Event::new(event))
+ }
+
+ /// Build an event from a pre-built envelope.
+ pub fn with_event(key: impl Into<String>, event: Event<WebhookTopicEvent>) -> Self {
+ Self {
+ key: key.into(),
+ event,
+ }
+ }
+}
+
+impl MacroEvent for WebhookMacroEvent {
+ type EventPayload = WebhookTopicEvent;
+
+ fn key(&self) -> &str {
+ &self.key
+ }
+
+ fn event(&self) -> &Event<Self::EventPayload> {
+ &self.event
+ }
+
+ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {
+ Self::with_event(key, event)
+ }
+}
diff --git a/crates/webhook/src/domain/events/test.rs b/crates/webhook/src/domain/events/test.rs
new file mode 100644
index 0000000000..d64141fd07
--- /dev/null
+++ b/crates/webhook/src/domain/events/test.rs
@@ -0,0 +1,294 @@
+use chrono::{DateTime, Utc};
+use macro_event_broker::{Event, MacroEvent};
+use macro_event_topics::Topic;
+use macro_user_id::user_id::MacroUserIdStr;
+use serde_json::json;
+use uuid::Uuid;
+
+use super::*;
+use crate::domain::models::WebhookFilter;
+
+const WEBHOOK_ID: &str = "wh_01998a2f-aaaa-7bbb-8ccc-dddddddddddd";
+const WORKSPACE_ID: &str = "team_0123";
+
+fn user_id(id: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(id.to_string()).expect("valid user id")
+}
+
+fn timestamp(value: &str) -> DateTime<Utc> {
+ DateTime::parse_from_rfc3339(value)
+ .expect("valid timestamp")
+ .with_timezone(&Utc)
+}
+
+fn filters() -> WebhookFilters {
+ vec![
+ WebhookFilter {
+ events: vec!["document.updated".to_string()],
+ ids: Some(vec!["document-123".to_string()]),
+ },
+ WebhookFilter {
+ events: vec!["webhook.updated".to_string()],
+ ids: None,
+ },
+ ]
+}
+
+#[test]
+fn created_event_has_exact_sanitized_wire_shape() {
+ let event = Event::with_event_id(
+ Uuid::parse_str("01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f").expect("valid event id"),
+ WebhookTopicEvent::Created(WebhookCreatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ created_by_user_id: user_id("macro|[email protected]"),
+ name: "Document updates".to_string(),
+ endpoint_url: "https://example.com/hooks/macro".to_string(),
+ status: WebhookStatus::Active,
+ is_valid: false,
+ filters: filters(),
+ header_names: vec!["Authorization".to_string(), "X-Customer".to_string()],
+ created_at: timestamp("2026-07-20T17:01:02Z"),
+ }),
+ );
+
+ assert_eq!(
+ serde_json::to_value(event).expect("serializable event"),
+ json!({
+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",
+ "schema_version": 1,
+ "event_type": "webhook.created",
+ "metadata": {
+ "webhook_id": WEBHOOK_ID,
+ "workspace_id": WORKSPACE_ID,
+ "created_by_user_id": "macro|[email protected]",
+ "name": "Document updates",
+ "endpoint_url": "https://example.com/hooks/macro",
+ "status": "active",
+ "is_valid": false,
+ "filters": [
+ {
+ "events": ["document.updated"],
+ "ids": ["document-123"],
+ },
+ {
+ "events": ["webhook.updated"],
+ },
+ ],
+ "header_names": ["Authorization", "X-Customer"],
+ "created_at": "2026-07-20T17:01:02Z",
+ },
+ })
+ );
+}
+
+#[test]
+fn updated_event_has_exact_sanitized_wire_shape() {
+ let event = Event::with_event_id(
+ Uuid::parse_str("01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a").expect("valid event id"),
+ WebhookTopicEvent::Updated(WebhookUpdatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ name: None,
+ endpoint_url: Some("https://example.com/hooks/updated".to_string()),
+ filters: Some(filters()),
+ headers_updated: true,
+ status: Some(WebhookStatus::Paused),
+ previous_status: Some(WebhookStatus::Active),
+ is_valid: false,
+ updated_at: timestamp("2026-07-20T17:03:11Z"),
+ }),
+ );
+
+ assert_eq!(
+ serde_json::to_value(event).expect("serializable event"),
+ json!({
+ "event_id": "01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a",
+ "schema_version": 1,
+ "event_type": "webhook.updated",
+ "metadata": {
+ "webhook_id": WEBHOOK_ID,
+ "workspace_id": WORKSPACE_ID,
+ "actor_user_id": "macro|[email protected]",
+ "name": null,
+ "endpoint_url": "https://example.com/hooks/updated",
+ "filters": [
+ {
+ "events": ["document.updated"],
+ "ids": ["document-123"],
+ },
+ {
+ "events": ["webhook.updated"],
+ },
+ ],
+ "headers_updated": true,
+ "status": "paused",
+ "previous_status": "active",
+ "is_valid": false,
+ "updated_at": "2026-07-20T17:03:11Z",
+ },
+ })
+ );
+}
+
+#[test]
+fn deleted_event_has_exact_wire_shape() {
+ let event = Event::with_event_id(
+ Uuid::parse_str("01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b").expect("valid event id"),
+ WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ }),
+ );
+
+ assert_eq!(
+ serde_json::to_value(event).expect("serializable event"),
+ json!({
+ "event_id": "01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b",
+ "schema_version": 1,
+ "event_type": "webhook.deleted",
+ "metadata": {
+ "webhook_id": WEBHOOK_ID,
+ "workspace_id": WORKSPACE_ID,
+ "actor_user_id": "macro|[email protected]",
+ },
+ })
+ );
+}
+
+#[test]
+fn validated_event_has_exact_sanitized_wire_shape() {
+ let event = Event::with_event_id(
+ Uuid::parse_str("01998a30-4d5e-7f60-8152-8d9e0f1a2b3c").expect("valid event id"),
+ WebhookTopicEvent::Validated(WebhookValidatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ is_valid: false,
+ response_status: Some(401),
+ message: Some("endpoint rejected validation request".to_string()),
+ }),
+ );
+
+ assert_eq!(
+ serde_json::to_value(event).expect("serializable event"),
+ json!({
+ "event_id": "01998a30-4d5e-7f60-8152-8d9e0f1a2b3c",
+ "schema_version": 1,
+ "event_type": "webhook.validated",
+ "metadata": {
+ "webhook_id": WEBHOOK_ID,
+ "workspace_id": WORKSPACE_ID,
+ "actor_user_id": "macro|[email protected]",
+ "is_valid": false,
+ "response_status": 401,
+ "message": "endpoint rejected validation request",
+ },
+ })
+ );
+}
+
+#[test]
+fn constructors_key_events_by_subject_webhook_id() {
+ let cases = [
+ WebhookMacroEvent::created(
+ WEBHOOK_ID,
+ WebhookCreatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ created_by_user_id: user_id("macro|[email protected]"),
+ name: "Lifecycle events".to_string(),
+ endpoint_url: "https://example.com/hooks/macro".to_string(),
+ status: WebhookStatus::Active,
+ is_valid: false,
+ filters: vec![],
+ header_names: vec![],
+ created_at: timestamp("2026-07-20T17:01:02Z"),
+ },
+ ),
+ WebhookMacroEvent::updated(
+ WEBHOOK_ID,
+ WebhookUpdatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ name: None,
+ endpoint_url: None,
+ filters: None,
+ headers_updated: false,
+ status: None,
+ previous_status: None,
+ is_valid: true,
+ updated_at: timestamp("2026-07-20T17:03:11Z"),
+ },
+ ),
+ WebhookMacroEvent::deleted(
+ WEBHOOK_ID,
+ WebhookDeletedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ },
+ ),
+ WebhookMacroEvent::validated(
+ WEBHOOK_ID,
+ WebhookValidatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ is_valid: true,
+ response_status: Some(204),
+ message: None,
+ },
+ ),
+ ];
+
+ let expected_event_types = [
+ "webhook.created",
+ "webhook.updated",
+ "webhook.deleted",
+ "webhook.validated",
+ ];
+
+ for (event, expected_event_type) in cases.into_iter().zip(expected_event_types) {
+ assert_eq!(event.key(), WEBHOOK_ID);
+ assert_eq!(event.topic().as_str(), "macro.webhooks");
+ assert_eq!(event.event().schema_version, 1);
+ assert_eq!(
+ serde_json::to_value(event.event()).expect("serializable event")["event_type"],
+ expected_event_type
+ );
+ }
+}
+
+#[test]
+fn macro_event_round_trips_without_exposing_secrets_or_header_values() {
+ let original = WebhookMacroEvent::created(
+ WEBHOOK_ID,
+ WebhookCreatedMetadata {
+ webhook_id: WEBHOOK_ID.to_string(),
+ workspace_id: WORKSPACE_ID.to_string(),
+ created_by_user_id: user_id("macro|[email protected]"),
+ name: "Lifecycle events".to_string(),
+ endpoint_url: "https://example.com/hooks/macro".to_string(),
+ status: WebhookStatus::Active,
+ is_valid: false,
+ filters: filters(),
+ header_names: vec!["Authorization".to_string()],
+ created_at: timestamp("2026-07-20T17:01:02Z"),
+ },
+ );
+ let payload = serde_json::to_vec(original.event()).expect("serializable event");
+ let serialized = String::from_utf8(payload.clone()).expect("JSON is UTF-8");
+
+ assert!(!serialized.contains("signing_secret"));
+ assert!(!serialized.contains("secret-value"));
+ assert!(!serialized.contains("custom-header-value"));
+
+ let decoded = WebhookMacroEvent::decode(original.key(), &payload).expect("decodable event");
+ assert_eq!(decoded.key(), WEBHOOK_ID);
+ assert_eq!(decoded.event(), original.event());
+ assert_eq!(decoded.topic().as_str(), "macro.webhooks");
+}
diff --git a/crates/webhook/src/domain/ingestion.rs b/crates/webhook/src/domain/ingestion.rs
index b25da39a5d..e77f1aa2e5 100644
--- a/crates/webhook/src/domain/ingestion.rs
+++ b/crates/webhook/src/domain/ingestion.rs
@@ -9,6 +9,7 @@
mod test;
use crate::domain::{
+ events::WebhookTopicEvent,
models::{NormalizedWebhookEvent, WebhookEventQueueMessage},
ports::{WebhookEventEnqueuer, WebhookRepo, WebhookWorkspaceResolver},
};
@@ -27,6 +28,7 @@ use uuid::Uuid;
const DOCUMENT_ENTITY_TYPE: &str = "document";
const CHANNEL_ENTITY_TYPE: &str = "channel";
+const WEBHOOK_ENTITY_TYPE: &str = "webhook";
/// Webhook event ingestion error.
#[derive(Debug, thiserror::Error)]
@@ -90,6 +92,12 @@ pub trait WebhookEventIngestionService: Clone + Send + Sync + 'static {
&self,
event: Event<ChannelTopicEvent>,
) -> impl Future<Output = Result<(), WebhookEventIngestionError>> + Send;
+
+ /// Ingest one `macro.webhooks` event envelope.
+ fn ingest_webhook_event(
+ &self,
+ event: Event<WebhookTopicEvent>,
+ ) -> impl Future<Output = Result<(), WebhookEventIngestionError>> + Send;
}
/// Webhook event ingestion service implementation.
@@ -139,11 +147,10 @@ where
entity_id = %event.entity_id,
accessor_count = tracing::field::Empty,
workspace_count = tracing::field::Empty,
- match_count = tracing::field::Empty,
),
err
)]
- async fn ingest_normalized_event(
+ async fn resolve_entity_access_and_enqueue(
&self,
event: NormalizedWebhookEvent,
entity_type: EntityType,
@@ -160,6 +167,26 @@ where
.map_err(|error| WebhookEventIngestionError::WorkspaceResolution(error.into()))?;
tracing::Span::current().record("workspace_count", workspace_ids.len());
+ self.match_and_enqueue(event, workspace_ids).await
+ }
+
+ #[tracing::instrument(
+ skip_all,
+ fields(
+ event_id = %event.event_id,
+ event_name = %event.event_name,
+ entity_type = %event.entity_type,
+ entity_id = %event.entity_id,
+ workspace_count = workspace_ids.len(),
+ match_count = tracing::field::Empty,
+ ),
+ err
+ )]
+ async fn match_and_enqueue(
+ &self,
+ event: NormalizedWebhookEvent,
+ workspace_ids: Vec<String>,
+ ) -> Result<(), WebhookEventIngestionError> {
let webhooks = self
.repository
.list_active_webhooks_matching_event(
@@ -263,6 +290,51 @@ fn normalized_channel_event(
))
}
+fn normalized_webhook_event(
+ event: &Event<WebhookTopicEvent>,
+) -> Result<(NormalizedWebhookEvent, String), WebhookEventIngestionError> {
+ let (event_name, webhook_id, workspace_id) = match &event.event {
+ WebhookTopicEvent::Created(metadata) => (
+ "webhook.created",
+ &metadata.webhook_id,
+ &metadata.workspace_id,
+ ),
+ WebhookTopicEvent::Updated(metadata) => (
+ "webhook.updated",
+ &metadata.webhook_id,
+ &metadata.workspace_id,
+ ),
+ WebhookTopicEvent::Deleted(metadata) => (
+ "webhook.deleted",
+ &metadata.webhook_id,
+ &metadata.workspace_id,
+ ),
+ WebhookTopicEvent::Validated(metadata) => (
+ "webhook.validated",
+ &metadata.webhook_id,
+ &metadata.workspace_id,
+ ),
+ };
+
+ if webhook_id.is_empty() || !webhook_id.starts_with("wh_") {
+ return Err(WebhookEventIngestionError::InvalidEntityId {
+ entity_type: WEBHOOK_ENTITY_TYPE,
+ entity_id: webhook_id.clone(),
+ });
+ }
+
+ let broker_envelope = serde_json::to_value(event)?;
+ let normalized = normalized_event(
+ event.event_id,
+ event.schema_version,
+ event_name,
+ WEBHOOK_ENTITY_TYPE,
+ webhook_id,
+ broker_envelope,
+ );
+ Ok((normalized, workspace_id.clone()))
+}
+
fn normalized_event(
event_id: Uuid,
schema_version: u8,
@@ -295,7 +367,7 @@ where
event: Event<DocumentTopicEvent>,
) -> Result<(), WebhookEventIngestionError> {
let event = normalized_document_event(&event)?;
- self.ingest_normalized_event(event, EntityType::Document)
+ self.resolve_entity_access_and_enqueue(event, EntityType::Document)
.await
}
@@ -305,7 +377,16 @@ where
event: Event<ChannelTopicEvent>,
) -> Result<(), WebhookEventIngestionError> {
let event = normalized_channel_event(&event)?;
- self.ingest_normalized_event(event, EntityType::Channel)
+ self.resolve_entity_access_and_enqueue(event, EntityType::Channel)
.await
}
+
+ #[tracing::instrument(skip(self, event), fields(event_id = %event.event_id), err)]
+ async fn ingest_webhook_event(
+ &self,
+ event: Event<WebhookTopicEvent>,
+ ) -> Result<(), WebhookEventIngestionError> {
+ let (event, workspace_id) = normalized_webhook_event(&event)?;
+ self.match_and_enqueue(event, vec![workspace_id]).await
+ }
}
diff --git a/crates/webhook/src/domain/ingestion/test.rs b/crates/webhook/src/domain/ingestion/test.rs
index 3b89c662a6..e4d0be4ee0 100644
--- a/crates/webhook/src/domain/ingestion/test.rs
+++ b/crates/webhook/src/domain/ingestion/test.rs
@@ -1,5 +1,9 @@
use super::*;
use crate::domain::{
+ events::{
+ WebhookCreatedMetadata, WebhookDeletedMetadata, WebhookTopicEvent, WebhookUpdatedMetadata,
+ WebhookValidatedMetadata,
+ },
models::{
CreateWebhookRequest, PatchWebhookRequest, Webhook, WebhookEventQueueMessage, WebhookStatus,
},
@@ -654,6 +658,92 @@ fn channel_event_cases() -> Vec<EventCase> {
]
}
+struct WebhookEventCase {
+ event: Event<WebhookTopicEvent>,
+ event_name: &'static str,
+ webhook_id: &'static str,
+ workspace_id: &'static str,
+}
+
+fn webhook_event_cases() -> Vec<WebhookEventCase> {
+ vec![
+ WebhookEventCase {
+ event: Event::with_event_id_and_schema_version(
+ Uuid::parse_str("01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f").unwrap(),
+ 2,
+ WebhookTopicEvent::Created(WebhookCreatedMetadata {
+ webhook_id: "wh_created".to_string(),
+ workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
+ created_by_user_id: user_id("macro|[email protected]"),
+ name: "Created webhook".to_string(),
+ endpoint_url: "https://example.com/created".to_string(),
+ status: WebhookStatus::Active,
+ is_valid: false,
+ filters: vec![],
+ header_names: vec!["X-Customer".to_string()],
+ created_at: timestamp(),
+ }),
+ ),
+ event_name: "webhook.created",
+ webhook_id: "wh_created",
+ workspace_id: PERSONAL_WORKSPACE_ID,
+ },
+ WebhookEventCase {
+ event: Event::with_event_id_and_schema_version(
+ Uuid::parse_str("01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a").unwrap(),
+ 3,
+ WebhookTopicEvent::Updated(WebhookUpdatedMetadata {
+ webhook_id: "wh_updated".to_string(),
+ workspace_id: TEAM_WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ name: Some("Updated webhook".to_string()),
+ endpoint_url: None,
+ filters: None,
+ headers_updated: true,
+ status: Some(WebhookStatus::Paused),
+ previous_status: Some(WebhookStatus::Active),
+ is_valid: false,
+ updated_at: timestamp(),
+ }),
+ ),
+ event_name: "webhook.updated",
+ webhook_id: "wh_updated",
+ workspace_id: TEAM_WORKSPACE_ID,
+ },
+ WebhookEventCase {
+ event: Event::with_event_id_and_schema_version(
+ Uuid::parse_str("01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b").unwrap(),
+ 4,
+ WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
+ webhook_id: "wh_deleted".to_string(),
+ workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ }),
+ ),
+ event_name: "webhook.deleted",
+ webhook_id: "wh_deleted",
+ workspace_id: PERSONAL_WORKSPACE_ID,
+ },
+ WebhookEventCase {
+ event: Event::with_event_id_and_schema_version(
+ Uuid::parse_str("01998a30-4d5e-7f60-8152-8d9e0f1a2b3c").unwrap(),
+ 5,
+ WebhookTopicEvent::Validated(WebhookValidatedMetadata {
+ webhook_id: "wh_validated".to_string(),
+ workspace_id: TEAM_WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ is_valid: true,
+ response_status: Some(204),
+ message: None,
+ }),
+ ),
+ event_name: "webhook.validated",
+ webhook_id: "wh_validated",
+ workspace_id: TEAM_WORKSPACE_ID,
+ },
+ ]
+}
+
#[tokio::test]
async fn normalizes_and_matches_all_fourteen_event_variants() {
let event_cases = document_event_cases()
@@ -727,6 +817,167 @@ async fn normalizes_and_matches_all_fourteen_event_variants() {
}
}
+#[tokio::test]
+async fn normalizes_all_webhook_variants_with_direct_workspace_ownership() {
+ for event_case in webhook_event_cases() {
+ let access = MockAccessService {
+ users: vec![user_id("macro|[email protected]")],
+ failure: Some(AccessFailure::Internal),
+ calls: Arc::default(),
+ };
+ let repository = MockRepository::new(
+ vec!["workspace-that-must-not-be-used".to_string()],
+ vec![webhook("wh_match", event_case.workspace_id)],
+ );
+ lock(&repository.state).fail_workspace_resolution = true;
+ let enqueuer = MockEnqueuer::default();
+ let service = service(access.clone(), repository.clone(), enqueuer.clone());
+ let expected_envelope = serde_json::to_value(&event_case.event).unwrap();
+ let before_ingestion = Utc::now();
+
+ service
+ .ingest_webhook_event(event_case.event.clone())
+ .await
+ .unwrap_or_else(|error| {
+ panic!("{} should be ingested: {error}", event_case.event_name)
+ });
+ let after_ingestion = Utc::now();
+
+ assert!(
+ lock(&access.calls).is_empty(),
+ "entity access must not be called for {}",
+ event_case.event_name
+ );
+ let repository_state = lock(&repository.state);
+ assert!(
+ repository_state.workspace_calls.is_empty(),
+ "workspace resolution must not be called for {}",
+ event_case.event_name
+ );
+ assert_eq!(
+ repository_state.match_calls,
+ vec![MatchCall {
+ workspace_ids: vec![event_case.workspace_id.to_string()],
+ event_name: event_case.event_name.to_string(),
+ entity_id: event_case.webhook_id.to_string(),
+ }],
+ "strict workspace matching for {}",
+ event_case.event_name
+ );
+ drop(repository_state);
+
+ let enqueuer_state = lock(&enqueuer.state);
+ assert_eq!(enqueuer_state.attempted_messages.len(), 1);
+ let message = &enqueuer_state.attempted_messages[0];
+ assert_eq!(message.webhook_id, "wh_match");
+ assert_eq!(
+ message.event.event_id,
+ event_case.event.event_id.to_string()
+ );
+ assert_eq!(
+ message.event.schema_version,
+ event_case.event.schema_version
+ );
+ assert_eq!(message.event.event_name, event_case.event_name);
+ assert_eq!(message.event.entity_type, WEBHOOK_ENTITY_TYPE);
+ assert_eq!(message.event.entity_id, event_case.webhook_id);
+ assert_eq!(message.event.ordering_key, event_case.webhook_id);
+ assert_eq!(message.event.broker_envelope, expected_envelope);
+ assert_eq!(
+ message.event.broker_envelope["event_type"],
+ event_case.event_name
+ );
+ assert!(message.event.occurred_at >= before_ingestion);
+ assert!(message.event.occurred_at <= after_ingestion);
+ }
+}
+
+#[tokio::test]
+async fn webhook_event_fans_out_complete_envelope_to_every_match() {
+ let event_case = webhook_event_cases().remove(1);
+ let expected_envelope = serde_json::to_value(&event_case.event).unwrap();
+ let access = MockAccessService::with_users(Vec::new());
+ let repository = MockRepository::new(
+ Vec::new(),
+ vec![
+ webhook("wh_first_match", event_case.workspace_id),
+ webhook("wh_second_match", event_case.workspace_id),
+ ],
+ );
+ let enqueuer = MockEnqueuer::default();
+ let service = service(access.clone(), repository.clone(), enqueuer.clone());
+
+ service
+ .ingest_webhook_event(event_case.event)
+ .await
+ .expect("webhook fan-out succeeds");
+
+ assert!(lock(&access.calls).is_empty());
+ let repository_state = lock(&repository.state);
+ assert!(repository_state.workspace_calls.is_empty());
+ assert_eq!(
+ repository_state.match_calls,
+ vec![MatchCall {
+ workspace_ids: vec![TEAM_WORKSPACE_ID.to_string()],
+ event_name: "webhook.updated".to_string(),
+ entity_id: "wh_updated".to_string(),
+ }]
+ );
+ drop(repository_state);
+
+ let enqueuer_state = lock(&enqueuer.state);
+ let mut webhook_ids = enqueuer_state
+ .attempted_messages
+ .iter()
+ .map(|message| message.webhook_id.as_str())
+ .collect::<Vec<_>>();
+ webhook_ids.sort_unstable();
+ assert_eq!(webhook_ids, vec!["wh_first_match", "wh_second_match"]);
+ assert_eq!(enqueuer_state.completed_webhook_ids.len(), 2);
+ for message in &enqueuer_state.attempted_messages {
+ assert_eq!(message.event.broker_envelope, expected_envelope);
+ }
+}
+
+#[tokio::test]
+async fn malformed_webhook_ids_are_permanent_and_skip_all_resolution() {
+ let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
+ let repository = MockRepository::new(
+ vec![PERSONAL_WORKSPACE_ID.to_string()],
+ vec![webhook("wh_match", PERSONAL_WORKSPACE_ID)],
+ );
+ let enqueuer = MockEnqueuer::default();
+ let service = service(access.clone(), repository.clone(), enqueuer.clone());
+
+ for invalid_id in ["", "webhook_without_prefix"] {
+ let event = Event::new(WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
+ webhook_id: invalid_id.to_string(),
+ workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
+ actor_user_id: user_id("macro|[email protected]"),
+ }));
+
+ let error = service
+ .ingest_webhook_event(event)
+ .await
+ .expect_err("malformed webhook ids are rejected");
+
+ assert!(matches!(
+ &error,
+ WebhookEventIngestionError::InvalidEntityId {
+ entity_type: WEBHOOK_ENTITY_TYPE,
+ entity_id,
+ } if entity_id == invalid_id
+ ));
+ assert!(!error.is_transient());
+ }
+
+ assert!(lock(&access.calls).is_empty());
+ let repository_state = lock(&repository.state);
+ assert!(repository_state.workspace_calls.is_empty());
+ assert!(repository_state.match_calls.is_empty());
+ assert!(lock(&enqueuer.state).attempted_messages.is_empty());
+}
+
#[tokio::test]
async fn no_accessors_resolves_no_workspaces_and_enqueues_nothing() {
let access = MockAccessService::with_users(Vec::new());
diff --git a/crates/webhook/src/domain/mod.rs b/crates/webhook/src/domain/mod.rs
index 79230450ad..948d26532e 100644
--- a/crates/webhook/src/domain/mod.rs
+++ b/crates/webhook/src/domain/mod.rs
@@ -3,6 +3,8 @@
#[cfg(feature = "ports")]
/// Webhook event delivery state machine and retry policy.
pub mod delivery;
+/// Webhook lifecycle event contracts.
+pub mod events;
#[cfg(feature = "ingestion")]
/// Webhook event ingestion service.
pub mod ingestion;
diff --git a/crates/webhook/src/domain/service.rs b/crates/webhook/src/domain/service.rs
index 74a6fabfe8..823d7d4960 100644
--- a/crates/webhook/src/domain/service.rs
+++ b/crates/webhook/src/domain/service.rs
@@ -1,6 +1,10 @@
//! Webhook service implementation.
use super::{
+ events::{
+ WebhookCreatedMetadata, WebhookDeletedMetadata, WebhookMacroEvent, WebhookUpdatedMetadata,
+ WebhookValidatedMetadata,
+ },
models::{
CreateWebhookRequest, PatchWebhookRequest, ValidateWebhookResponse, Webhook,
WebhookEndpointSchemePolicy, WebhookFilters, WebhookId, WebhookScope,
@@ -9,6 +13,7 @@ use super::{
ports::{WebhookError, WebhookRepo, WebhookService, WebhookValidationClient},
};
use chrono::Utc;
+use macro_event_broker::MacroEventBroker;
use macro_user_id::user_id::MacroUserIdStr;
use std::net::IpAddr;
use url::Url;
@@ -22,19 +27,21 @@ const VALIDATION_EVENT_NAME: &str = "webhook.validation.test";
/// Webhook service implementation.
#[derive(Debug, Clone)]
-pub struct WebhookServiceImpl<R, V> {
+pub struct WebhookServiceImpl<R, V, B> {
repo: R,
validation_client: V,
endpoint_scheme_policy: WebhookEndpointSchemePolicy,
+ event_broker: B,
}
-impl<R, V> WebhookServiceImpl<R, V> {
+impl<R, V, B> WebhookServiceImpl<R, V, B> {
/// Create a webhook service that requires public HTTPS endpoints.
- pub fn new(repo: R, validation_client: V) -> Self {
+ pub fn new(repo: R, validation_client: V, event_broker: B) -> Self {
Self::new_with_endpoint_scheme_policy(
repo,
validation_client,
WebhookEndpointSchemePolicy::HttpsOnly,
+ event_broker,
)
}
@@ -43,11 +50,13 @@ impl<R, V> WebhookServiceImpl<R, V> {
repo: R,
validation_client: V,
endpoint_scheme_policy: WebhookEndpointSchemePolicy,
+ event_broker: B,
) -> Self {
Self {
repo,
validation_client,
endpoint_scheme_policy,
+ event_broker,
}
}
}
@@ -239,11 +248,18 @@ fn validation_response(
}
}
-impl<R, V> WebhookServiceImpl<R, V>
+impl<R, V, B> WebhookServiceImpl<R, V, B>
where
R: WebhookRepo,
V: WebhookValidationClient,
+ B: MacroEventBroker,
{
+ fn publish_webhook_event(&self, event: &WebhookMacroEvent) {
+ drop(self.event_broker.send_event(event).inspect_err(|error| {
+ tracing::error!(error=?error, "failed to schedule webhook lifecycle event");
+ }));
+ }
+
async fn workspace_id_for_scope(
&self,
caller: MacroUserIdStr<'static>,
@@ -306,10 +322,11 @@ where
}
}
-impl<R, V> WebhookService for WebhookServiceImpl<R, V>
+impl<R, V, B> WebhookService for WebhookServiceImpl<R, V, B>
where
R: WebhookRepo,
V: WebhookValidationClient,
+ B: MacroEventBroker + Clone,
{
async fn create_webhook(
&self,
@@ -321,6 +338,11 @@ where
let workspace_id = self
.workspace_id_for_scope(caller.clone(), request.scope)
.await?;
+ let created_by_user_id = caller.clone();
+ let mut header_names = request.headers.as_ref().map_or_else(Vec::new, |headers| {
+ headers.keys().cloned().collect::<Vec<_>>()
+ });
+ header_names.sort();
let signing_secret = generate_signing_secret(&caller);
let headers = serde_json::to_value(request.headers.clone().unwrap_or_default())
@@ -331,6 +353,23 @@ where
.await
.map_err(|err| WebhookError::Repo(err.into()))?;
webhook.is_valid = false;
+
+ self.publish_webhook_event(&WebhookMacroEvent::created(
+ webhook.id.clone(),
+ WebhookCreatedMetadata {
+ webhook_id: webhook.id.clone(),
+ workspace_id: webhook.workspace_id.clone(),
+ created_by_user_id,
+ name: webhook.name.clone(),
+ endpoint_url: webhook.endpoint_url.clone(),
+ status: webhook.status,
+ is_valid: webhook.is_valid,
+ filters: webhook.filters.clone(),
+ header_names,
+ created_at: webhook.created_at,
+ },
+ ));
+
Ok(webhook)
}
@@ -341,10 +380,17 @@ where
request: PatchWebhookRequest,
) -> Result<Webhook, WebhookError> {
let webhook = self
- .load_authorized_webhook(caller, webhook_id.clone())
+ .load_authorized_webhook(caller.clone(), webhook_id.clone())
.await?;
validate_patch_request(&request, self.endpoint_scheme_policy)?;
- let reset_validity = request.endpoint_url.is_some() || request.headers.is_some();
+
+ let requested_name = request.name.clone();
+ let requested_endpoint_url = request.endpoint_url.clone();
+ let requested_filters = request.filters.clone();
+ let headers_updated = request.headers.is_some();
+ let requested_status = request.status;
+ let previous_status = requested_status.map(|_| webhook.status);
+ let reset_validity = requested_endpoint_url.is_some() || headers_updated;
let patched = self
.repo
@@ -353,15 +399,34 @@ where
.map_err(|err| WebhookError::Repo(err.into()))?
.ok_or_else(|| WebhookError::NotFound("webhook not found".to_string()))?;
- if !reset_validity {
- return Ok(patched);
- }
+ let final_webhook = if reset_validity {
+ self.repo
+ .set_webhook_validity(patched.id, false)
+ .await
+ .map_err(|err| WebhookError::Repo(err.into()))?
+ .ok_or_else(|| WebhookError::NotFound("webhook not found".to_string()))?
+ } else {
+ patched
+ };
+
+ self.publish_webhook_event(&WebhookMacroEvent::updated(
+ final_webhook.id.clone(),
+ WebhookUpdatedMetadata {
+ webhook_id: final_webhook.id.clone(),
+ workspace_id: final_webhook.workspace_id.clone(),
+ actor_user_id: caller,
+ name: requested_name,
+ endpoint_url: requested_endpoint_url,
+ filters: requested_filters,
+ headers_updated,
+ status: requested_status,
+ previous_status,
+ is_valid: final_webhook.is_valid,
+ updated_at: final_webhook.updated_at,
+ },
+ ));
- self.repo
- .set_webhook_validity(patched.id, false)
- .await
- .map_err(|err| WebhookError::Repo(err.into()))?
- .ok_or_else(|| WebhookError::NotFound("webhook not found".to_string()))
+ Ok(final_webhook)
}
async fn validate_webhook(
@@ -370,21 +435,33 @@ where
webhook_id: WebhookId,
) -> Result<ValidateWebhookResponse, WebhookError> {
let webhook = self
- .load_authorized_webhook(caller, webhook_id.clone())
+ .load_authorized_webhook(caller.clone(), webhook_id.clone())
.await?;
+ let workspace_id = webhook.workspace_id.clone();
let result = self
.validation_client
.validate_webhook(webhook)
.await
.map_err(|err| WebhookError::Repo(err.into()))?;
- let is_valid = result.is_valid;
self.repo
- .set_webhook_validity(webhook_id.clone(), is_valid)
+ .set_webhook_validity(webhook_id.clone(), result.is_valid)
.await
.map_err(|err| WebhookError::Repo(err.into()))?
.ok_or_else(|| WebhookError::NotFound("webhook not found".to_string()))?;
+ self.publish_webhook_event(&WebhookMacroEvent::validated(
+ webhook_id.clone(),
+ WebhookValidatedMetadata {
+ webhook_id: webhook_id.clone(),
+ workspace_id,
+ actor_user_id: caller,
+ is_valid: result.is_valid,
+ response_status: result.response_status,
+ message: result.message.clone(),
+ },
+ ));
+
Ok(validation_response(webhook_id, result))
}
@@ -394,15 +471,24 @@ where
webhook_id: WebhookId,
) -> Result<(), WebhookError> {
let webhook = self
- .load_authorized_webhook(caller, webhook_id.clone())
+ .load_authorized_webhook(caller.clone(), webhook_id)
.await?;
self.repo
- .delete_webhook(webhook.id)
+ .delete_webhook(webhook.id.clone())
.await
.map_err(|err| WebhookError::Repo(err.into()))?
.ok_or_else(|| WebhookError::NotFound("webhook not found".to_string()))?;
+ self.publish_webhook_event(&WebhookMacroEvent::deleted(
+ webhook.id.clone(),
+ WebhookDeletedMetadata {
+ webhook_id: webhook.id,
+ workspace_id: webhook.workspace_id,
+ actor_user_id: caller,
+ },
+ ));
+
Ok(())
}
}
diff --git a/crates/webhook/src/domain/service_test.rs b/crates/webhook/src/domain/service_test.rs
index 617c1f22fc..43b0f6273e 100644
--- a/crates/webhook/src/domain/service_test.rs
+++ b/crates/webhook/src/domain/service_test.rs
@@ -8,8 +8,15 @@ use super::{
service::WebhookServiceImpl,
};
use chrono::Utc;
+use macro_event_broker::{
+ EventBrokerError, MacroEvent, MacroEventBroker, NoopMacroEventBroker, Topic as _,
+};
use macro_user_id::user_id::MacroUserIdStr;
-use std::{collections::BTreeMap, sync::Arc};
+use serde_json::json;
+use std::{
+ collections::BTreeMap,
+ sync::{Arc, Mutex as StdMutex},
+};
use tokio::sync::Mutex;
#[derive(Debug, Default)]
@@ -17,6 +24,14 @@ struct RepoState {
team_workspace_id: Option<String>,
webhook: Option<Webhook>,
validation_updates: Vec<bool>,
+ create_fails: bool,
+ patch_fails: bool,
+ patch_returns_missing: bool,
+ delete_fails: bool,
+ delete_returns_missing: bool,
+ validity_update_fails: bool,
+ validity_update_returns_missing: bool,
+ team_lookup_fails: bool,
}
#[derive(Clone, Default)]
@@ -33,6 +48,18 @@ impl FakeRepo {
drop(state);
repo
}
+
+ async fn failing_create() -> Self {
+ let repo = Self::default();
+ repo.state.lock().await.create_fails = true;
+ repo
+ }
+
+ async fn failing_team_lookup() -> Self {
+ let repo = Self::default();
+ repo.state.lock().await.team_lookup_fails = true;
+ repo
+ }
}
fn filter_matches_event(filter: &WebhookFilter, event: &str, entity_id: &str) -> bool {
@@ -57,6 +84,10 @@ impl WebhookRepo for FakeRepo {
_signing_secret: String,
_headers: serde_json::Value,
) -> Result<Webhook, Self::Err> {
+ if self.state.lock().await.create_fails {
+ anyhow::bail!("create failed");
+ }
+
let webhook = webhook_from_create(created_by_user_id, workspace_id, request);
self.state.lock().await.webhook = Some(webhook.clone());
Ok(webhook)
@@ -107,6 +138,13 @@ impl WebhookRepo for FakeRepo {
request: PatchWebhookRequest,
) -> Result<Option<Webhook>, Self::Err> {
let mut state = self.state.lock().await;
+ if state.patch_fails {
+ anyhow::bail!("patch failed");
+ }
+ if state.patch_returns_missing {
+ return Ok(None);
+ }
+
let Some(webhook) = state
.webhook
.as_mut()
@@ -129,11 +167,19 @@ impl WebhookRepo for FakeRepo {
if let Some(status) = request.status {
webhook.status = status;
}
+ webhook.updated_at += chrono::Duration::seconds(1);
Ok(Some(webhook.clone()))
}
async fn delete_webhook(&self, webhook_id: String) -> Result<Option<Webhook>, Self::Err> {
let mut state = self.state.lock().await;
+ if state.delete_fails {
+ anyhow::bail!("delete failed");
+ }
+ if state.delete_returns_missing {
+ return Ok(None);
+ }
+
let webhook = state
.webhook
.as_mut()
@@ -151,7 +197,15 @@ impl WebhookRepo for FakeRepo {
is_valid: bool,
) -> Result<Option<Webhook>, Self::Err> {
let mut state = self.state.lock().await;
+ if state.validity_update_fails {
+ anyhow::bail!("validity update failed");
+ }
+
state.validation_updates.push(is_valid);
+ if state.validity_update_returns_missing {
+ return Ok(None);
+ }
+
let Some(webhook) = state
.webhook
.as_mut()
@@ -160,6 +214,7 @@ impl WebhookRepo for FakeRepo {
return Ok(None);
};
webhook.is_valid = is_valid;
+ webhook.updated_at += chrono::Duration::seconds(1);
Ok(Some(webhook.clone()))
}
@@ -167,7 +222,55 @@ impl WebhookRepo for FakeRepo {
&self,
_user_id: MacroUserIdStr<'static>,
) -> Result<Option<String>, Self::Err> {
- Ok(self.state.lock().await.team_workspace_id.clone())
+ let state = self.state.lock().await;
+ if state.team_lookup_fails {
+ anyhow::bail!("team lookup failed");
+ }
+ Ok(state.team_workspace_id.clone())
+ }
+}
+
+#[derive(Clone, Debug)]
+struct PublishedEvent {
+ topic: &'static str,
+ key: String,
+ payload: serde_json::Value,
+}
+
+#[derive(Clone, Default)]
+struct TestEventBroker {
+ published: Arc<StdMutex<Vec<PublishedEvent>>>,
+ fail: bool,
+}
+
+impl TestEventBroker {
+ fn failing() -> Self {
+ Self {
+ fail: true,
+ ..Self::default()
+ }
+ }
+
+ fn published(&self) -> Arc<StdMutex<Vec<PublishedEvent>>> {
+ Arc::clone(&self.published)
+ }
+}
+
+impl MacroEventBroker for TestEventBroker {
+ fn send_event<E: MacroEvent + ?Sized>(
+ &self,
+ event: &E,
+ ) -> Result<tokio::task::JoinHandle<Result<(), EventBrokerError>>, EventBrokerError> {
+ if self.fail {
+ return Err(EventBrokerError::Publish("test failure".to_string()));
+ }
+
+ self.published.lock().unwrap().push(PublishedEvent {
+ topic: event.topic().as_str(),
+ key: event.key().to_string(),
+ payload: serde_json::to_value(event.event())?,
+ });
+ Ok(tokio::spawn(async { Ok(()) }))
}
}
@@ -175,6 +278,7 @@ impl WebhookRepo for FakeRepo {
struct FakeValidationClient {
result: WebhookValidationResult,
calls: Arc<Mutex<usize>>,
+ transport_fails: bool,
}
impl FakeValidationClient {
@@ -194,10 +298,17 @@ impl FakeValidationClient {
})
}
+ fn transport_failure() -> Self {
+ let mut client = Self::succeeds();
+ client.transport_fails = true;
+ client
+ }
+
fn new(result: WebhookValidationResult) -> Self {
Self {
result,
calls: Arc::new(Mutex::new(0)),
+ transport_fails: false,
}
}
@@ -220,6 +331,9 @@ impl WebhookValidationClient for FakeValidationClient {
_webhook: Webhook,
) -> Result<WebhookValidationResult, Self::Err> {
*self.calls.lock().await += 1;
+ if self.transport_fails {
+ anyhow::bail!("validation transport failed");
+ }
Ok(self.result.clone())
}
}
@@ -290,22 +404,83 @@ fn assert_bad_request<T>(result: Result<T, WebhookError>) {
}
#[tokio::test]
-async fn create_succeeds_for_user_scope() {
- let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+async fn create_publishes_full_sanitized_snapshot() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ event_broker,
+ );
+ let mut request = create_request();
+ request.headers = Some(BTreeMap::from([
+ ("Z-Private".to_string(), "z-header-value".to_string()),
+ ("A-Private".to_string(), "a-header-value".to_string()),
+ ]));
let webhook = service
- .create_webhook(caller(), create_request())
+ .create_webhook(caller(), request)
.await
- .unwrap();
+ .expect("webhook should be created");
assert_eq!(webhook.id, "wh_test");
assert!(!webhook.is_valid);
+
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, webhook.id);
+ assert_eq!(event.payload["schema_version"], 1);
+ assert_eq!(event.payload["event_type"], "webhook.created");
+ assert!(event.payload["event_id"].is_string());
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": webhook.id,
+ "workspace_id": webhook.workspace_id,
+ "created_by_user_id": caller().as_ref(),
+ "name": webhook.name,
+ "endpoint_url": webhook.endpoint_url,
+ "status": webhook.status,
+ "is_valid": false,
+ "filters": webhook.filters,
+ "header_names": ["A-Private", "Z-Private"],
+ "created_at": webhook.created_at,
+ })
+ );
+
+ let serialized = event.payload.to_string();
+ assert!(!serialized.contains("signing_secret"));
+ assert!(!serialized.contains("a-header-value"));
+ assert!(!serialized.contains("z-header-value"));
+}
+
+#[tokio::test]
+async fn create_request_validation_failure_publishes_nothing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ event_broker,
+ );
+ let mut request = create_request();
+ request.name = " ".to_string();
+
+ assert_bad_request(service.create_webhook(caller(), request).await);
+ assert!(published.lock().unwrap().is_empty());
}
#[tokio::test]
async fn create_fails_bad_request_when_team_scope_user_has_no_team() {
- let service = WebhookServiceImpl::new(FakeRepo::default(), FakeValidationClient::default());
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ event_broker,
+ );
let mut request = create_request();
request.scope = WebhookScope::Team;
@@ -315,38 +490,318 @@ async fn create_fails_bad_request_when_team_scope_user_has_no_team() {
result,
Err(WebhookError::BadRequest(message)) if message == "team scope requires the user to belong to a team"
));
+ assert!(published.lock().unwrap().is_empty());
}
#[tokio::test]
-async fn patch_fails_not_found_for_missing_id() {
- let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+async fn create_team_scope_repository_failure_publishes_nothing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::failing_team_lookup().await,
+ FakeValidationClient::default(),
+ event_broker,
+ );
+ let mut request = create_request();
+ request.scope = WebhookScope::Team;
+
+ let result = service.create_webhook(caller(), request).await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn create_repository_failure_publishes_nothing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::failing_create().await,
+ FakeValidationClient::default(),
+ event_broker,
+ );
+
+ let result = service.create_webhook(caller(), create_request()).await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn create_succeeds_when_event_scheduling_fails() {
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ TestEventBroker::failing(),
+ );
+
+ let webhook = service
+ .create_webhook(caller(), create_request())
+ .await
+ .expect("event scheduling must not fail webhook creation");
+
+ assert_eq!(webhook.id, "wh_test");
+ assert!(!webhook.is_valid);
+}
+
+#[tokio::test]
+async fn patch_publishes_requested_fields_and_status_transition() {
+ let webhook = existing_webhook();
+ let unchanged_name = webhook.name.clone();
+ let repo = FakeRepo::with_webhook(webhook, None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+ let request = PatchWebhookRequest {
+ name: Some(unchanged_name.clone()),
+ endpoint_url: None,
+ headers: None,
+ filters: None,
+ status: Some(WebhookStatus::Paused),
+ };
+
+ let patched = service
+ .patch_webhook(caller(), "wh_test".to_string(), request)
+ .await
+ .expect("webhook should be patched");
+
+ assert_eq!(patched.name, unchanged_name);
+ assert_eq!(patched.status, WebhookStatus::Paused);
+
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, patched.id);
+ assert_eq!(event.payload["schema_version"], 1);
+ assert_eq!(event.payload["event_type"], "webhook.updated");
+ assert!(event.payload["event_id"].is_string());
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": patched.id,
+ "workspace_id": patched.workspace_id,
+ "actor_user_id": caller().as_ref(),
+ "name": unchanged_name,
+ "endpoint_url": null,
+ "filters": null,
+ "headers_updated": false,
+ "status": "paused",
+ "previous_status": "active",
+ "is_valid": true,
+ "updated_at": patched.updated_at,
+ })
+ );
+}
+
+#[tokio::test]
+async fn patch_endpoint_and_headers_publish_final_validity_row_without_secrets() {
+ let mut webhook = existing_webhook();
+ webhook.signing_secret = "stored-signing-secret".to_string();
+ let initial_updated_at = webhook.updated_at;
+ let repo = FakeRepo::with_webhook(webhook, None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service =
+ WebhookServiceImpl::new(repo.clone(), FakeValidationClient::default(), event_broker);
+ let replacement_filters = vec![WebhookFilter {
+ events: vec!["webhook.updated".to_string()],
+ ids: Some(vec!["wh_target".to_string()]),
+ }];
+ let request = PatchWebhookRequest {
+ name: None,
+ endpoint_url: Some("https://example.com/replacement".to_string()),
+ headers: Some(BTreeMap::from([(
+ "X-Private".to_string(),
+ "replacement-header-value".to_string(),
+ )])),
+ filters: Some(replacement_filters.clone()),
+ status: None,
+ };
+
+ let patched = service
+ .patch_webhook(caller(), "wh_test".to_string(), request)
+ .await
+ .expect("webhook should be patched and invalidated");
+
+ assert!(!patched.is_valid);
+ assert_eq!(
+ patched.updated_at,
+ initial_updated_at + chrono::Duration::seconds(2)
+ );
+ assert_eq!(repo.state.lock().await.validation_updates, vec![false]);
+
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, patched.id);
+ assert_eq!(event.payload["event_type"], "webhook.updated");
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": patched.id,
+ "workspace_id": patched.workspace_id,
+ "actor_user_id": caller().as_ref(),
+ "name": null,
+ "endpoint_url": "https://example.com/replacement",
+ "filters": replacement_filters,
+ "headers_updated": true,
+ "status": null,
+ "previous_status": null,
+ "is_valid": false,
+ "updated_at": patched.updated_at,
+ })
+ );
+
+ let serialized = event.payload.to_string();
+ assert!(!serialized.contains("signing_secret"));
+ assert!(!serialized.contains("stored-signing-secret"));
+ assert!(!serialized.contains("replacement-header-value"));
+}
+
+#[tokio::test]
+async fn patch_fails_not_found_for_missing_id_without_publishing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ event_broker,
+ );
let result = service
.patch_webhook(caller(), "wh_missing".to_string(), patch_request())
.await;
assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
}
#[tokio::test]
-async fn patch_fails_unauthorized_when_caller_does_not_own_workspace() {
+async fn patch_fails_unauthorized_without_publishing() {
let mut webhook = existing_webhook();
webhook.workspace_id = "other_workspace".to_string();
let repo = FakeRepo::with_webhook(webhook, None).await;
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
let result = service
.patch_webhook(caller(), "wh_test".to_string(), patch_request())
.await;
assert!(matches!(result, Err(WebhookError::Unauthorized)));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_missing_row_after_authorization_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.patch_returns_missing = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+
+ let result = service
+ .patch_webhook(caller(), "wh_test".to_string(), patch_request())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_repository_failure_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.patch_fails = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+
+ let result = service
+ .patch_webhook(caller(), "wh_test".to_string(), patch_request())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_validity_reset_failure_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.validity_update_fails = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+ let mut request = patch_request();
+ request.endpoint_url = Some("https://example.com/replacement".to_string());
+
+ let result = service
+ .patch_webhook(caller(), "wh_test".to_string(), request)
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_missing_validity_reset_row_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.validity_update_returns_missing = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+ let mut request = patch_request();
+ request.headers = Some(BTreeMap::new());
+
+ let result = service
+ .patch_webhook(caller(), "wh_test".to_string(), request)
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_request_validation_failure_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+ let mut request = patch_request();
+ request.endpoint_url = Some("http://example.com/replacement".to_string());
+
+ assert_bad_request(
+ service
+ .patch_webhook(caller(), "wh_test".to_string(), request)
+ .await,
+ );
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn patch_succeeds_when_event_scheduling_fails() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ let service = WebhookServiceImpl::new(
+ repo,
+ FakeValidationClient::default(),
+ TestEventBroker::failing(),
+ );
+
+ let patched = service
+ .patch_webhook(caller(), "wh_test".to_string(), patch_request())
+ .await
+ .expect("event scheduling must not fail webhook patching");
+
+ assert_eq!(patched.name, "Updated");
}
#[tokio::test]
async fn invalid_http_endpoint_is_rejected() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let mut request = create_request();
request.endpoint_url = "http://example.com/webhook".to_string();
@@ -359,6 +814,7 @@ async fn local_addresses_are_allowed_when_policy_permits_them() {
FakeRepo::default(),
FakeValidationClient::default(),
WebhookEndpointSchemePolicy::HttpAndHttps,
+ NoopMacroEventBroker,
);
for endpoint_url in [
@@ -383,7 +839,8 @@ async fn local_addresses_are_allowed_when_policy_permits_them() {
#[tokio::test]
async fn private_and_link_local_endpoints_are_rejected() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
for endpoint_url in [
"https://localhost/webhook",
@@ -403,7 +860,8 @@ async fn private_and_link_local_endpoints_are_rejected() {
#[tokio::test]
async fn empty_filters_list_is_rejected() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let mut request = create_request();
request.filters = Vec::new();
@@ -413,7 +871,8 @@ async fn empty_filters_list_is_rejected() {
#[tokio::test]
async fn filter_with_empty_events_is_rejected() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let mut request = create_request();
request.filters = vec![WebhookFilter {
events: Vec::new(),
@@ -426,7 +885,8 @@ async fn filter_with_empty_events_is_rejected() {
#[tokio::test]
async fn filter_with_empty_ids_is_rejected() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let mut request = create_request();
request.filters = vec![WebhookFilter {
events: vec!["file.created".to_string()],
@@ -439,7 +899,8 @@ async fn filter_with_empty_ids_is_rejected() {
#[tokio::test]
async fn filter_without_ids_is_accepted() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let webhook = service
.create_webhook(caller(), create_request())
@@ -452,7 +913,8 @@ async fn filter_without_ids_is_accepted() {
#[tokio::test]
async fn filter_with_valid_ids_is_accepted() {
let repo = FakeRepo::default();
- let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let service =
+ WebhookServiceImpl::new(repo, FakeValidationClient::default(), NoopMacroEventBroker);
let mut request = create_request();
request.filters = vec![WebhookFilter {
events: vec!["file.created".to_string()],
@@ -466,18 +928,110 @@ async fn filter_with_valid_ids_is_accepted() {
}
#[tokio::test]
-async fn validate_succeeds_and_sets_webhook_validity() {
- let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
- let service = WebhookServiceImpl::new(repo.clone(), FakeValidationClient::succeeds());
+async fn validate_publishes_success_when_persisted_validity_is_unchanged() {
+ let mut webhook = existing_webhook();
+ webhook.workspace_id = "team_workspace".to_string();
+ let repo = FakeRepo::with_webhook(webhook, Some("team_workspace".to_string())).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service =
+ WebhookServiceImpl::new(repo.clone(), FakeValidationClient::succeeds(), event_broker);
let response = service
.validate_webhook(caller(), "wh_test".to_string())
.await
- .unwrap();
+ .expect("webhook validation should succeed");
assert!(response.is_valid);
assert_eq!(response.response_status, Some(204));
assert_eq!(repo.state.lock().await.validation_updates, vec![true]);
+
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, "wh_test");
+ assert_eq!(event.payload["schema_version"], 1);
+ assert_eq!(event.payload["event_type"], "webhook.validated");
+ assert!(event.payload["event_id"].is_string());
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": "wh_test",
+ "workspace_id": "team_workspace",
+ "actor_user_id": caller().as_ref(),
+ "is_valid": true,
+ "response_status": 204,
+ "message": null,
+ })
+ );
+}
+
+#[tokio::test]
+async fn validate_http_failure_publishes_sanitized_invalid_result() {
+ let mut webhook = existing_webhook();
+ webhook.signing_secret = "stored-signing-secret".to_string();
+ webhook.headers =
+ BTreeMap::from([("X-Private".to_string(), "stored-header-value".to_string())]);
+ let repo = FakeRepo::with_webhook(webhook, None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service =
+ WebhookServiceImpl::new(repo.clone(), FakeValidationClient::fails(), event_broker);
+
+ let ValidateWebhookResponse {
+ is_valid,
+ response_status,
+ message,
+ ..
+ } = service
+ .validate_webhook(caller(), "wh_test".to_string())
+ .await
+ .expect("an HTTP failure is a completed validation");
+
+ assert!(!is_valid);
+ assert_eq!(response_status, Some(500));
+ assert_eq!(message.as_deref(), Some("webhook returned HTTP 500"));
+ assert_eq!(repo.state.lock().await.validation_updates, vec![false]);
+
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, "wh_test");
+ assert_eq!(event.payload["event_type"], "webhook.validated");
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": "wh_test",
+ "workspace_id": caller().as_ref(),
+ "actor_user_id": caller().as_ref(),
+ "is_valid": false,
+ "response_status": 500,
+ "message": "webhook returned HTTP 500",
+ })
+ );
+ let serialized = event.payload.to_string();
+ assert!(!serialized.contains("signing_secret"));
+ assert!(!serialized.contains("stored-signing-secret"));
+ assert!(!serialized.contains("stored-header-value"));
+}
+
+#[tokio::test]
+async fn validate_missing_webhook_publishes_nothing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let validation_client = FakeValidationClient::succeeds();
+ let service =
+ WebhookServiceImpl::new(FakeRepo::default(), validation_client.clone(), event_broker);
+
+ let result = service
+ .validate_webhook(caller(), "wh_missing".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert_eq!(validation_client.call_count().await, 0);
+ assert!(published.lock().unwrap().is_empty());
}
#[tokio::test]
@@ -486,7 +1040,9 @@ async fn validate_fails_unauthorized_before_calling_validation_client() {
webhook.workspace_id = "other_workspace".to_string();
let repo = FakeRepo::with_webhook(webhook, None).await;
let validation_client = FakeValidationClient::succeeds();
- let service = WebhookServiceImpl::new(repo, validation_client.clone());
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, validation_client.clone(), event_broker);
let result = service
.validate_webhook(caller(), "wh_test".to_string())
@@ -494,25 +1050,198 @@ async fn validate_fails_unauthorized_before_calling_validation_client() {
assert!(matches!(result, Err(WebhookError::Unauthorized)));
assert_eq!(validation_client.call_count().await, 0);
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn validate_transport_failure_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ repo.clone(),
+ FakeValidationClient::transport_failure(),
+ event_broker,
+ );
+
+ let result = service
+ .validate_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(repo.state.lock().await.validation_updates.is_empty());
+ assert!(published.lock().unwrap().is_empty());
}
#[tokio::test]
-async fn validate_records_invalid_and_returns_sanitized_failure() {
+async fn validate_validity_persistence_failure_publishes_nothing() {
let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
- let service = WebhookServiceImpl::new(repo.clone(), FakeValidationClient::fails());
+ repo.state.lock().await.validity_update_fails = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::succeeds(), event_broker);
- let ValidateWebhookResponse {
- is_valid,
- response_status,
- message,
- ..
- } = service
+ let result = service
+ .validate_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn validate_missing_validity_row_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.validity_update_returns_missing = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::succeeds(), event_broker);
+
+ let result = service
+ .validate_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn validate_succeeds_when_event_scheduling_fails() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ let service = WebhookServiceImpl::new(
+ repo,
+ FakeValidationClient::fails(),
+ TestEventBroker::failing(),
+ );
+
+ let response = service
.validate_webhook(caller(), "wh_test".to_string())
.await
- .unwrap();
+ .expect("event scheduling must not fail webhook validation");
- assert!(!is_valid);
- assert_eq!(response_status, Some(500));
- assert_eq!(message.as_deref(), Some("webhook returned HTTP 500"));
- assert_eq!(repo.state.lock().await.validation_updates, vec![false]);
+ assert!(!response.is_valid);
+ assert_eq!(response.response_status, Some(500));
+}
+
+#[tokio::test]
+async fn delete_publishes_event_after_soft_deletion() {
+ let mut webhook = existing_webhook();
+ webhook.workspace_id = "team_workspace".to_string();
+ let repo = FakeRepo::with_webhook(webhook, Some("team_workspace".to_string())).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service =
+ WebhookServiceImpl::new(repo.clone(), FakeValidationClient::default(), event_broker);
+
+ service
+ .delete_webhook(caller(), "wh_test".to_string())
+ .await
+ .expect("webhook should be deleted");
+
+ assert!(
+ repo.state
+ .lock()
+ .await
+ .webhook
+ .as_ref()
+ .is_some_and(|webhook| webhook.deleted_at.is_some())
+ );
+ let published = published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ let event = &published[0];
+ assert_eq!(event.topic, "macro.webhooks");
+ assert_eq!(event.key, "wh_test");
+ assert_eq!(event.payload["schema_version"], 1);
+ assert_eq!(event.payload["event_type"], "webhook.deleted");
+ assert!(event.payload["event_id"].is_string());
+ assert_eq!(
+ event.payload["metadata"],
+ json!({
+ "webhook_id": "wh_test",
+ "workspace_id": "team_workspace",
+ "actor_user_id": caller().as_ref(),
+ })
+ );
+}
+
+#[tokio::test]
+async fn delete_missing_webhook_publishes_nothing() {
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(
+ FakeRepo::default(),
+ FakeValidationClient::default(),
+ event_broker,
+ );
+
+ let result = service
+ .delete_webhook(caller(), "wh_missing".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn delete_unauthorized_webhook_publishes_nothing() {
+ let mut webhook = existing_webhook();
+ webhook.workspace_id = "other_workspace".to_string();
+ let repo = FakeRepo::with_webhook(webhook, None).await;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+
+ let result = service
+ .delete_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Unauthorized)));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn delete_repository_failure_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.delete_fails = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+
+ let result = service
+ .delete_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::Repo(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn delete_missing_row_after_authorization_publishes_nothing() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ repo.state.lock().await.delete_returns_missing = true;
+ let event_broker = TestEventBroker::default();
+ let published = event_broker.published();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default(), event_broker);
+
+ let result = service
+ .delete_webhook(caller(), "wh_test".to_string())
+ .await;
+
+ assert!(matches!(result, Err(WebhookError::NotFound(_))));
+ assert!(published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn delete_succeeds_when_event_scheduling_fails() {
+ let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
+ let service = WebhookServiceImpl::new(
+ repo,
+ FakeValidationClient::default(),
+ TestEventBroker::failing(),
+ );
+
+ service
+ .delete_webhook(caller(), "wh_test".to_string())
+ .await
+ .expect("event scheduling must not fail webhook deletion");
}
diff --git a/crates/webhook/src/inbound/kafka_consumer.rs b/crates/webhook/src/inbound/kafka_consumer.rs
index c60eb65370..c656668031 100644
--- a/crates/webhook/src/inbound/kafka_consumer.rs
+++ b/crates/webhook/src/inbound/kafka_consumer.rs
@@ -1,7 +1,8 @@
//! Kafka consumer that feeds broker events into webhook ingestion.
//!
-//! Subscribes to [`MacroDocumentsTopic`] and [`MacroChannelsTopic`] and hands
-//! every decoded event envelope to a [`WebhookEventIngestionService`].
+//! Subscribes to [`MacroDocumentsTopic`], [`MacroChannelsTopic`], and
+//! [`MacroWebhooksTopic`] and hands every decoded event envelope to a
+//! [`WebhookEventIngestionService`].
//!
//! Delivery is at-least-once: an event's offset is committed only after the
//! ingestion service accepted it or permanently rejected it. Transient
@@ -20,14 +21,14 @@
#[cfg(test)]
mod test;
-use crate::domain::ingestion::WebhookEventIngestionService;
+use crate::domain::{events::WebhookTopicEvent, ingestion::WebhookEventIngestionService};
use anyhow::Context as _;
use channels::domain::broker_events::ChannelTopicEvent;
use documents::domain::events::DocumentTopicEvent;
use macro_env::Environment;
use macro_event_broker::outbound::msk_iam::configure_sasl_iam;
use macro_event_broker::{Event, EventBrokerError, MskIamClientContext, Topic as _};
-use macro_event_topics::{MacroChannelsTopic, MacroDocumentsTopic};
+use macro_event_topics::{MacroChannelsTopic, MacroDocumentsTopic, MacroWebhooksTopic};
use rdkafka::ClientConfig;
use rdkafka::consumer::{CommitMode, Consumer, StreamConsumer};
use rdkafka::error::KafkaResult;
@@ -49,6 +50,14 @@ const MAX_INGEST_ATTEMPTS: u32 = 5;
/// consumer from its group.
const INGEST_RETRY_BASE_DELAY: Duration = Duration::from_secs(1);
+fn subscribed_topics() -> [&'static str; 3] {
+ [
+ MacroDocumentsTopic.as_str(),
+ MacroChannelsTopic.as_str(),
+ MacroWebhooksTopic.as_str(),
+ ]
+}
+
/// One decoded event envelope from a topic this consumer subscribes to.
///
/// The Kafka message key is not carried: every event's metadata already
@@ -59,6 +68,8 @@ pub enum WebhookConsumerEvent {
Documents(Event<DocumentTopicEvent>),
/// Event received on [`MacroChannelsTopic`].
Channels(Event<ChannelTopicEvent>),
+ /// Event received on [`MacroWebhooksTopic`].
+ Webhooks(Event<WebhookTopicEvent>),
}
impl WebhookConsumerEvent {
@@ -71,6 +82,9 @@ impl WebhookConsumerEvent {
topic if topic == MacroChannelsTopic.as_str() => {
Ok(Self::Channels(Event::decode(payload)?))
}
+ topic if topic == MacroWebhooksTopic.as_str() => {
+ Ok(Self::Webhooks(Event::decode(payload)?))
+ }
unknown => Err(EventBrokerError::UnknownTopic(unknown.to_string())),
}
}
@@ -117,7 +131,7 @@ impl WebhookKafkaConsumer {
}
fn subscribe(&self) -> KafkaResult<()> {
- let topics = [MacroDocumentsTopic.as_str(), MacroChannelsTopic.as_str()];
+ let topics = subscribed_topics();
match self {
Self::Plaintext(consumer) => consumer.subscribe(&topics),
Self::MskIam(consumer) => consumer.subscribe(&topics),
@@ -181,6 +195,9 @@ async fn ingest_with_retry<S: WebhookEventIngestionService>(
WebhookConsumerEvent::Channels(event) => {
service.ingest_channel_event(event.clone()).await
}
+ WebhookConsumerEvent::Webhooks(event) => {
+ service.ingest_webhook_event(event.clone()).await
+ }
};
match result {
Ok(()) => {
@@ -224,9 +241,10 @@ async fn ingest_with_retry<S: WebhookEventIngestionService>(
/// Run the webhook event consumer until `shutdown` resolves.
///
-/// Connects to `brokers`, subscribes to [`MacroDocumentsTopic`] and
-/// [`MacroChannelsTopic`] under the `webhook-event-ingestion` consumer group,
-/// and feeds every decoded event to `service`, committing each offset only
+/// Connects to `brokers` and subscribes to [`MacroDocumentsTopic`],
+/// [`MacroChannelsTopic`], and [`MacroWebhooksTopic`] under the
+/// `webhook-event-ingestion` consumer group. Every decoded event is fed to
+/// `service`, committing each offset only
/// after ingestion succeeds (see `ingest_with_retry` for the retry policy).
/// Returns an error when the consumer cannot be created or subscribed, or when
/// a transient ingestion failure exhausts its in-process retries; callers
@@ -246,7 +264,7 @@ where
.subscribe()
.context("failed to subscribe to webhook event topics")?;
tracing::info!(
- topics = ?[MacroDocumentsTopic.as_str(), MacroChannelsTopic.as_str()],
+ topics = ?subscribed_topics(),
group = GROUP_ID,
"webhook event consumer listening"
);
diff --git a/crates/webhook/src/inbound/kafka_consumer/test.rs b/crates/webhook/src/inbound/kafka_consumer/test.rs
index acc0274c64..861f11315c 100644
--- a/crates/webhook/src/inbound/kafka_consumer/test.rs
+++ b/crates/webhook/src/inbound/kafka_consumer/test.rs
@@ -1,9 +1,12 @@
use super::*;
-use crate::domain::ingestion::WebhookEventIngestionError;
+use crate::domain::{
+ events::{WebhookDeletedMetadata, WebhookTopicEvent},
+ ingestion::WebhookEventIngestionError,
+};
use channel_sender::ChannelSender;
use channels::domain::broker_events::ChannelDeletedMetadata;
use documents::domain::events::DocumentDeletedMetadata;
-use entity_access::domain::models::AccessError;
+use macro_user_id::user_id::MacroUserIdStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use uuid::Uuid;
@@ -16,6 +19,23 @@ fn document_event() -> Event<DocumentTopicEvent> {
}))
}
+fn webhook_event() -> Event<WebhookTopicEvent> {
+ Event::new(WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
+ webhook_id: "wh_1".to_string(),
+ workspace_id: "macro|[email protected]".to_string(),
+ actor_user_id: MacroUserIdStr::try_from("macro|[email protected]".to_string())
+ .expect("valid user id"),
+ }))
+}
+
+#[test]
+fn subscribes_to_all_ingestion_topics() {
+ assert_eq!(
+ subscribed_topics(),
+ ["macro.documents", "macro.channels", "macro.webhooks"]
+ );
+}
+
#[test]
fn decodes_document_events() {
let event = document_event();
@@ -26,7 +46,9 @@ fn decodes_document_events() {
match decoded {
WebhookConsumerEvent::Documents(decoded) => assert_eq!(decoded, event),
- WebhookConsumerEvent::Channels(_) => panic!("decoded into the wrong topic variant"),
+ WebhookConsumerEvent::Channels(_) | WebhookConsumerEvent::Webhooks(_) => {
+ panic!("decoded into the wrong topic variant")
+ }
}
}
@@ -44,7 +66,25 @@ fn decodes_channel_events() {
match decoded {
WebhookConsumerEvent::Channels(decoded) => assert_eq!(decoded, event),
- WebhookConsumerEvent::Documents(_) => panic!("decoded into the wrong topic variant"),
+ WebhookConsumerEvent::Documents(_) | WebhookConsumerEvent::Webhooks(_) => {
+ panic!("decoded into the wrong topic variant")
+ }
+ }
+}
+
+#[test]
+fn decodes_webhook_events() {
+ let event = webhook_event();
+ let payload = serde_json::to_vec(&event).expect("serializable");
+
+ let decoded =
+ WebhookConsumerEvent::decode(MacroWebhooksTopic.as_str(), &payload).expect("decodable");
+
+ match decoded {
+ WebhookConsumerEvent::Webhooks(decoded) => assert_eq!(decoded, event),
+ WebhookConsumerEvent::Documents(_) | WebhookConsumerEvent::Channels(_) => {
+ panic!("decoded into the wrong topic variant")
+ }
}
}
@@ -55,8 +95,8 @@ fn rejects_unknown_topics() {
}
#[test]
-fn rejects_malformed_payloads() {
- let err = WebhookConsumerEvent::decode(MacroDocumentsTopic.as_str(), b"not json")
+fn rejects_malformed_webhook_payloads() {
+ let err = WebhookConsumerEvent::decode(MacroWebhooksTopic.as_str(), b"not json")
.expect_err("malformed payload");
assert!(matches!(err, EventBrokerError::Serialization(_)));
}
@@ -74,16 +114,14 @@ impl FlakyIngestionService {
if self.transient {
WebhookEventIngestionError::Enqueue(anyhow::anyhow!("queue unavailable"))
} else {
- WebhookEventIngestionError::EntityAccess(AccessError::Unauthorized)
+ WebhookEventIngestionError::InvalidEntityId {
+ entity_type: "webhook",
+ entity_id: "invalid".to_string(),
+ }
}
}
-}
-impl WebhookEventIngestionService for FlakyIngestionService {
- async fn ingest_document_event(
- &self,
- _event: Event<DocumentTopicEvent>,
- ) -> Result<(), WebhookEventIngestionError> {
+ fn ingest(&self) -> Result<(), WebhookEventIngestionError> {
let attempt = self.attempts.fetch_add(1, Ordering::SeqCst) + 1;
if attempt <= self.failures {
Err(self.failure())
@@ -91,6 +129,15 @@ impl WebhookEventIngestionService for FlakyIngestionService {
Ok(())
}
}
+}
+
+impl WebhookEventIngestionService for FlakyIngestionService {
+ async fn ingest_document_event(
+ &self,
+ _event: Event<DocumentTopicEvent>,
+ ) -> Result<(), WebhookEventIngestionError> {
+ Ok(())
+ }
async fn ingest_channel_event(
&self,
@@ -98,6 +145,13 @@ impl WebhookEventIngestionService for FlakyIngestionService {
) -> Result<(), WebhookEventIngestionError> {
Ok(())
}
+
+ async fn ingest_webhook_event(
+ &self,
+ _event: Event<WebhookTopicEvent>,
+ ) -> Result<(), WebhookEventIngestionError> {
+ self.ingest()
+ }
}
fn flaky_service(failures: u32, transient: bool) -> (FlakyIngestionService, Arc<AtomicU32>) {
@@ -112,12 +166,12 @@ fn flaky_service(failures: u32, transient: bool) -> (FlakyIngestionService, Arc<
// `start_paused` auto-advances the tokio clock through the backoff sleeps.
#[tokio::test(start_paused = true)]
-async fn retries_transient_failures_until_success() {
+async fn retries_transient_webhook_failures_until_success() {
let (service, attempts) = flaky_service(2, true);
ingest_with_retry(
&service,
- &WebhookConsumerEvent::Documents(document_event()),
+ &WebhookConsumerEvent::Webhooks(webhook_event()),
0,
0,
)
@@ -133,7 +187,7 @@ async fn exhausted_transient_retries_bubble_up_for_redelivery() {
ingest_with_retry(
&service,
- &WebhookConsumerEvent::Documents(document_event()),
+ &WebhookConsumerEvent::Webhooks(webhook_event()),
0,
0,
)
@@ -144,12 +198,12 @@ async fn exhausted_transient_retries_bubble_up_for_redelivery() {
}
#[tokio::test]
-async fn permanent_failures_are_dropped_without_retry() {
+async fn permanent_webhook_failures_are_commit_safe_without_retry() {
let (service, attempts) = flaky_service(u32::MAX, false);
ingest_with_retry(
&service,
- &WebhookConsumerEvent::Documents(document_event()),
+ &WebhookConsumerEvent::Webhooks(webhook_event()),
0,
0,
)
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 7cada04f73..0a32a441a6 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -385,8 +385,11 @@ pub(crate) type CalWebhookServiceType = CalWebhookServiceImpl<AnalyticsClientSin
pub(crate) type DssCalWebhookState = CalWebhookRouterState<CalWebhookServiceType>;
/// Type alias for the product webhook service.
-pub(crate) type DssWebhookService =
- WebhookServiceImpl<PgWebhookRepo, ReqwestWebhookValidationClient>;
+pub(crate) type DssWebhookService = WebhookServiceImpl<
+ PgWebhookRepo,
+ ReqwestWebhookValidationClient,
+ MacroEventBrokerService<KafkaEventPublisher>,
+>;
/// Type alias for the product webhook rate limiter.
pub(crate) type DssWebhookRateLimiter =
diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rs
index e3df7d4bd9..aba283e41d 100644
--- a/services/document_storage_service/src/main.rs
+++ b/services/document_storage_service/src/main.rs
@@ -673,6 +673,7 @@ async fn main() -> anyhow::Result<()> {
webhook_repository.clone(),
webhook_http_client.clone(),
webhook_endpoint_scheme_policy,
+ macro_event_broker.clone(),
);
let webhook_rate_limiter = RateLimitServiceImpl {
repo: RedisRateLimitAdapter {