Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E1A52200D30 for ; Mon, 30 Oct 2017 23:35:35 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id E0937160C0D; Mon, 30 Oct 2017 22:35:35 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B6F91160BFC for ; Mon, 30 Oct 2017 23:35:33 +0100 (CET) Received: (qmail 30527 invoked by uid 500); 30 Oct 2017 22:35:31 -0000 Mailing-List: contact commits-help@ambari.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ambari-dev@ambari.apache.org Delivered-To: mailing list commits@ambari.apache.org Received: (qmail 30285 invoked by uid 99); 30 Oct 2017 22:35:31 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Oct 2017 22:35:31 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AA36AE08A1; Mon, 30 Oct 2017 22:35:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mradhakrishnan@apache.org To: commits@ambari.apache.org Date: Mon, 30 Oct 2017 22:36:15 -0000 Message-Id: <1c140921d9ec4ec4a25a6f49758c949a@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [49/56] [abbrv] ambari git commit: AMBARI-21307 Feature for supporting LDAP configuration from the UI archived-at: Mon, 30 Oct 2017 22:35:36 -0000 http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/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 new file mode 100644 index 0000000..db0e5a9 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java @@ -0,0 +1,215 @@ +/* + * 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; + +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.AmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory; +import org.easymock.Capture; +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.Maps; +import com.google.common.collect.Sets; + +/** + * Unit test suite for the LdapFacade operations. + */ +public class AmbariLdapFacadeTest extends EasyMockSupport { + + @Rule + public EasyMockRule mocks = new EasyMockRule(this); + + @Mock(type = MockType.STRICT) + public LdapConfigurationService ldapConfigurationServiceMock; + + @Mock(type = MockType.STRICT) + public LdapAttributeDetectionService ldapAttributeDetectionServiceMock; + + private AmbariLdapConfigurationFactory ambariLdapConfigurationFactory; + + + @TestSubject + private LdapFacade ldapFacade = new AmbariLdapFacade(); + + private AmbariLdapConfiguration ambariLdapConfiguration; + + + private Capture ambariLdapConfigurationCapture; + + @Before + public void before() { + ambariLdapConfigurationFactory = new TestAmbariLdapConfigurationFactory(); + ambariLdapConfiguration = ambariLdapConfigurationFactory.createLdapConfiguration(Maps.newHashMap()); + ambariLdapConfigurationCapture = Capture.newInstance(); + + + resetAll(); + } + + /** + * Tests whether the facade method call delegates to the proper service call. + * The thest is success if the same instance is passed to the service. + * + * @throws Exception + */ + @Test + public void testShouldConfigurationCheckDelegateToTheRightServiceCall() throws Exception { + // GIVEN + // the mocks are set up + ldapConfigurationServiceMock.checkConnection(EasyMock.capture(ambariLdapConfigurationCapture)); + replayAll(); + // WHEN + // the facade method is called + ldapFacade.checkConnection(ambariLdapConfiguration); + + // THEN + // the captured configuration instance is the same the facade method got called with + Assert.assertEquals("The configuration instance souldn't change before passing it to the service", + ambariLdapConfiguration, ambariLdapConfigurationCapture.getValue()); + } + + @Test(expected = AmbariLdapException.class) + public void testShouldConfigurationCheckFailureResultInAmbariLdapException() throws Exception { + // GIVEN + ldapConfigurationServiceMock.checkConnection(EasyMock.anyObject(AmbariLdapConfiguration.class)); + EasyMock.expectLastCall().andThrow(new AmbariLdapException("Testing ...")); + replayAll(); + + // WHEN + ldapFacade.checkConnection(ambariLdapConfiguration); + + // THEN + // exception is thrown + + } + + @Test + public void testShouldLdapAttributesCheckDelegateToTheRightServiceCalls() throws Exception { + // GIVEN + + Map parameters = Maps.newHashMap(); + parameters.put(AmbariLdapFacade.Parameters.TEST_USER_NAME.getParameterKey(), "testUser"); + parameters.put(AmbariLdapFacade.Parameters.TEST_USER_PASSWORD.getParameterKey(), "testPassword"); + + + Capture testUserCapture = Capture.newInstance(); + Capture testPasswordCapture = Capture.newInstance(); + Capture userDnCapture = Capture.newInstance(); + + EasyMock.expect(ldapConfigurationServiceMock.checkUserAttributes(EasyMock.capture(testUserCapture), EasyMock.capture(testPasswordCapture), + EasyMock.capture(ambariLdapConfigurationCapture))).andReturn("userDn"); + + EasyMock.expect(ldapConfigurationServiceMock.checkGroupAttributes(EasyMock.capture(userDnCapture), + EasyMock.capture(ambariLdapConfigurationCapture))).andReturn(Sets.newHashSet("userGroup")); + + replayAll(); + + // WHEN + Set testUserGroups = ldapFacade.checkLdapAttributes(parameters, ambariLdapConfiguration); + + // THEN + Assert.assertEquals("testUser", testUserCapture.getValue()); + Assert.assertEquals("testPassword", testPasswordCapture.getValue()); + Assert.assertEquals("userDn", userDnCapture.getValue()); + + Assert.assertTrue(testUserGroups.contains("userGroup")); + + } + + @Test(expected = AmbariLdapException.class) + public void testShouldAttributeCheckFailuresResultInAmbariLdapException() throws Exception { + // GIVEN + Map parameters = Maps.newHashMap(); + parameters.put(AmbariLdapFacade.Parameters.TEST_USER_NAME.getParameterKey(), "testUser"); + parameters.put(AmbariLdapFacade.Parameters.TEST_USER_PASSWORD.getParameterKey(), "testPassword"); + + EasyMock.expect(ldapConfigurationServiceMock.checkUserAttributes(EasyMock.anyString(), EasyMock.anyString(), + EasyMock.anyObject(AmbariLdapConfiguration.class))).andThrow(new AmbariLdapException("Testing ...")); + + replayAll(); + + // WHEN + Set testUserGroups = ldapFacade.checkLdapAttributes(parameters, ambariLdapConfiguration); + // THEN + // Exception is thrown + } + + @Test + public void testShouldLdapAttributeDetectionDelegateToTheRightServiceCalls() throws Exception { + + // configuration map with user attributes detected + Map userConfigMap = Maps.newHashMap(); + userConfigMap.put(AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE.key(), "uid"); + AmbariLdapConfiguration userAttrDecoratedConfig = ambariLdapConfigurationFactory.createLdapConfiguration(userConfigMap); + + // configuration map with user+group attributes detected + Map groupConfigMap = Maps.newHashMap(userConfigMap); + groupConfigMap.put(AmbariLdapConfigKeys.GROUP_NAME_ATTRIBUTE.key(), "dn"); + AmbariLdapConfiguration groupAttrDecoratedConfig = ambariLdapConfigurationFactory.createLdapConfiguration(groupConfigMap); + + Capture userAttrDetectionConfigCapture = Capture.newInstance(); + Capture groupAttrDetectionConfigCapture = Capture.newInstance(); + + // GIVEN + EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapUserAttributes(EasyMock.capture(userAttrDetectionConfigCapture))) + .andReturn(userAttrDecoratedConfig); + + EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapGroupAttributes(EasyMock.capture(groupAttrDetectionConfigCapture))) + .andReturn(groupAttrDecoratedConfig); + + replayAll(); + + // WHEN + AmbariLdapConfiguration detected = ldapFacade.detectAttributes(ambariLdapConfiguration); + + // THEN + Assert.assertEquals("User attribute detection called with the wrong configuration", ambariLdapConfiguration, + userAttrDetectionConfigCapture.getValue()); + + Assert.assertEquals("Group attribute detection called with the wrong configuration", userAttrDecoratedConfig, + groupAttrDetectionConfigCapture.getValue()); + + Assert.assertEquals("Attribute detection returned an invalid configuration", groupAttrDecoratedConfig, detected); + + } + + @Test(expected = AmbariLdapException.class) + public void testShouldAttributeDetectionFailuresResultInAmbariLdapException() throws Exception { + // GIVEN + EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapUserAttributes(EasyMock.anyObject(AmbariLdapConfiguration.class))) + .andThrow(new AmbariLdapException("Testing ...")); + + replayAll(); + + // WHEN + ldapFacade.detectAttributes(ambariLdapConfiguration); + + // THEN + // Exception is thrown + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java new file mode 100644 index 0000000..09dea1c --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java @@ -0,0 +1,188 @@ +/* + * 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.List; +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.AmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory; +import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.ambari.server.ldap.service.ads.detectors.AttributeDetectorFactory; +import org.apache.ambari.server.ldap.service.ads.detectors.ChainedAttributeDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector; +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.apache.directory.api.ldap.model.exception.LdapException; +import org.apache.directory.api.ldap.model.message.SearchRequest; +import org.apache.directory.api.ldap.model.message.SearchScope; +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.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; +import com.google.common.collect.Sets; + +public class DefaultLdapAttributeDetectionServiceTest extends EasyMockSupport { + @Rule + public EasyMockRule mocks = new EasyMockRule(this); + + @Mock + private AttributeDetectorFactory attributeDetectorFactoryMock; + + @Mock + private LdapConnectionTemplateFactory ldapConnectionTemplateFactoryMock; + + @Mock + private LdapConnectionTemplate ldapConnectionTemplateMock; + + @Mock + private SearchRequest searchRequestMock; + + + private AmbariLdapConfigurationFactory ldapConfigurationFactory = new TestAmbariLdapConfigurationFactory(); + + @TestSubject + private DefaultLdapAttributeDetectionService defaultLdapAttributeDetectionService = new DefaultLdapAttributeDetectionService(); + + @Before + public void before() { + resetAll(); + } + + @Test + @SuppressWarnings("unchecked") + public void shouldLdapUserAttributeDetection() throws Exception { + // GIVEN + Map configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.USER_SEARCH_BASE.key(), "dc=example,dc=com"); + AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap); + + List entryList = Lists.newArrayList(new DefaultEntry("uid=gauss")); + + EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andReturn(ldapConnectionTemplateMock); + + EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(entryMapperMock().getClass()))) + .andReturn(entryList); + + EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyString(), EasyMock.anyString(), + EasyMock.anyObject(SearchScope.class))).andReturn(searchRequestMock); + + EasyMock.expect(attributeDetectorFactoryMock.userAttributDetector()) + .andReturn(new ChainedAttributeDetector(Sets.newHashSet(new UserNameAttrDetector()))); + + EasyMock.expect(searchRequestMock.setSizeLimit(50)).andReturn(searchRequestMock); + + // WHEN + replayAll(); + AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration); + + // THEN + Assert.assertNotNull(decorated); + Assert.assertEquals("N/A", ldapConfiguration.userNameAttribute()); + } + + @Test(expected = AmbariLdapException.class) + public void testShouldUserAttributeDetectionFailWhenLdapOerationFails() throws Exception { + // GIVEN + Map configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.USER_SEARCH_BASE.key(), "dc=example,dc=com"); + AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap); + + EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andThrow(new AmbariLdapException("Testing ...")); + + // WHEN + replayAll(); + AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration); + + // THEN + // exception is thrown + + } + + + @Test + @SuppressWarnings("unchecked") + public void shouldLdapGroupAttributeDetection() throws Exception { + // GIVEN + Map configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.GROUP_SEARCH_BASE.key(), "dc=example,dc=com"); + AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap); + + List entryList = Lists.newArrayList(new DefaultEntry("uid=gauss")); + + EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andReturn(ldapConnectionTemplateMock); + + EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(entryMapperMock().getClass()))) + .andReturn(entryList); + + EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyString(), EasyMock.anyString(), + EasyMock.anyObject(SearchScope.class))).andReturn(searchRequestMock); + + EasyMock.expect(attributeDetectorFactoryMock.groupAttributeDetector()) + .andReturn(new ChainedAttributeDetector(Sets.newHashSet(new GroupMemberAttrDetector()))); + + EasyMock.expect(searchRequestMock.setSizeLimit(50)).andReturn(searchRequestMock); + + // WHEN + replayAll(); + AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapGroupAttributes(ldapConfiguration); + + // THEN + Assert.assertNotNull(decorated); + Assert.assertEquals("N/A", ldapConfiguration.groupMemberAttribute()); + } + + @Test(expected = AmbariLdapException.class) + public void testShouldGroupAttributeDetectionFailWhenLdapOerationFails() throws Exception { + // GIVEN + Map configMap = Maps.newHashMap(); + configMap.put(AmbariLdapConfigKeys.GROUP_SEARCH_BASE.key(), "dc=example,dc=com"); + AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap); + + EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andThrow(new AmbariLdapException("Testing ...")); + + // WHEN + replayAll(); + AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapGroupAttributes(ldapConfiguration); + + // THEN + // exception is thrown + + } + + + private EntryMapper entryMapperMock() { + return new EntryMapper() { + @Override + public Entry map(Entry entry) throws LdapException { + return null; + } + }; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/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 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 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 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 userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); + + // THEN + Assert.assertNotNull("No groups found", userGroups); + + } + + + @Test(expected = AmbariLdapException.class) + public void testShouldGroupAttributeConfigurationCheckFailWhenNoGroupsForUserDnFound() throws Exception { + // GIVEN + + Map 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 userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); + + // THEN + Assert.assertNotNull("No groups found", userGroups); + + } + + private Map groupConfigObjectMap() { + Map 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 http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetectorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetectorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetectorTest.java new file mode 100644 index 0000000..79af467 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetectorTest.java @@ -0,0 +1,107 @@ +/* + * 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 java.util.List; +import java.util.Map; + +import org.apache.directory.api.ldap.model.entry.DefaultAttribute; +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.apache.directory.api.ldap.model.entry.StringValue; +import org.easymock.TestSubject; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; + +/** + * Test suite for the attribute detector implementation + */ +public class GroupMemberAttrDetectorTest { + + private static final Logger LOG = LoggerFactory.getLogger(GroupMemberAttrDetector.class); + + @TestSubject + GroupMemberAttrDetector groupMemberAttrDetector = new GroupMemberAttrDetector(); + + @Test + public void testShouldDetectAttributeBasedOnOccurrence() throws Exception { + // GIVEN + // Mimic a sample set of entries where group membership attributes are different + List sampleEntryList = Lists.newArrayList(); + + sampleEntryList.addAll(getSampleEntryList(GroupMemberAttrDetector.GroupMemberAttr.MEMBER_UID, 2)); + + // this is the expected property to be detected as in the sample set the most entries have it + sampleEntryList.addAll(getSampleEntryList(GroupMemberAttrDetector.GroupMemberAttr.UNIQUE_MEMBER, 7)); + sampleEntryList.addAll(getSampleEntryList(GroupMemberAttrDetector.GroupMemberAttr.MEMBER, 5)); + + // WHEN + for (Entry entry : sampleEntryList) { + groupMemberAttrDetector.collect(entry); + } + + // The most frequently encountered attribute will be selected + Map detectedAttributeMap = groupMemberAttrDetector.detect(); + + // THEN + Assert.assertEquals(1, detectedAttributeMap.size()); + Map.Entry selectedEntry = detectedAttributeMap.entrySet().iterator().next(); + + Assert.assertEquals("The selected configuration property is not the expected one", groupMemberAttrDetector.detectedProperty(), selectedEntry.getKey()); + Assert.assertEquals("The selected configuration property value is not the expected one", GroupMemberAttrDetector.GroupMemberAttr.UNIQUE_MEMBER.attrName(), selectedEntry.getValue()); + + + } + + @Test + public void testShouldDetectorPassWhenEmptySampleSetProvided() throws Exception { + // GIVEN + List sampleEntryList = Lists.newArrayList(); + + // WHEN + // WHEN + for (Entry entry : sampleEntryList) { + groupMemberAttrDetector.collect(entry); + } + + Map detectedAttributeMap = groupMemberAttrDetector.detect(); + // THEN + Assert.assertEquals(1, detectedAttributeMap.size()); + Map.Entry selectedEntry = detectedAttributeMap.entrySet().iterator().next(); + + Assert.assertEquals("The selected configuration property is not the expected one", groupMemberAttrDetector.detectedProperty(), selectedEntry.getKey()); + Assert.assertEquals("The selected configuration property value is not the expected one", "N/A", selectedEntry.getValue()); + + } + + private List getSampleEntryList(GroupMemberAttrDetector.GroupMemberAttr member, int count) { + List entryList = Lists.newArrayList(); + for (int i = 0; i < count; i++) { + Entry entry = new DefaultEntry(); + try { + entry.setDn("dn=" + member.name() + "-" + i); + entry.add(new DefaultAttribute(member.attrName(), new StringValue("xxx"))); + entryList.add(entry); + } catch (Exception e) { + LOG.error(e.getMessage()); + } + } + return entryList; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java index 382799c..d34d732 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java @@ -23,6 +23,7 @@ import java.util.Properties; import org.apache.ambari.server.audit.AuditLoggerModule; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.ControllerModule; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.notifications.dispatchers.EmailDispatcher; import org.apache.ambari.server.notifications.dispatchers.SNMPDispatcher; import org.junit.Assert; @@ -55,7 +56,7 @@ public class DispatchFactoryTest { properties.setProperty(Configuration.SHARED_RESOURCES_DIR.getKey(),sourceResourceDirectory); properties.setProperty(Configuration.ALERTS_SNMP_DISPATCH_UDP_PORT.getKey(),snmpPort.toString()); - Injector injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties)); + Injector injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties), new LdapModule()); DispatchFactory dispatchFactory = injector.getInstance(DispatchFactory.class); DispatchFactory dispatchFactory2 = injector.getInstance(DispatchFactory.class); http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java index 434a2a1..ebc2596 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.ambari.server.audit.AuditLogger; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.ControllerModule; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.stack.StackManager; import org.apache.ambari.server.stack.StackManagerFactory; import org.apache.ambari.server.stack.StackManagerMock; @@ -122,6 +123,7 @@ public class InMemoryDefaultTestModule extends AbstractModule { } try { + install(new LdapModule()); install(Modules.override(new BeanDefinitionsCachingTestControllerModule(properties)).with(new AbstractModule() { @Override protected void configure() { http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/orm/JdbcPropertyTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/JdbcPropertyTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/JdbcPropertyTest.java index 427cede..14c5dd6 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/JdbcPropertyTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/JdbcPropertyTest.java @@ -23,6 +23,7 @@ import org.apache.ambari.server.H2DatabaseCleaner; import org.apache.ambari.server.audit.AuditLoggerModule; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.ControllerModule; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.state.Clusters; import org.junit.After; import org.junit.Assert; @@ -53,7 +54,7 @@ public class JdbcPropertyTest { @Test public void testNormal() throws Exception { - injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties)); + injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties), new LdapModule()); injector.getInstance(GuiceJpaInitializer.class); injector.getInstance(Clusters.class); @@ -62,7 +63,7 @@ public class JdbcPropertyTest { @Test public void testJdbcProperty() throws Exception { properties.setProperty(Configuration.SERVER_JDBC_PROPERTIES_PREFIX + "shutdown", "true"); - injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties)); + injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties), new LdapModule()); injector.getInstance(GuiceJpaInitializer.class); try { injector.getInstance(Clusters.class); http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java index 442414f..566d6b7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderForDNWithSpaceTest.java @@ -26,6 +26,7 @@ import org.apache.ambari.server.H2DatabaseCleaner; import org.apache.ambari.server.audit.AuditLoggerModule; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.ControllerModule; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.dao.UserDAO; import org.apache.ambari.server.security.ClientSecurityType; @@ -49,23 +50,23 @@ import com.google.inject.Injector; @RunWith(FrameworkRunner.class) @CreateDS(allowAnonAccess = true, - name = "AmbariLdapAuthenticationProviderForDNWithSpaceTest", - partitions = { - @CreatePartition(name = "Root", - suffix = "dc=the apache,dc=org", - contextEntry = @ContextEntry( - entryLdif = - "dn: dc=the apache,dc=org\n" + - "dc: the apache\n" + - "objectClass: top\n" + - "objectClass: domain\n\n" + - "dn: dc=ambari,dc=the apache,dc=org\n" + - "dc: ambari\n" + - "objectClass: top\n" + - "objectClass: domain\n\n")) - }) + name = "AmbariLdapAuthenticationProviderForDNWithSpaceTest", + partitions = { + @CreatePartition(name = "Root", + suffix = "dc=the apache,dc=org", + contextEntry = @ContextEntry( + entryLdif = + "dn: dc=the apache,dc=org\n" + + "dc: the apache\n" + + "objectClass: top\n" + + "objectClass: domain\n\n" + + "dn: dc=ambari,dc=the apache,dc=org\n" + + "dc: ambari\n" + + "objectClass: top\n" + + "objectClass: domain\n\n")) + }) @CreateLdapServer(allowAnonymousAccess = true, - transports = {@CreateTransport(protocol = "LDAP")}) + transports = {@CreateTransport(protocol = "LDAP")}) @ApplyLdifFiles("users_for_dn_with_space.ldif") public class AmbariLdapAuthenticationProviderForDNWithSpaceTest extends AmbariLdapAuthenticationProviderBaseTest { @@ -83,7 +84,7 @@ public class AmbariLdapAuthenticationProviderForDNWithSpaceTest extends AmbariLd @Before public void setUp() throws Exception { - injector = Guice.createInjector(new ControllerModule(getTestProperties()), new AuditLoggerModule()); + injector = Guice.createInjector(new ControllerModule(getTestProperties()), new AuditLoggerModule(), new LdapModule()); injector.getInstance(GuiceJpaInitializer.class); injector.injectMembers(this); http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java index 4941bc7..d8be809 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLdapAuthenticationProviderTest.java @@ -29,6 +29,7 @@ import static org.junit.Assert.fail; import org.apache.ambari.server.H2DatabaseCleaner; import org.apache.ambari.server.audit.AuditLoggerModule; import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.dao.UserDAO; import org.apache.ambari.server.orm.entities.UserEntity; @@ -90,7 +91,7 @@ public class AmbariLdapAuthenticationProviderTest extends AmbariLdapAuthenticati @Before public void setUp() { - injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule()); + injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule(), new LdapModule()); injector.injectMembers(this); injector.getInstance(GuiceJpaInitializer.class); configuration.setClientSecurityType(ClientSecurityType.LDAP); http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java index 2362823..d889372 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/AmbariLocalUserProviderTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import org.apache.ambari.server.H2DatabaseCleaner; import org.apache.ambari.server.audit.AuditLoggerModule; +import org.apache.ambari.server.ldap.LdapModule; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.OrmTestHelper; import org.apache.ambari.server.orm.dao.UserDAO; @@ -56,7 +57,7 @@ public class AmbariLocalUserProviderTest { @BeforeClass public static void prepareData() { - injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule()); + injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule(), new LdapModule()); injector.getInstance(GuiceJpaInitializer.class); injector.getInstance(OrmTestHelper.class).createTestUsers(); } http://git-wip-us.apache.org/repos/asf/ambari/blob/6b7a7a70/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/LdapServerPropertiesTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/LdapServerPropertiesTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/LdapServerPropertiesTest.java index 5747408..0e1515b 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/LdapServerPropertiesTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authorization/LdapServerPropertiesTest.java @@ -25,6 +25,7 @@ import java.util.List; import org.apache.ambari.server.audit.AuditLoggerModule; import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.ldap.LdapModule; import org.junit.Before; import org.junit.Test; @@ -45,13 +46,13 @@ public class LdapServerPropertiesTest { Configuration configuration; public LdapServerPropertiesTest() { - injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule()); + injector = Guice.createInjector(new AuditLoggerModule(), new AuthorizationTestModule(), new LdapModule()); injector.injectMembers(this); } @Before public void setUp() throws Exception { - ldapServerProperties = new LdapServerProperties(); + ldapServerProperties = new LdapServerProperties(); ldapServerProperties.setAnonymousBind(true); ldapServerProperties.setBaseDN("dc=ambari,dc=apache,dc=org"); ldapServerProperties.setManagerDn("uid=manager," + ldapServerProperties.getBaseDN());