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

RFC 62 implementation #1327

Merged
merged 19 commits into from
Nov 27, 2024
Merged

RFC 62 implementation #1327

merged 19 commits into from
Nov 27, 2024

Conversation

shaobo-he-aws
Copy link
Contributor

@shaobo-he-aws shaobo-he-aws commented Nov 19, 2024

Description of changes

Issue #, if available

#1329

Checklist for requesting a review

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

  • A backwards-compatible change requiring a minor version bump to cedar-policy (e.g., addition of a new API).

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

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).

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.

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

  • Requires updates, and I have made / will make these updates myself. (Please include in your description a timeline or link to the relevant PR in cedar-docs. PRs should be targeted at a staging-X.Y branch, not main.)

Signed-off-by: Shaobo He <[email protected]>
@shaobo-he-aws shaobo-he-aws marked this pull request as draft November 19, 2024 23:59
@@ -998,6 +1002,74 @@ impl Node<Option<cst::Add>> {
fn to_expr(&self) -> Result<ast::Expr> {
self.to_expr_or_special()?.into_expr()
}

// Peel the grammar onion until we see valid RHS
Copy link
Contributor

Choose a reason for hiding this comment

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

🧅

Signed-off-by: Shaobo He <[email protected]>
Signed-off-by: Shaobo He <[email protected]>
Signed-off-by: Shaobo He <[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.

Looks great!

cedar-policy-core/src/parser/cst_to_ast.rs Show resolved Hide resolved
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws left a comment

Choose a reason for hiding this comment

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

Code looks good.

Two test case requests:

  1. Some more invalid examples in the cst_to_ast.rs tests. There are a few different possible Err return paths that might not be tested. Some of them might be covered by existing has test, but it'd be good to be sure. (You mention there's some dead code, so maybe just annotate any unreachable error cases)
  2. A validator test check that principal has foo.bar.baz && principal.foo.bar.baz validates as expected for optional attribute foo, bar and baz

@john-h-kastner-aws
Copy link
Contributor

Also, we should add a formatter test case.

@john-h-kastner-aws
Copy link
Contributor

and while I'm suggesting more tests cases, we should have a test for conversion to EST

Either::Left(attr) => nonempty![attr],
Either::Right(ids) => ids.map(|id| id.to_smolstr()),
};
let (first, rest) = attrs.split_first();
Copy link
Contributor

Choose a reason for hiding this comment

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

This wouldn't be trivial since the types are all different, but I wonder if there's some nice way to make this code generic enough to be shared here and in construct_exprs_extended_has in the CST to AST conversion. I think we'd just need some sort of ExprBuilder<T> trait providing has_attr(T,SmolStr)->T, get_attr(T, SmolStr)->T and and(T, T) -> T. IDK if that'd be worthwhile, but maybe it's an idea that could help share code elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems to me like a good idea for a followup; this PR is large enough already and I think it would be good to review that trait change on its own

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's a good idea but worth a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

it would be very cool if cst->{ast,est} could share all of their code, but that sounds like a lot of work

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

Either::Left(attr) => nonempty![attr],
Either::Right(ids) => ids.map(|id| id.to_smolstr()),
};
let (first, rest) = attrs.split_first();
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems to me like a good idea for a followup; this PR is large enough already and I think it would be good to review that trait change on its own

cedar-policy-core/src/parser/cst_to_ast.rs Outdated Show resolved Hide resolved
Comment on lines +6 to +28
// An example from RFC
permit (
principal is User,
action == Action::"preview",
resource == Movie::"Blockbuster"
)
when
{
// extended has
principal
has
// contactInfo
contactInfo.
// address
address
.
// zip
zip &&
// we are safe to access all attributes
principal.contactInfo
.address
.zip == "90210"
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not very pretty, should we open an issue to improve this formatting? Or I guess the culprit is the comments; what does the formatter produce for this example without comments?

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 add another policy without comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will also open an issue after the PR is merged.

Signed-off-by: Shaobo He <[email protected]>
Signed-off-by: Shaobo He <[email protected]>
@shaobo-he-aws shaobo-he-aws merged commit c554807 into main Nov 27, 2024
19 checks passed
@shaobo-he-aws shaobo-he-aws deleted the feature/shaobo/extended-has branch November 27, 2024 17:48
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