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

Allow typed unknown resource and principal #1391

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

B-Lorentz
Copy link
Contributor

@B-Lorentz B-Lorentz commented Dec 23, 2024

Description of changes

Allows the caller to create a request with an unknown principal or resource, that is still known to belong to a certain type.

I had to modify the evaluator a little bit, to take the type annotation into account for 'is' expressions. I know this is enroaching on the open rfc of https://github.com/cedar-policy/rfcs/pull/83/files
but I found it to be very useful for a 'query construction' usecase, similar to the one outlined here:
https://cedarland.blog/usage/partial-evaluation/content.html#what-can-alice-access

Without this, the only way to remove from the residual policies which are clearly excluded by the scope is to construct a full dummy entity ID of the right type, and then insert into the entity store an entry with that id.
But this requires all the fields of the entity to be explicitly set to 'unknown', and makes the evaluator assume the entity has no parents (since the parents of a concrete entity can't be unknown atm)

Issue #, if available

#1393

Checklist for requesting a review

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

  • A change (breaking or otherwise) that only impacts unreleased or experimental code.

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.

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

  • I'm not sure how my change impacts cedar-spec. (Post your PR anyways, and we'll discuss in the comments.)
    (I guess partial-eval is not part of the formal spec, right)

I confirm that docs.cedarpolicy.com (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar language specification.

cedar-policy-core/src/ast/request.rs Show resolved Hide resolved
cedar-policy/src/api.rs Outdated Show resolved Hide resolved
cedar-policy/src/api.rs Show resolved Hide resolved
Signed-off-by: Lőrinc Bódy <[email protected]>
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.

Thanks for this! Very nice code. Appreciate the helpful incidental refactors too.

cedar-policy-core/src/evaluator.rs Show resolved Hide resolved
cedar-policy/CHANGELOG.md Outdated Show resolved Hide resolved
B-Lorentz and others added 4 commits January 4, 2025 13:27
Signed-off-by: Lőrinc Bódy <[email protected]>
Signed-off-by: Lőrinc Bódy <[email protected]>
…ore::ast::EntityType in the public api

Signed-off-by: Lőrinc Bódy <[email protected]>
let (arg1, arg2) = match (
self.partial_interpret(arg1, slots)?,
self.partial_interpret(arg2, slots)?,
) {
(PartialValue::Value(v1), PartialValue::Value(v2)) => (v1, v2),
(PartialValue::Value(v1), PartialValue::Residual(e2)) => {
return Ok(PartialValue::Residual(Expr::binary_app(*op, v1.into(), e2)))
if let Some(val) = self.short_circuit_typed_residual(&v1, &e2, *op) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: IMO, short_circuit_typed_residual should be "inlined" here, e.g., by more precise pattern matching.

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 could do that.. but it would get quite a but long

@@ -901,6 +918,35 @@ impl<'e> Evaluator<'e> {
PartialValue::Residual(r) => Ok(Either::Right(r)),
}
}

fn short_circuit_typed_residual(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can do better here: Two unknowns with different types are not equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, that would be consistent

///
/// This information is taken into account when evaluating 'is', '==' and '!=' expressions.
#[must_use]
pub fn unknown_action_with_type(self, action_type: EntityTypeName) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be more conservative here: not all EntityTypeName are allowed; only those with basename Action.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, setting a concrete action just above also doesn't have such a check.

@shaobo-he-aws
Copy link
Contributor

It looks reasonable to me. That being said, this PR kinda motivates #209. Specifically, changes of PE should not impact concrete evaluation.

@B-Lorentz
Copy link
Contributor Author

It looks reasonable to me. That being said, this PR kinda motivates #209. Specifically, changes of PE should not impact concrete evaluation.

Should we just hold off this PR, and then rebase it on top of yours?

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.

3 participants