8000 Introduce unique index for enums stored by storages by vramik · Pull Request #12299 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,48 @@
*/
package org.keycloak.representations.idm.authorization;

import java.util.Map;
import java.util.Objects;
import org.keycloak.util.EnumWithStableIndex;

/**
* The decision strategy dictates how the policies associated with a given policy are evaluated and how a final decision
* is obtained.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public enum DecisionStrategy {
public enum DecisionStrategy implements EnumWithStableIndex {

/**
* Defines that at least one policy must evaluate to a positive decision in order to the overall decision be also positive.
*/
AFFIRMATIVE,
AFFIRMATIVE(0),

/**
* Defines that all policies must evaluate to a positive decision in order to the overall decision be also positive.
*/
UNANIMOUS,
UNANIMOUS(1),

/**
* Defines that the number of positive decisions must be greater than the number of negative decisions. If the number of positive and negative is the same,
* the final decision will be negative.
*/
CONSENSUS
CONSENSUS(2);

private final int stableIndex;
private static final Map<Integer, DecisionStrategy> BY_ID = EnumWithStableIndex.getReverseIndex(values());

private DecisionStrategy(int stableIndex) {
Objects.requireNonNull(stableIndex);
this.stableIndex = stableIndex;
}

@Override
public int getStableIndex() {
return stableIndex;
}

