|
| 1 | +# nginx-ldap-auth |
| 2 | + |
| 3 | +Reference implementation of method for authenticating users on behalf of servers proxied by NGINX or NGINX Plus |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +**Note:** For ease of reading, this document refers to [NGINX Plus](http://www.nginx.com/products/), but it also applies to [open source NGINX](http://www.nginx.org/en). The prerequisite [ngx_http_auth_request_module](http://nginx.org/en/docs/http/ngx_http_auth_request_module.html) module is included both in [NGINX Plus packages](http://cs.nginx.com/repo_setup) and [prebuilt open source NGINX binaries](http://nginx.org/en/linux_packages.html). |
| 8 | + |
| 9 | +The ngx-ldap-auth software is a reference implementation of a method for authenticating users who request protected resources from servers proxied by NGINX Plus. It includes a daemon (*ldap-auth*) that communicates with an authentication server, and a sample daemon that stands in for an actual back-end server during testing, by generating an authentication cookie based on the user’s credentials. The daemons are written in Python for use with a Lightweight Directory Access Protocol (LDAP) authentication server (OpenLDAP or Microsoft Windows Active Directory 2003 and 2012). |
| 10 | + |
| 11 | +The ldap-auth daemon, which mediates between NGINX Plus and the LDAP server, is intended to serve as a model for "connector" daemons written in other languages, for different authentication systems, or both. [NGINX, Inc. Professional Services](http://nginx.com/services/) is available to assist with such adaptations. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +For a step-by-step description of the authentication process in the reference implementation, see [NGINX Plus and NGINX Can Authenticate Application Users](https://nginx.com/blog/nginx-plus-authenticate-users#ldap-auth-flow). |
| 16 | + |
| 17 | +## Installation and Configuration |
| 18 | + |
| 19 | +The NGINX Plus configuration file that is provided with the reference implementation configures all components other than the LDAP server (that is, NGINX Plus, the client, the ldap-auth daemon, and the back-end daemon) to run on the same host, which is adequate for testing purposes. The LDAP server can also run on that host during testing. |
| 20 | + |
| 21 | +In an actual deployment, the back-end application and authentication server typically each run on a separate host, with NGINX Plus on a third host. The ldap-auth daemon does not consume many resources in most situations, so it can run on the NGINX Plus host or another host of your choice. |
| 22 | + |
| 23 | +To install and configure the reference implementation, perform the following steps. |
| 24 | + |
| 25 | +1. Create a clone of the GitHub repository (**nginx-ldap-auth**). |
| 26 | + |
| 27 | +1. If NGINX Plus is not already running, install it according to the [instructions for your operating system](https://cs.nginx.com/repo_setup). |
| 28 | + |
| 29 | +1. If an LDAP authentication server is not already running, install and configure one. By default the ldap-auth daemon communicates with OpenLDAP, but can be configured to work with Active Directory. |
| 30 | + |
| 31 | + If you are using the LDAP server only to test the reference implementation, you can use the [OpenLDAP server Docker image](https://github.com/osixia/docker-openldap) that is available on GitHub, or you can set up a server in a virtual environment using instructions such as [How To Install and Configure a Basic LDAP Server on an Ubuntu 12.04 VPS](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-a-basic-ldap-server-on-an-ubuntu-12-04-vps). |
| 32 | + |
| 33 | +1. On the host where the ldap-auth daemon is to run, install the following additional software. We recommend using the versions that are distributed with the operating system, instead of downloading the software from an open source repository. |
| 34 | + |
| 35 | + - Python version 2. Version 3 is not supported. |
| 36 | + - The Python LDAP module, **python-ldap** (created by the [python-ldap.org](http://www.python-ldap.org) open source project). |
| 37 | + |
| 38 | +1. Copy the following files from your repository clone to the indicated hosts: |
| 39 | + - **nginx-ldap-auth.conf** – NGINX Plus configuration file, which contains the minimal set of directives for testing the reference implementation. Install on the NGINX Plus host (in the **/etc/nginx/conf.d** directory if using the conventional configuration scheme). To avoid configuration conflicts, remember to move or rename any default configuration files installed with NGINX Plus. |
| 40 | + - **nginx-ldap-auth-daemon.py** – Python code for the ldap-auth daemon. Install on the host of your choice. |
| 41 | + - **nginx-ldap-auth-daemon-ctl.sh** – Sample shell script for starting and stopping the daemon. Install on the same host as the ldap-auth daemon. |
| 42 | + - **backend-sample-app.py** – Python code for the daemon that during testing stands in for a real back-end application server. Install on the host of your choice. |
| 43 | + |
| 44 | +1. Modify the NGINX Plus configuration file as described in [Required Modifications to the NGINX Plus Configuration File](#required-mods) below. For information about customizing your deployment, see [Customization](#customization) below. We recommend running the `nginx -t` command after making your changes to verify that the file is syntactically valid. |
| 45 | + |
| 46 | +1. Start NGINX Plus. If it is already running, run the following command to reload the configuration file: |
| 47 | + <pre>root# <strong>nginx -s reload</strong></pre> |
| 48 | + |
| 49 | +1. Run the following commands to start the ldap-auth daemon and the back-end daemon. |
| 50 | + <pre>root# <strong>nginx-ldap-auth-daemon-ctl.sh start</strong> |
| 51 | + root# <strong>python backend-sample-app.py</strong></pre> |
| 52 | + |
| 53 | +1. To test the reference implementation, use a web browser to access **http://*nginx-server-address*:8081**. Verify that the browser presents a login form. After you fill out the form and submit it, verify that the server returns the expected response to valid credentials. The sample back-end daemon returns this: |
| 54 | +<pre>Hello, world! Requested URL: <em>URL</em></pre> |
| 55 | + |
| 56 | +<a name="required-mods"> |
| 57 | +### Required Modifications to the NGINX Plus Configuration File |
| 58 | +</a> |
| 59 | + |
| 60 | +Modify the **nginx-ldap-auth.conf** file, by changing values as appropriate for your deployment for the terms shown in bold font in the following configuration. |
| 61 | + |
| 62 | +For detailed instructions, see [Configuring the Reference Implementation](https://nginx.com/blog/nginx-plus-authenticate-users#configure) in the [NGINX Plus and NGINX Can Authenticate Application Users](https://nginx.com/blog/nginx-plus-authenticate-users) blog post. The **nginx-ldap-auth.conf** file includes detailed instructions (in comments not shown here) for setting the `proxy-set-header` directives; for information about other directives, see the [NGINX reference documentation](http://nginx.org/en/docs/). |
| 63 | + |
| 64 | +<pre>http { |
| 65 | + ... |
| 66 | + proxy_cache_path <strong>cache/</strong> keys_zone=<strong>auth_cache</strong>:<strong>10m</strong>; |
| 67 | + |
| 68 | + upstream backend { |
| 69 | + server <strong>127.0.0.1</strong>:9000; |
| 70 | + } |
| 71 | + |
| 72 | + server { |
| 73 | + listen <strong>127.0.0.1</strong>:8081; |
| 74 | + |
| 75 | + location = /auth-proxy { |
| 76 | + proxy_pass http://<strong>127.0.0.1</strong>:8888; |
| 77 | + proxy_cache <strong>auth_cache</strong>; # Must match the name in the proxy_cache_path directive above |
| 78 | + proxy_cache_valid 200 403 <strong>10m</strong>; |
| 79 | + |
| 80 | + # URL and port for connecting to the LDAP server |
| 81 | + proxy_set_header X-Ldap-URL "<strong>ldaps</strong>://<strong>example.com</strong>:<strong>636</strong>"; |
| 82 | + |
| 83 | + # Base DN |
| 84 | + proxy_set_header X-Ldap-BaseDN "<strong>cn=Users,dc=test,dc=local</strong>"; |
| 85 | + |
| 86 | + # Bind DN |
| 87 | + proxy_set_header X-Ldap-BindDN "<strong>cn=root,dc=test,dc=local</strong>"; |
| 88 | + |
| 89 | + # Bind password |
| 90 | + proxy_set_header X-Ldap-BindPass "<strong>secret</strong>"; |
| 91 | + } |
| 92 | + } |
| 93 | +}</pre> |
| 94 | + |
| 95 | +If the authentication server runs Active Directory rather than OpenLDAP, uncomment the following directive as shown: |
| 96 | + |
| 97 | +``` |
| 98 | +proxy_set_header X-Ldap-Template "(SAMAccountName=%(username)s)"; |
| 99 | +``` |
| 100 | + |
| 101 | +The reference implementation uses cookie-based authentication. If you are using HTTP basic authentication instead, comment out the following directives as shown: |
| 102 | + |
| 103 | +<pre><strong>#</strong>proxy_set_header X-CookieName "nginxauth"; |
| 104 | +<strong>#</strong>proxy_set_header Cookie nginxauth=$cookie_nginxauth;</pre> |
| 105 | + |
| 106 | +## Customization |
| 107 | +### Caching |
| 108 | + |
| 109 | +The **nginx-ldap-auth.conf** file enables caching of both data and credentials. To disable caching, comment out the four `proxy_cache*` directives as shown: |
| 110 | + |
| 111 | +<pre>http { |
| 112 | + ... |
| 113 | + <strong>#</strong>proxy_cache_path cache/ keys_zone=auth_cache:10m; |
| 114 | + ... |
| 115 | + server { |
| 116 | + ... |
| 117 | + location = /auth-proxy { |
| 118 | + <strong>#</strong>proxy_cache auth_cache; |
| 119 | + # note that cookie is added to cache key |
| 120 | + <strong>#</strong>proxy_cache_key "$http_authorization$cookie_nginxauth"; |
| 121 | + <strong>#</strong>proxy_cache_valid 200 403 10m; |
| 122 | + } |
| 123 | + } |
| 124 | +}</pre> |
| 125 | + |
| 126 | +### Optional LDAP Parameters |
| 127 | + |
| 128 | +If you want to change the value for the `template` parameter that the ldap-auth daemon passes to the OpenLDAP server by default, uncomment the following directive as shown, and change the value: |
| 129 | + |
| 130 | +<pre>proxy_set_header X-Ldap-Template "<strong>(cn=%(username)s)</strong>";</pre> |
| 131 | + |
| 132 | +If you want to change the realm name from the default value (**Restricted**), uncomment and change the following directive: |
| 133 | + |
| 134 | +<pre>proxy_set_header X-Ldap-Realm "<strong>Restricted</strong>";</pre> |
| 135 | + |
| 136 | +### Authentication Server |
| 137 | + |
| 138 | +To modify the ldap-auth daemon to communicate with a different (non-LDAP) type of authentication server, write a new authentication-handler class to replace `LDAPAuthHandler` in the **ngx-ldap-auth-daemon.py** script. |
| 139 | + |
| 140 | +## Compatibility |
| 141 | + |
| 142 | +The auth daemon was tested against default configurations of the following LDAP servers: |
| 143 | + |
| 144 | +<ul> |
| 145 | +<li>[OpenLDAP](http://www.openldap.org/)</li> |
| 146 | +<li> Microsoft Windows Server Active Directory 2003</li> |
| 147 | +<li> Microsoft Windows Server Active Directory 2012</li> |
| 148 | +</ul> |
| 149 | + |
| 150 | +## Limitations |
| 151 | + |
| 152 | +The back-end daemon uses Base64 encoding on the username and password in the cookie. Base64 is a very weak form of scrambling, rendering the credentials vulnerable to extraction and misuse. We strongly recommend using a more sophisticated algorithm in your actual back-end application. |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +The reference implementation is subject to the same 2-clause BSD license as the open source NGINX software. |
0 commit comments