Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/user-authenticators/ldap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<version>1.3</version>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</bean>

<bean id="LdapManager" class="org.apache.cloudstack.ldap.LdapManagerImpl" />
<bean id="LdapUserManager" class="org.apache.cloudstack.ldap.LdapUserManager" />
<bean id="LdapUserManagerFactory" class="org.apache.cloudstack.ldap.LdapUserManagerFactory" />
<bean id="LdapContextFactory" class="org.apache.cloudstack.ldap.LdapContextFactory" />
<bean id="LdapConfigurationDao"
class="org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.ldap;

import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ADLdapUserManagerImpl extends OpenLdapUserManagerImpl implements LdapUserManager {
public static final Logger s_logger = Logger.getLogger(ADLdapUserManagerImpl.class.getName());
private static final String MICROSOFT_AD_NESTED_MEMBERS_FILTER = "memberOf:1.2.840.113556.1.4.1941";

@Override
public List<LdapUser> getUsersInGroup(String groupName, LdapContext context) throws NamingException {
if (StringUtils.isBlank(groupName)) {
throw new IllegalArgumentException("ldap group name cannot be blank");
}

String basedn = _ldapConfiguration.getBaseDn();
if (StringUtils.isBlank(basedn)) {
throw new IllegalArgumentException("ldap basedn is not configured");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't Basedn a required config? In that case it should be validated while configuring the ldap manager and not every time with these operations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basedn comes from global config. Currently, there is no way to ensure that this configuration is not null when ldap is enabled. This will be handled once I move all the ldap configurations to the ldap_configuration table and not the global configuration table.

}

final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());

NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls);
final List<LdapUser> users = new ArrayList<LdapUser>();
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
users.add(createUser(result));
}
return users;
}

