Repository navigation
Introduce unique index for enums stored by storages #12299
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/keycloak/util/EnumWithStableIndex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2022 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.keycloak.util; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Classes implementing this interface guarantee that for each instance of this class, | ||
| * there exists an mutually unique integer which is stable in time, and identifies | ||
| * always the same instance of this class. | ||
| * The index might be used for persistence, hence the index of a particular item | ||
| * cannot be changed. | ||
| * This is mostly usable for @{code enum}s. | ||
| */ | ||
| public interface EnumWithStableIndex { | ||
| /** | ||
| * @return Unique numeric index which is stable in time and identifies an instance. | ||
| * Reusing the same index for two distinct entries of the same class is forbidden even | ||
| * if they cannot exist at the same time (e.g. one is deleted before other is introduced). | ||
| */ | ||
| public int getStableIndex(); | ||
|
|
||
| public static <E extends EnumWithStableIndex> Map<Integer, E> getReverseIndex(E[] values) { | ||
| return Stream.of(values).collect(Collectors.toMap(EnumWithStableIndex::getStableIndex, Function.identity())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this method doesn't work with CHM and Hotrod because the
MapAdminEventEntityinterface has theget/setResourceTypemethods inStringform. When I tested the events I noticed that the type passed to the mcb in this method must match the type found in the entity's interface (CHM uses comparators that simply don't match if the types are different) and I believe this particular case is not being tested properly at the moment. In other words, theAdminEventStoreProviderTesthas no tests with searches that involve the resource type.If you change the
OperationTypeintoStringin the entity's interface, you will see that the same test fails with CHM because the CHM won't be able to retrieve the events when searching by operation type.So in this case we either use the enum type in the entity's interface, or change the above to use
Arrays.stream(resourceTypes).map(Enum::toString)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.
Thanks for pointing this out. I agree it is not very well tested. I believe we cannot use enum type in the entity interface due to custom event types (KEYCLOAK-9573). We have to be able to store exact string of the custom type (test
AdminEventsStoreProviderTest.handleCustomResourceTypeEvents). Unless we'd store the value somewhere else (e.g. we can add attributes and put it there).But with custom types querying starts to be a little bit tricky. In scenario when there is more than one custom resource type and admin events are queried by
ResourceType.CUSTOM, all custom events are returned regardless of its actual value. Is this expected behavior?If not we'd need to make additional changes anyway.
If so (and we don't want to store custom type value elsewhere) easiest way seems to be to use
Arrays.stream(resourceTypes).map(Enum::toString)but in that case I'd also change fieldType toStringAdminEvent:Uh oh!
There was an error while loading. Please reload this page.
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.
Or maybe the cleanest solution (in case we won't support search by custom types) would be to change implementation of
MapFieldPredicates:Should this topic be put into its own separate PR?
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 honestly have no idea, but this is definitely a strange behavior in my opinion.
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.
Yeah, I think so. This PR doesn't seem like the proper place to handle these resource type inconsistencies. We should probably discuss that as part of a separate issue.
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.
Is there this issue filed already?
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've created #12485