Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions cedar-policy-core/src/ast/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,11 @@ impl From<StaticPolicy> for TemplateBody {
impl std::fmt::Display for TemplateBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k, v) in self.annotations.iter() {
writeln!(f, "@{}(\"{}\")", k, v.val.escape_debug())?
write!(f, "@{k}")?;
if let Some(v) = &v.raw_val {
write!(f, "(\"{}\")", v.escape_debug())?;
}
writeln!(f)?;
}
write!(
f,
Expand Down Expand Up @@ -1163,16 +1167,51 @@ impl From<BTreeMap<AnyId, Annotation>> for Annotations {
/// Struct which holds the value of a particular annotation
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug, PartialOrd, Ord)]
pub struct Annotation {
/// Annotation value
pub val: SmolStr,
/// Annotation value. `None` for annotations without a value, i.e., `@foo`.
/// An annotation without a value should be treated as equivalent to the
/// value being `""`. This interpretation is implemented by the `AsRef<str>`
/// impl an `val` method below.
raw_val: Option<SmolStr>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Option and explicitly saying that None and Some("") are equivalent? This seems like a footgun for later. Wouldn't it be better to just have a SmolStr here so that there's only one representation?

Copy link
Contributor Author

@john-h-kastner-aws john-h-kastner-aws Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that conversion to EST, which is currently written to go through this AST struct, even when converting directly from CST, should be lossless (mostly? not sure our exact standard here), so this struct needed to know if it was originally present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would propose that we pick one or the other representation to be canonical and output that EST. So either we decide that empty annotation values are always omitted, or always explicit "". I believe "lossless" already drops extraneous parens, is this any different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A related decision: In the EST I updated annotations so that null is an allowed value, equivalent to "". Thoughts? I think we would eventually need to allow that in a future world where we differentiate no-value and empty-string, but it's not necessary for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see an argument either way there. Some sugar in the EST can be helpful, but the feature request was probably primarily for sugar in the Cedar policy format, and it's totally reasonable to require explicitness in the EST.

Copy link
Contributor Author

@john-h-kastner-aws john-h-kastner-aws Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some of the cst-to-ast code generic so that est-to-ast can still reuse the same id and string validation checks without passing through the ast structs to avoid this issue. New impl preserves the absent value on cst<->est, but converts it to "" on ast<->est

/// Source location. Note this is the location of _the entire key-value
/// pair_ for the annotation, not just `val` above
pub loc: Option<Loc>,
loc: Option<Loc>,
}

impl Annotation {
/// Construct a new annotation.
pub fn new(val: Option<SmolStr>, loc: Option<Loc>) -> Self {
Self { raw_val: val, loc }
}

/// Get the annotation value, treating an annotation without a value as
/// being equivalent to the value `""`.
pub fn val(&self) -> &str {
self.as_ref()
}

/// Get the annotation value, returning `None` for annotations without a
/// value. We generally want to treat this as equivalent to `""`, but we
/// occasionally want a lossless representation.
pub fn raw_val(&self) -> Option<&SmolStr> {
self.raw_val.as_ref()
}

/// Get the annotation value, returning `None` for annotations without a
/// value. We generally want to treat this as equivalent to `""`, but we
/// occasionally want a lossless representation.
pub fn into_raw_val(self) -> Option<SmolStr> {
self.raw_val
}

/// Get the location for the whole annotation (not just the value).
pub fn loc(&self) -> Option<&Loc> {
self.loc.as_ref()
}
}

impl AsRef<str> for Annotation {
fn as_ref(&self) -> &str {
&self.val
self.raw_val.as_ref().map(SmolStr::as_str).unwrap_or("")
}
}

Expand Down Expand Up @@ -1704,7 +1743,11 @@ impl ActionConstraint {
impl std::fmt::Display for StaticPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k, v) in self.0.annotations.iter() {
writeln!(f, "@{}(\"{}\")", k, v.val.escape_debug())?
write!(f, "@{k}")?;
if let Some(v) = &v.raw_val {
write!(f, "(\"{}\")", v.escape_debug())?;
}
writeln!(f)?;
}
write!(
f,
Expand Down
99 changes: 93 additions & 6 deletions cedar-policy-core/src/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Policy {
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde_as(as = "serde_with::MapPreventDuplicates<_,_>")]
#[cfg_attr(feature = "wasm", tsify(type = "Record<string, string>"))]
annotations: BTreeMap<ast::AnyId, SmolStr>,
annotations: BTreeMap<ast::AnyId, Option<SmolStr>>,
}

