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