private String generateADGroupSearchFilter(String groupName) {
final StringBuilder userObjectFilter = new StringBuilder();
userObjectFilter.append("(objectClass=");
userObjectFilter.append(_ldapConfiguration.getUserObject());
userObjectFilter.append(")");

final StringBuilder memberOfFilter = new StringBuilder();
String groupCnName = _ldapConfiguration.getCommonNameAttribute() + "=" +groupName + "," + _ldapConfiguration.getBaseDn();
memberOfFilter.append("(" + MICROSOFT_AD_NESTED_MEMBERS_FILTER + ":=");
memberOfFilter.append(groupCnName);
memberOfFilter.append(")");

final StringBuilder result = new StringBuilder();
result.append("(&");
result.append(userObjectFilter);
result.append(memberOfFilter);
result.append(")");

s_logger.debug("group search filter = " + result);
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import javax.inject.Inject;
import javax.naming.directory.SearchControls;

import org.apache.cloudstack.api.command.LdapListConfigurationCmd;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.Configurable;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;

import com.cloud.utils.Pair;
import org.apache.cloudstack.ldap.dao.LdapConfigurationDao;

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this change please move all LDAP related config to use the ConfigKey/Configurable mechanism of publishing them. Also is it possible to have multiple ldap providers configured?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. multiple providers are not possible.
I havent touched any of the existing configurations as I will be refactoring them along with https://cwiki.apache.org/confluence/display/CLOUDSTACK/WIP%3A+LDAP%3A+Trust+AD+and+Auto+Import


private final static int scope = SearchControls.SUBTREE_SCOPE;

@Inject
private ConfigurationDao _configDao;

@Inject
private LdapManager _ldapManager;
private LdapConfigurationDao _ldapConfigurationDao;

public LdapConfiguration() {
}

public LdapConfiguration(final ConfigurationDao configDao, final LdapManager ldapManager) {
public LdapConfiguration(final ConfigurationDao configDao, final LdapConfigurationDao ldapConfigurationDao) {
_configDao = configDao;
_ldapManager = ldapManager;
_ldapConfigurationDao = ldapConfigurationDao;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why inject and pass on in the constructor. Do both have usage scenarios?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor initialized ones were used in the unit tests to supply mocks.

}

public String getAuthentication() {
Expand Down Expand Up @@ -94,7 +96,7 @@ public String getLastnameAttribute() {

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

public LdapUserManager.Provider getLdapProvider() {
LdapUserManager.Provider provider;
try {
provider = LdapUserManager.Provider.valueOf(ldapProvider.value().toUpperCase());
} catch (IllegalArgumentException ex) {
//openldap is the default
provider = LdapUserManager.Provider.OPENLDAP;
}
return provider;
}

@Override
public String getConfigComponentName() {
return LdapConfiguration.class.getSimpleName();
}

@Override
public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] {ldapReadTimeout, ldapPageSize};
return new ConfigKey<?>[] {ldapReadTimeout, ldapPageSize, ldapProvider};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,22 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
private LdapContextFactory _ldapContextFactory;

@Inject
private LdapUserManager _ldapUserManager;
private LdapConfiguration _ldapConfiguration;

@Inject LdapUserManagerFactory _ldapUserManagerFactory;


public LdapManagerImpl() {
super();
}

public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManager ldapUserManager) {
public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManagerFactory ldapUserManagerFactory,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put a comment that this is test-only

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not added now. its an existing one which is modified. I am planning on removing them in the future commits as there are other ways to inject them to tests.

final LdapConfiguration ldapConfiguration) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why inject and pass into constructor? is the ldap-provider-selector going to change on runtime?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor initialized ones were used in the unit tests to supply mocks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why not use injection for that as well? Is this a groovy thing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was initially done that way. We could inject as well. I did that in the new class.

-----Original Message-----
From: "Daan Hoogland" notifications@github.com
Sent: ‎22-‎07-‎2015 13:18
To: "apache/cloudstack" cloudstack@noreply.github.com
Cc: "Rajani Karuturi" rajanikaruturi@gmail.com
Subject: Re: [cloudstack] CLOUDSTACK-8596 ability to query nested groups forMicrosoft AD (#609)

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

 public LdapManagerImpl() {
     super();
 }
  • public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManager ldapUserManager) {
  • public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, final LdapContextFactory ldapContextFactory, final LdapUserManagerFactory ldapUserManagerFactory,
  •                       final LdapConfiguration ldapConfiguration) {
    
    And why not use injection for that as well? Is this a groovy thing?
    —
    Reply to this email directly or view it on GitHub.

super();
_ldapConfigurationDao = ldapConfigurationDao;
_ldapContextFactory = ldapContextFactory;
_ldapUserManager = ldapUserManager;
_ldapUserManagerFactory = ldapUserManagerFactory;
_ldapConfiguration = ldapConfiguration;
}

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

final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
return _ldapUserManager.getUser(escapedUsername, context);
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUser(escapedUsername, context);

} catch (NamingException | IOException e) {
s_logger.debug("ldap Exception: ",e);
Expand All @@ -188,7 +193,7 @@ public List<LdapUser> getUsers() throws NoLdapUserMatchingQueryException {
LdapContext context = null;
try {
context = _ldapContextFactory.createBindContext();
return _ldapUserManager.getUsers(context);
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsers(context);
} catch (NamingException | IOException e) {
s_logger.debug("ldap Exception: ",e);
throw new NoLdapUserMatchingQueryException("*");
Expand All @@ -202,7 +207,7 @@ public List<LdapUser> getUsersInGroup(String groupName) throws NoLdapUserMatchin
LdapContext context = null;
try {
context = _ldapContextFactory.createBindContext();
return _ldapUserManager.getUsersInGroup(groupName, context);
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsersInGroup(groupName, context);
} catch (NamingException | IOException e) {
s_logger.debug("ldap NamingException: ",e);
throw new NoLdapUserMatchingQueryException("groupName=" + groupName);
Expand Down Expand Up @@ -230,7 +235,7 @@ public List<LdapUser> searchUsers(final String username) throws NoLdapUserMatchi
try {
context = _ldapContextFactory.createBindContext();
final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
return _ldapUserManager.getUsers("*" + escapedUsername + "*", context);
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUsers("*" + escapedUsername + "*", context);
} catch (NamingException | IOException e) {
s_logger.debug("ldap Exception: ",e);
throw new NoLdapUserMatchingQueryException(username);
Expand Down
Loading