• Stars
    star
    681
  • Rank 66,346 (Top 2 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example of LDAP authentication using ngx_http_auth_request_module

PLEASE note that this project is not designed or hardened for production. It is intended as a model for such connector daemons

The nginx-ldap-auth project

This project provides a reference model implementation of a method for authenticating users on behalf of servers proxied by NGINX or NGINX Plus.

Note:

For ease of reading, this document refers to NGINX Plus, but it also applies to open source NGINX. The prerequisite ngx_http_auth_request_module module is included both in NGINX Plus packages and prebuilt open source NGINX binaries.

Description:

The nginx-ldap-auth software is a reference model 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).

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 is available to assist with such adaptations.

NGINX LDAP Architecture

For a step-by-step description of the authentication process in the reference implementation, see How Authentication Works in the Reference Implementation in NGINX Plus and NGINX Can Authenticate Application Users.

Installation and Configuration

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.

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.

To install and configure the reference implementation, perform the following steps.

  1. Create a clone of the GitHub repository (nginx-ldap-auth).

  2. If NGINX Plus is not already running, install it according to the instructions for your operating system.

  3. 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.

    If you are using the LDAP server only to test the reference implementation, you can use the OpenLDAP server Docker image 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.

  4. 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.

    • Python versions 2 and 3 are supported.
    • The Python LDAP module, python-ldap (created by the python-ldap.org open source project).
  5. Copy the following files from your repository clone to the indicated hosts:

    • 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.

    • nginx-ldap-auth-daemon.py โ€“ Python code for the ldap-auth daemon. Install on the host of your choice.

      Alternatively, use provided Dockerfile to build Docker image:

      docker build -t nginx-ldap-auth-daemon .
      docker run nginx-ldap-auth-daemon
      

      If you desire to use a container with Python3, you can supply an appropriate build argument:

      docker build -t nginx-ldap-auth-daemon --build-arg PYTHON_VERSION=3 .
      
    • 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.

    • 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.

  6. Modify the NGINX Plus configuration file as described in Required Modifications to the NGINX Plus Configuration File below. For information about customizing your deployment, see Customization below. We recommend running the nginx -t command after making your changes to verify that the file is syntactically valid.

  7. Start NGINX Plus. If it is already running, run the following command to reload the configuration file:

    root# nginx -s reload
  8. Run the following commands to start the ldap-auth daemon and the back-end daemon.

    root# nginx-ldap-auth-daemon-ctl.sh start
    root# python backend-sample-app.py
    
  9. 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:

Hello, world! Requested URL: URL

Required Modifications to the NGINX Plus Configuration File

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.

For detailed instructions, see Configuring the Reference Implementation in the NGINX Plus and NGINX Can Authenticate Application 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 {
  ...
  proxy_cache_path cache/ keys_zone=auth_cache:10m;

  upstream backend {
        server 127.0.0.1:9000;
  }

  server {
      listen 127.0.0.1:8081;

      location = /auth-proxy {
         proxy_pass http://127.0.0.1:8888;
         proxy_pass_request_body off;
         proxy_pass_request_headers off;
         proxy_set_header Content-Length "";
         proxy_cache auth_cache; # Must match the name in the proxy_cache_path directive above
         proxy_cache_valid 200 10m;

         # URL and port for connecting to the LDAP server
         proxy_set_header X-Ldap-URL "ldap://example.com";

         # Negotiate a TLS-enabled (STARTTLS) connection before sending credentials
         proxy_set_header X-Ldap-Starttls "true";

         # Base DN
         proxy_set_header X-Ldap-BaseDN "cn=Users,dc=test,dc=local";

         # Bind DN
         proxy_set_header X-Ldap-BindDN "cn=root,dc=test,dc=local";

         # Bind password
         proxy_set_header X-Ldap-BindPass "secret";
      }
   }
}

If the authentication server runs Active Directory rather than OpenLDAP, uncomment the following directive as shown:

proxy_set_header X-Ldap-Template "(sAMAccountName=%(username)s)";

In addition, the X-Ldap-Template header can be used to create complex LDAP searches. The code in ldap-auth-daemon creates a search filter that is based on this template header. By default, template is empty, and does not make any effect on LDAP search. However, you may decide for instance to authenticate only users from a specific user group (see LDAP documentation for more information regarding filters).

Suppose, your web resource should only be available for users from group1 group. In such a case you can define X-Ldap-Template template as follows:

proxy_set_header X-Ldap-Template "(&(cn=%(username)s)(memberOf=cn=group1,cn=Users,dc=example,dc=com))";

The search filters can be combined from less complex filters using boolean operations and can be rather complex.

The reference implementation uses cookie-based authentication. If you are using HTTP basic authentication instead, comment out the following directives, and enable the Authorization header as shown:

#proxy_set_header X-CookieName "nginxauth";
#proxy_set_header Cookie nginxauth=$cookie_nginxauth;
proxy_set_header Authorization $http_authorization;

