cedar_policy_core/est/
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 crate::ast;
18use crate::entities::json::err::JsonDeserializationError;
19use crate::parser::err::{parse_errors, ParseErrors};
20use crate::parser::unescape;
21use miette::Diagnostic;
22use nonempty::NonEmpty;
23use smol_str::SmolStr;
24use thiserror::Error;
25
26/// Errors arising while converting a policy from its JSON representation (aka EST) into an AST
27#[derive(Debug, Diagnostic, Error)]
28pub enum FromJsonError {
29    /// Error while deserializing JSON
30    #[error(transparent)]
31    #[diagnostic(transparent)]
32    JsonDeserializationError(#[from] JsonDeserializationError),
33    /// Tried to convert an EST representing a template to an AST representing a static policy
34    #[error(transparent)]
35    #[diagnostic(transparent)]
36    TemplateToPolicy(#[from] parse_errors::ExpectedStaticPolicy),
37    /// Tried to convert an EST representing a static policy to an AST representing a template
38    #[error(transparent)]
39    #[diagnostic(transparent)]
40    PolicyToTemplate(#[from] parse_errors::ExpectedTemplate),
41    /// Slot name was not valid for the position it was used in. (Currently, principal slots must
42    /// be named `?principal`, and resource slots must be named `?resource`.)
43    #[error("invalid slot name or slot used in wrong position")]
44    #[diagnostic(help(
45        "principal slots must be named `?principal` and resource slots must be named `?resource`"
46    ))]
47    InvalidSlotName,
48    /// EST contained a template slot for `action`. This is not currently allowed
49    #[error("slots are not allowed for actions")]
50    ActionSlot,
51    /// EST contained a template slot in policy condition
52    #[error(transparent)]
53    #[diagnostic(transparent)]
54    SlotsInConditionClause(#[from] parse_errors::SlotsInConditionClause),
55    /// EST contained the empty JSON object `{}` where a key (operator) was expected
56    #[error("missing operator, found empty object")]
57    MissingOperator,
58    /// EST contained an object with multiple keys (operators) where a single operator was expected
59    #[error("found multiple operators where one was expected: {ops:?}")]
60    MultipleOperators {
61        /// the multiple operators that were found where one was expected
62        ops: Vec<SmolStr>,
63    },
64    /// Error thrown while processing string escapes
65    // show just the first error in the main error message, like in [`ParseErrors`]; see #326 and discussion on #477
66    #[error("{}", .0.first())]
67    UnescapeError(#[related] NonEmpty<unescape::UnescapeError>),
68    /// Error reported when the entity type tested by an `is` expression cannot be parsed.
69    #[error("invalid entity type: {0}")]
70    #[diagnostic(transparent)]
71    InvalidEntityType(ParseErrors),
72    /// Error reported when the extension function name is unknown. Note that
73    /// unlike the Cedar policy format, the JSON format has no way to distinguish
74    /// between function-style and method-style calls.
75    #[error("invalid extension function: `{0}`")]
76    UnknownExtensionFunction(ast::Name),
77    /// Returned when an entity uid used as an action does not have the type `Action`
78    #[error(transparent)]
79    #[diagnostic(transparent)]
80    InvalidActionType(#[from] parse_errors::InvalidActionType),
81}
82
83/// Errors arising while converting a policy set from its JSON representation (aka EST) into an AST
84#[derive(Debug, Diagnostic, Error)]
85pub enum PolicySetFromJsonError {
86    /// Error reported when a policy set has duplicate ids
87    #[error(transparent)]
88    #[diagnostic(transparent)]
89    PolicySet(#[from] ast::PolicySetError),
90    /// Error reported when attempting to create a template-link
91    #[error(transparent)]
92    #[diagnostic(transparent)]
93    Linking(#[from] ast::LinkingError),
94    /// Error reported when converting an EST policy or template to an AST
95    #[error(transparent)]
96    #[diagnostic(transparent)]
97    FromJsonError(#[from] FromJsonError),
98}
99
100/// Errors while linking a policy
101#[derive(Debug, PartialEq, Diagnostic, Error)]
102pub enum LinkingError {
103    /// Template contains this slot, but a value wasn't provided for it
104    #[error("failed to link template: no value provided for `{slot}`")]
105    MissedSlot {
106        /// Slot which didn't have a value provided for it
107        slot: ast::SlotId,
108    },
109}
110
111impl From<ast::UnexpectedSlotError> for FromJsonError {
112    fn from(err: ast::UnexpectedSlotError) -> Self {
113        Self::TemplateToPolicy(err.into())
114    }
115}