A template project for Keycloak Customizations

Overview

Keycloak Project Example

Introduction

This repository contains a project setup for keycloak based projects.

This setup serves as a starting point to support the full lifecycle of development in a keycloak based project. This may include develop and deploy a set of Keycloak extensions, custom themes and configuration into a customized keycloak docker container (or tar-ball).

The project also shows how to write integration tests via Keycloak-Testcontainers. After successful test-run package all extensions and themes as a custom docker image. This image is meant to be the project base image fulfilling the projects requirements in contrast to the general keycloak image.

Use-Cases

These requirements work in different contextes, roles and use-cases:

a) Developer for keycloak themes, extensions and image

  1. build and integration-test with test-containers (uses standard keycloak image)
  2. run external keycloak with hotdeploy (theme, extension, ...), run integrationtest, e2e testing

a) Developer publishing an image:

  1. Standard keycloak docker image with extensions, themes und server config.
  2. Slim custom docker image with extensions, themes und server config (basis alpine) chose jdk version, base-os image version, base keycloak version.

c) Tester/Developer acceptance/e2e-testing with cypress

d) Operator configuring realm and server for different stages

Some Highlights

  • Extension: Custom REST Endpoint the can expose additional custom APIs: CustomResource
  • Support for deploying extensions to running Keycloak container
  • Support for instant reloading of theme and extension code changes
  • Support Keycloak configuration customization via CLI scripts
  • Examples for Integration Tests with Keycloak-Testcontainers
  • Example for End to End Tests with Cypress
  • Realm configuration as Configuration as Code via keycloak-config-cli
  • Multi-realm setup example with OpenID Connect and SAML based Identity Brokering
  • LDAP based User Federation backed by Docker-OpenLDAP
  • Mail Server integration backed by MailHog
  • TLS Support

Usage prerequisites

Tool Version
Java 11
mvn 3.6
docker 20.10
docker-compose 1.29

Development Environment

Build

The project can be build with the following maven command:

mvn clean verify

Build with Integration Tests

The example can be build with integration tests by running the following maven command:

mvn clean verify -Pwith-integration-tests

Run

We provide a platform agnostic single-file source-code Java launcher start.java to start the Keycloak environment.

To speed up development we can mount the keycloak/extensions class-folder and keycloak/themes folder into a Keycloak container that is started via docker-compose (see below). This allows for quick turnarounds while working on themes and extensions.

The default Keycloak admin username is admin with password admin.

Run with HTTP

You can start the Keycloak container via:

java start.java

Keycloak will be available on http://localhost:8080/auth.

Enable HTTPS

The example environment can be configured with https via the --https flag.

Preparation

Generate a certificate and Key for the example domain acme.test with mkcert.

java bin/createTlsCerts.java

This will generate a TLS certificate and key file in .pem format in config/stage/dev/tls.

Register map the following host names in your hosts configuration:

127.0.0.1 acme.test id.acme.test apps.acme.test admin.acme.test

Run with HTTPS

java start.java --https

Keycloak will be available on https://id.acme.test:8443/auth.

Note that after changing extensions code you need to run the java bin/triggerDockerExtensionDeploy.java script to trigger a redeployment of the custom extension by Keycloak.

Enable OpenLDAP

The example environment can be configured with OpenLDAP via the --openldap flag.

Run with OpenLDAP

java start.java --openldap

Enable PostgreSQL

The example environment can be configured to use PostgreSQL as a database via the --database=postgres flag to override the default h2 database.

Run with PostgreSQL

java start.java --database=postgres

Acme Example Realm Configuration

Realms

The example environment contains several realms to illustrate the interaction of different realms.

Acme-Apps Realm

The acme-apps realm contains a simple demo application and provides integration with the acme-internal, acme-ldap and acme-saml realm via Identity Brokering. The idea behind this setup is to provide a global acme-apps realm for applications that are shared between internal and external users.

The acme-internal realm provides applications that are only intended for internal users. The acme-ldap realm provides applications that are only intended for employees. The acme-internal and acme-ldap realms serve as an OpenID Connect based Identity Provider for the acme-apps realm. The acme-saml realm provides applications is similar to the acme-internal and serves as a SAML based Identity Provider for the acme-apps realm.