/// Serde JSON structure for a `when` or `unless` clause in the EST format
Expand Down Expand Up @@ -140,7 +140,10 @@ impl TryFrom<cst::Policy> for Policy {
action: action.into(),
resource: resource.into(),
conditions,
annotations: annotations.into_iter().map(|(k, v)| (k, v.val)).collect(),
annotations: annotations
.into_iter()
.map(|(k, v)| (k, v.into_raw_val()))
.collect(),
})
}
}
Expand Down Expand Up @@ -230,7 +233,7 @@ impl Policy {
None,
self.annotations
.into_iter()
.map(|(key, val)| (key, ast::Annotation { val, loc: None }))
.map(|(key, val)| (key, ast::Annotation::new(val, None)))
.collect(),
self.effect,
self.principal.try_into()?,
Expand Down Expand Up @@ -276,7 +279,7 @@ impl From<ast::Policy> for Policy {
conditions: vec![ast.non_scope_constraints().clone().into()],
annotations: ast
.annotations()
.map(|(k, v)| (k.clone(), v.val.clone()))
.map(|(k, v)| (k.clone(), v.raw_val().cloned()))
.collect(),
}
}
Expand All @@ -293,7 +296,7 @@ impl From<ast::Template> for Policy {
conditions: vec![ast.non_scope_constraints().clone().into()],
annotations: ast
.annotations()
.map(|(k, v)| (k.clone(), v.val.clone()))
.map(|(k, v)| (k.clone(), v.raw_val().cloned()))
.collect(),
}
}
Expand All @@ -308,7 +311,11 @@ impl<T: Clone> From<ast::Expr<T>> for Clause {
impl std::fmt::Display for Policy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k, v) in self.annotations.iter() {
writeln!(f, "@{k}(\"{}\") ", v.escape_debug())?;
write!(f, "@{k}")?;
if let Some(v) = v {
write!(f, "(\"{}\")", v.escape_debug())?;
}
writeln!(f)?;
}
write!(
f,
Expand Down Expand Up @@ -586,6 +593,86 @@ mod test {
);
}

#[test]
fn annotated_without_value_policy() {
let policy = r#"@foo permit(principal, action, resource);"#;
let cst = parser::text_to_cst::parse_policy(policy)
.unwrap()
.node
.unwrap();
let est: Policy = cst.try_into().unwrap();
let expected_json = json!(
{
"effect": "permit",
"principal": {
"op": "All",
},
"action": {
"op": "All",
},
"resource": {
"op": "All",
},
"conditions": [],
"annotations": { "foo": null, }
}
);
assert_eq!(
serde_json::to_value(&est).unwrap(),
expected_json,
"\nExpected:\n{}\n\nActual:\n{}\n\n",
serde_json::to_string_pretty(&expected_json).unwrap(),
serde_json::to_string_pretty(&est).unwrap()
);
let old_est = est.clone();
let roundtripped = est_roundtrip(est);
assert_eq!(&old_est, &roundtripped);
let est = text_roundtrip(&old_est);
assert_eq!(&old_est, &est);

// during the lossy transform to AST, the only difference for this policy is that
// a `when { true }` is added
let expected_json_after_roundtrip = json!(
{
"effect": "permit",
"principal": {
"op": "All",
},
"action": {
"op": "All",
},
"resource": {
"op": "All",
},
"conditions": [
{
"kind": "when",
"body": {
"Value": true
}
}
],
"annotations": { "foo": null, }
}
);
let roundtripped = serde_json::to_value(ast_roundtrip(est.clone())).unwrap();
assert_eq!(
roundtripped,
expected_json_after_roundtrip,
"\nExpected after roundtrip:\n{}\n\nActual after roundtrip:\n{}\n\n",
serde_json::to_string_pretty(&expected_json_after_roundtrip).unwrap(),
serde_json::to_string_pretty(&roundtripped).unwrap()
);
let roundtripped = serde_json::to_value(circular_roundtrip(est)).unwrap();
assert_eq!(
roundtripped,
expected_json_after_roundtrip,
"\nExpected after roundtrip:\n{}\n\nActual after roundtrip:\n{}\n\n",
serde_json::to_string_pretty(&expected_json_after_roundtrip).unwrap(),
serde_json::to_string_pretty(&roundtripped).unwrap()
);
}

/// Test that we can use Cedar reserved words like `if` and `has` as annotation keys
#[test]
fn reserved_words_as_annotations() {
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/parser/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Annotation {
/// key
pub key: Node<Ident>,
/// value
pub value: Node<Str>,
pub value: Option<Node<Str>>,
}

/// Literal strings
Expand Down
Loading
Loading