mohammadjkhan commented on a change in pull request #6972: Support LDAP authentication/authorization URL: https://github.com/apache/incubator-druid/pull/6972#discussion_r332074218
########## File path: extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java ########## @@ -0,0 +1,366 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.security.basic.authentication.validator; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.metadata.PasswordProvider; +import org.apache.druid.security.basic.BasicAuthLDAPConfig; +import org.apache.druid.security.basic.BasicAuthUtils; +import org.apache.druid.security.basic.BasicSecurityAuthenticationException; +import org.apache.druid.security.basic.BasicSecuritySSLSocketFactory; +import org.apache.druid.security.basic.authentication.BasicAuthenticatorUserPrincipal; +import org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentials; +import org.apache.druid.server.security.AuthenticationResult; + +import javax.annotation.Nullable; +import javax.naming.AuthenticationException; +import javax.naming.Context; +import javax.naming.InvalidNameException; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import javax.naming.ldap.LdapName; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.locks.ReentrantLock; + +@JsonTypeName("ldap") +public class LDAPCredentialsValidator implements CredentialsValidator +{ + private static final Logger LOG = new Logger(LDAPCredentialsValidator.class); + private static final ReentrantLock LOCK = new ReentrantLock(); + + private final LruBlockCache cache; + + private final BasicAuthLDAPConfig ldapConfig; + + @JsonCreator + public LDAPCredentialsValidator( + @JsonProperty("url") String url, + @JsonProperty("bindUser") String bindUser, + @JsonProperty("bindPassword") PasswordProvider bindPassword, + @JsonProperty("baseDn") String baseDn, + @JsonProperty("userSearch") String userSearch, + @JsonProperty("userAttribute") String userAttribute, + @JsonProperty("groupFilters") String[] groupFilters, + @JsonProperty("credentialIterations") Integer credentialIterations, + @JsonProperty("credentialVerifyDuration") Integer credentialVerifyDuration, + @JsonProperty("credentialMaxDuration") Integer credentialMaxDuration, + @JsonProperty("credentialCacheSize") Integer credentialCacheSize + ) + { + this.ldapConfig = new BasicAuthLDAPConfig( + url, + bindUser, + bindPassword, + baseDn, + userSearch, + userAttribute, + groupFilters, + credentialIterations == null ? BasicAuthUtils.DEFAULT_KEY_ITERATIONS : credentialIterations, + credentialVerifyDuration == null ? BasicAuthUtils.DEFAULT_CREDENTIAL_VERIFY_DURATION_SECONDS : credentialVerifyDuration, + credentialMaxDuration == null ? BasicAuthUtils.DEFAULT_CREDENTIAL_MAX_DURATION_SECONDS : credentialMaxDuration, + credentialCacheSize == null ? BasicAuthUtils.DEFAULT_CREDENTIAL_CACHE_SIZE : credentialCacheSize + ); + + this.cache = new LruBlockCache( + this.ldapConfig.getCredentialCacheSize(), + this.ldapConfig.getCredentialVerifyDuration(), + this.ldapConfig.getCredentialMaxDuration() + ); + } + + Properties bindProperties(BasicAuthLDAPConfig ldapConfig) + { + Properties properties = commonProperties(ldapConfig); + properties.put(Context.SECURITY_PRINCIPAL, ldapConfig.getBindUser()); + properties.put(Context.SECURITY_CREDENTIALS, ldapConfig.getBindPassword().getPassword()); + return properties; + } + + Properties userProperties(BasicAuthLDAPConfig ldapConfig, LdapName userDn, char[] password) + { + Properties properties = commonProperties(ldapConfig); + properties.put(Context.SECURITY_PRINCIPAL, userDn.toString()); + properties.put(Context.SECURITY_CREDENTIALS, String.valueOf(password)); + return properties; + } + + Properties commonProperties(BasicAuthLDAPConfig ldapConfig) + { + Properties properties = new Properties(); + properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); + properties.put(Context.PROVIDER_URL, ldapConfig.getUrl()); + properties.put(Context.SECURITY_AUTHENTICATION, "simple"); + if (StringUtils.toLowerCase(ldapConfig.getUrl()).startsWith("ldaps://")) { + properties.put(Context.SECURITY_PROTOCOL, "ssl"); + properties.put("java.naming.ldap.factory.socket", BasicSecuritySSLSocketFactory.class.getName()); + } + return properties; + } + + @Override + public AuthenticationResult validateCredentials( + String authenticatorName, + String authorizerName, + String username, + char[] password + ) + { + Set<LdapName> groups; + LdapName userDn; + Map<String, Object> contexMap = new HashMap<>(); + + BasicAuthenticatorUserPrincipal principal = this.cache.getOrExpire(username); + if (principal != null && principal.hasSameCredentials(password)) { + contexMap.put(BasicAuthUtils.GROUPS_CONTEXT_KEY, principal.getGroups()); + return new AuthenticationResult(username, authorizerName, authenticatorName, contexMap); + } else { + try { + InitialDirContext dirContext = new InitialDirContext(bindProperties(this.ldapConfig)); + + try { + SearchResult userResult = getLdapUserObject(this.ldapConfig, dirContext, username); + if (userResult == null) { + LOG.debug("User not found: %s", username); + return null; + } + userDn = new LdapName(userResult.getNameInNamespace()); + groups = getGroupsFromLdap(this.ldapConfig, userResult); Review comment: Done. This change required a bit more work/effort than I had expected but I agree that moving it to the LDAPRoleProvider makes more sense. Now there's no group related properties or operations within LDAPCredentialsValidator With this change I also had to move the groupFilters property to ldap authorizer druid.auth.authorizer.MyBasicLDAPAuthorizer.roleProvider.groupFilters ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
