はじめに
Keycloak(RedHat Build of Keycloak)ではkc.sh(kc.bat)でレルムの情報をエクスポート及びインポートが行えます。
本記事ではレルムをエクスポートした情報を取り込み直すことで、ユーザ情報がパスワード含めて移行できることを確認しました。
環境情報
- Red Hat Enterprise Linux 9.3
- rhbk-26.0.12
事前準備
管理コンソールから「test」というレルムを新規作成し、同レルムにtestuserというユーザの作成を行います。testuserにはpasswordというパスワードを設定してしました。
その状態で以下のコマンドでkcadmでログインを試みます。これによって簡易的にIDとパスワードによるログイン検証が行えます。
$ ./bin/kcadm.sh config credentials --server http://localhost:8080 --realm test --user testuser Logging into http://localhost:8080 as user testuser of realm test Enter password: Account is not fully set up [invalid_grant]
上記はパスワード認証が成功した際の応答で、パスワード認証が失敗した場合は以下の応答(Invalid user credentials)が出力されます。
# ./bin/kcadm.sh config credentials --server http://localhost:8080 --realm test --user testuser Logging into http://localhost:8080 as user testuser of realm test Enter password: Invalid user credentials [invalid_grant]
レルム情報の出力
サービスを停止させた状態で、以下のコマンドを押下します。事前に出力先のディレクトリは作成しました。
# ./bin/kc.sh export --dir export_dir/ # ls export_dir/ master-realm.json master-users-0.json test-realm.json test-users-0.json
今回はtestレルムを対象とするため、masterレルムに関するjsonファイルは削除します。
尚、ユーザ情報側のJSONファイルには以下のようにcredentialsとしてパスワード情報が含まれています。
{ "realm" : "test", "users" : [ { "id" : "xx-xx-xx-xx-xx", "username" : "testuser", "emailVerified" : false, "createdTimestamp" : xx, "enabled" : true, "totp" : false, "credentials" : [ { "id" : "xx-xx-xx-xx-xx", "type" : "password", "userLabel" : "My password", "createdDate" : xx, "secretData" : "{\"value\":\"+xx+\",\"salt\":\"xx==\",\"additionalParameters\":{}}", ...
レルム情報の取り込み
再度サービスを立ち上げ、testレルム自体の削除を行います。
レルムの削除が終わるとまたサービスを落とし、出力したファイルが格納されているディレクトリに対してimportを行います。
$ ./bin/kc.sh import --dir export_dir/
するとサーバログに以下のように出力された上で
INFO [org.keycloak.exportimport.util.ImportUtils] (main) Realm 'test' imported INFO [org.keycloak.exportimport.dir.DirImportProvider] (main) Imported users from /xx/rhbk-26.0.12/export_dir/test-users-0.json INFO [org.keycloak.services] (main) KC-SERVICES0032: Import finished successfully
ユーザ情報がパスワード含めて再作成されていました。コマンド自体は下記で確認しています。
$ ./bin/kcadm.sh config credentials --server http://localhost:8080 --realm test --user testuser Logging into http://localhost:8080 as user testuser of realm test Enter password: Account is not fully set up [invalid_grant]
終わりに
kc.sh export 及び kc.sh import を実際に試し、ユーザ情報が正しく移行できていることが確認できました。
以上、ご参考になれば幸いです。