Skip to content

Commit 6aadbc5

Browse files
DaanHooglandrohityadavcloud
authored andcommitted
CLOUDSTACK-10239: Fallback to default provider if needed (#2430)
Fallback to default provider if needed.
1 parent 22d6718 commit 6aadbc5

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

Diff for: plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LinkDomainToLdapCmd.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public class LinkDomainToLdapCmd extends BaseCmd {
5454
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true, description = "type of the ldap name. GROUP or OU")
5555
private String type;
5656

57-
@Parameter(name = ApiConstants.LDAP_DOMAIN, type = CommandType.STRING, required = true, description = "name of the group or OU in LDAP")
57+
@Parameter(name = ApiConstants.LDAP_DOMAIN, type = CommandType.STRING, required = false, description = "name of the group or OU in LDAP")
5858
private String ldapDomain;
5959

6060
@Deprecated
61-
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the group or OU in LDAP")
61+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = false, description = "name of the group or OU in LDAP")
6262
private String name;
6363

6464
@Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ")

Diff for: plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.naming.ldap.InitialLdapContext;
2626
import javax.naming.ldap.LdapContext;
2727

28+
import org.apache.commons.lang3.StringUtils;
2829
import org.apache.log4j.Logger;
2930

3031
public class LdapContextFactory {
@@ -40,12 +41,10 @@ public LdapContextFactory(final LdapConfiguration ldapConfiguration) {
4041
_ldapConfiguration = ldapConfiguration;
4142
}
4243

43-
// TODO add optional domain (optional only for backwards compatibility)
4444
public LdapContext createBindContext(Long domainId) throws NamingException, IOException {
4545
return createBindContext(null, domainId);
4646
}
4747

48-
// TODO add optional domain (optional only for backwards compatibility)
4948
public LdapContext createBindContext(final String providerUrl, Long domainId) throws NamingException, IOException {
5049
final String bindPrincipal = _ldapConfiguration.getBindPrincipal(domainId);
5150
final String bindPassword = _ldapConfiguration.getBindPassword(domainId);
@@ -80,9 +79,13 @@ private void enableSSL(final Hashtable<String, String> environment) {
8079

8180
private Hashtable<String, String> getEnvironment(final String principal, final String password, final String providerUrl, final boolean isSystemContext, Long domainId) {
8281
final String factory = _ldapConfiguration.getFactory();
83-
final String url = providerUrl == null ? _ldapConfiguration.getProviderUrl(domainId) : providerUrl;
82+
String url = providerUrl == null ? _ldapConfiguration.getProviderUrl(domainId) : providerUrl;
83+
if (StringUtils.isEmpty(url) && domainId != null) {
84+
//try a default ldap implementation
85+
url = _ldapConfiguration.getProviderUrl(null);
86+
}
8487

85-
final Hashtable<String, String> environment = new Hashtable<String, String>();
88+
final Hashtable<String, String> environment = new Hashtable<>();
8689

8790
environment.put(Context.INITIAL_CONTEXT_FACTORY, factory);
8891
environment.put(Context.PROVIDER_URL, url);

Diff for: plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ enum LinkType { GROUP, OU;}
5252
@Deprecated
5353
LdapConfigurationResponse deleteConfiguration(String hostname, int port, Long domainId) throws InvalidParameterValueException;
5454

55-
// TODO username is only unique withing domain scope (add domain id to call)
5655
LdapUser getUser(final String username, Long domainId) throws NoLdapUserMatchingQueryException;
5756

5857
LdapUser getUser(String username, String type, String name, Long domainId) throws NoLdapUserMatchingQueryException;

Diff for: plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public List<LdapUser> searchUsers(final String username) throws NoLdapUserMatchi
313313

314314
@Override
315315
public LinkDomainToLdapResponse linkDomainToLdap(LinkDomainToLdapCmd cmd) {
316-
Validate.isTrue(_ldapConfiguration.getBaseDn(cmd.getDomainId()) == null, "can not configure an ldap server and an ldap group/ou to a domain");
316+
Validate.isTrue(_ldapConfiguration.getBaseDn(cmd.getDomainId()) == null, "can not link a domain unless a basedn is configured for it.");
317317
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");
318318
return linkDomainToLdap(cmd.getDomainId(),cmd.getType(),cmd.getLdapDomain(),cmd.getAccountType());
319319
}
@@ -356,8 +356,9 @@ public LdapTrustMapVO getLinkedLdapGroup(long domainId, String group) {
356356
return _ldapTrustMapDao.findGroupInDomain(domainId, group);
357357
}
358358

359-
@Override public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
360-
Validate.notNull(_ldapConfiguration.getBaseDn(cmd.getDomainId()), "can not configure an ldap server and an ldap group/ou to a domain");
359+
@Override
360+
public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
361+
Validate.notNull(_ldapConfiguration.getBaseDn(cmd.getDomainId()), "can not link an account to ldap in a domain for which no basdn is configured");
361362
Validate.notNull(cmd.getDomainId(), "domainId cannot be null.");
362363
Validate.notEmpty(cmd.getAccountName(), "accountName cannot be empty.");
363364
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");

Diff for: plugins/user-authenticators/ldap/test/org/apache/cloudstack/api/command/LdapCreateAccountCmdTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public void failureToRetrieveLdapUser() throws Exception {
6262
}
6363

6464
@Test(expected = ServerApiException.class)
65-
public void failedCreationDueToANullResponseFromCloudstackAccountCreater() throws Exception {
65+
public void failedCreationDueToANullResponseFromCloudstackAccountCreator() throws Exception {
6666
// We have an LdapManager, AccountService and LdapCreateAccountCmd
6767
LdapUser mrMurphy = new LdapUser("rmurphy", "[email protected]", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering", false, null);
68-
when(ldapManager.getUser(anyString(), isNull(Long.class))).thenReturn(mrMurphy);
68+
when(ldapManager.getUser(anyString(), isNull(Long.class))).thenReturn(mrMurphy).thenReturn(mrMurphy);
6969
ldapCreateAccountCmd.execute();
7070
fail("An exception should have been thrown: " + ServerApiException.class);
7171
}

0 commit comments

Comments
 (0)