public static DecisionStrategy valueOfInteger(Integer id) {
return id == null ? null : BY_ID.get(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,42 @@
*/
package org.keycloak.representations.idm.authorization;

import java.util.Map;
import java.util.Objects;
import org.keycloak.util.EnumWithStableIndex;

/**
* The decision strategy dictates how the policies associated with a given policy are evaluated and how a final decision
* is obtained.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public enum Logic {
public enum Logic implements EnumWithStableIndex {

/**
* Defines that this policy follows a positive logic. In other words, the final decision is the policy outcome.
*/
POSITIVE,
POSITIVE(0),

/**
* Defines that this policy uses a logical negation. In other words, the final decision would be a negative of the policy outcome.
*/
NEGATIVE,
NEGATIVE(1);

private final int stableIndex;
private static final Map<Integer, Logic> BY_ID = EnumWithStableIndex.getReverseIndex(values());

private Logic(int stableIndex) {
Objects.requireNonNull(stableIndex);
this.stableIndex = stableIndex;
}

@Override
public int getStableIndex() {
return stableIndex;
}

public static Logic valueOfInteger(Integer id) {
return id == null ? null : BY_ID.get(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,46 @@
*/
package org.keycloak.representations.idm.authorization;

import java.util.Map;
import java.util.Objects;
import org.keycloak.util.EnumWithStableIndex;

/**
* The policy enforcement mode dictates how authorization requests are handled by the server.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public enum PolicyEnforcementMode {
public enum PolicyEnforcementMode implements EnumWithStableIndex {

/**
* Requests are denied by default even when there is no policy associated with a given resource.
*/
ENFORCING,
ENFORCING(0),

/**
* Requests are allowed even when there is no policy associated with a given resource.
*/
PERMISSIVE,
PERMISSIVE(1),

/**
* Completely disables the evaluation of policies and allow access to any resource.
*/
DISABLED
DISABLED(2);

private final int stableIndex;
private static final Map<Integer, PolicyEnforcementMode> BY_ID = EnumWithStableIndex.getReverseIndex(values());

private PolicyEnforcementMode(int stableIndex) {
Objects.requireNonNull(stableIndex);
this.stableIndex = stableIndex;
}

@Override
public int getStableIndex() {
return stableIndex;
}

public static PolicyEnforcementMode valueOfInteger(Integer id) {
return id == null ? null : BY_ID.get(id);
}
}
43 changes: 43 additions & 0 deletions core/src/main/java/org/keycloak/util/EnumWithStableIndex.java
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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -77,8 +78,7 @@
import org.keycloak.models.map.realm.entity.MapRequiredCredentialEntityImpl;
import org.keycloak.models.map.realm.entity.MapWebAuthnPolicyEntity;
import org.keycloak.models.map.realm.entity.MapWebAuthnPolicyEntityImpl;

import static org.keycloak.models.map.storage.jpa.hibernate.jsonb.JpaEntityMigration.MIGRATIONS;
import org.keycloak.util.EnumWithStableIndex;

public class JsonbType extends AbstractSingleColumnStandardBasicType<Object> implements DynamicParameterizedType {

Expand All @@ -105,7 +105,9 @@ public class JsonbType extends AbstractSingleColumnStandardBasicType<Object> imp
.addAbstractTypeMapping(MapWebAuthnPolicyEntity.class, MapWebAuthnPolicyEntityImpl.class))
.addMixIn(UpdatableEntity.class, IgnoreUpdatedMixIn.class)
.addMixIn(DeepCloner.class, IgnoredTypeMixIn.class)
.addMixIn(EntityWithAttributes.class, IgnoredMetadataFieldsMixIn.class);
.addMixIn(EntityWithAttributes.class, IgnoredMetadataFieldsMixIn.class)
.addMixIn(EnumWithStableIndex.class, EnumsMixIn.class)
;

abstract class IgnoredMetadataFieldsMixIn {
@JsonIgnore public abstract String getId();
Expand All @@ -115,6 +117,13 @@ abstract class IgnoredMetadataFieldsMixIn {
@JsonIgnore public abstract Boolean isClientRole();
}

abstract static class EnumsMixIn implements EnumWithStableIndex {

// we convert enums to its index and vice versa
@Override
@JsonValue public abstract int getStableIndex();
}

public JsonbType() {
super(JsonbSqlTypeDescriptor.INSTANCE, new JsonbJavaTypeDescriptor());
}
Expand Down Expand Up @@ -232,7 +241,7 @@ public Object fromString(String json) {
}

private ObjectNode migrate(ObjectNode tree, Integer entityVersion) {
return MIGRATIONS.getOrDefault(valueType, (node, version) -> node).apply(tree, entityVersion);
return JpaEntityMigration.MIGRATIONS.getOrDefault(valueType, (node, version) -> node).apply(tree, entityVersion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.keycloak.events.Event;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.map.common.TimeAdapter;

import java.util.Collections;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public AdminEventQuery operation(OperationType... operations) {

@Override
public AdminEventQuery resourceType(ResourceType... resourceTypes) {
mcb = mcb.compare(SearchableFields.RESOURCE_TYPE, EQ, Arrays.stream(resourceTypes));
mcb = mcb.compare(SearchableFields.RESOURCE_TYPE, IN, Arrays.stream(resourceTypes));
Copy link
Copy Markdown
Contributor

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 MapAdminEventEntity interface has the get/setResourceType methods in String form. 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, the AdminEventStoreProviderTest has no tests with searches that involve the resource type.

If you change the OperationType into String in 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)

Copy link
Copy Markdown
Contributor Author

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 to String
AdminEvent:

 public static final SearchableModelField<AdminEvent> RESOURCE_TYPE   = new SearchableModelField<>("resourceType", String.class);

@vramik vramik Jun 3, 2022 •
Copy link
Copy Markdown
Contributor Author

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:

put(ADMIN_EVENTS_PREDICATES, AdminEvent.SearchableFields.RESOURCE_TYPE, mapAdminEventEntity -> ResourceType.valueOf(mapAdminEventEntity.getResourceType()));

Should this topic be put into its own separate PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

I honestly have no idea, but this is definitely a strange behavior in my opinion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this topic be put into its own separate PR?

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.

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've created #12485

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1771,10 +1771,12 @@ public String toString() {
return String.format("%s@%08x", getId(), hashCode());
}

@Override
public CibaConfig getCibaPolicy() {
return new CibaConfig(this);
}

@Override
public ParConfig getParPolicy() {
return new ParConfig(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.map.annotations.GenerateEntityImplementations;
import org.keycloak.models.map.common.AbstractEntity;

import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.ExpirableEntity;
import org.keycloak.models.map.common.UpdatableEntity;
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
<infinispan.version>12.1.7.Final</infinispan.version>
<infinispan.protostream.processor.version>4.4.1.Final</infinispan.protostream.processor.version>
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
<jackson.version>2.12.1</jackson.version>
<jackson.databind.version>2.12.6.1</jackson.databind.version>
<jackson.version>2.13.2</jackson.version>
<jackson.databind.version>2.13.2.2</jackson.databind.version>
<jackson.annotations.version>${jackson.databind.version}</jackson.annotations.version>
<jakarta.mail.version>1.6.5</jakarta.mail.version>
<jboss-jaxrs-api_2.1_spec>2.0.1.Final</jboss-jaxrs-api_2.1_spec>
Expand Down Expand Up @@ -177,7 +177,7 @@
<jmeter.plugin.version>1.9.0</jmeter.plugin.version>
<jmeter.analysis.plugin.version>1.0.4</jmeter.analysis.plugin.version>
<minify.plugin.version>1.7.6</minify.plugin.version>
<osgi.bundle.plugin.version>2.3.7</osgi.bundle.plugin.version>
<osgi.bundle.plugin.version>2.4.0</osgi.bundle.plugin.version>
<wildfly.plugin.version>2.0.1.Final</wildfly.plugin.version>
<nexus.staging.plugin.version>1.6.5</nexus.staging.plugin.version>
<frontend.plugin.version>1.12.0</frontend.plugin.version>
Expand Down
Loading
0