Skip to content

Commit 7febdb5

Browse files
committed
Merge branch 'CLOUDSTACK-8596' of https://github.com/karuturi/cloudstack
This closes apache#609
2 parents 2984acc + 96cf032 commit 7febdb5

14 files changed

Lines changed: 733 additions & 299 deletions

‎plugins/user-authenticators/ldap/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<plugin>
2525
<groupId>org.codehaus.gmaven</groupId>
2626
<artifactId>gmaven-plugin</artifactId>
27-
<version>1.5</version>
27+
<version>1.3</version>
2828
<configuration>
2929
<providerSelection>1.7</providerSelection>
3030
</configuration>

‎plugins/user-authenticators/ldap/resources/META-INF/cloudstack/ldap/spring-ldap-context.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</bean>
3131

3232
<bean id="LdapManager" class="org.apache.cloudstack.ldap.LdapManagerImpl" />
33-
<bean id="LdapUserManager" class="org.apache.cloudstack.ldap.LdapUserManager" />
33+
<bean id="LdapUserManagerFactory" class="org.apache.cloudstack.ldap.LdapUserManagerFactory" />
3434
<bean id="LdapContextFactory" class="org.apache.cloudstack.ldap.LdapContextFactory" />
3535
<bean id="LdapConfigurationDao"
3636
class="org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl" />
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.ldap;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import javax.naming.NamingEnumeration;
25+
import javax.naming.NamingException;
26+
import javax.naming.directory.SearchControls;
27+
import javax.naming.directory.SearchResult;
28+
import javax.naming.ldap.LdapContext;
29+
30+
import org.apache.commons.lang.StringUtils;
31+
import org.apache.log4j.Logger;
32+
33+
public class ADLdapUserManagerImpl extends OpenLdapUserManagerImpl implements LdapUserManager {
34+
public static final Logger s_logger = Logger.getLogger(ADLdapUserManagerImpl.class.getName());
35+
private static final String MICROSOFT_AD_NESTED_MEMBERS_FILTER = "memberOf:1.2.840.113556.1.4.1941";
36+
37+
@Override
38+
public List<LdapUser> getUsersInGroup(String groupName, LdapContext context) throws NamingException {
39+
if (StringUtils.isBlank(groupName)) {
40+
throw new IllegalArgumentException("ldap group name cannot be blank");
41+
}
42+
43+
String basedn = _ldapConfiguration.getBaseDn();
44+
if (StringUtils.isBlank(basedn)) {
45+
throw new IllegalArgumentException("ldap basedn is not configured");
46+
}
47+
48+
final SearchControls searchControls = new SearchControls();
49+
searchControls.setSearchScope(_ldapConfiguration.getScope());
50+
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
51+
52+
NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls);
53+
final List<LdapUser> users = new ArrayList<LdapUser>();
54+
while (results.hasMoreElements()) {
55+
final SearchResult result = results.nextElement();
56+
users.add(createUser(result));
57+
}
58+
return users;
59+
}
60+
61+
private String generateADGroupSearchFilter(String groupName) {
62+
final StringBuilder userObjectFilter = new StringBuilder();
63+
userObjectFilter.append("(objectClass=");
64+
userObjectFilter.append(_ldapConfiguration.getUserObject());
65+
userObjectFilter.append(")");
66+
67+
final StringBuilder memberOfFilter = new StringBuilder();
68+
String groupCnName = _ldapConfiguration.getCommonNameAttribute() + "=" +groupName + "," + _ldapConfiguration.getBaseDn();
69+
memberOfFilter.append("(" + MICROSOFT_AD_NESTED_MEMBERS_FILTER + ":=");
70+
memberOfFilter.append(groupCnName);
71+
memberOfFilter.append(")");
72+
73+
final StringBuilder result = new StringBuilder();
74+
result.append("(&");
75+
result.append(userObjectFilter);
76+
result.append(memberOfFilter);
77+
result.append(")");
78+
79+
s_logger.debug("group search filter = " + result);
80+
return result.toString();
81+
}
82+
}

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import javax.inject.Inject;
2222
import javax.naming.directory.SearchControls;
2323

24-
import org.apache.cloudstack.api.command.LdapListConfigurationCmd;
2524
import org.apache.cloudstack.framework.config.ConfigKey;
2625
import org.apache.cloudstack.framework.config.Configurable;
2726
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
2827

2928
import com.cloud.utils.Pair;
29+
import org.apache.cloudstack.ldap.dao.LdapConfigurationDao;
3030

