Skip to content

Commit

Permalink
Changes needed by cedar-policy/cedar#1114 (#412)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <[email protected]>
Co-authored-by: John Kastner <[email protected]>
  • Loading branch information
shaobo-he-aws and john-h-kastner-aws authored Aug 6, 2024
1 parent da84cf1 commit 30d38aa
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 37 deletions.
8 changes: 4 additions & 4 deletions cedar-drt/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RawName>::from_str_natural(&src, Extensions::all_available())
json_schema::Fragment::<RawName>::from_cedarschema_str(&src, Extensions::all_available())
{
if TryInto::<ValidatorSchema>::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()) {
Expand All @@ -43,7 +43,7 @@ fuzz_target!(|src: String| {
SimpleDiff::from_str(
&format!("{:#?}", parsed),
&format!("{:#?}", json_parsed),
"Parsed human readable",
"Parsed Cedar",
"JSON round-tripped"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RawName>::from_json_str(&src) {
if TryInto::<ValidatorSchema>::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::<RawName>::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::<RawName>::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}");
Expand Down
9 changes: 3 additions & 6 deletions cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
9 changes: 5 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand All @@ -83,7 +84,7 @@ fuzz_target!(|i: Input| {
&format!("{:#?}", downgraded),
&format!("{:#?}", parsed),
"Initial Schema",
"Human Round tripped"
"Cedar Round tripped"
)
);
panic!("{msg}");
Expand Down
4 changes: 2 additions & 2 deletions cedar-drt/fuzz/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions cedar-drt/fuzz/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,7 +49,7 @@ pub fn equivalence_check<N: Clone + PartialEq + Debug + Display + TypeName + Ord
) -> 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);
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-generators/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl From<&HierarchyArgs> for ABACSettings {

fn generate_hierarchy_from_schema(byte_length: usize, args: &HierarchyArgs) -> Result<Entities> {
let f = File::open(&args.schema_file)?;
let fragment = json_schema::Fragment::<RawName>::from_file(f)?;
let fragment = json_schema::Fragment::<RawName>::from_json_file(f)?;
let mut rng = thread_rng();
let mut bytes = Vec::with_capacity(byte_length);
bytes.resize_with(byte_length, || rng.gen());
Expand Down
4 changes: 2 additions & 2 deletions cedar-policy-generators/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 30d38aa

Please sign in to comment.