Skip to content

Commit 7b62eb0

Browse files
Isaiah InuwaIsaiah Inuwa
authored andcommitted
Add test configuration for LDAP TLS server
1 parent c820654 commit 7b62eb0

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/libraries/Common/tests/System/DirectoryServices/LDAP.Configuration.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,34 @@ and to test and view status
2828

2929
docker exec -it slapd01 slapcat
3030

31+
SLAPD OPENLDAP SERVER WITH TLS
32+
==============================
33+
34+
The osixia/openldap container image automatically creates a TLS lisener with a self-signed certificate. This can be used to test TLS.
35+
36+
Start the container, with TLS on port 1636, without client certificate verification:
37+
38+
docker run --publish 1389:389 --publish 1636:636 --name ldap --hostname ldap.local --detach --rm --env LDAP_TLS_VERIFY_CLIENT=never --env LDAP_ADMIN_PASSWORD=password osixia/openldap --loglevel debug
39+
40+
Extract the CA certificate and write to a temporary file:
41+
42+
docker exec ldap cat /container/service/slapd/assets/certs/ca.crt > /tmp/ca.crt
43+
44+
Set the LDAP client CA certificate path in `/etc/ldap/ldap.conf` so OpenLDAP trusts the self-signed certificate:
45+
46+
# /etc/ldap/ldap.conf
47+
#...
48+
TLS_CACERT /tmp/ca.crt
49+
50+
Finally, map the `ldap.local` hostname manually set above to the loopback address:
51+
52+
# /etc/hosts
53+
127.0.0.1 ldap.local
54+
55+
To test and view the status:
56+
57+
ldapsearch -H ldaps://ldap.local:1636 -b dc=example,dc=org -x -D cn=admin,dc=example,dc=org -w password
58+
3159
ACTIVE DIRECTORY
3260
================
3361

@@ -83,5 +111,14 @@ Note:
83111
<Password>%TESTPASSWORD%</Password>
84112
<AuthenticationTypes>ServerBind,None</AuthenticationTypes>
85113
</Connection>
114+
<Connection Name="SLAPD OPENLDAP SERVER TLS">
115+
<ServerName>ldap.local</ServerName>
116+
<SearchDN>DC=example,DC=org</SearchDN>
117+
<Port>1636</Port>
118+
<User>cn=admin,dc=example,dc=org</User>
119+
<Password>password</Password>
120+
<AuthenticationTypes>ServerBind,None</AuthenticationTypes>
121+
<UseTls>true</UseTls>
122+
</Connection>
86123

87124
</Configuration>

src/libraries/Common/tests/System/DirectoryServices/LdapConfiguration.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ namespace System.DirectoryServices.Tests
1010
{
1111
internal class LdapConfiguration
1212
{
13-
private LdapConfiguration(string serverName, string searchDn, string userName, string password, string port, AuthenticationTypes at)
13+
private LdapConfiguration(string serverName, string searchDn, string userName, string password, string port, AuthenticationTypes at, bool useTls)
1414
{
1515
ServerName = serverName;
1616
SearchDn = searchDn;
1717
UserName = userName;
1818
Password = password;
1919
Port = port;
2020
AuthenticationTypes = at;
21+
UseTls = useTls;
2122
}
2223

2324
private static LdapConfiguration s_ldapConfiguration = GetConfiguration("LDAP.Configuration.xml");
@@ -30,6 +31,7 @@ private LdapConfiguration(string serverName, string searchDn, string userName, s
3031
internal string Port { get; set; }
3132
internal string SearchDn { get; set; }
3233
internal AuthenticationTypes AuthenticationTypes { get; set; }
34+
internal bool UseTls { get; set; }
3335
internal string LdapPath => string.IsNullOrEmpty(Port) ? $"LDAP://{ServerName}/{SearchDn}" : $"LDAP://{ServerName}:{Port}/{SearchDn}";
3436
internal string RootDSEPath => string.IsNullOrEmpty(Port) ? $"LDAP://{ServerName}/rootDSE" : $"LDAP://{ServerName}:{Port}/rootDSE";
3537
internal string UserNameWithNoDomain
@@ -104,6 +106,7 @@ internal static LdapConfiguration GetConfiguration(string configFile)
104106
string user = "";
105107
string password = "";
106108
AuthenticationTypes at = AuthenticationTypes.None;
109+
bool useTls = false;
107110

108111
XElement child = connection.Element("ServerName");
109112
if (child != null)
@@ -132,6 +135,12 @@ internal static LdapConfiguration GetConfiguration(string configFile)
132135
password = val;
133136
}
134137

138+
child = connection.Element("UseTls");
139+
if (child != null)
140+
{
141+
useTls = bool.Parse(child.Value);
142+
}
143+
135144
child = connection.Element("AuthenticationTypes");
136145
if (child != null)
137146
{
@@ -161,7 +170,7 @@ internal static LdapConfiguration GetConfiguration(string configFile)
161170
at |= AuthenticationTypes.Signing;
162171
}
163172

164-
ldapConfig = new LdapConfiguration(serverName, searchDn, user, password, port, at);
173+
ldapConfig = new LdapConfiguration(serverName, searchDn, user, password, port, at, useTls);
165174
}
166175
}
167176
catch (Exception ex)

src/libraries/System.DirectoryServices.Protocols/tests/DirectoryServicesProtocolsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ private LdapConnection GetConnection()
630630
// Set server protocol before bind; OpenLDAP servers default
631631
// to LDAP v2, which we do not support, and will return LDAP_PROTOCOL_ERROR
632632
connection.SessionOptions.ProtocolVersion = 3;
633+
connection.SessionOptions.SecureSocketLayer = LdapConfiguration.Configuration.UseTls;
633634
connection.Bind();
634635

635636
connection.Timeout = new TimeSpan(0, 3, 0);

0 commit comments

Comments
 (0)