cedar_policy

Struct Schema

Source
pub struct Schema(/* private fields */);
Expand description

Object containing schema information used by the validator.

Implementations§

Source§

impl Schema

Source

pub fn from_schema_fragments( fragments: impl IntoIterator<Item = SchemaFragment>, ) -> Result<Self, SchemaError>

Create a Schema from multiple SchemaFragment. The individual fragments may reference entity or common types that are not declared in that fragment, but all referenced entity and common types must be declared in some fragment.

Source

pub fn from_json_value(json: Value) -> Result<Self, SchemaError>

Create a Schema from a JSON value (which should be an object of the shape required for the JSON schema format).

Source

pub fn from_json_str(json: &str) -> Result<Self, SchemaError>

Create a Schema from a string containing JSON in the appropriate shape.

Source

pub fn from_json_file(file: impl Read) -> Result<Self, SchemaError>

Create a Schema directly from a file containing JSON in the appropriate shape.

Source

pub fn from_cedarschema_file( file: impl Read, ) -> Result<(Self, impl Iterator<Item = SchemaWarning> + 'static), CedarSchemaError>

Parse the schema from a reader, in the Cedar schema format.

Source

pub fn from_cedarschema_str( src: &str, ) -> Result<(Self, impl Iterator<Item = SchemaWarning>), CedarSchemaError>

Parse the schema from a string, in the Cedar schema format.

Source

pub fn action_entities(&self) -> Result<Entities, EntitiesError>

Extract from the schema an Entities containing the action entities declared in the schema.

Source

pub fn principals(&self) -> impl Iterator<Item = &EntityTypeName>

Returns an iterator over every entity type that can be a principal for any action in this schema

Note: this iterator may contain duplicates.

§Examples

Here’s an example of using a std::collections::HashSet to get a de-duplicated set of principals

use std::collections::HashSet;
use cedar_policy::Schema;
let schema : Schema = r#"
    entity User;
    entity Folder;
    action Access appliesTo {
        principal : User,
        resource : Folder,
    };
    action Delete appliesTo {
        principal : User,
        resource : Folder,
    };
"#.parse().unwrap();
let principals = schema.principals().collect::<HashSet<_>>();
assert_eq!(principals, HashSet::from([&"User".parse().unwrap()]));
Source

pub fn resources(&self) -> impl Iterator<Item = &EntityTypeName>

Returns an iterator over every entity type that can be a resource for any action in this schema

Note: this iterator may contain duplicates.

§Examples

Here’s an example of using a std::collections::HashSet to get a de-duplicated set of resources

use std::collections::HashSet;
use cedar_policy::Schema;
let schema : Schema = r#"
    entity User;
    entity Folder;
    action Access appliesTo {
        principal : User,
        resource : Folder,
    };
    action Delete appliesTo {
        principal : User,
        resource : Folder,
    };
"#.parse().unwrap();
let resources = schema.resources().collect::<HashSet<_>>();
assert_eq!(resources, HashSet::from([&"Folder".parse().unwrap()]));
Source

pub fn principals_for_action( &self, action: &EntityUid, ) -> Option<impl Iterator<Item = &EntityTypeName>>

Returns an iterator over every entity type that can be a principal for action in this schema

§Errors

Returns None if action is not found in the schema

Source

pub fn resources_for_action( &self, action: &EntityUid, ) -> Option<impl Iterator<Item = &EntityTypeName>>

Returns an iterator over every entity type that can be a resource for action in this schema

§Errors

Returns None if action is not found in the schema

Source

pub fn ancestors<'a>( &'a self, ty: &'a EntityTypeName, ) -> Option<impl Iterator<Item = &EntityTypeName> + 'a>

Returns an iterator over all the entity types that can be an ancestor of ty

§Errors

Returns None if the ty is not found in the schema

Source

pub fn action_groups(&self) -> impl Iterator<Item = &EntityUid>

Returns an iterator over all the action groups defined in this schema

Source

pub fn entity_types(&self) -> impl Iterator<Item = &EntityTypeName>

Returns an iterator over all entity types defined in this schema

Source

pub fn actions(&self) -> impl Iterator<Item = &EntityUid>

Returns an iterator over all actions defined in this schema

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Schema

Source§

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

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

impl FromStr for Schema

Source§

fn from_str(schema_src: &str) -> Result<Self, Self::Err>

Construct a Schema from a string containing a schema formatted in the Cedar schema format. This can fail if it is not possible to parse a schema from the string, or if errors in values in the schema are uncovered after parsing. For instance, when an entity attribute name is found to not be a valid attribute name according to the Cedar grammar.

Source§

type Err = CedarSchemaError

The associated error which can be returned from parsing.
Source§

impl RefCast for Schema

Source§

type From = ValidatorSchema

Source§

fn ref_cast(_from: &Self::From) -> &Self

Source§

fn ref_cast_mut(_from: &mut Self::From) -> &mut Self

Source§

impl TryInto<Schema> for SchemaFragment

Source§

fn try_into(self) -> Result<Schema, Self::Error>

Convert SchemaFragment into a Schema. To build the Schema we need to have all entity types defined, so an error will be returned if any undeclared entity types are referenced in the schema fragment.

Source§

type Error = SchemaError

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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> 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> 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 = 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.