AMBARI-21307 Improved the attribute detection implementation (using detector chains, added logging, improved guice setup).
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3750daf3 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3750daf3 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3750daf3 Branch: refs/heads/feature-branch-AMBARI-21307 Commit: 3750daf39381e9a995fbbeb9f8a4665661ef630c Parents: d5f8cc6 Author: lpuskas <lpus...@apache.org> Authored: Mon Oct 16 18:24:32 2017 +0200 Committer: lpuskas <lpus...@apache.org> Committed: Tue Oct 17 18:41:14 2017 +0200 ---------------------------------------------------------------------- .../apache/ambari/server/ldap/LdapModule.java | 32 +++++++++ .../ldap/domain/AmbariLdapConfigKeys.java | 11 +++ .../server/ldap/service/AttributeDetector.java | 6 +- .../DefaultLdapAttributeDetectionService.java | 70 +++++++++----------- .../ads/detectors/AttributeDetectorFactory.java | 52 +++++++++++++++ .../ads/detectors/ChainedAttributeDetector.java | 59 +++++++++++++++++ .../ads/detectors/GroupMemberAttrDetector.java | 6 ++ .../ads/detectors/GroupNameAttrDetector.java | 6 ++ .../ads/detectors/GroupObjectClassDetector.java | 6 ++ .../OccurrenceAndWeightBasedDetector.java | 10 ++- .../detectors/UserGroupMemberAttrDetector.java | 6 ++ .../ads/detectors/UserNameAttrDetector.java | 8 +++ .../ads/detectors/UserObjectClassDetector.java | 6 ++ .../server/ldap/LdapModuleFunctionalTest.java | 20 ++++++ 14 files changed, 255 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/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 67e84dc..089da1d 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 @@ -19,6 +19,7 @@ import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; import org.apache.ambari.server.ldap.domain.AmbariLdapConfigurationFactory; import org.apache.ambari.server.ldap.service.AmbariLdapConfigurationProvider; import org.apache.ambari.server.ldap.service.AmbariLdapFacade; +import org.apache.ambari.server.ldap.service.AttributeDetector; import org.apache.ambari.server.ldap.service.LdapAttributeDetectionService; import org.apache.ambari.server.ldap.service.LdapConfigurationService; import org.apache.ambari.server.ldap.service.LdapConnectionConfigService; @@ -26,15 +27,27 @@ 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.DefaultLdapConnectionConfigService; +import org.apache.ambari.server.ldap.service.ads.detectors.AttributeDetectorFactory; +import org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.GroupNameAttrDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.GroupObjectClassDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.UserGroupMemberAttrDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.UserObjectClassDetector; import com.google.inject.AbstractModule; import com.google.inject.assistedinject.FactoryModuleBuilder; +import com.google.inject.multibindings.Multibinder; +import com.google.inject.name.Names; /** * GUICE configuration module for setting up LDAP related infrastructure. */ public class LdapModule extends AbstractModule { + public static final String USER_ATTRIBUTES_DETECTORS = "UserAttributesDetectors"; + public static final String GROUP_ATTRIBUTES_DETECTORS = "GroupAttributesDetectors"; + @Override protected void configure() { bind(LdapFacade.class).to(AmbariLdapFacade.class); @@ -45,6 +58,25 @@ public class LdapModule extends AbstractModule { // this binding requires the JPA module! bind(AmbariLdapConfiguration.class).toProvider(AmbariLdapConfigurationProvider.class); + bind(AttributeDetectorFactory.class); + install(new FactoryModuleBuilder().build(AmbariLdapConfigurationFactory.class)); + + // binding the set of user attributes detector + Multibinder<AttributeDetector> userAttributeDetectorBinder = Multibinder.newSetBinder(binder(), AttributeDetector.class, + Names.named(USER_ATTRIBUTES_DETECTORS)); + userAttributeDetectorBinder.addBinding().to(UserObjectClassDetector.class); + userAttributeDetectorBinder.addBinding().to(UserNameAttrDetector.class); + userAttributeDetectorBinder.addBinding().to(UserGroupMemberAttrDetector.class); + + + // binding the set of group attributes detector + Multibinder<AttributeDetector> groupAttributeDetectorBinder = Multibinder.newSetBinder(binder(), AttributeDetector.class, + Names.named(GROUP_ATTRIBUTES_DETECTORS)); + groupAttributeDetectorBinder.addBinding().to(GroupObjectClassDetector.class); + groupAttributeDetectorBinder.addBinding().to(GroupNameAttrDetector.class); + groupAttributeDetectorBinder.addBinding().to(GroupMemberAttrDetector.class); + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfigKeys.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfigKeys.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfigKeys.java index b7b41a3..da655ad 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfigKeys.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfigKeys.java @@ -69,4 +69,15 @@ public enum AmbariLdapConfigKeys { public String key() { return this.propertyName; } + + public static AmbariLdapConfigKeys fromKeyStr(String keyStr) { + for (AmbariLdapConfigKeys key : values()) { + if (key.key().equals(keyStr)) { + return key; + } + } + + throw new IllegalStateException("invalid konfiguration key found!"); + + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java index c694b17..f39a1fd 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java @@ -14,6 +14,8 @@ package org.apache.ambari.server.ldap.service; +import java.util.Map; + /** * Operations for detecting LDAP related settings. * The basis for the attribute or value detection is a set of entries returned by a search operation. @@ -31,9 +33,9 @@ public interface AttributeDetector<T> { /** * Implements the decision based on which the "best" possible attribute or value is selected. * - * @return the most probable attribute name or value (based on the logic in the implementer) + * @return a map of the form <property-key, detected-value> */ - String detect(); + Map<String, String> detect(); } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java index 204c46a..ea6e278 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java @@ -15,6 +15,7 @@ package org.apache.ambari.server.ldap.service.ads; import java.util.List; +import java.util.Map; import javax.inject.Inject; import javax.inject.Singleton; @@ -22,13 +23,9 @@ import javax.inject.Singleton; import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration; import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.ambari.server.ldap.service.AttributeDetector; import org.apache.ambari.server.ldap.service.LdapAttributeDetectionService; -import org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector; -import org.apache.ambari.server.ldap.service.ads.detectors.GroupNameAttrDetector; -import org.apache.ambari.server.ldap.service.ads.detectors.GroupObjectClassDetector; -import org.apache.ambari.server.ldap.service.ads.detectors.UserGroupMemberAttrDetector; -import org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector; -import org.apache.ambari.server.ldap.service.ads.detectors.UserObjectClassDetector; +import org.apache.ambari.server.ldap.service.ads.detectors.AttributeDetectorFactory; 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; @@ -47,22 +44,7 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect private static final int SAMPLE_RESULT_SIZE = 50; @Inject - private UserNameAttrDetector userNameAttrDetector; - - @Inject - private UserObjectClassDetector userObjectClassDetector; - - @Inject - private UserGroupMemberAttrDetector userGroupMemberAttrDetector; - - @Inject - private GroupNameAttrDetector groupNameAttrDetector; - - @Inject - private GroupObjectClassDetector groupObjectClassDetector; - - @Inject - private GroupMemberAttrDetector groupMemberAttrDetector; + private AttributeDetectorFactory attributeDetectorFactory; @Inject private LdapConnectionTemplateFactory ldapConnectionTemplateFactory; @@ -76,7 +58,7 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect public AmbariLdapConfiguration detectLdapUserAttributes(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { LOGGER.info("Detecting LDAP user attributes ..."); LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); - + AttributeDetector<Entry> userAttributDetector = attributeDetectorFactory.userAttributDetector(); // perform a search using the user search base if (Strings.isEmpty(ambariLdapConfiguration.userSearchBase())) { @@ -92,23 +74,23 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect List<Entry> entries = ldapConnectionTemplate.search(searchRequest, getEntryMapper()); for (Entry entry : entries) { - LOGGER.info("Processing sample entry with dn: [{}]", entry.getDn()); - - userNameAttrDetector.collect(entry); - userObjectClassDetector.collect(entry); - userGroupMemberAttrDetector.collect(entry); - + userAttributDetector.collect(entry); } - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE, userNameAttrDetector.detect()); - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.USER_OBJECT_CLASS, userObjectClassDetector.detect()); - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.USER_GROUP_MEMBER_ATTRIBUTE, userGroupMemberAttrDetector.detect()); + // select attributes based on the collected information + Map<String, String> detectedUserAttributes = userAttributDetector.detect(); + + // setting the attributes into the configuration + setDetectedAttributes(ambariLdapConfiguration, detectedUserAttributes); LOGGER.info("Decorated ambari ldap config : [{}]", ambariLdapConfiguration); } catch (Exception e) { + LOGGER.error("Ldap operation failed", e); + throw new AmbariLdapException(e); + } return ambariLdapConfiguration; @@ -126,6 +108,7 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect } LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); + AttributeDetector<Entry> groupAttributDetector = attributeDetectorFactory.groupAttributDetector(); try { @@ -134,30 +117,39 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect // do the search List<Entry> groupEntries = ldapConnectionTemplate.search(searchRequest, getEntryMapper()); - for (Entry groupEntry : groupEntries) { LOGGER.info("Processing sample entry with dn: [{}]", groupEntry.getDn()); - groupNameAttrDetector.collect(groupEntry); - groupObjectClassDetector.collect(groupEntry); - groupMemberAttrDetector.collect(groupEntry); + groupAttributDetector.collect(groupEntry); } - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.GROUP_NAME_ATTRIBUTE, groupNameAttrDetector.detect()); - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.GROUP_OBJECT_CLASS, groupObjectClassDetector.detect()); - ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.GROUP_MEMBER_ATTRIBUTE, groupMemberAttrDetector.detect()); + // select attributes based on the collected information + Map<String, String> detectedGroupAttributes = groupAttributDetector.detect(); + + // setting the attributes into the configuration + setDetectedAttributes(ambariLdapConfiguration, detectedGroupAttributes); LOGGER.info("Decorated ambari ldap config : [{}]", ambariLdapConfiguration); } catch (Exception e) { LOGGER.error("Ldap operation failed", e); + throw new AmbariLdapException(e); + } return ambariLdapConfiguration; } + private void setDetectedAttributes(AmbariLdapConfiguration ambariLdapConfiguration, Map<String, String> detectedAttributes) { + for (Map.Entry<String, String> detecteMapEntry : detectedAttributes.entrySet()) { + LOGGER.info("Setting detected configuration value: [{}] - > [{}]", detecteMapEntry.getKey(), detecteMapEntry.getValue()); + ambariLdapConfiguration.setValueFor(AmbariLdapConfigKeys.fromKeyStr(detecteMapEntry.getKey()), detecteMapEntry.getValue()); + } + + } + private SearchRequest assembleUserSearchRequest(LdapConnectionTemplate ldapConnectionTemplate, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { try { http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java new file mode 100644 index 0000000..08e3625 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java @@ -0,0 +1,52 @@ +/* + * 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.Set; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.ambari.server.ldap.service.AttributeDetector; + +@Singleton +public class AttributeDetectorFactory { + + private static final String USER_ATTRIBUTES_DETECTORS = "UserAttributesDetectors"; + private static final String GROUP_ATTRIBUTES_DETECTORS = "GroupAttributesDetectors"; + + + @Inject + @Named(USER_ATTRIBUTES_DETECTORS) + private Set<AttributeDetector> userAttributeDetectors; + + @Inject + @Named(GROUP_ATTRIBUTES_DETECTORS) + Set<AttributeDetector> groupAttributeDetectors; + + public AttributeDetectorFactory() { + } + + public ChainedAttributeDetector userAttributDetector() { + return new ChainedAttributeDetector(userAttributeDetectors); + } + + public ChainedAttributeDetector groupAttributDetector() { + return new ChainedAttributeDetector(groupAttributeDetectors); + } + + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/ChainedAttributeDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/ChainedAttributeDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/ChainedAttributeDetector.java new file mode 100644 index 0000000..ad70d0b --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/ChainedAttributeDetector.java @@ -0,0 +1,59 @@ +/* + * 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.Map; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.apache.ambari.server.ldap.service.AttributeDetector; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; + +@Singleton +public class ChainedAttributeDetector implements AttributeDetector<Entry> { + + private static final Logger LOG = LoggerFactory.getLogger(ChainedAttributeDetector.class); + + private Set<AttributeDetector> detectors; + + @Inject + public ChainedAttributeDetector(Set<AttributeDetector> detectors) { + this.detectors = detectors; + } + + @Override + public void collect(Entry entry) { + for (AttributeDetector detector : detectors) { + LOG.info("Collecting information for the detector: [{}]", detector); + detector.collect(entry); + } + } + + @Override + public Map<String, String> detect() { + Map<String, String> detectedAttributes = Maps.newHashMap(); + for (AttributeDetector detector : detectors) { + LOG.info("Detecting ldap configuration value using the detector: [{}]", detector); + detectedAttributes.putAll(detector.detect()); + } + return detectedAttributes; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetector.java index 9f38357..72e753b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupMemberAttrDetector.java @@ -16,6 +16,7 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; public class GroupMemberAttrDetector extends OccurrenceAndWeightBasedDetector { @@ -56,4 +57,9 @@ public class GroupMemberAttrDetector extends OccurrenceAndWeightBasedDetector { protected boolean applies(Entry entry, String value) { return entry.containsAttribute(value); } + + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.USER_GROUP_MEMBER_ATTRIBUTE.key(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupNameAttrDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupNameAttrDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupNameAttrDetector.java index 0f48348..ca54dad 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupNameAttrDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupNameAttrDetector.java @@ -16,6 +16,7 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,4 +62,9 @@ public class GroupNameAttrDetector extends OccurrenceAndWeightBasedDetector { protected boolean applies(Entry entry, String value) { return entry.containsAttribute(value); } + + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.GROUP_NAME_ATTRIBUTE.key(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupObjectClassDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupObjectClassDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupObjectClassDetector.java index 9338e70..8f342f1 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupObjectClassDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/GroupObjectClassDetector.java @@ -16,6 +16,7 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,4 +65,9 @@ public class GroupObjectClassDetector extends OccurrenceAndWeightBasedDetector { protected boolean applies(Entry entry, String value) { return entry.hasObjectClass(value); } + + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.GROUP_OBJECT_CLASS.key(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/OccurrenceAndWeightBasedDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/OccurrenceAndWeightBasedDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/OccurrenceAndWeightBasedDetector.java index 2ec559b..cb78d25 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/OccurrenceAndWeightBasedDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/OccurrenceAndWeightBasedDetector.java @@ -41,9 +41,13 @@ public abstract class OccurrenceAndWeightBasedDetector implements AttributeDetec protected abstract boolean applies(Entry entry, String value); + public abstract String detectedProperty(); + @Override - public String detect() { + public Map<String, String> detect() { LOGGER.info("Calculating the most probable attribute/value ..."); + Map<String, String> detectedMap = Maps.newHashMap(); + Map.Entry<String, Integer> selectedEntry = null; for (Map.Entry<String, Integer> entry : occurrenceMap().entrySet()) { @@ -73,9 +77,11 @@ public abstract class OccurrenceAndWeightBasedDetector implements AttributeDetec } LOGGER.info("Detected attribute or value: [{}]", detectedVal); - return detectedVal; + detectedMap.put(detectedProperty(), detectedVal); + return detectedMap; } + @Override public void collect(Entry entry) { LOGGER.info("Collecting ldap attributes/values form entry with dn: [{}]", entry.getDn()); http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java index f04201e..63ad8ba 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java @@ -16,6 +16,7 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; public class UserGroupMemberAttrDetector extends OccurrenceAndWeightBasedDetector { @@ -55,4 +56,9 @@ public class UserGroupMemberAttrDetector extends OccurrenceAndWeightBasedDetecto protected boolean applies(Entry entry, String value) { return entry.containsAttribute(value); } + + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.USER_GROUP_MEMBER_ATTRIBUTE.key(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserNameAttrDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserNameAttrDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserNameAttrDetector.java index 2c41162..8f46d72 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserNameAttrDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserNameAttrDetector.java @@ -15,11 +15,14 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import javax.inject.Singleton; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Singleton public class UserNameAttrDetector extends OccurrenceAndWeightBasedDetector { private static final Logger LOGGER = LoggerFactory.getLogger(UserNameAttrDetector.class); @@ -60,4 +63,9 @@ public class UserNameAttrDetector extends OccurrenceAndWeightBasedDetector { return entry.containsAttribute(value); } + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE.key(); + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserObjectClassDetector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserObjectClassDetector.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserObjectClassDetector.java index 2b47671..902dff9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserObjectClassDetector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserObjectClassDetector.java @@ -16,6 +16,7 @@ package org.apache.ambari.server.ldap.service.ads.detectors; import javax.inject.Inject; +import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys; import org.apache.directory.api.ldap.model.entry.Entry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,4 +62,9 @@ public class UserObjectClassDetector extends OccurrenceAndWeightBasedDetector { return entry.hasObjectClass(value); } + @Override + public String detectedProperty() { + return AmbariLdapConfigKeys.USER_OBJECT_CLASS.key(); + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/3750daf3/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 91f4e10..e77f816 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 @@ -15,13 +15,16 @@ package org.apache.ambari.server.ldap; 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.AttributeDetector; 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; +import org.apache.ambari.server.ldap.service.ads.detectors.AttributeDetectorFactory; import org.apache.directory.api.ldap.model.constants.SchemaConstants; import org.apache.directory.api.ldap.model.exception.LdapException; import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException; @@ -36,6 +39,7 @@ import org.junit.Ignore; import org.junit.Test; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; @@ -170,4 +174,20 @@ public class LdapModuleFunctionalTest { // THEN // no exceptions thrown } + + + @Test + public void testShouldDetectorsBeBound() throws Exception { + // GIVEN + + Set<AttributeDetector> adSet = Sets.newHashSet(); +// ChainedAttributeDetector ad = injector.getInstance(ChainedAttributeDetector.class); + + AttributeDetectorFactory f = injector.getInstance(AttributeDetectorFactory.class); + // WHEN + Assert.assertNotNull(f); + + // THEN + + } } \ No newline at end of file