This image provides a powerful integration for Kong API Gateway to support seamless OpenID Connect (OIDC) authentication.
cookies-to-headers plugin to propagate cookies (e.g., ID tokens) as headers to your backend services.This guide explains how to quickly set up a development environment for cnapcloud/kong-oidc using Docker Compose, including PostgreSQL, Keycloak, Redis, and Kong.
kong-oidc-demo
├─ docker-compose.yml
├─ .env
└─ init-db.sql
Create a .env file with the following content:
# Postgres
POSTGRES_TAG=:13
POSTGRES_USER=postgres
POSTGRES_PW=password
POSTGRES_DB_NAME=postgres
# Database for Kong
KONG_DB_USER=kong
KONG_DB_PW=password
KONG_DB_NAME=kong
# Keycloak
KEYCLOAK_TAG=:25.0.6
KEYCLOAK_PORT=8080
KEYCLOAK_USER=admin
KEYCLOAK_PW=password
KEYCLOAK_DB_USER=keycloak
KEYCLOAK_DB_PASSWORD=password
KEYCLOAK_DB_NAME=keycloak
# Kong
KONG_TAG=:3.9.1
KONG_DB_PORT=5432
KONG_SESSION_STORE_PORT=6379
KONG_HTTP_ADMIN_PORT=8001
KONG_HTTP_PROXY_PORT=8000
Change passwords and ports if needed for your environment.
Create an init-db.sql file with the following content to set up PostgreSQL users and databases:
-- User creation
CREATE USER keycloak WITH PASSWORD 'password';
CREATE USER kong WITH PASSWORD 'password';
-- Database creation
CREATE DATABASE kong OWNER kong;
CREATE DATABASE keycloak OWNER keycloak;
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE kong TO kong;
GRANT ALL PRIVILEGES ON DATABASE keycloak TO keycloak;
This SQL will automatically be executed when the Postgres container starts.
docker-compose.ymlCreate a docker-compose.yml with the following configuration:
services:
postgres_db:
image: postgres${POSTGRES_TAG}
ports:
- 5432:5432
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PW}
POSTGRES_DB: ${POSTGRES_DB_NAME}
volumes:
- .data/postgres16:/var/lib/postgresql/data
- ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
keycloak:
image: quay.io/keycloak/keycloak${KEYCLOAK_TAG}
environment:
JAVA_OPTS: -Xms1024m -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=*:8000
_JAVA_OPTIONS: -XX:UseSVE=0
KC_LOG_LEVEL: info
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://postgres_db/${KEYCLOAK_DB_NAME}
KC_DB_USERNAME: postgres
KC_DB_PASSWORD: password
KC_HTTP_ENABLED: true
HTTP_ADDRESS_FORWARDING: true
KEYCLOAK_ADMIN: ${KEYCLOAK_USER}
KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_PW}
command: start-dev
ports:
- 8080:8080
- 9000:9000
depends_on:
- postgres_db
redis:
image: redis
command: ["redis-server", "--requirepass", "redis"]
ports:
- 6389:6379
kong:
image: cnapcloud/kong-oidc${KONG_TAG}
ports:
- 8000:8000
- 8443:8443
- 8001:8001
- 8002:8002
- 8444:8444
environment:
KONG_LOG_LEVEL: info
KONG_PLUGINS: bundled,oidc,cookies-to-headers
KONG_X_SESSION_COMPRESSOR: zlib
KONG_NGINX_LARGE_CLIENT_HEADER_BUFFERS: "4 16k"
KONG_X_SESSION_STORAGE: redis
KONG_X_SESSION_REDIS_HOST: redis
KONG_X_SESSION_REDIS_PASSWORD: redis
KONG_DB_UPDATE_FREQUENCY: "5"
KONG_DB_UPDATE_PROPAGATION: "0"
KONG_DB_CACHE_TTL: "3600"
KONG_DATABASE: postgres
KONG_PG_HOST: postgres_db
KONG_PG_DATABASE: ${KONG_DB_NAME}
KONG_PG_USER: ${KONG_DB_USER}
KONG_PG_PASSWORD: ${KONG_DB_PW}
KONG_ADMIN_LISTEN: 0.0.0.0:${KONG_HTTP_ADMIN_PORT}
KONG_PROXY_LISTEN: 0.0.0.0:${KONG_HTTP_PROXY_PORT}
KONG_PROXY_ACCESS_LOG: /dev/stdout
KONG_ADMIN_ACCESS_LOG: /dev/stdout
KONG_PROXY_ERROR_LOG: /dev/stderr
KONG_ADMIN_ERROR_LOG: /dev/stderr
depends_on:
- postgres_db
httpbin:
image: kong/httpbin:latest
ports:
- 9080:80
Remove the _JAVA_OPTIONS: -XX:UseSVE=0 entry from the Keycloak service environment configuration on amd64 linux systems.
Run the following command in the project root:
docker-compose up postgres_db redis -d
docker-compose run --rm kong kong migrations bootstrap
docker-compose up -d
This will:
httpbin) for proxy testing.localhost:5432http://localhost:8080http://localhost:8001http://localhost:8002http://localhost:8000http://localhost:9080
docker-compose logs -f
docker-compose down
This stops and removes all containers while keeping volumes intact.
| Variable | Description | Default / Example |
|---|---|---|
| KONG_LOG_LEVEL | Kong log level | info |
| KONG_PLUGINS | List of plugins to load | bundled,oidc,cookies-to-headers |
| KONG_DATABASE | Database type | postgres |
| KONG_PG_HOST | PostgreSQL host | postgres_db |
| KONG_PG_DATABASE | PostgreSQL database name | ${KONG_DB_NAME} |
| KONG_PG_USER | PostgreSQL username | ${KONG_DB_USER} |
| KONG_PG_PASSWORD | PostgreSQL password | ${KONG_PW} |
| KONG_ADMIN_LISTEN | Admin API listen address | 0.0.0.0:8001 |
| KONG_PROXY_LISTEN | Proxy listen address | 0.0.0.0:8000 |
| Variable | Description | Default / Example |
|---|---|---|
| KONG_X_SESSION_STORAGE | Session storage backend | redis, shm, cookie, memcached, dshm |
| KONG_X_SESSION_NAME | Session name identifier | oidc_session |
| KONG_X_SESSION_COMPRESSOR | Session compression algorithm | zlib |
| Variable | Description | Default / Example |
|---|---|---|
| KONG_X_SESSION_REDIS_HOST | Redis host for session storage | redis |
| KONG_X_SESSION_REDIS_PASSWORD | Redis password | redis |
| KONG_X_SESSION_REDIS_DATABASE | Redis DB index | 0 |
| KONG_X_SESSION_REDIS_PREFIX | Redis key prefix | kong_sessions |
| KONG_X_SESSION_REDIS_POOL_SIZE | Redis connection pool size | 30 |
| KONG_X_SESSION_REDIS_POOL_TIMEOUT | Redis pool timeout | 3000 |
| Variable | Description | Default / Example |
|---|---|---|
| KONG_X_SESSION_SHM_STORE | Shared memory store name | oidc_sessions |
| KONG_X_SESSION_SHM_STORE_SIZE | SHM store size | 5m |
| Variable | Description | Default / Example |
|---|---|---|
| KONG_DB_UPDATE_FREQUENCY | DB cache sync frequency | 5 |
| KONG_DB_CACHE_TTL | Cache TTL | 3600 |
| Variable | Description | Default / Example |
|---|---|---|
| X_OIDC_CACHE_DISCOVERY_SIZE | Cache size for OIDC discovery | 128k |
| X_OIDC_CACHE_JWKS_SIZE | Cache size for JWKS | 128k |
| X_OIDC_CACHE_INTROSPECTION_SIZE | Cache size for token introspection | 128k |
For production, it’s recommended to use redis as session storage and configure connection credentials securely.
Enhancements and fixes introduced while testing with Keycloak 25.0.6, focusing on OIDC compatibility, logout handling, and session management. A detailed list of the improvements is provided below.
session_name = route_id .. "_" .. plugin_name .. "_session" ensuring service-level session isolation.Content type
Image
Digest
sha256:9a4386f31…
Size
265.1 MB
Last updated
6 months ago
docker pull cnapcloud/kong-oidc:3.9.1