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>Directory on local filesystem</listitem>
<listitem>Single JSON file on your filesystem</listitem> <listitem>Single JSON file on your filesystem</listitem>
</itemizedlist> </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>
<para> <para>
Encrypted ZIP is recommended as export contains many sensitive informations like passwords of your users (even if they are hashed), 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> </varlistentry>
</variablelist> </variablelist>
</para> </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> </chapter>

View file

@ -194,15 +194,19 @@ public class KeycloakApplication extends Application {
} }
public void importRealmFile() { public void importRealmFile() {
String file = System.getProperty("keycloak.import"); String files = System.getProperty("keycloak.import");
if (file != null) { if (files != null) {
RealmRepresentation rep = null; StringTokenizer tokenizer = new StringTokenizer(files, ",");
try { while (tokenizer.hasMoreTokens()) {
rep = loadJson(new FileInputStream(file), RealmRepresentation.class); String file = tokenizer.nextToken().trim();
} catch (FileNotFoundException e) { RealmRepresentation rep = null;
throw new RuntimeException(e); try {
rep = loadJson(new FileInputStream(file), RealmRepresentation.class);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
importRealm(rep, "file " + file);
} }
importRealm(rep, "file " + file);
} }
} }
@ -223,11 +227,14 @@ public class KeycloakApplication extends Application {
return; return;
} }
RealmModel realm = manager.importRealm(rep); 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);
session.getTransaction().commit(); } catch (Throwable t) {
session.getTransaction().rollback();
log.warn("Unable to import realm " + rep.getRealm() + " from " + from + ". Cause: " + t.getMessage());
}
} finally { } finally {
session.close(); session.close();
} }