Acme-Internal Realm

The acme-internal realm contains a test users which are stored in the Keycloak database.

Users:

  • Username tester and password test (from database)
  • Username support and password test (from database)

The support user has access to a dedicated realm scoped admin-console and can perform user and group lookups. An example for a realm scoped admin-console URL is: https://id.acme.test:8443/auth/admin/acme-internal/console.

Acme-LDAP Realm

The acme-ldap realm contains a test user and is connected to a federated user store (LDAP directory) provided via openldap.

  • Username FleugelR and password Password1 (from LDAP federation)

Acme-SAML Realm

The acme-saml realm contains a test user and stores the users in the Keycloak database.

Users:

  • Username acmesaml and password test (from database)

Example App

A simple demo app can be used to show information from the Access-Token, ID-Token and UserInfo endpoint provided by Keycloak.

The demo app is started and will be accessible via http://localhost:4000/?realm=acme-internal or https://apps.acme.test:4443/?realm=acme-internal.

Deployment

Custom Docker Image

Build a custom Docker Image

The dockerfile for the docker image build uses the keycloak/Dockerfile.plain by default.

To build a custom Keycloak Docker image that contains the custom extensions and themes, you can run the following command:

mvn clean verify -Pwith-integration-tests io.fabric8:docker-maven-plugin:build

The dockerfile can be customized via -Ddocker.file=keycloak/Dockerfile.alpine-slim after mvn clean verify. It is also possible to configure the image name via -Ddocker.image=acme/acme-keycloak2.

Running the custom Docker Image locally

The custom docker image created during the build can be stared with the following command:

docker run \
--name acme-keycloak \
-e KEYCLOAK_USER=admin \
-e KEYCLOAK_PASSWORD=admin \
-e KEYCLOAK_CONFIG_FILE=standalone-ha.xml \
-v $PWD/imex:/opt/jboss/imex:z \
-it \
--rm \
-p 8080:8080 \
acme/acme-keycloak:latest

Testing

Run End to End Tests

The cypress based End to End tests can be found in the keycloak-e2e folder.

To run the e2e tests, start the Keycloak environment and run the following commands:

cd keycloak-e2e
yarn run cypress:open
# yarn run cypress:test

Scripts

Check prequisits

To manually check if all prequisits are fulfilled.

java bin/prerequisites.bin

Manually Trigger Extension Deployment

To manually trigger an extension redeployment after extension code changes / build, you can run the following script:

java bin/deployExtensions.java

Import-/Exporting a Realm

To import/export of an existing realm as JSON start the docker-compose infrastructure and run the following script. The export will create a file like acme-apps-realm.json in the ./keycloak/imex folder.

java bin/realm.java --realm=acme-apps

The import would search an file acme-apps-realm.json in the ./keycloak/imex folder.

java bin/realm.java --realm=acme-apps --action=import

Tools

Mailhog

Web Interface: http://localhost:1080/# Web API: http://localhost:1080/api/v2/messages

phpldapadmin

Web Interface: http://localhost:17080 Username: cn=admin,dc=corp,dc=acme,dc=local Password: admin

Misc

Add external tool in IntelliJ to trigger extension deployment

Instead of running the deployment trigger script yourself, you can register it as an external tool in IntelliJ as shown below.

  • Name: kc-deploy-extensions
  • Description: Deploy Extensions to Keycloak Docker Container
  • Program: $JDKPath$/bin/java
  • Arguments: $ProjectFileDir$/bin/deployExtensions.java
  • Working directory: $ProjectFileDir$
  • Only select: Synchronize files after execution.

The extensions can now be redeployed by running Tools -> External Tools -> kc-deploy-extensions

Add external tool in IntelliJ to trigger realm configuration

Instead of running the Keycloak Config CLI script yourself, you can register it as an external tool in IntelliJ as shown below.

  • Name: kc-deploy-config
  • Description: Deploy Realm Config to Keycloak Docker Container
  • Program: $JDKPath$/bin/java
  • Arguments: $ProjectFileDir$/bin/applyRealmConfig.java
  • Working directory: $ProjectFileDir$
  • Only select: Synchronize files after execution.