Customization

Caching

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:

http {
  ...
  #proxy_cache_path cache/ keys_zone=auth_cache:10m;
  ...
  server {
    ...
    location = /auth-proxy {
      #proxy_cache auth_cache;
      # note that cookie is added to cache key
      #proxy_cache_key "$http_authorization$cookie_nginxauth";
      #proxy_cache_valid 200 10m;
     }
   }
}

Optional LDAP Parameters

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:

proxy_set_header X-Ldap-Template "(cn=%(username)s)";

If you want to change the realm name from the default value (Restricted), uncomment and change the following directive:

proxy_set_header X-Ldap-Realm "Restricted";

Authentication Server

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 nginx-ldap-auth-daemon.py script.

Compatibility

The auth daemon was tested against default configurations of the following LDAP servers:

  • OpenLDAP
  • Microsoft Windows Server Active Directory 2003
  • Microsoft Windows Server Active Directory 2012

Limitations

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.

License

The reference implementation is subject to the same 2-clause BSD license as the open source NGINX software.

More Repositories

1

kubernetes-ingress

NGINX and NGINX Plus Ingress Controllers for Kubernetes
Go
4,651
star
2

docker-nginx

Official NGINX Dockerfiles
Shell
3,236
star
3

nginx-prometheus-exporter

NGINX Prometheus Exporter for NGINX and NGINX Plus
Go
1,572
star
4

NGINX-Demos

NGINX and NGINX Plus demos
HTML
1,254
star
5

ngx-rust

Rust binding for NGINX
Rust
720
star
6

ansible-role-nginx

Ansible role for installing NGINX
Shell
633
star
7

kic-reference-architectures

MARA: Modern Application Reference Architecture
Python
633
star
8

crossplane

Quick and reliable way to convert NGINX configurations into JSON and back.
Python
620
star
9

nginmesh

Istio compatible service mesh using NGINX
Go
611
star
10

nginx-s3-gateway

NGINX S3 Caching Gateway
JavaScript
511
star
11

nginx-gateway-fabric

NGINX Gateway Fabric provides an implementation for the Gateway API using NGINX as the data plane.
Go
488
star
12

docker-nginx-unprivileged

Unprivileged NGINX Dockerfiles
Shell
376
star
13

nginx-wiki

ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com
HTML
291
star
14

docker-nginx-amplify

Official NGINX and Amplify Dockerfiles
Shell
230
star
15

nginx-amplify-doc

Public documentation for Amplify
Makefile
201
star
16

nginx-openid-connect

Reference implementation of OpenID Connect integration for NGINX Plus
JavaScript
188
star
17

nginx-otel

Perl
157
star
18

ansible-role-nginx-config

Ansible role for configuring NGINX
Jinja
154
star
19

mra-ingenious

A photo-sharing app built by NGINX and implemented using the Fabric Model from the Microservices Reference Architecture.
JavaScript
143
star
20

rtapi

Real time API latency analyzer - Create a PDF report and HDR histogram of your APIs
Go
133
star
21

nginx-service-mesh

A service mesh powered by NGINX Plus to manage container traffic in Kubernetes environments.
Go
93
star
22

ansible-collection-nginx

Ansible collection for NGINX
67
star
23

nginx-ingress-operator

WARNING - DEPRECATION NOTICE: The NGINX Ingress Operator has been updated to be a Helm based operator. This repo has been deprecated and will soon be archived - the new NGINX Ingress Operator repo can be found at https://github.com/nginxinc/nginx-ingress-helm-operator.
Go
66
star
24

nginx-loadbalancer-kubernetes

A Kubernetes Controller to synchronize NGINX+ Resources with Kubernetes Ingress Resources
Go
58
star
25

nginx-asg-sync

NGINX Plus Integration with Cloud Autoscaling
Go
56
star
26

nginx-go-crossplane

A library for working with NGINX configs in Go
Go
55
star
27

bank-of-sirius

Bank of Sirius
Java
53
star
28

helm-charts

NGINX Helm Charts repository
50
star
29

nginx-plus-go-client

A client for NGINX Plus API for Go
Go
49
star
30

nginx-openshift-router

NGINX and NGINX Plus OpenShift Routers
HTML
42
star
31

nginx-ingress-helm-operator

NGINX Ingress Operator for NGINX and NGINX Plus Ingress Controllers. Based on the Helm chart for NGINX Ingress Controller - https://github.com/nginxinc/helm-charts
Mustache
37
star
32

docker-nginx-controller

Docker support for NGINX Controller Agent in Containers
Dockerfile
29
star
33

nginx-ingress-workshops

Nginx Ingress Controller Hands on Workshops, with Lab Exercises and Guides
Shell
29
star
34

microservices-march

Examples from the Microservices March lectures and exercises.
27
star
35

ngx-istio-mixer

NGINX module for Istio mixer
Rust
24
star
36

