use std::collections::BTreeMap;
use std::sync::Arc;
pub mod cleanup;
pub mod cursor;
pub mod ingress;
pub mod limits;
pub mod metadata;
pub mod policy;
pub mod pool;
pub mod provider;
pub mod routes;
pub mod store;
pub mod sweeper;
pub mod verify;
#[cfg(test)]
mod route_tests;
#[cfg(test)]
mod tests;
pub use policy::Policy;
pub use store::Store;
pub struct IngressGate {
pub reads: tokio::sync::Semaphore,
pub lookups: tokio::sync::Semaphore,
pub blocked_waiters: tokio::sync::Semaphore,
pub reads_per_endpoint: usize,
pub reading: std::sync::Mutex<std::collections::HashMap<String, usize>>,
pub database: tokio::sync::Semaphore,
pub read_timeout: std::time::Duration,
pub wait: std::time::Duration,
}
pub const INGRESS_READS: usize = 8;
pub const INGRESS_DATABASE: usize = 2;
pub const INGRESS_LOOKUPS: usize = 1;
pub const INGRESS_READS_PER_ENDPOINT: usize = 4;
pub const INGRESS_BLOCKED_WAITERS: usize = 2;
impl Default for IngressGate {
fn default() -> Self {
Self {
reads: tokio::sync::Semaphore::new(INGRESS_READS),
lookups: tokio::sync::Semaphore::new(INGRESS_LOOKUPS),
blocked_waiters: tokio::sync::Semaphore::new(INGRESS_BLOCKED_WAITERS),
reads_per_endpoint: INGRESS_READS_PER_ENDPOINT,
reading: Default::default(),
database: tokio::sync::Semaphore::new(INGRESS_DATABASE),
read_timeout: std::time::Duration::from_secs(10),
wait: std::time::Duration::from_secs(3),
}
}
}
pub const MAX_WAITERS: usize = 256;
pub const MAX_WAITERS_PER_CONSUMER: usize = 4;
pub struct Webhooks {
pub notifications_only: bool,
pub store: Arc<Store>,
shared_secrets: BTreeMap<String, Vec<u8>>,
pub gate: IngressGate,
pub waiting: std::sync::Mutex<std::collections::HashMap<String, usize>>,
pub limits: limits::Limits,
}
impl Webhooks {
#[cfg(test)]
pub fn new(store: Arc<Store>, shared_secrets: BTreeMap<String, Vec<u8>>) -> Self {
Self::with_limits(
store,
shared_secrets,
limits::Limits::from_config(&crate::config::WebhookConfig::default()),
)
}
pub fn with_limits(
store: Arc<Store>,
shared_secrets: BTreeMap<String, Vec<u8>>,
limits: limits::Limits,
) -> Self {
Self {
notifications_only: false,
store,
shared_secrets,
gate: IngressGate::default(),
waiting: Default::default(),
limits,
}
}
pub fn shared_secret(&self, platform: &str) -> Option<&[u8]> {
self.shared_secrets.get(platform).map(Vec::as_slice)
}
}
pub fn secret_variable(platform: &str) -> String {
format!(
"WEBHOOK_SECRET_{}",
platform.to_ascii_uppercase().replace('-', "_")
)
}
pub fn hook_secret_aad(hook_id: &str) -> Vec<u8> {
format!("webhook-hook-secret-v1:{hook_id}").into_bytes()
}
pub async fn start(
config: &crate::config::WebhookConfig,
database_url: &str,
security: &crate::security::Security,
catalog: &crate::catalog::Catalog,
) -> Result<Option<Arc<Webhooks>>, String> {
if !config.enabled {
return Ok(None);
}
let store = Arc::new(
Store::connect(
database_url,
security,
Policy::pilot(config.inbox_max_bytes),
)
.await?,
);
store.check_payload_mode(config.notifications_only).await?;
let mut secrets = BTreeMap::new();
for platform in catalog.names() {
if catalog.webhook_document(&platform).is_none() {
continue;
}
match std::env::var(secret_variable(&platform)) {
Ok(secret) if !secret.is_empty() => {
secrets.insert(platform, secret.into_bytes());
}
_ => tracing::warn!(
%platform,
variable = %secret_variable(&platform),
"declares webhook deliveries but has no shared-hook secret; its deliveries are refused"
),
}
}
tracing::info!("webhook inbox enabled");
let mut webhooks = Webhooks::with_limits(store, secrets, limits::Limits::from_config(config));
webhooks.notifications_only = config.notifications_only;
Ok(Some(Arc::new(webhooks)))
}