CLOUDSTACK-10239: Fallback to default provider if needed (#2430)

Fallback to default provider if needed.
This commit is contained in:
dahn 2018-01-26 12:03:11 +00:00 committed by Rohit Yadav
parent 22d6718fe3
commit 6aadbc5219
5 changed files with 15 additions and 12 deletions

View File

@ -54,11 +54,11 @@ public class LinkDomainToLdapCmd extends BaseCmd {
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true, description = "type of the ldap name. GROUP or OU")
private String type;
@Parameter(name = ApiConstants.LDAP_DOMAIN, type = CommandType.STRING, required = true, description = "name of the group or OU in LDAP")
@Parameter(name = ApiConstants.LDAP_DOMAIN, type = CommandType.STRING, required = false, description = "name of the group or OU in LDAP")
private String ldapDomain;
@Deprecated
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the group or OU in LDAP")
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = false, description = "name of the group or OU in LDAP")
private String name;
@Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ")

View File

@ -25,6 +25,7 @@ import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class LdapContextFactory {
@ -40,12 +41,10 @@ public class LdapContextFactory {
_ldapConfiguration = ldapConfiguration;
}
// TODO add optional domain (optional only for backwards compatibility)
public LdapContext createBindContext(Long domainId) throws NamingException, IOException {
return createBindContext(null, domainId);
}
// TODO add optional domain (optional only for backwards compatibility)
public LdapContext createBindContext(final String providerUrl, Long domainId) throws NamingException, IOException {
final String bindPrincipal = _ldapConfiguration.getBindPrincipal(domainId);
final String bindPassword = _ldapConfiguration.getBindPassword(domainId);
@ -80,9 +79,13 @@ public class LdapContextFactory {
private Hashtable<String, String> getEnvironment(final String principal, final String password, final String providerUrl, final boolean isSystemContext, Long domainId) {
final String factory = _ldapConfiguration.getFactory();
final String url = providerUrl == null ? _ldapConfiguration.getProviderUrl(domainId) : providerUrl;
String url = providerUrl == null ? _ldapConfiguration.getProviderUrl(domainId) : providerUrl;
if (StringUtils.isEmpty(url) && domainId != null) {
//try a default ldap implementation
url = _ldapConfiguration.getProviderUrl(null);
}
final Hashtable<String, String> environment = new Hashtable<String, String>();
final Hashtable<String, String> environment = new Hashtable<>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, factory);
environment.put(Context.PROVIDER_URL, url);

View File

@ -52,7 +52,6 @@ public interface LdapManager extends PluggableService {
@Deprecated
LdapConfigurationResponse deleteConfiguration(String hostname, int port, Long domainId) throws InvalidParameterValueException;
// TODO username is only unique withing domain scope (add domain id to call)
LdapUser getUser(final String username, Long domainId) throws NoLdapUserMatchingQueryException;
LdapUser getUser(String username, String type, String name, Long domainId) throws NoLdapUserMatchingQueryException;

View File

@ -313,7 +313,7 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
@Override
public LinkDomainToLdapResponse linkDomainToLdap(LinkDomainToLdapCmd cmd) {
Validate.isTrue(_ldapConfiguration.getBaseDn(cmd.getDomainId()) == null, "can not configure an ldap server and an ldap group/ou to a domain");
Validate.isTrue(_ldapConfiguration.getBaseDn(cmd.getDomainId()) == null, "can not link a domain unless a basedn is configured for it.");
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");
return linkDomainToLdap(cmd.getDomainId(),cmd.getType(),cmd.getLdapDomain(),cmd.getAccountType());
}
@ -356,8 +356,9 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
return _ldapTrustMapDao.findGroupInDomain(domainId, group);
}
@Override public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
Validate.notNull(_ldapConfiguration.getBaseDn(cmd.getDomainId()), "can not configure an ldap server and an ldap group/ou to a domain");
@Override
public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
Validate.notNull(_ldapConfiguration.getBaseDn(cmd.getDomainId()), "can not link an account to ldap in a domain for which no basdn is configured");
Validate.notNull(cmd.getDomainId(), "domainId cannot be null.");
Validate.notEmpty(cmd.getAccountName(), "accountName cannot be empty.");
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");

View File

@ -62,10 +62,10 @@ public class LdapCreateAccountCmdTest implements LdapConfigurationChanger {
}
@Test(expected = ServerApiException.class)
public void failedCreationDueToANullResponseFromCloudstackAccountCreater() throws Exception {
public void failedCreationDueToANullResponseFromCloudstackAccountCreator() throws Exception {
// We have an LdapManager, AccountService and LdapCreateAccountCmd
LdapUser mrMurphy = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering", false, null);
when(ldapManager.getUser(anyString(), isNull(Long.class))).thenReturn(mrMurphy);
when(ldapManager.getUser(anyString(), isNull(Long.class))).thenReturn(mrMurphy).thenReturn(mrMurphy);
ldapCreateAccountCmd.execute();
fail("An exception should have been thrown: " + ServerApiException.class);
}