cedar-policy-core 4.10.0

Core implementation of the Cedar policy language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 * Copyright Cedar Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Error types for PST (Public Syntax Tree) construction and conversion.
//!
//! This module defines errors that can occur when:
//! - Programmatically constructing PST expressions, policies, and constraints
//! - Converting between PST and other representations (EST, AST)
//! - Validating PST structure and semantics

use std::collections::HashSet;

use crate::extensions::ExtensionFunctionLookupError;
use crate::pst;
use miette::Diagnostic;
use smol_str::ToSmolStr;
use thiserror::Error;

use crate::ast;
use crate::est;

/// Errors that can occur during PST construction or conversion
#[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
#[non_exhaustive]
pub enum PstConstructionError {
    /// Trying to construct a policy from an empty representation of another type
    #[error(transparent)]
    #[diagnostic(transparent)]
    PolicyFromEmptyRepresentation(#[from] error_body::PolicyFromEmptyRepresentationError),

    /// A policy is a linked policy but no link id has been provided
    #[error(transparent)]
    #[diagnostic(transparent)]
    PolicyMissingLinkId(#[from] error_body::PolicyMissingLinkIdError),

    /// Action constraints cannot contain template slots
    #[error(transparent)]
    #[diagnostic(transparent)]
    ActionConstraintCannotHaveSlots(#[from] error_body::ActionConstraintCannotHaveSlotsError),

    /// Expected a template with slots, but found a static policy
    #[error(transparent)]
    #[diagnostic(transparent)]
    ExpectedTemplateWithSlots(#[from] error_body::ExpectedTemplateWithSlotsError),

    /// Slot occurs in the wrong position (e.g., principal slot in resource)
    #[error(transparent)]
    #[diagnostic(transparent)]
    WrongSlotPosition(#[from] error_body::WrongSlotPositionError),

    /// Duplicate key found in a record literal
    #[error(transparent)]
    #[diagnostic(transparent)]
    DuplicateRecordKey(#[from] error_body::DuplicateRecordKeyError),

    /// Invalid annotation in a policy or template
    #[error(transparent)]
    #[diagnostic(transparent)]
    InvalidAnnotation(#[from] error_body::InvalidAnnotationError),

    /// Invalid entity UID format or structure
    #[error(transparent)]
    #[diagnostic(transparent)]
    InvalidEntityUid(#[from] error_body::InvalidEntityUidError),

    /// Invalid entity type name
    #[error(transparent)]
    #[diagnostic(transparent)]
    InvalidEntityType(#[from] error_body::InvalidEntityTypeError),

    /// A generic invalid expression error with a description
    #[error(transparent)]
    #[diagnostic(transparent)]
    InvalidExpression(#[from] error_body::InvalidExpressionError),

    /// Unknown function name (not a built-in or registered extension function)
    #[error(transparent)]
    #[diagnostic(transparent)]
    UnknownFunction(#[from] error_body::UnknownFunctionError),

    /// Function called with wrong number of arguments
    #[error(transparent)]
    #[diagnostic(transparent)]
    WrongArity(#[from] error_body::WrongArityError),

    /// Error nodes from parsing are not supported in PST conversion
    #[error(transparent)]
    #[diagnostic(transparent)]
    UnsupportedErrorNode(#[from] error_body::UnsupportedErrorNode),

    /// A parsing error occurred, usually in names
    #[error(transparent)]
    #[diagnostic(transparent)]
    ParsingFailed(#[from] error_body::ParsingFailedError),

    /// A linking error occurred.
    #[error(transparent)]
    #[diagnostic(transparent)]
    LinkingFailed(#[from] error_body::LinkingError),

    /// Contains unexpected slots
    #[error(transparent)]
    #[diagnostic(transparent)]
    ContainsSlots(#[from] error_body::ContainsSlotError),
}

#[doc(hidden)]
impl From<est::FromJsonError> for PstConstructionError {
    fn from(err: est::FromJsonError) -> Self {
        match err {
            est::FromJsonError::UnknownExtensionFunction(e) => {
                error_body::UnknownFunctionError::new(e.to_smolstr()).into()
            }
            est::FromJsonError::InvalidEntityType(e) => error_body::InvalidEntityTypeError {
                description: e.to_string(),
            }
            .into(),
            est::FromJsonError::JsonDeserializationError(e) => {
                // An error while deserializing JSON can occur only in small transformations; this
                // is likely a parsing error on a literal.
                error_body::ParsingFailedError::new(e.to_string()).into()
            }
            est::FromJsonError::UnescapeError(e) => {
                // Show just first error in main error message, like original err
                error_body::ParsingFailedError::new(e.head.to_string()).into()
            }

            // Errors below should not occur in normal expression conversion paths, but we still
            // map them to the closest PST error for completeness.
            est::FromJsonError::ActionSlot => {
                error_body::ActionConstraintCannotHaveSlotsError.into()
            }
            est::FromJsonError::InvalidActionType(e) => error_body::InvalidEntityTypeError {
                description: e.to_string(),
            }
            .into(),
            est::FromJsonError::InvalidSlotName => {
                error_body::ParsingFailedError::new(err.to_string()).into()
            }
            est::FromJsonError::TemplateToPolicy(e) => {
                let mut slots: HashSet<pst::SlotId, _> = HashSet::new();
                slots.insert(e.slot.id.into());
                error_body::ContainsSlotError { slots }.into()
            }
            est::FromJsonError::PolicyToTemplate(_) => {
                error_body::ExpectedTemplateWithSlotsError.into()
            }
            est::FromJsonError::SlotsInConditionClause(e) => error_body::ContainsSlotError {
                slots: std::iter::once(e.slot.id.into()).collect(),
            }
            .into(),
            est::FromJsonError::MissingOperator | est::FromJsonError::MultipleOperators { .. } => {
                error_body::InvalidExpressionError::new(err.to_string()).into()
            }
            #[cfg(feature = "tolerant-ast")]
            est::FromJsonError::ASTErrorNode => {
                error_body::UnsupportedErrorNode::new("AST contains an error node").into()
            }
        }
    }
}

#[doc(hidden)]
impl From<ast::ExpressionConstructionError> for PstConstructionError {
    fn from(err: ast::ExpressionConstructionError) -> Self {
        let ast::ExpressionConstructionError::DuplicateKey(k) = err;
        error_body::DuplicateRecordKeyError { key: k.key }.into()
    }
}

#[doc(hidden)]
impl From<crate::parser::err::ParseErrors> for PstConstructionError {
    fn from(value: crate::parser::err::ParseErrors) -> Self {
        error_body::ParsingFailedError::from(value).into()
    }
}

// Extension function lookup failed

#[doc(hidden)]
impl From<ExtensionFunctionLookupError> for PstConstructionError {
    fn from(err: ExtensionFunctionLookupError) -> Self {
        let ExtensionFunctionLookupError::FuncDoesNotExist(body) = err;
        error_body::UnknownFunctionError::new(body.name.to_smolstr()).into()
    }
}

/// Error subtypes for [`PstConstructionError`]
pub mod error_body {
    use miette::Diagnostic;
    use smol_str::SmolStr;
    use std::collections::HashSet;
    use thiserror::Error;

    use crate::est;
    use crate::pst;

    /// Trying to construct a policy from an empty representation of another type
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("cannot construct policy from empty representation")]
    pub struct PolicyFromEmptyRepresentationError;

    /// A policy is a linked policy but no link id has been provided
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("linked policy is missing an instance id")]
    pub struct PolicyMissingLinkIdError;

    /// Action constraints cannot contain template slots
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("action constraint cannot have slots")]
    pub struct ActionConstraintCannotHaveSlotsError;

    /// Expected a template with slots, but found a static policy
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("expected a template with slots, but found a static policy")]
    pub struct ExpectedTemplateWithSlotsError;

    /// Slot occurs in the wrong position (e.g., principal slot in resource)
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("slot `{found}` cannot be used in this position (expected slot `{expected}`)")]
    pub struct WrongSlotPositionError {
        found: pst::SlotId,
        expected: pst::SlotId,
    }

    impl WrongSlotPositionError {
        pub(crate) fn new(found: pst::SlotId, expected: pst::SlotId) -> Self {
            Self { found, expected }
        }
    }

    /// Duplicate key found in a record literal
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("duplicate record key: `{key}`")]
    pub struct DuplicateRecordKeyError {
        pub(crate) key: SmolStr,
    }

    impl DuplicateRecordKeyError {
        /// The duplicate key
        pub fn key(&self) -> &str {
            &self.key
        }
    }

    /// Invalid annotation in a policy or template
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("invalid annotation: {description}")]
    pub struct InvalidAnnotationError {
        description: String,
    }

    impl InvalidAnnotationError {
        pub(crate) fn new(description: impl Into<String>) -> Self {
            Self {
                description: description.into(),
            }
        }
    }

    /// Invalid entity UID format or structure
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("invalid entity UID: {description}")]
    pub struct InvalidEntityUidError {
        pub(crate) description: String,
    }

    /// Invalid entity type error (often failure to parse the name)
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("invalid entity type: `{description}`")]
    pub struct InvalidEntityTypeError {
        pub(crate) description: String,
    }

    /// A generic invalid expression error with a description
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("invalid expression: {description}")]
    pub struct InvalidExpressionError {
        pub(crate) description: String,
    }

    impl InvalidExpressionError {
        pub(crate) fn new(description: String) -> Self {
            Self { description }
        }
    }

    /// Unknown function name
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("unknown function: `{name}`")]
    pub struct UnknownFunctionError {
        pub(crate) name: SmolStr,
    }

    impl UnknownFunctionError {
        pub(crate) fn new(name: SmolStr) -> Self {
            Self { name }
        }

        /// The unknown function name
        pub fn name(&self) -> &str {
            &self.name
        }
    }

    /// Function called with wrong number of arguments
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("function `{name}` expects {expected} argument(s), got {got}")]
    pub struct WrongArityError {
        pub(crate) name: String,
        pub(crate) expected: usize,
        pub(crate) got: usize,
    }

    impl WrongArityError {
        pub(crate) fn new(name: String, expected: usize, got: usize) -> Self {
            Self {
                name,
                expected,
                got,
            }
        }

        /// The function name
        pub fn name(&self) -> &str {
            &self.name
        }

        /// The expected number of arguments
        pub fn expected(&self) -> usize {
            self.expected
        }

        /// The actual number of arguments provided
        pub fn got(&self) -> usize {
            self.got
        }
    }

    /// Error nodes from parsing are not supported in PST conversion
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("error nodes not supported in conversion: {description}")]
    pub struct UnsupportedErrorNode {
        /// Information about where this error node might come from
        description: &'static str,
    }

    impl UnsupportedErrorNode {
        pub(crate) fn new(description: &'static str) -> Self {
            Self { description }
        }
    }

    /// A parsing error occurred
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("parse error: {description}")]
    pub struct ParsingFailedError {
        pub(crate) description: String,
    }

    impl ParsingFailedError {
        pub(crate) fn new(description: String) -> Self {
            Self { description }
        }
    }

    impl From<crate::parser::err::ParseErrors> for ParsingFailedError {
        fn from(value: crate::parser::err::ParseErrors) -> Self {
            Self::new(format!("{value}"))
        }
    }

    /// Errors that can occur when linking a template policy
    #[derive(Debug, PartialEq, Eq, Diagnostic, Error, Clone)]
    pub enum LinkingError {
        /// Template contains this slot, but a value wasn't provided for it
        #[error("failed to link template: no value provided for `{slot}`")]
        MissedSlot {
            /// Slot which didn't have a value provided for it
            slot: pst::SlotId,
        },
    }

    impl From<LinkingError> for est::LinkingError {
        fn from(err: LinkingError) -> Self {
            match err {
                LinkingError::MissedSlot { slot } => Self::MissedSlot { slot: slot.into() },
            }
        }
    }

    /// The policy or an expression contains slots
    #[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
    #[error("policy or expression contains slots: {slots:?}")]
    pub struct ContainsSlotError {
        pub(crate) slots: HashSet<crate::pst::SlotId>,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cool_asserts::assert_matches;

    #[test]
    fn from_json_error_conversions() {
        use crate::est::FromJsonError;

        // This is a set of rather shallow tests to cover the different cases in FromJsonError.
        // We don't actually expect those to happen, cause you need a valid EST/AST to then convert
        // to the PST, so those FromJsonError would already have happened. However, we want to
        // cover nicely the conversion just in case. The real error cases should be covered in
        // unit tests in the conversions.

        // JsonDeserializationError
        let serde_err = serde_json::from_str::<String>("bad").unwrap_err();
        let json_deser_err: crate::entities::json::err::JsonDeserializationError = serde_err.into();
        let err: PstConstructionError =
            FromJsonError::JsonDeserializationError(json_deser_err).into();
        assert_matches!(err, PstConstructionError::ParsingFailed(..));

        // ActionSlot
        let err: PstConstructionError = FromJsonError::ActionSlot.into();
        assert!(matches!(
            err,
            PstConstructionError::ActionConstraintCannotHaveSlots(..)
        ));

        // InvalidActionType
        let euid = ast::EntityUID::with_eid_and_type("Bad", "act").unwrap();
        let err: PstConstructionError =
            FromJsonError::InvalidActionType(crate::parser::err::parse_errors::InvalidActionType {
                euids: nonempty::nonempty![std::sync::Arc::new(euid)],
            })
            .into();
        assert_matches!(err, PstConstructionError::InvalidEntityType(..));

        // InvalidSlotName
        let err: PstConstructionError = FromJsonError::InvalidSlotName.into();
        assert_matches!(err, PstConstructionError::ParsingFailed(..));

        // TemplateToPolicy
        let err: PstConstructionError = FromJsonError::TemplateToPolicy(
            crate::parser::err::parse_errors::ExpectedStaticPolicy {
                slot: ast::Slot {
                    id: ast::SlotId::principal(),
                    loc: None,
                },
            },
        )
        .into();
        assert_matches!(err, PstConstructionError::ContainsSlots(..));

        // PolicyToTemplate
        let err: PstConstructionError = FromJsonError::PolicyToTemplate(
            crate::parser::err::parse_errors::ExpectedTemplate::new(),
        )
        .into();
        assert!(matches!(
            err,
            PstConstructionError::ExpectedTemplateWithSlots(..)
        ));

        // SlotsInConditionClause
        let err: PstConstructionError = FromJsonError::SlotsInConditionClause(
            crate::parser::err::parse_errors::SlotsInConditionClause {
                slot: ast::Slot {
                    id: ast::SlotId::resource(),
                    loc: None,
                },
                clause_type: "when",
            },
        )
        .into();
        assert_matches!(err, PstConstructionError::ContainsSlots(..));

        // MissingOperator
        let err: PstConstructionError = FromJsonError::MissingOperator.into();
        assert_matches!(err, PstConstructionError::InvalidExpression(..));

        // MultipleOperators
        let err: PstConstructionError = FromJsonError::MultipleOperators {
            ops: vec!["a".into(), "b".into()],
        }
        .into();
        assert_matches!(err, PstConstructionError::InvalidExpression(..));
    }

    #[test]
    fn from_expression_construction_error() {
        let err: PstConstructionError = ast::ExpressionConstructionError::DuplicateKey(
            ast::expression_construction_errors::DuplicateKeyError {
                key: "k".into(),
                context: "in record literal",
            },
        )
        .into();
        assert_matches!(err, PstConstructionError::DuplicateRecordKey(..));
    }

    #[test]
    fn from_extension_function_lookup_error() {
        use crate::extensions::ExtensionFunctionLookupError;
        let err: PstConstructionError = ExtensionFunctionLookupError::FuncDoesNotExist(
            crate::extensions::extension_function_lookup_errors::FuncDoesNotExistError {
                name: "bogus".parse().unwrap(),
                source_loc: None,
            },
        )
        .into();
        assert_matches!(err, PstConstructionError::UnknownFunction(..));
    }
}