jon-wei commented on a change in pull request #6972: Support LDAP authentication/authorization URL: https://github.com/apache/incubator-druid/pull/6972#discussion_r270208298
########## File path: extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java ########## @@ -0,0 +1,420 @@ +/* + * 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.JacksonInject; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.google.inject.Provider; +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.DefaultPasswordProvider; +import org.apache.druid.metadata.PasswordProvider; +import org.apache.druid.security.basic.BasicAuthDBConfig; +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.db.cache.BasicAuthenticatorCacheManager; +import org.apache.druid.security.basic.authentication.entity.BasicAuthConfig; +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.atomic.AtomicReference; +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 BasicAuthenticatorCacheManager cacheManager; + + private AtomicReference<BasicAuthDBConfig> dbConfig = new AtomicReference<>(); + + @JsonCreator + public LDAPCredentialsValidator( + @JacksonInject Provider<BasicAuthenticatorCacheManager> cacheManager, + @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.cacheManager = cacheManager.get(); + this.dbConfig.set(new BasicAuthDBConfig( + null, + null, + null, + null, + null, + true, + BasicAuthDBConfig.DEFAULT_CACHE_NOTIFY_TIMEOUT_MS, + credentialIterations == null ? BasicAuthUtils.DEFAULT_KEY_ITERATIONS : credentialIterations, + url, + bindUser, + bindPassword, + baseDn, + userSearch, + userAttribute, + groupFilters, + 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.dbConfig.get().getCredentialCacheSize(), + this.dbConfig.get().getCredentialVerifyDuration(), + this.dbConfig.get().getCredentialMaxDuration() + ); + } + + Properties bindProperties(BasicAuthDBConfig dbConfig) + { + Properties properties = commonProperties(dbConfig); + properties.put(Context.SECURITY_PRINCIPAL, dbConfig.getBindUser()); + properties.put(Context.SECURITY_CREDENTIALS, dbConfig.getBindPassword().getPassword()); + return properties; + } + + Properties userProperties(BasicAuthDBConfig dbConfig, LdapName userDn, char[] password) + { + Properties properties = commonProperties(dbConfig); + properties.put(Context.SECURITY_PRINCIPAL, userDn.toString()); + properties.put(Context.SECURITY_CREDENTIALS, String.valueOf(password)); + return properties; + } + + Properties commonProperties(BasicAuthDBConfig dbConfig) + { + Properties properties = new Properties(); + properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); + properties.put(Context.PROVIDER_URL, dbConfig.getUrl()); + properties.put(Context.SECURITY_AUTHENTICATION, "simple"); + if (StringUtils.toLowerCase(dbConfig.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 + ) + { + updateConfig(authenticatorName); + BasicAuthDBConfig currentDBConfig = this.dbConfig.get(); + 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(currentDBConfig)); + + try { + SearchResult userResult = getLdapUserObject(currentDBConfig, dirContext, username); + if (userResult == null) { + LOG.debug("User not found: %s", username); + return null; + } + userDn = new LdapName(userResult.getNameInNamespace()); + groups = getGroupsFromLdap(currentDBConfig, userResult); + if (groups == null || groups.isEmpty()) { Review comment: I don't think not having a group assigned should be authentication failure (Suppose a user only cares about using LDAP for authentication and doesn't really care about roles/groups), if the groups are necessary for LDAP-based authorization checks then the missing groups should be checked in the authorizer, not the authenticator ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org For additional commands, e-mail: commits-h...@druid.apache.org