The extensions can now be redeployed by running Tools -> External Tools -> kc-deploy-config

Scan Image for Vulnerabilities

We use aquasec/trivy to scan the generated docker image for vulnerabilities.

java bin/scanImage.java --image-name=acme/acme-keycloak:latest
Comments
  • Update readme.md

    Update readme.md

    Update envcheck details to reflect current peroject dependencies

    While setting up a new Keycloak SPI project using your sample (thanks btw!), I recognized that it couldn't execute according to your readme instructions. I was using Ubuntu with package defaults (openjdk-11 and maven-3.6) and first got an error from maven invalid target release for the specified java version (which is set to 17).

    After updating to Java 17, my maven version (3.6) complained with a cryptic error which is obviously related to an incompability to Java 17. Updating to maven 3.8 fixed the issue.

    So please find my PR for the README attached.

    opened by datenzar 1
  • Several start up issues - included some suggestions and solutions

    Several start up issues - included some suggestions and solutions

    1. Encountered issues when building the project on Java 11:
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project backend-api-springboot3: Fatal error compiling: error: invalid target release: 17 -> [Help 1]   
    

    Resolved this by installing Java 17

    1. java start.java does not work. Resolved this by passing --http flag like so: java start.java --http
    2. Issue encountered when building service acme-keycloak:
    Building acme-keycloak
    [+] Building 1.8s (8/9)                                                                                                                                                   => [internal] load build definition from Dockerfile                                                                                                                0.0s
     => => transferring dockerfile: 1.28kB                                                                                                                              0.0s
     => [internal] load .dockerignore                                                                                                                                   0.0s
     => => transferring context: 2B                                                                                                                                     0.0s
     => [internal] load metadata for quay.io/keycloak/keycloak:19.0.1                                                                                                   1.1s
     => CACHED [1/5] FROM quay.io/keycloak/keycloak:19.0.1@sha256:b8dd5185ed3856488d127fe419a2593c675f1dc6c7e1e0977234f3b0cc13598f                                      0.0s
     => [internal] load build context                                                                                                                                   0.0s
     => => transferring context: 2B                                                                                                                                     0.0s
     => CANCELED [2/5] RUN microdnf update -y && microdnf install -y java-17-openjdk-devel && microdnf clean all                                                        0.5s
     => CACHED [3/5] RUN echo 1659621300842                                                                                                                             0.0s
     => ERROR [4/5] COPY --chown=keycloak:keycloak ./acme.test+1.pem /etc/x509/tls.crt.pem                                                                              0.0s
    ------
     > [4/5] COPY --chown=keycloak:keycloak ./acme.test+1.pem /etc/x509/tls.crt.pem:
    ------
    failed to compute cache key: "/acme.test+1.pem" not found: not found
    ERROR: Service 'acme-keycloak' failed to build : Build failed
    

    Resolved this by generating a certificate as described in here

    Summary of issues

    1. Should indicate that this should work on Java 17
    2. java start.java does not work
    3. java start.java --http still requires a certificate for some reason
    opened by tedmdelacruz 1
  • Better OpenTelemetry tracing

    Better OpenTelemetry tracing

    Currently, the operations of the traces produced by the Quarkus OTel extension only groups by HTTP request. This is inconvenient, but changing it will require changes in the quarkus extension.

    Another extension upgrade would be the ability to exclude certain paths from tracing/sampling. Currently, the extension supports skipping Quarkus /q endpoints, as well as "resources", but keycloak-resource-urls are not treated as "resources" by the configuration switch.

    opened by sonOfRa 0
  • OpenTelemetry authentication

    OpenTelemetry authentication

    While #40 adds support for OpenTelemetry-based tracing, for now all 3 interaction points lack authentication:

    • Jaeger UI
    • Jaeger collector endpoint
    • OpenTelemetry collector endpoint

    The Jaeger UI cannot be secured on its own, but this is easily doable via any HTTP reverse proxy that supports OIDC. The collector endpoints.

    The Jaeger collection endpoint also does not support authentication out of the box, but there are some blog posts by the developers that outline how it could be done: https://medium.com/jaegertracing/protecting-the-collection-of-spans-1948d88682e5

    The OTEL collector endpoint supports OIDC out of the box: https://opentelemetry.io/docs/collector/configuration/#authentication, but the agent does not. It looks like the suggested way of doing things is having the collector on localhost (or a sidecar) of the application, where only the application can talk to the collector. The collector then forwards them to Jaeger. This whole setup needs some proper investigation to see how and if it all works.

    opened by sonOfRa 0
  • External infinispan with kubernetes

    External infinispan with kubernetes

    HI Team, we deployed keycloak on EKS and are currently using the infinispan inbuilt in this cluster. We are planning to deploy inifinispan as a separate deployment on same EKS cluster or a separate cluster(not kubernetes) for inifinispan. how can we achieve this? and how can we point the keycloak kubernetes deployment to use this infinispan cluster for distributed caching.

    opened by Parth6288 0
  • Keycloak && IS cluster

    Keycloak && IS cluster

    Hello! We're using the same config of cache keycloak as described here - https://github.com/thomasdarimont/keycloak-project-example/blob/main/deployments/local/clusterx/haproxy-external-ispn/cache-ispn-remote.xml Keycloak (1 bare metal instance) - 18.0.0 (quakus) IS cluster (2 nodes) - 13.0.9

    But after "sudo bin/kc.sh start --auto-build --cache=ispn --cache-config-file=cache.xml --cache-stack tcp --https-port 443" faced with error

    ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Multiple datacenters available, but site name is not configured! Check your configuration

    Have you got any idea what does he expect for?

    opened by AlexeiKlimenko 0
  • Error to run scheduled task ClearExpiredClientInitialAccessTokens

    Error to run scheduled task ClearExpiredClientInitialAccessTokens

    After using the patch that ignores SkipCacheStore and start to using jdbc persistence, I start to see that error in log.

    keycloak-0 keycloak 12:43:25,227 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Timer-2) SQL Error: 0, SQLState: 22003
    keycloak-0 keycloak 12:43:25,227 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Timer-2) ERROR: integer out of range
    keycloak-0 keycloak 12:43:25,236 ERROR [org.keycloak.services] (Timer-2) KC-SERVICES0089: Failed to run scheduled task ClearExpiredClientInitialAccessTokens: javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute statement
    ...
    keycloak-0 keycloak Caused by: org.hibernate.exception.DataException: could not execute statement
    ...
    keycloak-0 keycloak Caused by: org.postgresql.util.PSQLException: ERROR: integer out of range
    ...
    

    keycloak version: 17.0.1-legacy environment: kubernetes

    Did you get this error in your tests?

    Thank you

    opened by klinux 1
