Skip to content

Commit 919a916

Browse files
oflattcdisselkoenkhieta
authored
Add Entity Manifests to Cedar (#1102)
Signed-off-by: oflatt <[email protected]> Co-authored-by: Craig Disselkoen <[email protected]> Co-authored-by: Kesha Hietala <[email protected]>
1 parent edffd9c commit 919a916

File tree

10 files changed

+1466
-6
lines changed

10 files changed

+1466
-6
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ impl From<PartialValue> for Expr {
180180
}
181181
}
182182

183+
impl<T> ExprKind<T> {
184+
/// Describe this operator for error messages.
185+
pub fn operator_description(self: &ExprKind<T>) -> String {
186+
match self {
187+
ExprKind::Lit(_) => "literal".to_string(),
188+
ExprKind::Var(_) => "variable".to_string(),
189+
ExprKind::Slot(_) => "slot".to_string(),
190+
ExprKind::Unknown(_) => "unknown".to_string(),
191+
ExprKind::If { .. } => "if".to_string(),
192+
ExprKind::And { .. } => "&&".to_string(),
193+
ExprKind::Or { .. } => "||".to_string(),
194+
ExprKind::UnaryApp { op, .. } => op.to_string(),
195+
ExprKind::BinaryApp { op, .. } => op.to_string(),
196+
ExprKind::ExtensionFunctionApp { fn_name, .. } => fn_name.to_string(),
197+
ExprKind::GetAttr { .. } => "get attribute".to_string(),
198+
ExprKind::HasAttr { .. } => "has attribute".to_string(),
199+
ExprKind::Like { .. } => "like".to_string(),
200+
ExprKind::Is { .. } => "is".to_string(),
201+
ExprKind::Set(_) => "set".to_string(),
202+
ExprKind::Record(_) => "record".to_string(),
203+
}
204+
}
205+
}
206+
183207
impl<T> Expr<T> {
184208
fn new(expr_kind: ExprKind<T>, source_loc: Option<Loc>, data: T) -> Self {
185209
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
@@ -47,6 +47,7 @@ arbitrary = ["dep:arbitrary", "cedar-policy-core/arbitrary"]
4747
# Experimental features.
4848
partial-validate = []
4949
wasm = ["serde-wasm-bindgen", "tsify", "wasm-bindgen"]
50+
entity-manifest = []
5051

5152
[dev-dependencies]
5253
similar-asserts = "1.5.0"

0 commit comments

Comments
 (0)