|
| 1 | +# Keycloak on LocalStack |
| 2 | + |
| 3 | +This repo contains a [LocalStack Extension](https://github.com/localstack/localstack-extensions) that runs [Keycloak](https://www.keycloak.org/) alongside LocalStack for identity and access management with local AWS applications. |
| 4 | + |
| 5 | +This Extension: |
| 6 | + |
| 7 | +- Spins up a Keycloak instance on LocalStack startup. |
| 8 | +- Auto-registers Keycloak as an OIDC identity provider in LocalStack IAM. |
| 9 | +- Ships with a default realm (`localstack`) ready for OAuth2/OIDC flows. |
| 10 | +- Exchanges Keycloak JWTs for temporary AWS credentials via `AssumeRoleWithWebIdentity`. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Docker |
| 15 | +- LocalStack Pro |
| 16 | +- `localstack` CLI |
| 17 | +- `make` |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +localstack extensions install "git+https://github.com/localstack/localstack-extensions.git#egg=localstack-keycloak&subdirectory=keycloak" |
| 23 | +``` |
| 24 | + |
| 25 | +## Install local development version |
| 26 | + |
| 27 | +To install the extension into LocalStack in developer mode, you will need Python 3.11, and create a virtual environment in the extensions project. |
| 28 | + |
| 29 | +```bash |
| 30 | +make install |
| 31 | +``` |
| 32 | + |
| 33 | +Then, to enable the extension for LocalStack, run |
| 34 | + |
| 35 | +```bash |
| 36 | +localstack extensions dev enable . |
| 37 | +``` |
| 38 | + |
| 39 | +You can then start LocalStack with `EXTENSION_DEV_MODE=1` to load all enabled extensions: |
| 40 | + |
| 41 | +```bash |
| 42 | +EXTENSION_DEV_MODE=1 localstack start |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +Start LocalStack: |
| 48 | + |
| 49 | +```bash |
| 50 | +localstack start |
| 51 | +``` |
| 52 | + |
| 53 | +Keycloak will be available at: |
| 54 | + |
| 55 | +| Endpoint | URL | |
| 56 | +|----------|-----| |
| 57 | +| Admin Console | http://localhost:8080/admin | |
| 58 | +| Token Endpoint | http://keycloak.localhost.localstack.cloud:4566/realms/localstack/protocol/openid-connect/token | |
| 59 | +| JWKS URL | http://keycloak.localhost.localstack.cloud:4566/realms/localstack/protocol/openid-connect/certs | |
| 60 | + |
| 61 | +Keycloak ports are exposed directly on the host for easy access: |
| 62 | + |
| 63 | +- **Admin Console & HTTP (8080)**: `http://localhost:8080` - Use this for the admin UI and direct API access |
| 64 | +- **Management (9000)**: `http://localhost:9000` - Health and metrics endpoints (Keycloak 26+) |
| 65 | + |
| 66 | +The gateway URL (`keycloak.localhost.localstack.cloud:4566`) is available for token endpoints and OIDC flows. |
| 67 | + |
| 68 | +- **Default Admin Credentials**: `admin` / `admin` |
| 69 | +- **Health check**: `curl http://localhost:9000/health/ready` |
| 70 | + |
| 71 | +### Get an Access Token |
| 72 | + |
| 73 | +```bash |
| 74 | +TOKEN=$(curl -s -X POST \ |
| 75 | + "http://keycloak.localhost.localstack.cloud:4566/realms/localstack/protocol/openid-connect/token" \ |
| 76 | + -d "grant_type=client_credentials" \ |
| 77 | + -d "client_id=localstack-client" \ |
| 78 | + -d "client_secret=localstack-client-secret" | jq -r '.access_token') |
| 79 | +``` |
| 80 | + |
| 81 | +### Exchange Token for AWS Credentials |
| 82 | + |
| 83 | +```bash |
| 84 | +# Create IAM role that trusts Keycloak |
| 85 | +cat > trust-policy.json << 'EOF' |
| 86 | +{ |
| 87 | + "Version": "2012-10-17", |
| 88 | + "Statement": [{ |
| 89 | + "Effect": "Allow", |
| 90 | + "Principal": { |
| 91 | + "Federated": "arn:aws:iam::000000000000:oidc-provider/keycloak.localhost.localstack.cloud:4566/realms/localstack" |
| 92 | + }, |
| 93 | + "Action": "sts:AssumeRoleWithWebIdentity" |
| 94 | + }] |
| 95 | +} |
| 96 | +EOF |
| 97 | + |
| 98 | +awslocal iam create-role \ |
| 99 | + --role-name KeycloakAuthRole \ |
| 100 | + --assume-role-policy-document file://trust-policy.json |
| 101 | + |
| 102 | +# Exchange Keycloak token for AWS credentials |
| 103 | +awslocal sts assume-role-with-web-identity \ |
| 104 | + --role-arn arn:aws:iam::000000000000:role/KeycloakAuthRole \ |
| 105 | + --role-session-name test-session \ |
| 106 | + --web-identity-token "$TOKEN" |
| 107 | +``` |
| 108 | + |
| 109 | +## Configuration |
| 110 | + |
| 111 | +| Environment Variable | Default | Description | |
| 112 | +|---------------------|---------|-------------| |
| 113 | +| `KEYCLOAK_REALM` | `localstack` | Name of the default realm | |
| 114 | +| `KEYCLOAK_VERSION` | `26.0` | Keycloak Docker image version | |
| 115 | +| `KEYCLOAK_REALM_FILE` | - | Path to custom realm JSON file | |
| 116 | +| `KEYCLOAK_DEFAULT_USER` | - | Username for auto-created test user | |
| 117 | +| `KEYCLOAK_DEFAULT_PASSWORD` | - | Password for auto-created test user | |
| 118 | +| `KEYCLOAK_OIDC_AUDIENCE` | `localstack-client` | Audience claim for OIDC provider | |
| 119 | +| `KEYCLOAK_FLAGS` | - | Additional flags for Keycloak start command | |
| 120 | + |
| 121 | +> **Note**: When using `localstack start`, prefix environment variables with `LOCALSTACK_` (e.g., `LOCALSTACK_KEYCLOAK_REALM`). |
| 122 | +
|
| 123 | +### Custom Realm Configuration |
| 124 | + |
| 125 | +Use your own realm JSON file with pre-configured users, roles, and clients. |
| 126 | + |
| 127 | +```bash |
| 128 | +# The path must be an absolute HOST path for Docker mount |
| 129 | +# Use LOCALSTACK_ prefix when running via CLI |
| 130 | +LOCALSTACK_KEYCLOAK_REALM_FILE=/path/to/my-realm.json localstack start |
| 131 | +``` |
| 132 | + |
| 133 | +See [`quickstart/sample-realm.json`](quickstart/sample-realm.json) for a realm template and [`quickstart/README.md`](quickstart/README.md) for a step-by-step guide. |
| 134 | + |
| 135 | +### Create Test Users |
| 136 | + |
| 137 | +```bash |
| 138 | +# Auto-create a test user on startup (use LOCALSTACK_ prefix with CLI) |
| 139 | +LOCALSTACK_KEYCLOAK_DEFAULT_USER=testuser LOCALSTACK_KEYCLOAK_DEFAULT_PASSWORD=password123 localstack start |
| 140 | +``` |
| 141 | + |
| 142 | +## Default Client |
| 143 | + |
| 144 | +The extension creates a default client `localstack-client` with: |
| 145 | + |
| 146 | +- **Client Secret**: `localstack-client-secret` |
| 147 | +- **Flows**: Authorization Code, Client Credentials, Direct Access Grants |
| 148 | +- **Service Account Roles**: `admin`, `user` |
| 149 | + |
| 150 | +The service account for `localstack-client` is automatically assigned the `admin` realm role, enabling full access when using client credentials flow. |
| 151 | + |
| 152 | +## Sample Application |
| 153 | + |
| 154 | +See the `sample-app/` directory for a complete example demonstrating: |
| 155 | + |
| 156 | +- API Gateway with Lambda Authorizer |
| 157 | +- JWT validation with Keycloak |
| 158 | +- Role-based access control |
| 159 | +- DynamoDB user management |
| 160 | + |
| 161 | +## Troubleshooting |
| 162 | + |
| 163 | +### Keycloak takes a long time to start |
| 164 | + |
| 165 | +Keycloak typically takes 30-60 seconds to fully start. The extension waits for the health check to pass before marking LocalStack as ready. |
| 166 | + |
| 167 | +### Health check endpoint returns 404 |
| 168 | + |
| 169 | +In Keycloak 26+, the health endpoint is on port 9000: |
| 170 | + |
| 171 | +```bash |
| 172 | +curl http://localhost:9000/health/ready |
| 173 | +``` |
| 174 | + |
| 175 | +### View Keycloak logs |
| 176 | + |
| 177 | +```bash |
| 178 | +docker logs ls-ext-keycloak |
| 179 | +``` |
| 180 | + |
| 181 | +## Development |
| 182 | + |
| 183 | +```bash |
| 184 | +# Install dependencies |
| 185 | +make install |
| 186 | + |
| 187 | +# Run tests (requires LocalStack with extension running) |
| 188 | +make test |
| 189 | + |
| 190 | +# Format code |
| 191 | +make format |
| 192 | + |
| 193 | +# Lint |
| 194 | +make lint |
| 195 | +``` |
| 196 | + |
| 197 | +## License |
| 198 | + |
| 199 | +Apache License 2.0 |
0 commit comments