|
| 1 | +--- |
| 2 | +title: "LDAP Authentication" |
| 3 | +description: "Authenticate users via LDAP by building custom Docker images" |
| 4 | +weight: 20 |
| 5 | +audience: opensource |
| 6 | +aliases: |
| 7 | + - /en/open_source/ldap-authentication |
| 8 | +--- |
| 9 | + |
| 10 | +**This feature is experimental, and is not implemented in DefectDojo Pro**. |
| 11 | + |
| 12 | +DefectDojo does not support LDAP authentication out of the box. However, since DefectDojo is built on Django, LDAP can be added by building your own Docker images and modifying a small number of configuration files. |
| 13 | + |
| 14 | +## Files to Modify |
| 15 | + |
| 16 | +- `Dockerfile.django-*` |
| 17 | +- `Dockerfile.nginx-*` |
| 18 | +- `requirements.txt` |
| 19 | +- `local_settings.py` |
| 20 | +- `docker-compose.yml` *(optional — for passing secrets via environment variables)* |
| 21 | + |
| 22 | +## Dockerfile Modifications |
| 23 | + |
| 24 | +In both `Dockerfile.django-alpine` and `Dockerfile.nginx-alpine`, add the following to the `apk add` layer: |
| 25 | + |
| 26 | +```bash |
| 27 | +openldap-dev \ |
| 28 | +cyrus-sasl-dev \ |
| 29 | +``` |
| 30 | + |
| 31 | +In `Dockerfile.django-debian`, add the following to the `apt-get install` layer: |
| 32 | + |
| 33 | +```bash |
| 34 | +libldap2-dev \ |
| 35 | +libsasl2-dev \ |
| 36 | +ldap-utils \ |
| 37 | +``` |
| 38 | + |
| 39 | +## requirements.txt |
| 40 | + |
| 41 | +Check [pypi.org](https://pypi.org) for the latest versions at the time of implementation, then add: |
| 42 | + |
| 43 | +``` |
| 44 | +python-ldap==3.4.5 |
| 45 | +django-auth-ldap==5.2.0 |
| 46 | +``` |
| 47 | + |
| 48 | +- [python-ldap](https://pypi.org/project/python-ldap/) |
| 49 | +- [django-auth-ldap](https://pypi.org/project/django-auth-ldap/) |
| 50 | + |
| 51 | +## local_settings.py |
| 52 | + |
| 53 | +Find the settings file (see `/dojo/settings/settings.py` for instructions on using `local_settings.py`) and make the following additions. |
| 54 | + |
| 55 | +At the top of the file: |
| 56 | + |
| 57 | +```python |
| 58 | +import ldap |
| 59 | +from django_auth_ldap.config import LDAPSearch, GroupOfNamesType |
| 60 | +import environ |
| 61 | +``` |
| 62 | + |
| 63 | +Add LDAP variables to the `env` dict: |
| 64 | + |
| 65 | +```python |
| 66 | +# LDAP |
| 67 | +env = environ.FileAwareEnv( |
| 68 | + DD_LDAP_SERVER_URI=(str, 'ldap://ldap.example.com'), |
| 69 | + DD_LDAP_BIND_DN=(str, ''), |
| 70 | + DD_LDAP_BIND_PASSWORD=(str, ''), |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +Then add the LDAP settings beneath the `env` dict: |
| 75 | + |
| 76 | +```python |
| 77 | +AUTH_LDAP_SERVER_URI = env('DD_LDAP_SERVER_URI') |
| 78 | +AUTH_LDAP_BIND_DN = env('DD_LDAP_BIND_DN') |
| 79 | +AUTH_LDAP_BIND_PASSWORD = env('DD_LDAP_BIND_PASSWORD') |
| 80 | + |
| 81 | +AUTH_LDAP_USER_SEARCH = LDAPSearch( |
| 82 | + "ou=Groups,dc=example,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)" |
| 83 | +) |
| 84 | + |
| 85 | +AUTH_LDAP_USER_ATTR_MAP = { |
| 86 | + "first_name": "givenName", |
| 87 | + "last_name": "sn", |
| 88 | + "email": "mail", |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Customise all search variables to match your organisation's LDAP configuration. |
| 93 | + |
| 94 | +### Optional: Group Controls |
| 95 | + |
| 96 | +```python |
| 97 | +AUTH_LDAP_GROUP_SEARCH = LDAPSearch( |
| 98 | + "dc=example,dc=com", |
| 99 | + ldap.SCOPE_SUBTREE, |
| 100 | + "(objectClass=groupOfNames)", |
| 101 | +) |
| 102 | +AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn") |
| 103 | + |
| 104 | +AUTH_LDAP_REQUIRE_GROUP = "cn=DD_USER_ACTIVE,ou=Groups,dc=example,dc=com" |
| 105 | + |
| 106 | +AUTH_LDAP_USER_FLAGS_BY_GROUP = { |
| 107 | + "is_active": "cn=DD_USER_ACTIVE,ou=Groups,dc=example,dc=com", |
| 108 | + "is_staff": "cn=DD_USER_STAFF,ou=Groups,dc=example,dc=com", |
| 109 | + "is_superuser": "cn=DD_USER_ADMIN,ou=Groups,dc=example,dc=com", |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Finally, add `django_auth_ldap.backend.LDAPBackend` to `AUTHENTICATION_BACKENDS`: |
| 114 | + |
| 115 | +```python |
| 116 | +AUTHENTICATION_BACKENDS = ( |
| 117 | + 'django_auth_ldap.backend.LDAPBackend', |
| 118 | + 'django.contrib.auth.backends.RemoteUserBackend', |
| 119 | + 'django.contrib.auth.backends.ModelBackend', |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +Full documentation: [Django Authentication with LDAP](https://django-auth-ldap.readthedocs.io/en/latest/) |
| 124 | + |
| 125 | +## docker-compose.yml |
| 126 | + |
| 127 | +To pass LDAP credentials to the container via environment variables, add these to the `uwsgi` service environment section: |
| 128 | + |
| 129 | +```yaml |
| 130 | +DD_LDAP_SERVER_URI: "${DD_LDAP_SERVER_URI:-ldap://ldap.example.com}" |
| 131 | +DD_LDAP_BIND_DN: "${DD_LDAP_BIND_DN:-}" |
| 132 | +DD_LDAP_BIND_PASSWORD: "${DD_LDAP_BIND_PASSWORD:-}" |
| 133 | +``` |
| 134 | +
|
| 135 | +Alternatively, set these values directly in `local_settings.py`. |
0 commit comments