Added example providers for audit listener and audit provider

This commit is contained in:
Stian Thorgersen 2014-05-27 15:01:35 +01:00
parent 9261c1608f
commit 6dda7fc356
13 changed files with 508 additions and 0 deletions

View file

@ -34,5 +34,6 @@
</build>
<modules>
<module>demo-template</module>
<module>providers</module>
</modules>
</project>

View file

@ -0,0 +1,4 @@
Example Audit Listener that prints events to System.out
=======================================================
To deploy copy target/audit-listener-sysout-example.jar to standalone/deployments/auth-server.war/WEB-INF/lib. Then start (or restart) the server. Once started open the admin console, select your realm, then click on Audit, followed by config. Click on Audit Listeners select box, then pick sysout from the dropdown. After this try to logout and login again to see events printed to System.out.

View file

@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>examples-providers-pom</artifactId>
<groupId>org.keycloak</groupId>
<version>1.0-beta-1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>Audit Listener System.out Example</name>
<description/>
<modelVersion>4.0.0</modelVersion>
<artifactId>audit-listener-sysout-example</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>audit-listener-sysout-example</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.1.1.Final</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<modules>
</modules>
</project>

View file

@ -0,0 +1,72 @@
package org.keycloak.examples.providers.audit;
import org.keycloak.audit.AuditListener;
import org.keycloak.audit.Event;
import org.keycloak.audit.EventType;
import java.util.Map;
import java.util.Set;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class SysoutAuditListener implements AuditListener {
private Set<EventType> excludedEvents;
public SysoutAuditListener(Set<EventType> excludedEvents) {
this.excludedEvents = excludedEvents;
}
@Override
public void onEvent(Event event) {
// Ignore excluded events
if (excludedEvents != null && excludedEvents.contains(event.getEvent())) {
return;
} else {
System.out.println("EVENT: " + toString(event));
}
}
private String toString(Event event) {
StringBuilder sb = new StringBuilder();
sb.append("event=");
sb.append(event.getEvent());
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<String, String> 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("'");
}
}
}
return sb.toString();
}
@Override
public void close() {
}
}

View file

@ -0,0 +1,44 @@
package org.keycloak.examples.providers.audit;
import org.keycloak.Config;
import org.keycloak.audit.AuditListener;
import org.keycloak.audit.AuditListenerFactory;
import org.keycloak.audit.EventType;
import org.keycloak.provider.ProviderSession;
import java.util.HashSet;
import java.util.Set;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class SysoutAuditListenerFactory implements AuditListenerFactory {
private Set<EventType> excludedEvents;
@Override
public AuditListener create(ProviderSession providerSession) {
return new SysoutAuditListener(excludedEvents);
}
@Override
public void init(Config.Scope config) {
String excludes = config.get("excludes");
if (excludes != null) {
excludedEvents = new HashSet<EventType>();
for (String e : excludes.split(",")) {
excludedEvents.add(EventType.valueOf(e));
}
}
}
@Override
public void close() {
}
@Override
public String getId() {
return "sysout";
}
}

View file

@ -0,0 +1 @@
org.keycloak.examples.providers.audit.SysoutAuditListenerFactory

View file

@ -0,0 +1,16 @@
Example Audit Provider that stores events in a List
===================================================
To deploy copy target/audit-provider-mem-example.jar to standalone/deployments/auth-server.war/WEB-INF/lib. Then edit standalone/configuration/keycloak-server.json, change:
"audit": {
"provider": "jpa"
}
to:
"audit": {
"provider": "in-mem"
}
Then start (or restart)the server. Once started open the admin console, select your realm, then click on Audit, followed by config. Set the toggle for Enabled to ON. After this try to logout and login again then open the Audit tab again in the admin console to view events from the in-mem provider.

View file

@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>examples-providers-pom</artifactId>
<groupId>org.keycloak</groupId>
<version>1.0-beta-1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>Audit Provider In-Mem Example</name>
<description/>
<modelVersion>4.0.0</modelVersion>
<artifactId>audit-provider-mem-example</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>audit-provider-mem-example</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.1.1.Final</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<modules>
</modules>
</project>

View file

