pub struct PolicySet { /* private fields */ }Expand description
Represents a set of Policys
Implementations§
Source§impl PolicySet
impl PolicySet
Sourcepub fn tpe<'a>(
&self,
request: &'a PartialRequest,
entities: &'a PartialEntities,
schema: &'a Schema,
) -> Result<TpeResponse<'a>, TpeError>
Available on crate feature tpe only.
pub fn tpe<'a>( &self, request: &'a PartialRequest, entities: &'a PartialEntities, schema: &'a Schema, ) -> Result<TpeResponse<'a>, TpeError>
tpe only.Perform type-aware partial evaluation on this PolicySet
If successful, the result is a PolicySet containing residual
policies ready for re-authorization
Available on crate feature tpe only.
tpe only.Like Authorizer::is_authorized but uses an EntityLoader to load
entities on demand.
Calls loader at most max_iters times, returning
early if an authorization result is reached.
Otherwise, it iterates max_iters times and returns
a partial result.
Sourcepub fn query_resource(
&self,
request: &ResourceQueryRequest,
entities: &Entities,
schema: &Schema,
) -> Result<impl Iterator<Item = EntityUid>, PermissionQueryError>
Available on crate feature tpe only.
pub fn query_resource( &self, request: &ResourceQueryRequest, entities: &Entities, schema: &Schema, ) -> Result<impl Iterator<Item = EntityUid>, PermissionQueryError>
tpe only.Perform a permission query on the resource
Sourcepub fn query_principal(
&self,
request: &PrincipalQueryRequest,
entities: &Entities,
schema: &Schema,
) -> Result<impl Iterator<Item = EntityUid>, PermissionQueryError>
Available on crate feature tpe only.
pub fn query_principal( &self, request: &PrincipalQueryRequest, entities: &Entities, schema: &Schema, ) -> Result<impl Iterator<Item = EntityUid>, PermissionQueryError>
tpe only.Perform a permission query on the principal
Sourcepub fn query_action<'a>(
&self,
request: &'a ActionQueryRequest,
entities: &PartialEntities,
) -> Result<impl Iterator<Item = (&'a EntityUid, Option<Decision>)>, PermissionQueryError>
Available on crate feature tpe only.
pub fn query_action<'a>( &self, request: &'a ActionQueryRequest, entities: &PartialEntities, ) -> Result<impl Iterator<Item = (&'a EntityUid, Option<Decision>)>, PermissionQueryError>
tpe only.Given a ActionQueryRequest (a partial request without a concrete
action) enumerate actions in the schema which might be authorized
for that request.
Each action is returned with a partial authorization decision. If
the action is definitely authorized, then it is Some(Decision::Allow).
If we did not reach a concrete authorization decision, then it is
None. Actions which are definitely not authorized (i.e., the
decision is Some(Decision::Deny)) are not returned by this
function. It is also possible that some actions without a concrete
authorization decision are never authorized if the residual
expressions after partial evaluation are not satisfiable.
If the partial request for a particular action is invalid (e.g., the action does not apply to the type of principal and resource), then that action is not included in the result regardless of whether a request with that action would be authorized.
// Construct a request for a concrete principal and resource, but leaving the context unknown so
// that we can see all actions that might be authorized for some context.
let request = ActionQueryRequest::new(
PartialEntityUid::from_concrete(r#"User::"alice""#.parse().unwrap()),
PartialEntityUid::from_concrete(r#"Photo::"vacation.jpg""#.parse().unwrap()),
None,
schema,
).unwrap();
// All actions which might be allowed for this principal and resource.
// The exact authorization result may depend on currently unknown
// context and entity data.
let possibly_allowed_actions: Vec<&EntityUid> =
policies.query_action(&request, &entities)
.unwrap()
.map(|(a, _)| a)
.collect();
// These actions are definitely allowed for this principal and resource.
// These will be allowed for _any_ context.
let allowed_actions: Vec<&EntityUid> =
policies.query_action(&request, &entities).unwrap()
.filter(|(_, resp)| resp == &Some(Decision::Allow))
.map(|(a, _)| a)
.collect();Source§impl PolicySet
impl PolicySet
Sourcepub fn from_json_str(src: impl AsRef<str>) -> Result<Self, PolicySetError>
pub fn from_json_str(src: impl AsRef<str>) -> Result<Self, PolicySetError>
Deserialize the PolicySet from a JSON string
Sourcepub fn from_json_value(src: Value) -> Result<Self, PolicySetError>
pub fn from_json_value(src: Value) -> Result<Self, PolicySetError>
Deserialize the PolicySet from a JSON value
Sourcepub fn from_json_file(r: impl Read) -> Result<Self, PolicySetError>
pub fn from_json_file(r: impl Read) -> Result<Self, PolicySetError>
Deserialize the PolicySet from a JSON reader
Sourcepub fn to_cedar(&self) -> Option<String>
pub fn to_cedar(&self) -> Option<String>
Get the human-readable Cedar syntax representation of this policy set. This function is primarily intended for rendering JSON policies in the human-readable syntax, but it will also return the original policy text (though possibly re-ordering policies within the policy set) when the policy-set contains policies parsed from the human-readable syntax.
This will return None if there are any linked policies in the policy
set because they cannot be directly rendered in Cedar syntax. It also
cannot record policy ids because these cannot be specified in the Cedar
syntax. The policies may be reordered, so parsing the resulting string
with PolicySet::from_str is likely to yield different policy id
assignments. For these reasons you should prefer serializing as JSON (or protobuf) and
only using this function to obtain a representation to display to human
users.
This function does not format the policy according to any particular
rules. Policy formatting can be done through the Cedar policy CLI or
the cedar-policy-formatter crate.
Sourcepub fn from_policies(
policies: impl IntoIterator<Item = Policy>,
) -> Result<Self, PolicySetError>
pub fn from_policies( policies: impl IntoIterator<Item = Policy>, ) -> Result<Self, PolicySetError>
Create a PolicySet from the given policies
Sourcepub fn merge(
&mut self,
other: &Self,
rename_duplicates: bool,
) -> Result<HashMap<PolicyId, PolicyId>, PolicySetError>
pub fn merge( &mut self, other: &Self, rename_duplicates: bool, ) -> Result<HashMap<PolicyId, PolicyId>, PolicySetError>
Merges this PolicySet with another PolicySet.
This PolicySet is modified while the other PolicySet
remains unchanged.
The flag rename_duplicates controls the expected behavior
when a PolicyId in this and the other PolicySet conflict.
When rename_duplicates is false, conflicting PolicyIds result
in a PolicySetError::AlreadyDefined error.
Otherwise, when rename_duplicates is true, conflicting PolicyIds from
the other PolicySet are automatically renamed to avoid conflict.
This renaming is returned as a Hashmap from the old PolicyId to the
renamed PolicyId.
Sourcepub fn add(&mut self, policy: Policy) -> Result<(), PolicySetError>
pub fn add(&mut self, policy: Policy) -> Result<(), PolicySetError>
Add an static policy to the PolicySet. To add a template instance, use
link instead. This function will return an error (and not modify
the PolicySet) if a template-linked policy is passed in.
Sourcepub fn remove_static(
&mut self,
policy_id: PolicyId,
) -> Result<Policy, PolicySetError>
pub fn remove_static( &mut self, policy_id: PolicyId, ) -> Result<Policy, PolicySetError>
Remove a static Policy from the PolicySet.
This will error if the policy is not a static policy.
Sourcepub fn add_template(&mut self, template: Template) -> Result<(), PolicySetError>
pub fn add_template(&mut self, template: Template) -> Result<(), PolicySetError>
Add a Template to the PolicySet
Sourcepub fn remove_template(
&mut self,
template_id: PolicyId,
) -> Result<Template, PolicySetError>
pub fn remove_template( &mut self, template_id: PolicyId, ) -> Result<Template, PolicySetError>
Remove a Template from the PolicySet.
This will error if any policy is linked to the template.
This will error if policy_id is not a template.
Sourcepub fn get_linked_policies(
&self,
template_id: PolicyId,
) -> Result<impl Iterator<Item = &PolicyId>, PolicySetError>
pub fn get_linked_policies( &self, template_id: PolicyId, ) -> Result<impl Iterator<Item = &PolicyId>, PolicySetError>
Get policies linked to a Template in the PolicySet.
If any policy is linked to the template, this will error
Sourcepub fn policies(&self) -> impl Iterator<Item = &Policy>
pub fn policies(&self) -> impl Iterator<Item = &Policy>
Iterate over all the Policys in the PolicySet.
This will include both static and template-linked policies.
Sourcepub fn templates(&self) -> impl Iterator<Item = &Template>
pub fn templates(&self) -> impl Iterator<Item = &Template>
Iterate over the Template’s in the PolicySet.
Sourcepub fn annotation(&self, id: &PolicyId, key: impl AsRef<str>) -> Option<&str>
pub fn annotation(&self, id: &PolicyId, key: impl AsRef<str>) -> Option<&str>
Extract annotation data from a Policy by its PolicyId and annotation key.
If the annotation is present without an explicit value (e.g., @annotation),
then this function returns Some(""). It returns None only when the
annotation is not present.
Sourcepub fn template_annotation(
&self,
id: &PolicyId,
key: impl AsRef<str>,
) -> Option<&str>
pub fn template_annotation( &self, id: &PolicyId, key: impl AsRef<str>, ) -> Option<&str>
Extract annotation data from a Template by its PolicyId and annotation key.
If the annotation is present without an explicit value (e.g., @annotation),
then this function returns Some(""). It returns None only when the
annotation is not present.
Sourcepub fn num_of_policies(&self) -> usize
pub fn num_of_policies(&self) -> usize
Returns the number of Policys in the PolicySet.
This will include both static and template-linked policies.
Sourcepub fn num_of_templates(&self) -> usize
pub fn num_of_templates(&self) -> usize
Returns the number of Templates in the PolicySet.
Sourcepub fn link(
&mut self,
template_id: PolicyId,
new_id: PolicyId,
vals: HashMap<SlotId, EntityUid>,
) -> Result<(), PolicySetError>
pub fn link( &mut self, template_id: PolicyId, new_id: PolicyId, vals: HashMap<SlotId, EntityUid>, ) -> Result<(), PolicySetError>
Attempt to link a template and add the new template-linked policy to the policy set.
If link fails, the PolicySet is not modified.
Failure can happen for three reasons
- The map passed in
valsmay not match the slots in the template - The
new_idmay conflict w/ a policy that already exists in the set template_iddoes not correspond to a template. Either the id is not in the policy set, or it is in the policy set but is either a linked or static policy rather than a template
Sourcepub fn unknown_entities(&self) -> HashSet<EntityUid>
Available on crate feature partial-eval only.
pub fn unknown_entities(&self) -> HashSet<EntityUid>
partial-eval only.Get all the unknown entities from the policy set
Trait Implementations§
Source§impl FromStr for PolicySet
impl FromStr for PolicySet
Source§fn from_str(policies: &str) -> Result<Self, Self::Err>
fn from_str(policies: &str) -> Result<Self, Self::Err>
Create a policy set from multiple statements.
Policy ids will default to “policy*” with numbers from 0. If you load more policies, do not use the default id, or there will be conflicts.
See Policy for more.
Source§type Err = ParseErrors
type Err = ParseErrors
impl Eq for PolicySet
Auto Trait Implementations§
impl Freeze for PolicySet
impl RefUnwindSafe for PolicySet
impl Send for PolicySet
impl Sync for PolicySet
impl Unpin for PolicySet
impl UnwindSafe for PolicySet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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