Skip to main content

Policy

Struct Policy 

Source
pub struct Policy {
    pub rules: Vec<Rule>,
}
Expand description

An ordered list of Rules, evaluated top-down with first match wins; when nothing matches, the default decision is allow-read / deny-everything-else (authorization-attributes.md §5).

Pure data: load once at boot (from <ENTITY>_ABAC_POLICY / <ENTITY>_ABAC_POLICY_FILE via Policy::from_json, falling back to Policy::default_policy on parse failure), then call Policy::evaluate per request. Unknown JSON fields are ignored (see Rule).

Fields§

§rules: Vec<Rule>

The ordered rules; index order is evaluation order.

Implementations§

Source§

impl Policy

Source

pub fn from_json(json: &str) -> Result<Self, Error>

Parse a policy from its JSON document form ({ "rules": [ { "effect": ..., "actions": [...], "when": {...} }, ... ] }).

§Errors

Returns the underlying serde_json::Error when the document is not valid JSON or does not fit the policy shape (unknown effect / action names, non-list when values, …). Callers fall back to Policy::default_policy — a bad policy must never take a service down at boot.

Source

pub fn default_policy() -> Self

The built-in default policy (authorization-attributes.md §5), used when no policy is configured:

  1. svc=true ⇒ allow write + destructive (machine peers may do everything; read is default-allowed anyway),
  2. access=admin ⇒ allow destructive (which covers delete),
  3. access=write or access=admin ⇒ allow write.

Everything else falls through to the default decision: read allowed, mutation denied.

Source

pub fn evaluate( &self, claims: &Claims, action: Action, entity: &str, ) -> Decision

Evaluate the policy for one request: the verified subject (Claims, whose attrs carry the subject attributes), the derived Action, and the resource entity (the crate’s entity type, e.g. "place"; reserved for resource rules — a when key entity matches against it).

Considers no record-level resource attributes — every resource.* when key resolves empty. This is the coarse, no-record-load path the blanket guards use; a service that has loaded the target record calls Policy::evaluate_with_resource instead (handler-level record checks).

Rules are checked top-down; the first matching rule decides. When no rule matches, the default decision applies: Action::Read is allowed, every other action is denied.

Pure and total: no I/O, and no input — claims, attrs, policy, or entity — can make it panic.

Source

pub fn evaluate_with_resource( &self, claims: &Claims, action: Action, entity: &str, resource: &BTreeMap<String, Vec<String>>, ) -> Decision

Evaluate the policy with record-level resource attributes — a string→strings map describing the specific target record (e.g. {"sensitivity": ["high"]}), matched by when keys prefixed resource. (resource.sensitivity). Otherwise identical to Policy::evaluate: same subject attributes, same action, same entity, same first-match-wins ordering and default decision.

A service derives the resource attributes from the record it just loaded (handler-level, after fetch) and passes them here, so a deployment can express e.g. “deny write on a high-sensitivity record unless access=admin” as ordered policy rules (authorization-attributes.md §9). The resource. namespace is disjoint from subject attributes, so a caller can never spoof a resource attribute through its token. Considers no environment attributes (every env.* key resolves empty) — call Policy::evaluate_with_context to pass those too.

Pure and total: no I/O, no panics on any input.

Source

pub fn evaluate_with_context( &self, claims: &Claims, action: Action, entity: &str, resource: &BTreeMap<String, Vec<String>>, env: &BTreeMap<String, Vec<String>>, ) -> Decision

Evaluate the policy with both record-level resource attributes (matched by resource.* when keys) and environment attributes (matched by env.* keys) — a string→strings map of request-time / network context, e.g. {"hour": ["22"], "network": ["office"]}. Otherwise identical to Policy::evaluate.

A service derives the environment attributes per request (clock, source IP, …) and passes them here so a deployment can express e.g. “deny write outside working hours” or geofencing as ordered policy rules (authorization-attributes.md §10). Like resource.*, the env.* namespace is disjoint from subject attributes, so a caller cannot spoof it through its token.

Pure and total: no I/O (the caller supplies the clock/network, keeping the engine deterministic), no panics on any input.

Trait Implementations§

Source§

impl Clone for Policy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Policy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Policy

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Policy

Source§

impl PartialEq for Policy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Policy

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Policy

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.