@ -0,0 +1,69 @@
package org.keycloak.examples.providers.audit;
import org.keycloak.audit.AuditProvider;
import org.keycloak.audit.Event;
import org.keycloak.audit.EventQuery;
import org.keycloak.audit.EventType;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class MemAuditProvider implements AuditProvider {
private final List<Event> events;
private final Set<EventType> excludedEvents;
public MemAuditProvider(List<Event> events, Set<EventType> excludedEvents) {
this.events = events;
this.excludedEvents = excludedEvents;
}
@Override
public EventQuery createQuery() {
return new MemEventQuery(new LinkedList<Event>(events));
}
@Override
public void clear() {
}
@Override
public void clear(String realmId) {
synchronized(events) {
Iterator<Event> itr = events.iterator();
while (itr.hasNext()) {
if (itr.next().getRealmId().equals(realmId)) {
itr.remove();
}
}
}
}
@Override
public void clear(String realmId, long olderThan) {
synchronized(events) {
Iterator<Event> itr = events.iterator();
while (itr.hasNext()) {
Event e = itr.next();
if (e.getRealmId().equals(realmId) && e.getTime() < olderThan) {
itr.remove();
}
}
}
}
@Override
public void onEvent(Event event) {
events.add(event);
}
@Override
public void close() {
}
}

View file

@ -0,0 +1,53 @@
package org.keycloak.examples.providers.audit;
import org.keycloak.Config;
import org.keycloak.audit.AuditProvider;
import org.keycloak.audit.AuditProviderFactory;
import org.keycloak.audit.Event;
import org.keycloak.audit.EventType;
import org.keycloak.provider.ProviderSession;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class MemAuditProviderFactory implements AuditProviderFactory {
private List<Event> events;
private Set<EventType> excludedEvents;
@Override
public AuditProvider create(ProviderSession providerSession) {
return new MemAuditProvider(events, excludedEvents);
}
@Override
public void init(Config.Scope config) {
events = Collections.synchronizedList(new LinkedList<Event>());
String excludes = config.get("excludes");
if (excludes != null) {
excludedEvents = new HashSet<EventType>();
for (String e : excludes.split(",")) {
excludedEvents.add(EventType.valueOf(e));
}
}
}
@Override
public void close() {
events = null;
excludedEvents = null;
}
@Override
public String getId() {
return "in-mem";
}
}

View file

@ -0,0 +1,106 @@
package org.keycloak.examples.providers.audit;
import org.keycloak.audit.Event;
import org.keycloak.audit.EventQuery;
import org.keycloak.audit.EventType;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class MemEventQuery implements EventQuery {
private List<Event> events;
private int first;
private int max;
public MemEventQuery(List<Event> events) {
this.events = events;
}
@Override
public EventQuery event(EventType... events) {
Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) {
Event next = itr.next();
for (EventType e : events) {
if (next.getEvent().equals(e)) {
break;
}
}
itr.remove();
}
return this;
}
@Override
public EventQuery realm(String realmId) {
Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) {
if (!itr.next().getRealmId().equals(realmId)) {
itr.remove();
}
}
return this;
}
@Override
public EventQuery client(String clientId) {
Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) {
if (!itr.next().getClientId().equals(clientId)) {
itr.remove();
}
}
return this;
}
@Override
public EventQuery user(String userId) {
Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) {
if (!itr.next().getUserId().equals(userId)) {
itr.remove();
}
}
return this;
}
@Override
public EventQuery ipAddress(String ipAddress) {
Iterator<Event> itr = this.events.iterator();
while (itr.hasNext()) {
if (!itr.next().getIpAddress().equals(ipAddress)) {
itr.remove();
}
}
return this;
}
@Override
public EventQuery firstResult(int result) {
this.first = result;
return this;
}
@Override
public EventQuery maxResults(int results) {
this.max = results;
return this;
}
@Override
public List<Event> getResultList() {
if (events.size() < first) {
return Collections.emptyList();
}
int end = first + max <= events.size() ? first + max : events.size();
return events.subList(first, end);
}
}

View file

@ -0,0 +1 @@
org.keycloak.examples.providers.audit.MemAuditProviderFactory

39
examples/providers/pom.xml Executable file
View file

@ -0,0 +1,39 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>examples-pom</artifactId>
<groupId>org.keycloak</groupId>
<version>1.0-beta-1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>Provider Examples</name>
<description/>
<modelVersion>4.0.0</modelVersion>
<artifactId>examples-providers-pom</artifactId>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.1.1.Final</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>audit-listener-sysout</module>
<module>audit-provider-mem</module>
</modules>
</project>