Skip to content

Commit 925e257

Browse files
[enhance](auth) introduction of ldaps support via configuration property (#60275)
### What problem does this PR solve? This PR adds new configuration property **ldap_use_ssl** to enable usage of LDAPS to establish connection to LDAP instance. If **ldap_use_ssl** in ldap.conf is specified as **true** - LDAPS is used to create connection string. If **ldap_use_ssl** in ldap.conf is not specified or specified as **false** - LDAP is used to create connection string as now.
1 parent a597240 commit 925e257

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

conf/ldap.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ldap_group_basedn = ou=group,dc=domain,dc=com
4444

4545
# ldap_user_cache_timeout_s = 5 * 60;
4646

47+
## ldap_use_ssl - use secured connection to LDAP server if required (disabled by default). Note: When enabling SSL, ensure ldap_port is set appropriately (typically 636 for LDAPS instead of 389 for LDAP).
48+
# ldap_use_ssl = false
49+
4750
# LDAP pool configuration
4851
# https://docs.spring.io/spring-ldap/docs/2.3.3.RELEASE/reference/#pool-configuration
4952
# ldap_pool_max_active = 8

fe/fe-common/src/main/java/org/apache/doris/common/LdapConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,22 @@ public class LdapConfig extends ConfigBase {
157157
*/
158158
@ConfigBase.ConfField
159159
public static boolean ldap_pool_test_while_idle = true;
160+
161+
/**
162+
* Flag to enable usage of LDAPS.
163+
*/
164+
@ConfigBase.ConfField
165+
public static boolean ldap_use_ssl = false;
166+
167+
/**
168+
* The method constructs the correct URL connection string for the specified host and port depending on
169+
* the value of the {@code ldap_use_ssl} property.
170+
* If {@code ldap_use_ssl} is true, LDAPS is used as the protocol.
171+
* If {@code ldap_use_ssl} is false or not specified, LDAP is used as the protocol.
172+
* @param hostPortInAccessibleFormat the host and port in accessible format (for example, "host:port")
173+
* @return the LDAP or LDAPS connection URL string
174+
*/
175+
public static String getConnectionURL(String hostPortInAccessibleFormat) {
176+
return ((LdapConfig.ldap_use_ssl ? "ldaps" : "ldap") + "://" + hostPortInAccessibleFormat);
177+
}
160178
}

fe/fe-core/src/main/java/org/apache/doris/mysql/authenticate/ldap/LdapClient.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public ClientInfo(String ldapPassword) {
6565

6666
private void setLdapTemplateNoPool(String ldapPassword) {
6767
LdapContextSource contextSource = new LdapContextSource();
68-
String url = "ldap://" + NetUtils
69-
.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port);
68+
String url = LdapConfig.getConnectionURL(
69+
NetUtils.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port));
7070

7171
contextSource.setUrl(url);
7272
contextSource.setUserDn(LdapConfig.ldap_admin_name);
@@ -78,8 +78,8 @@ private void setLdapTemplateNoPool(String ldapPassword) {
7878

7979
private void setLdapTemplatePool(String ldapPassword) {
8080
LdapContextSource contextSource = new LdapContextSource();
81-
String url = "ldap://" + NetUtils
82-
.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port);
81+
String url = LdapConfig.getConnectionURL(
82+
NetUtils.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port));
8383

8484
contextSource.setUrl(url);
8585
contextSource.setUserDn(LdapConfig.ldap_admin_name);
@@ -108,6 +108,7 @@ private void setLdapTemplatePool(String ldapPassword) {
108108
public boolean checkUpdate(String ldapPassword) {
109109
return this.ldapPassword == null || !this.ldapPassword.equals(ldapPassword);
110110
}
111+
111112
}
112113

113114
private void init() {

fe/fe-core/src/test/java/org/apache/doris/mysql/authenticate/ldap/LdapClientTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import org.apache.doris.common.Config;
2121
import org.apache.doris.common.LdapConfig;
22+
import org.apache.doris.common.util.NetUtils;
2223

2324
import mockit.Expectations;
2425
import mockit.Tested;
26+
import org.junit.After;
2527
import org.junit.Assert;
2628
import org.junit.Before;
2729
import org.junit.Test;
@@ -43,6 +45,7 @@ public void setUp() {
4345
LdapConfig.ldap_user_basedn = "dc=baidu,dc=com";
4446
LdapConfig.ldap_group_basedn = "ou=group,dc=baidu,dc=com";
4547
LdapConfig.ldap_user_filter = "(&(uid={login}))";
48+
LdapConfig.ldap_use_ssl = false;
4649
}
4750

4851
@Test
@@ -95,4 +98,28 @@ public void testGetGroups() {
9598
};
9699
Assert.assertEquals(1, ldapClient.getGroups("zhangsan").size());
97100
}
101+
102+
@Test
103+
public void testSecuredProtocolIsUsed() {
104+
//testing default case with not specified property ldap_use_ssl or it is specified as false
105+
String insecureUrl = LdapConfig.getConnectionURL(
106+
NetUtils.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port));
107+
108+
Assert.assertNotNull("connection URL should not be null", insecureUrl);
109+
Assert.assertTrue("with ldap_use_ssl = false or not specified URL should start with ldap, but received: " + insecureUrl,
110+
insecureUrl.startsWith("ldap://"));
111+
112+
//testing new case with specified property ldap_use_ssl as true
113+
LdapConfig.ldap_use_ssl = true;
114+
String secureUrl = LdapConfig.getConnectionURL(
115+
NetUtils.getHostPortInAccessibleFormat(LdapConfig.ldap_host, LdapConfig.ldap_port));
116+
Assert.assertNotNull("connection URL should not be null", secureUrl);
117+
Assert.assertTrue("with ldap_use_ssl = true URL should start with ldaps, but received: " + secureUrl,
118+
secureUrl.startsWith("ldaps://"));
119+
}
120+
121+
@After
122+
public void tearDown() {
123+
LdapConfig.ldap_use_ssl = false; // restoring default value for other tests
124+
}
98125
}

0 commit comments

Comments
 (0)