Project: ClickHouse (App Definition and Development / Database)
File: ldap.md

slug: /en/operations/external-authenticators/ldap title: "LDAP" import SelfManaged from '@site/docs/en/snippets/selfmanagedonlynoroadmap.md'; <SelfManaged /> LDAP server can be used to authenticate ClickHouse users. There are two different approaches for doing this: Use LDAP as an external authenticator for existing users, which are defined in `users.xml` or in local access control paths. Use LDAP as an external user directory and allow locally undefined users to be authenticated if they exist on the LDAP server. For both of these approaches, an internally named LDAP server must be defined in the ClickHouse config so that other parts of the config can refer to it. To define LDAP server you must add `ldap_servers` section to the `config.xml`. Example ```xml <clickhouse> <!- ... --> <ldap_servers> <!- Typical LDAP server. --> <myldapserver> <host>localhost</host> <port>636</port> <binddn>uid={username},ou=users,dc=example,dc=com</bind_dn> <verificationcooldown>300</verificationcooldown> <enabletls>yes</enabletls> <tlsminimumprotocolversion>tls1.2</tlsminimumprotocolversion> <tlsrequirecert>demand</tlsrequirecert> <tlscertfile>/path/to/tlscertfile</tlscertfile> <tlskeyfile>/path/to/tlskeyfile</tlskeyfile> <tlscacertfile>/path/to/tlscacertfile</tlscacert_file> <tlscacertdir>/path/to/tlscacertdir</tlscacert_dir> <tlsciphersuite>ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:AES256-GCM-SHA384</tlsciphersuite> </myldapserver> <!- Typical Active Directory with configured user DN detection for further role mapping. --> <myadserver> <host>localhost</host> <port>389</port> <binddn>EXAMPLE\{username}</bind_dn> <userdndetection> <basedn>CN=Users,DC=example,DC=com</basedn> <searchfilter>(&amp;(objectClass=user)(sAMAccountName={username}))</search_filter> </userdndetection> <enabletls>no</enabletls> </myadserver> </ldap_servers> </clickhouse> ``` Note, that you can define multiple LDAP servers inside the `ldap_servers` section using distinct names. Parameters `host` LDAP server hostname or IP, this parameter is mandatory and cannot be empty. `port` LDAP server port, default is `636` if `enable_tls` is set to `true`, `389` otherwise. `bind_dn` Template used to construct the DN to bind to. The resulting DN will be constructed by replacing all `{user_name}` substrings of the template with the actual user name during each authentication attempt. `userdndetection` Section with LDAP search parameters for detecting the actual user DN of the bound user. This is mainly used in search filters for further role mapping when the server is Active Directory. The resulting user DN will be used when replacing `{user_dn}` substrings wherever they are allowed. By default, user DN is set equal to bind DN, but once search is performed, it will be updated with to the actual detected user DN value. `base_dn` Template used to construct the base DN for the LDAP search. The resulting DN will be constructed by replacing all `{username}` and `{binddn}` substrings of the template with the actual user name and bind DN during the LDAP search. `scope` Scope of the LDAP search. Accepted values are: `base`, `one_level`, `children`, `subtree` (the default). `search_filter` Template used to construct the search filter for the LDAP search. The resulting filter will be constructed by replacing all `{username}`, `{binddn}`, and `{base_dn}` substrings of the template with the actual user name, bind DN, and base DN during the LDAP search. Note, that the special characters must be escaped properly in

