use crate::secret::SecretScope;
#[cfg(any(test, feature = "test-support"))]
pub static TEST_TRANSPORT_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
pub const SECRET_CONSENT_KIND: &str = "secret_use";
#[derive(Clone, PartialEq, Eq)]
pub struct SecretConsentRequest {
pub scope: SecretScope,
pub project_root: std::path::PathBuf,
pub session: String,
pub label: String,
pub request_id: String,
pub statement_b64url: String,
pub expires_at_ms: Option<u64>,
}
impl std::fmt::Debug for SecretConsentRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SecretConsentRequest")
.field("scope", &self.scope)
.field("label", &self.label)
.field("request_id", &self.request_id)
.field("statement_b64url_len", &self.statement_b64url.len())
.finish()
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum ConsentOutcome {
Answered,
AnsweredOnce,
Declined,
Expired,
Cancelled,
Indeterminate,
Pending,
Unavailable {
detail: String,
},
}
impl std::fmt::Debug for ConsentOutcome {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConsentOutcome::Answered => f.write_str("Answered"),
ConsentOutcome::AnsweredOnce => f.write_str("AnsweredOnce"),
ConsentOutcome::Declined => f.write_str("Declined"),
ConsentOutcome::Expired => f.write_str("Expired"),
ConsentOutcome::Cancelled => f.write_str("Cancelled"),
ConsentOutcome::Indeterminate => f.write_str("Indeterminate"),
ConsentOutcome::Pending => f.write_str("Pending"),
ConsentOutcome::Unavailable { detail } => f
.debug_struct("Unavailable")
.field("detail", detail)
.finish(),
}
}
}
pub trait SecretConsentTransport: Send + Sync + 'static {
fn runtime(&self) -> Option<tokio::runtime::Handle> {
None
}
fn wait(
self: std::sync::Arc<Self>,
request: SecretConsentRequest,
cancel: Cancellation,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = ConsentOutcome> + Send>> {
Box::pin(async move {
let worker = self.clone();
let ask = request.clone();
let held_cancel = cancel.clone();
let held = tokio::task::spawn_blocking(move || {
if held_cancel.is_cancelled() {
ConsentOutcome::Cancelled
} else {
worker.hold(&ask, &held_cancel)
}
});
let outcome = tokio::select! {
biased;
() = cancel.cancelled() => ConsentOutcome::Cancelled,
result = held => result.unwrap_or(ConsentOutcome::Unavailable { detail: "Cingulate could not settle the secret-use card".into() }),
};
if matches!(
outcome,
ConsentOutcome::Cancelled
| ConsentOutcome::Unavailable { .. }
| ConsentOutcome::Indeterminate
) {
let _ = tokio::task::spawn_blocking(move || self.withhold(&request)).await;
}
outcome
})
}
fn file(&self, request: &SecretConsentRequest) -> ConsentOutcome;
fn hold(&self, request: &SecretConsentRequest, cancel: &Cancellation) -> ConsentOutcome {
if cancel.is_cancelled() {
ConsentOutcome::Cancelled
} else {
self.file(request)
}
}
fn withhold(&self, request: &SecretConsentRequest) -> ConsentOutcome;
fn forget_expired(
&self,
_request: &SecretConsentRequest,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + '_>> {
Box::pin(std::future::ready(()))
}
}
static TRANSPORT: std::sync::LazyLock<
std::sync::Mutex<Option<std::sync::Arc<dyn SecretConsentTransport>>>,
> = std::sync::LazyLock::new(|| std::sync::Mutex::new(None));
pub fn install_transport(transport: Option<std::sync::Arc<dyn SecretConsentTransport>>) {
drop(replace_transport(transport));
}
fn replace_transport(transport: Option<std::sync::Arc<dyn SecretConsentTransport>>) -> Cards {
let mut installed = TRANSPORT
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
*installed = transport;
for card in cards
.values()
.filter(|card| card.outcome.borrow().is_none())
{
card.cancel.cancel();
}
std::mem::take(&mut *cards)
}
pub async fn shutdown() {
let cards = replace_transport(None);
let _ = tokio::time::timeout(std::time::Duration::from_secs(10), async {
for card in cards.values() {
let mut outcome = card.outcome.subscribe();
let _ = outcome.wait_for(Option::is_some).await;
}
})
.await;
}
fn transport() -> Option<std::sync::Arc<dyn SecretConsentTransport>> {
TRANSPORT
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
pub fn file_consent(request: &SecretConsentRequest) -> ConsentOutcome {
match transport() {
Some(transport) => transport.file(request),
None => ConsentOutcome::Unavailable {
detail: "no secret consent transport is connected".into(),
},
}
}
#[derive(Clone)]
pub struct Cancellation(tokio::sync::watch::Sender<Option<CancellationReason>>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CancellationReason {
Expired,
Cancelled,
}
impl CancellationReason {
pub fn code(self) -> &'static str {
match self {
Self::Expired => "expired",
Self::Cancelled => "cancelled",
}
}
}
impl Default for Cancellation {
fn default() -> Self {
Self(tokio::sync::watch::channel(None).0)
}
}
impl Cancellation {
pub fn cancel(&self) {
self.cancel_with_reason(CancellationReason::Cancelled);
}
pub fn cancel_with_reason(&self, reason: CancellationReason) {
self.0.send_if_modified(|current| {
if current.is_none() {
*current = Some(reason);
true
} else {
false
}
});
}
pub fn reason(&self) -> Option<CancellationReason> {
*self.0.borrow()
}
pub fn is_cancelled(&self) -> bool {
self.reason().is_some()
}
pub async fn cancelled(&self) {
let mut receiver = self.0.subscribe();
loop {
if receiver.borrow_and_update().is_some() {
return;
}
if receiver.changed().await.is_err() {
return;
}
}
}
}
#[derive(Clone)]
pub struct CardKey(pub SecretScope, pub String);
impl CardKey {
fn identity(&self) -> (&str, &str, u64, &str) {
(
self.0.owner(),
self.0.reference(),
self.0.stamp().scope_epoch,
&self.1,
)
}
}
impl PartialEq for CardKey {
fn eq(&self, other: &Self) -> bool {
self.identity() == other.identity()
}
}
impl Eq for CardKey {}
impl std::hash::Hash for CardKey {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
std::hash::Hash::hash(&self.identity(), hasher);
}
}
type Cards = std::collections::HashMap<CardKey, std::sync::Arc<SharedCard>>;
const MAX_CLIENTLESS_CARDS: usize = 32;
const MAX_CARD_LIFETIME_MS: u64 = 600_000;
struct SharedCard {
created: tokio::time::Instant,
statement: String,
expires: u64,
deadline: tokio::time::Instant,
outcome: tokio::sync::watch::Sender<Option<ConsentOutcome>>,
outcome_code: std::sync::Mutex<Option<&'static str>>,
cancel: Cancellation,
waiters: std::sync::atomic::AtomicUsize,
}
impl SharedCard {
fn is_pending(&self) -> bool {
!self.cancel.is_cancelled() && self.outcome.borrow().is_none()
}
fn is_clientless(&self) -> bool {
self.waiters.load(std::sync::atomic::Ordering::Acquire) == 0 && self.is_pending()
}
}
pub fn pending_scope_stamps() -> Vec<subc_protocol::scope::ScopeStamp> {
CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.iter()
.filter(|(_, card)| card.is_pending())
.map(|(key, _)| key.0.stamp().clone())
.collect()
}
pub fn revoke_scope(stamp: &subc_protocol::scope::ScopeStamp) {
let cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
for (key, card) in cards.iter() {
let held = key.0.stamp();
if held.owner == stamp.owner
&& held.scope_ref == stamp.scope_ref
&& held.scope_epoch == stamp.scope_epoch
&& card.is_pending()
{
card.cancel.cancel();
}
}
}
pub fn record_outcome_code(scope: &SecretScope, request_id: &str, code: &str) {
let cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(card) = cards.get(&CardKey(scope.clone(), request_id.to_owned())) {
*card
.outcome_code
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) =
Some(super::transport::diagnostics::safe_code(code));
}
}
static CARDS: std::sync::LazyLock<std::sync::Mutex<Cards>> =
std::sync::LazyLock::new(Default::default);
struct Waiter<'a> {
card: std::sync::Arc<SharedCard>,
request_id: &'a str,
local_cancel: Option<&'a Cancellation>,
}
impl Drop for Waiter<'_> {
fn drop(&mut self) {
let cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let last = self
.card
.waiters
.fetch_sub(1, std::sync::atomic::Ordering::AcqRel)
== 1;
if !self.card.is_pending() {
return;
}
if let Some(reason) = self.local_cancel.and_then(Cancellation::reason) {
if last {
self.card.cancel.cancel_with_reason(reason);
}
return;
}
if last {
let mut clientless = cards.values().filter(|card| card.is_clientless());
if let Some(oldest) = clientless.next() {
let (count, oldest) = clientless.fold((1, oldest), |(count, oldest), card| {
(
count + 1,
if card.created < oldest.created {
card
} else {
oldest
},
)
});
if count > MAX_CLIENTLESS_CARDS {
oldest
.cancel
.cancel_with_reason(CancellationReason::Expired);
}
}
}
if !self.card.cancel.is_cancelled() {
crate::slog_info!(
"secret consent waiter dropped; card kept until expiry: request_id={:?} expires_at_ms={}",
self.request_id,
self.card.expires,
);
}
}
}
fn scope_inventory_changed() {
crate::scope_watch_events::changed(crate::scope_watch_events::Source::Parked);
}
async fn run_card(
key: CardKey,
card: std::sync::Arc<SharedCard>,
transport: std::sync::Arc<dyn SecretConsentTransport>,
request: SecretConsentRequest,
) {
let wait = transport.clone().wait(request.clone(), card.cancel.clone());
let expiry = tokio::time::sleep_until(card.deadline);
tokio::pin!(wait, expiry);
let answer = tokio::select! {
biased;
() = &mut expiry => {
card.cancel.cancel_with_reason(CancellationReason::Expired);
wait.await
}
answer = &mut wait => answer,
};
{
let _cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
card.outcome.send_replace(Some(answer));
}
scope_inventory_changed();
expiry.await;
transport.forget_expired(&request).await;
let mut cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if cards
.get(&key)
.is_some_and(|current| std::sync::Arc::ptr_eq(current, &card))
{
cards.remove(&key);
}
}
#[rustfmt::skip]
test_support_visibility! {
#[cfg(any(test, feature = "test-support"))]
pub(crate) async fn wait_consent(request: SecretConsentRequest) -> ConsentOutcome {
wait_consent_with_code(request, None).await.0
}
}
pub(crate) async fn wait_consent_with_code(
mut request: SecretConsentRequest,
local_cancel: Option<&Cancellation>,
) -> (ConsentOutcome, Option<&'static str>) {
let card = {
use std::collections::hash_map::Entry;
let installed = TRANSPORT
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let Some(transport) = installed.clone() else {
return (
ConsentOutcome::Unavailable {
detail: "no secret consent transport is connected".into(),
},
None,
);
};
let now = super::vault::now_ms();
let expires = request
.expires_at_ms
.unwrap_or(u64::MAX)
.min(now.saturating_add(MAX_CARD_LIFETIME_MS));
if expires <= now {
return (ConsentOutcome::Expired, None);
}
request.expires_at_ms = Some(expires);
let mut cards = CARDS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cards.retain(|_, card| {
card.waiters.load(std::sync::atomic::Ordering::Acquire) != 0
|| !matches!(
&*card.outcome.borrow(),
Some(ConsentOutcome::Unavailable { .. } | ConsentOutcome::Indeterminate)
)
});
let key = CardKey(request.scope.clone(), request.request_id.clone());
let card = match cards.entry(key.clone()) {
Entry::Occupied(entry) => {
let card = entry.get();
if card.statement != request.statement_b64url {
return (
ConsentOutcome::Unavailable {
detail: "Cingulate could not file the secret-use card".into(),
},
None,
);
}
if tokio::time::Instant::now() >= card.deadline {
return (ConsentOutcome::Expired, None);
}
if card.is_pending() {
crate::slog_info!(
"secret consent retry attached to existing card: request_id={:?}",
request.request_id,
);
}
card.clone()
}
Entry::Vacant(entry) => {
let runtime = transport
.runtime()
.unwrap_or_else(tokio::runtime::Handle::current);
let created = tokio::time::Instant::now();
let card = std::sync::Arc::new(SharedCard {
created,
statement: request.statement_b64url.clone(),
expires,
deadline: created + std::time::Duration::from_millis(expires - now),
outcome: tokio::sync::watch::channel(None).0,
outcome_code: Default::default(),
cancel: Default::default(),
waiters: Default::default(),
});
entry.insert(card.clone());
scope_inventory_changed();
runtime.spawn(run_card(key, card.clone(), transport, request.clone()));
card
}
};
card.waiters
.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
card
};
let _waiter = Waiter {
card: card.clone(),
request_id: &request.request_id,
local_cancel,
};
let mut receiver = card.outcome.subscribe();
loop {
if let Some(answer) = receiver.borrow_and_update().clone() {
let code = *card
.outcome_code
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
return (answer, code);
}
if receiver.changed().await.is_err() {
return (
ConsentOutcome::Unavailable {
detail: "Cingulate could not settle the secret-use card".into(),
},
None,
);
}
}
}
pub fn withhold_consent(request: &SecretConsentRequest) -> ConsentOutcome {
match transport() {
Some(transport) => transport.withhold(request),
None => ConsentOutcome::Unavailable {
detail: "no secret consent transport is connected".into(),
},
}
}
pub fn request_params(request: &SecretConsentRequest) -> serde_json::Value {
serde_json::json!({
"kind": SECRET_CONSENT_KIND,
"secret_use": {
"items": [{
"request_id": request.request_id,
"statement_b64url": request.statement_b64url,
}],
},
})
}
#[cfg(test)]
#[path = "consent_cancel_tests.rs"]
mod cancellation_cache_tests;