Integrating Keycloak With SonarQube

sonar-keycloak
You can grab the plugin here

Step 1: Create a Realm
ks1

Step 2: Create An Application

ks2

Step 3: Copy the keycloak.json

ks3

Step 4: Replace the new lines in json file

ks4

Step 5:  Configure Sonar

ks5

Step 6:  Copy the Plugin to Sonar Extensions

ks6

Step 7:  Login to Sonar

ks7

Step 8: You would be redirected to Keycloak

ks8

Step 9: Successful Login

ks9

Refer this if you would like to integrate Keycloak with Jenkins

Deploying Keycloak In TomEE

We would be using apache-tomee-plus-1.7.1 and Keycloak 1.1.0.Beta2

Refer this blog first Deploying Keycloak In Tomcat

Step 1: Configure TomEE for Hibernate instead of OpenJPA

Follow the guidelines here  to configure Hibernate

kte1

kte3

Step 2 : Configure TomEE with DataSource

Follow the guidelines here  and here to configure datasource

Since we would be using H2 database, copy h2-1.3.176.jar to TOMEE_HOME/lib

Modify the TOMEE_HOME/conf/tomee.xml as follows

kte2

Step 3 : Configure the Keycloak Server

Download the keycloak-server from github

Change the datasource name as follows

For TomcatEE external JNDI name starts with java:comp (Not  java:comp/env as is Pure Tomcat)

kte4

Deploy to TomEE and start the server, you would get the following error

kte5

Step 4 : Fix the issues

Create the policyConfgi.xml as follows

kte6

Step 5 : Have fun

Login to app with admin/admin

kte7

Deploying Keycloak In Tomcat

Introduction

Note: You can download the source from github

AS per the Keycloak documentation currently server installation is supported only in Jboss Servers (AS,Wildfly and EAP), However it does not make sense for Organizations to use JBOSS servers only to host Keycloak, Verily they would be running servers other than JBOSS (Tomcat, Jetty, Glassfish etc.)

kt14

As per the documentation it must be a easy task for the other servers, let’s explore.

Setup

If you do a build of the keycloak server, and deploy the war to Tomcat

kt14

 Issue #1

You will get the following error, which definitely seems to be pom issue

kt14

After adding required dependency you may get other ClassNotFoundExceptions, and finally you may add the following dependencies in your pom file

[gist https://gist.github.com/mnadeem/194b09fdbf8df0e8f0d2 /]

 Issue #2

This time you would get the following error

kt14

The issue is that ResteasyProviderFactory does not have instance of org.jboss.resteasy.core.Dispatcher and javax.servlet.ServletContext so that it can inject to KeycloakAppication

kt14

To fix the issue here is what I have done.

kt14

Make sure the following dependency is also added in the pom file

<dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>javax.servlet-api</artifactId>

    <scope>provided</scope>

    <version>3.1.0</version>

</dependency>

 Issue #3

After fixing the above issues you may get the following issue

kt14

The Tomcat Resource has to be configured properly

kt14

kt14

 For Tomcat external JNDI name starts with java:comp/env

Issue #4

This time you would get the following error

kt14

Copy the following jars to you TOMCAT_HOME/lib directory

kt14

Create setenv.bat/setenv.sh with the following content and copy it to TOMCAT_HOME/bin

CATALINA_OPTS=-Djavax.persistence.provider=org.hibernate.ejb.HibernatePersistence

Things should be working like a charm now

kt14

kt14

kt14

Two Issues on Keycloak

Issue #1 : No data for Dispather and ServletContext in ResteasyProviderFactory

Issue #2: keycloak-server.json is loaded from incorrect place, i.e, classes/META-INF

 config = Thread.currentThread().getContextClassLoader().getResource(“META-INF/keycloak-server.json”);

Refer this for TomEE setup