use std::collections::BTreeMap;
use std::future::Future;
use axum::http::{Method, StatusCode};
use super::cleanup::{CleanupError, HookDeleter};
use super::metadata::{fill_path, Deliveries};
use super::store::{AccessCheck, HookToDelete};
use crate::proxy::ReceiverReply;
use crate::AppState;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Checked {
Passed(String),
Failed,
Unavailable,
Invalid(&'static str),
}
impl Checked {
pub fn against(self, bound_key: &str) -> AccessCheck {
match self {
Checked::Passed(key) if key == bound_key => AccessCheck::Passed,
Checked::Passed(_) | Checked::Failed => AccessCheck::Failed,
Checked::Unavailable => AccessCheck::Unavailable,
Checked::Invalid(_) => AccessCheck::Failed,
}
}
}
pub fn parameters(json: &str) -> Option<BTreeMap<String, String>> {
serde_json::from_str(json).ok()
}
pub async fn check_access(
state: &AppState,
connection_id: &str,
platform: &str,
deliveries: &Deliveries,
source_kind: &str,
parameters: &BTreeMap<String, String>,
) -> Checked {
let Some(security) = state.security.as_ref() else {
return Checked::Unavailable;
};
let Some(source) = deliveries.sources.get(source_kind) else {
return Checked::Invalid("unknown source kind");
};
let Ok(path) = fill_path(&source.access_path, parameters) else {
return Checked::Invalid("access parameters must fill the access path, one segment each");
};
let reply =
crate::proxy::receiver_call(state, security, connection_id, platform, Method::GET, &path)
.await;
classify(reply, source)
}
fn classify(reply: ReceiverReply, source: &super::metadata::Source) -> Checked {
match reply {
ReceiverReply::Answered(status, body) if status.is_success() => {
match serde_json::from_slice::<serde_json::Value>(&body) {
Err(_) => Checked::Unavailable,
Ok(value) => match source.access_key.key(&value) {
Some(key) => Checked::Passed(key),
None => Checked::Failed,
},
}
}
ReceiverReply::Answered(
StatusCode::UNAUTHORIZED
| StatusCode::FORBIDDEN
| StatusCode::NOT_FOUND
| StatusCode::GONE,
_,
) => Checked::Failed,
ReceiverReply::Answered(..) | ReceiverReply::Unreachable => Checked::Unavailable,
ReceiverReply::Refused("unknown-connection" | "platform-mismatch")
| ReceiverReply::RefreshRefused => Checked::Failed,
ReceiverReply::Refused(reason) => Checked::Invalid(reason),
}
}
pub fn deliveries(state: &AppState, platform: &str) -> Option<Deliveries> {
Deliveries::from_document(&state.catalog.webhook_document(platform)?).ok()
}
pub fn endpoint_url(state: &AppState, endpoint_id: &str) -> String {
format!("{}/webhooks/{endpoint_id}", state.base_url)
}
async fn find_hook(
state: &AppState,
connection: &str,
hook: &HookToDelete,
dedicated: &super::metadata::Dedicated,
values: &BTreeMap<String, String>,
) -> Result<Option<String>, CleanupError> {
let Some((path, url_pointer, id_pointer)) = &dedicated.list else {
return Err(CleanupError::Permanent("no-list-operation"));
};
let security = state
.security
.as_ref()
.ok_or(CleanupError::Retry("no-database"))?;
let path = fill_path(path, values).map_err(|_| CleanupError::Permanent("invalid-list-path"))?;
let body = match crate::proxy::receiver_call(
state,
security,
connection,
&hook.platform,
Method::GET,
&path,
)
.await
{
ReceiverReply::Answered(status, body) if status.is_success() => body,
ReceiverReply::Answered(StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN, _) => {
return Err(CleanupError::Permanent("refused"))
}
ReceiverReply::Answered(..) | ReceiverReply::Unreachable => {
return Err(CleanupError::Retry("unavailable"))
}
ReceiverReply::Refused(_) => return Err(CleanupError::Permanent("not-allowlisted")),
ReceiverReply::RefreshRefused => return Err(CleanupError::Permanent("refresh-refused")),
};
let listed: serde_json::Value =
serde_json::from_slice(&body).map_err(|_| CleanupError::Retry("unreadable-list"))?;
let ours = endpoint_url(state, &hook.endpoint_id);
let (Some((items, url_inside)), Some((id_items, id_inside))) =
(url_pointer.split_wildcard(), id_pointer.split_wildcard())
else {
return Err(CleanupError::Permanent("invalid-list-pointers"));
};
if items != id_items {
return Err(CleanupError::Permanent("invalid-list-pointers"));
}
for item in items
.select(&listed)
.into_iter()
.filter_map(serde_json::Value::as_array)
.flatten()
{
if url_inside.key(item).as_deref() == Some(ours.as_str()) {
return Ok(id_inside.key(item));
}
}
Ok(None)
}
#[derive(Clone)]
pub struct ProviderHookDeleter {
pub state: AppState,
}
impl HookDeleter for ProviderHookDeleter {
fn delete(&self, hook: &HookToDelete) -> impl Future<Output = Result<(), CleanupError>> + Send {
let state = self.state.clone();
let hook = hook.clone();
async move {
tracing::debug!(hook = %hook.hook_id, platform = %hook.platform, "deleting a dedicated webhook hook");
let deliveries = deliveries(&state, &hook.platform)
.ok_or(CleanupError::Permanent("no-declaration"))?;
let dedicated = deliveries
.dedicated
.as_ref()
.ok_or(CleanupError::Permanent("no-dedicated-hooks"))?;
if dedicated.source != hook.source_kind {
return Err(CleanupError::Permanent("source-kind-changed"));
}
let connection = hook
.management_connection_id
.as_deref()
.ok_or(CleanupError::Permanent("no-managing-connection"))?;
let mut values = parameters(&hook.access_parameters)
.ok_or(CleanupError::Permanent("invalid-parameters"))?;
match check_access(
&state,
connection,
&hook.platform,
&deliveries,
&hook.source_kind,
&values,
)
.await
.against(&hook.source_key)
{
AccessCheck::Passed => {}
AccessCheck::Failed => return Err(CleanupError::Permanent("access-check-failed")),
AccessCheck::Unavailable => {
return Err(CleanupError::Retry("access-check-unavailable"))
}
}
let provider_hook_id = match hook.provider_hook_id.clone() {
Some(id) => id,
None => match find_hook(&state, connection, &hook, dedicated, &values).await? {
Some(id) => id,
None => return Ok(()),
},
};
values.insert(dedicated.hook_id_parameter.clone(), provider_hook_id);
let path = fill_path(&dedicated.delete.path, &values)
.map_err(|_| CleanupError::Permanent("invalid-delete-path"))?;
let method =
Method::from_bytes(dedicated.delete.method.to_ascii_uppercase().as_bytes())
.map_err(|_| CleanupError::Permanent("invalid-delete-method"))?;
let security = state
.security
.as_ref()
.ok_or(CleanupError::Retry("no-database"))?;
match crate::proxy::receiver_call(
&state,
security,
connection,
&hook.platform,
method,
&path,
)
.await
{
ReceiverReply::Answered(status, _)
if status.is_success()
|| status == StatusCode::NOT_FOUND
|| status == StatusCode::GONE =>
{
Ok(())
}
ReceiverReply::Answered(StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN, _) => {
Err(CleanupError::Permanent("refused"))
}
ReceiverReply::Answered(..) | ReceiverReply::Unreachable => {
Err(CleanupError::Retry("unavailable"))
}
ReceiverReply::Refused(_) => Err(CleanupError::Permanent("not-allowlisted")),
ReceiverReply::RefreshRefused => Err(CleanupError::Permanent("refresh-refused")),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Bytes;
fn project() -> super::super::metadata::Source {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../openapi-extensions/spec/webhook-deliveries/examples/tracker.yaml");
let document = serde_yaml::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
Deliveries::from_document(&document).unwrap().sources["project"].clone()
}
fn answered(status: StatusCode, body: &'static str) -> ReceiverReply {
ReceiverReply::Answered(status, Bytes::from_static(body.as_bytes()))
}
#[test]
fn a_2xx_that_is_not_json_is_unavailable_not_failed() {
let source = project();
assert_eq!(
classify(
answered(StatusCode::OK, "<html>maintenance</html>"),
&source
),
Checked::Unavailable
);
assert_eq!(
classify(answered(StatusCode::OK, r#"{"id": 7}"#), &source),
Checked::Passed("7".into())
);
assert_eq!(
classify(answered(StatusCode::OK, r#"{"name": "no id"}"#), &source),
Checked::Failed
);
assert_eq!(
classify(ReceiverReply::Unreachable, &source),
Checked::Unavailable
);
assert_eq!(
classify(answered(StatusCode::FORBIDDEN, ""), &source),
Checked::Failed
);
}
}