From 30d38aa3e8fe4121fe609defac94d93504fd8968 Mon Sep 17 00:00:00 2001 From: shaobo-he-aws <130499339+shaobo-he-aws@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:51:42 -0700 Subject: [PATCH] Changes needed by cedar-policy/cedar#1114 (#412) Signed-off-by: Shaobo He Co-authored-by: John Kastner <130772734+john-h-kastner-aws@users.noreply.github.com> --- cedar-drt/fuzz/Cargo.toml | 8 ++++---- ...son.rs => convert-schema-cedar-to-json.rs} | 10 +++++----- ...man.rs => convert-schema-json-to-cedar.rs} | 20 +++++++++---------- .../fuzz_targets/json-schema-roundtrip.rs | 9 +++------ .../fuzz/fuzz_targets/schema-roundtrip.rs | 9 +++++---- cedar-drt/fuzz/src/dump.rs | 4 ++-- cedar-drt/fuzz/src/schemas.rs | 6 +++--- cedar-policy-generators/src/main.rs | 2 +- cedar-policy-generators/src/schema.rs | 4 ++-- 9 files changed, 35 insertions(+), 37 deletions(-) rename cedar-drt/fuzz/fuzz_targets/{convert-schema-human-to-json.rs => convert-schema-cedar-to-json.rs} (86%) rename cedar-drt/fuzz/fuzz_targets/{convert-schema-json-to-human.rs => convert-schema-json-to-cedar.rs} (74%) diff --git a/cedar-drt/fuzz/Cargo.toml b/cedar-drt/fuzz/Cargo.toml index dd7a2b4a6..3ae1a1636 100644 --- a/cedar-drt/fuzz/Cargo.toml +++ b/cedar-drt/fuzz/Cargo.toml @@ -158,14 +158,14 @@ test = false doc = false [[bin]] -name = "convert-schema-json-to-human" -path = "fuzz_targets/convert-schema-json-to-human.rs" +name = "convert-schema-json-to-cedar" +path = "fuzz_targets/convert-schema-json-to-cedar.rs" test = false doc = false [[bin]] -name = "convert-schema-human-to-json" -path = "fuzz_targets/convert-schema-human-to-json.rs" +name = "convert-schema-cedar-to-json" +path = "fuzz_targets/convert-schema-cedar-to-json.rs" test = false doc = false diff --git a/cedar-drt/fuzz/fuzz_targets/convert-schema-human-to-json.rs b/cedar-drt/fuzz/fuzz_targets/convert-schema-cedar-to-json.rs similarity index 86% rename from cedar-drt/fuzz/fuzz_targets/convert-schema-human-to-json.rs rename to cedar-drt/fuzz/fuzz_targets/convert-schema-cedar-to-json.rs index 06c8a08b0..0da8c6bd7 100644 --- a/cedar-drt/fuzz/fuzz_targets/convert-schema-human-to-json.rs +++ b/cedar-drt/fuzz/fuzz_targets/convert-schema-cedar-to-json.rs @@ -23,17 +23,17 @@ use similar_asserts::SimpleDiff; // Natural String -> json_schema::Fragment -> JSON String -> json_schema::Fragment // Assert that schema fragments are equivalent. By starting with a Natural -// String we test for the existence of schema that are valid in the natural +// String we test for the existence of schema that are valid in the Cedar // format but with an invalid json schema conversion. fuzz_target!(|src: String| { if let Ok((parsed, _)) = - json_schema::Fragment::::from_str_natural(&src, Extensions::all_available()) + json_schema::Fragment::::from_cedarschema_str(&src, Extensions::all_available()) { if TryInto::::try_into(parsed.clone()).is_err() { return; } - let json = serde_json::to_value(parsed.clone()) - .expect("Failed to convert human readable schema to JSON"); + let json = + serde_json::to_value(parsed.clone()).expect("Failed to convert Cedar schema to JSON"); let json_parsed = json_schema::Fragment::from_json_value(json) .expect("Failed to parse converted JSON schema"); if let Err(msg) = equivalence_check(parsed.clone(), json_parsed.clone()) { @@ -43,7 +43,7 @@ fuzz_target!(|src: String| { SimpleDiff::from_str( &format!("{:#?}", parsed), &format!("{:#?}", json_parsed), - "Parsed human readable", + "Parsed Cedar", "JSON round-tripped" ) ); diff --git a/cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-human.rs b/cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-cedar.rs similarity index 74% rename from cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-human.rs rename to cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-cedar.rs index a2e096b0a..9d42b73a3 100644 --- a/cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-human.rs +++ b/cedar-drt/fuzz/fuzz_targets/convert-schema-json-to-cedar.rs @@ -24,29 +24,29 @@ use similar_asserts::SimpleDiff; // JSON String -> json_schema::Fragment -> Natural String -> json_schema::Fragment // Assert that schema fragments are equivalent. By starting with a JSON String // we test for the existence of schema that are valid in JSON but with an -// invalid natural schema conversion. +// invalid cedar schema conversion. fuzz_target!(|src: String| { if let Ok(parsed) = json_schema::Fragment::::from_json_str(&src) { if TryInto::::try_into(parsed.clone()).is_err() { return; } - let natural_src = parsed - .as_natural_schema() - .expect("Failed to convert the JSON schema into a human readable schema"); - let (natural_parsed, _) = json_schema::Fragment::::from_str_natural( - &natural_src, + let ceadr_src = parsed + .to_cedarschema() + .expect("Failed to convert the JSON schema into a Cedar schema"); + let (ceadr_parsed, _) = json_schema::Fragment::::from_cedarschema_str( + &ceadr_src, Extensions::all_available(), ) - .expect("Failed to parse converted human readable schema"); - if let Err(msg) = equivalence_check(parsed.clone(), natural_parsed.clone()) { + .expect("Failed to parse converted Cedar schema"); + if let Err(msg) = equivalence_check(parsed.clone(), ceadr_parsed.clone()) { println!("Schema: {src}"); println!( "{}", SimpleDiff::from_str( &format!("{:#?}", parsed), - &format!("{:#?}", natural_parsed), + &format!("{:#?}", ceadr_parsed), "Parsed JSON", - "Human Round tripped" + "Cedar Round tripped" ) ); panic!("{msg}"); diff --git a/cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs b/cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs index 1c11c3c46..2acf69750 100644 --- a/cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs +++ b/cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs @@ -74,13 +74,10 @@ fuzz_target!(|i: Input| { downgrade_frag_to_raw(i.schema.clone()), "JSON roundtrip failed" ); - let src = json_ast.as_natural_schema().unwrap(); + let src = json_ast.to_cedarschema().unwrap(); let (final_ast, _) = - json_schema::Fragment::from_str_natural(&src, Extensions::all_available()).unwrap(); + json_schema::Fragment::from_cedarschema_str(&src, Extensions::all_available()).unwrap(); if let Err(e) = equivalence_check(downgrade_frag_to_raw(i.schema), final_ast) { - panic!( - "Human-readable roundtrip failed: {}\nSrc:\n```\n{}\n```", - e, src - ); + panic!("Cedar roundtrip failed: {}\nSrc:\n```\n{}\n```", e, src); } }); diff --git a/cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs b/cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs index 9018a495a..7b0c516d4 100644 --- a/cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs +++ b/cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs @@ -70,10 +70,11 @@ impl<'a> Arbitrary<'a> for Input { fuzz_target!(|i: Input| { let src = i .schema - .as_natural_schema() + .to_cedarschema() .expect("Failed to convert schema into a human readable schema"); - let (parsed, _) = json_schema::Fragment::from_str_natural(&src, Extensions::all_available()) - .expect("Failed to parse converted human readable schema"); + let (parsed, _) = + json_schema::Fragment::from_cedarschema_str(&src, Extensions::all_available()) + .expect("Failed to parse converted human readable schema"); let downgraded = downgrade_frag_to_raw(i.schema.clone()); if let Err(msg) = equivalence_check(downgraded.clone(), parsed.clone()) { println!("Schema: {src}"); @@ -83,7 +84,7 @@ fuzz_target!(|i: Input| { &format!("{:#?}", downgraded), &format!("{:#?}", parsed), "Initial Schema", - "Human Round tripped" + "Cedar Round tripped" ) ); panic!("{msg}"); diff --git a/cedar-drt/fuzz/src/dump.rs b/cedar-drt/fuzz/src/dump.rs index 416ba4efa..ef9f08145 100644 --- a/cedar-drt/fuzz/src/dump.rs +++ b/cedar-drt/fuzz/src/dump.rs @@ -68,7 +68,7 @@ pub fn dump( .append(false) .truncate(true) .open(&schema_filename)?; - let schema_text = schema.as_natural_schema().unwrap(); + let schema_text = schema.to_cedarschema().unwrap(); writeln!(schema_file, "{schema_text}")?; let mut policies_file = std::fs::OpenOptions::new() @@ -167,7 +167,7 @@ fn check_test( .unwrap_or_else(|e| panic!("error re-parsing policy file: {e}")); let parsed_schema = - ValidatorSchema::from_str_natural(&formatted_schema, Extensions::all_available()) + ValidatorSchema::from_cedarschema_str(&formatted_schema, Extensions::all_available()) .unwrap_or_else(|e| panic!("error re-parsing schema: {e}")) .0; diff --git a/cedar-drt/fuzz/src/schemas.rs b/cedar-drt/fuzz/src/schemas.rs index a704bfadf..0b4d9671d 100644 --- a/cedar-drt/fuzz/src/schemas.rs +++ b/cedar-drt/fuzz/src/schemas.rs @@ -27,13 +27,13 @@ use std::fmt::{Debug, Display}; /// Check if two schema fragments are equivalent, modulo empty apply specs. /// We do this because there are schemas that are representable in the JSON that are not -/// representable in the human-readable syntax. All of these non-representable schemas +/// representable in the Cedar syntax. All of these non-representable schemas /// are equivalent to one that is representable. /// /// Example: /// You can have a JSON schema with an action that has no applicable principals and some applicable /// resources. -/// In the human-readable syntax, you can't. The only way to write an action with no applicable +/// In the Cedar syntax, you can't. The only way to write an action with no applicable /// principals is: /// ```cedarschema /// action a; @@ -49,7 +49,7 @@ pub fn equivalence_check Result<(), String> { // We need to remove trivial empty namespaces because both `{}` // and `{"": {"entityTypes": {}, "actions": {}}}` translate to empty strings - // in the human-readable schema format + // in the Cedar schema format let mut lhs = lhs; let mut rhs = rhs; remove_trivial_empty_namespace(&mut lhs); diff --git a/cedar-policy-generators/src/main.rs b/cedar-policy-generators/src/main.rs index 31b967f13..86ac84ccf 100644 --- a/cedar-policy-generators/src/main.rs +++ b/cedar-policy-generators/src/main.rs @@ -84,7 +84,7 @@ impl From<&HierarchyArgs> for ABACSettings { fn generate_hierarchy_from_schema(byte_length: usize, args: &HierarchyArgs) -> Result { let f = File::open(&args.schema_file)?; - let fragment = json_schema::Fragment::::from_file(f)?; + let fragment = json_schema::Fragment::::from_json_file(f)?; let mut rng = thread_rng(); let mut bytes = Vec::with_capacity(byte_length); bytes.resize_with(byte_length, || rng.gen()); diff --git a/cedar-policy-generators/src/schema.rs b/cedar-policy-generators/src/schema.rs index a6c7a8b64..c653d34d4 100644 --- a/cedar-policy-generators/src/schema.rs +++ b/cedar-policy-generators/src/schema.rs @@ -2134,7 +2134,7 @@ mod tests { #[test] fn entities_generation_github() { - let fragment = json_schema::Fragment::from_file(GITHUB_SCHEMA_STR.as_bytes()) + let fragment = json_schema::Fragment::from_json_file(GITHUB_SCHEMA_STR.as_bytes()) .expect("schema str should be valid!"); let mut rng = thread_rng(); for _ in 0..ITERATION { @@ -2144,7 +2144,7 @@ mod tests { #[test] fn entities_generation_document_cloud() { - let fragment = json_schema::Fragment::from_file(DOCUMENT_CLOUD_SCHEMA_STR.as_bytes()) + let fragment = json_schema::Fragment::from_json_file(DOCUMENT_CLOUD_SCHEMA_STR.as_bytes()) .expect("schema str should be valid!"); let mut rng = thread_rng(); for _ in 0..ITERATION {