use std::any::type_name;
use std::fmt;
use std::sync::{Arc, Mutex, PoisonError};
use id_effect::{Effect, HCons, HList, HNil, Layer, Selector, fail, need, succeed, sync};
use serde::{Deserialize, Serialize};
use crate::policy::{AuthzError, Decision, Policy, PolicyEffect};
use crate::principal::CurrentPrincipal;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
Allow,
Deny,
Error,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DecisionEvent {
pub principal: String,
pub authenticated: bool,
pub tenant: Option<String>,
pub policy: String,
pub resource: String,
pub outcome: Outcome,
pub reason: Option<String>,
}
impl DecisionEvent {
pub fn new(
principal: &CurrentPrincipal,
policy: impl Into<String>,
resource: impl Into<String>,
result: Result<&Decision, &AuthzError>,
) -> Self {
let (outcome, reason) = match result {
Ok(Decision::Allow) => (Outcome::Allow, None),
Ok(Decision::Deny(denial)) => (Outcome::Deny, Some(denial.reason().to_owned())),
Err(error) => (Outcome::Error, Some(error.to_string())),
};
Self {
principal: principal.id().to_owned(),
authenticated: principal.is_authenticated(),
tenant: principal.tenant().map(str::to_owned),
policy: policy.into(),
resource: resource.into(),
outcome,
reason,
}
}
pub fn is_allowed(&self) -> bool {
self.outcome == Outcome::Allow
}
}
impl fmt::Display for DecisionEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let who = if self.authenticated {
self.principal.as_str()
} else {
"<anonymous>"
};
write!(
f,
"{who} {} {} on {}",
match self.outcome {
Outcome::Allow => "allowed by",
Outcome::Deny => "denied by",
Outcome::Error => "failed evaluating",
},
self.policy,
self.resource
)?;
match &self.reason {
Some(reason) => write!(f, ": {reason}"),
None => Ok(()),
}
}
}
pub trait AuthzSink: Send + Sync + 'static {
fn record(&self, event: &DecisionEvent);
}
#[derive(Clone, Default)]
pub struct AuthzEvents(Arc<[Arc<dyn AuthzSink>]>);
impl AuthzEvents {
pub fn new(sink: impl AuthzSink) -> Self {
Self(Arc::new([Arc::new(sink) as Arc<dyn AuthzSink>]))
}
pub fn none() -> Self {
Self::default()
}
pub fn from_fn(subscriber: impl Fn(&DecisionEvent) + Send + Sync + 'static) -> Self {
Self::new(FnSink(subscriber))
}
#[must_use]
pub fn and(self, other: AuthzEvents) -> Self {
Self(self.0.iter().chain(other.0.iter()).cloned().collect())
}
pub fn sinks(&self) -> usize {
self.0.len()
}
pub fn record(&self, event: &DecisionEvent) {
for sink in self.0.iter() {
sink.record(event);
}
}
pub fn emit<E: Send + 'static, R: Send + Sync + 'static>(
&self,
event: DecisionEvent,
) -> Effect<(), E, R> {
let events = self.clone();
sync(move || events.record(&event))
}
pub fn observe<P>(&self, name: impl Into<String>, policy: P) -> Observed<P> {
Observed {
name: name.into(),
policy,
events: self.clone(),
}
}
}
impl fmt::Debug for AuthzEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AuthzEvents")
.field("sinks", &self.sinks())
.finish()
}
}
struct FnSink<F>(F);
impl<F: Fn(&DecisionEvent) + Send + Sync + 'static> AuthzSink for FnSink<F> {
fn record(&self, event: &DecisionEvent) {
(self.0)(event);
}
}
#[derive(Clone, Debug, Default)]
pub struct MemorySink(Arc<Mutex<Vec<DecisionEvent>>>);
impl MemorySink {
pub fn new() -> Self {
Self::default()
}
pub fn events(&self) -> Vec<DecisionEvent> {
self.lock().clone()
}
pub fn take(&self) -> Vec<DecisionEvent> {
std::mem::take(&mut *self.lock())
}
pub fn len(&self) -> usize {
self.lock().len()
}
pub fn is_empty(&self) -> bool {
self.lock().is_empty()
}
fn lock(&self) -> std::sync::MutexGuard<'_, Vec<DecisionEvent>> {
self.0.lock().unwrap_or_else(PoisonError::into_inner)
}
}
impl AuthzSink for MemorySink {
fn record(&self, event: &DecisionEvent) {
self.lock().push(event.clone());
}
}
#[derive(Clone, Debug)]
pub struct Observed<P> {
name: String,
policy: P,
events: AuthzEvents,
}
impl<Res: ?Sized, P: Policy<Res>> Policy<Res> for Observed<P> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
audited(
self.policy.evaluate(principal, resource),
&self.events,
principal,
&self.name,
type_name::<Res>(),
)
}
}
pub(crate) fn audited(
effect: PolicyEffect,
events: &AuthzEvents,
principal: &CurrentPrincipal,
policy: &str,
resource: &str,
) -> PolicyEffect {
let on_error = (
events.clone(),
principal.clone(),
policy.to_owned(),
resource.to_owned(),
);
let events = events.clone();
let principal = principal.clone();
let (policy, resource) = (policy.to_owned(), resource.to_owned());
effect
.flat_map(move |decision| {
let event = DecisionEvent::new(&principal, policy, resource, Ok(&decision));
events.emit(event).map(move |()| decision)
})
.catch(move |error| {
let (events, principal, policy, resource) = on_error;
let event = DecisionEvent::new(&principal, policy, resource, Err(&error));
events.emit(event).flat_map(move |()| fail(error))
})
}
pub type AuthzEventsEnv = HCons<AuthzEvents, HNil>;
#[inline]
pub fn provide_authz_events<E: Send + 'static>(events: AuthzEvents) -> Layer<AuthzEventsEnv, E> {
Layer::succeed(id_effect::hlist![events])
}
pub fn emit_decision<E, R, I>(event: DecisionEvent) -> Effect<(), E, R>
where
E: Send + 'static,
R: Selector<AuthzEvents, I> + HList,
{
need::<AuthzEvents>().flat_map(move |events| {
events.record(&event);
succeed(())
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::policy::{Denial, allow_all, deny_all, from_effect};
use crate::principal::BasicPrincipal;
use id_effect::{Cause, Exit};
fn ada() -> CurrentPrincipal {
CurrentPrincipal::new(BasicPrincipal::new("ada").with_tenant("acme"))
}
#[test]
fn events_describe_the_decision_and_serialise() {
let allowed = DecisionEvent::new(&ada(), "p", "Doc", Ok(&Decision::Allow));
assert!(allowed.is_allowed());
assert_eq!(allowed.tenant.as_deref(), Some("acme"));
assert_eq!(allowed.to_string(), "ada allowed by p on Doc");
let denied = DecisionEvent::new(
&CurrentPrincipal::anonymous(),
"p",
"Doc",
Ok(&Decision::Deny(Denial::new("no"))),
);
assert_eq!(denied.to_string(), "<anonymous> denied by p on Doc: no");
let failed = DecisionEvent::new(&ada(), "p", "Doc", Err(&AuthzError::Evaluation("x".into())));
assert_eq!(failed.outcome, Outcome::Error);
assert_eq!(
failed.to_string(),
"ada failed evaluating p on Doc: policy evaluation failed: x"
);
let json = serde_json::to_value(&denied).expect("serialise");
assert_eq!(json["outcome"], "deny");
assert_eq!(json["principal"], "");
assert_eq!(json["authenticated"], false);
let back: DecisionEvent = serde_json::from_value(json).expect("deserialise");
assert_eq!(back, denied);
}
#[test]
fn sinks_fan_out_and_memory_sink_buffers() {
let memory = MemorySink::new();
let seen = Arc::new(Mutex::new(0_usize));
let counter = Arc::clone(&seen);
let events = AuthzEvents::new(memory.clone())
.and(AuthzEvents::from_fn(move |_| *counter.lock().unwrap() += 1))
.and(AuthzEvents::none());
assert_eq!(events.sinks(), 2);
assert_eq!(format!("{events:?}"), "AuthzEvents { sinks: 2 }");
assert!(memory.is_empty());
events.record(&DecisionEvent::new(&ada(), "p", "r", Ok(&Decision::Allow)));
let emitted: Effect<(), (), HNil> =
events.emit(DecisionEvent::new(&ada(), "q", "r", Ok(&Decision::Allow)));
assert_eq!(emitted.block_on(), Exit::Success(()));
assert_eq!(memory.len(), 2);
assert_eq!(*seen.lock().unwrap(), 2);
assert_eq!(memory.take().len(), 2);
assert!(memory.events().is_empty());
}
#[test]
fn observed_policies_record_decisions_and_failures() {
let memory = MemorySink::new();
let events = AuthzEvents::new(memory.clone());
let yes = events.observe("yes", allow_all());
let no = events.observe("no", deny_all("closed"));
let broken = events.observe(
"broken",
from_effect(|_: &CurrentPrincipal, _: &u8| fail(AuthzError::Evaluation("db down".into()))),
);
assert_eq!(
yes.evaluate(&ada(), &()).block_on(),
Exit::Success(Decision::Allow)
);
assert!(matches!(
no.evaluate(&ada(), &()).block_on(),
Exit::Success(Decision::Deny(_))
));
assert_eq!(
broken.evaluate(&ada(), &7).block_on(),
Exit::Failure(Cause::Fail(AuthzError::Evaluation("db down".into())))
);
let recorded = memory.events();
let outcomes: Vec<_> = recorded
.iter()
.map(|e| (e.policy.as_str(), e.outcome))
.collect();
assert_eq!(
outcomes,
[
("yes", Outcome::Allow),
("no", Outcome::Deny),
("broken", Outcome::Error)
]
);
assert_eq!(recorded[2].resource, "u8");
assert!(format!("{yes:?}").contains("yes"));
}
#[test]
fn the_capability_is_read_from_the_environment() {
let memory = MemorySink::new();
type Env = HCons<AuthzEvents, HNil>;
let event = DecisionEvent::new(&ada(), "p", "r", Ok(&Decision::Allow));
let eff: Effect<(), (), Env> = emit_decision(event);
let run = eff.provide_layer(provide_authz_events(AuthzEvents::new(memory.clone())));
assert_eq!(run.block_on(), Exit::Success(()));
assert_eq!(memory.events()[0].policy, "p");
}
}