cedar_policy_core/entities/json/
schema.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
17use super::SchemaType;
18use crate::ast::{Entity, EntityType, EntityUID};
19use crate::entities::{Name, UnreservedId};
20use smol_str::SmolStr;
21use std::collections::{HashMap, HashSet};
22use std::sync::Arc;
23
24/// Trait for `Schema`s that can inform the parsing of Entity JSON data
25pub trait Schema {
26    /// Type returned by `entity_type()`. Must implement the `EntityTypeDescription` trait
27    type EntityTypeDescription: EntityTypeDescription;
28
29    /// Type returned by `action_entities()`
30    type ActionEntityIterator: IntoIterator<Item = Arc<Entity>>;
31
32    /// Get an `EntityTypeDescription` for the given entity type, or `None` if that
33    /// entity type is not declared in the schema (in which case entities of that
34    /// type should not appear in the JSON data).
35    fn entity_type(&self, entity_type: &EntityType) -> Option<Self::EntityTypeDescription>;
36
37    /// Get the entity information for the given action, or `None` if that
38    /// action is not declared in the schema (in which case this action should
39    /// not appear in the JSON data).
40    fn action(&self, action: &EntityUID) -> Option<Arc<Entity>>;
41
42    /// Get the names of all entity types declared in the schema that have the
43    /// given basename (in the sense of `Name::basename()`).
44    fn entity_types_with_basename<'a>(
45        &'a self,
46        basename: &'a UnreservedId,
47    ) -> Box<dyn Iterator<Item = EntityType> + 'a>;
48
49    /// Get all the actions declared in the schema
50    fn action_entities(&self) -> Self::ActionEntityIterator;
51}
52
53/// Simple type that implements `Schema` by expecting no entities to exist at all
54#[derive(Debug, Clone)]
55pub struct NoEntitiesSchema;
56impl Schema for NoEntitiesSchema {
57    type EntityTypeDescription = NullEntityTypeDescription;
58    type ActionEntityIterator = std::iter::Empty<Arc<Entity>>;
59    fn entity_type(&self, _entity_type: &EntityType) -> Option<NullEntityTypeDescription> {
60        None
61    }
62    fn action(&self, _action: &EntityUID) -> Option<Arc<Entity>> {
63        None
64    }
65    fn entity_types_with_basename<'a>(
66        &'a self,
67        _basename: &'a UnreservedId,
68    ) -> Box<dyn Iterator<Item = EntityType> + 'a> {
69        Box::new(std::iter::empty())
70    }
71    fn action_entities(&self) -> std::iter::Empty<Arc<Entity>> {
72        std::iter::empty()
73    }
74}
75
76/// Simple type that implements `Schema` by allowing entities of all types to
77/// exist, and allowing all actions to exist, but expecting no attributes or
78/// parents on any entity (action or otherwise).
79///
80/// This type returns an empty iterator for `action_entities()`, which is kind
81/// of inconsistent with its behavior on `action()`. But it works out -- the
82/// result is that, in `EntityJsonParser`, all actions encountered in JSON data
83/// are allowed to exist without error, but no additional actions from the
84/// schema are added.
85#[derive(Debug, Clone)]
86pub struct AllEntitiesNoAttrsSchema;
87impl Schema for AllEntitiesNoAttrsSchema {
88    type EntityTypeDescription = NullEntityTypeDescription;
89    type ActionEntityIterator = std::iter::Empty<Arc<Entity>>;
90    fn entity_type(&self, entity_type: &EntityType) -> Option<NullEntityTypeDescription> {
91        Some(NullEntityTypeDescription {
92            ty: entity_type.clone(),
93        })
94    }
95    fn action(&self, action: &EntityUID) -> Option<Arc<Entity>> {
96        Some(Arc::new(Entity::new_with_attr_partial_value(
97            action.clone(),
98            HashMap::new(),
99            HashSet::new(),
100        )))
101    }
102    fn entity_types_with_basename<'a>(
103        &'a self,
104        basename: &'a UnreservedId,
105    ) -> Box<dyn Iterator<Item = EntityType> + 'a> {
106        Box::new(std::iter::once(EntityType::from(Name::unqualified_name(
107            basename.clone(),
108        ))))
109    }
110    fn action_entities(&self) -> std::iter::Empty<Arc<Entity>> {
111        std::iter::empty()
112    }
113}
114
115/// Trait for a schema's description of an individual entity type
116pub trait EntityTypeDescription {
117    /// Get the `EntityType` this `EntityTypeDescription` is describing
118    fn entity_type(&self) -> EntityType;
119
120    /// Do entities of this type have the given attribute, and if so, what type?
121    ///
122    /// Returning `None` indicates that attribute should not exist.
123    fn attr_type(&self, attr: &str) -> Option<SchemaType>;
124
125    /// Get the names of all the required attributes for this entity type.
126    fn required_attrs<'s>(&'s self) -> Box<dyn Iterator<Item = SmolStr> + 's>;
127
128    /// Get the entity types which are allowed to be parents of this entity type.
129    fn allowed_parent_types(&self) -> Arc<HashSet<EntityType>>;
130
131    /// May entities with this type have attributes other than those specified
132    /// in the schema
133    fn open_attributes(&self) -> bool;
134}
135
136/// Simple type that implements `EntityTypeDescription` by expecting no
137/// attributes to exist
138#[derive(Debug, Clone)]
139pub struct NullEntityTypeDescription {
140    /// null description for this type
141    ty: EntityType,
142}
143impl EntityTypeDescription for NullEntityTypeDescription {
144    fn entity_type(&self) -> EntityType {
145        self.ty.clone()
146    }
147    fn attr_type(&self, _attr: &str) -> Option<SchemaType> {
148        None
149    }
150    fn required_attrs(&self) -> Box<dyn Iterator<Item = SmolStr>> {
151        Box::new(std::iter::empty())
152    }
153    fn allowed_parent_types(&self) -> Arc<HashSet<EntityType>> {
154        Arc::new(HashSet::new())
155    }
156    fn open_attributes(&self) -> bool {
157        false
158    }
159}