-
Notifications
You must be signed in to change notification settings - Fork 6
Add a sample docker-compose configuration #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+272
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Keycloak Example for pg_oidc_validator | ||
|
|
||
| This example provides a demo environment with Keycloak and PostgreSQL configured for OAuth authentication using Docker Compose. | ||
|
|
||
| **Notes:** | ||
| * This is only a demo environment, with self-signed certificates. | ||
| Do not use for production. | ||
| * This example doesn't use persistent volumes. When you stop the containers, all data is lost. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### Required software | ||
|
|
||
| Docker with Compose V2 installed (`docker compose` command) | ||
|
|
||
| ### Add keycloak to hosts file | ||
|
|
||
| For the OAuth device flow to work in your browser, add `keycloak` to your hosts file: | ||
|
|
||
| **On Linux/Mac:** | ||
| ```bash | ||
| echo "127.0.0.1 keycloak" | sudo tee -a /etc/hosts | ||
| ``` | ||
|
|
||
| **On Windows:** Edit `C:\Windows\System32\drivers\etc\hosts` as Administrator and add: | ||
| ``` | ||
| 127.0.0.1 keycloak | ||
| ``` | ||
|
|
||
| This allows your browser to resolve the `https://keycloak:8443` URL used in the OAuth device flow. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```bash | ||
| # Start Keycloak and PostgreSQL | ||
| docker compose up -d | ||
|
|
||
| # Wait for services to be ready | ||
| # Optional: watch the logs while waiting | ||
| docker compose logs -f | ||
|
|
||
| # Once ready, run the interactive psql client | ||
| # Follow the OAuth device flow, login with `testuser` / `asdfasdf` | ||
| docker compose run --rm psql-client | ||
|
|
||
| # When done, stop the services | ||
| docker compose down | ||
| ``` | ||
|
|
||
| ## Keycloak admin interface access | ||
|
|
||
| Open https://keycloak:8443 in your browser: | ||
| - Username: `admin` | ||
| - Password: `admin` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIF+zCCA+OgAwIBAgIUVMuXUdFg9gbl4APdBQyTT9Yi4xkwDQYJKoZIhvcNAQEL | ||
| BQAwejELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFtZTERMA8GA1UEBwwI | ||
| Q2l0eU5hbWUxFDASBgNVBAoMC0NvbXBhbnlOYW1lMRswGQYDVQQLDBJDb21wYW55 | ||
| U2VjdGlvbk5hbWUxETAPBgNVBAMMCGtleWNsb2FrMB4XDTI1MTExMzE4NTEyNVoX | ||
| DTM1MTExMTE4NTEyNVowejELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFt | ||
| ZTERMA8GA1UEBwwIQ2l0eU5hbWUxFDASBgNVBAoMC0NvbXBhbnlOYW1lMRswGQYD | ||
| VQQLDBJDb21wYW55U2VjdGlvbk5hbWUxETAPBgNVBAMMCGtleWNsb2FrMIICIjAN | ||
| BgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp9HjUfZ3e40Hfa9ZxiJvPsP9fjZ/ | ||
| 7QKB+BCaJqPybJb1+Q1G4fRNrL611eVJRBR09Rf3hlH4e5/isHZeZx3SiGKq+O3C | ||
| E2wcG1mF9jQ4KULBqMFaTyT4N5rhFL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JWLaWDpS | ||
| Ltp8KVZzFt583O+DTygWhCmdXA7WIxz6QjiQYUTnTQHT2HC+EQqqYX+ZB3Gok8wx | ||
| 1YQ/SBgwL0cc+4ehJqYRXkf3he57AhbR/7LFsYeIHPNg+Swcn29Hr3R8hHo6Ghmr | ||
| v6oHxmVbIioGTeC5QUIfDJqiGg9O2uFxDqjCiihQNY1trs+Q13yRJ747g/BpQiha | ||
| CCd89b+bedZf4KuOGnbEVIOJqFEdnXyniggXOIf1Sqna5dv2b26Ld1OJqxIpgbZc | ||
| BKrKJTI2nzWje0PoNa+2IhIEP/vX/WEHFKzHuInwFQ5pTSIEKVzA+jQ7fLzH8zxZ | ||
| dwlUbvDy/C5NojepD/d9MnfMPfl78Lps6VrJJ7suOZBOuBmo/zCiaMg6+WGA5Iis | ||
| VaTPiZUtxKRX1jkGQBV2JNlYoJsWF6lW6Fzw05EBaN0hxvxvJjf3Ds50noZ2q3A= | ||
| -----END CERTIFICATE----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/bin/bash | ||
| # Command to generate the files in the certs folder. | ||
| # Since they have a 10 year lifetime, running this isn't necessary for a long time. | ||
| openssl req -x509 -newkey rsa:4096 -keyout key.pem -out crt.pem -sha256 -days 3650 -nodes \ | ||
| -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=keycloak" \ | ||
| -addext "subjectAltName=DNS:keycloak,DNS:localhost,IP:127.0.0.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| -----BEGIN PRIVATE KEY----- | ||
| MIIJQgIBADANBgkqhkiG9w0BA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| 9SFroCAMAxLx4s3/94bHECc4A2yonxgZzxSXKTB5/6MM+PcJP0X2oXDFZbRr9YD7 | ||
| c9xuQgJ61lbG3Y6cnjoUc5/xJysOT7uRTfKVL6haWuOUh6LyZaDBY1bDMRST5W1g | ||
| o6DCbFPHJduZ0A/Tpwojg3ZcxP82R/853atXZmlUuDjFHee1AWiLRJP60FqESx5A | ||
| Z0chEXWiWdvIrJ0PVV304gW1cdzvQQ== | ||
| -----END PRIVATE KEY----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| services: | ||
| keycloak: | ||
| image: quay.io/keycloak/keycloak:latest | ||
| hostname: keycloak | ||
| environment: | ||
| KC_BOOTSTRAP_ADMIN_USERNAME: admin | ||
| KC_BOOTSTRAP_ADMIN_PASSWORD: admin | ||
| KC_HTTPS_CERTIFICATE_FILE: /keys/crt.pem | ||
| KC_HTTPS_CERTIFICATE_KEY_FILE: /keys/key.pem | ||
| KC_HEALTH_ENABLED: "true" | ||
| KC_HOSTNAME: keycloak | ||
| KC_HOSTNAME_PORT: 8443 | ||
| KC_HOSTNAME_STRICT: "false" | ||
| KC_HOSTNAME_STRICT_BACKCHANNEL: "true" | ||
| volumes: | ||
| - ../../test/import:/opt/keycloak/data/import | ||
| - ./certs:/keys | ||
| ports: | ||
| - "127.0.0.1:8443:8443" | ||
| command: start-dev --import-realm | ||
| healthcheck: | ||
| test: ["CMD-SHELL", "exec 3<>/dev/tcp/localhost/8443"] | ||
| interval: 5s | ||
| timeout: 3s | ||
| retries: 60 | ||
| start_period: 30s | ||
|
|
||
| postgres: | ||
| image: docker.io/postgres:18 | ||
| environment: | ||
| POSTGRES_HOST_AUTH_METHOD: trust | ||
| volumes: | ||
| - ./certs/crt.pem:/usr/local/share/ca-certificates/keycloak-test.crt:ro | ||
| ports: | ||
| - "127.0.0.1:15432:5432" | ||
| depends_on: | ||
| keycloak: | ||
| condition: service_healthy | ||
| entrypoint: | ||
| - bash | ||
| - -c | ||
| - | | ||
| apt-get update -qq | ||
| apt-get install -y -qq ca-certificates curl libcurl4 wget | ||
| update-ca-certificates | ||
|
|
||
| wget -q https://github.com/Percona-Lab/pg_oidc_validator/releases/download/latest/pg-oidc-validator-pgdg18.deb | ||
| dpkg -i pg-oidc-validator-pgdg18.deb | ||
| rm pg-oidc-validator-pgdg18.deb | ||
|
|
||
| mkdir -p /etc/postgresql/conf.d /docker-entrypoint-initdb.d | ||
|
|
||
| cat > /etc/postgresql/conf.d/oauth.conf <<'EOF' | ||
| listen_addresses = '*' | ||
| oauth_validator_libraries = 'pg_oidc_validator' | ||
| pg_oidc_validator.authn_field = 'email' | ||
| EOF | ||
|
|
||
| cat > /tmp/pg_hba.conf <<'EOF' | ||
| host all all 0.0.0.0/0 oauth scope="email pgscope",issuer=https://keycloak:8443/realms/pgrealm,map=emap | ||
| local all all trust | ||
| host all all 127.0.0.1/32 trust | ||
| host all all ::1/128 trust | ||
| EOF | ||
|
|
||
| cat > /tmp/pg_ident.conf <<'EOF' | ||
| emap testuser@example.com testuser | ||
| EOF | ||
|
|
||
| cat > /docker-entrypoint-initdb.d/create-testuser.sh <<'EOF' | ||
| #!/bin/bash | ||
| set -e | ||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL | ||
| CREATE ROLE testuser LOGIN SUPERUSER; | ||
| EOSQL | ||
| EOF | ||
|
|
||
| chmod +x /docker-entrypoint-initdb.d/create-testuser.sh | ||
|
|
||
| exec docker-entrypoint.sh postgres -c hba_file=/tmp/pg_hba.conf -c ident_file=/tmp/pg_ident.conf -c config_file=/etc/postgresql/conf.d/oauth.conf | ||
| healthcheck: | ||
| test: ["CMD-SHELL", "pg_isready -U postgres"] | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 10 | ||
|
|
||
| psql-client: | ||
| image: postgres:18 | ||
| profiles: | ||
| - client | ||
| volumes: | ||
| - ./certs/crt.pem:/usr/local/share/ca-certificates/keycloak-test.crt:ro | ||
| depends_on: | ||
| postgres: | ||
| condition: service_healthy | ||
| entrypoint: | ||
| - bash | ||
| - -c | ||
| - | | ||
| apt-get update -qq | ||
| apt-get install -y -qq ca-certificates libpq-oauth | ||
| update-ca-certificates | ||
|
|
||
| echo '' | ||
| echo '========================================' | ||
| echo 'PostgreSQL OAuth Client' | ||
| echo '========================================' | ||
| echo '' | ||
| echo 'Connecting to PostgreSQL with OAuth...' | ||
| echo '' | ||
| echo 'When prompted, follow the device flow:' | ||
| echo ' 1. Visit the URL shown in your browser' | ||
| echo ' 2. Enter the device code' | ||
| echo ' 3. Login with: testuser / asdfasdf' | ||
| echo '' | ||
| echo '========================================' | ||
| echo '' | ||
|
|
||
| exec psql 'host=postgres dbname=postgres user=testuser oauth_issuer=https://keycloak:8443/realms/pgrealm oauth_client_id=pgtest' | ||
| stdin_open: true | ||
| tty: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.