Struct cedar_policy::Policy
source · pub struct Policy { /* private fields */ }
Expand description
Structure for a Policy
. Includes both static policies and template-linked policies.
Implementations§
source§impl Policy
impl Policy
sourcepub fn template_id(&self) -> Option<&PolicyId>
pub fn template_id(&self) -> Option<&PolicyId>
Get the PolicyId
of the Template
this is linked to.
If this is a static policy, this will return None
.
sourcepub fn annotation(&self, key: impl AsRef<str>) -> Option<&str>
pub fn annotation(&self, key: impl AsRef<str>) -> Option<&str>
Get an annotation value of this template-linked or static policy
sourcepub fn annotations(&self) -> impl Iterator<Item = (&str, &str)>
pub fn annotations(&self) -> impl Iterator<Item = (&str, &str)>
Iterate through annotation data of this template-linked or static policy
sourcepub fn principal_constraint(&self) -> PrincipalConstraint
pub fn principal_constraint(&self) -> PrincipalConstraint
Get the head constraint on this policy’s principal
sourcepub fn action_constraint(&self) -> ActionConstraint
pub fn action_constraint(&self) -> ActionConstraint
Get the head constraint on this policy’s action
sourcepub fn resource_constraint(&self) -> ResourceConstraint
pub fn resource_constraint(&self) -> ResourceConstraint
Get the head constraint on this policy’s resource
sourcepub fn parse(
id: Option<String>,
policy_src: impl AsRef<str>,
) -> Result<Self, ParseErrors>
pub fn parse( id: Option<String>, policy_src: impl AsRef<str>, ) -> Result<Self, ParseErrors>
Parse a single policy.
If id
is Some, the policy will be given that Policy Id.
If id
is None, then “policy0” will be used.
The behavior around None may change in the future.
sourcepub fn from_json(
id: Option<PolicyId>,
json: Value,
) -> Result<Self, EstToAstError>
pub fn from_json( id: Option<PolicyId>, json: Value, ) -> Result<Self, EstToAstError>
Create a Policy
from its JSON representation.
If id
is Some, the policy will be given that Policy Id.
If id
is None, then “JSON policy” will be used.
The behavior around None may change in the future.
use cedar_policy::{Policy, PolicyId};
use std::str::FromStr;
let data : serde_json::Value = serde_json::json!(
{
"effect":"permit",
"principal":{
"op":"==",
"entity":{
"type":"User",
"id":"bob"
}
},
"action":{
"op":"==",
"entity":{
"type":"Action",
"id":"view"
}
},
"resource":{
"op":"==",
"entity":{
"type":"Album",
"id":"trip"
}
},
"conditions":[
{
"kind":"when",
"body":{
">":{
"left":{
".":{
"left":{
"Var":"principal"
},
"attr":"age"
}
},
"right":{
"Value":18
}
}
}
}
]
}
);
let policy = Policy::from_json(None, data).unwrap();
let src = r#"
permit(
principal == User::"bob",
action == Action::"view",
resource == Album::"trip"
)
when { principal.age > 18 };"#;
let expected_output = Policy::parse(None, src).unwrap();
assert_eq!(policy.to_string(), expected_output.to_string());
sourcepub fn to_json(&self) -> Result<Value, impl Error>
pub fn to_json(&self) -> Result<Value, impl Error>
Get the JSON representation of this Policy
.
use cedar_policy::Policy;
let src = r#"
permit(
principal == User::"bob",
action == Action::"view",
resource == Album::"trip"
)
when { principal.age > 18 };"#;
let policy = Policy::parse(None, src).unwrap();
println!("{}", policy);
// convert the policy to JSON
let json = policy.to_json().unwrap();
println!("{}", json);
assert_eq!(policy.to_string(), Policy::from_json(None, json).unwrap().to_string());
Trait Implementations§
source§impl FromStr for Policy
impl FromStr for Policy
source§fn from_str(policy: &str) -> Result<Self, Self::Err>
fn from_str(policy: &str) -> Result<Self, Self::Err>
Create a policy
Important note: Policies have ids, but this interface does not
allow them to be set. It will use the default “policy0”, which
may cause id conflicts if not handled. Use Policy::parse
to set
the id when parsing, or Policy::new_id
to clone a policy with
a new id.
source§type Err = ParseErrors
type Err = ParseErrors
impl Eq for Policy
Auto Trait Implementations§
impl Freeze for Policy
impl RefUnwindSafe for Policy
impl Send for Policy
impl Sync for Policy
impl Unpin for Policy
impl UnwindSafe for Policy
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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