Repository navigation
CLOUDSTACK-8596 ability to query nested groups for Microsoft AD #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -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"; | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no. multiple providers are not possible. |
||
|
|
||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
|
@@ -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()) { | ||
|
|
@@ -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 |
|---|---|---|
|
|
@@ -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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put a comment that this is test-only
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constructor initialized ones were used in the unit tests to supply mocks.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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----- In plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java:
|
||
| super(); | ||
| _ldapConfigurationDao = ldapConfigurationDao; | ||
| _ldapContextFactory = ldapContextFactory; | ||
| _ldapUserManager = ldapUserManager; | ||
| _ldapUserManagerFactory = ldapUserManagerFactory; | ||
| _ldapConfiguration = ldapConfiguration; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -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); | ||
|
|
@@ -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("*"); | ||
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.