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

ast: Marshal non-string object keys when converting to interface{} #2577

Merged
merged 2 commits into from
Jul 27, 2020

Conversation

tsandall
Copy link
Member

Previously, when OPA attempted to convert an ast.Object to interface{}
it would error if the ast.Object contained any keys that were not
ast.String values. This behaviour was implemented because JSON only
supports object keys as strings.

This commit changes the conversion implementation to simply JSON
marshal non-string object keys when they are encountered. This way
we avoid runtime errors which can be difficult to debug.

Fixes #516

Signed-off-by: Torin Sandall torinsandall@gmail.com

Previously, when OPA attempted to convert an ast.Object to interface{}
it would error if the ast.Object contained any keys that were not
ast.String values. This behaviour was implemented because JSON only
supports object keys as strings.

This commit changes the conversion implementation to simply JSON
marshal non-string object keys when they are encountered. This way
we avoid runtime errors which can be difficult to debug.

Fixes open-policy-agent#516

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
@tsandall tsandall requested a review from patrick-east July 24, 2020 15:57
@tsandall
Copy link
Member Author

cc @timothyhinrichs @srenatus @jaspervdj-luminal tagging a few folks on this change to get thoughts.

Copy link
Contributor

@patrick-east patrick-east left a comment

Choose a reason for hiding this comment

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

I think of the options we have this seems like the best approach. IMO I much prefer this over raising an error.

@timothyhinrichs
Copy link
Member

Seems good to me. Gives us more flexibility inside of Rego AND moves us toward fewer runtime errors.

Copy link
Contributor

@jaspervdj-luminal jaspervdj-luminal left a comment

Choose a reason for hiding this comment

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

I also like this, makes it easy to debug what the problematic keys were.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

I was thinking of some potential trouble in unmarshaling, but this is not what's happening here. LGTM :)

}
vi, err := ValueToInterface(v.Value, resolver)
if err != nil {
return err
}
buf[asStr] = vi
buf[str] = vi
Copy link
Contributor

Choose a reason for hiding this comment

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

In case of a clash ({"1":0, 1:0}), the key processed second will overwrite the one processed first, right?

I see no solid solution here, only ways to make it less likely, like marshaling the example thing to {"[1]":0,"1":0}.

Copy link
Contributor

Choose a reason for hiding this comment

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

Tried it briefly to verify:

> { 1: true, "1": false, "{}": "foo", {}: "bar" }
{
  "1": false,
  "{}": "bar"
}
>

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice find. Agreed, there's no good solution here. I think this is still better than having policy evaluation return an error. I'm going to go ahead and merge.

@tsandall tsandall merged commit 8b550ce into open-policy-agent:master Jul 27, 2020
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.

Improve error message when using an invalid object key
5 participants