Enforce that realm name is unique in model
This commit is contained in:
1 parent
6778fbfa3b
commit
364998b34b
21 files changed
+193
-67
No files matched your search
@@ -6,11 +6,15 @@ import java.io.InputStream;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.Config;
|
||||
import org.keycloak.provider.ProviderSession;
|
||||
import org.keycloak.provider.ProviderSessionFactory;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
@@ -24,23 +28,41 @@ import org.keycloak.util.JsonSerialization;
|
||||
*/
|
||||
public class AbstractModelTest {
|
||||
|
||||
protected KeycloakSessionFactory factory;
|
||||
protected static KeycloakSessionFactory factory;
|
||||
protected static ProviderSessionFactory providerSessionFactory;
|
||||
|
||||
protected KeycloakSession identitySession;
|
||||
protected RealmManager realmManager;
|
||||
protected ProviderSessionFactory providerSessionFactory;
|
||||
protected ProviderSession providerSession;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
factory = KeycloakApplication.createSessionFactory();
|
||||
providerSessionFactory = KeycloakApplication.createProviderSessionFactory();
|
||||
|
||||
KeycloakSession identitySession = factory.createSession();
|
||||
try {
|
||||
identitySession.getTransaction().begin();
|
||||
new ApplianceBootstrap().bootstrap(identitySession, "/auth");
|
||||
identitySession.getTransaction().commit();
|
||||
} finally {
|
||||
identitySession.close();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
providerSessionFactory.close();
|
||||
factory.close();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
factory = KeycloakApplication.createSessionFactory();
|
||||
identitySession = factory.createSession();
|
||||
identitySession.getTransaction().begin();
|
||||
realmManager = new RealmManager(identitySession);
|
||||
|
||||
providerSessionFactory = KeycloakApplication.createProviderSessionFactory();
|
||||
providerSession = providerSessionFactory.createSession();
|
||||
|
||||
new ApplianceBootstrap().bootstrap(identitySession, "/auth");
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -48,12 +70,35 @@ public class AbstractModelTest {
|
||||
identitySession.getTransaction().commit();
|
||||
providerSession.close();
|
||||
identitySession.close();
|
||||
providerSessionFactory.close();
|
||||
factory.close();
|
||||
|
||||
identitySession = factory.createSession();
|
||||
try {
|
||||
identitySession.getTransaction().begin();
|
||||
|
||||
RealmManager rm = new RealmManager(identitySession);
|
||||
for (RealmModel realm : identitySession.getRealms()) {
|
||||
if (!realm.getName().equals(Config.getAdminRealm())) {
|
||||
rm.removeRealm(realm);
|
||||
}
|
||||
}
|
||||
|
||||
identitySession.getTransaction().commit();
|
||||
} finally {
|
||||
identitySession.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void commit() {
|
||||
identitySession.getTransaction().commit();
|
||||
commit(false);
|
||||
}
|
||||
|
||||
protected void commit(boolean rollback) {
|
||||
if (rollback) {
|
||||
identitySession.getTransaction().rollback();
|
||||
} else {
|
||||
identitySession.getTransaction().commit();
|
||||
}
|
||||
identitySession.close();
|
||||
identitySession = factory.createSession();
|
||||
identitySession.getTransaction().begin();
|
||||
@@ -68,8 +113,6 @@ public class AbstractModelTest {
|
||||
os.write(c);
|
||||
}
|
||||
byte[] bytes = os.toByteArray();
|
||||
System.out.println(new String(bytes));
|
||||
|
||||
return JsonSerialization.readValue(bytes, RealmRepresentation.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.keycloak.models.ApplicationModel;
|
||||
import org.keycloak.models.Constants;
|
||||
import org.keycloak.models.ModelDuplicateException;
|
||||
import org.keycloak.models.OAuthClientModel;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RequiredCredentialModel;
|
||||
@@ -44,7 +45,6 @@ public class AdapterTest extends AbstractModelTest {
|
||||
realmModel.setUpdateProfileOnInitialSocialLogin(true);
|
||||
realmModel.addDefaultRole("foo");
|
||||
|
||||
System.out.println(realmModel.getId());
|
||||
realmModel = realmManager.getRealm(realmModel.getId());
|
||||
Assert.assertNotNull(realmModel);
|
||||
Assert.assertEquals(realmModel.getAccessCodeLifespan(), 100);
|
||||
@@ -302,7 +302,7 @@ public class AdapterTest extends AbstractModelTest {
|
||||
}
|
||||
String[] usernames = users.toArray(new String[users.size()]);
|
||||
Arrays.sort(usernames);
|
||||
Assert.assertArrayEquals(new String[] { "doublefirst", "doublelast"}, usernames);
|
||||
Assert.assertArrayEquals(new String[]{"doublefirst", "doublelast"}, usernames);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -472,5 +472,67 @@ public class AdapterTest extends AbstractModelTest {
|
||||
Assert.assertFalse(realmModel.hasRole(user, appBarRole));
|
||||
}
|
||||
|
||||
// TODO: test scopes
|
||||
@Test
|
||||
public void testScopes() throws Exception {
|
||||
test1CreateRealm();
|
||||
RoleModel realmRole = realmModel.addRole("realm");
|
||||
|
||||
ApplicationModel app1 = realmModel.addApplication("app1");
|
||||
RoleModel appRole = app1.addRole("app");
|
||||
|
||||
ApplicationModel app2 = realmModel.addApplication("app2");
|
||||
realmModel.addScopeMapping(app2, realmRole);
|
||||
realmModel.addScopeMapping(app2, appRole);
|
||||
|
||||
OAuthClientModel client = realmModel.addOAuthClient("client");
|
||||
realmModel.addScopeMapping(client, realmRole);
|
||||
realmModel.addScopeMapping(client, appRole);
|
||||
|
||||
commit();
|
||||
|
||||
realmModel = identitySession.getRealmByName("JUGGLER");
|
||||
app1 = realmModel.getApplicationByName("app1");
|
||||
app2 = realmModel.getApplicationByName("app2");
|
||||
client = realmModel.getOAuthClient("client");
|
||||
|
||||
Set<RoleModel> scopeMappings = realmModel.getScopeMappings(app2);
|
||||
Assert.assertEquals(2, scopeMappings.size());
|
||||
Assert.assertTrue(scopeMappings.contains(realmModel.getRole("realm")));
|
||||
Assert.assertTrue(scopeMappings.contains(app1.getRole("app")));
|
||||
|
||||
scopeMappings = realmModel.getScopeMappings(client);
|
||||
Assert.assertEquals(2, scopeMappings.size());
|
||||
Assert.assertTrue(scopeMappings.contains(realmModel.getRole("realm")));
|
||||
Assert.assertTrue(scopeMappings.contains(app1.getRole("app")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRealmNameCollisions() throws Exception {
|
||||
test1CreateRealm();
|
||||
|
||||
commit();
|
||||
|
||||
// Try to create realm with duplicate name
|
||||
try {
|
||||
test1CreateRealm();
|
||||
commit();
|
||||
Assert.fail("Expected exception");
|
||||
} catch (ModelDuplicateException e) {
|
||||
}
|
||||
commit(true);
|
||||
|
||||
// Ty to rename realm to duplicate name
|
||||
realmModel = realmManager.createRealm("JUGGLER2");
|
||||
commit();
|
||||
|
||||
realmModel = realmManager.getRealmByName("JUGGLER2");
|
||||
try {
|
||||
realmModel.setName("JUGGLER");
|
||||
commit();
|
||||
Assert.fail("Expected exception");
|
||||
} catch (ModelDuplicateException e) {
|
||||
}
|
||||
commit(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,8 +7,10 @@ import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
@@ -29,22 +31,38 @@ import org.keycloak.authentication.AuthenticationProviderManager;
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class AuthProvidersLDAPTest extends AbstractModelTest {
|
||||
|
||||
private static LDAPEmbeddedServer embeddedServer;
|
||||
|
||||
private RealmModel realm;
|
||||
private AuthenticationManager am;
|
||||
private LDAPEmbeddedServer embeddedServer;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void before() throws Exception {
|
||||
super.before();
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
AbstractModelTest.beforeClass();
|
||||
|
||||
try {
|
||||
this.embeddedServer = new LDAPEmbeddedServer();
|
||||
this.embeddedServer.setup();
|
||||
this.embeddedServer.importLDIF("ldap/users.ldif");
|
||||
embeddedServer = new LDAPEmbeddedServer();
|
||||
embeddedServer.setup();
|
||||
embeddedServer.importLDIF("ldap/users.ldif");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error starting Embedded LDAP server.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
AbstractModelTest.afterClass();
|
||||
|
||||
try {
|
||||
embeddedServer.tearDown();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error starting Embedded LDAP server.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
super.before();
|
||||
|
||||
// Create realm and configure ldap
|
||||
realm = realmManager.createRealm("realm");
|
||||
@@ -55,17 +73,6 @@ public class AuthProvidersLDAPTest extends AbstractModelTest {
|
||||
am = new AuthenticationManager(providerSession);
|
||||
}
|
||||
|
||||
@After
|
||||
@Override
|
||||
public void after() throws Exception {
|
||||
super.after();
|
||||
try {
|
||||
this.embeddedServer.tearDown();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error starting Embedded LDAP server.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLdapAuthentication() {
|
||||
MultivaluedMap<String, String> formData = AuthProvidersExternalModelTest.createFormData("john", "password");
|
||||
|
||||
@@ -70,6 +70,7 @@ public class ModelTest extends AbstractModelTest {
|
||||
|
||||
private RealmModel importExport(RealmModel src, String copyName) {
|
||||
RealmRepresentation representation = ModelToRepresentation.toRepresentation(src);
|
||||
representation.setRealm(copyName);
|
||||
RealmModel copy = realmManager.createRealm(copyName);
|
||||
realmManager.importRealm(representation, copy);
|
||||
return realmManager.getRealm(copy.getId());
|
||||
|
||||
@@ -22,8 +22,8 @@ public class MultipleRealmsTest extends AbstractModelTest {
|
||||
@Override
|
||||
public void before() throws Exception {
|
||||
super.before();
|
||||
realm1 = identitySession.createRealm("id1", "realm1");
|
||||
realm2 = identitySession.createRealm("id2", "realm2");
|
||||
realm1 = realmManager.createRealm("id1", "realm1");
|
||||
realm2 = realmManager.createRealm("id2", "realm2");
|
||||
|
||||
createObjects(realm1);
|
||||
createObjects(realm2);
|
||||
@@ -93,7 +93,7 @@ public class MultipleRealmsTest extends AbstractModelTest {
|
||||
realm.addRole("role2");
|
||||
|
||||
app1.addRole("app1Role1");
|
||||
app1.addScope(realm.getRole("role1"));
|
||||
realm.addScopeMapping(app1, realm.getRole("role1"));
|
||||
|
||||
realm.addOAuthClient("cl1");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user