diff --git a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ExportUtils.java b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ExportUtils.java index d36828093d..e6b71ba7f0 100755 --- a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ExportUtils.java +++ b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ExportUtils.java @@ -71,6 +71,7 @@ public class ExportUtils { List oauthClientReps = new ArrayList(); for (OAuthClientModel oauthClient : oauthClients) { OAuthClientRepresentation clientRep = ModelToRepresentation.toRepresentation(oauthClient); + clientRep.setSecret(oauthClient.getSecret()); oauthClientReps.add(clientRep); } rep.setOauthClients(oauthClientReps); diff --git a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java index 1425b361e2..755c34f6c0 100755 --- a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java +++ b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java @@ -182,7 +182,7 @@ public class ImportUtils { parser.nextToken(); String currRealmName = parser.getText(); if (!currRealmName.equals(realmName)) { - throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + realmName); + throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName); } } else if ("users".equals(parser.getText())) { parser.nextToken(); diff --git a/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java b/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java index 512004d381..1c9f484845 100644 --- a/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java +++ b/export-import/export-import-dir/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java @@ -5,9 +5,6 @@ import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; import java.util.List; import org.jboss.logging.Logger; @@ -86,7 +83,7 @@ public class DirImportProvider implements ImportProvider { @Override public boolean accept(File dir, String name) { - return (name.startsWith(realmName)) && (name.endsWith(".json")) && (name.substring(realmName.length()).contains("-users-") ); + return name.matches(realmName + "-users-[0-9]+\\.json"); } }); diff --git a/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipImportProvider.java b/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipImportProvider.java index 6970e87899..50651794cc 100644 --- a/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipImportProvider.java +++ b/export-import/export-import-zip/src/main/java/org/keycloak/exportimport/zip/ZipImportProvider.java @@ -94,7 +94,7 @@ public class ZipImportProvider implements ImportProvider { // Import users for (ExtZipEntry entry : this.decrypter.getEntryList()) { String name = entry.getName(); - if ( (name.startsWith(realmName)) && (name.endsWith(".json")) && (name.substring(realmName.length()).contains("-users-")) ) { + if (name.matches(realmName + "-users-[0-9]+\\.json")) { bos = new ByteArrayOutputStream(); this.decrypter.extractEntry(entry, bos, this.password); final ByteArrayInputStream bis2 = new ByteArrayInputStream(bos.toByteArray()); diff --git a/model/api/src/main/java/org/keycloak/models/utils/ModelToRepresentation.java b/model/api/src/main/java/org/keycloak/models/utils/ModelToRepresentation.java index 9e19b75ce6..7e183b7786 100755 --- a/model/api/src/main/java/org/keycloak/models/utils/ModelToRepresentation.java +++ b/model/api/src/main/java/org/keycloak/models/utils/ModelToRepresentation.java @@ -134,10 +134,7 @@ public class ModelToRepresentation { if (fedProviderModels.size() > 0) { List fedProviderReps = new ArrayList(); for (UserFederationProviderModel model : fedProviderModels) { - UserFederationProviderRepresentation fedProvRep = new UserFederationProviderRepresentation(); - fedProvRep.setId(model.getId()); - fedProvRep.setProviderName(model.getProviderName()); - fedProvRep.setConfig(model.getConfig()); + UserFederationProviderRepresentation fedProvRep = toRepresentation(model); fedProviderReps.add(fedProvRep); } rep.setUserFederationProviders(fedProviderReps); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java index ed7fa5cc43..ae8c33fc0f 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java @@ -297,6 +297,7 @@ public class MongoUserProvider implements UserProvider { @Override public void addSocialLink(RealmModel realm, UserModel user, SocialLinkModel socialLink) { + user = getUserById(user.getId(), realm); MongoUserEntity userEntity = ((UserAdapter) user).getUser(); SocialLinkEntity socialLinkEntity = new SocialLinkEntity(); socialLinkEntity.setSocialProvider(socialLink.getSocialProvider()); 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 2eaaaa5a37..0dc40ff675 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 @@ -1,8 +1,10 @@ package org.keycloak.testsuite.exportimport; import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -26,10 +28,14 @@ import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.RealmProvider; import org.keycloak.models.UserCredentialModel; +import org.keycloak.models.UserFederationProviderModel; import org.keycloak.models.UserModel; import org.keycloak.models.UserProvider; import org.keycloak.representations.idm.CredentialRepresentation; +import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.RealmManager; +import org.keycloak.testsuite.model.AbstractModelTest; +import org.keycloak.testsuite.model.ImportTest; import org.keycloak.testsuite.rule.KeycloakRule; /** @@ -92,14 +98,22 @@ public class ExportImportTest { @Override public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { + // Create some users in "test" and "master" realms addUser(manager.getSession().users(), appRealm, "user1", "password"); addUser(manager.getSession().users(), appRealm, "user2", "password"); addUser(manager.getSession().users(), appRealm, "user3", "password"); addUser(manager.getSession().users(), adminstrationRealm, "admin2", "admin2"); + + // Import "test-realm" realm + try { + RealmRepresentation rep = AbstractModelTest.loadJson("model/testrealm.json"); + RealmModel demoRealm = manager.createRealm("test-realm", rep.getRealm()); + manager.importRealm(rep, demoRealm); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } } - - }) { @Override protected void after() { @@ -137,8 +151,8 @@ public class ExportImportTest { testFullExportImport(); - // There should be 4 files in target directory (2 realm, 2 user) - Assert.assertEquals(4, new File(targetDirPath).listFiles().length); + // There should be 6 files in target directory (3 realm, 3 user) + Assert.assertEquals(6, new File(targetDirPath).listFiles().length); } @Test @@ -210,7 +224,7 @@ public class ExportImportTest { RealmProvider realmProvider = session.realms(); UserProvider userProvider = session.users(); new RealmManager(session).removeRealm(realmProvider.getRealmByName("test")); - Assert.assertEquals(1, realmProvider.getRealms().size()); + Assert.assertEquals(2, realmProvider.getRealms().size()); RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm()); UserModel admin2 = session.users().getUserByUsername("admin2", master); @@ -235,7 +249,7 @@ public class ExportImportTest { try { RealmProvider model = session.realms(); UserProvider userProvider = session.users(); - Assert.assertEquals(2, model.getRealms().size()); + Assert.assertEquals(3, model.getRealms().size()); assertAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2"); assertAuthenticated(userProvider, model, "test", "test-user@localhost", "password"); @@ -243,6 +257,8 @@ public class ExportImportTest { assertAuthenticated(userProvider, model, "test", "user2", "password"); assertAuthenticated(userProvider, model, "test", "user3", "password"); + RealmModel testRealmRealm = model.getRealm("test-realm"); + ImportTest.assertDataImportedInRealm(session, testRealmRealm); } finally { keycloakRule.stopSession(session, true); } @@ -261,7 +277,7 @@ public class ExportImportTest { RealmProvider realmProvider = session.realms(); UserProvider userProvider = session.users(); new RealmManager(session).removeRealm(realmProvider.getRealmByName("test")); - Assert.assertEquals(1, realmProvider.getRealms().size()); + Assert.assertEquals(2, realmProvider.getRealms().size()); RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm()); UserModel admin2 = session.users().getUserByUsername("admin2", master); @@ -287,7 +303,7 @@ public class ExportImportTest { try { RealmProvider realmProvider = session.realms(); UserProvider userProvider = session.users(); - Assert.assertEquals(2, realmProvider.getRealms().size()); + Assert.assertEquals(3, realmProvider.getRealms().size()); assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2"); assertAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password"); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java index 5c852203a6..96b60c684a 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java @@ -185,6 +185,7 @@ public class ImportTest extends AbstractModelTest { Assert.assertTrue(session.users().removeSocialLink(realm, socialUser, "facebook")); Assert.assertNull(session.users().getSocialLink(socialUser, "facebook", realm)); Assert.assertFalse(session.users().removeSocialLink(realm, socialUser, "facebook")); + session.users().addSocialLink(realm, socialUser, new SocialLinkModel("facebook", "facebook1", "fbuser1")); // Test smtp config Map smtpConfig = realm.getSmtpConfig();