Skip to content

Commit

Permalink
Make sure event details are not stored if they are null
Browse files Browse the repository at this point in the history
Closes keycloak#35288

Signed-off-by: Pedro Igor <[email protected]>
  • Loading branch information
pedroigor authored and ahus1 committed Dec 2, 2024
1 parent 88cfe42 commit e5f1c9a
Showing 1 changed file with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -147,11 +148,7 @@ private EventEntity convertEvent(Event event) {
eventEntity.setSessionId(event.getSessionId());
eventEntity.setIpAddress(event.getIpAddress());
eventEntity.setError(event.getError());
try {
eventEntity.setDetailsJson(mapper.writeValueAsString(event.getDetails()));
} catch (IOException ex) {
logger.error("Failed to write log details", ex);
}
setDetails(eventEntity::setDetailsJson, event.getDetails());
return eventEntity;
}

Expand All @@ -166,12 +163,7 @@ static Event convertEvent(EventEntity eventEntity) {
event.setSessionId(eventEntity.getSessionId());
event.setIpAddress(eventEntity.getIpAddress());
event.setError(eventEntity.getError());
try {
Map<String, String> details = mapper.readValue(eventEntity.getDetailsJson(), mapType);
event.setDetails(details);
} catch (IOException ex) {
logger.error("Failed to read log details", ex);
}
setDetails(event::setDetails, eventEntity.getDetailsJson());
return event;
}

Expand All @@ -194,11 +186,7 @@ private AdminEventEntity convertAdminEvent(AdminEvent adminEvent, boolean includ
adminEventEntity.setRepresentation(adminEvent.getRepresentation());
}

try {
adminEventEntity.setDetailsJson(mapper.writeValueAsString(adminEvent.getDetails()));
} catch (IOException ex) {
logger.error("Failed to write log details", ex);
}
setDetails(adminEventEntity::setDetailsJson, adminEvent.getDetails());

return adminEventEntity;
}
Expand All @@ -222,12 +210,7 @@ static AdminEvent convertAdminEvent(AdminEventEntity adminEventEntity) {
adminEvent.setRepresentation(adminEventEntity.getRepresentation());
}

try {
Map<String, String> details = mapper.readValue(adminEventEntity.getDetailsJson(), mapType);
adminEvent.setDetails(details);
} catch (IOException ex) {
logger.error("Failed to read log details", ex);
}
setDetails(adminEvent::setDetails, adminEventEntity.getDetailsJson());

return adminEvent;
}
Expand Down Expand Up @@ -277,4 +260,24 @@ protected void clearExpiredAdminEvents() {
logger.tracef("Deleted %d admin events for the expiration %d", currentNumDeleted, key);
});
}

private static void setDetails(Consumer<String> setter, Map<String, String> details) {
if (details != null) {
try {
setter.accept(mapper.writeValueAsString(details));
} catch (IOException e) {
logger.error("Failed to write event details", e);
}
}
}

private static void setDetails(Consumer<Map<String, String>> setter, String details) {
if (details != null) {
try {
setter.accept(mapper.readValue(details, mapType));
} catch (IOException e) {
logger.error("Failed to read event details", e);
}
}
}
}

0 comments on commit e5f1c9a

Please sign in to comment.