• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An example project for Keycloak Customizations

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 contexts, 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 hot-deploy (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

  • Extensions: SMS Authenticator, Backup-Codes, Remote Claim Mapper, Audit Event Listener, and Custom REST Endpoint the can expose custom endpoints: CustomResource
  • Support for deploying extensions to a 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
  • Support for exposing metrics via smallrye-metrics
  • Examples for running a cluster behind a reverse proxy with examples for HAProxy, Apache, nginx, caddy
  • Examples for running a Keycloak cluster with an external infinispan cluster with remote cache store and hotrod cache store.
  • Example for Keycloak with Graylog for log analysis, dashboards and alerting.
  • Example for metrics collection and dashboards with Prometheus and Grafana.
  • Example for tracing with OpenTelemetry and Jaeger

Usage envcheck

Tool Version
Java 17
mvn 3.8
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
# AND 
java bin/createTlsCerts.java --pkcs12 --keep

This will generate a TLS certificates and key file in .pem format in config/stage/dev/tls. The later command will create a certificate in .p12 PKCS12 format, which will be used as a custom truststore by Keycloak.

Register map the following host names in your hosts file configuration, e.g. /etc/hosts on linux / OSX or c:\Windows\System32\Drivers\etc\hosts on Windows:

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

Run with HTTPS

java start.java --https

The Keycloak admin-console will be available on https://admin.acme.test:8443/auth/admin.

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

Run with Legacy Keycloak

By default, we use the quarkus based Keycloak distribution to run the example environment. To use the legacy wildfly based Keycloak distribution, add the flag --keycloak=keycloak.

java start.java --keycloak=keycloak

Access metrics

The example environment includes an smallrye-metrics and eclipse-metrics integration for wildfly.

Metrics are exposed via the wildfly management interface on http://localhost:9990/metrics

Realm level metrics are collected by a custom EventListenerProvider called metrics.

Enable Graylog

The example environment can be configured to send Keycloak's logout output to Graylog via the --logging=graylog option.

Note that you need to download the logstash-gelf wildfly module and unzip the libraries into the deployments/local/dev/graylog/modules folder.

cd deployments/local/dev/graylog/modules
wget -O logstash-gelf-1.14.1-logging-module.zip https://search.maven.org/remotecontent?filepath=biz/paluch/logging/logstash-gelf/1.14.1/logstash-gelf-1.14.1-logging-module.zip
unzip -o logstash-gelf-1.14.1-logging-module.zip
rm *.zip

Run with Graylog

java start.java --logging=graylog

Enable Prometheus

Prometheus can scrape0 metrics from configured targets and persists the collected data in a time series database. The metrics data can be used to create monitoring dashboards with tools like grafana (see Grafana).

Scrape targets configured:

System Target Additional Labels
keycloak http://acme-keycloak:8080/auth/metrics env

Run with Prometheus

java start.java --metrics=prometheus

Enable Grafana

Grafana supports dashboards and alerting based on data from various datasources.

Note: To enable grafana with tls, a permission change is required as docker does not support a way to map users for shared files. You need to add read permissions for the key file acme.test+1-key.pem in config/stage/dev/tls for the group of the current user.

Access to Grafana can be configured in multiple ways, even a login with Keycloak is possible. In this example we use configured admin user account to access Grafana, but we also offer a login via Keycloak by leveraging the generic OAuth integration. Grafana is configured to not allow login as guest.

Run with Grafana

java start.java --grafana

Open Grafana

Manual steps when logged in as an Admin (Example User: devops_fallback, Password: test)

Enable Tracing

With OpenTelemetry and Jaeger, it is possible to trace requests traveling through Keycloak and the systems integrating it. This uses the Quarkus OpenTelemetry extension in order to create traces, which are then sent to the otel-collector. The collector then passes the information on to Jaeger, where they can be viewed in the web interface

Run with Tracing

java start.java --tracing

Open Jaeger or Jaeger with TLS, depending on configuration. When TLS is enabled, it is enabled for all three of the following:

  • Jaeger UI
  • Keycloak -> Collector communication
  • Collector -> Jaeger communication

Instrumentation

In order to gain additional insights, other applications that integrate with Keycloak can also send traces to the collector. The OpenTelemetry Documentation contains tools to instrument applications in various languages.

You can use the bin/downloadOtel.java scrtipt to download the otel agent.

Quarkus applications like Keycloak can also use the Quarkus OpenTelemetry extension instead of the agent. An example for running an instrumented Spring Boot app could look like this:

OTEL_METRICS_EXPORTER=none OTEL_SERVICE_NAME="frontend-webapp-springboot" OTEL_PROPAGATORS="b3multi" \
 OTEL_EXPORTER_OTLP_ENDPOINT="http://id.acme.test:4317" java -javaagent:bin/opentelemetry-javaagent.jar \
 -jar apps/frontend-webapp-springboot/target/frontend-webapp-springboot-0.0.1-SNAPSHOT.jar

The included IDEA run-config for the frontend-webapp-springboot module contains the necessary configuration to run that module with tracing enabled. If you then navigate to the frontend webapp, you can navigate through the application, and then later check the Jaeger UI for traces.

Clustering

Clustering examples can be found in the deployments/local/cluster folder.

Running with non-default docker networks

Some features of this project setup communicate with services inside the docker stack through the host. By default, the IP of the host in Docker is 172.17.0.1, but this can be changed by configuration. One reason to change it is because Wi-Fi on ICE trains uses IP addresses from the same network. An example for a changed setup from /etc/docker/daemon.json can look like this:

{
    "default-address-pools":
    [
        {"base":"172.19.0.0/16","size":24}
    ]
}

In this case, the host IP is 172.19.0.1, which can be configured for the project using the start option --docker-host=172.19.0.1

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://admin.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.

To build the image with Keycloak.X use:

mvn clean package -DskipTests -Ddocker.file=keycloakx/Dockerfile.plain io.fabric8:docker-maven-plugin:build

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/envcheck.java

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/realmImex.java --realm=acme-internal --verbose

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

java bin/realmImex.java --realm=acme-internal --verbose --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

More Repositories

1

awesome-keycloak

A curated list of resources for learning about http://www.keycloak.org
1,458
star
2

keycloak-extension-playground

Simple project environment for creating custom Keycloak extensions
Java
540
star
3

embedded-spring-boot-keycloak-server

Keycloak JAX-RS application embedded in a Spring-Boot App.
Java
263
star
4

keycloak-docker-demo

Repository for Keycloak Demos
Java
182
star
5

spring-boot-2-keycloak-oauth-example

PoC for Spring Boot 2 + Spring Security 5 + Keycloak 3.4.3 without Keycloak Adapter
Java
142
star
6

keycloak-health-checks

Keycloak Extension for health checks
Java
134
star
7

spring-labs

Here be dragons
Java
94
star
8

spring-boot-keycloak-server-example

THIS PROJECT MOVED: PoC for embedding Keycloak's JAX-RS application in a Spring-Boot App. New Location:
Java
92
star
9

keycloak-user-storage-provider-demo

Simple custom User Storage Provider extension for Keycloak. Deployed as .ear with dependencies & jboss-cli based setup.
Java
89
star
10

springio18-spring-keycloak

Slides & Code from my "How to secure your Spring Boot Apps with Keycloak" talk at Spring I/O 2018
Java
88
star
11

keycloak-avatar-minio-extension

Simple example for managing avatar images with Keycloak
Java
69
star
12

spring-boot-admin-keycloak-example

Example for protecting Spring Boot Admin & Spring Boot Actuator endpoints with Keycloak
Java
57
star
13

spring-boot-prometheus-example

Simple example for exposing Metrics in a Spring Boot App for consumption by Prometheus
Shell
51
star
14

kc-dnc-demo

PoC for securing AspNetCore based Web Apps with Keycloak.
C#
38
star
15

keycloak-freeradius-demo

PoC for delegating a free-radius authentication to Keycloak via PAM
Dockerfile
25
star
16

screen-casting-app

Spring Boot App which periodically captures Screenshots of the current system and exposes them via a simple Web-Interface
Java
22
star
17

quarkus-keycloak-demo

Simple example for a stateless JAX-RS webservice that can be accessed with JWTs issued by Keycloak
HTML
22
star
18

vertx-playground

Some experiments with the Vert.x Web Framework
Java
21
star
19

keycloak-custom-server

Custom Keycloak.X Server Distribution with selective features
Java
20
star
20

spring-boot-protobuf-example

Revised version of Josh Longs Spring Boot Protobuf example from 2015 https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services
Shell
19
star
21

spring-boot-micro-frontend-example

Simple Spring Boot frontend integration with ParcelJS
Java
18
star
22

keycloak-tc-extension-playground

Example for testing Keycloak extensions in docker with testcontainers
Java
16
star
23

keycloak_mod_auth_oidc_example

Demonstrates the configuration of the mod_auth_oidc Apache Module for use with Keycloak.
PHP
14
star
24

wjax2018-spring-keycloak

Code & Slides for my "Securing Spring Apps with Keycloak" talk at WJAX 2018
Java
13
star
25

prometheus-spring-boot-starter

An unofficial spring-boot-starter for the Prometheus Monitoring System
Shell
12
star
26

spring-boot-keycloak-jwe-example

PoC: Spring Boot Keycloak Signed & Encrypted JWT Example
Java
12
star
27

keycloak-infini-kube

PoC for a Keycloak Dockerimage with service discovery via KUBE_PING
Shell
12
star
28

keycloak-extensions-talk

Talk and Slides of my talk about Keycloak Extension Development
Java
10
star
29

keycloak-opa-config-validation

PoC for Validating Keycloak Configurations with Open Policy Agent Polices
Open Policy Agent
9
star
30

custom-opa-spicedb

OPA Authzed experiments
Go
9
star
31

go-kc-example

PoC for Keycloak OIDC Support in Golang
Go
7
star
32

custom-opa-openfga

OPA OpenFGA experiments
Go
6
star
33

spring-data-in-memory-poc

Java
6
star
34

keycloak-graalvm-js-scripting-provider

GraalVM.JS based JavaScript ScriptingProvider extension for Keycloak.
Java
5
star
35

jcon2017-keycloak

Code & Slides of my SSO with Keycloak talk at JCON 2017
Java
5
star
36

spring-boot-keycloak-graphql-demo

Example for securing a Spring Boot based GraphQL app with Keycloak
Java
5
star
37

de.tutorials.training.spring311

Tutorials.de Example Demonstrating some Spring 3.1.1 Goodness
JavaScript
5
star
38

quarkus-jumpstart-talk

Java
5
star
39

spring-boot-jee-web-example

Java
4
star
40

asw-web2-examples

Examples for the Course Web2 at ASW Berufsakademie Saarland
Java
4
star
41

sd-repositories-best-practices-javaland

Code & Slides from Spring Data Repositories Best Practices Talk at JavaLand 2016
Java
4
star
42

spring-boot-custom-starter-example

Java
4
star
43

spring-boot-cdi-instance-example

Prototypic implementation of support for CDI Instance<T> backed by a Spring BeanFactory.
Java
3
star
44

zitadel-spring-boot-examples

Zitadel.ch Example Project with Spring Boot and Spring Security
Java
3
star
45

wildfly-buildpack

Ruby
3
star
46

extism-java-example

Simple example for using the java-sdk of the extism web assembly plugin system.
Java
3
star
47

acme-keycloakx-demo

FreeMarker
2
star
48

java16-panama-win-dll-demo

Demo for calling a function from a Windows 64-bit DLL with Panama's Foreign Linker API
C
2
star
49

quarkus-demo

HTML
2
star
50

js-storytelling

JavaScript
2
star
51

thomasdarimont.github.com

The GitHub Page of thomasdarimont
JavaScript
2
star
52

java-docker-training

Java
2
star
53

spring-boot-k8s-app

Example spring-boot app with custom Java Image that can be run in Kubernetes
Dockerfile
2
star
54

keycloak-13.0.1-docker-microprofile-workaround

Workaround for removed Smallrye / Microprofile components for Keycloak 13.0.x
Dockerfile
2
star
55

spring-boot-dockerized-service-example

Shell
1
star
56

jvmoa

JVM Option Analyzer - A java based tool to list jvm settings of a local or a remote jvm
Java
1
star
57

spring-boot-keycloak-actuator-example

Shell
1
star
58

keycloakify-demo-app

TypeScript
1
star
59

java-workbench

Java Workbench for trying out things in various JDKs
Java
1
star
60

spring-boot2-training

Some experiments with Spring Boot 2
Java
1
star
61

spring-boot-keycloak-server-gist

Embedded Keycloak Server in Spring Boot
Java
1
star
62

embedded-quarkus-keycloak-server-example

PoC for an embedded keycloak server with Quarkus
HTML
1
star
63

spring-boot-layered-dockerimage-with-fabric8-demo

Example for building a layered Docker Image with fabric8 Maven Plugin and Spring Boot 2.3.0+
Java
1
star
64

reified-java-demo

Java
1
star
65

spring-boot-graal-demo

Java
1
star
66

spring-boot-camel-hawt-io-example

Java
1
star
67

git-training

Training project to learn how to do distributed development using git
1
star
68

de.thomasdarimont.labs.jpa

Plain JPA Examples Repository - Mostly for reporting bugs in the persistence providers
Java
1
star
69

201905-dubjug-keycloak-talk

Notes & Slides from Keycloak talk at Dublin Java User Group 2019
1
star
70

spring-boot-thymeleaf-layout-example

Shell
1
star
71

spring-boot-tls-with-san

Simple example for x509 Client Authentication with Mutual TLS
Java
1
star
72

javaland2023-authz-for-java-devs-with-opa

Slides & Code
1
star
73

jwks-aggregator

Java
1
star
74

goexercises

Go
1
star
75

archetype-demo

Custom Maven Archtetype Example that can be fetched via jitpack
Java
1
star
76

spring-training-spring-boot-keycloak-direct-access-grant-form-auth

PoC for using Keycloaks direct access grants as backing auth mechanism for form-based auth in a traditional spring security application
Java
1
star
77

springxd-smartgrid-demo

springxd-smartgrid-demo
1
star
78

keycloak-patches

backported patches for existing Keycloak versions
Java
1
star