Change enum ResourceType to interface with String constants

Closes #12485
This commit is contained in:
vramik 2022-06-21 13:57:48 +02:00 committed by Hynek Mlnařík
parent c3c8b9f0c8
commit 3b5a578934
14 changed files with 59 additions and 251 deletions

View file

@ -20,7 +20,6 @@ package org.keycloak.events.jpa;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AdminEventQuery;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
@ -77,14 +76,8 @@ public class JpaAdminEventQuery implements AdminEventQuery {
}
@Override
public AdminEventQuery resourceType(ResourceType... resourceTypes) {
List<String> resourceTypeStrings = new LinkedList<String>();
for (ResourceType e : resourceTypes) {
resourceTypeStrings.add(e.toString());
}
predicates.add(root.get("resourceType").in(resourceTypeStrings));
public AdminEventQuery resourceType(List<String> resourceTypes) {
predicates.add(root.get("resourceType").in(resourceTypes));
return this;
}

View file

@ -210,11 +210,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
adminEventEntity.setRealmId(adminEvent.getRealmId());
setAuthDetails(adminEventEntity, adminEvent.getAuthDetails());
adminEventEntity.setOperationType(adminEvent.getOperationType().toString());
if (adminEvent.getResourceTypeAsString() != null) {
adminEventEntity.setResourceType(adminEvent.getResourceTypeAsString());
}
adminEventEntity.setResourceType(adminEvent.getResourceType());
adminEventEntity.setResourcePath(adminEvent.getResourcePath());
adminEventEntity.setError(adminEvent.getError());
@ -231,11 +227,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
adminEvent.setRealmId(adminEventEntity.getRealmId());
setAuthDetails(adminEvent, adminEventEntity);
adminEvent.setOperationType(OperationType.valueOf(adminEventEntity.getOperationType()));
if (adminEventEntity.getResourceType() != null) {
adminEvent.setResourceTypeAsString(adminEventEntity.getResourceType());
}
adminEvent.setResourceType(adminEventEntity.getResourceType());
adminEvent.setResourcePath(adminEventEntity.getResourcePath());
adminEvent.setError(adminEventEntity.getError());

View file

@ -50,7 +50,7 @@ public class EventUtils {
adminEvent.setRealmId(adminEventEntity.getRealmId());
setAuthDetails(adminEvent, adminEventEntity);
adminEvent.setOperationType(adminEventEntity.getOperationType());
adminEvent.setResourceTypeAsString(adminEventEntity.getResourceType());
adminEvent.setResourceType(adminEventEntity.getResourceType());
adminEvent.setResourcePath(adminEventEntity.getResourcePath());
adminEvent.setError(adminEventEntity.getError());
@ -68,7 +68,7 @@ public class EventUtils {
mapAdminEvent.setRealmId(adminEvent.getRealmId());
setAuthDetails(mapAdminEvent, adminEvent.getAuthDetails());
mapAdminEvent.setOperationType(adminEvent.getOperationType());
mapAdminEvent.setResourceType(adminEvent.getResourceTypeAsString());
mapAdminEvent.setResourceType(adminEvent.getResourceType());
mapAdminEvent.setResourcePath(adminEvent.getResourcePath());
mapAdminEvent.setError(adminEvent.getError());

View file

@ -21,12 +21,12 @@ import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AdminEvent.SearchableFields;
import org.keycloak.events.admin.AdminEventQuery;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.map.storage.QueryParameters;
import org.keycloak.models.map.storage.criteria.DefaultModelCriteria;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
@ -86,8 +86,8 @@ public class MapAdminEventQuery implements AdminEventQuery {
}
@Override
public AdminEventQuery resourceType(ResourceType... resourceTypes) {
mcb = mcb.compare(SearchableFields.RESOURCE_TYPE, IN, Arrays.stream(resourceTypes));
public AdminEventQuery resourceType(List<String> resourceTypes) {
mcb = mcb.compare(SearchableFields.RESOURCE_TYPE, IN, resourceTypes);
return this;
}

View file

@ -64,7 +64,7 @@ public class AdminEvent {
this.time = toCopy.getTime();
this.realmId = toCopy.getRealmId();
this.authDetails = new AuthDetails(toCopy.getAuthDetails());
this.resourceType = toCopy.getResourceTypeAsString();
this.resourceType = toCopy.getResourceType();
this.operationType = toCopy.getOperationType();
this.resourcePath = toCopy.getResourcePath();
this.representation = toCopy.getRepresentation();
@ -186,35 +186,11 @@ public class AdminEvent {
*
* @return
*/
public ResourceType getResourceType() {
if (resourceType == null) {
return null;
}
try {
return ResourceType.valueOf(resourceType);
}
catch (IllegalArgumentException e) {
return ResourceType.CUSTOM;
}
}
public void setResourceType(ResourceType resourceType) {
this.resourceType = resourceType == null ? null : resourceType.toString();
}
/**
* Returns the type as string. Custom resource types with values different from {@link ResourceType} are possible. In this case {@link #getResourceType()} returns <code>CUSTOM</code>.
*
* @return
*/
public String getResourceTypeAsString() {
public String getResourceType() {
return resourceType;
}
/**
* Setter for custom resource types with values different from {@link ResourceType}.
*/
public void setResourceTypeAsString(String resourceType) {
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
}

View file

@ -80,7 +80,7 @@ public interface AdminEventQuery {
* @param resourceTypes
* @return <code>this</code> for method chaining
*/
AdminEventQuery resourceType(ResourceType ... resourceTypes);
AdminEventQuery resourceType(List<String> resourceTypes);
/**
* Search by resource path. Supports wildcard <code>*</code>. For example:

View file

@ -21,170 +21,38 @@ package org.keycloak.events.admin;
*
* @author <a href="mailto:thomas.darimont@gmail.com">Thomas Darimont</a>
*/
public enum ResourceType {
public interface ResourceType {
/**
*
*/
REALM
/**
*
*/
, REALM_ROLE
/**
*
*/
, REALM_ROLE_MAPPING
/**
*
*/
, REALM_SCOPE_MAPPING
/**
*
*/
, AUTH_FLOW
/**
*
*/
, AUTH_EXECUTION_FLOW
/**
*
*/
, AUTH_EXECUTION
/**
*
*/
, AUTHENTICATOR_CONFIG
/**
*
*/
, REQUIRED_ACTION
/**
*
*/
, IDENTITY_PROVIDER
/**
*
*/
, IDENTITY_PROVIDER_MAPPER
/**
*
*/
, PROTOCOL_MAPPER
/**
*
*/
, USER
/**
*
*/
, USER_LOGIN_FAILURE
/**
*
*/
, USER_SESSION
/**
*
*/
, USER_FEDERATION_PROVIDER
/**
*
*/
, USER_FEDERATION_MAPPER
/**
*
*/
, GROUP
/**
*
*/
, GROUP_MEMBERSHIP
/**
*
*/
, CLIENT
/**
*
*/
, CLIENT_INITIAL_ACCESS_MODEL
/**
*
*/
, CLIENT_ROLE
/**
*
*/
, CLIENT_ROLE_MAPPING
/**
*
*/
, CLIENT_SCOPE
/**
*
*/
, CLIENT_SCOPE_MAPPING
/**
*
*/
, CLIENT_SCOPE_CLIENT_MAPPING
/**
*
*/
, CLUSTER_NODE
/**
*
*/
, COMPONENT
/**
*
*/
, AUTHORIZATION_RESOURCE_SERVER
/**
*
*/
, AUTHORIZATION_RESOURCE
/**
*
*/
, AUTHORIZATION_SCOPE
/**
*
*/
, AUTHORIZATION_POLICY
/**
*
*/
, CUSTOM;
public static String REALM = "REALM";
public static String REALM_ROLE = "REALM_ROLE";
public static String REALM_ROLE_MAPPING = "REALM_ROLE_MAPPING";
public static String REALM_SCOPE_MAPPING = "REALM_SCOPE_MAPPING";
public static String AUTH_FLOW = "AUTH_FLOW";
public static String AUTH_EXECUTION_FLOW = "AUTH_EXECUTION_FLOW";
public static String AUTH_EXECUTION = "AUTH_EXECUTION";
public static String AUTHENTICATOR_CONFIG = "AUTHENTICATOR_CONFIG";
public static String REQUIRED_ACTION = "REQUIRED_ACTION";
public static String IDENTITY_PROVIDER = "IDENTITY_PROVIDER";
public static String IDENTITY_PROVIDER_MAPPER = "IDENTITY_PROVIDER_MAPPER";
public static String PROTOCOL_MAPPER = "PROTOCOL_MAPPER";
public static String USER = "USER";
public static String USER_LOGIN_FAILURE = "USER_LOGIN_FAILURE";
public static String USER_SESSION = "USER_SESSION";
public static String USER_FEDERATION_PROVIDER = "USER_FEDERATION_PROVIDER";
public static String USER_FEDERATION_MAPPER = "USER_FEDERATION_MAPPER";
public static String GROUP = "GROUP";
public static String GROUP_MEMBERSHIP = "GROUP_MEMBERSHIP";
public static String CLIENT = "CLIENT";
public static String CLIENT_INITIAL_ACCESS_MODEL = "CLIENT_INITIAL_ACCESS_MODEL";
public static String CLIENT_ROLE = "CLIENT_ROLE";
public static String CLIENT_ROLE_MAPPING = "CLIENT_ROLE_MAPPING";
public static String CLIENT_SCOPE = "CLIENT_SCOPE";
public static String CLIENT_SCOPE_MAPPING = "CLIENT_SCOPE_MAPPING";
public static String CLIENT_SCOPE_CLIENT_MAPPING = "CLIENT_SCOPE_CLIENT_MAPPING";
public static String CLUSTER_NODE = "CLUSTER_NODE";
public static String COMPONENT = "COMPONENT";
public static String AUTHORIZATION_RESOURCE_SERVER = "AUTHORIZATION_RESOURCE_SERVER";
public static String AUTHORIZATION_RESOURCE = "AUTHORIZATION_RESOURCE";
public static String AUTHORIZATION_SCOPE = "AUTHORIZATION_SCOPE";
public static String AUTHORIZATION_POLICY = "AUTHORIZATION_POLICY";
}

View file

@ -282,9 +282,7 @@ public class ModelToRepresentation {
rep.setAuthDetails(toRepresentation(adminEvent.getAuthDetails()));
}
rep.setOperationType(adminEvent.getOperationType().toString());
if (adminEvent.getResourceTypeAsString() != null) {
rep.setResourceType(adminEvent.getResourceTypeAsString());
}
rep.setResourceType(adminEvent.getResourceType());
rep.setResourcePath(adminEvent.getResourcePath());
rep.setRepresentation(adminEvent.getRepresentation());
rep.setError(adminEvent.getError());

View file

@ -136,7 +136,7 @@ public class JBossLoggingEventListenerProvider implements EventListenerProvider
sb.append(", ipAddress=");
sb.append(adminEvent.getAuthDetails().getIpAddress());
sb.append(", resourceType=");
sb.append(adminEvent.getResourceTypeAsString());
sb.append(adminEvent.getResourceType());
sb.append(", resourcePath=");
sb.append(adminEvent.getResourcePath());

View file

@ -24,7 +24,6 @@ import org.keycloak.events.EventStoreProvider;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
@ -113,16 +112,8 @@ public class AdminEventBuilder {
return this;
}
public AdminEventBuilder resource(ResourceType resourceType){
adminEvent.setResourceType(resourceType);
return this;
}
/**
* Setter for custom resource types with values different from {@link ResourceType}.
*/
public AdminEventBuilder resource(String resourceType){
adminEvent.setResourceTypeAsString(resourceType);
adminEvent.setResourceType(resourceType);
return this;
}

View file

@ -849,15 +849,9 @@ public class RealmAdminResource {
}
if (resourceTypes != null && !resourceTypes.isEmpty()) {
ResourceType[] t = new ResourceType[resourceTypes.size()];
for (int i = 0; i < t.length; i++) {
t[i] = ResourceType.valueOf(resourceTypes.get(i));
}
query.resourceType(t);
query.resourceType(resourceTypes);
}
if(dateFrom != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date from = null;

View file

@ -25,7 +25,6 @@ import org.keycloak.common.Profile;
import org.keycloak.component.ComponentFactory;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.policy.PasswordPolicyProvider;
@ -75,7 +74,7 @@ import java.util.stream.Stream;
*/
public class ServerInfoAdminResource {
private static final Map<String, List<String>> ENUMS = createEnumsMap(EventType.class, OperationType.class, ResourceType.class);
private static final Map<String, List<String>> ENUMS = createEnumsMap(EventType.class, OperationType.class);
@Context
private KeycloakSession session;

View file

@ -559,9 +559,7 @@ public class TestingResourceProvider implements RealmResourceProvider {
event.setAuthDetails(repToModel(rep.getAuthDetails()));
event.setError(rep.getError());
event.setOperationType(OperationType.valueOf(rep.getOperationType()));
if (rep.getResourceType() != null) {
event.setResourceTypeAsString(rep.getResourceType());
}
event.setResourceType(rep.getResourceType());
event.setRealmId(rep.getRealmId());
event.setRepresentation(rep.getRepresentation());
event.setResourcePath(rep.getResourcePath());

View file

@ -27,7 +27,6 @@ import org.junit.runners.model.Statement;
import org.keycloak.common.util.ObjectUtil;
import org.keycloak.common.util.reflections.Reflections;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.jose.jws.JWSInput;
import org.keycloak.jose.jws.JWSInputException;
import org.keycloak.representations.AccessToken;
@ -97,19 +96,19 @@ public class AssertAdminEvents implements TestRule {
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, String resourcePath, ResourceType resourceType) {
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, String resourcePath, String resourceType) {
return assertEvent(realmId, operationType, resourcePath, null, resourceType);
}
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, Matcher<String> resourcePath, ResourceType resourceType) {
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, Matcher<String> resourcePath, String resourceType) {
return assertEvent(realmId, operationType, resourcePath, null, resourceType);
}
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, String resourcePath, Object representation, ResourceType resourceType) {
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, String resourcePath, Object representation, String resourceType) {
return assertEvent(realmId, operationType, Matchers.equalTo(resourcePath), representation, resourceType);
}
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, Matcher<String> resourcePath, Object representation, ResourceType resourceType) {
public AdminEventRepresentation assertEvent(String realmId, OperationType operationType, Matcher<String> resourcePath, Object representation, String resourceType) {
return expect().realmId(realmId)
.operationType(operationType)
.resourcePath(resourcePath)
@ -124,7 +123,7 @@ public class AssertAdminEvents implements TestRule {
private AdminEventRepresentation expected = new AdminEventRepresentation();
private Matcher<String> resourcePath;
private ResourceType resourceType;
private String resourceType;
private Object expectedRep;
public ExpectedAdminEvent realmId(String realmId) {
@ -151,8 +150,8 @@ public class AssertAdminEvents implements TestRule {
return this;
}
public ExpectedAdminEvent resourceType(ResourceType resourceType){
expected.setResourceType(resourceType.toString());
public ExpectedAdminEvent resourceType(String resourceType){
expected.setResourceType(resourceType);
return this;
}