Skip to content

Commit 6ee5665

Browse files
cdisselkoenshaobo-he-aws
authored andcommitted
fix accepting tags as an attr name (#1209)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent 2eb7d0b commit 6ee5665

File tree

6 files changed

+92
-64
lines changed

6 files changed

+92
-64
lines changed

cedar-policy-core/src/parser.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,18 @@ mod tests {
621621
assert!(!result.expect("parse error").is_empty());
622622
}
623623

624+
#[test]
625+
fn attr_named_tags() {
626+
let src = r#"
627+
permit(principal, action, resource)
628+
when {
629+
resource.tags.contains({k: "foo", v: "bar"})
630+
};
631+
"#;
632+
parse_policy_to_est_and_ast(None, src)
633+
.unwrap_or_else(|e| panic!("{:?}", &miette::Report::new(e)));
634+
}
635+
624636
#[test]
625637
fn test_parse_policyset() {
626638
use crate::ast::PolicyID;

cedar-policy-validator/src/cedar_schema/err.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ lazy_static! {
7272
("TYPE", "`type`"),
7373
("SET", "`Set`"),
7474
("IDENTIFIER", "identifier"),
75+
("TAGS", "`tags`"),
7576
]),
7677
impossible_tokens: HashSet::new(),
7778
special_identifier_tokens: HashSet::from([
@@ -85,6 +86,7 @@ lazy_static! {
8586
"RESOURCE",
8687
"CONTEXT",
8788
"ATTRIBUTES",
89+
"TAGS",
8890
"LONG",
8991
"STRING",
9092
"BOOL",

cedar-policy-validator/src/cedar_schema/grammar.lalrpop

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ match {
6161
"entity" => ENTITY,
6262
"in" => IN,
6363
"type" => TYPE,
64-
"tags" => TAGS,
6564
"Set" => SET,
6665
"appliesTo" => APPLIESTO,
6766
"principal" => PRINCIPAL,
6867
"action" => ACTION,
6968
"resource" => RESOURCE,
7069
"context" => CONTEXT,
7170
"attributes" => ATTRIBUTES,
71+
"tags" => TAGS,
7272
"Long" => LONG,
7373
"String" => STRING,
7474
"Bool" => BOOL,
@@ -220,6 +220,8 @@ Ident: Node<Id> = {
220220
=> Node::with_source_loc("context".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
221221
<l:@L> ATTRIBUTES <r:@R>
222222
=> Node::with_source_loc("attributes".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
223+
<l:@L> TAGS <r:@R>
224+
=> Node::with_source_loc("tags".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
223225
<l:@L> BOOL <r:@R>
224226
=> Node::with_source_loc("Bool".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
225227
<l:@L> LONG <r:@R>

cedar-policy-validator/src/cedar_schema/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod demo_tests {
3535
err::{ToJsonSchemaError, NO_PR_HELP_MSG},
3636
},
3737
json_schema,
38-
schema::test::collect_warnings,
38+
schema::test::utils::collect_warnings,
3939
CedarSchemaError, RawName,
4040
};
4141

@@ -1168,7 +1168,7 @@ mod translator_tests {
11681168
to_json_schema::cedar_schema_to_json_schema,
11691169
},
11701170
json_schema,
1171-
schema::test::collect_warnings,
1171+
schema::test::utils::collect_warnings,
11721172
types::{EntityLUB, EntityRecordKind, Primitive, Type},
11731173
ValidatorSchema,
11741174
};
@@ -2302,7 +2302,7 @@ mod common_type_references {
23022302
#[cfg(test)]
23032303
mod entity_tags {
23042304
use crate::json_schema;
2305-
use crate::schema::test::collect_warnings;
2305+
use crate::schema::test::utils::collect_warnings;
23062306
use cedar_policy_core::extensions::Extensions;
23072307
use cool_asserts::assert_matches;
23082308

cedar-policy-validator/src/schema.rs

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,27 +1328,68 @@ pub(crate) mod test {
13281328

13291329
use super::*;
13301330

1331-
/// Transform the output of functions like
1332-
/// `ValidatorSchema::from_cedarschema_str()`, which has type `(ValidatorSchema, impl Iterator<...>)`,
1333-
/// into `(ValidatorSchema, Vec<...>)`, which implements `Debug` and thus can be used with
1334-
/// `assert_matches`, `.unwrap_err()`, etc
1335-
pub fn collect_warnings<A, B, E>(
1336-
r: std::result::Result<(A, impl Iterator<Item = B>), E>,
1337-
) -> std::result::Result<(A, Vec<B>), E> {
1338-
r.map(|(a, iter)| (a, iter.collect()))
1339-
}
1340-
1341-
/// Given an entity type as string, get the `ValidatorEntityType` from the
1342-
/// schema, panicking if it does not exist (or if `etype` fails to parse as
1343-
/// an entity type)
1344-
#[track_caller]
1345-
pub fn assert_entity_type_exists<'s>(
1346-
schema: &'s ValidatorSchema,
1347-
etype: &str,
1348-
) -> &'s ValidatorEntityType {
1349-
schema.get_entity_type(&etype.parse().unwrap()).unwrap()
1331+
pub(crate) mod utils {
1332+
use super::{CedarSchemaError, SchemaError, ValidatorEntityType, ValidatorSchema};
1333+
use cedar_policy_core::extensions::Extensions;
1334+
1335+
/// Transform the output of functions like
1336+
/// `ValidatorSchema::from_cedarschema_str()`, which has type `(ValidatorSchema, impl Iterator<...>)`,
1337+
/// into `(ValidatorSchema, Vec<...>)`, which implements `Debug` and thus can be used with
1338+
/// `assert_matches`, `.unwrap_err()`, etc
1339+
pub fn collect_warnings<A, B, E>(
1340+
r: std::result::Result<(A, impl Iterator<Item = B>), E>,
1341+
) -> std::result::Result<(A, Vec<B>), E> {
1342+
r.map(|(a, iter)| (a, iter.collect()))
1343+
}
1344+
1345+
/// Given an entity type as string, get the `ValidatorEntityType` from the
1346+
/// schema, panicking if it does not exist (or if `etype` fails to parse as
1347+
/// an entity type)
1348+
#[track_caller]
1349+
pub fn assert_entity_type_exists<'s>(
1350+
schema: &'s ValidatorSchema,
1351+
etype: &str,
1352+
) -> &'s ValidatorEntityType {
1353+
schema.get_entity_type(&etype.parse().unwrap()).unwrap()
1354+
}
1355+
1356+
#[track_caller]
1357+
pub fn assert_valid_cedar_schema(src: &str) -> ValidatorSchema {
1358+
match ValidatorSchema::from_cedarschema_str(src, Extensions::all_available()) {
1359+
Ok((schema, _)) => schema,
1360+
Err(e) => panic!("{:?}", miette::Report::new(e)),
1361+
}
1362+
}
1363+
1364+
#[track_caller]
1365+
pub fn assert_invalid_cedar_schema(src: &str) {
1366+
match ValidatorSchema::from_cedarschema_str(src, Extensions::all_available()) {
1367+
Ok(_) => panic!("{src} should be an invalid schema"),
1368+
Err(CedarSchemaError::Parsing(_)) => {}
1369+
Err(e) => panic!("unexpected error: {:?}", miette::Report::new(e)),
1370+
}
1371+
}
1372+
1373+
#[track_caller]
1374+
pub fn assert_valid_json_schema(json: serde_json::Value) -> ValidatorSchema {
1375+
match ValidatorSchema::from_json_value(json, Extensions::all_available()) {
1376+
Ok(schema) => schema,
1377+
Err(e) => panic!("{:?}", miette::Report::new(e)),
1378+
}
1379+
}
1380+
1381+
#[track_caller]
1382+
pub fn assert_invalid_json_schema(json: serde_json::Value) {
1383+
match ValidatorSchema::from_json_value(json.clone(), Extensions::all_available()) {
1384+
Ok(_) => panic!("{json} should be an invalid schema"),
1385+
Err(SchemaError::JsonDeserialization(_)) => {}
1386+
Err(e) => panic!("unexpected error: {:?}", miette::Report::new(e)),
1387+
}
1388+
}
13501389
}
13511390

1391+
use utils::*;
1392+
13521393
// Well-formed schema
13531394
#[test]
13541395
fn test_from_schema_file() {
@@ -3443,15 +3484,23 @@ pub(crate) mod test {
34433484
);
34443485
});
34453486
}
3487+
3488+
#[test]
3489+
fn attr_named_tags() {
3490+
let src = r#"
3491+
entity E { tags: Set<{key: String, value: Set<String>}> };
3492+
"#;
3493+
assert_valid_cedar_schema(src);
3494+
}
34463495
}
34473496

34483497
#[cfg(test)]
34493498
mod test_579; // located in separate file test_579.rs
34503499

34513500
#[cfg(test)]
34523501
mod test_rfc70 {
3453-
use super::test::{assert_entity_type_exists, collect_warnings};
3454-
use super::{CedarSchemaError, SchemaError, ValidatorSchema};
3502+
use super::test::utils::*;
3503+
use super::ValidatorSchema;
34553504
use crate::types::Type;
34563505
use cedar_policy_core::{
34573506
extensions::Extensions,
@@ -3460,40 +3509,6 @@ mod test_rfc70 {
34603509
use cool_asserts::assert_matches;
34613510
use serde_json::json;
34623511

3463-
#[track_caller]
3464-
fn assert_valid_cedar_schema(src: &str) -> ValidatorSchema {
3465-
match ValidatorSchema::from_cedarschema_str(src, Extensions::all_available()) {
3466-
Ok((schema, _)) => schema,
3467-
Err(e) => panic!("{:?}", miette::Report::new(e)),
3468-
}
3469-
}
3470-
3471-
#[track_caller]
3472-
fn assert_invalid_cedar_schema(src: &str) {
3473-
match ValidatorSchema::from_cedarschema_str(src, Extensions::all_available()) {
3474-
Ok(_) => panic!("{src} should be an invalid schema"),
3475-
Err(CedarSchemaError::Parsing(_)) => {}
3476-
Err(e) => panic!("unexpected error: {:?}", miette::Report::new(e)),
3477-
}
3478-
}
3479-
3480-
#[track_caller]
3481-
fn assert_valid_json_schema(json: serde_json::Value) -> ValidatorSchema {
3482-
match ValidatorSchema::from_json_value(json, Extensions::all_available()) {
3483-
Ok(schema) => schema,
3484-
Err(e) => panic!("{:?}", miette::Report::new(e)),
3485-
}
3486-
}
3487-
3488-
#[track_caller]
3489-
fn assert_invalid_json_schema(json: serde_json::Value) {
3490-
match ValidatorSchema::from_json_value(json.clone(), Extensions::all_available()) {
3491-
Ok(_) => panic!("{json} should be an invalid schema"),
3492-
Err(SchemaError::JsonDeserialization(_)) => {}
3493-
Err(e) => panic!("unexpected error: {:?}", miette::Report::new(e)),
3494-
}
3495-
}
3496-
34973512
/// Common type shadowing a common type is disallowed in both syntaxes
34983513
#[test]
34993514
fn common_common_conflict() {
@@ -4476,10 +4491,7 @@ mod test_rfc70 {
44764491
mod entity_tags {
44774492
use crate::types::Primitive;
44784493

4479-
use super::{
4480-
test::{assert_entity_type_exists, collect_warnings},
4481-
*,
4482-
};
4494+
use super::{test::utils::*, *};
44834495
use cedar_policy_core::{
44844496
extensions::Extensions,
44854497
test_utils::{expect_err, ExpectedErrorMessageBuilder},

cedar-policy-validator/src/schema/test_579.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
//! we only do the test for the more sensible one. (For instance, for 1a1, we
6161
//! only test an entity type reference, not a common type reference.)
6262
63-
use super::{test::collect_warnings, SchemaWarning, ValidatorSchema};
63+
use super::{test::utils::collect_warnings, SchemaWarning, ValidatorSchema};
6464
use cedar_policy_core::extensions::Extensions;
6565
use cedar_policy_core::test_utils::{
6666
expect_err, ExpectedErrorMessage, ExpectedErrorMessageBuilder,

0 commit comments

Comments
 (0)