use std::fmt;
use std::net::IpAddr;
use std::sync::Arc;
use adk_core::egress::EgressPolicy;
use adk_core::webhook::WebhookSecret;
use adk_core::webhook_delivery::{
DeadLetterSink, DeliveryReport, RetryPolicy, WebhookDelivery, WebhookEndpoint, WebhookMessage,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::Serialize;
use tokio::sync::Semaphore;
use super::{EventFilter, EventRecord, EventSink, KernelEvent};
use crate::error::{KernelError, Result};
#[derive(Serialize)]
struct Payload<'a> {
#[serde(rename = "type")]
kind: &'static str,
timestamp: DateTime<Utc>,
data: &'a EventRecord,
}
#[derive(Clone)]
pub struct WebhookSink {
endpoint: WebhookEndpoint,
origin: String,
filter: EventFilter,
delivery: WebhookDelivery,
in_flight: Arc<Semaphore>,
max_in_flight: usize,
}
impl fmt::Debug for WebhookSink {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WebhookSink")
.field("origin", &self.origin)
.field("filter", &self.filter)
.field("retry", &self.delivery.retry_policy())
.field("max_in_flight", &self.max_in_flight)
.finish_non_exhaustive()
}
}
impl WebhookSink {
pub const DEFAULT_MAX_IN_FLIGHT: usize = 1;
pub fn new(url: &str, secret: WebhookSecret) -> Result<Self> {
let parsed = check_url(url)?;
let delivery = WebhookDelivery::new()
.with_egress_policy(EgressPolicy::allow_private_networks());
Ok(Self {
endpoint: WebhookEndpoint::new(parsed.as_str(), secret),
origin: parsed.origin().ascii_serialization(),
filter: EventFilter::default(),
delivery,
in_flight: Arc::new(Semaphore::new(Self::DEFAULT_MAX_IN_FLIGHT)),
max_in_flight: Self::DEFAULT_MAX_IN_FLIGHT,
})
}
#[must_use]
pub fn with_filter(mut self, filter: EventFilter) -> Self {
self.filter = filter;
self
}
#[must_use]
pub fn with_retry_policy(mut self, retry: RetryPolicy) -> Self {
self.delivery = self.delivery.with_retry_policy(retry);
self
}
#[must_use]
pub fn with_dead_letter_sink(mut self, sink: Arc<dyn DeadLetterSink>) -> Self {
self.delivery = self.delivery.with_dead_letter_sink(sink);
self
}
#[must_use]
pub fn with_max_in_flight(mut self, max: usize) -> Self {
self.max_in_flight = max.max(1);
self.in_flight = Arc::new(Semaphore::new(self.max_in_flight));
self
}
pub fn filter(&self) -> &EventFilter {
&self.filter
}
pub fn origin(&self) -> &str {
&self.origin
}
}
#[async_trait]
impl EventSink for WebhookSink {
async fn emit(&self, event: &KernelEvent) -> Result<()> {
self.emit_record(&EventRecord { cursor: 0, at: Utc::now(), event: event.clone() }).await
}
async fn emit_record(&self, record: &EventRecord) -> Result<()> {
if !self.filter.matches(&record.event) {
return Ok(());
}
let kind = record.event.name();
let id = format!("evt_{}", uuid::Uuid::new_v4().simple());
let payload = Payload { kind, timestamp: record.at, data: record };
let message = WebhookMessage::json(id, &payload)?;
let permit = Arc::clone(&self.in_flight).acquire_owned().await.map_err(|_| {
KernelError::Config { reason: "the webhook sink's delivery limit is closed".into() }
})?;
let (delivery, endpoint, origin) =
(self.delivery.clone(), self.endpoint.clone(), self.origin.clone());
let cursor = record.cursor;
tokio::spawn(async move {
let report = delivery.deliver(&endpoint, &message).await;
drop(permit);
match report {
DeliveryReport::Delivered { attempts, status } => tracing::debug!(
webhook.id = %message.id,
webhook.origin = %origin,
event.kind = kind,
event.cursor = cursor,
attempts,
http.status = status,
"kernel event delivered"
),
DeliveryReport::DeadLettered(letter) => tracing::warn!(
webhook.id = %message.id,
webhook.origin = %origin,
event.kind = kind,
event.cursor = cursor,
attempts = letter.attempts,
http.status = letter.last_status,
"kernel event not delivered"
),
}
});
Ok(())
}
}
fn check_url(url: &str) -> Result<reqwest::Url> {
let invalid =
|reason: String| KernelError::Config { reason: format!("webhook sink: {reason}") };
let parsed = reqwest::Url::parse(url)
.map_err(|error| invalid(format!("the URL does not parse: {error}")))?;
if !parsed.username().is_empty() || parsed.password().is_some() {
return Err(invalid(
"the URL carries credentials; remove them, as the signature authenticates each \
delivery"
.into(),
));
}
match parsed.scheme() {
"https" => Ok(parsed),
"http" if is_loopback(&parsed) => Ok(parsed),
"http" => Err(invalid(format!(
"{} must use https; plain http is accepted on loopback only",
parsed.origin().ascii_serialization()
))),
other => Err(invalid(format!("the scheme `{other}` is not allowed; use https"))),
}
}
fn is_loopback(url: &reqwest::Url) -> bool {
let host = url.host_str().unwrap_or_default();
let host = host.trim_start_matches('[').trim_end_matches(']');
host == "localhost"
|| host.ends_with(".localhost")
|| host.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
}
#[cfg(test)]
mod tests {
use super::*;
fn secret() -> WebhookSecret {
WebhookSecret::from_base64("whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw").unwrap()
}
#[test]
fn urls_are_https_or_loopback_http_without_credentials() {
for url in [
"https://hooks.example.com/kernel",
"https://10.0.0.7/kernel",
"http://127.0.0.1:9000/kernel",
"http://[::1]:9000/kernel",
"http://localhost:9000/kernel",
"http://kernel.localhost/hooks",
] {
assert!(WebhookSink::new(url, secret()).is_ok(), "{url}");
}
for (url, reason) in [
("http://hooks.example.com/kernel", "must use https"),
("http://10.0.0.7/kernel", "must use https"),
("ftp://hooks.example.com/kernel", "scheme `ftp`"),
("https://user:pass@hooks.example.com/kernel", "carries credentials"),
("https://token@hooks.example.com/kernel", "carries credentials"),
("not a url", "does not parse"),
] {
let error = WebhookSink::new(url, secret()).unwrap_err().to_string();
assert!(error.contains(reason), "{url}: {error}");
assert!(!error.contains("pass") && !error.contains("/kernel"), "{url}: {error}");
}
}
#[test]
fn the_debug_form_names_the_origin_only() {
let sink = WebhookSink::new("https://hooks.example.com/T0K3N?key=s3cr3t", secret())
.unwrap()
.with_max_in_flight(0);
let debug = format!("{sink:?}");
assert!(debug.contains("https://hooks.example.com"), "{debug}");
for hidden in ["T0K3N", "s3cr3t", "MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw"] {
assert!(!debug.contains(hidden), "{debug}");
}
assert_eq!(sink.max_in_flight, 1, "at least one delivery runs");
assert_eq!(sink.origin(), "https://hooks.example.com");
}
}