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

Added Array, Set and operator 'IN' #110

Closed
wants to merge 18 commits into from
Closed

Conversation

ystrict
Copy link

@ystrict ystrict commented Jun 19, 2023

Hi, I've added implementation for array, set and 'in' support. Please take a look on it, will be happy if you make some comments!

@ystrict ystrict requested a review from a team as a code owner June 19, 2023 13:36
@ystrict ystrict requested review from tgeens and rschev June 19, 2023 13:36
@tgeens
Copy link
Contributor

tgeens commented Jun 21, 2023

I was going to ask to add a test in QuerySetToThunkExpressionConverterTest first, but that probably depends on xenit-eu/opa-java-client#12 ?

@ystrict
Copy link
Author

ystrict commented Jun 23, 2023

QuerySetToThunkExpressionConverterTest

That's right. I can add default implementation (throwing UnsupportedException) to RegoVisitor (T visit(SetTerm setTerm);) not to fail the build of ThunX. And then add test to ThunX after merging opa-client

@tgeens
Copy link
Contributor

tgeens commented Jun 26, 2023

@ystrict the opa-java-client PR has been merged. Can you update this PR accordingly ?

@ystrict
Copy link
Author

ystrict commented Jun 26, 2023

@ystrict the opa-java-client PR has been merged. Can you update this PR accordingly ?

Done, but please take a look on this one firstly(made some small fixes) - xenit-eu/opa-java-client#26

@ystrict
Copy link
Author

ystrict commented Jun 26, 2023

@tgeens can you please restart the build? looks like dependencies didn't get in time

@ystrict
Copy link
Author

ystrict commented Jun 26, 2023

@tgeens Can you please take a look on this build issue? I've checked locally and it looks like something is wrong with the github publishing process of opa-client. I've checked your sonatype repo and it contains the correct version of opa-client with my last updates. Locally gradle gets the correct jars and so on, but it's not building and IDE says that the bytecode doesn't match the source code, it's strange

@tgeens
Copy link
Contributor

tgeens commented Jun 27, 2023

@tgeens Can you please take a look on this build issue? I've checked locally and it looks like something is wrong with the github publishing process of opa-client. I've checked your sonatype repo and it contains the correct version of opa-client with my last updates. Locally gradle gets the correct jars and so on, but it's not building and IDE says that the bytecode doesn't match the source code, it's strange

You should update the dependency to opa-java-client in thunx-bom/build.gradle from 0.4.0 to 0.4.1-SNAPSHOT

@ystrict
Copy link
Author

ystrict commented Jun 27, 2023

@tgeens Can you please take a look on this build issue? I've checked locally and it looks like something is wrong with the github publishing process of opa-client. I've checked your sonatype repo and it contains the correct version of opa-client with my last updates. Locally gradle gets the correct jars and so on, but it's not building and IDE says that the bytecode doesn't match the source code, it's strange

You should update the dependency to opa-java-client in thunx-bom/build.gradle from 0.4.0 to 0.4.1-SNAPSHOT

done

@tgeens
Copy link
Contributor

tgeens commented Jun 27, 2023

Ok, but that won't be sufficient. See my comment on CollectionValue.

@ystrict
Copy link
Author

ystrict commented Jul 3, 2023

Ok, but that won't be sufficient. See my comment on CollectionValue.

Hi, can you please take a look on my changes?

@ystrict
Copy link
Author

ystrict commented Jul 11, 2023

@tgeens Hi, I've changed the version but it's still falling. For me it looks like there are some building or publishing issues, because on local using publishToMavenLocal it works good. Can you please take a look and help me in your free time?

@vierbergenlars
Copy link
Member

because on local using publishToMavenLocal it works good.

I found a compile error here: https://github.com/xenit-eu/thunx/pull/110/files#diff-e1519f085982147798370b05f00c49957d1c5f2be3db626be759193e055631a3R155

It's trying to pass Rego objects to something that should be a thunx Scalar.

It's strange that it compiles for you locally, do you still have some uncommitted changes, or perhaps an includeBuild() left in settings.gradle that is replacing some dependencies?

