cedar_policy_core/pst/policy_set.rs
1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Policy set types for PST.
18
19use super::{EntityUID, PolicyID, SlotId, StaticPolicy, Template};
20
21use linked_hash_map::LinkedHashMap;
22use std::collections::HashMap;
23
24/// A collection of Cedar policies, templates, and template links.
25///
26/// Use [`PolicySet`] to group related policies for conversion to the
27/// authorization engine's internal representation via
28/// [`cedar_policy::PolicySet::from_pst()`](https://docs.rs/cedar-policy/latest/cedar_policy/struct.PolicySet.html#method.from_pst).
29///
30/// ```
31/// # use cedar_policy_core::pst::*;
32/// # use smol_str::SmolStr;
33/// let mut ps = PolicySet {
34/// templates: LinkedHashMap::new(),
35/// policies: LinkedHashMap::new(),
36/// template_links: vec![],
37/// };
38/// let template = Template::new(
39/// "p0", Effect::Permit,
40/// PrincipalConstraint::Any, ActionConstraint::Any, ResourceConstraint::Any,
41/// );
42/// ps.policies.insert(
43/// template.id.clone(),
44/// StaticPolicy::try_from(template).unwrap(),
45/// );
46/// ```
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct PolicySet {
49 /// Templates (policies with slots) keyed by template id.
50 pub templates: LinkedHashMap<PolicyID, Template>,
51 /// Static policies (no slots) keyed by policy id.
52 pub policies: LinkedHashMap<PolicyID, StaticPolicy>,
53 /// Links that instantiate templates with concrete entity UIDs.
54 pub template_links: Vec<TemplateLink>,
55}
56
57/// A link that instantiates a [`Template`] by filling its slots with concrete entity UIDs.
58///
59/// ```
60/// # use cedar_policy_core::pst::*;
61/// # use smol_str::SmolStr;
62/// # use std::collections::HashMap;
63/// let user = Name::unqualified("User").unwrap();
64/// let user_alice = EntityUID {
65/// ty: EntityType::from_name(user),
66/// eid: SmolStr::from("alice"),
67/// };
68/// let link = TemplateLink {
69/// template_id: PolicyID("template_123".into()),
70/// new_id: PolicyID("instance_0".into()),
71/// values: HashMap::from([(SlotId::Principal, user_alice)]),
72/// };
73/// ```
74#[derive(Debug, Clone, PartialEq, Eq)]
75pub struct TemplateLink {
76 /// Id of the template to instantiate.
77 pub template_id: PolicyID,
78 /// Id for the resulting linked policy.
79 pub new_id: PolicyID,
80 /// Slot values: maps each slot to a concrete entity UID.
81 pub values: HashMap<SlotId, EntityUID>,
82}