pub mod wire;
mod poller;
#[cfg(test)]
mod tests;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Result, anyhow, bail};
use async_trait::async_trait;
use greentic_types::{DispatchMode, EnvId, TenantCtx, TenantId};
use parking_lot::Mutex;
use secrecy::{ExposeSecret, SecretString};
use tokio::sync::Notify;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;
use super::dispatch_listener::SessionResumer;
use super::remote_dispatch::{RemoteDispatch, RemoteDispatchAction, RemoteDispatchHandler};
pub use wire::{
APPROVAL_RAIL_HEADER, APPROVAL_RAIL_USER_AGENT, APPROVAL_RAIL_VERSION, ApprovalDecisionBody,
ApprovalDecisionKind, ApprovalRequestBody, ApprovalWithdrawBody, WithdrawReason,
};
pub const MAX_APPROVAL_WAIT: Duration = Duration::from_secs(7 * 24 * 60 * 60);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(3);
const REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
#[derive(Clone)]
pub struct ApprovalInboxTarget {
pub request_url: String,
pub decision_url: String,
pub withdraw_url: String,
pub token: String,
pub tenant_slug: String,
}
impl std::fmt::Debug for ApprovalInboxTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ApprovalInboxTarget")
.field("request_url", &self.request_url)
.field("decision_url", &self.decision_url)
.field("withdraw_url", &self.withdraw_url)
.field("token", &"<redacted>")
.field("tenant_slug", &self.tenant_slug)
.finish()
}
}
#[derive(Debug, thiserror::Error)]
pub enum ApprovalInboxError {
#[error("approval inbox needs a non-blank `{0}`")]
Blank(&'static str),
#[error(
"approval inbox `{0}` is not https and not loopback http; refusing to send a bearer \
token in cleartext"
)]
UnsafeUrl(String),
#[error("approval inbox has no HTTP client: {0}")]
Client(String),
}
#[derive(Clone, Debug)]
pub(crate) struct Timing {
pub(crate) send_retry_delays: Vec<Duration>,
pub(crate) fast_interval: Duration,
pub(crate) fast_window: Duration,
pub(crate) slow_interval: Duration,
pub(crate) backoff: Duration,
pub(crate) max_wait: Duration,
}
impl Default for Timing {
fn default() -> Self {
Self {
send_retry_delays: vec![
Duration::from_secs(1),
Duration::from_secs(4),
Duration::from_secs(10),
],
fast_interval: Duration::from_secs(5),
fast_window: Duration::from_secs(60),
slow_interval: Duration::from_secs(15),
backoff: Duration::from_secs(60),
max_wait: MAX_APPROVAL_WAIT,
}
}
}
pub(crate) struct Pending {
pub(crate) tenant: TenantCtx,
pub(crate) env: String,
pub(crate) decision_token: Option<SecretString>,
pub(crate) registered_at: Instant,
pub(crate) deadline_at: Instant,
pub(crate) deadline_ms: u64,
pub(crate) deadline_reason: WithdrawReason,
pub(crate) next_poll_at: Instant,
}
pub(crate) struct Inner {
pub(crate) request_url: String,
pub(crate) decision_url: url::Url,
pub(crate) withdraw_url: String,
pub(crate) token: SecretString,
pub(crate) tenant_slug: String,
pub(crate) http: reqwest::Client,
pub(crate) timing: Timing,
pub(crate) pending: Mutex<HashMap<String, Pending>>,
pub(crate) resumer: Mutex<Option<Arc<dyn SessionResumer>>>,
pub(crate) cancel: CancellationToken,
pub(crate) wake: Notify,
pub(crate) poller_started: Mutex<bool>,
}
#[derive(Clone)]
pub struct HttpApprovalDispatcher {
pub(crate) inner: Arc<Inner>,
}
impl std::fmt::Debug for HttpApprovalDispatcher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HttpApprovalDispatcher")
.field("request_url", &self.inner.request_url)
.field("token", &"<redacted>")
.field("tenant_slug", &self.inner.tenant_slug)
.field("pending", &self.inner.pending.lock().len())
.finish()
}
}
fn required(field: &'static str, value: &str) -> Result<String, ApprovalInboxError> {
let trimmed = value.trim();
if trimmed.is_empty() {
return Err(ApprovalInboxError::Blank(field));
}
Ok(trimmed.to_string())
}
fn safe_url(field: &'static str, value: &str) -> Result<url::Url, ApprovalInboxError> {
let value = required(field, value)?;
let unsafe_url = || ApprovalInboxError::UnsafeUrl(value.clone());
let url = url::Url::parse(&value).map_err(|_| unsafe_url())?;
let safe = match url.scheme() {
"https" => url.host().is_some(),
"http" => match url.host() {
Some(url::Host::Domain(domain)) => domain.eq_ignore_ascii_case("localhost"),
Some(url::Host::Ipv4(ip)) => ip.is_loopback(),
Some(url::Host::Ipv6(ip)) => ip.is_loopback(),
None => false,
},
_ => false,
};
if safe { Ok(url) } else { Err(unsafe_url()) }
}
impl HttpApprovalDispatcher {
pub fn new(target: ApprovalInboxTarget) -> Result<Self, ApprovalInboxError> {
Self::with_timing(target, Timing::default())
}
pub(crate) fn with_timing(
target: ApprovalInboxTarget,
timing: Timing,
) -> Result<Self, ApprovalInboxError> {
let request_url = safe_url("request_url", &target.request_url)?;
let decision_url = safe_url("decision_url", &target.decision_url)?;
let withdraw_url = safe_url("withdraw_url", &target.withdraw_url)?;
let token = required("token", &target.token)?;
let tenant_slug = required("tenant_slug", &target.tenant_slug)?;
let http = reqwest::Client::builder()
.timeout(REQUEST_TIMEOUT)
.connect_timeout(CONNECT_TIMEOUT)
.user_agent(APPROVAL_RAIL_USER_AGENT)
.build()
.map_err(|err| ApprovalInboxError::Client(err.to_string()))?;
Ok(Self {
inner: Arc::new(Inner {
request_url: request_url.to_string(),
decision_url,
withdraw_url: withdraw_url.to_string(),
token: SecretString::from(token),
tenant_slug,
http,
timing,
pending: Mutex::new(HashMap::new()),
resumer: Mutex::new(None),
cancel: CancellationToken::new(),
wake: Notify::new(),
poller_started: Mutex::new(false),
}),
})
}
pub fn attach_resumer(&self, resumer: Arc<dyn SessionResumer>) {
if self.inner.cancel.is_cancelled() {
return;
}
*self.inner.resumer.lock() = Some(resumer);
}
pub fn shutdown(&self) {
self.inner.cancel.cancel();
self.inner.pending.lock().clear();
self.inner.resumer.lock().take();
}
pub fn cancellation_token(&self) -> CancellationToken {
self.inner.cancel.clone()
}
pub fn pending_count(&self) -> usize {
self.inner.pending.lock().len()
}
fn authed(&self, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
builder
.bearer_auth(self.inner.token.expose_secret())
.header(APPROVAL_RAIL_HEADER, APPROVAL_RAIL_VERSION)
}
async fn send(&self, body: &ApprovalRequestBody) -> Result<()> {
let delays = self.inner.timing.send_retry_delays.clone();
let mut last = String::new();
for attempt in 0..=delays.len() {
if attempt > 0 {
tokio::time::sleep(delays[attempt - 1]).await;
}
let sent = self
.authed(self.inner.http.post(&self.inner.request_url))
.json(body)
.send()
.await;
let response = match sent {
Ok(response) => response,
Err(error) => {
last = format!("transport error: {}", error.without_url());
continue;
}
};
let status = response.status();
if status.is_success() {
return Ok(());
}
let text = response.text().await.unwrap_or_default();
if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
if text.contains(wire::PENDING_CAP_CODE) {
bail!(
"approval inbox refused the request: this worker already has the maximum \
number of pending approvals ({})",
wire::PENDING_CAP_CODE
);
}
last = format!("HTTP {status}");
continue;
}
if status.is_server_error() {
last = format!("HTTP {status}");
continue;
}
bail!("approval inbox refused the request with HTTP {status}");
}
Err(anyhow!(
"approval inbox did not record the request after {} attempts ({last})",
delays.len() + 1
))
}
fn register(&self, correlation_id: String, pending: Pending) {
self.inner.pending.lock().insert(correlation_id, pending);
self.ensure_poller();
self.inner.wake.notify_one();
}
fn ensure_poller(&self) {
let mut started = self.inner.poller_started.lock();
if *started || self.inner.cancel.is_cancelled() {
return;
}
*started = true;
tokio::spawn(poller::run(Arc::clone(&self.inner)));
}
}
fn effective_deadline(node_deadline_ms: Option<u64>, cap: Duration) -> (u64, WithdrawReason) {
let cap_ms = u64::try_from(cap.as_millis()).unwrap_or(u64::MAX);
match node_deadline_ms {
Some(ms) if ms <= cap_ms => (ms, WithdrawReason::Timeout),
_ => (cap_ms, WithdrawReason::PollCap),
}
}
#[async_trait]
impl RemoteDispatchHandler for HttpApprovalDispatcher {
async fn dispatch(&self, request: RemoteDispatch) -> Result<RemoteDispatchAction> {
if request.runtime != crate::runner::engine::APPROVAL_RUNTIME {
bail!(
"{}.call dispatched to the approval inbox, which serves only approval.call",
request.runtime
);
}
if self.inner.cancel.is_cancelled() {
bail!("approval inbox is shut down; this revision is no longer serving");
}
let tenant = TenantCtx::new(
EnvId::try_from(request.env.as_str())
.map_err(|error| anyhow!("invalid env for approval dispatch: {error}"))?,
TenantId::try_from(request.tenant.as_str())
.map_err(|error| anyhow!("invalid tenant for approval dispatch: {error}"))?,
);
let (deadline_ms, deadline_reason) =
effective_deadline(request.deadline_ms, self.inner.timing.max_wait);
let body = wire::request_body(
&self.inner.tenant_slug,
&request.correlation_id,
&request.target,
&request.operation,
request.mode,
request.input,
deadline_ms,
);
self.send(&body).await?;
match request.mode {
DispatchMode::FireAndForget => Ok(RemoteDispatchAction::Dispatched),
DispatchMode::Await => {
let now = Instant::now();
self.register(
request.correlation_id.clone(),
Pending {
tenant,
env: request.env,
decision_token: request.decision_token.map(SecretString::from),
registered_at: now,
deadline_at: now + Duration::from_millis(deadline_ms),
deadline_ms,
deadline_reason,
next_poll_at: now + self.inner.timing.fast_interval,
},
);
Ok(RemoteDispatchAction::AwaitingResponse {
correlation_id: request.correlation_id,
})
}
}
}
}
pub(crate) fn inbox_to_install(
nats_connected: bool,
target: Option<ApprovalInboxTarget>,
) -> Option<ApprovalInboxTarget> {
if nats_connected { None } else { target }
}