Merge pull request #841 from jpkrohling/JPK-KEYCLOAK-821

KEYCLOAK-821 - Support for comma-separated list of file names to import.
This commit is contained in:
Marek Posolda 2014-11-06 20:32:31 +01:00
commit c0f377c8c7
2 changed files with 36 additions and 13 deletions

View file

@ -12,6 +12,13 @@
<listitem>Directory on local filesystem</listitem>
<listitem>Single JSON file on your filesystem</listitem>
</itemizedlist>
When importing using the "dir" or "zip" strategies, note that the files need to follow the naming convention specified below.
If you are importing files which were previously exported, the files already follow this convention.
<itemizedlist>
<listitem>{REALM_NAME}-realm.json, such as "acme-roadrunner-affairs-realm.json" for the realm named "acme-roadrunner-affairs"</listitem>
<listitem>{REALM_NAME}-users-{INDEX}.json, such as "acme-roadrunner-affairs-users-0.json" for the first users file of the realm named "acme-roadrunner-affairs"</listitem>
</itemizedlist>
</para>
<para>
Encrypted ZIP is recommended as export contains many sensitive informations like passwords of your users (even if they are hashed),
@ -111,5 +118,14 @@ bin/standalone.sh -Dkeycloak.migration.action=import
</varlistentry>
</variablelist>
</para>
<para>
When importing realm files that weren't exported before, the option <literal>keycloak.import</literal> can be used. If more than one realm
file needs to be imported, a comma separated list of file names can be specified. This is more appropriate than the cases before, as this
will happen only after the master realm has been initialized. Examples:
<itemizedlist>
<listitem>-Dkeycloak.import=/tmp/realm1.json</listitem>
<listitem>-Dkeycloak.import=/tmp/realm1.json,/tmp/realm2.json</listitem>
</itemizedlist>
</para>
</chapter>

View file

@ -194,8 +194,11 @@ public class KeycloakApplication extends Application {
}
public void importRealmFile() {
String file = System.getProperty("keycloak.import");
if (file != null) {
String files = System.getProperty("keycloak.import");
if (files != null) {
StringTokenizer tokenizer = new StringTokenizer(files, ",");
while (tokenizer.hasMoreTokens()) {
String file = tokenizer.nextToken().trim();
RealmRepresentation rep = null;
try {
rep = loadJson(new FileInputStream(file), RealmRepresentation.class);
@ -205,6 +208,7 @@ public class KeycloakApplication extends Application {
importRealm(rep, "file " + file);
}
}
}
public void importRealm(RealmRepresentation rep, String from) {
KeycloakSession session = sessionFactory.create();
@ -223,11 +227,14 @@ public class KeycloakApplication extends Application {
return;
}
try {
RealmModel realm = manager.importRealm(rep);
log.info("Imported realm " + realm.getName() + " from " + from);
session.getTransaction().commit();
log.info("Imported realm " + realm.getName() + " from " + from);
} catch (Throwable t) {
session.getTransaction().rollback();
log.warn("Unable to import realm " + rep.getRealm() + " from " + from + ". Cause: " + t.getMessage());
}
} finally {
session.close();
}