Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-25576] A Ldap plugin test that runs with LDAP server is added.
There was only one Ldap plugin test which requires Docker to run. The newly created LdapPluginNoDockerTest runs without Docker but with a real LDAP server. Detailed configuration information can be set through environment variables. Because LdapPluginNoDockerTest has similar test stories with ActiveDirectoryTest, ActiveDirectoryTest is refactored to reduce duplicate code. Change-Id: If729ceb05eb96a0b3abfc33fa141b748a0ba461a
- Loading branch information
Bowen Cheng
authored and
Bowen Cheng
committed
Nov 12, 2014
1 parent
076fb39
commit 2c87fd8
Showing
5 changed files
with
405 additions
and
16 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
src/main/java/org/jenkinsci/test/acceptance/plugins/ldap/LdapEnv.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2014 Ericsson | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jenkinsci.test.acceptance.plugins.ldap; | ||
|
||
import static org.junit.Assume.assumeNotNull; | ||
|
||
/** | ||
* Thread-unsafe singleton serving ldap-prefixed test env args. | ||
* | ||
* @author Bowen.Cheng@ericsson.com | ||
*/ | ||
public class LdapEnv { | ||
|
||
private static final int DEFAULT_LDAP_PORT = 3268; | ||
private static final String TRUE = "true"; | ||
private static final String FALSE = "false"; | ||
private static final String PARSE_USER_ATTRIBUTE_STRATEGY = "ParseUserAttribute"; | ||
private static final String SEARCH_FOR_GROUPS_STRATEGY = "SearchForGroups"; | ||
|
||
private static LdapDetails ldapDetails = null; | ||
private static LdapEnv ldapEnv = null; | ||
|
||
private String user; | ||
private String password; | ||
private String group; | ||
|
||
private LdapEnv() { | ||
user = System.getenv("ldapUser"); | ||
password = System.getenv("ldapPassword"); | ||
group = System.getenv("ldapGroup"); | ||
} | ||
|
||
/** | ||
* @return Existing ldapEnv object or a newly constructed one with given environment variables | ||
*/ | ||
public static LdapEnv getLdapEnv() { | ||
if(ldapEnv == null) { | ||
ldapEnv = new LdapEnv(); | ||
} | ||
return ldapEnv; | ||
} | ||
|
||
/** | ||
* @return Existing ldapDetails object or a newly constructed one with given environment variables | ||
*/ | ||
public static LdapDetails getLdapDetails() { | ||
if(ldapDetails == null) { | ||
ldapDetails = new LdapEnv().constructLdapDetails(); | ||
} | ||
return ldapDetails; | ||
} | ||
|
||
/** | ||
* @return user name by "-ldapUser" attribute | ||
*/ | ||
public String getUser() { | ||
assumeNotNull(user); | ||
return user; | ||
} | ||
|
||
/** | ||
* @return password used for logging in by "-ldapPassword" attribute | ||
*/ | ||
public String getPassword() { | ||
assumeNotNull(password); | ||
return password; | ||
} | ||
|
||
/** | ||
* @return user group by "-ldapGroup" attribute | ||
*/ | ||
public String getGroup() { | ||
assumeNotNull(group); | ||
return group; | ||
} | ||
|
||
private LdapDetails constructLdapDetails() { | ||
String host = System.getenv("ldapHost"); | ||
int port = parseInteger(System.getenv("ldapPort"), DEFAULT_LDAP_PORT); | ||
|
||
LdapDetails ldapDetails = new LdapDetails( | ||
host, port, | ||
System.getenv("ldapManagerDn"), | ||
System.getenv("ldapManagerPassword"), | ||
System.getenv("ldapRootDn") | ||
); | ||
ldapDetails.setHostWithPort(host + ":" + port); | ||
ldapDetails.setUserSearchBase(System.getenv("ldapUserSearchBase")); | ||
ldapDetails.setUserSearchFilter(System.getenv("ldapUserSearchFilter")); | ||
ldapDetails.setGroupSearchBase(System.getenv("ldapGroupSearchBase")); | ||
ldapDetails.setGroupSearchFilter(System.getenv("ldapGroupSearchFilter")); | ||
ldapDetails.setGroupMembershipStrategy(parseGroupMembershipStrategy(System.getenv("ldapGroupMembershipStrategy"))); | ||
ldapDetails.setGroupMembershipStrategyParam(System.getenv("ldapGroupMembershipStrategyParam")); | ||
ldapDetails.setGroupMembershipFilter(System.getenv("ldapGroupMembershipFilter")); | ||
ldapDetails.setDisplayNameAttributeName(System.getenv("ldapDisplayNameAttributeName")); | ||
ldapDetails.setMailAddressAttributeName(System.getenv("ldapMailAddressAttributeName")); | ||
ldapDetails.setDisableLdapEmailResolver(parseBoolean(System.getenv("ldapDisableLdapEmailResolver"), false)); | ||
ldapDetails.setEnableCache(parseBoolean(System.getenv("ldapEnableCache"), false)); | ||
ldapDetails.setCacheSize(parseInteger(System.getenv("ldapCacheSize"), 20)); | ||
ldapDetails.setCacheTTL(parseInteger(System.getenv("ldapCacheTTL"), 300)); | ||
|
||
return ldapDetails; | ||
} | ||
|
||
private Class parseGroupMembershipStrategy(String strategy) { | ||
Class<? extends LdapGroupMembershipStrategy> strategyClass = null; | ||
|
||
if (isEmptyOrNullString(strategy)) { | ||
return strategyClass; | ||
} | ||
|
||
if (strategy.compareToIgnoreCase(PARSE_USER_ATTRIBUTE_STRATEGY) == 0) { | ||
strategyClass = ParseUserAttributeLdapGroupMembershipStrategy.class; | ||
} else if (strategy.compareToIgnoreCase(SEARCH_FOR_GROUPS_STRATEGY) == 0) { | ||
strategyClass = SearchForGroupsLdapGroupMembershipStrategy.class; | ||
} | ||
|
||
return strategyClass; | ||
} | ||
|
||
/** | ||
* Parse string representation of a boolean | ||
* | ||
* @param booleanString string representation of a boolean to be parsed | ||
* @param defaultValue the value to be returned if there is any parse exception | ||
* @return boolean value parsed | ||
*/ | ||
private boolean parseBoolean(String booleanString, boolean defaultValue) { | ||
boolean result = defaultValue; | ||
|
||
if(isEmptyOrNullString(booleanString)) { | ||
return result; | ||
} | ||
if(booleanString.compareToIgnoreCase(TRUE) == 0 || booleanString.compareToIgnoreCase(FALSE) == 0) { | ||
result = Boolean.parseBoolean(booleanString); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Parse string representation of a integer | ||
* | ||
* @param intString string representation of a integer to be parsed | ||
* @param defaultValue the value to be returned if there is any parse exception | ||
* @return integer value after parsed | ||
*/ | ||
private int parseInteger(String intString, int defaultValue) { | ||
int result; | ||
try { | ||
result = Integer.parseInt(intString); | ||
} catch (NumberFormatException e) { | ||
result = defaultValue; | ||
} | ||
return result; | ||
} | ||
|
||
private boolean isEmptyOrNullString(String target) { | ||
return target == null || target.isEmpty(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jenkinsci/test/acceptance/utils/pluginTests/SecurityConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2014 Ericsson | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jenkinsci.test.acceptance.utils.pluginTests; | ||
|
||
import org.jenkinsci.test.acceptance.plugins.matrix_auth.MatrixRow; | ||
import org.jenkinsci.test.acceptance.plugins.matrix_auth.ProjectBasedMatrixAuthorizationStrategy; | ||
import org.jenkinsci.test.acceptance.po.GlobalSecurityConfig; | ||
|
||
public class SecurityConfigUtils { | ||
|
||
private SecurityConfigUtils() { | ||
} | ||
|
||
/** | ||
* Add and authorize given user admin role under "Project-based Matrix Authorization Strategy" | ||
* | ||
* @param user user to be added and authorized as admin | ||
* @param security page object | ||
* @return security page object | ||
*/ | ||
public static GlobalSecurityConfig authorizeUserAsAdmin(String user, GlobalSecurityConfig security) { | ||
ProjectBasedMatrixAuthorizationStrategy auth = security.useAuthorizationStrategy(ProjectBasedMatrixAuthorizationStrategy.class); | ||
MatrixRow userAuth = auth.addUser(user); | ||
userAuth.admin(); | ||
return security; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.