Add possibility to limit field length in legacy event store

Closes #14888
This commit is contained in:
vramik 2022-10-14 12:08:16 +02:00 committed by Hynek Mlnařík
parent 440077de42
commit 791c457c32
2 changed files with 26 additions and 12 deletions

View file

@ -64,11 +64,13 @@ public class JpaEventStoreProvider implements EventStoreProvider {
private final KeycloakSession session; private final KeycloakSession session;
private final EntityManager em; private final EntityManager em;
private final int maxDetailLength; private final int maxDetailLength;
private final int maxFieldLength;
public JpaEventStoreProvider(KeycloakSession session, EntityManager em, int maxDetailLength) { public JpaEventStoreProvider(KeycloakSession session, EntityManager em, int maxDetailLength, int maxFieldLength) {
this.session = session; this.session = session;
this.em = em; this.em = em;
this.maxDetailLength = maxDetailLength; this.maxDetailLength = maxDetailLength;
this.maxFieldLength = maxFieldLength;
} }
@Override @Override
@ -171,9 +173,9 @@ public class JpaEventStoreProvider implements EventStoreProvider {
try { try {
if (maxDetailLength > 0 && event.getDetails() != null) { if (maxDetailLength > 0 && event.getDetails() != null) {
Map<String, String> result = new HashMap<>(event.getDetails()); Map<String, String> result = new HashMap<>(event.getDetails());
result.entrySet().forEach(t -> t.setValue(trimToMaxLength(t.getValue()))); result.entrySet().forEach(t -> t.setValue(trimToMaxDetailLength(t.getValue())));
eventEntity.setDetailsJson(mapper.writeValueAsString(result)); eventEntity.setDetailsJson(trimToMaxFieldLength(mapper.writeValueAsString(result)));
} else { } else {
eventEntity.setDetailsJson(mapper.writeValueAsString(event.getDetails())); eventEntity.setDetailsJson(mapper.writeValueAsString(event.getDetails()));
} }
@ -183,17 +185,26 @@ public class JpaEventStoreProvider implements EventStoreProvider {
return eventEntity; return eventEntity;
} }
private String trimToMaxLength(String detail) { private String trimToMaxDetailLength(String detail) {
if (detail != null && detail.length() > maxDetailLength) { if (detail != null && detail.length() > maxDetailLength) {
logger.warnf("Detail '%s' will be truncated.", detail);
// (maxDetailLength - 3) takes "..." into account // (maxDetailLength - 3) takes "..." into account
String result = detail.substring(0, maxDetailLength - 3).concat("..."); return detail.substring(0, maxDetailLength - 3).concat("...");
logger.warn("Detail was truncated to " + result);
return result;
} else { } else {
return detail; return detail;
} }
} }
private String trimToMaxFieldLength(String field) {
if (maxFieldLength > 0 && field != null && field.length() > maxFieldLength) {
logger.warnf("Field '%s' will be truncated.", field);
// (maxFieldLength - 3) takes "..." into account
return field.substring(0, maxFieldLength - 3).concat("...");
} else {
return field;
}
}
static Event convertEvent(EventEntity eventEntity) { static Event convertEvent(EventEntity eventEntity) {
Event event = new Event(); Event event = new Event();
event.setId(eventEntity.getId() == null ? UUID.randomUUID().toString() : eventEntity.getId()); event.setId(eventEntity.getId() == null ? UUID.randomUUID().toString() : eventEntity.getId());
@ -214,7 +225,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
return event; return event;
} }
static AdminEventEntity convertAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) { private AdminEventEntity convertAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) {
AdminEventEntity adminEventEntity = new AdminEventEntity(); AdminEventEntity adminEventEntity = new AdminEventEntity();
adminEventEntity.setId(adminEvent.getId() == null ? UUID.randomUUID().toString() : adminEvent.getId()); adminEventEntity.setId(adminEvent.getId() == null ? UUID.randomUUID().toString() : adminEvent.getId());
adminEventEntity.setTime(adminEvent.getTime()); adminEventEntity.setTime(adminEvent.getTime());
@ -230,7 +241,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
adminEventEntity.setError(adminEvent.getError()); adminEventEntity.setError(adminEvent.getError());
if(includeRepresentation) { if(includeRepresentation) {
adminEventEntity.setRepresentation(adminEvent.getRepresentation()); adminEventEntity.setRepresentation(trimToMaxFieldLength(adminEvent.getRepresentation()));
} }
return adminEventEntity; return adminEventEntity;
} }

View file

@ -18,7 +18,6 @@
package org.keycloak.events.jpa; package org.keycloak.events.jpa;
import org.keycloak.Config; import org.keycloak.Config;
import org.keycloak.common.util.Time;
import org.keycloak.connections.jpa.JpaConnectionProvider; import org.keycloak.connections.jpa.JpaConnectionProvider;
import org.keycloak.events.EventStoreProvider; import org.keycloak.events.EventStoreProvider;
import org.keycloak.events.EventStoreProviderFactory; import org.keycloak.events.EventStoreProviderFactory;
@ -34,16 +33,20 @@ public class JpaEventStoreProviderFactory implements EventStoreProviderFactory,
public static final String ID = "jpa"; public static final String ID = "jpa";
private int maxDetailLength; private int maxDetailLength;
private int maxFieldLength;
@Override @Override
public EventStoreProvider create(KeycloakSession session) { public EventStoreProvider create(KeycloakSession session) {
JpaConnectionProvider connection = session.getProvider(JpaConnectionProvider.class); JpaConnectionProvider connection = session.getProvider(JpaConnectionProvider.class);
return new JpaEventStoreProvider(session, connection.getEntityManager(), maxDetailLength); return new JpaEventStoreProvider(session, connection.getEntityManager(), maxDetailLength, maxFieldLength);
} }
@Override @Override
public void init(Config.Scope config) { public void init(Config.Scope config) {
maxDetailLength = config.getInt("max-detail-length", 0); maxDetailLength = config.getInt("max-detail-length", -1);
maxFieldLength = config.getInt("max-field-length", -1);
if (maxDetailLength != -1 && maxDetailLength < 3) throw new IllegalArgumentException("max-detail-length cannot be less that 3.");
if (maxFieldLength != -1 && maxFieldLength < 3) throw new IllegalArgumentException("max-field-length cannot be less that 3.");
} }
@Override @Override