Skip to content

Commit

Permalink
[CLOUDTRUST-773] Refactor POM hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed Sep 27, 2019
1 parent 9eaa746 commit 456ade4
Show file tree
Hide file tree
Showing 47 changed files with 247 additions and 604 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -9,7 +9,7 @@ The aim of this module is to send those Events and AdminEvents to another server
Java 8 is required (Java 9+ is not supported yet).

### Build and Tests
This module contains both unit and integration tests, and a parent inherited from Keycloak tests for simplifying the
This project 2 modules, one with the event emitter code (could contain unit tests) and one for integration tests using a parent inherited from Keycloak tests for simplifying the
POM content.

The integration tests rely on the arquillian-based Keycloak test framework. As Keycloak does not publish publicly
Expand All @@ -29,12 +29,12 @@ with the module.
## Installation
Event emitter module is expected to be installed as a module in a specific layer.

To install the release, use the TAR.GZ file produced by the build; either run the `install.sh` script
To install the release, go to event-emitter module directory and use the TAR.GZ file produced by the build; either run the `install.sh` script
or manually proceed as follows:

```Bash
# Install the module binaries
tar -zxf event-emitter-<version>.Final-dist.tar.gz --directory <PATH_TO_KEYCLOAK>/modules/system/layers
tar -zxf event-emitter-<version>-dist.tar.gz --directory <PATH_TO_KEYCLOAK>/modules/system/layers

# Set the appropriate permissions on the new files
chmod -R 755 <PATH_TO_KEYCLOAK>/modules/system/layers/event-emitter
Expand Down Expand Up @@ -101,6 +101,7 @@ This module will authenticate itself to the server endpoint with Basic authentic

### Flatbuffers

Go to event-emitter module directory.
Flatbuffers schema is located under src/main/flatbuffers/flatbuffers/event.fbs.

Compilation of the schema
Expand Down
114 changes: 114 additions & 0 deletions event-emitter-tests/pom.xml
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.cloudtrust</groupId>
<artifactId>kc-cloudtrust-testsuite</artifactId>
<version>6.0.1</version>
<relativePath/>
</parent>

<artifactId>event-emitter-tests</artifactId>

<properties>
<!--browser>chrome</browser-->

<apache-cxf.version>3.2.9</apache-cxf.version>
</properties>

<dependencies>
<dependency>
<groupId>io.cloudtrust</groupId>
<artifactId>event-emitter</artifactId>
<version>${project.version}</version>
</dependency>

<!--keycloak -->
<dependency>
<groupId>org.keycloak.testsuite</groupId>
<artifactId>integration-arquillian-tests-base</artifactId>
<version>${keycloak.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak.testsuite</groupId>
<artifactId>integration-arquillian-tests-console</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<!-- CXF -->
<dependency>
<groupId>org.apache.cxf.services.sts</groupId>
<artifactId>cxf-services-sts-core</artifactId>
<version>${apache-cxf.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>${apache-cxf.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<!--dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency-->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.maven.plugin}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${version.frontend.maven.plugin}</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
86 changes: 86 additions & 0 deletions event-emitter/pom.xml
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.cloudtrust</groupId>
<artifactId>event-emitter-parent</artifactId>
<version>6.0.1</version>
</parent>

<artifactId>event-emitter</artifactId>
<description>Keycloak module to transmit events to keycloak-bridge</description>
<packaging>jar</packaging>

<properties>
<jackson.version>2.9.9</jackson.version>
</properties>

<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>provided</scope>
</dependency>

<!-- 3rd Party libraries-->
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
</plugin>
</plugins>
</build>
</project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -35,6 +35,10 @@ public class EventEmitterProvider implements EventListenerProvider{

private static final int HTTP_OK = 200;

interface EventSender<T extends HasUid> {
void send(T event) throws EventEmitterException, IOException;
}

private CloseableHttpClient httpClient;
private HttpClientContext httpContext;
private IdGenerator idGenerator;
Expand Down Expand Up @@ -69,18 +73,19 @@ public class EventEmitterProvider implements EventListenerProvider{
this.pendingAdminEvents = pendingAdminEvents;

// Secret token
secretToken = System.getenv(KEYCLOAK_BRIDGE_SECRET_TOKEN);
if (secretToken == null) {
logger.error("Cannot find the environment variable '" + KEYCLOAK_BRIDGE_SECRET_TOKEN + "'");
throw new IllegalStateException("Cannot find the environment variable '" + KEYCLOAK_BRIDGE_SECRET_TOKEN + "'");
}

secretToken = checkEnv(KEYCLOAK_BRIDGE_SECRET_TOKEN);
// Hostname
username = System.getenv(HOSTNAME);
if (username == null) {
logger.error("Cannot find the environment variable '" + HOSTNAME + "'");
throw new IllegalStateException("Cannot find the environment variable '" + HOSTNAME + "'");
username = checkEnv(HOSTNAME);
}

private String checkEnv(String key) {
String env = System.getenv(key);
if (env != null) {
return env;
}
String message = "Cannot find the environment variable '" + key + "'";
logger.error(message);
throw new IllegalStateException(message);
}

public void onEvent(Event event) {
Expand Down Expand Up @@ -146,88 +151,36 @@ private void sendEvents() {
}


private void sendEventsWithJsonFormat() {
int pendingEventsSize = pendingEvents.size();
private <T extends HasUid> void sendEvents(LinkedBlockingQueue<T> queue, EventSender<T> eventProcessor) {
int pendingEventsSize = queue.size();
for (int i=0; i < pendingEventsSize; i++){
IdentifiedEvent event = pendingEvents.poll();
T event = queue.poll();

if(event == null){
break;
}

try {
String json = SerialisationUtils.toJson(event);
sendJson(json);
eventProcessor.send(event);
} catch (EventEmitterException | IOException e) {
pendingEvents.offer(event);
logger.infof("Failed to send event(ID=%s), try again later.", event.getUid());
String repushed = queue.offer(event) ? "success" : "failure";
logger.infof("Failed to send %s(ID=%s), try again later. Re-push: %s", event.getClass().getName(), event.getUid(), repushed);
logger.debug("Failed to serialize or send event", e);
break;
}
}

int pendingAdminEventsSize = pendingAdminEvents.size();
for (int i=0; i < pendingAdminEventsSize; i++){
IdentifiedAdminEvent event = pendingAdminEvents.poll();

if(event == null){
break;
}

try {
String json = SerialisationUtils.toJson(event);
sendJson(json);
} catch (EventEmitterException | IOException e) {
pendingAdminEvents.offer(event);
logger.infof("Failed to send adminEvent(ID=%s), try again later.", event.getUid());
logger.debug("Failed to serialize or send adminEvent", e);
break;
}
}
}

private void sendEventsWithJsonFormat() {
sendEvents(pendingEvents, e -> sendJson(SerialisationUtils.toJson(e)));
sendEvents(pendingAdminEvents, e -> sendJson(SerialisationUtils.toJson(e)));
}

private void sendEventsWithFlatbufferFormat() {
int pendingEventsSize = pendingEvents.size();
for (int i=0; i < pendingEventsSize; i++){
IdentifiedEvent event = pendingEvents.poll();

if(event == null){
break;
}

try {
ByteBuffer buffer = SerialisationUtils.toFlat(event);
sendBytes(buffer, Event.class.getSimpleName());
} catch (EventEmitterException | IOException e) {
pendingEvents.offer(event);
logger.infof("Failed to send event(ID=%s), try again later.", event.getUid());
logger.debug("Failed to serialize or send event", e);
break;
}
}

int pendingAdminEventsSize = pendingAdminEvents.size();
for (int i=0; i < pendingAdminEventsSize; i++){
IdentifiedAdminEvent event = pendingAdminEvents.poll();

if(event == null){
break;
}

try {
ByteBuffer buffer = SerialisationUtils.toFlat(event);
sendBytes(buffer, AdminEvent.class.getSimpleName());
} catch (EventEmitterException | IOException e) {
pendingAdminEvents.offer(event);
logger.infof("Failed to send adminEvent(ID=%s), try again later.", event.getUid());
logger.debug("Failed to serialize or send adminEvent", e);
break;
}
}
sendEvents(pendingEvents, e -> sendBytes(SerialisationUtils.toFlat(e), Event.class.getSimpleName()));
sendEvents(pendingAdminEvents, e -> sendBytes(SerialisationUtils.toFlat(e), AdminEvent.class.getSimpleName()));
}


private void sendBytes(ByteBuffer buffer, String type) throws IOException, EventEmitterException {
byte[] b = new byte[buffer.remaining()];
buffer.get(b);
Expand Down
@@ -0,0 +1,5 @@
package io.cloudtrust.keycloak.eventemitter;

public interface HasUid {
long getUid();
}
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.keycloak.events.admin.AdminEvent;

public class IdentifiedAdminEvent extends AdminEvent{
public class IdentifiedAdminEvent extends AdminEvent implements HasUid {

private long uid;

Expand Down
Expand Up @@ -3,7 +3,7 @@

import org.keycloak.events.Event;

public class IdentifiedEvent extends Event {
public class IdentifiedEvent extends Event implements HasUid {

private long uid;

Expand Down

0 comments on commit 456ade4

Please sign in to comment.