Skip to content

Commit

Permalink
Merge pull request #9 from andresrc/JENKINS-8152
Browse files Browse the repository at this point in the history
[JENKINS-8152] Encode rootDN in provider URL.
  • Loading branch information
andresrc committed Sep 20, 2016
2 parents 5156fed + 5065f52 commit b0b8622
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/main/java/hudson/security/LDAPSecurityRealm.java
Expand Up @@ -51,7 +51,9 @@
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -622,20 +624,39 @@ private String inferRootDN(String server) {
}
}

private static String toProviderUrl(String serverUrl, String rootDN) {
/* package for testing */ static String toProviderUrl(String serverUrl, String rootDN) {
StringBuilder buf = new StringBuilder();
boolean first = true;
for (String s: serverUrl.split("\\s+")) {
if (s.trim().length() == 0) continue;
if (first) first = false; else buf.append(' ');
s = addPrefix(s);
buf.append(s);
if (!s.endsWith("/")) buf.append('/');
buf.append(fixNull(rootDN));
s = getProviderUrl(s, rootDN);
if (s != null) {
if (first) first = false; else buf.append(' ');
buf.append(s);
}
}
return buf.toString();
}

private static String getProviderUrl(String server, String rootDN) {
server = addPrefix(server);
if (!server.endsWith("/")) {
server = server + '/';
}
if (rootDN != null) {
rootDN = rootDN.trim();
if (!rootDN.isEmpty()) {
try {
server = server + new URI(null, null, rootDN, null).toASCIIString();
} catch(URISyntaxException e) {
LOGGER.log(Level.WARNING, "Unable to build URL with rootDN: " + server, e);
return null;
}
}
}
return server;
}

public String getManagerPassword() {
return Secret.toString(managerPasswordSecret);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/hudson/security/LDAPSecurityRealm_Test.java
Expand Up @@ -32,7 +32,9 @@
import jenkins.security.plugins.ldap.FromGroupSearchLDAPGroupMembershipStrategy;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.jvnet.hudson.test.recipes.LocalData;

public class LDAPSecurityRealm_Test { // different name so as not to clash with LDAPSecurityRealmTest.groovy
Expand Down Expand Up @@ -68,4 +70,18 @@ private void check() {
assertEquals("mAAN", sr.getMailAddressAttributeName());
}

@Issue("JENKINS-8152")
@WithoutJenkins
@Test public void providerUrl() throws Exception {
assertEquals("ldap://example.com/", LDAPSecurityRealm.toProviderUrl("example.com", null));
assertEquals("ldap://example.com/", LDAPSecurityRealm.toProviderUrl("example.com", ""));
assertEquals("ldap://example.com/", LDAPSecurityRealm.toProviderUrl("example.com", " "));
assertEquals("ldap://example.com/ ldap://example.net/", LDAPSecurityRealm.toProviderUrl("example.com ldap://example.net", null));
assertEquals("ldap://example.com/o=O,c=C", LDAPSecurityRealm.toProviderUrl("example.com", "o=O,c=C"));
assertEquals("ldap://example.com/o=O,c=C", LDAPSecurityRealm.toProviderUrl("example.com", " o=O,c=C"));
assertEquals("ldap://example.com/o=O,c=C ldap://example.net/o=O,c=C", LDAPSecurityRealm.toProviderUrl("ldap://example.com example.net", "o=O,c=C"));
assertEquals("ldap://example.com/o=O%20O,c=C", LDAPSecurityRealm.toProviderUrl("example.com", "o=O O,c=C"));
assertEquals("ldap://example.com/o=O%20O,c=C ldap://example.net/o=O%20O,c=C", LDAPSecurityRealm.toProviderUrl("example.com example.net", "o=O O,c=C "));
}

}

0 comments on commit b0b8622

Please sign in to comment.