Owner
Thomas Darimont
Spring Team Alumni, tutorials.de Admin AD, Open Sourcerer, @keycloak contributor, @jugsaar founder
Thomas Darimont
Lib-Tile is a multi Maven project written in JavaFX and NetBeans IDE 8 and provides the functionalities to use and handle easily Tiles in your JavaFX application.

Lib-Tile Intention Lib-Tile is a multi Maven project written in JavaFX and NetBeans IDE and provides the functionalities to use and handle easily Tile

Peter Rogge 13 Apr 13, 2022
To quickly integrate your applications into the EdgeGallery platform, we provide the toolchain project to help developers quickly modify code and migrate applications to the platform.

Toolchain 工具链 工具链是MEC Developer开发者平台中的一个重要特性,当x86平台的App想要上车ARM平台时,底层的代码不可避免的需要进行修改或重写。 App提供者可以通过MEC Developer开发者平台中集成的工具链进行源代码分析,定位需要修改的源代码并根据指导意见进行修

EdgeGallery 19 Jan 7, 2022
A project that shows the different ways on how to create custom controls in JavaFX

JavaFX Custom Controls This project will show different ways on how to create custom controls in JavaFX. It will cover the following approaches: Resty

Gerrit Grunwald 27 Sep 5, 2022
In the Developer - Platform of EdgeGallery, we have provided a lot of useful APIs, in this project, try to simulates APIs of the competence center to help develoers test API request and response online.

