Skip to content

Commit

Permalink
CB-5954. Fixed if bind DN not filled
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisSinelnikov committed Dec 23, 2024
1 parent ea3221b commit 7f26c4b
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.directory.*;
import java.util.HashMap;

Check warning on line 41 in server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Wrong lexicographical order for 'java.util.HashMap' import. Should be before 'javax.naming.directory.*'. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java:41:1: warning: Wrong lexicographical order for 'java.util.HashMap' import. Should be before 'javax.naming.directory.*'. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)
import java.util.Hashtable;

Check warning on line 42 in server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Wrong lexicographical order for 'java.util.Hashtable' import. Should be before 'javax.naming.directory.*'. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java:42:1: warning: Wrong lexicographical order for 'java.util.Hashtable' import. Should be before 'javax.naming.directory.*'. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)
import java.util.Map;

Check warning on line 43 in server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Wrong lexicographical order for 'java.util.Map' import. Should be before 'javax.naming.directory.*'. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java:43:1: warning: Wrong lexicographical order for 'java.util.Map' import. Should be before 'javax.naming.directory.*'. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)
Expand Down Expand Up @@ -75,10 +72,7 @@ public Map<String, Object> authExternalUser(
Hashtable<String, String> environment = creteAuthEnvironment(ldapSettings);

Map<String, Object> userData = null;
if (isFullDN(userName) && CommonUtils.isNotEmpty(ldapSettings.getLoginAttribute())) {
if (CommonUtils.isEmpty(ldapSettings.getBaseDN())) {
throw new DBException("Base DN doesnt configure");
}
if (!isFullDN(userName) && CommonUtils.isNotEmpty(ldapSettings.getLoginAttribute())) {
userData = validateAndLoginUserAccessByUsername(userName, password, ldapSettings);

}
Expand Down Expand Up @@ -217,8 +211,12 @@ private String findUserDN(DirContext adminContext, LdapSettings ldapSettings, St
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(new String[]{"distinguishedName"});
String baseDN = ldapSettings.getBaseDN();
if (CommonUtils.isEmpty(baseDN)) {
baseDN = getRootDN(adminContext);
}

NamingEnumeration<SearchResult> results = adminContext.search(ldapSettings.getBaseDN(), searchFilter, searchControls);
NamingEnumeration<SearchResult> results = adminContext.search(baseDN, searchFilter, searchControls);

if (results.hasMore()) {
SearchResult result = results.next();
Expand All @@ -231,6 +229,19 @@ private String findUserDN(DirContext adminContext, LdapSettings ldapSettings, St
}
}

private String getRootDN(DirContext adminContext) throws DBException {
try {
Attributes attributes = adminContext.getAttributes("", new String[]{"namingContexts"});
Attribute namingContexts = attributes.get("namingContexts");
if (namingContexts != null && namingContexts.size() > 0) {
return (String) namingContexts.get(0);
}
throw new DBException("Root DN not found in namingContexts");
} catch (Exception e) {
throw new DBException("Error retrieving root DN: " + e.getMessage(), e);
}
}

private Map<String, Object> authenticateWithUserLogin(String userDN, String password, LdapSettings ldapSettings, String login) throws DBException {

Check warning on line 245 in server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Line is longer than 140 characters (found 151). Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java:245:0: warning: Line is longer than 140 characters (found 151). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

Check warning on line 245 in server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.service.ldap.auth/src/io/cloudbeaver/service/ldap/auth/LdapAuthProvider.java#L245

Line is longer than 140 characters (found 151).
Hashtable<String, String> userEnvironment = creteAuthEnvironment(ldapSettings);
userEnvironment.put(Context.SECURITY_PRINCIPAL, userDN);
Expand Down

0 comments on commit 7f26c4b

Please sign in to comment.