Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize v4.0 JSON FFI #1014

Merged
merged 7 commits into from
Jul 9, 2024
Merged

Finalize v4.0 JSON FFI #1014

merged 7 commits into from
Jul 9, 2024

Conversation

khieta
Copy link
Contributor

@khieta khieta commented Jun 20, 2024

Description of changes

This PR completes the remaining changes required by #757, and should be an accurate reflection of the JSON FFI to be used by Cedar v4.0 (aside from #854, which I'll address in a future PR).

The primary change in this PR is a new definition of PolicySet to be used by both authorization and validation that supports static policies, templates, and template links. I also updated the current Schema type to be an untagged enum to make the JSON prettier. The rest of the diff is changes to testing code.

Issue #, if available

Resolves #757

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A breaking change requiring a major version bump to cedar-policy (e.g., changes to the signature of an existing API).

I confirm that this PR (choose one, and delete the other options):

  • Does not update the CHANGELOG because my change does not significantly impact released code.

(Changes are covered by the current entry "Finalized the ffi module which was preview-released in 3.2.0. This involved a few additional API breaking changes in ffi. See #757.")

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar formal model or DRT infrastructure.

(This PR won't break DRT, but it will break the Java bindings and possibly others downstream)

@khieta khieta marked this pull request as draft June 20, 2024 16:10
@khieta
Copy link
Contributor Author

khieta commented Jun 20, 2024

Marking as a draft for now since this PR will break the Java bindings & I want to make sure things are fixable before I commit to the current design. Still happy for comments now though.

@khieta khieta mentioned this pull request Jun 20, 2024
@khieta khieta marked this pull request as ready for review July 3, 2024 17:25
@khieta
Copy link
Contributor Author

khieta commented Jul 3, 2024

Marking this PR as ready for review 🎉 The PR to fix the downstream Java break is here cedar-policy/cedar-java#173, and I'll get up a PR to fix the downstream Wasm break later this week.

Copy link
Contributor

@aaronjeline aaronjeline left a comment

Choose a reason for hiding this comment

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

Minor comments

cedar-policy/src/ffi/is_authorized.rs Outdated Show resolved Hide resolved
let policies = set
.into_iter()
.map(|policy| policy.parse(None))
.filter_map(|r| r.map_err(|e| errs.push(e)).ok())
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be more idiomatic here to use something like .partition instead of mutating a vec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly 🙂 The examples here https://doc.rust-lang.org/rust-by-example/error/iter_result.html show how to do this in a couple different ways (incl. using partition), but I think the filter_map + push approach is the most readable.

Copy link
Contributor

@cdisselkoen cdisselkoen left a comment

Choose a reason for hiding this comment

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

Looks good overall. Happy to see Slice gone.

@@ -373,7 +373,7 @@ impl From<PolicyId> for ast::PolicyID {
/// Identifier for a Template slot
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, RefCast)]
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, RefCast, Serialize, Deserialize)]
Copy link
Contributor

Choose a reason for hiding this comment

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

deny_unknown_fields?

Does repr(transparent) imply serde(transparent)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll make sure denny_unknown_fields gets added as part of #1041 (assuming this PR is merged first)

Copy link
Contributor

Choose a reason for hiding this comment

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

then my question is just, should we also have serde(transparent)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, didn't answer that part. From a quick google search: I'm not sure. Slot ids are definitely de/serializing like I'd expect (?principal and ?resource), so I don't think adding serde(transparent) will change anything, but maybe I should add it to be safe?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure either. As long as we have tests to catch regressions, I think we're fine without it

.clone()
.map_or(String::new(), |id| format!(" with id `{id}`"));
match self {
Self::Human(str) => crate::Policy::parse(id.map(|id| id.to_string()), str)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a crate::Policy constructor that takes a PolicyId instead of a String here, to avoid unparsing and reparsing. Doesn't have to be in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made an issue: #1049

cedar-policy/src/ffi/utils.rs Outdated Show resolved Hide resolved
cedar-policy/src/ffi/validate.rs Outdated Show resolved Hide resolved
cedar-policy/src/ffi/validate.rs Outdated Show resolved Hide resolved
cedar-policy/src/ffi/validate.rs Show resolved Hide resolved
@khieta khieta merged commit 8e92c1e into main Jul 9, 2024
14 of 18 checks passed
@khieta khieta deleted the khieta/cedar-757 branch July 9, 2024 14:02
@khieta khieta mentioned this pull request Jul 9, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cedar FFI Overhaul
3 participants