api-emulator api-emulator模块,为EdgeGallery提供了基本能力的模拟api,开发者可以调用该模拟器提供的api,不需要真实部署就可以查看平台已有的能力。目前该api-emulator集成了两种平台能力:位置服务和人脸识别能力。 平台能力简介 位置服务 提供用户位置,E

EdgeGallery 21 Dec 25, 2021
A 3D graphics engine created in Java. The project was inspired by javidx9's series on YouTube.

3D Graphics Engine A 3D graphics engine created in Java. The project was inspired by javidx9's series on YouTube. Installation Clone this repository o

Farhan Towhid 17 Sep 16, 2022
HUAWEI 3D Modeling Kit project contains a sample app. Guided by this demo, you will be able to implement full 3D Modeling Kit capabilities, including 3D object reconstruction and material generation.

HUAWEI 3D Modeling Kit Sample English | 中文 Introduction This project includes apps developed based on HUAWEI 3D Modeling Kit. The project directory is

HMS 59 Jan 1, 2023
Example of using the TestFX library to perform E2E tests in a Maven project

demoTestFX Ejemplo de uso de la librería TestFX para realizar pruebas E2E en un proyecto Maven. El principal problema para conseguir ejecutar los test

null 1 Jan 28, 2022
ITI TicTacToe Server project

TicTacToeClient ITI TicTacToe Client project Steps Clone project Create local branch name Development command git branch Development Checkout to the D

Mohamed Eldfrawy 1 Jan 27, 2022
ldts-project-assignment-g0103 created by GitHub Classroom

LDTS_T01_G03 - Maze Conqueror Game Description The game consists of solving procedurally generated mazes as fast as possible while avoiding any obstac

Eduardo Correia 1 May 23, 2022
The goal of this project is to create AssertJ assertions for JavaFX (8).

The goal of this project is to create AssertJ assertions for JavaFX (8).

Manuel Mauky 6 Jul 30, 2021
JavaFX with CDI example project

javafx-cdi-example JavaFX with CDI example. ⚠️ In case you have a hi-resolution display and want to run the application on Ubuntu, make sure to pass a

fuin.org 4 Aug 4, 2022
OOP course final project - Group: Leon Baiocchi, Federico Brunelli, Alessandro Pioggia, Luca Rengo

Bullet Ballet Bullet Ballet is a 2D platformer developed for a University Project at the Alma Mater Studiorum of Bologna by Leon Baiocchi, Alessandro

null 4 Dec 8, 2022
Project for "Software Engineering" Course, University of Macedonia (UoM).

CovIDetect CovIDetect is a dashboard application for universities that aims to facilitate the process of managing covid cases and provide statistical

null 12 Dec 21, 2022
ClashWars doesn't allow direct customizations, So here we are

ClashWarsFixes Custom made plugin for ClashWars bedwars plugin What does it do? As ClashWars is not configurable at all, I needed to customize some fu

Alijk 2 Nov 13, 2022
Te4j (Template Engine For Java) - Fastest and easy template engine

Te4j About the project Te4j (Template Engine For Java) - Fastest and easy template engine Pros Extremely fast (127k renders per second on 4790K) Easy

Lero4ka16 19 Nov 11, 2022
Keycloak: Home IdP Discovery - discover home identity provider or realm by email domain

Keycloak: Home IdP Discovery This is a simple Keycloak authenticator to redirect users to their home identity provider during login. What is it good f

Sven-Torben Janus 74 Dec 19, 2022
Plugin for keycloak that serves as an event listener, displaying user information in the log when there are registration and login events

Keycloak - Event listener Details Plugin for keycloak that serves as an event listener, displaying user information in the log when there are registra

José alisson 2 Jan 14, 2022
Keycloak Login Recaptcha

Keycloak Login Recaptcha Keycloak supports the recaptcha in the registration flow but not in the login flow at this time. That's why this repository i

null 7 Jun 4, 2022
Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker.

spring-microservice Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker. Arc

null 4 Sep 13, 2022
Keycloak - an Open Source Identity and Access Management tool

Keycloak is an Open Source Identity and Access Management tool. You can use it to add authentication to applications and secure services with minimum effort. No need to deal with storing users or authenticating users.

Erdem Günay 9 Sep 20, 2022