@@ -57,6 +63,7 @@ public ThunkExpression<Boolean> convert(Query query) {

var expressions = query.stream()
.map(this::convert)
.filter(Objects::nonNull)
Copy link
Member

Choose a reason for hiding this comment

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

Are there ever null values returned from this::convert?

I think that would indicate a bug, so filtering them out might be counterproductive, and actually result in an expression that can't be converted being dropped, thereby widening the access policies, which is an undesired effect.

We should prefer erroring out (even if it's a null pointer exception) over accidentally allowing access to data that should have been protected.

Copy link
Author

Choose a reason for hiding this comment

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

This filtering is done because there is an ability to return empty Term for in operation.
Example:
"index": 4, "terms": [ { "type": "ref", "value": [ { "type": "var", "value": "internal" }, { "type": "string", "value": "member_2" } ] }, { "type": "ref", "value": [ { "type": "var", "value": "input" }, { "type": "string", "value": "entity" }, { "type": "string", "value": "accountId" } ] }, { "type": "array", "value": [] # will return null in conversion, cause we need to skip empty in clause } ] }

Copy link
Author

Choose a reason for hiding this comment

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

That's why in this place we are filtering empty in cases

@@ -6,5 +6,8 @@ public interface ThunkExpressionVisitor<T, C> {
T visit(FunctionExpression<?> functionExpression, C context);
T visit(SymbolicReference symbolicReference, C context);
T visit(Variable variable, C context);
default T visit(CollectionValue collection, C context) {
throw new UnsupportedOperationException("Visit for CollectionValue is not yet implemented.");
Copy link
Member

Choose a reason for hiding this comment

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

During development/experimenting this is fine, but we should remove the default implementation.

At some, all visitors need to implement this method, so we can be sure that we can handle collection values.

One of the important ones that is still missing an implementation is the ExpressionJsonConverter.JsonEncoderVisitor, without which we can't send the partial expression to the application.


@Override
public ThunkExpression<?> visit(ArrayTerm arrayTerm) {
arrayTerm.getValue().forEach(scalarTerm -> scalarTerm.accept(this));
Copy link
Member

Choose a reason for hiding this comment

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

Doing this would unpack a rego array ['a', 'b', ['c', 'd']] into ['a', 'b', 'c', 'd'], which is not a valid conversion.

Instead, this would probably need to use the same code as used above to collect scalars, and add the whole resulting CollectionValue as a scalar value.

Copy link
Author

Choose a reason for hiding this comment

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

Now SetTerm and ArrayTerm extends Term, that means that SetTerm cannot have SetTerm as a value. If you want to have such case when SetTerm contains SetTerm, then we need to change opa-client, to make SetTerm and ArrayTerm extend ScalarTerm

Copy link
Author

Choose a reason for hiding this comment

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

I've added support of collection in collection for opa-client (xenit-eu/opa-java-client#28) and for thunx


@Override
public ThunkExpression<?> visit(SetTerm setTerm) {
setTerm.getValue().forEach(scalarTerm -> scalarTerm.accept(this));
Copy link
Member

Choose a reason for hiding this comment

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

Same remark as for the array case, nested sets shouldn't be flattened.

Copy link
Author

Choose a reason for hiding this comment

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

fixed


@Override
public ThunkExpression<?> visit(QuerySet querySet) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

All these rego types are unexpected in a scalar collection, so they should throw an exception instead of silently dropping the term.

For example, in rego, you can have a Ref in an array, for example, when constructing input.entity.something in [input.entity.optionA, input.entity.optionB]

That is probably (I didn't check) a valid rego expression, but is not supported as entry in the current CollectionValue.

It's fine to limit ourselves to only scalar values for now, but we should not drop those terms silently.

Copy link
Author

Choose a reason for hiding this comment

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

I've added support of collection in collection for opa-client (xenit-eu/opa-java-client#28) and for thunx

@ystrict
Copy link
Author

ystrict commented Jul 12, 2023

because on local using publishToMavenLocal it works good.

I found a compile error here: https://github.com/xenit-eu/thunx/pull/110/files#diff-e1519f085982147798370b05f00c49957d1c5f2be3db626be759193e055631a3R155

It's trying to pass Rego objects to something that should be a thunx Scalar.

It's strange that it compiles for you locally, do you still have some uncommitted changes, or perhaps an includeBuild() left in settings.gradle that is replacing some dependencies?

includeBuild is commented, but everything builds fine without any errors and it also works. Really strange, where did you find this error? It's trying to pass Rego objects to something that should be a thunx Scalar.

@vierbergenlars
Copy link
Member

It's trying to pass Rego objects to something that should be a thunx Scalar.

This line:

Scalar.of(
   Set.of(new Term.Numeric(4), new Term.Numeric(5))
)

Scalar.of() accepts Collection<Scalar<?>>, but what you are creating is Set<Term.Numeric>. Term.Numeric comes from the opa-java-client, and Scalar is defined in thunx-model.

These types are not compatible with each other: Term.Numeric (the rego object) does not extend or implement Scalar<?> (the thunx model)

@ystrict
Copy link
Author

ystrict commented Jul 12, 2023

It's trying to pass Rego objects to something that should be a thunx Scalar.

This line:

Scalar.of(
   Set.of(new Term.Numeric(4), new Term.Numeric(5))
)

Scalar.of() accepts Collection<Scalar<?>>, but what you are creating is Set<Term.Numeric>. Term.Numeric comes from the opa-java-client, and Scalar is defined in thunx-model.

These types are not compatible with each other: Term.Numeric (the rego object) does not extend or implement Scalar<?> (the thunx model)

thanks, let me check

@ystrict
Copy link
Author

ystrict commented Jul 21, 2023

It's trying to pass Rego objects to something that should be a thunx Scalar.

This line:

Scalar.of(
   Set.of(new Term.Numeric(4), new Term.Numeric(5))
)

Scalar.of() accepts Collection<Scalar<?>>, but what you are creating is Set<Term.Numeric>. Term.Numeric comes from the opa-java-client, and Scalar is defined in thunx-model.

These types are not compatible with each other: Term.Numeric (the rego object) does not extend or implement Scalar<?> (the thunx model)

Hi, I've added json functionality, please take a look as soon as you have time

@ystrict
Copy link
Author

ystrict commented Aug 16, 2023

Hi, my opa-client PR(xenit-eu/opa-java-client#28) was merged, please take a look on this one.

}

private static JsonScalarDto<?> processCollectionDto(CollectionValue collectionValue) {
Collection<JsonExpressionDto> result;
if (JsonCollectionValueDto.getTypeByClass(collectionValue.getResultType()).equals("set")) {
Copy link
Member

Choose a reason for hiding this comment

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

If you need to do typechecks here, this indicates that we need two separate visit methods: one for the set type and one for the array type.

This also means that we need two separate classes: CollectionValue needs to be split up in ArrayCollectionValue and SetCollectionValue. You can keep CollectionValue as an interface so it can be used in places where an array or a set would be sufficient.

Copy link
Author

Choose a reason for hiding this comment

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

Yeap, I agree with you, but can we merge this PR and create issue, for this? I will pick it up and fix in next PR, cause this one became to big and too time consuming. This change requires rewriting the whole PR:(

@vierbergenlars
Copy link
Member

@ystrict: We are working on upgrading Thunx to Spring Boot 3, which will make it incompatible with Spring Boot 2 applications.

Please let us know if you have to integrate this in a Spring Boot 2 application, so we can plan a final release for Spring Boot 2 containing this PR.

@ystrict
Copy link
Author

ystrict commented Aug 18, 2023

@ystrict: We are working on upgrading Thunx to Spring Boot 3, which will make it incompatible with Spring Boot 2 applications.

Please let us know if you have to integrate this in a Spring Boot 2 application, so we can plan a final release for Spring Boot 2 containing this PR.

Can you please include my changes to Spring Boot 2 release?

Copy link

sonarcloud bot commented Dec 19, 2023

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@thijslemmens
Copy link

Really sorry to close this PR, but at this point we cannot get it merged in. Sorry @ystrict

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.

4 participants