cedar-policy / cedar

Implementation of the Cedar Policy Language
https://www.cedarpolicy.com
Apache License 2.0
832 stars 72 forks source link

CLI translate-schema command does not work in "json-to-cedar" direction #1159

Closed adamrothman closed 3 weeks ago

adamrothman commented 3 weeks ago

Before opening, please confirm:

Bug Category

Schemas and Validation

Describe the bug

The CLI's translate-schema command fails when invoked with a JSON schema and --direction json-to-cedar:

$ cargo run -- translate-schema --schema ~/Downloads/photoflash.cedarschema.json --direction json-to-cedar
warning: /Users/adam.rothman/src/cedar/cedar-policy-core/Cargo.toml: unused manifest key: lints.rust.unexpected_cfgs.check-cfg
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s
     Running `/Users/adam.rothman/src/cedar/target/debug/cedar translate-schema --schema /Users/adam.rothman/Downloads/photoflash.cedarschema.json --direction json-to-cedar`
  × error parsing schema: unexpected token `{`
  help: this API was expecting a schema in the Cedar schema format; did you mean to use a different function, which expects a JSON-format Cedar schema

Expected behavior

The CLI should successfully parse the JSON schema file and output a translation to the human-readable format.

For example, it works fine when translating the example human-readable schema to JSON:

$ cargo run -- translate-schema --schema ~/Downloads/photoflash.cedarschema --direction cedar-to-json 
warning: /Users/adam.rothman/src/cedar/cedar-policy-core/Cargo.toml: unused manifest key: lints.rust.unexpected_cfgs.check-cfg
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
     Running `/Users/adam.rothman/src/cedar/target/debug/cedar translate-schema --schema /Users/adam.rothman/Downloads/photoflash.cedarschema --direction cedar-to-json`
{"PhotoFlash":{"entityTypes":{"Album":{"memberOfTypes":["Album"],"shape":{"type":"Record","attributes":{"account":{"type":"EntityOrCommon","name":"Account"},"private":{"type":"EntityOrCommon","name":"Bool"}}}},"User":{"memberOfTypes":["UserGroup"],"shape":{"type":"Record","attributes":{"department":{"type":"EntityOrCommon","name":"String"},"jobLevel":{"type":"EntityOrCommon","name":"Long"}}}},"Account":{"shape":{"type":"Record","attributes":{"admins":{"type":"Set","element":{"type":"EntityOrCommon","name":"User"},"required":false},"owner":{"type":"EntityOrCommon","name":"User"}}}},"Photo":{"memberOfTypes":["Album"],"shape":{"type":"Record","attributes":{"account":{"type":"EntityOrCommon","name":"Account"},"private":{"type":"EntityOrCommon","name":"Bool"}}}},"UserGroup":{}},"actions":{"uploadPhoto":{"appliesTo":{"resourceTypes":["Album"],"principalTypes":["User"],"context":{"type":"Record","attributes":{"authenticated":{"type":"EntityOrCommon","name":"Bool"},"photo":{"type":"Record","attributes":{"file_size":{"type":"EntityOrCommon","name":"Long"},"file_type":{"type":"EntityOrCommon","name":"String"}}}}}}},"viewPhoto":{"appliesTo":{"resourceTypes":["Photo"],"principalTypes":["User"],"context":{"type":"Record","attributes":{"authenticated":{"type":"EntityOrCommon","name":"Bool"}}}}},"listAlbums":{"appliesTo":{"resourceTypes":["Account"],"principalTypes":["User"],"context":{"type":"Record","attributes":{"authenticated":{"type":"EntityOrCommon","name":"Bool"}}}}}}}}

Reproduction steps

  1. Clone https://github.com/cedar-policy/cedar at latest master (3458cd20ec56756d164faf1108c20515bb839196 at time of report)
  2. Download the example JSON format schema here; attached as photoflash.cedarschema.json
  3. In the cedar-policy-cli directory, run cargo run -- translate-schema --schema ~/Downloads/photoflash.cedarschema.json --direction json-to-cedar
  4. Error

Code Snippet

No response

Log output

No response

Additional configuration

No response

Operating System

macOS Sonoma 14.6.1

Additional information and screenshots

No response

khieta commented 3 weeks ago

Thanks for reporting this! This was an unintentional break caused by #1114. I can have a fix up shortly.

khieta commented 3 weeks ago

Fix is merged! Thanks again for filing the issue.

adamrothman commented 3 weeks ago

Thanks for turning it around so quickly!