[GitHub] [spark] xiuzhu9527 commented on a diff in pull request #36784: [SPARK-39396][SQL] Fix LDAP login exception 'error code 49 - invalid credentials'

2022-06-20 Thread GitBox


xiuzhu9527 commented on code in PR #36784:
URL: https://github.com/apache/spark/pull/36784#discussion_r901761793


##
sql/hive-thriftserver/src/main/java/org/apache/hive/service/auth/LdapAuthenticationProviderImpl.java:
##
@@ -30,21 +35,19 @@ public class LdapAuthenticationProviderImpl implements 
PasswdAuthenticationProvi
   private final String ldapURL;
   private final String baseDN;
   private final String ldapDomain;
+  private final String userDNPattern;
 
   LdapAuthenticationProviderImpl() {
 HiveConf conf = new HiveConf();
 ldapURL = conf.getVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_URL);
 baseDN = conf.getVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BASEDN);
 ldapDomain = conf.getVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_DOMAIN);
+userDNPattern = conf.get("hive.server2.authentication.ldap.userDNPattern");

Review Comment:
   Thanks. I will fix it



-- 
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.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] xiuzhu9527 commented on a diff in pull request #36784: [SPARK-39396][SQL] Fix LDAP login exception 'error code 49 - invalid credentials'

2022-06-20 Thread GitBox


xiuzhu9527 commented on code in PR #36784:
URL: https://github.com/apache/spark/pull/36784#discussion_r901283124


##
sql/hive-thriftserver/src/main/java/org/apache/hive/service/auth/LdapAuthenticationProviderImpl.java:
##
@@ -58,22 +61,46 @@ public void Authenticate(String user, String password) 
throws AuthenticationExce
 }
 
 // setup the security principal
-String bindDN;
-if (baseDN == null) {
-  bindDN = user;
+List candidatePrincipals = new ArrayList<>();
+if (StringUtils.isBlank(userDNPattern)) {
+  if (StringUtils.isNotBlank(baseDN)) {
+String pattern = "uid=" + user + "," + baseDN;
+candidatePrincipals.add(pattern);
+  }
 } else {
-  bindDN = "uid=" + user + "," + baseDN;
+  String[] patterns = userDNPattern.split(":");
+  for (String pattern : patterns) {
+if (StringUtils.contains(pattern, ",") && 
StringUtils.contains(pattern, "=")) {
+  candidatePrincipals.add(pattern.replaceAll("%s", user));
+}
+  }
+}
+
+if (candidatePrincipals.isEmpty()) {
+  candidatePrincipals = Collections.singletonList(user);
 }
-env.put(Context.SECURITY_AUTHENTICATION, "simple");
-env.put(Context.SECURITY_PRINCIPAL, bindDN);
-env.put(Context.SECURITY_CREDENTIALS, password);
 
-try {
-  // Create initial context
-  Context ctx = new InitialDirContext(env);
-  ctx.close();
-} catch (NamingException e) {
-  throw new AuthenticationException("Error validating LDAP user", e);
+for (Iterator iterator = candidatePrincipals.iterator(); 
iterator.hasNext();) {
+  String principal = iterator.next();
+
+  Hashtable env = new Hashtable();
+  env.put(Context.INITIAL_CONTEXT_FACTORY, 
"com.sun.jndi.ldap.LdapCtxFactory");
+  env.put(Context.PROVIDER_URL, ldapURL);
+  env.put(Context.SECURITY_AUTHENTICATION, "simple");
+  env.put(Context.SECURITY_PRINCIPAL, principal);
+  env.put(Context.SECURITY_CREDENTIALS, password);
+
+  try {
+
+// Create initial context
+Context ctx = new InitialDirContext(env);
+ctx.close();
+break;
+  } catch (NamingException e) {
+if (!iterator.hasNext()) {
+  throw new AuthenticationException("Error validating LDAP user", e);

Review Comment:
   This should not be after the for block because In LDAP, there are two DN 
formats: DN (cn=user, ou=people, dc=example, dc=com) and DN (uid=user, 
ou=people, dc=example, dc=com). Users may choose any one when creating a DN, so 
we can exit the for loop only after matching the successful login



-- 
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.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] xiuzhu9527 commented on a diff in pull request #36784: [SPARK-39396][SQL] Fix LDAP login exception 'error code 49 - invalid credentials'

2022-06-19 Thread GitBox


xiuzhu9527 commented on code in PR #36784:
URL: https://github.com/apache/spark/pull/36784#discussion_r901278388


##
sql/hive-thriftserver/src/main/java/org/apache/hive/service/auth/LdapAuthenticationProviderImpl.java:
##
@@ -58,22 +61,46 @@ public void Authenticate(String user, String password) 
throws AuthenticationExce
 }
 
 // setup the security principal
-String bindDN;
-if (baseDN == null) {
-  bindDN = user;
+List candidatePrincipals = new ArrayList<>();
+if (StringUtils.isBlank(userDNPattern)) {
+  if (StringUtils.isNotBlank(baseDN)) {
+String pattern = "uid=" + user + "," + baseDN;
+candidatePrincipals.add(pattern);
+  }
 } else {
-  bindDN = "uid=" + user + "," + baseDN;
+  String[] patterns = userDNPattern.split(":");
+  for (String pattern : patterns) {
+if (StringUtils.contains(pattern, ",") && 
StringUtils.contains(pattern, "=")) {
+  candidatePrincipals.add(pattern.replaceAll("%s", user));
+}
+  }
+}
+
+if (candidatePrincipals.isEmpty()) {
+  candidatePrincipals = Collections.singletonList(user);
 }
-env.put(Context.SECURITY_AUTHENTICATION, "simple");
-env.put(Context.SECURITY_PRINCIPAL, bindDN);
-env.put(Context.SECURITY_CREDENTIALS, password);
 
-try {
-  // Create initial context
-  Context ctx = new InitialDirContext(env);
-  ctx.close();
-} catch (NamingException e) {
-  throw new AuthenticationException("Error validating LDAP user", e);
+for (Iterator iterator = candidatePrincipals.iterator(); 
iterator.hasNext();) {
+  String principal = iterator.next();
+
+  Hashtable env = new Hashtable();

Review Comment:
   I think it's OK. Hashtable has been used in the latest Hive  ``` 
LdapAuthenticationProviderImpl ```



-- 
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.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org