Skip to content

Commit 71d9719

Browse files
Revert "Revert "Add Entity Manifests to Cedar (#1102)" (#1235)" (#1240)
1 parent ab2a8c7 commit 71d9719

File tree

9 files changed

+1466
-5
lines changed

9 files changed

+1466
-5
lines changed

cedar-policy-core/src/ast/expr.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ impl From<PartialValue> for Expr {
185185
}
186186
}
187187

188+
impl<T> ExprKind<T> {
189+
/// Describe this operator for error messages.
190+
pub fn operator_description(self: &ExprKind<T>) -> String {
191+
match self {
192+
ExprKind::Lit(_) => "literal".to_string(),
193+
ExprKind::Var(_) => "variable".to_string(),
194+
ExprKind::Slot(_) => "slot".to_string(),
195+
ExprKind::Unknown(_) => "unknown".to_string(),
196+
ExprKind::If { .. } => "if".to_string(),
197+
ExprKind::And { .. } => "&&".to_string(),
198+
ExprKind::Or { .. } => "||".to_string(),
199+
ExprKind::UnaryApp { op, .. } => op.to_string(),
200+
ExprKind::BinaryApp { op, .. } => op.to_string(),
201+
ExprKind::ExtensionFunctionApp { fn_name, .. } => fn_name.to_string(),
202+
ExprKind::GetAttr { .. } => "get attribute".to_string(),
203+
ExprKind::HasAttr { .. } => "has attribute".to_string(),
204+
ExprKind::Like { .. } => "like".to_string(),
205+
ExprKind::Is { .. } => "is".to_string(),
206+
ExprKind::Set(_) => "set".to_string(),
207+
ExprKind::Record(_) => "record".to_string(),
208+
}
209+
}
210+
}
211+
188212
impl<T> Expr<T> {
189213
fn new(expr_kind: ExprKind<T>, source_loc: Option<Loc>, data: T) -> Self {
190214
Self {

cedar-policy-core/src/ast/request.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ use crate::evaluator::{EvaluationError, RestrictedEvaluator};
2121
use crate::extensions::Extensions;
2222
use crate::parser::Loc;
2323
use miette::Diagnostic;
24-
use serde::Serialize;
24+
use serde::{Deserialize, Serialize};
2525
use smol_str::SmolStr;
2626
use std::collections::{BTreeMap, HashMap};
2727
use std::sync::Arc;
2828
use thiserror::Error;
2929

3030
use super::{
31-
BorrowedRestrictedExpr, EntityUID, Expr, ExprKind, ExpressionConstructionError, PartialValue,
32-
RestrictedExpr, Unknown, Value, ValueKind, Var,
31+
BorrowedRestrictedExpr, EntityType, EntityUID, Expr, ExprKind, ExpressionConstructionError,
32+
PartialValue, RestrictedExpr, Unknown, Value, ValueKind, Var,
3333
};
3434

3535
/// Represents the request tuple <P, A, R, C> (see the Cedar design doc).
@@ -49,6 +49,18 @@ pub struct Request {
4949
pub(crate) context: Option<Context>,
5050
}
5151

52+
/// Represents the principal type, resource type, and action UID.
53+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
54+
#[serde(rename_all = "camelCase")]
55+
pub struct RequestType {
56+
/// Principal type
57+
pub principal: EntityType,
58+
/// Action type
59+
pub action: EntityUID,
60+
/// Resource type
61+
pub resource: EntityType,
62+
}
63+
5264
/// An entry in a request for a Entity UID.
5365
/// It may either be a concrete EUID
5466
/// or an unknown in the case of partial evaluation
@@ -186,6 +198,19 @@ impl Request {
186198
pub fn context(&self) -> Option<&Context> {
187199
self.context.as_ref()
188200
}
201+
202+
/// Get the request types that correspond to this request.
203+
/// This includes the types of the principal, action, and resource.
204+
/// [`RequestType`] is used by the entity manifest.
205+
/// The context type is implied by the action's type.
206+
/// Returns `None` if the request is not fully concrete.
207+
pub fn to_request_type(&self) -> Option<RequestType> {
208+
Some(RequestType {
209+
principal: self.principal().uid()?.entity_type().clone(),
210+
action: self.action().uid()?.clone(),
211+
resource: self.resource().uid()?.entity_type().clone(),
212+
})
213+
}
189214
}
190215

191216
impl std::fmt::Display for Request {

cedar-policy-validator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ arbitrary = ["dep:arbitrary", "cedar-policy-core/arbitrary"]
4646
# Experimental features.
4747
partial-validate = []
4848
wasm = ["serde-wasm-bindgen", "tsify", "wasm-bindgen"]
49+
entity-manifest = []
4950
entity-tags = []
5051

5152
[dev-dependencies]

0 commit comments

Comments
 (0)