-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Allow importing realms during startup #10754
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <#import "/templates/guide.adoc" as tmpl> | ||
| <#import "/templates/kc.adoc" as kc> | ||
|
|
||
| <@tmpl.guide | ||
| title="Importing and Exporting Realms" | ||
| summary="An overview about how to import and export realms"> | ||
|
|
||
| In this guide, you are going to understand the different approaches for importing and exporting realms using JSON files. | ||
|
|
||
| == Exporting a Realm to a Directory | ||
|
|
||
| To export a realm, you can use the `export` command. Your Keycloak server instance must not be started when invoking this command. | ||
|
|
||
| <@kc.export parameters="--help"/> | ||
|
|
||
| To export a realm to a directory, you can use the `--dir <dir>` option. | ||
|
|
||
| <@kc.export parameters="--dir <dir>"/> | ||
|
|
||
| When exporting realms to a directory, the server is going to create separate files for each realm being exported. | ||
|
|
||
| === Configuring how users are exported | ||
|
|
||
| You are also able to configure how users are going to be exported by setting the `--users <strategy>` option. The values available for this | ||
| option are: | ||
|
|
||
| * *different_files*: Users export into different json files, depending on the maximum number of users per file set by `--users-per-file`. This is the default value. | ||
|
|
||
| * *skip*: Skips exporting users. | ||
|
|
||
| * *realm_file*: Users will be exported to the same file as the realm settings. For a realm named "foo", this would be "foo-realm.json" with realm data and users. | ||
|
|
||
| * *same_file*: All users are exported to one explicit file. So you will get two json files for a realm, one with realm data and one with users. | ||
|
|
||
| If you are exporting users using the `different_files` strategy, you can set how many users per file you want by setting the `--users-per-file` option. The default value is `50`. | ||
|
|
||
| <@kc.export parameters="--dir <dir> --users different_files --users-per-file 100"/> | ||
|
|
||
| == Exporting a Realm to a File | ||
|
|
||
| To export a realm to a file, you can use the `--file <file>` option. | ||
|
|
||
| <@kc.export parameters="--file <file>"/> | ||
|
pedroigor marked this conversation as resolved.
Outdated
|
||
|
|
||
| When exporting realms to a file, the server is going to use the same file to store the configuration for all the realms being exported. | ||
|
pedroigor marked this conversation as resolved.
Outdated
|
||
|
|
||
| == Exporting a specific realm | ||
|
|
||
| If you do not specify a specific realm to export, all realms are exported. To export a single realm, you can use the `--realm` option as follows: | ||
|
|
||
| <@kc.export parameters="[--dir|--file] <path> --realm my-realm"/> | ||
|
|
||
| == Importing a Realm from a Directory | ||
|
|
||
| To import a realm, you can use the `import` command. Your Keycloak server instance must not be started when invoking this command. | ||
|
|
||
| <@kc.import parameters="--help"/> | ||
|
|
||
| After exporting a realm to a directory, you can use the `--dir <dir>` option to import the realm back to the server as follows: | ||
|
|
||
| <@kc.import parameters="--dir <dir>"/> | ||
|
|
||
| When importing realms using the `import` command, you are able to set if existing realms should be skipped, or if they should be overridden with the new configuration. For that, | ||
| you can set the `--override` option as follows: | ||
|
|
||
| <@kc.import parameters="--dir <dir> --override false"/> | ||
|
|
||
| By default, the `--override` option is set to `true` so that realms are always overridden with the new configuration. | ||
|
|
||
| == Importing a Realm from a File | ||
|
|
||
| To import a realm previously exported in a single file, you can use the `--file <file>` option as follows: | ||
|
|
||
| <@kc.import parameters="--file <file>"/> | ||
|
|
||
| == Importing a Realm during Startup | ||
|
|
||
| You are also able to import realms when the server is starting by using the `--import-realm` option. | ||
|
|
||
| <@kc.start parameters="--import-realm"/> | ||
|
|
||
| When you set the `--import-realm` option, the server is going to try to import any realm configuration file from the `data/import` directory. Each file in this directory should | ||
| contain a single realm configuration. | ||
|
|
||
| If a realm already exists in the server, the import operation is skipped. | ||
|
pedroigor marked this conversation as resolved.
Outdated
|
||
|
|
||
| == Using Environment Variables within the Realm Configuration Files | ||
|
|
||
| When importing a realm, you are able to use placeholders to resolve values from environment variables for any realm configuration. | ||
|
pedroigor marked this conversation as resolved.
Outdated
|
||
|
|
||
| .Realm configuration using placeholders | ||
| [source, bash] | ||
| ---- | ||
| { | ||
| "realm": "${r"${MY_REALM_NAME}"}", | ||
| "enabled": true, | ||
| ... | ||
| } | ||
| ---- | ||
|
|
||
| In the example above, the value set to the `MY_REALM_NAME` environment variable is going to be used to set the `realm` property. | ||
|
|
||
| </@tmpl.guide> | ||
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
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
56 changes: 56 additions & 0 deletions
56
quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/command/ImportRealmMixin.java
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,56 @@ | ||
| /* | ||
| * Copyright 2021 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.quarkus.runtime.cli.command; | ||
|
|
||
| import static org.keycloak.quarkus.runtime.cli.Picocli.NO_PARAM_LABEL; | ||
|
|
||
| import java.io.File; | ||
| import java.util.Optional; | ||
| import org.keycloak.quarkus.runtime.Environment; | ||
|
|
||
| import picocli.CommandLine; | ||
|
|
||
| public final class ImportRealmMixin { | ||
|
|
||
| public static final String IMPORT_REALM = "--import-realm"; | ||
|
|
||
| @CommandLine.Spec | ||
| private CommandLine.Model.CommandSpec spec; | ||
|
|
||
| @CommandLine.Option(names = IMPORT_REALM, | ||
| description = "Import realms during startup by reading any realm configuration file from the 'data/import' directory.", | ||
| paramLabel = NO_PARAM_LABEL, | ||
| arity = "0") | ||
| public void setImportRealm(String realmFiles) { | ||
| StringBuilder filesToImport = new StringBuilder(Optional.ofNullable(realmFiles).orElse("")); | ||
|
|
||
| if (filesToImport.length() > 0) { | ||
| throw new CommandLine.ParameterException(spec.commandLine(), "Instead of manually specifying the files to import, just copy them to the 'data/import' directory."); | ||
| } | ||
|
|
||
| File importDir = Environment.getHomePath().resolve("data").resolve("import").toFile(); | ||
|
|
||
| if (importDir.exists()) { | ||
| for (File realmFile : importDir.listFiles()) { | ||
| filesToImport.append(realmFile.getAbsolutePath()).append(","); | ||
| } | ||
| } | ||
|
|
||
| System.setProperty("keycloak.import", filesToImport.toString()); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.