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<String> 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<String> iterator = candidatePrincipals.iterator();
iterator.hasNext();) {
+ String principal = iterator.next();
+
+ Hashtable<String, Object> env = new Hashtable<String, Object>();
+ 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]