Migrate from Keycloak to Ory Kratos
This guide covers moving users from Keycloak to Ory Kratos: what carries over and what doesn’t, how their passwords and second factors are handled, and the commands to run. It sticks to what is specific to this pair. The export guide for Keycloak and the import guide for Ory Kratos cover each side in full, including a rehearsal with synthetic users.
What carries over
Section titled “What carries over”| Keycloak to Ory Kratos | |
|---|---|
| Passwords | Partly kept: Argon2 hashes are imported. PBKDF2 users must reset their password. |
| Second factors | TOTP can’t be imported and makes the user fail: remove it first. Users enroll again. |
| User IDs | Ory Kratos generates new IDs. The report’s target_ids maps each source_id to its new one. |
The Keycloak export leaves behind:
- Group and role memberships
- Federated (social) identities
- HOTP credentials, which CMF has no type for
- Required actions, consents and timestamps
Before you start
Section titled “Before you start”- iamigrate installed (see Installation).
- Shell access to the Keycloak server or container, to run
kc.sh export. The Admin REST API never returns password hashes, so iamigrate reads a realm export rather than a live instance. - Ory Kratos. A self-hosted Kratos instance whose Admin API you can reach, with an identity schema that accepts the traits iamigrate writes and the
passwordmethod enabled (use a disposable instance for the rehearsal). The import guide walks through it.
1. Export from Keycloak
Section titled “1. Export from Keycloak”# On the Keycloak host, or inside its containerkc.sh export --dir /tmp/export --realm acme --users realm_file --http-management-port 9001
# Copy the result out, then convert itiamigrate export --source keycloak --in ./realm-export --out ./export/Service account users and users iamigrate can’t translate are skipped and listed in manifest.json. The Keycloak export guide covers running kc.sh export next to a live server.
The export is a snapshot: users created, and passwords changed, after it aren’t in it, so plan a freeze or a final re-export before cutover. Check export/manifest.json for skipped_records, because the command exits with status 0 even when users are skipped, and for the per-algorithm counts.
2. Deal with passwords
Section titled “2. Deal with passwords”Keycloak stores PBKDF2 and Argon2, and Ory Kratos imports only Argon2. A user whose hash can’t be translated isn’t created at all, so deal with the PBKDF2 users before the import.
| Hash in the export | Ory Kratos |
|---|---|
| PBKDF2 | Not supported. The user isn’t created: it fails validate and the import reports TRANSLATION_ERROR. |
| Argon2 | Imported if it is argon2id. argon2i and argon2d are rejected. |
manifest.json shows how many users hold each algorithm. If any hold PBKDF2, drop their password, so they are created without one:
gunzip -c export/users.cmf.jsonl.gz \ | jq -c 'if .password.algorithm == "pbkdf2" then .password = null else . end' \ | gzip > export/users.ready.cmf.jsonl.gzThose users then need to set a new password. Let them use Kratos’ recovery flow. The minimal identity schema in the import guide enables recovery by email.
Read the later steps from export/users.ready.cmf.jsonl.gz.
3. Deal with second factors
Section titled “3. Deal with second factors”| Second factor in the export | On Ory Kratos |
|---|---|
| TOTP (portable) | The user isn’t created. Kratos v1.3.1 rejects the totp credential, so remove the factor first (see below). |
| WebAuthn (flagged non-portable) | Not imported. The user is created and listed in the report for follow-up, and enrolls again after signing in. |
| Recovery codes (flagged non-portable) | Not imported. The user is created and listed in the report for follow-up, and enrolls again after signing in. |
To create those users anyway, remove the totp factors from the CMF records. They can enroll again after signing in:
gunzip -c export/users.ready.cmf.jsonl.gz \ | jq -c '.mfa_factors = [(.mfa_factors // [])[] | select(.type != "totp")]' \ | gzip > export/users.nototp.cmf.jsonl.gz4. Fit the data to Ory Kratos
Section titled “4. Fit the data to Ory Kratos”- The identity schema decides what imports. Kratos rejects any identity that doesn’t match it. The schema needs an
emailtrait marked as a password identifier (orusername/phone, if that’s how your users sign in) and must not require traits iamigrate doesn’t fill. Runvalidatewith--schema-fileto find mismatches before the import. - Metadata becomes traits. Every other user attribute arrives in
user_metadata. Everyuser_metadatakey is also copied intotraits, so the schema must declare it or set"additionalProperties": true. A key namedemail,usernameorphonecollides with an identifier, andvalidatereports it. - Phone numbers. The first one becomes the
phonetrait, so declare it in the schema, as"format": "tel"if you want it checked. Kratos validates it as E.164. - Names aren’t imported. Kratos gets no
given_name,family_nameorname. To keep them, add them touser_metadataunder the trait names your schema expects, with ametadataroute inmapping.yamlor by editing the CMF records. - Username. It becomes
traits.username, which must be declared in the schema and be at most 255 characters.
5. Validate against Ory Kratos
Section titled “5. Validate against Ory Kratos”Set the target in the scaffolded mapping, then check the export against what the target can import. validate makes no network calls:
iamigrate map --target kratos --mapping ./export/mapping.yamliamigrate validate --in ./export/users.nototp.cmf.jsonl.gz \ --mapping ./export/mapping.yaml --target kratos --schema-file identity.schema.json--schema-file checks every user’s traits against your identity schema, so schema problems show up before the import.
Fix every reported problem at the source, or in the CMF records, and run it again until it comes back clean. Review mapping.yaml too: custom metadata keys are flagged under manual_steps for you to place, rather than guessed at.
6. Rehearse, then import
Section titled “6. Rehearse, then import”Do a rehearsal first. Follow the Ory Kratos import guide with a fixture that holds the same hash algorithms and second factors as your users, against a disposable target, and sign in as a few of the users (see Generate test fixtures).
For the real run, point iamigrate at Ory Kratos:
export KRATOS_ADMIN_URL=http://127.0.0.1:4434Split off a small canary, import it, and check the result before you import the rest:
gunzip -c export/users.nototp.cmf.jsonl.gz | head -n 100 | gzip > export/canary.cmf.jsonl.gzgunzip -c export/users.nototp.cmf.jsonl.gz | tail -n +101 | gzip > export/rest.cmf.jsonl.gz
iamigrate import kratos \ --in ./export/canary.cmf.jsonl.gz \ --mapping ./export/mapping.yaml \ --schema-id default \ --report ./export/canary-report.jsonSign in as a few canary users, then import the rest:
iamigrate import kratos \ --in ./export/rest.cmf.jsonl.gz \ --mapping ./export/mapping.yaml \ --schema-id default \ --report ./export/rest-report.jsonThe command exits with status 0 even when users fail. Always read the reports: each failed user is listed under failed with a code, and the follow-up lists (requires_password_reset, requires_reenrollment and so on) say who needs to hear from you. The import guide explains each code.
7. Verify
Section titled “7. Verify”Reconcile the file with the live target. It reports users missing from it and drift in the fields it can read back:
iamigrate diff kratos --in ./export/users.nototp.cmf.jsonl.gz --mapping ./export/mapping.yamlA user showing up doesn’t prove its password hash was translated correctly. Before the export, create a few test users in Keycloak with passwords you know, one for each hash algorithm in manifest.json, and sign in as each of them on Ory Kratos after the import: Kratos’ login flow, or any application built on it.
Kratos generates each identity’s UUID and rejects a chosen one. The report’s target_ids maps every source_id to it. If your applications reference users by the old ID, keep that mapping.
Cutover
Section titled “Cutover”- Freeze changes in Keycloak, re-export, and import the final file.
- Reconnect what the export doesn’t carry: federated (social) identities, and group and role memberships. Then point every application at Ory Kratos.
- Tell users what to expect. Users whose hash was imported keep their password. The others need a reset. A second factor that didn’t move has to be enrolled again.
- Clean up the rehearsal. Delete the rehearsal identities with
rollback kratos, or throw the disposable instance away.
See also
Section titled “See also”- Export users from Keycloak
- Import users into Ory Kratos
export,validateandmapreferencesimport kratosanddiff kratosmapping.yaml