AMBARI-21307 refactoring - renamed, moved classes, variables
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/32f4f8de Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/32f4f8de Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/32f4f8de Branch: refs/heads/feature-branch-AMBARI-21307 Commit: 32f4f8de19ced5df64d88699fabc86da36f6f197 Parents: ba0b28d Author: lpuskas <lpus...@apache.org> Authored: Tue Aug 22 10:03:24 2017 +0200 Committer: lpuskas <lpus...@apache.org> Committed: Tue Aug 29 15:05:30 2017 +0200 ---------------------------------------------------------------------- .../api/services/ldap/AmbariConfiguration.java | 87 +++++++ .../services/ldap/AmbariConfigurationDTO.java | 67 ----- .../ldap/LdapCheckConfigurationRequest.java | 47 ---- .../api/services/ldap/LdapConfigOperation.java | 43 ++++ .../services/ldap/LdapConfigurationRequest.java | 49 ++++ .../services/ldap/LdapConfigurationService.java | 53 +--- .../api/services/ldap/LdapOperationRequest.java | 18 -- .../server/ldap/LdapConfigurationService.java | 60 +++++ .../ldap/LdapConfigurationValidatorService.java | 60 ----- .../apache/ambari/server/ldap/LdapModule.java | 4 +- .../server/ldap/service/AmbariLdapFacade.java | 10 +- .../ads/DefaultLdapConfigurationService.java | 243 +++++++++++++++++++ ...efaultLdapConfigurationValidatorService.java | 243 ------------------- .../api/services/ldap/LDAPServiceTest.java | 6 +- .../DefaultLdapConfigurationServiceTest.java | 113 +++++++++ ...ltLdapConfigurationValidatorServiceTest.java | 113 --------- 16 files changed, 618 insertions(+), 598 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfiguration.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfiguration.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfiguration.java new file mode 100644 index 0000000..b5cc921 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfiguration.java @@ -0,0 +1,87 @@ +/* + * 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.api.services.ldap; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +/** + * Domain POJO representing generic ambari configuration data. + */ +public class AmbariConfiguration { + + /** + * The type of the configuration, eg.: ldap-configuration + */ + private String type; + + /** + * Version tag + */ + private String versionTag; + + /** + * Version number + */ + private Integer version; + + /** + * Created timestamp + */ + private long createdTs; + + private Set<Map<String, Object>> data = Collections.emptySet(); + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Set<Map<String, Object>> getData() { + return data; + } + + public void setData(Set<Map<String, Object>> data) { + this.data = data; + } + + public String getVersionTag() { + return versionTag; + } + + public void setVersionTag(String versionTag) { + this.versionTag = versionTag; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + public long getCreatedTs() { + return createdTs; + } + + public void setCreatedTs(long createdTs) { + this.createdTs = createdTs; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfigurationDTO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfigurationDTO.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfigurationDTO.java deleted file mode 100644 index 1b134fe..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/AmbariConfigurationDTO.java +++ /dev/null @@ -1,67 +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.api.services.ldap; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -public class AmbariConfigurationDTO { - private String type; - private Set<Map<String, Object>> data = Collections.emptySet(); - private String versionTag; - private Integer version; - private long createdTs; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Set<Map<String, Object>> getData() { - return data; - } - - public void setData(Set<Map<String, Object>> data) { - this.data = data; - } - - public String getVersionTag() { - return versionTag; - } - - public void setVersionTag(String versionTag) { - this.versionTag = versionTag; - } - - public Integer getVersion() { - return version; - } - - public void setVersion(Integer version) { - this.version = version; - } - - public long getCreatedTs() { - return createdTs; - } - - public void setCreatedTs(long createdTs) { - this.createdTs = createdTs; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapCheckConfigurationRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapCheckConfigurationRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapCheckConfigurationRequest.java deleted file mode 100644 index 188f1b9..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapCheckConfigurationRequest.java +++ /dev/null @@ -1,47 +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.api.services.ldap; - - -import com.google.gson.annotations.SerializedName; - -public class LdapCheckConfigurationRequest implements LdapOperationRequest { - - @SerializedName("AmbariConfiguration") - private AmbariConfigurationDTO ambariConfiguration; - - @SerializedName("RequestInfo") - private LdapRequestInfo requestInfo; - - public LdapCheckConfigurationRequest() { - } - - - public AmbariConfigurationDTO getAmbariConfiguration() { - return ambariConfiguration; - } - - public void setAmbariConfiguration(AmbariConfigurationDTO ambariConfiguration) { - this.ambariConfiguration = ambariConfiguration; - } - - public LdapRequestInfo getRequestInfo() { - return requestInfo; - } - - public void setRequestInfo(LdapRequestInfo requestInfo) { - this.requestInfo = requestInfo; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigOperation.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigOperation.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigOperation.java new file mode 100644 index 0000000..478d4ff --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigOperation.java @@ -0,0 +1,43 @@ +/* + * 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.api.services.ldap; + +/** + * Enumeration for supported operations related to LDAP configuration. + */ +public enum LdapConfigOperation { + TEST_CONNECTION("test-connection"), + TEST_ATTRIBUTES("test-attributes"), + DETECT_ATTRIBUTES("detect-attributes"); + + private String actionStr; + + LdapConfigOperation(String actionStr) { + this.actionStr = actionStr; + } + + public static LdapConfigOperation fromAction(String action) { + for (LdapConfigOperation val : LdapConfigOperation.values()) { + if (val.action().equals(action)) { + return val; + } + } + throw new IllegalStateException("Action [ " + action + " ] is not supported"); + } + + public String action() { + return this.actionStr; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationRequest.java new file mode 100644 index 0000000..2e478c4 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationRequest.java @@ -0,0 +1,49 @@ +/* + * 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.api.services.ldap; + + +import com.google.gson.annotations.SerializedName; + +/** + * Request object wrapping information for LDAP configuration related request calls. + */ +public class LdapConfigurationRequest { + + @SerializedName("AmbariConfiguration") + private AmbariConfiguration ambariConfiguration; + + @SerializedName("RequestInfo") + private LdapRequestInfo requestInfo; + + public LdapConfigurationRequest() { + } + + public AmbariConfiguration getAmbariConfiguration() { + return ambariConfiguration; + } + + public void setAmbariConfiguration(AmbariConfiguration ambariConfiguration) { + this.ambariConfiguration = ambariConfiguration; + } + + public LdapRequestInfo getRequestInfo() { + return requestInfo; + } + + public void setRequestInfo(LdapRequestInfo requestInfo) { + this.requestInfo = requestInfo; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationService.java index 52244bc..fc6bd41 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapConfigurationService.java @@ -74,40 +74,13 @@ public class LdapConfigurationService extends AmbariConfigurationService { @Inject private static LdapConfigurationFactory ldapConfigurationFactory; - /** - * Actions supported by this endpoint - */ - private enum LdapAction { - TEST_CONNECTION("test-connection"), - TEST_ATTRIBUTES("test-attributes"), - DETECT_ATTRIBUTES("detect-attributes"); - - private String actionStr; - - LdapAction(String actionStr) { - this.actionStr = actionStr; - } - - public static LdapAction fromAction(String action) { - for (LdapAction val : LdapAction.values()) { - if (val.action().equals(action)) { - return val; - } - } - throw new IllegalStateException("Action [ " + action + " ] is not supported"); - } - - public String action() { - return this.actionStr; - } - } @POST @ApiIgnore // until documented @Path("/validate") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Response validateConfiguration(LdapCheckConfigurationRequest ldapCheckConfigurationRequest) { + public Response validateConfiguration(LdapConfigurationRequest ldapConfigurationRequest) { authorize(); @@ -116,12 +89,12 @@ public class LdapConfigurationService extends AmbariConfigurationService { Result result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.OK)); try { - validateRequest(ldapCheckConfigurationRequest); + validateRequest(ldapConfigurationRequest); AmbariLdapConfiguration ambariLdapConfiguration = ldapConfigurationFactory.createLdapConfiguration( - ldapCheckConfigurationRequest.getAmbariConfiguration().getData().iterator().next()); + ldapConfigurationRequest.getAmbariConfiguration().getData().iterator().next()); - LdapAction action = LdapAction.fromAction(ldapCheckConfigurationRequest.getRequestInfo().getAction()); + LdapConfigOperation action = LdapConfigOperation.fromAction(ldapConfigurationRequest.getRequestInfo().getAction()); switch (action) { case TEST_CONNECTION: @@ -133,7 +106,7 @@ public class LdapConfigurationService extends AmbariConfigurationService { case TEST_ATTRIBUTES: LOGGER.info("Testing LDAP attributes ...."); - groups = ldapFacade.checkLdapAttibutes(ldapCheckConfigurationRequest.getRequestInfo().getParameters(), ambariLdapConfiguration); + groups = ldapFacade.checkLdapAttibutes(ldapConfigurationRequest.getRequestInfo().getParameters(), ambariLdapConfiguration); setResult(groups, result); break; @@ -161,24 +134,24 @@ public class LdapConfigurationService extends AmbariConfigurationService { result.getResultTree().addChild(resource, "payload"); } - private void validateRequest(LdapCheckConfigurationRequest ldapCheckConfigurationRequest) { + private void validateRequest(LdapConfigurationRequest ldapConfigurationRequest) { String errMsg; - if (null == ldapCheckConfigurationRequest) { + if (null == ldapConfigurationRequest) { errMsg = "No ldap configuraiton request provided"; LOGGER.error(errMsg); throw new IllegalArgumentException(errMsg); } - if (null == ldapCheckConfigurationRequest.getRequestInfo()) { - errMsg = String.format("No request information provided. Request: [%s]", ldapCheckConfigurationRequest); + if (null == ldapConfigurationRequest.getRequestInfo()) { + errMsg = String.format("No request information provided. Request: [%s]", ldapConfigurationRequest); LOGGER.error(errMsg); throw new IllegalArgumentException(errMsg); } - if (null == ldapCheckConfigurationRequest.getAmbariConfiguration() - || ldapCheckConfigurationRequest.getAmbariConfiguration().getData().size() != 1) { - errMsg = String.format("No / Invalid configuration data provided. Request: [%s]", ldapCheckConfigurationRequest); + if (null == ldapConfigurationRequest.getAmbariConfiguration() + || ldapConfigurationRequest.getAmbariConfiguration().getData().size() != 1) { + errMsg = String.format("No / Invalid configuration data provided. Request: [%s]", ldapConfigurationRequest); LOGGER.error(errMsg); throw new IllegalArgumentException(errMsg); } @@ -202,7 +175,7 @@ public class LdapConfigurationService extends AmbariConfigurationService { } - Set<RoleAuthorization> requiredAuthorizations() { + private Set<RoleAuthorization> requiredAuthorizations() { return Sets.newHashSet(RoleAuthorization.AMBARI_MANAGE_CONFIGURATION); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapOperationRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapOperationRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapOperationRequest.java deleted file mode 100644 index 06f6c40..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ldap/LdapOperationRequest.java +++ /dev/null @@ -1,18 +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.api.services.ldap; - -public interface LdapOperationRequest { -} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java new file mode 100644 index 0000000..69a641f --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java @@ -0,0 +1,60 @@ +/* + * 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; + +import java.util.Set; + +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.directory.ldap.client.api.LdapConnection; + +/** + * Collection of operations for validating ldap configuration. + * It's intended to decouple implementations using different libraries. + */ +public interface LdapConfigurationService { + + /** + * Tests the connection based on the provided configuration. + * + * @param ldapConnection connection instance + * @param configuration the ambari ldap configuration instance + * @throws AmbariLdapException if the connection is not possible + */ + void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration configuration) throws AmbariLdapException; + + + /** + * Implements LDAP user related configuration settings validation logic. + * Implementers communicate with the LDAP server (search, bind) to validate attributes in the provided configuration + * instance + * + * @param ldapConnection connection instance used to connect to the LDAP server + * @param testUserName the test username + * @param testPassword the test password + * @param configuration the available ldap configuration + * @return The DN of the found user entry + * @throws AmbariException if the connection couldn't be estabilisheds + */ + String checkUserAttributes(LdapConnection ldapConnection, String testUserName, String testPassword, AmbariLdapConfiguration configuration) throws AmbariLdapException; + + /** + * Checks whether the group related LDAP attributes in the configuration are correct. + * + * @throws AmbariException if the attributes are not valid + */ + Set<String> checkGroupAttributes(LdapConnection ldapConnection, String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException; + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationValidatorService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationValidatorService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationValidatorService.java deleted file mode 100644 index 7efa3b7..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationValidatorService.java +++ /dev/null @@ -1,60 +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; - -import java.util.Set; - -import org.apache.ambari.server.AmbariException; -import org.apache.ambari.server.ldap.service.AmbariLdapException; -import org.apache.directory.ldap.client.api.LdapConnection; - -/** - * Collection of operations for validating ldap configuration. - * It's intended to decouple implementations using different libraries. - */ -public interface LdapConfigurationValidatorService { - - /** - * Tests the connection based on the provided configuration. - * - * @param ldapConnection connection instance - * @param configuration the ambari ldap configuration instance - * @throws AmbariLdapException if the connection is not possible - */ - void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration configuration) throws AmbariLdapException; - - - /** - * Implements LDAP user related configuration settings validation logic. - * Implementers communicate with the LDAP server (search, bind) to validate attributes in the provided configuration - * instance - * - * @param ldapConnection connection instance used to connect to the LDAP server - * @param testUserName the test username - * @param testPassword the test password - * @param configuration the available ldap configuration - * @return The DN of the found user entry - * @throws AmbariException if the connection couldn't be estabilisheds - */ - String checkUserAttributes(LdapConnection ldapConnection, String testUserName, String testPassword, AmbariLdapConfiguration configuration) throws AmbariLdapException; - - /** - * Checks whether the group related LDAP attributes in the configuration are correct. - * - * @throws AmbariException if the attributes are not valid - */ - Set<String> checkGroupAttributes(LdapConnection ldapConnection, String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException; - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/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 a4ad2ee..3ae4587 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 @@ -18,7 +18,7 @@ package org.apache.ambari.server.ldap; import org.apache.ambari.server.ldap.service.AmbariLdapFacade; import org.apache.ambari.server.ldap.service.LdapConnectionService; import org.apache.ambari.server.ldap.service.LdapFacade; -import org.apache.ambari.server.ldap.service.ads.DefaultLdapConfigurationValidatorService; +import org.apache.ambari.server.ldap.service.ads.DefaultLdapConfigurationService; import org.apache.ambari.server.ldap.service.ads.DefaultLdapConnectionService; import com.google.inject.AbstractModule; @@ -32,7 +32,7 @@ public class LdapModule extends AbstractModule { @Override protected void configure() { bind(LdapFacade.class).to(AmbariLdapFacade.class); - bind(LdapConfigurationValidatorService.class).to(DefaultLdapConfigurationValidatorService.class); + bind(LdapConfigurationService.class).to(DefaultLdapConfigurationService.class); bind(LdapConnectionService.class).to(DefaultLdapConnectionService.class); install(new FactoryModuleBuilder().build(LdapConfigurationFactory.class)); http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/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 eec47ce..683ed43 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 @@ -22,7 +22,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import org.apache.ambari.server.ldap.AmbariLdapConfiguration; -import org.apache.ambari.server.ldap.LdapConfigurationValidatorService; +import org.apache.ambari.server.ldap.LdapConfigurationService; import org.apache.directory.ldap.client.api.LdapConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,7 +49,7 @@ public class AmbariLdapFacade implements LdapFacade { } @Inject - private LdapConfigurationValidatorService ldapConfigurationValidatorService; + private LdapConfigurationService ldapConfigurationService; @Inject private LdapConnectionService ldapConnectionService; @@ -63,7 +63,7 @@ public class AmbariLdapFacade implements LdapFacade { try { LOGGER.info("Validating LDAP connection related configuration based on: {}", ambariLdapConfiguration); LdapConnection connection = ldapConnectionService.createLdapConnection(ambariLdapConfiguration); - ldapConfigurationValidatorService.checkConnection(connection, ambariLdapConfiguration); + ldapConfigurationService.checkConnection(connection, ambariLdapConfiguration); } catch (AmbariLdapException e) { LOGGER.error("Validating LDAP connection configuration failed", e); throw e; @@ -90,10 +90,10 @@ public class AmbariLdapFacade implements LdapFacade { LdapConnection ldapConnection = ldapConnectionService.createLdapConnection(ldapConfiguration); LOGGER.info("Testing LDAP user attributes with test user: {}", userName); - String userDn = ldapConfigurationValidatorService.checkUserAttributes(ldapConnection, userName, testUserPass, ldapConfiguration); + String userDn = ldapConfigurationService.checkUserAttributes(ldapConnection, userName, testUserPass, ldapConfiguration); LOGGER.info("Testing LDAP group attributes with test user dn: {}", userDn); - Set<String> groups = ldapConfigurationValidatorService.checkGroupAttributes(ldapConnection, userDn, ldapConfiguration); + Set<String> groups = ldapConfigurationService.checkGroupAttributes(ldapConnection, userDn, ldapConfiguration); return groups; } http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/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 new file mode 100644 index 0000000..abc9201 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java @@ -0,0 +1,243 @@ +/* + * 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.io.IOException; +import java.util.List; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.ldap.AmbariLdapConfiguration; +import org.apache.ambari.server.ldap.LdapConfigurationService; +import org.apache.ambari.server.ldap.service.AmbariLdapException; +import org.apache.ambari.server.ldap.service.LdapConnectionService; +import org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator; +import org.apache.directory.api.ldap.model.constants.SchemaConstants; +import org.apache.directory.api.ldap.model.cursor.EntryCursor; +import org.apache.directory.api.ldap.model.cursor.SearchCursor; +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.Response; +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.api.LdapConnection; +import org.apache.directory.ldap.client.api.search.FilterBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +/** + * Implementation of the validation logic using the Apache Directory API. + */ +@Singleton +public class DefaultLdapConfigurationService implements LdapConfigurationService { + + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLdapConfigurationService.class); + + @Inject + private LdapConnectionService ldapConnectionService; + + /** + * Facilitating the instantiation + */ + @Inject + public DefaultLdapConfigurationService() { + } + + @Override + public void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + try { + bind(ambariLdapConfiguration, ldapConnection); + } catch (LdapException e) { + LOGGER.error("Could not connect to the LDAP server", e); + throw new AmbariLdapException(e); + } + } + + + /** + * Checks the user attributes provided in the configuration instance by issuing a search for a (known) test user in the LDAP. + * Attributes are considered correct if there is at least one entry found. + * + * Invalid attributes are signaled by throwing an exception. + * + * @param testUserName the test username + * @param testPassword the test password + * @param ambariLdapConfiguration configuration instance holding ldap configuration details + * @return the DN of the test user + * @throws AmbariException if the attributes are not valid or any errors occurs + */ + @Override + public String checkUserAttributes(LdapConnection ldapConnection, String testUserName, String testPassword, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + SearchCursor searchCursor = null; + String userDn = null; + try { + LOGGER.info("Checking user attributes for user {} r ...", testUserName); + + // bind anonimously or with manager data + bind(ambariLdapConfiguration, ldapConnection); + + // set up a filter based on the provided attributes + String filter = FilterBuilder.and( + FilterBuilder.equal(SchemaConstants.OBJECT_CLASS_AT, ambariLdapConfiguration.userObjectClass()), + FilterBuilder.equal(ambariLdapConfiguration.userNameAttribute(), testUserName)) + .toString(); + + LOGGER.info("Searching for the user: {} using the search filter: {}", testUserName, filter); + EntryCursor entryCursor = ldapConnection.search(new Dn(ambariLdapConfiguration.userSearchBase()), filter, SearchScope.SUBTREE); + + // collecting search result entries + List<Entry> users = Lists.newArrayList(); + for (Entry entry : entryCursor) { + users.add(entry); + userDn = entry.getDn().getNormName(); + } + + // there should be at least one user found + if (users.isEmpty()) { + String msg = String.format("There are no users found using the filter: [ %s ]. Try changing the attribute values", filter); + LOGGER.error(msg); + throw new Exception(msg); + } + + LOGGER.info("Attibute validation succeeded. Filter: {}", filter); + + } catch (Exception e) { + + LOGGER.error("User attributes validation failed.", e); + throw new AmbariLdapException(e.getMessage(), e); + + } finally { + closeResources(ldapConnection, searchCursor); + } + return userDn; + } + + + @Override + public Set<String> checkGroupAttributes(LdapConnection ldapConnection, String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { + SearchCursor searchCursor = null; + Set<Response> groupResponses = Sets.newHashSet(); + + try { + LOGGER.info("Checking group attributes for user dn {} ...", userDn); + + bind(ambariLdapConfiguration, ldapConnection); + + // set up a filter based on the provided attributes + String filter = FilterBuilder.and( + FilterBuilder.equal(SchemaConstants.OBJECT_CLASS_AT, ambariLdapConfiguration.groupObjectClass()), + FilterBuilder.equal(ambariLdapConfiguration.groupMemberAttribute(), userDn) + ).toString(); + + LOGGER.info("Searching for the groups the user dn: {} is member of using the search filter: {}", userDn, filter); + + // assemble a search request + SearchRequest searchRequest = new SearchRequestImpl(); + searchRequest.setFilter(filter); + searchRequest.setBase(new Dn(ambariLdapConfiguration.groupSearchBase())); + searchRequest.setScope(SearchScope.SUBTREE); + searchRequest.addAttributes(ambariLdapConfiguration.groupMemberAttribute(), ambariLdapConfiguration.groupNameAttribute()); + + // perform the search + searchCursor = ldapConnection.search(searchRequest); + + for (Response response : searchCursor) { + groupResponses.add(response); + } + + } catch (Exception e) { + + LOGGER.error("User attributes validation failed.", e); + throw new AmbariLdapException(e.getMessage(), e); + + } finally { + closeResources(ldapConnection, searchCursor); + } + + return processGroupResults(groupResponses, ambariLdapConfiguration); + } + + /** + * Binds to the LDAP server (anonimously or wit manager credentials) + * + * @param ambariLdapConfiguration configuration instance + * @param connection connection instance + * @throws LdapException if the bind operation fails + */ + private void bind(AmbariLdapConfiguration ambariLdapConfiguration, LdapConnection connection) throws LdapException { + LOGGER.info("Connecting to LDAP ...."); + if (!ambariLdapConfiguration.bindAnonimously()) { + LOGGER.debug("Anonimous binding not supported, binding with the manager detailas..."); + connection.bind(ambariLdapConfiguration.managerDn(), ambariLdapConfiguration.managerPassword()); + } else { + LOGGER.debug("Binding anonimously ..."); + connection.bind(); + } + + if (!connection.isConnected()) { + LOGGER.error("Not connected to the LDAP server. Connection instance: {}", connection); + throw new IllegalStateException("The connection to the LDAP server is not alive"); + } + LOGGER.info("Connected to LDAP."); + } + + + /** + * Extracts meaningful values from the search result. + * + * @param groupResponses the result entries returned by the search + * @param ambariLdapConfiguration holds the keys of the meaningful attributes + * @return a set with the group names the test user belongs to + */ + private Set<String> processGroupResults(Set<Response> groupResponses, AmbariLdapConfiguration ambariLdapConfiguration) { + Set<String> groupStrSet = Sets.newHashSet(); + for (Response response : groupResponses) { + Entry entry = ((SearchResultEntryDecorator) response).getEntry(); + groupStrSet.add(entry.get(ambariLdapConfiguration.groupNameAttribute()).get().getString()); + } + + LOGGER.debug("Extracted group names from group search responses: {}", groupStrSet); + return groupStrSet; + } + + private void closeResources(LdapConnection connection, SearchCursor searchCursor) { + LOGGER.debug("Housekeeping: closing the connection and the search cursor ..."); + + if (null != searchCursor) { + // this method is idempotent + searchCursor.close(); + } + + if (null != connection) { + try { + connection.close(); + } catch (IOException e) { + LOGGER.error("Exception occurred while closing the connection", e); + } + } + } + +} + + + http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorService.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorService.java deleted file mode 100644 index 040983a..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorService.java +++ /dev/null @@ -1,243 +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; - -import java.io.IOException; -import java.util.List; -import java.util.Set; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.apache.ambari.server.AmbariException; -import org.apache.ambari.server.ldap.AmbariLdapConfiguration; -import org.apache.ambari.server.ldap.LdapConfigurationValidatorService; -import org.apache.ambari.server.ldap.service.AmbariLdapException; -import org.apache.ambari.server.ldap.service.LdapConnectionService; -import org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator; -import org.apache.directory.api.ldap.model.constants.SchemaConstants; -import org.apache.directory.api.ldap.model.cursor.EntryCursor; -import org.apache.directory.api.ldap.model.cursor.SearchCursor; -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.Response; -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.api.LdapConnection; -import org.apache.directory.ldap.client.api.search.FilterBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; - -/** - * Implementation of the validation logic using the Apache Directory API. - */ -@Singleton -public class DefaultLdapConfigurationValidatorService implements LdapConfigurationValidatorService { - - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLdapConfigurationValidatorService.class); - - @Inject - private LdapConnectionService ldapConnectionService; - - /** - * Facilitating the instantiation - */ - @Inject - public DefaultLdapConfigurationValidatorService() { - } - - @Override - public void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - try { - bind(ambariLdapConfiguration, ldapConnection); - } catch (LdapException e) { - LOGGER.error("Could not connect to the LDAP server", e); - throw new AmbariLdapException(e); - } - } - - - /** - * Checks the user attributes provided in the configuration instance by issuing a search for a (known) test user in the LDAP. - * Attributes are considered correct if there is at least one entry found. - * - * Invalid attributes are signaled by throwing an exception. - * - * @param testUserName the test username - * @param testPassword the test password - * @param ambariLdapConfiguration configuration instance holding ldap configuration details - * @return the DN of the test user - * @throws AmbariException if the attributes are not valid or any errors occurs - */ - @Override - public String checkUserAttributes(LdapConnection ldapConnection, String testUserName, String testPassword, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - SearchCursor searchCursor = null; - String userDn = null; - try { - LOGGER.info("Checking user attributes for user {} r ...", testUserName); - - // bind anonimously or with manager data - bind(ambariLdapConfiguration, ldapConnection); - - // set up a filter based on the provided attributes - String filter = FilterBuilder.and( - FilterBuilder.equal(SchemaConstants.OBJECT_CLASS_AT, ambariLdapConfiguration.userObjectClass()), - FilterBuilder.equal(ambariLdapConfiguration.userNameAttribute(), testUserName)) - .toString(); - - LOGGER.info("Searching for the user: {} using the search filter: {}", testUserName, filter); - EntryCursor entryCursor = ldapConnection.search(new Dn(ambariLdapConfiguration.userSearchBase()), filter, SearchScope.SUBTREE); - - // collecting search result entries - List<Entry> users = Lists.newArrayList(); - for (Entry entry : entryCursor) { - users.add(entry); - userDn = entry.getDn().getNormName(); - } - - // there should be at least one user found - if (users.isEmpty()) { - String msg = String.format("There are no users found using the filter: [ %s ]. Try changing the attribute values", filter); - LOGGER.error(msg); - throw new Exception(msg); - } - - LOGGER.info("Attibute validation succeeded. Filter: {}", filter); - - } catch (Exception e) { - - LOGGER.error("User attributes validation failed.", e); - throw new AmbariLdapException(e.getMessage(), e); - - } finally { - closeResources(ldapConnection, searchCursor); - } - return userDn; - } - - - @Override - public Set<String> checkGroupAttributes(LdapConnection ldapConnection, String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { - SearchCursor searchCursor = null; - Set<Response> groupResponses = Sets.newHashSet(); - - try { - LOGGER.info("Checking group attributes for user dn {} ...", userDn); - - bind(ambariLdapConfiguration, ldapConnection); - - // set up a filter based on the provided attributes - String filter = FilterBuilder.and( - FilterBuilder.equal(SchemaConstants.OBJECT_CLASS_AT, ambariLdapConfiguration.groupObjectClass()), - FilterBuilder.equal(ambariLdapConfiguration.groupMemberAttribute(), userDn) - ).toString(); - - LOGGER.info("Searching for the groups the user dn: {} is member of using the search filter: {}", userDn, filter); - - // assemble a search request - SearchRequest searchRequest = new SearchRequestImpl(); - searchRequest.setFilter(filter); - searchRequest.setBase(new Dn(ambariLdapConfiguration.groupSearchBase())); - searchRequest.setScope(SearchScope.SUBTREE); - searchRequest.addAttributes(ambariLdapConfiguration.groupMemberAttribute(), ambariLdapConfiguration.groupNameAttribute()); - - // perform the search - searchCursor = ldapConnection.search(searchRequest); - - for (Response response : searchCursor) { - groupResponses.add(response); - } - - } catch (Exception e) { - - LOGGER.error("User attributes validation failed.", e); - throw new AmbariLdapException(e.getMessage(), e); - - } finally { - closeResources(ldapConnection, searchCursor); - } - - return processGroupResults(groupResponses, ambariLdapConfiguration); - } - - /** - * Binds to the LDAP server (anonimously or wit manager credentials) - * - * @param ambariLdapConfiguration configuration instance - * @param connection connection instance - * @throws LdapException if the bind operation fails - */ - private void bind(AmbariLdapConfiguration ambariLdapConfiguration, LdapConnection connection) throws LdapException { - LOGGER.info("Connecting to LDAP ...."); - if (!ambariLdapConfiguration.bindAnonimously()) { - LOGGER.debug("Anonimous binding not supported, binding with the manager detailas..."); - connection.bind(ambariLdapConfiguration.managerDn(), ambariLdapConfiguration.managerPassword()); - } else { - LOGGER.debug("Binding anonimously ..."); - connection.bind(); - } - - if (!connection.isConnected()) { - LOGGER.error("Not connected to the LDAP server. Connection instance: {}", connection); - throw new IllegalStateException("The connection to the LDAP server is not alive"); - } - LOGGER.info("Connected to LDAP."); - } - - - /** - * Extracts meaningful values from the search result. - * - * @param groupResponses the result entries returned by the search - * @param ambariLdapConfiguration holds the keys of the meaningful attributes - * @return a set with the group names the test user belongs to - */ - private Set<String> processGroupResults(Set<Response> groupResponses, AmbariLdapConfiguration ambariLdapConfiguration) { - Set<String> groupStrSet = Sets.newHashSet(); - for (Response response : groupResponses) { - Entry entry = ((SearchResultEntryDecorator) response).getEntry(); - groupStrSet.add(entry.get(ambariLdapConfiguration.groupNameAttribute()).get().getString()); - } - - LOGGER.debug("Extracted group names from group search responses: {}", groupStrSet); - return groupStrSet; - } - - private void closeResources(LdapConnection connection, SearchCursor searchCursor) { - LOGGER.debug("Housekeeping: closing the connection and the search cursor ..."); - - if (null != searchCursor) { - // this method is idempotent - searchCursor.close(); - } - - if (null != connection) { - try { - connection.close(); - } catch (IOException e) { - LOGGER.error("Exception occurred while closing the connection", e); - } - } - } - -} - - - http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/test/java/org/apache/ambari/server/api/services/ldap/LDAPServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ldap/LDAPServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ldap/LDAPServiceTest.java index f20cd1f..5e8eac6 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ldap/LDAPServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ldap/LDAPServiceTest.java @@ -46,11 +46,11 @@ public class LDAPServiceTest { // WHEN - LdapCheckConfigurationRequest ldapCheckConfigurationRequest = gsonJsonProvider.fromJson(JSON_STRING, LdapCheckConfigurationRequest.class); - // LdapCheckConfigurationRequest ldapCheckConfigurationRequest = objectMapper.readValue(JSON_STRING, LdapCheckConfigurationRequest.class); + LdapConfigurationRequest ldapConfigurationRequest = gsonJsonProvider.fromJson(JSON_STRING, LdapConfigurationRequest.class); + // LdapConfigurationRequest ldapConfigurationRequest = objectMapper.readValue(JSON_STRING, LdapConfigurationRequest.class); // THEN - Assert.assertNotNull(ldapCheckConfigurationRequest); + Assert.assertNotNull(ldapConfigurationRequest); } http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/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..2b7448e --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java @@ -0,0 +1,113 @@ +/* + * 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 org.junit.Assert.assertNotNull; + +import java.util.Map; + +import org.apache.ambari.server.ldap.AmbariLdapConfiguration; +import org.apache.ambari.server.ldap.LdapConfigurationService; +import org.apache.ambari.server.ldap.service.LdapConnectionService; +import org.apache.directory.api.ldap.model.constants.SchemaConstants; +import org.apache.directory.api.ldap.model.cursor.EntryCursor; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.apache.directory.api.ldap.model.message.SearchScope; +import org.apache.directory.ldap.client.api.LdapConnection; +import org.apache.directory.ldap.client.api.LdapConnectionConfig; +import org.apache.directory.ldap.client.api.LdapNetworkConnection; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; + +public class DefaultLdapConfigurationServiceTest { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLdapConfigurationService.class); + private static final String TEST_USER = "einstein"; + + LdapConfigurationService ldapConfigurationService = new DefaultLdapConfigurationService(); + + + @Test + public void testCheckAttributes() throws Exception { + + // WHEN + LdapConnectionConfig config = new LdapConnectionConfig(); + config.setLdapHost("localhost"); + config.setLdapPort(389); + LdapConnection connection = new LdapNetworkConnection(config); + + // THEN + connection.anonymousBind(); + + + EntryCursor cursor = connection.search("dc=dev,dc=local", "(objectclass=*)", SearchScope.ONELEVEL); + + for (Entry entry : cursor) { + assertNotNull(entry); + System.out.println(entry); + } + + cursor.close(); + + } + + @Test + public void testCheckUserAttributes() throws Exception { + // GIVEN + Map<String, Object> ldapPropsMap = Maps.newHashMap(); + + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BIND_ANONIMOUSLY.propertyName(), "true"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_HOST.propertyName(), "ldap.forumsys.com"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_PORT.propertyName(), "389"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BASE_DN.propertyName(), "dc=example,dc=com"); + + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_OBJECT_CLASS.propertyName(), SchemaConstants.PERSON_OC); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_NAME_ATTRIBUTE.propertyName(), SchemaConstants.UID_AT); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_SEARCH_BASE.propertyName(), "dc=example,dc=com"); + + + AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(ldapPropsMap); + LdapConnectionService connectionService = new DefaultLdapConnectionService(); + LdapNetworkConnection ldapConnection = connectionService.createLdapConnection(ambariLdapConfiguration); + + ldapConfigurationService.checkUserAttributes(ldapConnection, "einstein", "", ambariLdapConfiguration); + } + + @Test + public void testRetrieveGorupsForuser() throws Exception { + // GIVEN + Map<String, Object> ldapPropsMap = Maps.newHashMap(); + + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BIND_ANONIMOUSLY.propertyName(), "true"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_HOST.propertyName(), "ldap.forumsys.com"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_PORT.propertyName(), "389"); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BASE_DN.propertyName(), "dc=example,dc=com"); + + + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_OBJECT_CLASS.propertyName(), SchemaConstants.GROUP_OF_UNIQUE_NAMES_OC); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_NAME_ATTRIBUTE.propertyName(), SchemaConstants.CN_AT); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_MEMBER_ATTRIBUTE.propertyName(), SchemaConstants.UNIQUE_MEMBER_AT); + ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_SEARCH_BASE.propertyName(), "dc=example,dc=com"); + + + AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(ldapPropsMap); + LdapConnectionService connectionService = new DefaultLdapConnectionService(); + LdapNetworkConnection ldapConnection = connectionService.createLdapConnection(ambariLdapConfiguration); + + ldapConfigurationService.checkGroupAttributes(ldapConnection, "uid=einstein,dc=example,dc=com", ambariLdapConfiguration); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/32f4f8de/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorServiceTest.java deleted file mode 100644 index 1c7f75d..0000000 --- a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationValidatorServiceTest.java +++ /dev/null @@ -1,113 +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; - -import static org.junit.Assert.assertNotNull; - -import java.util.Map; - -import org.apache.ambari.server.ldap.AmbariLdapConfiguration; -import org.apache.ambari.server.ldap.LdapConfigurationValidatorService; -import org.apache.ambari.server.ldap.service.LdapConnectionService; -import org.apache.directory.api.ldap.model.constants.SchemaConstants; -import org.apache.directory.api.ldap.model.cursor.EntryCursor; -import org.apache.directory.api.ldap.model.entry.Entry; -import org.apache.directory.api.ldap.model.message.SearchScope; -import org.apache.directory.ldap.client.api.LdapConnection; -import org.apache.directory.ldap.client.api.LdapConnectionConfig; -import org.apache.directory.ldap.client.api.LdapNetworkConnection; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Maps; - -public class DefaultLdapConfigurationValidatorServiceTest { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLdapConfigurationValidatorService.class); - private static final String TEST_USER = "einstein"; - - LdapConfigurationValidatorService ldapConfigurationValidatorService = new DefaultLdapConfigurationValidatorService(); - - - @Test - public void testCheckAttributes() throws Exception { - - // WHEN - LdapConnectionConfig config = new LdapConnectionConfig(); - config.setLdapHost("localhost"); - config.setLdapPort(389); - LdapConnection connection = new LdapNetworkConnection(config); - - // THEN - connection.anonymousBind(); - - - EntryCursor cursor = connection.search("dc=dev,dc=local", "(objectclass=*)", SearchScope.ONELEVEL); - - for (Entry entry : cursor) { - assertNotNull(entry); - System.out.println(entry); - } - - cursor.close(); - - } - - @Test - public void testCheckUserAttributes() throws Exception { - // GIVEN - Map<String, Object> ldapPropsMap = Maps.newHashMap(); - - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BIND_ANONIMOUSLY.propertyName(), "true"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_HOST.propertyName(), "ldap.forumsys.com"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_PORT.propertyName(), "389"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BASE_DN.propertyName(), "dc=example,dc=com"); - - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_OBJECT_CLASS.propertyName(), SchemaConstants.PERSON_OC); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_NAME_ATTRIBUTE.propertyName(), SchemaConstants.UID_AT); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.USER_SEARCH_BASE.propertyName(), "dc=example,dc=com"); - - - AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(ldapPropsMap); - LdapConnectionService connectionService = new DefaultLdapConnectionService(); - LdapNetworkConnection ldapConnection = connectionService.createLdapConnection(ambariLdapConfiguration); - - ldapConfigurationValidatorService.checkUserAttributes(ldapConnection, "einstein", "", ambariLdapConfiguration); - } - - @Test - public void testRetrieveGorupsForuser() throws Exception { - // GIVEN - Map<String, Object> ldapPropsMap = Maps.newHashMap(); - - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BIND_ANONIMOUSLY.propertyName(), "true"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_HOST.propertyName(), "ldap.forumsys.com"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.LDAP_SERVER_PORT.propertyName(), "389"); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.BASE_DN.propertyName(), "dc=example,dc=com"); - - - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_OBJECT_CLASS.propertyName(), SchemaConstants.GROUP_OF_UNIQUE_NAMES_OC); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_NAME_ATTRIBUTE.propertyName(), SchemaConstants.CN_AT); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_MEMBER_ATTRIBUTE.propertyName(), SchemaConstants.UNIQUE_MEMBER_AT); - ldapPropsMap.put(AmbariLdapConfiguration.LdapConfigProperty.GROUP_SEARCH_BASE.propertyName(), "dc=example,dc=com"); - - - AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(ldapPropsMap); - LdapConnectionService connectionService = new DefaultLdapConnectionService(); - LdapNetworkConnection ldapConnection = connectionService.createLdapConnection(ambariLdapConfiguration); - - ldapConfigurationValidatorService.checkGroupAttributes(ldapConnection, "uid=einstein,dc=example,dc=com", ambariLdapConfiguration); - } -} \ No newline at end of file