nginx-saml

Perl
23
star
37

new-relic-agent

A new relic agent for NGINX Plus metrics
Python
23
star
38

ansible-role-nginx-app-protect

Ansible role to install and configure NGINX App Protect (WAF and DoS) for NGINX Plus on your target host
Jinja
21
star
39

router-mesh-architecture

NGINX Router Mesh Network Architecture for Microservices
CSS
19
star
40

aws-ha-elastic-ip

Active-Passive HA Deployment on AWS Using an Elastic IP Address
Shell
17
star
41

nginx-plus-dashboard

HTML
16
star
42

website-resources-conf

content for nginx.com/resources/conf/ -- configuration files shared in blog posts, etc.
16
star
43

fabric-model-architecture

Repository for the NGINX Fabric Model Architecture
CSS
15
star
44

nginx-management-suite-iac

NMS IAC repo
HCL
14
star
45

ngxinfo

Python
13
star
46

Community-Code-of-Conduct

NGINX Open Source Community's Code of Conduct
11
star
47

nginx-amplify-agent

NGINX Amplify Agent
Python
11
star
48

mra-user-manager

User manager
HTML
10
star
49

snarejs

Snare.js
JavaScript
10
star
50

nginx-ns1-gslb

ARCHIVED - NGINX Plus Integration with NS1 GSLB
Go
10
star
51

mra-auth-proxy

Auth proxy for MRA
Jinja
9
star
52

nginx-wrapper

NGINX Event Process Wrapper
Go
9
star
53

template-repository

A template repository for new NGINX projects
9
star
54

ngx-stream-nginmesh-dest

Nginx module to get dest ip and port
C
8
star
55

ansible-role-nginx-unit

Ansible role for NGINX Unit
Jinja
8
star
56

ebook-managing-kubernetes-nginx

Shell
8
star
57

nginx-basics-workshops

HTML
8
star
58

nginx-unsupported-modules

Container builds of unsupported NGINX modules
Shell
7
star
59

nginx-hugo-theme

A hugo theme for NGINX documentation
CSS
7
star
60

ansible-role-nginx-management-suite

Ansible role for the NGINX Management Suite
Jinja
7
star
61

mra-content-service

Go
6
star
62

nginx-for-azure-deploy-action

Github Actions to sync NGINX configs into the NGINX for Azure service.
TypeScript
6
star
63

mra-photouploader

HTML
5
star
64

ansible_collection_nginx_controller

Collection of NGINX Controller Roles for Ansible
5
star
65

mra-photoresizer

HTML
5
star
66

nginx-controller-lab

Shell
4
star
67

mra-pages

JavaScript
4
star
68

nginxaas-for-azure-snippets

Example ARM templates for common NGINX for Azure use cases
Python
4
star
69

.github

4
star
70

nginx-plus-install-tools

NGINX Plus Install tools
Shell
4
star
71

mra-album-manager

Ruby
4
star
72

nginx-azure-workshops

Instructor Lead and Hands-on Lab Exercises and Lab Guides for NGINX as a Service for Microsoft Azure
Shell
4
star
73

homebrew-tap

Ruby
3
star
74

ansible-role-nginx_controller_application

Jinja
3
star
75

ansible_role_nginx_controller_agent

Ansible role for installing the NGINX Controller agent
Jinja
3
star
76

nap-dos-arbitrator-helm-chart

Smarty
3
star
77

nginx-aws-signature

NGINX AWS Signature Library to authenticate AWS services such as S3 and Lambda via NGINX and NGINX Plus.
JavaScript
3
star
78

alpine-fips

Alpine Linux with FIPS OpenSSL module
Dockerfile
3
star
79

ansible-role-nginx_controller_publish_api

Jinja
2
star
80

kic-test-containers

Docker containers used by the KIC team
Go
2
star
81

ansible-role-nginx_controller_user

Jinja
2
star
82

ansible-role-nginx_controller_environment

Managing environments within NGINX Controller
Jinja
2
star
83

ansible-role-nginx_controller_api_definition_import

Jinja
2
star
84

ansible-role-nginx-controller-gateway

Jinja
2
star
85

ansible_role_nginx_controller_install

Ansible role for installing NGINX Controller
Jinja
2
star
86

ansible-role-nginx-controller-certificate

Jinja
2
star
87

ansible-role-nginx-controller-component

Jinja
2
star
88

ansible_role_nginx_controller_generate_token

Jinja
2
star
89

ansible-role-nginx_controller_location

Jinja
2
star
90

ansible-role-nginx-controller-license

Jinja
2
star
91

ansible-role-nginx_controller_integration

Jinja
2
star
92

aws-marketplace-publish

Publish Docker images to AWS Marketplace
TypeScript
2
star
93

ansible-role-nginx_controller_user_role

Jinja
1
star
94

ansible-role-nginx_controller_forwarder

Jinja
1
star