diff --git a/core/src/main/java/org/keycloak/Version.java b/core/src/main/java/org/keycloak/Version.java index 005a4c20c4..a1efa649c2 100755 --- a/core/src/main/java/org/keycloak/Version.java +++ b/core/src/main/java/org/keycloak/Version.java @@ -14,7 +14,7 @@ public class Version { public static String VERSION; public static String BUILD_TIME; public static final String UNKNOWN = "UNKNOWN"; - public static final Version SINGLETON = new Version(); + public static final Version SINGLETON; private final String version = VERSION; private final String buildTime = BUILD_TIME; @@ -30,6 +30,8 @@ public class Version { VERSION=UNKNOWN; BUILD_TIME=UNKNOWN; } + + SINGLETON = new Version(); } @JsonProperty("version") diff --git a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/MultipleStepsExportProvider.java b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/MultipleStepsExportProvider.java index 2f66d09b42..c72708b362 100755 --- a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/MultipleStepsExportProvider.java +++ b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/MultipleStepsExportProvider.java @@ -1,6 +1,7 @@ package org.keycloak.exportimport.util; import org.jboss.logging.Logger; +import org.keycloak.Version; import org.keycloak.exportimport.ExportImportConfig; import org.keycloak.exportimport.ExportProvider; import org.keycloak.exportimport.UsersExportStrategy; @@ -37,12 +38,19 @@ public abstract class MultipleStepsExportProvider implements ExportProvider { }); for (RealmModel realm : holder.realms) { - exportRealm(factory, realm.getName()); + exportRealmImpl(factory, realm.getName()); } + + writeVersion("version.json", Version.SINGLETON); } @Override - public void exportRealm(KeycloakSessionFactory factory, final String realmName) throws IOException { + public void exportRealm(KeycloakSessionFactory factory, String realmName) throws IOException { + exportRealmImpl(factory, realmName); + writeVersion("version.json", Version.SINGLETON); + } + + protected void exportRealmImpl(KeycloakSessionFactory factory, final String realmName) throws IOException { final UsersExportStrategy usersExportStrategy = ExportImportConfig.getUsersExportStrategy(); final int usersPerFile = ExportImportConfig.getUsersPerFile(); final UsersHolder usersHolder = new UsersHolder(); @@ -102,6 +110,8 @@ public abstract class MultipleStepsExportProvider implements ExportProvider { protected abstract void writeUsers(String fileName, KeycloakSession session, RealmModel realm, List users) throws IOException; + protected abstract void writeVersion(String fileName, Version version) throws IOException; + public static class RealmsHolder { List realms; diff --git a/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirExportProvider.java b/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirExportProvider.java index ea6b7e1916..175629929f 100755 --- a/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirExportProvider.java +++ b/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirExportProvider.java @@ -1,5 +1,6 @@ package org.keycloak.exportimport.dir; +import org.keycloak.Version; import org.keycloak.exportimport.util.ExportUtils; import org.keycloak.exportimport.util.MultipleStepsExportProvider; import org.keycloak.models.KeycloakSession; @@ -68,6 +69,13 @@ public class DirExportProvider extends MultipleStepsExportProvider { ExportUtils.exportUsersToStream(session, realm, users, JsonSerialization.prettyMapper, os); } + @Override + protected void writeVersion(String fileName, Version version) throws IOException { + File file = new File(this.rootDirectory, fileName); + FileOutputStream stream = new FileOutputStream(file); + JsonSerialization.prettyMapper.writeValue(stream, version); + } + @Override public void close() { } diff --git a/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipExportProvider.java b/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipExportProvider.java index e9196e6912..3517788658 100755 --- a/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipExportProvider.java +++ b/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipExportProvider.java @@ -4,6 +4,7 @@ import de.idyl.winzipaes.AesZipFileEncrypter; import de.idyl.winzipaes.impl.AESEncrypter; import de.idyl.winzipaes.impl.AESEncrypterBC; import org.jboss.logging.Logger; +import org.keycloak.Version; import org.keycloak.exportimport.util.ExportUtils; import org.keycloak.exportimport.util.MultipleStepsExportProvider; import org.keycloak.models.KeycloakSession; @@ -47,17 +48,24 @@ public class ZipExportProvider extends MultipleStepsExportProvider { protected void writeRealm(String fileName, RealmRepresentation rep) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); JsonSerialization.mapper.writeValue(stream, rep); - - byte[] byteArray = stream.toByteArray(); - ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); - this.encrypter.add(fileName, bis, this.password); + writeStream(fileName, stream); } @Override protected void writeUsers(String fileName, KeycloakSession session, RealmModel realm, List users) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ExportUtils.exportUsersToStream(session, realm, users, JsonSerialization.mapper, stream); + writeStream(fileName, stream); + } + @Override + protected void writeVersion(String fileName, Version version) throws IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + JsonSerialization.mapper.writeValue(stream, version); + writeStream(fileName, stream); + } + + private void writeStream(String fileName, ByteArrayOutputStream stream) throws IOException { byte[] byteArray = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); this.encrypter.add(fileName, bis, this.password); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java index 6c6d05dc97..c4bf603c8b 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java @@ -147,8 +147,8 @@ public class ExportImportTest { testFullExportImport(); - // There should be 6 files in target directory (3 realm, 3 user) - Assert.assertEquals(6, new File(targetDirPath).listFiles().length); + // There should be 6 files in target directory (3 realm, 3 user, 1 version) + Assert.assertEquals(7, new File(targetDirPath).listFiles().length); } @Test @@ -161,8 +161,8 @@ public class ExportImportTest { testRealmExportImport(); - // There should be 3 files in target directory (1 realm, 2 user) - Assert.assertEquals(3, new File(targetDirPath).listFiles().length); + // There should be 3 files in target directory (1 realm, 2 user, 1 version) + Assert.assertEquals(4, new File(targetDirPath).listFiles().length); } @Test