KEYCLOAK-18471 Added ID to admin event object.

This commit is contained in:
bal1imb 2021-04-22 01:29:33 -07:00 committed by Hynek Mlnařík
parent 740248fd54
commit fbaeb18a5f
6 changed files with 37 additions and 3 deletions

View file

@ -146,7 +146,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
private EventEntity convertEvent(Event event) { private EventEntity convertEvent(Event event) {
EventEntity eventEntity = new EventEntity(); EventEntity eventEntity = new EventEntity();
eventEntity.setId(UUID.randomUUID().toString()); eventEntity.setId(event.getId() == null ? UUID.randomUUID().toString() : event.getId());
eventEntity.setTime(event.getTime()); eventEntity.setTime(event.getTime());
eventEntity.setType(event.getType().toString()); eventEntity.setType(event.getType().toString());
eventEntity.setRealmId(event.getRealmId()); eventEntity.setRealmId(event.getRealmId());
@ -183,6 +183,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
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.setTime(eventEntity.getTime()); event.setTime(eventEntity.getTime());
event.setType(EventType.valueOf(eventEntity.getType())); event.setType(EventType.valueOf(eventEntity.getType()));
event.setRealmId(eventEntity.getRealmId()); event.setRealmId(eventEntity.getRealmId());
@ -202,7 +203,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
static AdminEventEntity convertAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) { static AdminEventEntity convertAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) {
AdminEventEntity adminEventEntity = new AdminEventEntity(); AdminEventEntity adminEventEntity = new AdminEventEntity();
adminEventEntity.setId(UUID.randomUUID().toString()); adminEventEntity.setId(adminEvent.getId() == null ? UUID.randomUUID().toString() : adminEvent.getId());
adminEventEntity.setTime(adminEvent.getTime()); adminEventEntity.setTime(adminEvent.getTime());
adminEventEntity.setRealmId(adminEvent.getRealmId()); adminEventEntity.setRealmId(adminEvent.getRealmId());
setAuthDetails(adminEventEntity, adminEvent.getAuthDetails()); setAuthDetails(adminEventEntity, adminEvent.getAuthDetails());
@ -223,6 +224,7 @@ public class JpaEventStoreProvider implements EventStoreProvider {
static AdminEvent convertAdminEvent(AdminEventEntity adminEventEntity) { static AdminEvent convertAdminEvent(AdminEventEntity adminEventEntity) {
AdminEvent adminEvent = new AdminEvent(); AdminEvent adminEvent = new AdminEvent();
adminEvent.setId(adminEventEntity.getId() == null ? UUID.randomUUID().toString() : adminEventEntity.getId());
adminEvent.setTime(adminEventEntity.getTime()); adminEvent.setTime(adminEventEntity.getTime());
adminEvent.setRealmId(adminEventEntity.getRealmId()); adminEvent.setRealmId(adminEventEntity.getRealmId());
setAuthDetails(adminEvent, adminEventEntity); setAuthDetails(adminEvent, adminEventEntity);

View file

@ -25,6 +25,8 @@ import java.util.Map;
*/ */
public class Event { public class Event {
private String id;
private long time; private long time;
private EventType type; private EventType type;
@ -43,6 +45,14 @@ public class Event {
private Map<String, String> details; private Map<String, String> details;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public long getTime() { public long getTime() {
return time; return time;
} }
@ -117,6 +127,7 @@ public class Event {
public Event clone() { public Event clone() {
Event clone = new Event(); Event clone = new Event();
clone.id = id;
clone.time = time; clone.time = time;
clone.type = type; clone.type = type;
clone.realmId = realmId; clone.realmId = realmId;

View file

@ -30,6 +30,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -178,6 +179,7 @@ public class EventBuilder {
private void send() { private void send() {
event.setTime(Time.currentTimeMillis()); event.setTime(Time.currentTimeMillis());
event.setId(UUID.randomUUID().toString());
if (store != null) { if (store != null) {
Set<String> eventTypes = realm.getEnabledEventTypesStream().collect(Collectors.toSet()); Set<String> eventTypes = realm.getEnabledEventTypesStream().collect(Collectors.toSet());

View file

@ -22,6 +22,8 @@ package org.keycloak.events.admin;
*/ */
public class AdminEvent { public class AdminEvent {
private String id;
private long time; private long time;
private String realmId; private String realmId;
@ -43,6 +45,7 @@ public class AdminEvent {
public AdminEvent() {} public AdminEvent() {}
public AdminEvent(AdminEvent toCopy) { public AdminEvent(AdminEvent toCopy) {
this.id = toCopy.getId();
this.time = toCopy.getTime(); this.time = toCopy.getTime();
this.realmId = toCopy.getRealmId(); this.realmId = toCopy.getRealmId();
this.authDetails = new AuthDetails(toCopy.getAuthDetails()); this.authDetails = new AuthDetails(toCopy.getAuthDetails());
@ -53,8 +56,19 @@ public class AdminEvent {
this.error = toCopy.getError(); this.error = toCopy.getError();
} }
/**
* Returns the UUID of the event.
*
* @return
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/** /**
* Returns the time of the event * Returns the time of the event
* *

View file

@ -36,6 +36,7 @@ import javax.ws.rs.core.UriInfo;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate; import java.util.function.Predicate;
public class AdminEventBuilder { public class AdminEventBuilder {
@ -238,6 +239,7 @@ public class AdminEventBuilder {
// Event needs to be copied because the same builder can be used with another event // Event needs to be copied because the same builder can be used with another event
AdminEvent eventCopy = new AdminEvent(adminEvent); AdminEvent eventCopy = new AdminEvent(adminEvent);
eventCopy.setTime(Time.currentTimeMillis()); eventCopy.setTime(Time.currentTimeMillis());
eventCopy.setId(UUID.randomUUID().toString());
if (store != null) { if (store != null) {
try { try {

View file

@ -109,6 +109,7 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.UUID;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -391,6 +392,7 @@ public class TestingResourceProvider implements RealmResourceProvider {
private Event repToModel(EventRepresentation rep) { private Event repToModel(EventRepresentation rep) {
Event event = new Event(); Event event = new Event();
event.setId(UUID.randomUUID().toString());
event.setClientId(rep.getClientId()); event.setClientId(rep.getClientId());
event.setDetails(rep.getDetails()); event.setDetails(rep.getDetails());
event.setError(rep.getError()); event.setError(rep.getError());
@ -536,6 +538,7 @@ public class TestingResourceProvider implements RealmResourceProvider {
private AdminEvent repToModel(AdminEventRepresentation rep) { private AdminEvent repToModel(AdminEventRepresentation rep) {
AdminEvent event = new AdminEvent(); AdminEvent event = new AdminEvent();
event.setId(UUID.randomUUID().toString());
event.setAuthDetails(repToModel(rep.getAuthDetails())); event.setAuthDetails(repToModel(rep.getAuthDetails()));
event.setError(rep.getError()); event.setError(rep.getError());
event.setOperationType(OperationType.valueOf(rep.getOperationType())); event.setOperationType(OperationType.valueOf(rep.getOperationType()));