`verification_cooldown` A period of time, in seconds, after a successful bind attempt, during which the user will be assumed to be successfully authenticated for all consecutive requests without contacting the LDAP server. Specify `0` (the default) to disable caching and force contacting the LDAP server for each authentication request. `enable_tls` A flag to trigger the use of the secure connection to the LDAP server. Specify `no` for plain text `ldap://` protocol (not recommended). Specify `yes` for LDAP over SSL/TLS `ldaps://` protocol (recommended, the default). Specify `starttls` for legacy StartTLS protocol (plain text `ldap://` protocol, upgraded to TLS). `tlsminimumprotocol_version` The minimum protocol version of SSL/TLS. Accepted values are: `ssl2`, `ssl3`, `tls1.0`, `tls1.1`, `tls1.2` (the default). `tlsrequirecert` SSL/TLS peer certificate verification behavior. Accepted values are: `never`, `allow`, `try`, `demand` (the default). `tlscertfile` Path to certificate file. `tlskeyfile` Path to certificate key file. `tlscacert_file` Path to CA certificate file. `tlscacert_dir` Path to the directory containing CA certificates. `tlsciphersuite` Allowed cipher suite (in OpenSSL notation). A remote LDAP server can be used as a method for verifying passwords for locally defined users (users defined in `users.xml` or in local access control paths). To achieve this, specify previously defined LDAP server name instead of `password` or similar sections in the user definition. At each login attempt, ClickHouse tries to "bind" to the specified DN defined by the `bind_dn` parameter in the using the provided credentials, and if successful, the user is considered authenticated. This is often called a "simple bind" method. Example ```xml <clickhouse> <!- ... --> <users> <!- ... --> <my_user> <!- ... --> <ldap> <server>myldapserver</server> </ldap> </my_user> </users> </clickhouse> ``` Note, that user `myuser` refers to `myldap_server`. This LDAP server must be configured in the main `config.xml` file as described previously. When SQL-driven is enabled, users that are authenticated by LDAP servers can also be created using the statement. Query: ```sql CREATE USER myuser IDENTIFIED WITH ldap SERVER 'myldap_server'; ``` In addition to the locally defined users, a remote LDAP server can be used as a source of user definitions. To achieve this, specify previously defined LDAP server name (see ) in the `ldap` section inside the `users_directories` section of the `config.xml` file. At each login attempt, ClickHouse tries to find the user definition locally and authenticate it as usual. If the user is not defined, ClickHouse will assume the definition exists in the external LDAP directory and will try to "bind" to the specified DN at the LDAP server using the provided credentials. If successful, the user will be considered existing and authenticated. The user will be assigned roles from the list specified in the `roles` section. Additionally, LDAP "search" can be performed and results can be transformed and treated as role names and then be assigned to the user if the `role_mapping` section is also

All this implies that the SQL-driven is enabled and roles are created using the statement. Example Goes into `config.xml`. ```xml <clickhouse> <!- ... --> <user_directories> <!- Typical LDAP server. --> <ldap> <server>myldapserver</server> <roles> <mylocalrole1 /> <mylocalrole2 /> </roles> <role_mapping> <basedn>ou=groups,dc=example,dc=com</basedn> <scope>subtree</scope> <searchfilter>(&amp;(objectClass=groupOfNames)(member={binddn}))</search_filter> <attribute>cn</attribute> <prefix>clickhouse_</prefix> </role_mapping> </ldap> <!- Typical Active Directory with role mapping that relies on the detected user DN. --> <ldap> <server>myadserver</server> <role_mapping> <basedn>CN=Users,DC=example,DC=com</basedn> <attribute>CN</attribute> <scope>subtree</scope> <searchfilter>(&amp;(objectClass=group)(member={userdn}))</search_filter> <prefix>clickhouse_</prefix> </role_mapping> </ldap> </user_directories> </clickhouse> ``` Note that `myldapserver` referred in the `ldap` section inside the `user_directories` section must be a previously defined LDAP server that is configured in the `config.xml` (see ). Parameters `server` One of LDAP server names defined in the `ldap_servers` config section above. This parameter is mandatory and cannot be empty. `roles` Section with a list of locally defined roles that will be assigned to each user retrieved from the LDAP server. If no roles are specified here or assigned during role mapping (below), user will not be able to perform any actions after authentication. `role_mapping` Section with LDAP search parameters and mapping rules. When a user authenticates, while still bound to LDAP, an LDAP search is performed using `search_filter` and the name of the logged-in user. For each entry found during that search, the value of the specified attribute is extracted. For each attribute value that has the specified prefix, the prefix is removed, and the rest of the value becomes the name of a local role defined in ClickHouse, which is expected to be created beforehand by the statement. There can be multiple `role_mapping` sections defined inside the same `ldap` section. All of them will be applied. `base_dn` Template used to construct the base DN for the LDAP search. The resulting DN will be constructed by replacing all `{username}`, `{binddn}`, and `{user_dn}` substrings of the template with the actual user name, bind DN, and user DN during each LDAP search. `scope` Scope of the LDAP search. Accepted values are: `base`, `one_level`, `children`, `subtree` (the default). `search_filter` Template used to construct the search filter for the LDAP search. The resulting filter will be constructed by replacing all `{username}`, `{binddn}`, `{userdn}`, and `{basedn}` substrings of the template with the actual user name, bind DN, user DN, and base DN during each LDAP search. Note, that the special characters must be escaped properly in XML. `attribute` Attribute name whose values will be returned by the LDAP search. `cn`, by default. `prefix` Prefix, that will be expected to be in front of each string in the original list of strings returned by the LDAP search. The prefix will be removed from the original strings and the resulting strings will be treated as local role names. Empty by default.