From 30a36498c973447fecca61bb2841c888236e9187 Mon Sep 17 00:00:00 2001 From: girirajsharma Date: Wed, 4 Feb 2015 02:02:44 +0530 Subject: [PATCH] Provided sys-logging provider to enable keycloak events remote logging. --- events/pom.xml | 1 + events/syslog/pom.xml | 47 ++++++++++++++ .../log/SysLoggingEventListenerProvider.java | 65 +++++++++++++++++++ ...ysLoggingEventListenerProviderFactory.java | 47 ++++++++++++++ ...ycloak.events.EventListenerProviderFactory | 1 + pom.xml | 8 ++- 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 events/syslog/pom.xml create mode 100755 events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProvider.java create mode 100644 events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProviderFactory.java create mode 100644 events/syslog/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory diff --git a/events/pom.xml b/events/pom.xml index b6a0a3c540..17d8553a7f 100755 --- a/events/pom.xml +++ b/events/pom.xml @@ -20,6 +20,7 @@ email jpa jboss-logging + syslog mongo diff --git a/events/syslog/pom.xml b/events/syslog/pom.xml new file mode 100644 index 0000000000..4d5bf534df --- /dev/null +++ b/events/syslog/pom.xml @@ -0,0 +1,47 @@ + + + + keycloak-events-parent + org.keycloak + 1.2.0.Beta1-SNAPSHOT + + + 4.0.0 + + keycloak-events-syslog + Keycloak Events Syslog Provider + + + + + org.syslog4j + syslog4j + ${syslog4j.version} + provided + + + org.keycloak + keycloak-core + ${project.version} + provided + + + org.keycloak + keycloak-model-api + ${project.version} + provided + + + org.keycloak + keycloak-events-api + ${project.version} + provided + + + junit + junit + test + + + + diff --git a/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProvider.java b/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProvider.java new file mode 100755 index 0000000000..32ed137bc6 --- /dev/null +++ b/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProvider.java @@ -0,0 +1,65 @@ +package org.keycloak.events.log; + +import java.util.Map; + +import org.keycloak.events.Event; +import org.keycloak.events.EventListenerProvider; +import org.productivity.java.syslog4j.SyslogConstants; +import org.productivity.java.syslog4j.SyslogIF; + +/** + * @author Giriraj Sharma + */ +public class SysLoggingEventListenerProvider implements EventListenerProvider { + + private final SyslogIF syslogger; + + public SysLoggingEventListenerProvider(SyslogIF syslogger) { + this.syslogger = syslogger; + } + + @Override + public void onEvent(Event event) { + int level = event.getError() != null ? SyslogConstants.LEVEL_ERROR : SyslogConstants.LEVEL_INFO; + + StringBuilder sb = new StringBuilder(); + + sb.append("type="); + sb.append(event.getType()); + sb.append(", realmId="); + sb.append(event.getRealmId()); + sb.append(", clientId="); + sb.append(event.getClientId()); + sb.append(", userId="); + sb.append(event.getUserId()); + sb.append(", ipAddress="); + sb.append(event.getIpAddress()); + + if (event.getError() != null) { + sb.append(", error="); + sb.append(event.getError()); + } + + if (event.getDetails() != null) { + for (Map.Entry e : event.getDetails().entrySet()) { + sb.append(", "); + sb.append(e.getKey()); + if (e.getValue() == null || e.getValue().indexOf(' ') == -1) { + sb.append("="); + sb.append(e.getValue()); + } else { + sb.append("='"); + sb.append(e.getValue()); + sb.append("'"); + } + } + } + + syslogger.log(level, sb.toString()); + } + + @Override + public void close() { + } + +} diff --git a/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProviderFactory.java b/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProviderFactory.java new file mode 100644 index 0000000000..15157f0be5 --- /dev/null +++ b/events/syslog/src/main/java/org/keycloak/events/log/SysLoggingEventListenerProviderFactory.java @@ -0,0 +1,47 @@ +package org.keycloak.events.log; + +import org.keycloak.Config; +import org.keycloak.events.EventListenerProvider; +import org.keycloak.events.EventListenerProviderFactory; +import org.keycloak.models.KeycloakSession; +import org.productivity.java.syslog4j.Syslog; +import org.productivity.java.syslog4j.SyslogIF; + +/** + * @author Giriraj Sharma + */ +public class SysLoggingEventListenerProviderFactory implements EventListenerProviderFactory { + + public static final String ID = "sys-logging"; + + private SyslogIF syslogger; + private String protocol; + private String host; + private int port; + + @Override + public EventListenerProvider create(KeycloakSession session) { + return new SysLoggingEventListenerProvider(syslogger); + } + + @Override + public void init(Config.Scope config) { + protocol = config.get("protocol"); + host = config.get("host"); + port = config.getInt("port"); + + syslogger = Syslog.getInstance(protocol); + syslogger.getConfig().setHost(host); + syslogger.getConfig().setPort(port); + } + + @Override + public void close() { + } + + @Override + public String getId() { + return ID; + } + +} diff --git a/events/syslog/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory b/events/syslog/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory new file mode 100644 index 0000000000..9c9938ca6a --- /dev/null +++ b/events/syslog/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory @@ -0,0 +1 @@ +org.keycloak.events.log.SysLoggingEventListenerProviderFactory \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7ae9a0776e..7afa93790b 100755 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 Keycloak @@ -25,6 +25,7 @@ 1.0.2.Final 2.11.3 3.1.4.GA + 0.9.30 1.2.0.Beta1 1.0.1.Final 4.0.1.Final @@ -316,6 +317,11 @@ jboss-logging ${jboss.logging.version} + + org.syslog4j + syslog4j + ${syslog4j.version} + log4j log4j