| 1 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection: |
| 2 |
|
| 3 |
********************* |
| 4 |
LDAP Connection guide |
| 5 |
********************* |
| 6 |
|
| 7 |
This guide covers information about communicating with an LDAP server, like Microsoft Active Directory, from the Ansible host. Unlike Windows hosts, there are no builtin mechanisms to communicate and authenticate with an LDAP server, so the plugins that run on the Ansible host require some extra configuration to get working. |
| 8 |
|
| 9 |
.. note:: |
| 10 |
This guide covers LDAP communication from the Ansible host. This does not apply to the modules that run on the remote Windows hosts. See :ref:`AD Authentication in Modules <ansible_collections.microsoft.ad.docsite.guide_ad_module_authentication>` for information on how modules authentication can be configured. |
| 11 |
|
| 12 |
.. contents:: |
| 13 |
:local: |
| 14 |
:depth: 1 |
| 15 |
|
| 16 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection.requirements: |
| 17 |
|
| 18 |
Requirements |
| 19 |
============ |
| 20 |
|
| 21 |
The LDAP connection code requires the `sansldap <https://pypi.org/project/sansldap/>`_ and `pyspnego <https://pypi.org/project/pyspnego/>`_ libraries. They can be installed using ``pip`` with: |
| 22 |
|
| 23 |
.. code-block:: shell-session |
| 24 |
|
| 25 |
$ python3 -m pip install --user \ |
| 26 |
'pyspnego >= 0.8.0' |
| 27 |
sansldap |
| 28 |
|
| 29 |
.. note:: |
| 30 |
This guide assumes ``python3`` is the same Python that Ansible uses, see ``ansible --version`` for details on the Python version/location. |
| 31 |
|
| 32 |
There are also optional dependencies to provide extra features |
| 33 |
|
| 34 |
+-------------------------+-----------------------------+ |
| 35 |
| Feature | Package | |
| 36 |
+=========================+=============================+ |
| 37 |
| Kerberos Authentication | pyspnego[kerberos] >= 0.8.0 | |
| 38 |
+-------------------------+-----------------------------+ |
| 39 |
| Server Lookups | dnspython | |
| 40 |
+-------------------------+-----------------------------+ |
| 41 |
| LAPS Decryption | dpapi-ng | |
| 42 |
+-------------------------+-----------------------------+ |
| 43 |
|
| 44 |
To install all the optional features run: |
| 45 |
|
| 46 |
.. code-block:: shell-session |
| 47 |
|
| 48 |
$ python3 -m pip install --user \ |
| 49 |
dnspython \ |
| 50 |
dpapi-ng \ |
| 51 |
'pyspnego[kerberos] >= 0.8.0' |
| 52 |
|
| 53 |
The Kerberos authentication components require the Kerberos system libraries to be present. For RPM based systems, these are: |
| 54 |
|
| 55 |
.. code-block:: shell-session |
| 56 |
|
| 57 |
$ dnf install gcc python3-devel krb5-libs krb5-devel |
| 58 |
|
| 59 |
Other Linux distributions require the same packages listed above but they are likely listed under different names than what ``dnf`` uses. |
| 60 |
|
| 61 |
The :ref:`microsoft.ad.debug_ldap_client <ansible_collections.microsoft.ad.debug_ldap_client_module>`. action plugin can be used to debug the Ansible host setup and its LDAP capabilities. It includes details such as: |
| 62 |
|
| 63 |
* The Python packages related to LDAP that are installed, or import failure messages if not installed |
| 64 |
* The Kerberos host and credential cache information if the Kerberos extras are installed |
| 65 |
* The SRV lookup information if ``dnspython`` and Kerberos extras are installed |
| 66 |
|
| 67 |
To use this module simply run |
| 68 |
|
| 69 |
.. code-block:: shell-session |
| 70 |
|
| 71 |
$ ansible localhost -m microsoft.ad.debug_ldap_client |
| 72 |
|
| 73 |
|
| 74 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection.connection_options: |
| 75 |
|
| 76 |
Connection options |
| 77 |
================== |
| 78 |
|
| 79 |
Connecting to a Microsoft Active Directory or LDAP server requires information like the domain controller hostname, port, whether to use LDAPS or StartTLS, and authentication information. Some of this information can be retrieved based on the Ansible host environment but can also be manually specified through the plugin options. These options include: |
| 80 |
|
| 81 |
+---------------+--------------------------------+---------------------------------------------+ |
| 82 |
| Option | Default | Purpose | |
| 83 |
+===============+================================+=============================================+ |
| 84 |
| server | Server lookup through Kerberos | The LDAP server hostname | |
| 85 |
+---------------+--------------------------------+---------------------------------------------+ |
| 86 |
| port | 389 or 686 if tls_mode=ldaps | The LDAP port | |
| 87 |
+---------------+--------------------------------+---------------------------------------------+ |
| 88 |
| tls_mode | LDAPS if port=686 else None | TLS details - LDAP, LDAP + StartTLS, LDAPS | |
| 89 |
+---------------+--------------------------------+---------------------------------------------+ |
| 90 |
| auth_protocol | Negotiate | Authentication protocol | |
| 91 |
+---------------+--------------------------------+---------------------------------------------+ |
| 92 |
| username | None | Attempts to use Kerberos cache if available | |
| 93 |
+---------------+--------------------------------+---------------------------------------------+ |
| 94 |
| password | None | Attempts to use Kerberos cache if available | |
| 95 |
+---------------+--------------------------------+---------------------------------------------+ |
| 96 |
|
| 97 |
The server lookup details are described below. The port defaults to ``389`` unless ``tls_mode: ldaps`` is specified. The TLS mode defaults to ``ldaps`` if the port is explicitly set to ``686`` otherwise it defaults to ``389``. The authentication protocol defaults to ``negotiate`` while attempting to use the implicit credential if it's available. |
| 98 |
|
| 99 |
|
| 100 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection.server_lookup: |
| 101 |
|
| 102 |
Server lookup |
| 103 |
------------- |
| 104 |
|
| 105 |
If no server option was explicitly set, the plugin will attempt to lookup the LDAP server based on the current environment configuration. This is only possible if: |
| 106 |
|
| 107 |
* The ``dnspython`` Python package is installed |
| 108 |
* The ``pyspnego[kerberos]`` Python package for Kerberos is installed |
| 109 |
* The underlying Kerberos library has a ``default_realm`` set in the `MIT krb5.conf <https://web.mit.edu/kerberos/krb5-latest/doc/admin/host_config.html#default-realm>`_ |
| 110 |
|
| 111 |
If none of the above are true, the connection will fail and an explicit server must be supplied. If all the requirements are satisfied this is the server lookup workflow: |
| 112 |
|
| 113 |
* The ``default_realm`` of the local Kerberos configuration is retrieved |
| 114 |
* A DNS SRV lookup is done for the record ``_ldap._tcp.dc._msdcs.{{ default_realm }}`` |
| 115 |
* The DNS records are sorted by priority and weight and the first is selected |
| 116 |
* The hostname and port on the selected SRV record are used for the lookup |
| 117 |
|
| 118 |
.. note:: |
| 119 |
If an explicit port is specified, it will take priority over the port returned by the SRV record. |
| 120 |
|
| 121 |
|
| 122 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection.authentication: |
| 123 |
|
| 124 |
Authentication |
| 125 |
============== |
| 126 |
|
| 127 |
A critical component of LDAP connections is how the user authenticates itself to the server. The following authentication mechanisms are supported: |
| 128 |
|
| 129 |
+----------------+---------------------+----------------------------+ |
| 130 |
| Authentication | Supports Encryption | Implicit Credential | |
| 131 |
+================+=====================+============================+ |
| 132 |
| simple | No - TLS needed | Yes - Appears as Anonymous | |
| 133 |
+----------------+---------------------+----------------------------+ |
| 134 |
| certificate | Yes | No | |
| 135 |
+----------------+---------------------+----------------------------+ |
| 136 |
| negotiate | Yes | Yes - With Kerberos | |
| 137 |
+----------------+---------------------+----------------------------+ |
| 138 |
| kerberos | Yes | Yes | |
| 139 |
+----------------+---------------------+----------------------------+ |
| 140 |
| ntlm | Yes | No | |
| 141 |
+----------------+---------------------+----------------------------+ |
| 142 |
|
| 143 |
Unless otherwise specified, the default authentication protocol used is ``negotiate`` which relies on the ``pyspnego`` library. See :ref:`requirements <ansible_collections.microsoft.ad.docsite.guide_ldap_connection.requirements>` for more information on how to install this requirement. |
| 144 |
|
| 145 |
Any protocol that does not support encryption must either be used with LDAPS, StartTLS, or they must explicitly disable the encryption checks with the ``encrypt: false`` option. Disabling encryption is not recommended as it will send the credentials without any protection and any of the data exchanged can be seen by anyone. It also requires the target server to allow unencrypted connections as they can reject such connections. |
| 146 |
|
| 147 |
Implicit credential support documents whether the authentication protocol can authenticate without an explicit ``username`` and ``password`` specified. Currently only ``simple`` and ``negotiate/kerberos`` supports implicit credentials. See each protocol section for more details. |
| 148 |
|
| 149 |
Simple |
| 150 |
------ |
| 151 |
|
| 152 |
Simple authentication is the most basic authentication protocol supported. It works by sending the username and password in plaintext to the server, similar to HTTP Basic authentication. Microsoft AD requires the username to be the ``sAMAccountName`` or ``userPrincipalName`` of the account but other LDAP implementations require the LDAP ``distinguishedName``. While it is possible to do an anonymous bind when no username or password is specified, it is likely the server will reject any search operations unless it is authenticated with an actual users credentials. Simple authentication is not allowed over a connection that is not protected by TLS. It is possible to allow simple authentication over such connections by disabling the encryption check but this is not recommended. |
| 153 |
|
| 154 |
.. warning:: |
| 155 |
Simple authentication should be avoided unless TLS is used, either through LDAPS or StartTLS. Failure to use use LDAPS will expose the credentials used during the authentication and the subsequent data unprotected from eavesdropping or tampering. |
| 156 |
|
| 157 |
|
| 158 |
Certificate |
| 159 |
----------- |
| 160 |
|
| 161 |
Certificate authentication uses TLS client authentication as part of the TLS handshake to authenticate the user to the host. As it is part of the TLS handshake, it can only be used over an LDAPS connection or with StartTLS. It uses a certificate and certificate key of the user to authenticate as. There are three options that can be used to specify a client certificate and key to use for authentication: |
| 162 |
|
| 163 |
* ``certificate`` - The certificate, and optionally bundled key |
| 164 |
* ``certificate_key`` - The certificate key if not bundled in ``certificate`` |
| 165 |
* ``certificate_password`` - The password used to decrypt the certificate key |
| 166 |
|
| 167 |
The ``certificate`` and ``certificate_key`` can either be a file path to the certificate and key or they can be a string of the PEM encoded certificate/key. The ``certificate`` file path can be a PEM, DER, or PKCS12/PFX encoded certificate with optional key bundle whereas the ``certificate_key`` file path can be a PEM or DER encoded key. If the key inside the PEM, DER, or PKCS12/PFX content is encrypted, the ``certificate_password`` can be used to specify the password used to decrypt the key. |
| 168 |
|
| 169 |
.. note:: |
| 170 |
Setting these options are dependent on the plugin itself, the keys here reflect the option name and not necessarily Ansible variables that can be set and read automatically by a plugin. |
| 171 |
|
| 172 |
|
| 173 |
Negotiate |
| 174 |
--------- |
| 175 |
|
| 176 |
Negotiate authentication is the default authentication protocol used by LDAP connections. It is a combination of both ``kerberos`` and ``ntlm`` with the client negotiating which one to use. It will favor ``kerberos`` if it is available and fallback to ``ntlm`` if not. The ``pyspnego`` Python package provides ``negotiate`` with just ``ntlm`` support, ``kerberos`` support is provided by the ``pyspnego[kerberos]`` extras option. See :ref:`requirements <ansible_collections.microsoft.ad.docsite.guide_ldap_connection.requirements>` for more information on how to install this requirement. |
| 177 |
|
| 178 |
Kerberos |
| 179 |
-------- |
| 180 |
|
| 181 |
Kerberos authentication is a modern authentication protocol supported by Microsoft AD servers and is the preferred protocol for authentication. It is only available if the ``pyspnego[kerberos]`` extras package is installed and the host has been configured properly. Typically this configuration is done through the `/etc/krb5.conf <https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html>`_ file on the system. This guide will not go into configuring the host's Kerberos settings as it is environment specific. |
| 182 |
|
| 183 |
A good way to ensure the host has been configured to use Kerberos correctly is to ensure the following commands work: |
| 184 |
|
| 185 |
.. code-block:: shell-session |
| 186 |
|
| 187 |
$ python -c "import krb5" |
| 188 |
$ kinit [email protected] |
| 189 |
$ kvno ldap/dc.domain.realm |
| 190 |
|
| 191 |
.. note:: |
| 192 |
The ``kvno`` command is an MIT krb5 specific command, it is not available on hosts that use Heimdal krb5 like macOS. |
| 193 |
|
| 194 |
The ``python`` command ensures the required Python libraries have been installed. The ``kinit`` command will retrieve a Kerberos ticket for the user specified and the ``kvno`` command will attempt to retrieve a service ticket for the service principal name (SPN) requested. If both commands work then there is a good chance Kerberos authentication will work with the LDAP connection. |
| 195 |
|
| 196 |
Using the ``kinit`` command it is possible to set up a credential cache for Ansible to use for authentication. By having a credential retrieved using ``kinit``, it is possible to authenticate with the LDAP server without any explicit username and password set in Ansible. It is still possible to use Kerberos with explicit credentials. |
| 197 |
|
| 198 |
NTLM |
| 199 |
---- |
| 200 |
|
| 201 |
NTLM authentication is a simple authentication protocol that can be used by itself or as part of the ``negotiate`` fallback if ``kerberos`` is unavailable. Unlike ``kerberos`` support, it does not normally support implicit credentials so typically needs an explicit username and password specified to be used. It requires no extra host configuration and should work once ``pyspnego`` has been installed. |
| 202 |
|
| 203 |
.. warning:: |
| 204 |
While NTLM does support encryption it is considered weak by modern standards. It is recommended to only use NTLM with an LDAPS or StartTLS connection where the stronger encryption and server checks provided by TLS mitigate the weaknesses in NTLM. |
| 205 |
|
| 206 |
|
| 207 |
.. _ansible_collections.microsoft.ad.docsite.guide_ldap_connection.cert_validation: |
| 208 |
|
| 209 |
Certificate validation |
| 210 |
====================== |
| 211 |
|
| 212 |
Using LDAPS or LDAP over StartTLS will perform a TLS handshake which by default has the client attempting to validate the certificate presented by the server. If the certificate chain cannot be trusted, or the hostname does not match the one being requested the connection will fail with an error indicating why. The default trust store location is dependent on the Python configuration and what SSL library it has been linked to. Typically it would be the OS' default trust store but when in doubt the following Python code can be used to verify the LDAPS certificate. Make sure to change ``hostname`` to the hostname of the LDAP server that should be tested. |
| 213 |
|
| 214 |
.. code-block:: python |
| 215 |
|
| 216 |
import socket |
| 217 |
import ssl |
| 218 |
|
| 219 |
hostname = 'dc.domain.com' |
| 220 |
port = 636 |
| 221 |
context = ssl.create_default_context() |
| 222 |
|
| 223 |
with socket.create_connection((hostname, port)) as sock: |
| 224 |
with context.wrap_socket(sock, server_hostname=hostname) as ssock: |
| 225 |
print(ssock.version()) |
| 226 |
|
| 227 |
The ``ca_cert`` connection option can be used to set an explicit CA bundle to use for verification. This is useful if the CA bundle is not part of the OS store but located somewhere else on the filesystem. The value can be in the form of: |
| 228 |
|
| 229 |
* a file path to a PEM or DER encoded bundle of certificates |
| 230 |
* A directory path that contains several CA certificates in the PEM format following an OpenSSL specific layout as document by `CApath <https://www.openssl.org/docs/manmaster/man3/SSL_CTX_load_verify_locations.html>`_ |
| 231 |
* A string containing PEM encoded certificates |
| 232 |
|
| 233 |
It is also possible to disable certificate verification using the ``cert_validation`` connection option. The default is ``always`` but can be set to ``ignore`` to disable all checks or ``ignore_hostname`` to disable just the hostname check. This can be useful for test environments that use self signed certificates but it should not be used in a production environment. |
| 234 |
|
| 235 |
.. warning:: |
| 236 |
Disabling certificate validation removes a lot of the benefits that TLS offers. There is no way to verify the target server is who it says that it is. |