-
Notifications
You must be signed in to change notification settings - Fork 107
Internal data structures for Generalized templates #1732
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
base: main
Are you sure you want to change the base?
Internal data structures for Generalized templates #1732
Conversation
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
cedar-policy-core/src/ast/policy.rs
Outdated
}, | ||
|
||
/// Value provided for a slot is not of the type annotated | ||
#[error("value provided `{value}` for `{slot}` is not of type annotated `{ty}`")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[error("value provided `{value}` for `{slot}` is not of type annotated `{ty}`")] | |
#[error("value provided `{value}` for `{slot}` is not of declared type `{ty}`")] |
/// Values of the slots | ||
values: SlotEnv, | ||
/// Generalized values of the slots | ||
generalized_values: GeneralizedSlotEnv, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be simpler to store the values of all slots in a GeneralizedSlotEnv
? EntityUID
s could be stored as a RestrictedExpr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent some more time thinking about this and I think one of the consequences would be that now every time we have code involving templates we would have to handle the regular templates and the generalized case differently since the values of their slots appear in different fields and we would also have to enforce the invariant that only one of these HashMaps
can have values in it. I was considering making the default value field of type HashMap<SlotId, RestrictedExpr>
however, that would cause issues with the EST format.
One alternative solution in terms of making it easier for user's of the API not needing to supply values in two HashMaps
and the potentially confusing error messages is to have the API handle the translation to our internal data structures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, you think it would be better to have a single map for internal data structures, two maps for the EST, and handle the conversion during translation. That sounds fine to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update this comment thread based on our out-of-band discussion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After implementing a version of this PR that used a single HashMap to store all slot values, there were issues involved with need to introduce many unwrap
calls to translate between RestrictedExpr
and EntityUID
. Therefore, the choice ultimately made is to internally represent values of templates using two HashMap's and for user convenience at the API level to expose a single HashMap from SlotId
to RestrictedExpr
. Internally, we will do this conversion to use two HashMap's and so therefore we have to maintain the invariant that in value there are only bindings for SlotId's ?principal
and ?resource
and in generalized_values there are only bindings for generalized slots.
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you say a little more about the relation between this and #1722. And I guess lay out more the implementation plan so I know what to expect coming in future PRs.
Signed-off-by: Alan Wang <[email protected]>
The changes in #1722 only allow the current |
Signed-off-by: Alan Wang <[email protected]>
Signed-off-by: Alan Wang <[email protected]>
This reverts commit 48ad73c.
This reverts commit d83ee15.
This reverts commit 5913651.
…EST format"" This reverts commit 4b4ed2a.
This reverts commit 92f3c36.
This reverts commit d83ee15.
This reverts commit 5913651.
This reverts commit 8e725ec.
…ing error messages
/// Values of the slots | ||
values: SlotEnv, | ||
/// Generalized values of the slots | ||
generalized_values: GeneralizedSlotEnv, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update this comment thread based on our out-of-band discussion?
Signed-off-by: Alan Wang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple questions, but looks good
|
||
/// Struct storing the pairs of SlotId's and their corresponding type | ||
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)] | ||
pub struct GeneralizedSlotsDeclaration(BTreeMap<SlotId, JSONSchemaType<RawName>>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a first pass it's not clear why we need this and ValidatorGeneralizedSlotsDeclaration
. Probably worth a comment explaining this design
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, yeah will include a comment. The reason why we need both is that JSONSchemaType<RawName>
can't be used by the typechecker and needs to be converted to a validator type by combing information from the Schema.
} | ||
|
||
/// Construct a [`Type`] from [`json_schema::Type<RawName>`] | ||
pub fn json_schema_type_to_validator_type( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this function not already exist somewhere else? I feel like it must
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the other place where a similar conversion occurs is the function from_schema_fragments
in cedar-policy-core/src/validator/schema.rs
but it's basically inlined within it.
Description of changes
Introduce internal data structures for generalized templates. The main changes are having a policy now store a map from slots to types. The differences in the changes between Allow principal resource slots in condition and this PR are here.
Issue #, if available
Checklist for requesting a review
The change in this PR is (choose one, and delete the other options):
cedar-policy
(e.g., changes to the signature of an existing API).cedar-policy
(e.g., addition of a new API).cedar-policy
.cedar-policy-core
,cedar-validator
, etc.)I confirm that this PR (choose one, and delete the other options):
I confirm that
cedar-spec
(choose one, and delete the other options):cedar-spec
, and how you have tested that your updates are correct.)cedar-spec
. (Post your PR anyways, and we'll discuss in the comments.)I confirm that
docs.cedarpolicy.com
(choose one, and delete the other options):cedar-docs
. PRs should be targeted at astaging-X.Y
branch, notmain
.)