Skip to main content

cedar_policy_core/entities/
err.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::EntityUID;
18use crate::transitive_closure;
19use miette::Diagnostic;
20use thiserror::Error;
21
22/// Errors in serializing, deserializing, and processing of Entities
23//
24// CAUTION: this type is publicly exported in `cedar-policy`.
25// Don't make fields `pub`, don't make breaking changes, and use caution
26// when adding public methods.
27#[derive(Debug, Diagnostic, Error)]
28pub enum EntitiesError {
29    /// Error occurring in serialization of entities
30    #[error("error during entity serialization: {0}")]
31    #[diagnostic(transparent)]
32    Serialization(#[from] crate::entities::json::err::JsonSerializationError),
33    /// Error occurring in deserialization of entities
34    #[error("error during entity deserialization")]
35    #[diagnostic(transparent)]
36    Deserialization(#[from] crate::entities::json::err::JsonDeserializationError),
37    /// Error constructing the Entities collection due to encountering two different entities
38    /// with the same Entity UID
39    #[error(transparent)]
40    #[diagnostic(transparent)]
41    Duplicate(Duplicate),
42    /// Errors occurring while computing or enforcing transitive closure on the
43    /// entity hierarchy.
44    #[error("transitive closure computation/enforcement error")]
45    #[diagnostic(transparent)]
46    TransitiveClosureError(#[from] TransitiveClosureError),
47    /// Error because an entity doesn't conform to the schema
48    #[error("entity does not conform to the schema")]
49    #[diagnostic(transparent)]
50    InvalidEntity(#[from] crate::entities::conformance::err::EntitySchemaConformanceError),
51    /// Error because an entity is not well-formed
52    #[error(transparent)]
53    #[diagnostic(transparent)]
54    InvalidEntityStructure(#[from] InvalidEntityStructureError),
55}
56
57impl EntitiesError {
58    pub(crate) fn duplicate(euid: EntityUID) -> Self {
59        Self::Duplicate(Duplicate { euid })
60    }
61}
62
63impl From<transitive_closure::TcError<EntityUID>> for EntitiesError {
64    fn from(value: transitive_closure::TcError<EntityUID>) -> Self {
65        let tc: TransitiveClosureError = value.into();
66        tc.into()
67    }
68}
69
70/// Errors occurring while computing or enforcing transitive closure on the
71/// entity hierarchy.
72//
73// CAUTION: this type is publicly exported in `cedar-policy`.
74// Don't make fields `pub`, don't make breaking changes, and use caution
75// when adding public methods.
76#[derive(Debug, Error, Diagnostic)]
77#[error(transparent)]
78#[diagnostic(transparent)]
79pub struct TransitiveClosureError {
80    err: Box<transitive_closure::TcError<EntityUID>>,
81}
82
83#[cfg(test)]
84impl TransitiveClosureError {
85    pub(crate) fn inner(&self) -> &transitive_closure::TcError<EntityUID> {
86        self.err.as_ref()
87    }
88}
89
90impl From<transitive_closure::TcError<EntityUID>> for TransitiveClosureError {
91    fn from(v: transitive_closure::TcError<EntityUID>) -> Self {
92        Self { err: Box::new(v) }
93    }
94}
95
96/// Error type for entity sets containing duplicate entity uids
97//
98// CAUTION: this type is publicly exported in `cedar-policy`.
99// Don't make fields `pub`, don't make breaking changes, and use caution
100// when adding public methods.
101#[derive(Debug, PartialEq, Eq, Error, Diagnostic)]
102#[error("duplicate entity entry `{}`", .euid)]
103pub struct Duplicate {
104    /// The [`EntityUID`] that was duplicated
105    pub(crate) euid: EntityUID,
106}
107
108#[cfg(test)]
109impl Duplicate {
110    pub(crate) fn euid(&self) -> &EntityUID {
111        &self.euid
112    }
113}
114
115/// Type alias for convenience
116pub type Result<T> = std::result::Result<T, EntitiesError>;
117
118/// Error type when an entity is not well formed used in entity validation.
119#[derive(Debug, Error, Diagnostic)]
120pub enum InvalidEntityStructureError {
121    /// Entity has itself as an ancestor
122    #[error("entity `{uid}` has itself as an ancestor")]
123    SelfAncestor {
124        /// The entity UID
125        uid: EntityUID,
126    },
127    /// Parents and indirect ancestors overlap
128    #[error("entity `{uid}` has `{ancestor}` in both parents and indirect ancestors")]
129    DuplicateAncestor {
130        /// The entity UID
131        uid: EntityUID,
132        /// The ancestor that appears in both sets
133        ancestor: EntityUID,
134    },
135    /// Action entity has a non-action parent
136    #[error("action `{uid}` has a non-action parent `{parent}`")]
137    ActionParentIsNotAction {
138        /// The action entity UID
139        uid: EntityUID,
140        /// The non-action parent
141        parent: EntityUID,
142    },
143}