3131
public class LdapConfiguration implements Configurable{
3232
private final static String factory = "com.sun.jndi.ldap.LdapCtxFactory";
@@ -36,21 +36,23 @@ public class LdapConfiguration implements Configurable{
3636

3737
private static final ConfigKey<Integer> ldapPageSize = new ConfigKey<Integer>(Integer.class, "ldap.request.page.size", "Advanced", "1000",
3838
"page size sent to ldap server on each request to get user", true, ConfigKey.Scope.Global, 1);
39+
private static final ConfigKey<String> ldapProvider = new ConfigKey<String>(String.class, "ldap.provider", "Advanced", "openldap", "ldap provider ex:openldap, microsoftad",
40+
true, ConfigKey.Scope.Global, null);
3941

4042
private final static int scope = SearchControls.SUBTREE_SCOPE;
4143

4244
@Inject
4345
private ConfigurationDao _configDao;
4446

4547
@Inject
46-
private LdapManager _ldapManager;
48+
private LdapConfigurationDao _ldapConfigurationDao;
4749

4850
public LdapConfiguration() {
4951
}
5052

51-
public LdapConfiguration(final ConfigurationDao configDao, final LdapManager ldapManager) {
53+
public LdapConfiguration(final ConfigurationDao configDao, final LdapConfigurationDao ldapConfigurationDao) {
5254
_configDao = configDao;
53-
_ldapManager = ldapManager;
55+
_ldapConfigurationDao = ldapConfigurationDao;
5456
}
5557

5658
public String getAuthentication() {
@@ -94,7 +96,7 @@ public String getLastnameAttribute() {
9496

9597
public String getProviderUrl() {
9698
final String protocol = getSSLStatus() == true ? "ldaps://" : "ldap://";
97-
final Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager.listConfigurations(new LdapListConfigurationCmd(_ldapManager));
99+
final Pair<List<LdapConfigurationVO>, Integer> result = _ldapConfigurationDao.searchConfigurations(null, 0);
98100
final StringBuilder providerUrls = new StringBuilder();
99101
String delim = "";
100102
for (final LdapConfigurationVO resource : result.first()) {
@@ -165,13 +167,24 @@ public Integer getLdapPageSize() {
165167
return ldapPageSize.value();
166168
}
167169

170+
public LdapUserManager.Provider getLdapProvider() {
171+
LdapUserManager.Provider provider;
172+
try {
173+
provider = LdapUserManager.Provider.valueOf(ldapProvider.value().toUpperCase());
174+
} catch (IllegalArgumentException ex) {
175+
//openldap is the default
176+
provider = LdapUserManager.Provider.OPENLDAP;
177+
}
178+
return provider;
179+
}
180+
168181
@Override
169182
public String getConfigComponentName() {
170183
return LdapConfiguration.class.getSimpleName();
171184
}
172185

173186
@Override
174187
public ConfigKey<?>[] getConfigKeys() {
175-
return new ConfigKey<?>[] {ldapReadTimeout, ldapPageSize};
188+
return new ConfigKey<?>[] {ldapReadTimeout, ldapPageSize, ldapProvider};
176189
}
177190
}

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
5757
private LdapContextFactory _ldapContextFactory;
5858

5959
@Inject
60-
private LdapUserManager _ldapUserManager;
60+
private LdapConfiguration _ldapConfiguration;
61+
62+
@Inject LdapUserManagerFactory _ldapUserManagerFactory;
63+
6164

6265
public LdapManagerImpl() {
6366
super();
6467
}
6568

66-
public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManager ldapUserManager) {
69+
public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManagerFactory ldapUserManagerFactory,
70+
final LdapConfiguration ldapConfiguration) {
6771
super();
6872
_ldapConfigurationDao = ldapConfigurationDao;
6973
_ldapContextFactory = ldapContextFactory;
70-
_ldapUserManager = ldapUserManager;
74+
_ldapUserManagerFactory = ldapUserManagerFactory;
75+
_ldapConfiguration = ldapConfiguration;
7176
}
7277

7378
@Override
@@ -173,7 +178,7 @@ public LdapUser getUser(final String username) throws NoLdapUserMatchingQueryExc
173178
context = _ldapContextFactory.createBindContext();
174179

175180
final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
176-
return _ldapUserManager.getUser(escapedUsername, context);
181+
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUser(escapedUsername, context);
177182

178183
} catch (NamingException | IOException e) {
179184
s_logger.debug("ldap Exception: ",e);
@@ -188,7 +193,7 @@ public List<LdapUser> getUsers() throws NoLdapUserMatchingQueryException {
188193
LdapContext context = null;
189194
try {
190195
context = _ldapContextFactory.createBindContext();
191-
return _ldapUserManager.getUsers(context);
196+
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsers(context);
192197
} catch (NamingException | IOException e) {
193198
s_logger.debug("ldap Exception: ",e);
194199
throw new NoLdapUserMatchingQueryException("*");
@@ -202,7 +207,7 @@ public List<LdapUser> getUsersInGroup(String groupName) throws NoLdapUserMatchin
202207
LdapContext context = null;
203208
try {
204209
context = _ldapContextFactory.createBindContext();
205-
return _ldapUserManager.getUsersInGroup(groupName, context);
210+
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsersInGroup(groupName, context);
206211
} catch (NamingException | IOException e) {
207212
s_logger.debug("ldap NamingException: ",e);
208213
throw new NoLdapUserMatchingQueryException("groupName=" + groupName);
@@ -230,7 +235,7 @@ public List<LdapUser> searchUsers(final String username) throws NoLdapUserMatchi
230235
try {
231236
context = _ldapContextFactory.createBindContext();
232237
final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
233-
return _ldapUserManager.getUsers("*" + escapedUsername + "*", context);
238+
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsers("*" + escapedUsername + "*", context);
234239
} catch (NamingException | IOException e) {
235240
s_logger.debug("ldap Exception: ",e);
236241
throw new NoLdapUserMatchingQueryException(username);

0 commit comments

Comments
 (0)