From f6a2c2331412b6826ccdc930b5977be4e088ac14 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 9 Mar 2015 15:23:54 +0100 Subject: [PATCH] Tweaks to file provider --- .../models/utils/RepresentationToModel.java | 1 - .../models/file/adapter/RealmAdapter.java | 16 ++++++---- .../testsuite/broker/BrokerKeyCloakRule.java | 6 +--- .../broker/IdentityProviderHintTest.java | 5 +-- .../OIDCKeyCloakServerBrokerBasicTest.java | 5 +-- .../SAMLKeyCloakServerBrokerBasicTest.java | 5 +-- ...KeyCloakServerBrokerWithSignatureTest.java | 5 +-- .../keycloak/testsuite/model/AdapterTest.java | 32 +++++++++++++------ .../testsuite/rule/AbstractKeycloakRule.java | 10 ++++++ 9 files changed, 48 insertions(+), 37 deletions(-) diff --git a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java index 5265aefbc4..90b00dd196 100755 --- a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java +++ b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java @@ -727,7 +727,6 @@ public class RepresentationToModel { hashedCred.setHashIterations(cred.getHashIterations()); try { if (cred.getSalt() != null) hashedCred.setSalt(Base64.decode(cred.getSalt())); -// hashedCred.setSalt(Base64.decode(cred.getSalt())); } catch (IOException ioe) { throw new RuntimeException(ioe); } diff --git a/model/file/src/main/java/org/keycloak/models/file/adapter/RealmAdapter.java b/model/file/src/main/java/org/keycloak/models/file/adapter/RealmAdapter.java index eab6ecb48b..c2cfab83f6 100755 --- a/model/file/src/main/java/org/keycloak/models/file/adapter/RealmAdapter.java +++ b/model/file/src/main/java/org/keycloak/models/file/adapter/RealmAdapter.java @@ -1028,13 +1028,17 @@ public class RealmAdapter implements RealmModel { @Override public void setMasterAdminApp(ApplicationModel app) { - if (app == null) throw new NullPointerException("app == null"); - String appId = app.getId(); - if (appId == null) { - throw new IllegalStateException("Master Admin app not initialized."); + if (app == null) { + realm.setAdminAppId(null); + this.masterAdminApp = null; + } else { + String appId = app.getId(); + if (appId == null) { + throw new IllegalStateException("Master Admin app not initialized."); + } + realm.setAdminAppId(appId); + this.masterAdminApp = app; } - realm.setAdminAppId(appId); - this.masterAdminApp = app; } @Override diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/BrokerKeyCloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/BrokerKeyCloakRule.java index 32d1f84b92..deb261143f 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/BrokerKeyCloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/BrokerKeyCloakRule.java @@ -24,8 +24,6 @@ import org.keycloak.testsuite.broker.util.UserSessionStatusServlet; import org.keycloak.testsuite.rule.AbstractKeycloakRule; import java.net.URL; -import org.keycloak.representations.idm.RealmRepresentation; -import org.keycloak.testutils.KeycloakServer; /** * @author pedroigor @@ -34,9 +32,7 @@ public class BrokerKeyCloakRule extends AbstractKeycloakRule { @Override protected void configure(KeycloakSession session, RealmManager manager, RealmModel adminRealm) { - //server.importRealm(getClass().getResourceAsStream("/broker-test/test-realm-with-broker.json")); - RealmRepresentation realmWithBroker = KeycloakServer.loadJson(getClass().getResourceAsStream("/broker-test/test-realm-with-broker.json"), RealmRepresentation.class); - manager.importRealm(realmWithBroker); + server.importRealm(getClass().getResourceAsStream("/broker-test/test-realm-with-broker.json")); URL url = getClass().getResource("/broker-test/test-app-keycloak.json"); deployApplication("test-app", "/test-app", UserSessionStatusServlet.class, url.getPath(), "manager"); deployApplication("test-app-allowed-providers", "/test-app-allowed-providers", UserSessionStatusServlet.class, url.getPath(), "manager"); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/IdentityProviderHintTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/IdentityProviderHintTest.java index d9c67307ce..b46617eac4 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/IdentityProviderHintTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/IdentityProviderHintTest.java @@ -17,7 +17,6 @@ import org.openqa.selenium.WebDriver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.keycloak.representations.idm.RealmRepresentation; /** * @author pedroigor @@ -37,9 +36,7 @@ public class IdentityProviderHintTest { @Override protected void configure(KeycloakSession session, RealmManager manager, RealmModel adminRealm) { - //server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json")); - RealmRepresentation realmWithOIDC = KeycloakServer.loadJson(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json"), RealmRepresentation.class); - manager.importRealm(realmWithOIDC); + server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json")); } }; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/OIDCKeyCloakServerBrokerBasicTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/OIDCKeyCloakServerBrokerBasicTest.java index 9b84345e25..66e4b7183d 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/OIDCKeyCloakServerBrokerBasicTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/OIDCKeyCloakServerBrokerBasicTest.java @@ -15,7 +15,6 @@ import java.io.IOException; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; -import org.keycloak.representations.idm.RealmRepresentation; /** * @author pedroigor @@ -32,9 +31,7 @@ public class OIDCKeyCloakServerBrokerBasicTest extends AbstractIdentityProviderT @Override protected void configure(KeycloakSession session, RealmManager manager, RealmModel adminRealm) { - //server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json")); - RealmRepresentation realmWithOIDC = KeycloakServer.loadJson(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json"), RealmRepresentation.class); - manager.importRealm(realmWithOIDC); + server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-kc-oidc.json")); } }; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerBasicTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerBasicTest.java index 50f7b9885e..77836655bd 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerBasicTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerBasicTest.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; -import org.keycloak.representations.idm.RealmRepresentation; /** * @author pedroigor @@ -36,9 +35,7 @@ public class SAMLKeyCloakServerBrokerBasicTest extends AbstractIdentityProviderT @Override protected void configure(KeycloakSession session, RealmManager manager, RealmModel adminRealm) { - //server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml.json")); - RealmRepresentation realmWithSAML = KeycloakServer.loadJson(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml.json"), RealmRepresentation.class); - manager.importRealm(realmWithSAML); + server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml.json")); } }; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerWithSignatureTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerWithSignatureTest.java index d1886f9e9c..e387097316 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerWithSignatureTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/broker/SAMLKeyCloakServerBrokerWithSignatureTest.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; -import org.keycloak.representations.idm.RealmRepresentation; /** * @author pedroigor @@ -36,9 +35,7 @@ public class SAMLKeyCloakServerBrokerWithSignatureTest extends AbstractIdentityP @Override protected void configure(KeycloakSession session, RealmManager manager, RealmModel adminRealm) { - //server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml-with-signature.json")); - RealmRepresentation realmWithOIDC = KeycloakServer.loadJson(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml-with-signature.json"), RealmRepresentation.class); - manager.importRealm(realmWithOIDC); + server.importRealm(getClass().getResourceAsStream("/broker-test/test-broker-realm-with-saml-with-signature.json")); } }; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java index cf5dfbc895..a2ebb91f5d 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java @@ -20,6 +20,9 @@ import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.services.managers.RealmManager; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -44,11 +47,17 @@ public class AdapterTest extends AbstractModelTest { realmModel.setAccessCodeLifespanUserAction(600); realmModel.setEnabled(true); realmModel.setName("JUGGLER"); - // realmModel.setPrivateKeyPem("0234234"); - // realmModel.setPublicKeyPem("0234234"); + + KeyPair keyPair = generateKeypair(); + + realmModel.setPrivateKey(keyPair.getPrivate()); + realmModel.setPublicKey(keyPair.getPublic()); realmModel.setAccessTokenLifespan(1000); realmModel.addDefaultRole("foo"); + session.getTransaction().commit(); + resetSession(); + realmModel = realmManager.getRealm(realmModel.getId()); assertNotNull(realmModel); Assert.assertEquals(realmModel.getAccessCodeLifespan(), 100); @@ -56,8 +65,8 @@ public class AdapterTest extends AbstractModelTest { Assert.assertEquals(realmModel.getAccessTokenLifespan(), 1000); Assert.assertEquals(realmModel.isEnabled(), true); Assert.assertEquals(realmModel.getName(), "JUGGLER"); - // Assert.assertEquals(realmModel.getPrivateKeyPem(), "0234234"); - // Assert.assertEquals(realmModel.getPublicKeyPem(), "0234234"); + Assert.assertArrayEquals(realmModel.getPrivateKey().getEncoded(), keyPair.getPrivate().getEncoded()); + Assert.assertArrayEquals(realmModel.getPublicKey().getEncoded(), keyPair.getPublic().getEncoded()); Assert.assertEquals(1, realmModel.getDefaultRoles().size()); Assert.assertEquals("foo", realmModel.getDefaultRoles().get(0)); } @@ -69,8 +78,9 @@ public class AdapterTest extends AbstractModelTest { realmModel.setAccessCodeLifespanUserAction(600); realmModel.setEnabled(true); realmModel.setName("JUGGLER"); - // realmModel.setPrivateKeyPem("0234234"); - // realmModel.setPublicKeyPem("0234234"); + KeyPair keyPair = generateKeypair(); + realmModel.setPrivateKey(keyPair.getPrivate()); + realmModel.setPublicKey(keyPair.getPublic()); realmModel.setAccessTokenLifespan(1000); realmModel.addDefaultRole("foo"); @@ -81,8 +91,8 @@ public class AdapterTest extends AbstractModelTest { Assert.assertEquals(realmModel.getAccessTokenLifespan(), 1000); Assert.assertEquals(realmModel.isEnabled(), true); Assert.assertEquals(realmModel.getName(), "JUGGLER"); - // Assert.assertEquals(realmModel.getPrivateKeyPem(), "0234234"); - // Assert.assertEquals(realmModel.getPublicKeyPem(), "0234234"); + Assert.assertArrayEquals(realmModel.getPrivateKey().getEncoded(), keyPair.getPrivate().getEncoded()); + Assert.assertArrayEquals(realmModel.getPublicKey().getEncoded(), keyPair.getPublic().getEncoded()); Assert.assertEquals(1, realmModel.getDefaultRoles().size()); Assert.assertEquals("foo", realmModel.getDefaultRoles().get(0)); @@ -90,7 +100,7 @@ public class AdapterTest extends AbstractModelTest { commit(); List realms = model.getRealms(); - // Assert.assertEquals(realms.size(), 2); + Assert.assertEquals(realms.size(), 2); } @@ -738,4 +748,8 @@ public class AdapterTest extends AbstractModelTest { resetSession(); } + private KeyPair generateKeypair() throws NoSuchAlgorithmException { + return KeyPairGenerator.getInstance("RSA").generateKeyPair(); + } + } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java index d1e37b3e17..fbd47bc383 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java @@ -8,6 +8,7 @@ import io.undertow.servlet.api.ServletInfo; import io.undertow.servlet.api.WebResourceCollection; import org.jboss.resteasy.spi.ResteasyDeployment; import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; import org.keycloak.Config; import org.keycloak.adapters.KeycloakConfigResolver; import org.keycloak.models.KeycloakSession; @@ -37,9 +38,15 @@ import java.util.Map; */ public abstract class AbstractKeycloakRule extends ExternalResource { + protected TemporaryFolder temporaryFolder; + protected KeycloakServer server; protected void before() throws Throwable { + temporaryFolder = new TemporaryFolder(); + temporaryFolder.create(); + System.setProperty("keycloak.tmp.dir", temporaryFolder.newFolder().getAbsolutePath()); + server = new KeycloakServer(); configureServer(server); @@ -210,6 +217,9 @@ public abstract class AbstractKeycloakRule extends ExternalResource { removeTestRealms(); stopServer(); Time.setOffset(0); + + temporaryFolder.delete(); + System.getProperties().remove("keycloak.tmp.dir"); } protected void removeTestRealms() {