From e521fa355426df9c12979227d6d5b04f56c8098e Mon Sep 17 00:00:00 2001 From: mposolda Date: Thu, 23 Jul 2015 21:17:17 +0200 Subject: [PATCH 1/2] Simplify embedded server bootstrap in LDAP and Kerberos examples --- examples/kerberos/README.md | 14 +- ...users.ldif => kerberos-example-users.ldif} | 0 examples/kerberos/pom.xml | 29 ++++ examples/ldap/README.md | 12 +- examples/ldap/embedded-ldap/assembly.xml | 21 --- examples/ldap/embedded-ldap/pom.xml | 77 ----------- .../ldap/embedded/EmbeddedLDAPLauncher.java | 129 ------------------ examples/ldap/ldap-app/pom.xml | 58 -------- .../users.ldif => ldap-example-users.ldif} | 1 - examples/ldap/{ldap-app => }/ldaprealm.json | 0 examples/ldap/pom.xml | 87 ++++++++++-- .../src/main/webapp/WEB-INF/keycloak.json | 0 .../src/main/webapp/WEB-INF/web.xml | 0 .../{ldap-app => }/src/main/webapp/index.jsp | 0 util/embedded-ldap/pom.xml | 9 +- 15 files changed, 120 insertions(+), 317 deletions(-) rename examples/kerberos/{users.ldif => kerberos-example-users.ldif} (100%) delete mode 100644 examples/ldap/embedded-ldap/assembly.xml delete mode 100644 examples/ldap/embedded-ldap/pom.xml delete mode 100644 examples/ldap/embedded-ldap/src/main/java/org/keycloak/example/ldap/embedded/EmbeddedLDAPLauncher.java delete mode 100644 examples/ldap/ldap-app/pom.xml rename examples/ldap/{ldap-app/users.ldif => ldap-example-users.ldif} (99%) rename examples/ldap/{ldap-app => }/ldaprealm.json (100%) rename examples/ldap/{ldap-app => }/src/main/webapp/WEB-INF/keycloak.json (100%) rename examples/ldap/{ldap-app => }/src/main/webapp/WEB-INF/web.xml (100%) rename examples/ldap/{ldap-app => }/src/main/webapp/index.jsp (100%) diff --git a/examples/kerberos/README.md b/examples/kerberos/README.md index c65316c4bf..7b95993ef8 100644 --- a/examples/kerberos/README.md +++ b/examples/kerberos/README.md @@ -43,21 +43,13 @@ is in your `/etc/hosts` before other records for the 127.0.0.1 host to avoid iss for credential delegation example, as application needs to forward Kerberos ticket and authenticate with it against LDAP server. See [this file](https://github.com/keycloak/keycloak/blob/master/testsuite/integration/src/test/resources/kerberos/test-krb5.conf) for inspiration. -**6)** Run ApacheDS based Kerberos server . The [LDAP Example](../ldap) contains the embedded server, which you can run for example -with these commands (assuming you're in `kerberos` directory with this example) +**6)** Run ApacheDS based LDAP server. You can run the command like this (assuming you're in the "kerberos" directory with this example): ``` -cd ../ldap -mvn clean install -cd .. -java -jar ldap/embedded-ldap/target/embedded-ldap.jar kerberos +mvn exec:java -Pkerberos ``` -This will also automatically import the LDIF from `users.ldif` of kerberos example into the LDAP server. If you want to import your own LDIF file, -you can add the system property `ldap.ldif` with the path of the LDIF file to the command. For example: -``` -java -jar -Dldap.ldif=/tmp/my-users.ldif ldap/embedded-ldap/target/embedded-ldap.jar kerberos -``` +This will also automatically import the LDIF from `kerberos-example-users.ldif` of kerberos example into the LDAP server. Replace with your own LDIF file if you want different users. A bit more details about embedded Kerberos server in [testsuite README](https://github.com/keycloak/keycloak/blob/master/misc/Testsuite.md#kerberos-server). diff --git a/examples/kerberos/users.ldif b/examples/kerberos/kerberos-example-users.ldif similarity index 100% rename from examples/kerberos/users.ldif rename to examples/kerberos/kerberos-example-users.ldif diff --git a/examples/kerberos/pom.xml b/examples/kerberos/pom.xml index ffe8259a11..69429f51c6 100755 --- a/examples/kerberos/pom.xml +++ b/examples/kerberos/pom.xml @@ -40,6 +40,11 @@ keycloak-adapter-core provided + + org.keycloak + keycloak-util-embedded-ldap + test + @@ -62,4 +67,28 @@ + + + kerberos + + + + org.codehaus.mojo + exec-maven-plugin + + org.keycloak.util.ldap.KerberosEmbeddedServer + test + + + ldap.ldif + kerberos-example-users.ldif + + + + + + + + + diff --git a/examples/ldap/README.md b/examples/ldap/README.md index ab3c978efc..4ad1d5d70b 100644 --- a/examples/ldap/README.md +++ b/examples/ldap/README.md @@ -13,24 +13,20 @@ The example application is then showing all the basic claims of current user tog Detailed steps how to make the example working: -**1)** Build and deploy this sample's WAR file in `ldap-app/target/ldap-portal.war` . For this example, deploy on the same server that is running the Keycloak Server, +**1)** Build and deploy this sample's WAR file in `target/ldap-portal.war` . For this example, deploy on the same server that is running the Keycloak Server, although this is not required for real world scenarios. **2)** Run ApacheDS based LDAP server. You can run the command like this (assuming you're in the "ldap" directory with this example): ``` -java -jar embedded-ldap/target/embedded-ldap.jar ldap +mvn exec:java -Pldap ``` -This will also automatically import the LDIF from `ldap-app/users.ldif` into the LDAP server. If you want to import your own LDIF file, -you can add the system property `ldap.ldif` with the path of the LDIF file to the command. For example: -``` -java -jar -Dldap.ldif=/tmp/my-users.ldif embedded-ldap/target/embedded-ldap.jar ldap -``` +This will also automatically import the LDIF from `ldap-example-users.ldif` into the LDAP server. Replace with your own LDIF file if you want different users. -**3)** Run Keycloak server and import `ldap-app/ldaprealm.json` into it through admin console. This contains the realm with preconfigured LDAP federation provider and LDAP mappers +**3)** Run Keycloak server and import `ldaprealm.json` into it through admin console. This contains the realm with preconfigured LDAP federation provider and LDAP mappers and protocol mappers. Note that there are not any roles or users in this file. All of users, roles and role mappings data will be imported automatically from LDAP. diff --git a/examples/ldap/embedded-ldap/assembly.xml b/examples/ldap/embedded-ldap/assembly.xml deleted file mode 100644 index 58afeca05b..0000000000 --- a/examples/ldap/embedded-ldap/assembly.xml +++ /dev/null @@ -1,21 +0,0 @@ - - embedded-ldap - - - dir - - - false - - - - false - true - true - - org.keycloak:keycloak-util-embedded-ldap - org.slf4j:slf4j-log4j12 - - - - \ No newline at end of file diff --git a/examples/ldap/embedded-ldap/pom.xml b/examples/ldap/embedded-ldap/pom.xml deleted file mode 100644 index 44f68e1082..0000000000 --- a/examples/ldap/embedded-ldap/pom.xml +++ /dev/null @@ -1,77 +0,0 @@ - - - - keycloak-examples-ldap-parent - org.keycloak - 1.4.0.Final-SNAPSHOT - - - 4.0.0 - org.keycloak.example.demo - keycloak-examples-embedded-ldap - jar - LDAP Demo Application - - - - org.keycloak - keycloak-util-embedded-ldap - - - - - embedded-ldap - - - maven-assembly-plugin - - - assemble - package - - single - - - - assembly.xml - - - target - - - target/assembly/work - - false - - - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - org.keycloak.example.ldap.embedded.EmbeddedLDAPLauncher - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - - - \ No newline at end of file diff --git a/examples/ldap/embedded-ldap/src/main/java/org/keycloak/example/ldap/embedded/EmbeddedLDAPLauncher.java b/examples/ldap/embedded-ldap/src/main/java/org/keycloak/example/ldap/embedded/EmbeddedLDAPLauncher.java deleted file mode 100644 index 191d63315a..0000000000 --- a/examples/ldap/embedded-ldap/src/main/java/org/keycloak/example/ldap/embedded/EmbeddedLDAPLauncher.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.keycloak.example.ldap.embedded; - -import java.io.File; -import java.lang.reflect.Method; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Allows to run embedded ApacheDS LDAP or Kerberos server - * - * It is supposed to be executed from JAR file. For example: - * java -jar target/embedded-ldap.jar ldap - * - * @author Marek Posolda - */ -public class EmbeddedLDAPLauncher { - - public static void main(String[] args) throws Exception { - String arg = args.length == 0 ? null : args[0]; - if (arg == null) { - System.err.println("Missing argument: either 'kerberos', 'ldap' or 'keytabCreator' must be passed as argument"); - System.exit(1); - } - - String clazz = null; - File home = getHome(); - Properties defaultProperties = new Properties(); - if (arg.equalsIgnoreCase("ldap")) { - - clazz = "org.keycloak.util.ldap.LDAPEmbeddedServer"; - File ldapLdif = file(home, "..", "ldap-app", "users.ldif"); - defaultProperties.put("ldap.ldif", ldapLdif.getAbsolutePath()); - } else if (arg.equalsIgnoreCase("kerberos")) { - - clazz = "org.keycloak.util.ldap.KerberosEmbeddedServer"; - File kerberosLdif = file(home, "..", "..", "kerberos", "users.ldif"); - defaultProperties.put("ldap.ldif", kerberosLdif.getAbsolutePath()); - } else if (arg.equalsIgnoreCase("keytabCreator")) { - - clazz = "org.keycloak.util.ldap.KerberosKeytabCreator"; - } else { - - System.err.println("Invalid argument: '" + arg + "' . Either 'kerberos', 'ldap' or 'keytabCreator' must be passed as argument"); - System.exit(1); - } - - // Remove first argument - String[] newArgs = new String[args.length - 1]; - for (int i=0 ; i<(args.length - 1) ; i++) { - newArgs[i] = args[i + 1]; - } - - System.out.println("Executing " + clazz); - runClass(clazz, newArgs, defaultProperties); - } - - - private static void runClass(String className, String[] args, Properties defaultProperties) throws Exception { - File home = getHome(); - File lib = file(home, "target", "embedded-ldap"); - - if (!lib.exists()) { - System.err.println("Could not find lib directory: " + lib.toString()); - System.exit(1); - } else { - System.out.println("Found directory to load jars: " + lib.getAbsolutePath()); - } - - List jars = new ArrayList(); - for (File file : lib.listFiles()) { - jars.add(file.toURI().toURL()); - } - URL[] urls = jars.toArray(new URL[jars.size()]); - URLClassLoader loader = new URLClassLoader(urls, EmbeddedLDAPLauncher.class.getClassLoader()); - - Class mainClass = loader.loadClass(className); - Method executeMethod = null; - for (Method m : mainClass.getMethods()) if (m.getName().equals("execute")) { executeMethod = m; break; } - Object obj = args; - executeMethod.invoke(null, obj, defaultProperties); - } - - - private static File getHome() { - String launcherPath = EmbeddedLDAPLauncher.class.getName().replace('.', '/') + ".class"; - URL jarfile = EmbeddedLDAPLauncher.class.getClassLoader().getResource(launcherPath); - if (jarfile != null) { - Matcher m = Pattern.compile("jar:(file:.*)!/" + launcherPath).matcher(jarfile.toString()); - if (m.matches()) { - try { - File jarPath = new File(new URI(m.group(1))); - File libPath = jarPath.getParentFile().getParentFile(); - System.out.println("Home directory: " + libPath.toString()); - if (!libPath.exists()) { - System.exit(1); - - } - return libPath; - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - } - } else { - System.err.println("jar file null: " + launcherPath); - } - return null; - } - - private static File file(File home, String... pathItems) { - File current = home; - - for (String item : pathItems) { - if (item.equals("..")) { - current = current.getParentFile(); - } else { - current = new File(current, item); - } - } - return current; - } -} diff --git a/examples/ldap/ldap-app/pom.xml b/examples/ldap/ldap-app/pom.xml deleted file mode 100644 index d7c79f46a8..0000000000 --- a/examples/ldap/ldap-app/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - keycloak-examples-ldap-parent - org.keycloak - 1.4.0.Final-SNAPSHOT - - - 4.0.0 - org.keycloak.example.demo - keycloak-examples-ldap-app - war - LDAP Demo Application - - - - jboss - jboss repo - http://repository.jboss.org/nexus/content/groups/public/ - - - - - - org.jboss.spec.javax.servlet - jboss-servlet-api_3.0_spec - provided - - - org.keycloak - keycloak-core - provided - - - - - ldap-portal - - - org.jboss.as.plugins - jboss-as-maven-plugin - - false - - - - org.wildfly.plugins - wildfly-maven-plugin - - false - - - - - - \ No newline at end of file diff --git a/examples/ldap/ldap-app/users.ldif b/examples/ldap/ldap-example-users.ldif similarity index 99% rename from examples/ldap/ldap-app/users.ldif rename to examples/ldap/ldap-example-users.ldif index 520d477e1a..d503255d9c 100644 --- a/examples/ldap/ldap-app/users.ldif +++ b/examples/ldap/ldap-example-users.ldif @@ -64,4 +64,3 @@ objectclass: top objectclass: groupOfNames cn: accountant member: uid=bwilson,ou=People,dc=keycloak,dc=org - diff --git a/examples/ldap/ldap-app/ldaprealm.json b/examples/ldap/ldaprealm.json similarity index 100% rename from examples/ldap/ldap-app/ldaprealm.json rename to examples/ldap/ldaprealm.json diff --git a/examples/ldap/pom.xml b/examples/ldap/pom.xml index d506efc1cc..80814305b7 100644 --- a/examples/ldap/pom.xml +++ b/examples/ldap/pom.xml @@ -1,20 +1,87 @@ - + + keycloak-examples-parent org.keycloak 1.4.0.Final-SNAPSHOT - Keycloak LDAP Examples - Parent - + 4.0.0 + org.keycloak.example.demo + keycloak-examples-ldap + war + LDAP Demo Application - keycloak-examples-ldap-parent - pom + + + jboss + jboss repo + http://repository.jboss.org/nexus/content/groups/public/ + + - - embedded-ldap - ldap-app - + + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.0_spec + provided + + + org.keycloak + keycloak-core + provided + + + org.keycloak + keycloak-util-embedded-ldap + test + + + + + ldap-portal + + + org.jboss.as.plugins + jboss-as-maven-plugin + + false + + + + org.wildfly.plugins + wildfly-maven-plugin + + false + + + + + + + + ldap + + + + org.codehaus.mojo + exec-maven-plugin + + org.keycloak.util.ldap.LDAPEmbeddedServer + test + + + ldap.ldif + ldap-example-users.ldif + + + + + + + + \ No newline at end of file diff --git a/examples/ldap/ldap-app/src/main/webapp/WEB-INF/keycloak.json b/examples/ldap/src/main/webapp/WEB-INF/keycloak.json similarity index 100% rename from examples/ldap/ldap-app/src/main/webapp/WEB-INF/keycloak.json rename to examples/ldap/src/main/webapp/WEB-INF/keycloak.json diff --git a/examples/ldap/ldap-app/src/main/webapp/WEB-INF/web.xml b/examples/ldap/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from examples/ldap/ldap-app/src/main/webapp/WEB-INF/web.xml rename to examples/ldap/src/main/webapp/WEB-INF/web.xml diff --git a/examples/ldap/ldap-app/src/main/webapp/index.jsp b/examples/ldap/src/main/webapp/index.jsp similarity index 100% rename from examples/ldap/ldap-app/src/main/webapp/index.jsp rename to examples/ldap/src/main/webapp/index.jsp diff --git a/util/embedded-ldap/pom.xml b/util/embedded-ldap/pom.xml index 6eeeece763..5a1e927309 100644 --- a/util/embedded-ldap/pom.xml +++ b/util/embedded-ldap/pom.xml @@ -28,8 +28,13 @@ log4j - org.jboss.logging - jboss-logging + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + compile From 5f0f92fd154436735214893c73ed14396861b821 Mon Sep 17 00:00:00 2001 From: mposolda Date: Thu, 23 Jul 2015 21:39:40 +0200 Subject: [PATCH 2/2] Fix test to pass with MSAD --- .../federation/FederationProvidersIntegrationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/FederationProvidersIntegrationTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/FederationProvidersIntegrationTest.java index 533b43f13b..2caed441de 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/FederationProvidersIntegrationTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/FederationProvidersIntegrationTest.java @@ -56,7 +56,7 @@ public class FederationProvidersIntegrationTest { @Override public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - FederationTestUtils.addLocalUser(manager.getSession(), appRealm, "mary", "mary@test.com", "password-app"); + FederationTestUtils.addLocalUser(manager.getSession(), appRealm, "marykeycloak", "mary@test.com", "password-app"); Map ldapConfig = ldapRule.getConfig(); ldapConfig.put(LDAPConstants.SYNC_REGISTRATIONS, "true"); @@ -225,7 +225,7 @@ public class FederationProvidersIntegrationTest { @Test public void loginClassic() { loginPage.open(); - loginPage.login("mary", "password-app"); + loginPage.login("marykeycloak", "password-app"); Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertNotNull(oauth.getCurrentQuery().get(OAuth2Constants.CODE)); @@ -488,7 +488,7 @@ public class FederationProvidersIntegrationTest { @Override public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { LDAPFederationProvider ldapFedProvider = FederationTestUtils.getLdapProvider(session, ldapModel); - FederationTestUtils.addLDAPUser(ldapFedProvider, appRealm, "mary", "Mary1", "Kelly1", "mary1@email.org", null, "123"); + FederationTestUtils.addLDAPUser(ldapFedProvider, appRealm, "marykeycloak", "Mary1", "Kelly1", "mary1@email.org", null, "123"); FederationTestUtils.addLDAPUser(ldapFedProvider, appRealm, "mary-duplicatemail", "Mary2", "Kelly2", "mary@test.com", null, "123"); }