Struct Authorizer

Source
pub struct Authorizer { /* private fields */ }
Expand description

Stateful authorizer wrapping cedar’s engine.

Implementations§

Source§

impl Authorizer

Source

pub fn new(store: PolicyStore) -> Result<Self>

Build a new authorizer from a PolicyStore.

§Errors

Returns Error::Io / Error::Json / Error::PolicyParse from PolicyStore::load_policies or load_schema (e.g. malformed Cedar, missing schema).

Source

pub fn with_cache(self, config: CacheConfig) -> Self

Enable the decision cache with the supplied config. Call multiple times to replace the cache (the previous Arc is dropped).

§Examples
use agentguard_core::{Authorizer, PolicyStore};
use agentguard_core::decision::cache::CacheConfig;
use std::time::Duration;

let dir = tempfile::tempdir().unwrap();
let store = PolicyStore::open(dir.path()).unwrap();
let authorizer = Authorizer::new(store)
    .unwrap()
    .with_cache(CacheConfig {
        capacity: 1000,
        allow_ttl: Duration::from_secs(60),
        deny_ttl: Duration::from_secs(5),
        cache_denies: true,
    });
// Subsequent identical requests served within 60 s hit the
Source

pub fn try_with_cache(self, config: CacheConfig) -> Result<Self, String>

Enable the decision cache, returning invalid configuration as an error instead of panicking. Prefer this when config originates from user, operator, or embedder settings.

Source

pub fn cache(&self) -> Option<&Arc<DecisionCache>>

Direct handle on the cache (for invalidation, metrics, tests). Returns None when the cache is disabled.

Source

pub fn invalidate_cache(&self)

Invalidate every cached decision. Call after a policy reload — the next authorize() falls through to cedar and re-populates.

Source

pub fn sweep_stale_cache(&self) -> usize

Drain entries that became stale (policy-version mismatch) or expired since the last sweep. Cheap no-op when the cache is empty or nothing is stale. Returns the number of entries removed.

The read path no longer evicts on staleness to avoid serializing many concurrent readers on a write lock after a policy reload; this method is the matching cleanup hook.

Source

pub fn authorize( &self, req: &AgentRequest, entities: &Entities, ) -> Result<Decision>

Evaluate an AgentRequest against the loaded policy set.

When a decision cache is configured (with_cache), identical requests served within the configured TTL are returned from memory with from_cache: true.

§Errors

Returns Error::Json if the request cannot be serialized into the Cedar request shape (should be unreachable today).

§Examples
use agentguard_core::{Authorizer, AgentRequest, AgentRequestBuilder,
    Principal, AgentAction, Resource, AgentContext};
use cedar_policy::Entities;

let dir = tempfile::tempdir().unwrap();
let store = agentguard_core::PolicyStore::open(dir.path()).unwrap();
store
    .write_policy(
        "p",
        r#"permit (principal in User::"alice", action, resource);"#,
    )
    .unwrap();
let authorizer = Authorizer::new(store).unwrap();
let req = AgentRequestBuilder::new(Principal::user("alice"))
    .action(AgentAction::tool("send"))
    .resource(Resource::new("Mailbox", "alice@acme"))
    .context(AgentContext::new())
    .build()
    .unwrap();
let decision = authorizer
    .authorize(&req, &Entities::empty())
    .unwrap();
assert_eq!(decision.effect, agentguard_core::authorize::Effect::Allow);
Source

pub fn schema(&self) -> Option<&Schema>

Source

pub fn store(&self) -> &PolicyStore

Source

pub fn policies(&self) -> &PolicySet

Source

pub fn policy_count(&self) -> usize

Number of policies in the loaded set. O(1) for the PolicySet length (cached). Used by /readyz to check that policies are loaded without iterating the full set.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,