pub struct Template {
pub id: PolicyID,
pub effect: Effect,
pub principal: PrincipalConstraint,
pub action: ActionConstraint,
pub resource: ResourceConstraint,
pub annotations: BTreeMap<String, SmolStr>,
/* private fields */
}Expand description
A Cedar policy template.
Represents a complete Cedar policy template including its scope constraints, condition clauses, and annotations. If there are no slots used, this is effectively a policy.
For example:
@id("policy0")
permit (
principal == User::"alice",
action == Action::"view",
resource in Album::"vacation"
)
when { resource.public == true }
unless { context.is_blocked };Is the following Template:
let user_alice = EntityUID {
ty: EntityType::from_name(Name::unqualified("User").unwrap()),
eid: SmolStr::from("alice"),
};
let action_view = EntityUID {
ty: EntityType::from_name(Name::unqualified("Action").unwrap()),
eid: SmolStr::from("view"),
};
let album_vacation = EntityUID {
ty: EntityType::from_name(Name::unqualified("Album").unwrap()),
eid: SmolStr::from("vacation"),
};
let template = Template::new(
PolicyID(SmolStr::from("policy0")),
Effect::Permit,
PrincipalConstraint::Eq(EntityOrSlot::Entity(user_alice)),
ActionConstraint::Eq(action_view),
ResourceConstraint::In(EntityOrSlot::Entity(album_vacation)),
)
.try_with_clauses(vec![
Clause::When(Arc::new(Expr::BinaryOp {
op: BinaryOp::Eq,
left: Arc::new(Expr::GetAttr {
expr: Arc::new(Expr::Var(Var::Resource)),
attr: SmolStr::from("public"),
}),
right: Arc::new(Expr::Literal(Literal::Bool(true))),
})),
Clause::Unless(Arc::new(Expr::GetAttr {
expr: Arc::new(Expr::Var(Var::Context)),
attr: SmolStr::from("is_blocked"),
})),
])
.unwrap()
.with_annotations(BTreeMap::from([
("id".to_string(), SmolStr::from("policy0")),
]));Fields§
§id: PolicyIDTemplate ID
effect: EffectPermit or forbid
principal: PrincipalConstraintPrincipal constraint
action: ActionConstraintAction constraint
resource: ResourceConstraintResource constraint
annotations: BTreeMap<String, SmolStr>Annotations (empty string for no value)
Implementations§
Source§impl Template
impl Template
Sourcepub fn new(
id: impl Into<PolicyID>,
effect: Effect,
principal: PrincipalConstraint,
action: ActionConstraint,
resource: ResourceConstraint,
) -> Template
pub fn new( id: impl Into<PolicyID>, effect: Effect, principal: PrincipalConstraint, action: ActionConstraint, resource: ResourceConstraint, ) -> Template
Create a new policy with the given id, effect and scope. Constraints need to be added with try_with_clauses (or try_add_clause)
Sourcepub fn into_clauses(self) -> Vec<Clause>
pub fn into_clauses(self) -> Vec<Clause>
Get the clauses of the policy
Sourcepub fn try_with_clauses(
self,
clauses: impl IntoIterator<Item = Clause>,
) -> Result<Template, PstConstructionError>
pub fn try_with_clauses( self, clauses: impl IntoIterator<Item = Clause>, ) -> Result<Template, PstConstructionError>
Replace all clauses on this template. Fails if any clause contains a slot or unknown.
Sourcepub fn try_add_clause(
&mut self,
clause: Clause,
) -> Result<(), PstConstructionError>
pub fn try_add_clause( &mut self, clause: Clause, ) -> Result<(), PstConstructionError>
Append a single clause to this template. Fails if the clause contains a slot or unknown.
Sourcepub fn with_annotations(
self,
annotations: BTreeMap<String, SmolStr>,
) -> Template
pub fn with_annotations( self, annotations: BTreeMap<String, SmolStr>, ) -> Template
Set the annotations on this template, replacing any existing annotations.
Sourcepub fn link(
self,
vals: &HashMap<SlotId, EntityUID>,
) -> Result<StaticPolicy, PstConstructionError>
pub fn link( self, vals: &HashMap<SlotId, EntityUID>, ) -> Result<StaticPolicy, PstConstructionError>
Fill in any slots in this policy using the values in vals.
Performing the link operation should result in a StaticPolicy.
If there are unfilled slots, this results in an Error.
Trait Implementations§
Source§impl TryFrom<Template> for StaticPolicy
impl TryFrom<Template> for StaticPolicy
Source§type Error = ContainsSlotError
type Error = ContainsSlotError
Source§fn try_from(
body: Template,
) -> Result<StaticPolicy, <StaticPolicy as TryFrom<Template>>::Error>
fn try_from( body: Template, ) -> Result<StaticPolicy, <StaticPolicy as TryFrom<Template>>::Error>
impl Eq for Template
impl StructuralPartialEq for Template
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnsafeUnpin for Template
impl UnwindSafe for Template
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