AMBARI-21307 Added unit tests. Improved logging, refactoring: rename, organized classes into packages
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3bb03e98 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3bb03e98 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3bb03e98 Branch: refs/heads/feature-branch-AMBARI-21307 Commit: 3bb03e989424413b188474ee009f90d76951fb18 Parents: 010ff38 Author: lpuskas <lpus...@apache.org> Authored: Fri Oct 13 17:19:41 2017 +0200 Committer: lpuskas <lpus...@apache.org> Committed: Thu Oct 19 14:42:02 2017 +0200 ---------------------------------------------------------------------- .../apache/ambari/server/ldap/LdapModule.java | 2 +- .../AmbariLdapConfigurationProvider.java | 13 +- .../server/ldap/service/AmbariLdapFacade.java | 2 +- .../service/LdapConnectionConfigService.java | 4 +- .../ads/DefaultLdapConfigurationService.java | 38 +++- .../ads/DefaultLdapConnectionConfigService.java | 116 ++++++++++ .../DefaultLdapConnectionConfigService.java | 116 ---------- .../server/ldap/LdapModuleFunctionalTest.java | 4 +- ...estAmbariAmbariLdapConfigurationFactory.java | 29 --- .../TestAmbariLdapConfigurationFactory.java | 29 +++ .../ldap/service/AmbariLdapFacadeTest.java | 4 +- .../DefaultLdapConfigurationServiceTest.java | 221 +++++++++++++++++++ 12 files changed, 414 insertions(+), 164 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java index 4abf4e7..67e84dc 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java @@ -25,7 +25,7 @@ import org.apache.ambari.server.ldap.service.LdapConnectionConfigService; import org.apache.ambari.server.ldap.service.LdapFacade; import org.apache.ambari.server.ldap.service.ads.DefaultLdapAttributeDetectionService; import org.apache.ambari.server.ldap.service.ads.DefaultLdapConfigurationService; -import org.apache.ambari.server.ldap.service.ads.detectors.DefaultLdapConnectionConfigService; +import org.apache.ambari.server.ldap.service.ads.DefaultLdapConnectionConfigService; import com.google.inject.AbstractModule; import com.google.inject.assistedinject.FactoryModuleBuilder; http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapConfigurationProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapConfigurationProvider.java index 9fad896..c88d420 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapConfigurationProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapConfigurationProvider.java @@ -36,7 +36,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** - * Provider implementation for LDAP configurations. It needs to be registered in the related GUICE module as a provider + * Provider implementation for LDAP configurations. + * It needs to be registered in the related GUICE module as a provider. * It's responsible for managing LDAP configurations in the application. * Whenever requested, this provider returns an AmbariLdapConfiguration which is always in sync with the persisted LDAP * configuration resource. @@ -85,15 +86,15 @@ public class AmbariLdapConfigurationProvider implements Provider<AmbariLdapConfi AmbariConfigurationEntity configEntity = null; LOGGER.info("Loading LDAP configuration ..."); - if (null != configurationId) { + if (null == configurationId) { - LOGGER.debug("Reloading configuration based on the provied id: {}", configurationId); - configEntity = ambariConfigurationDAOProvider.get().findByPK(configurationId); + LOGGER.debug("Initial loading of the ldap configuration ..."); + configEntity = ambariConfigurationDAOProvider.get().getLdapConfiguration(); } else { - LOGGER.debug("Initial loading of the ldap configuration ..."); - configEntity = ambariConfigurationDAOProvider.get().getLdapConfiguration(); + LOGGER.debug("Reloading configuration based on the provied id: {}", configurationId); + configEntity = ambariConfigurationDAOProvider.get().findByPK(configurationId); } http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java index bbfbc8e..0118840 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java @@ -33,7 +33,7 @@ public class AmbariLdapFacade implements LdapFacade { /** * Additional parameters expected to be provided along with the configuration */ - protected enum Parameters { + public enum Parameters { TEST_USER_NAME("ambari.ldap.test.user.name"), TEST_USER_PASSWORD("ambari.ldap.test.user.password"); http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConnectionConfigService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConnectionConfigService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConnectionConfigService.java index e2055bb..a882075 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConnectionConfigService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConnectionConfigService.java @@ -18,7 +18,9 @@ import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; import org.apache.directory.ldap.client.api.LdapConnectionConfig; /** - * Contract for creating connection configuration instances + * Contract for creating connection configuration instances. + * Implementers are in charge for implementing any required custom logic based on the ambari configuration properties. + * (Eg.: using custom key stores etc...) */ public interface LdapConnectionConfigService { http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java index 60c1272..d80a636 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java @@ -58,7 +58,7 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService @Override public void checkConnection(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - + LOGGER.info("Trying to connect to the LDAP server using provided configuration..."); LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); // check if the connection from the connection pool of the template is connected @@ -74,6 +74,8 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService throw new AmbariLdapException("Could not connect to the LDAP server. Configuration: " + ambariLdapConfiguration); } + LOGGER.info("Successfully conencted to the LDAP."); + } /** @@ -92,7 +94,7 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService public String checkUserAttributes(String testUserName, String testPassword, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { String userDn; try { - LOGGER.info("Checking user attributes for user {} r ...", testUserName); + LOGGER.info("Checking user attributes for user [{}] ...", testUserName); // set up a filter based on the provided attributes String filter = FilterBuilder.and( @@ -100,10 +102,15 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService FilterBuilder.equal(ambariLdapConfiguration.userNameAttribute(), testUserName)) .toString(); - LOGGER.info("Searching for the user: {} using the search filter: {}", testUserName, filter); + LOGGER.info("Searching for the user: [{}] using the search filter: [{}]", testUserName, filter); userDn = ldapConnectionTemplateFactory.create(ambariLdapConfiguration).searchFirst(new Dn(ambariLdapConfiguration.userSearchBase()), filter, SearchScope.SUBTREE, getUserDnNameEntryMapper(ambariLdapConfiguration)); - LOGGER.info("Attribute validation succeeded. Filter: {}", filter); + if (null == userDn) { + LOGGER.info("Could not find user based on the provided configuration. User attributes are not complete "); + throw new AmbariLdapException("User attribute configuration incomplete"); + } + LOGGER.info("Attribute validation succeeded. Filter: [{}]", filter); + } catch (Exception e) { @@ -126,7 +133,7 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService public Set<String> checkGroupAttributes(String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { List<String> groups = Lists.newArrayList(); try { - LOGGER.info("Checking group attributes for user dn {} ...", userDn); + LOGGER.info("Checking group attributes for user dn: [{}] ...", userDn); // set up a filter based on the provided attributes String filter = FilterBuilder.and( @@ -134,7 +141,7 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService FilterBuilder.equal(ambariLdapConfiguration.groupMemberAttribute(), userDn) ).toString(); - LOGGER.info("Searching for the groups the user dn: {} is member of using the search filter: {}", userDn, filter); + LOGGER.info("Searching for the groups the user dn: [{}] is member of using the search filter: [{}]", userDn, filter); LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); // assemble a search request @@ -145,6 +152,13 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService // perform the search groups = ldapConnectionTemplate.search(searchRequest, getGroupNameEntryMapper(ambariLdapConfiguration)); + if (groups == null || groups.isEmpty()) { + LOGGER.info("No groups found for the user dn. Group attributes configuration is incomplete"); + throw new AmbariLdapException("Group attribute ldap configuration is incomplete"); + } + + LOGGER.info("Group attribute configuration check succeeded."); + } catch (Exception e) { LOGGER.error("User attributes validation failed.", e); @@ -156,6 +170,12 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService } + /** + * Entry mapper for handling user search results. + * + * @param ambariLdapConfiguration ambari ldap configuration values + * @return user dn entry mapper instance + */ private EntryMapper<String> getGroupNameEntryMapper(AmbariLdapConfiguration ambariLdapConfiguration) { EntryMapper<String> entryMapper = new EntryMapper<String>() { @@ -168,6 +188,12 @@ public class DefaultLdapConfigurationService implements LdapConfigurationService return entryMapper; } + /** + * Entry mapper for handling group searches. + * + * @param ambariLdapConfiguration ambari ldap configuration values + * @return + */ private EntryMapper<String> getUserDnNameEntryMapper(AmbariLdapConfiguration ambariLdapConfiguration) { EntryMapper<String> entryMapper = new EntryMapper<String>() { http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConnectionConfigService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConnectionConfigService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConnectionConfigService.java new file mode 100644 index 0000000..9bc2daf --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConnectionConfigService.java @@ -0,0 +1,116 @@ +/* + * Licensed 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.ambari.server.ldap.service.ads; + +import static javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm; + +import java.io.FileInputStream; +import java.security.KeyStore; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; +import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.ambari.server.ldap.service.LdapConnectionConfigService; +import org.apache.directory.api.util.Strings; +import org.apache.directory.ldap.client.api.LdapConnectionConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Singleton +public class DefaultLdapConnectionConfigService implements LdapConnectionConfigService { + + private static Logger LOG = LoggerFactory.getLogger(DefaultLdapConnectionConfigService.class); + + @Inject + public DefaultLdapConnectionConfigService() { + } + + @Override + public LdapConnectionConfig createLdapConnectionConfig(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + + LOG.debug("Assembling ldap connection config based on: {}", ambariLdapConfiguration); + + LdapConnectionConfig config = new LdapConnectionConfig(); + config.setLdapHost(ambariLdapConfiguration.serverHost()); + config.setLdapPort(ambariLdapConfiguration.serverPort()); + config.setName(ambariLdapConfiguration.bindDn()); + config.setCredentials(ambariLdapConfiguration.bindPassword()); + config.setUseSsl(ambariLdapConfiguration.useSSL()); + + // todo implement proper validation logic here: identify optional/mandatory settings + // todo suggest proper naming + if ("custom".equals(ambariLdapConfiguration.trustStore())) { + LOG.info("Using custom trust manager configuration"); + config.setTrustManagers(trustManagers(ambariLdapConfiguration)); + } + + + return config; + } + + + /** + * Configure the trustmanagers to use the custom keystore. + * + * @param ambariLdapConfiguration congiguration instance holding current values + * @return the array of trust managers + * @throws AmbariLdapException if an error occurs while setting up the connection + */ + private TrustManager[] trustManagers(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + try { + + TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(getDefaultAlgorithm()); + tmFactory.init(keyStore(ambariLdapConfiguration)); + return tmFactory.getTrustManagers(); + + } catch (Exception e) { + + LOG.error("Failed to initialize trust managers", e); + throw new AmbariLdapException(e); + + } + + } + + private KeyStore keyStore(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + + // validating configuration settings + if (Strings.isEmpty(ambariLdapConfiguration.trustStoreType())) { + throw new AmbariLdapException("Key Store Type must be specified"); + } + + if (Strings.isEmpty(ambariLdapConfiguration.trustStorePath())) { + throw new AmbariLdapException("Key Store Path must be specified"); + } + + try { + + KeyStore ks = KeyStore.getInstance(ambariLdapConfiguration.trustStoreType()); + FileInputStream fis = new FileInputStream(ambariLdapConfiguration.trustStorePath()); + ks.load(fis, ambariLdapConfiguration.trustStorePassword().toCharArray()); + return ks; + + } catch (Exception e) { + + LOG.error("Failed to create keystore", e); + throw new AmbariLdapException(e); + + } + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/DefaultLdapConnectionConfigService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/DefaultLdapConnectionConfigService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/DefaultLdapConnectionConfigService.java deleted file mode 100644 index b12cc85..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/DefaultLdapConnectionConfigService.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed 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.ambari.server.ldap.service.ads.detectors; - -import static javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm; - -import java.io.FileInputStream; -import java.security.KeyStore; - -import javax.inject.Inject; -import javax.inject.Singleton; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; - -import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; -import org.apache.ambari.server.ldap.service.AmbariLdapException; -import org.apache.ambari.server.ldap.service.LdapConnectionConfigService; -import org.apache.directory.api.util.Strings; -import org.apache.directory.ldap.client.api.LdapConnectionConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Singleton -public class DefaultLdapConnectionConfigService implements LdapConnectionConfigService { - - private static Logger LOG = LoggerFactory.getLogger(DefaultLdapConnectionConfigService.class); - - @Inject - public DefaultLdapConnectionConfigService() { - } - - @Override - public LdapConnectionConfig createLdapConnectionConfig(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - - LOG.debug("Assembling ldap connection config based on: {}", ambariLdapConfiguration); - - LdapConnectionConfig config = new LdapConnectionConfig(); - config.setLdapHost(ambariLdapConfiguration.serverHost()); - config.setLdapPort(ambariLdapConfiguration.serverPort()); - config.setName(ambariLdapConfiguration.bindDn()); - config.setCredentials(ambariLdapConfiguration.bindPassword()); - config.setUseSsl(ambariLdapConfiguration.useSSL()); - - // todo implement proper validation logic here: identify optional/mandatory settings - // todo suggest proper naming - if ("custom".equals(ambariLdapConfiguration.trustStore())) { - LOG.info("Using custom trust manager configuration"); - config.setTrustManagers(trustManagers(ambariLdapConfiguration)); - } - - - return config; - } - - - /** - * Configure the trustmanagers to use the custom keystore. - * - * @param ambariLdapConfiguration congiguration instance holding current values - * @return the array of trust managers - * @throws AmbariLdapException if an error occurs while setting up the connection - */ - private TrustManager[] trustManagers(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - try { - - TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(getDefaultAlgorithm()); - tmFactory.init(keyStore(ambariLdapConfiguration)); - return tmFactory.getTrustManagers(); - - } catch (Exception e) { - - LOG.error("Failed to initialize trust managers", e); - throw new AmbariLdapException(e); - - } - - } - - private KeyStore keyStore(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - - // validating configuration settings - if (Strings.isEmpty(ambariLdapConfiguration.trustStoreType())) { - throw new AmbariLdapException("Key Store Type must be specified"); - } - - if (Strings.isEmpty(ambariLdapConfiguration.trustStorePath())) { - throw new AmbariLdapException("Key Store Path must be specified"); - } - - try { - - KeyStore ks = KeyStore.getInstance(ambariLdapConfiguration.trustStoreType()); - FileInputStream fis = new FileInputStream(ambariLdapConfiguration.trustStorePath()); - ks.load(fis, ambariLdapConfiguration.trustStorePassword().toCharArray()); - return ks; - - } catch (Exception e) { - - LOG.error("Failed to create keystore", e); - throw new AmbariLdapException(e); - - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/test/java/org/apache/ambari/server/ldap/LdapModuleFunctionalTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/LdapModuleFunctionalTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/LdapModuleFunctionalTest.java index f3f0644..91f4e10 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/ldap/LdapModuleFunctionalTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/LdapModuleFunctionalTest.java @@ -18,7 +18,7 @@ import java.util.Map; import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; -import org.apache.ambari.server.ldap.domain.TestAmbariAmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory; import org.apache.ambari.server.ldap.service.LdapConfigurationService; import org.apache.ambari.server.ldap.service.LdapFacade; import org.apache.ambari.server.ldap.service.ads.LdapConnectionTemplateFactory; @@ -47,7 +47,7 @@ public class LdapModuleFunctionalTest { private static Injector injector; private static Module testModule; - private static TestAmbariAmbariLdapConfigurationFactory ldapConfigurationFactory = new TestAmbariAmbariLdapConfigurationFactory(); + private static TestAmbariLdapConfigurationFactory ldapConfigurationFactory = new TestAmbariLdapConfigurationFactory(); @BeforeClass public static void beforeClass() throws Exception { http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariAmbariLdapConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariAmbariLdapConfigurationFactory.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariAmbariLdapConfigurationFactory.java deleted file mode 100644 index 1155494..0000000 --- a/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariAmbariLdapConfigurationFactory.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed 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.ambari.server.ldap.domain; - - -import java.util.Map; - -/** - * Implementation used for testing purposes only! - */ -public class TestAmbariAmbariLdapConfigurationFactory implements AmbariLdapConfigurationFactory { - - @Override - public AmbariLdapConfiguration createLdapConfiguration(Map<String, Object> configuration) { - return new AmbariLdapConfiguration(configuration); - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariLdapConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariLdapConfigurationFactory.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariLdapConfigurationFactory.java new file mode 100644 index 0000000..aa26498 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/domain/TestAmbariLdapConfigurationFactory.java @@ -0,0 +1,29 @@ +/* + * Licensed 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.ambari.server.ldap.domain; + + +import java.util.Map; + +/** + * Implementation used for testing purposes only! + */ +public class TestAmbariLdapConfigurationFactory implements AmbariLdapConfigurationFactory { + + @Override + public AmbariLdapConfiguration createLdapConfiguration(Map<String, Object> configuration) { + return new AmbariLdapConfiguration(configuration); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java index 8552f86..db0e5a9 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java @@ -20,7 +20,7 @@ import java.util.Set; import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; import org.apache.ambari.server.ldap.domain.AmbariLdapConfigurationFactory; -import org.apache.ambari.server.ldap.domain.TestAmbariAmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory; import org.easymock.Capture; import org.easymock.EasyMock; import org.easymock.EasyMockRule; @@ -63,7 +63,7 @@ public class AmbariLdapFacadeTest extends EasyMockSupport { @Before public void before() { - ambariLdapConfigurationFactory = new TestAmbariAmbariLdapConfigurationFactory(); + ambariLdapConfigurationFactory = new TestAmbariLdapConfigurationFactory(); ambariLdapConfiguration = ambariLdapConfigurationFactory.createLdapConfiguration(Maps.newHashMap()); ambariLdapConfigurationCapture = Capture.newInstance(); http://git-wip-us.apache.org/repos/asf/ambari/blob/3bb03e98/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java new file mode 100644 index 0000000..4d6d2a6 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java @@ -0,0 +1,221 @@ +/* + * Licensed 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.ambari.server.ldap.service.ads; + +import java.util.Map; +import java.util.Set; + +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; +import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.ambari.server.ldap.service.LdapConfigurationService; +import org.apache.directory.api.ldap.model.message.SearchRequest; +import org.apache.directory.api.ldap.model.message.SearchRequestImpl; +import org.apache.directory.api.ldap.model.message.SearchScope; +import org.apache.directory.api.ldap.model.name.Dn; +import org.apache.directory.ldap.client.template.ConnectionCallback; +import org.apache.directory.ldap.client.template.EntryMapper; +import org.apache.directory.ldap.client.template.LdapConnectionTemplate; +import org.easymock.EasyMock; +import org.easymock.EasyMockRule; +import org.easymock.EasyMockSupport; +import org.easymock.Mock; +import org.easymock.MockType; +import org.easymock.TestSubject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class DefaultLdapConfigurationServiceTest extends EasyMockSupport { + @Rule + public EasyMockRule mocks = new EasyMockRule(this); + + @Mock(type = MockType.STRICT) + private LdapConnectionTemplateFactory ldapConnectionTemplateFactory; + + @Mock(type = MockType.STRICT) + private LdapConnectionTemplate ldapConnectionTemplateMock; + + + @TestSubject + private LdapConfigurationService ldapConfigurationService = new DefaultLdapConfigurationService(); + + @Before + public void before() { + resetAll(); + } + + @Test + public void testShouldConnectionCheckSucceedWhenConnectionCallbackSucceeds() throws Exception { + // GIVEN + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(Maps.newHashMap()); + + // the cllback returns TRUE + EasyMock.expect(ldapConnectionTemplateMock.execute(EasyMock.anyObject(ConnectionCallback.class))).andReturn(Boolean.TRUE); + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + + replayAll(); + // WHEN + ldapConfigurationService.checkConnection(ambariLdapConfiguration); + + // THEN + // no exceptions are thrown + + } + + @Test(expected = AmbariLdapException.class) + public void testShouldConnectionCheckFailWhenConnectionCallbackFails() throws Exception { + + // GIVEN + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(Maps.newHashMap()); + + // the callback returns FALSE + EasyMock.expect(ldapConnectionTemplateMock.execute(EasyMock.anyObject(ConnectionCallback.class))).andReturn(Boolean.FALSE); + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + + replayAll(); + // WHEN + ldapConfigurationService.checkConnection(ambariLdapConfiguration); + + // THEN + // exception is thrown + + } + + @Test + public void testShouldUserAttributeConfigurationCheckSucceedWhenUserDnIsFound() throws Exception { + // GIVEN + Map<String, Object> configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.USER_OBJECT_CLASS.key(), "person"); + configMap.put(AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE.key(), "uid"); + + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(configMap); + + // the callback returns FALSE + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + // users found with dn + EasyMock.expect(ldapConnectionTemplateMock.searchFirst(EasyMock.anyObject(Dn.class), EasyMock.anyString(), EasyMock.anyObject(SearchScope.class), + EasyMock.anyObject(EntryMapper.class))).andReturn("dn"); + + replayAll(); + // WHEN + String userDn = ldapConfigurationService.checkUserAttributes("testUser", "testPassword", ambariLdapConfiguration); + + // THEN + Assert.assertEquals("The found userDn is not the expected one", userDn, "dn"); + + } + + @Test(expected = AmbariLdapException.class) + public void testShouldUserAttributeConfigurationCheckFailWhenNoUsersFound() throws Exception { + // GIVEN + Map<String, Object> configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.USER_OBJECT_CLASS.key(), "posixAccount"); + configMap.put(AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE.key(), "dn"); + + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(configMap); + + // the callback returns FALSE + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + + // no users found, the returned dn is null + EasyMock.expect(ldapConnectionTemplateMock.searchFirst(EasyMock.anyObject(Dn.class), EasyMock.anyString(), + EasyMock.anyObject(SearchScope.class), + EasyMock.anyObject(EntryMapper.class))).andReturn(null); + + replayAll(); + // WHEN + String userDn = ldapConfigurationService.checkUserAttributes("testUser", "testPassword", + ambariLdapConfiguration); + + // THEN + Assert.assertEquals("The found userDn is not the expected one", userDn, "dn"); + + } + + + @Test + public void testShouldGroupAttributeConfigurationCheckSucceedWhenGroupForUserDnIsFound() throws Exception { + // GIVEN + + Map<String, Object> configMap = groupConfigObjectMap(); + + SearchRequest sr = new SearchRequestImpl(); + + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(configMap); + + // the callback returns FALSE + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + + EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyObject(Dn.class), EasyMock.anyString(), + EasyMock.anyObject(SearchScope.class))).andReturn(sr); + + EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(EntryMapper.class))) + .andReturn(Lists.newArrayList("userGroup")); + + replayAll(); + // WHEN + Set<String> userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); + + // THEN + Assert.assertNotNull("No groups found", userGroups); + + } + + + @Test(expected = AmbariLdapException.class) + public void testShouldGroupAttributeConfigurationCheckFailWhenNoGroupsForUserDnFound() throws Exception { + // GIVEN + + Map<String, Object> configMap = groupConfigObjectMap(); + + SearchRequest sr = new SearchRequestImpl(); + + AmbariLdapConfiguration ambariLdapConfiguration = new TestAmbariLdapConfigurationFactory().createLdapConfiguration(configMap); + + // the callback returns FALSE + EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); + + EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyObject(Dn.class), EasyMock.anyString(), + EasyMock.anyObject(SearchScope.class))).andReturn(sr); + + EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(EntryMapper.class))) + .andReturn(Lists.newArrayList()); + + replayAll(); + // WHEN + Set<String> userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); + + // THEN + Assert.assertNotNull("No groups found", userGroups); + + } + + private Map<String, Object> groupConfigObjectMap() { + Map<String, Object> configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.GROUP_OBJECT_CLASS.key(), "groupOfNames"); + configMap.put(AmbariLdapConfigKeys.GROUP_SEARCH_BASE.key(), "dc=example,dc=com"); + configMap.put(AmbariLdapConfigKeys.GROUP_NAME_ATTRIBUTE.key(), "uid"); + configMap.put(AmbariLdapConfigKeys.GROUP_MEMBER_ATTRIBUTE.key(), "member"); + return configMap; + } + + +} \ No newline at end of file