ldap.md 15.5 KB
Newer Older
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
1 2 3 4
[//]: # (Do *NOT* modify this file in EE documentation. All changes in this)
[//]: # (file should happen in CE, too. If the change is EE-specific, put)
[//]: # (it in `ldap-ee.md`.)

Drew Blessing's avatar
Drew Blessing committed
5 6 7 8 9
# LDAP

GitLab integrates with LDAP to support user authentication.
This integration works with most LDAP-compliant directory
servers, including Microsoft Active Directory, Apple Open Directory, Open LDAP,
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
10 11 12 13 14 15 16 17
and 389 Server. GitLab Enterprise Editions include enhanced integration,
including group membership syncing as well as multiple LDAP servers support.

## GitLab EE

The information on this page is relevant for both GitLab CE and EE. For more
details about EE-specific LDAP features, see the
[LDAP Enterprise Edition documentation](https://docs.gitlab.com/ee/administration/auth/ldap-ee.html).
Drew Blessing's avatar
Drew Blessing committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

## Security

GitLab assumes that LDAP users are not able to change their LDAP 'mail', 'email'
or 'userPrincipalName' attribute. An LDAP user who is allowed to change their
email on the LDAP server can potentially
[take over any account](#enabling-ldap-sign-in-for-existing-gitlab-users)
on your GitLab server.

We recommend against using LDAP integration if your LDAP users are
allowed to change their 'mail', 'email' or 'userPrincipalName'  attribute on
the LDAP server.

### User deletion

If a user is deleted from the LDAP server, they will be blocked in GitLab, as
well. Users will be immediately blocked from logging in. However, there is an
LDAP check cache time (sync time) of one hour (see note). This means users that
are already logged in or are using Git over SSH will still be able to access
GitLab for up to one hour. Manually block the user in the GitLab Admin area to
immediately block all access.

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
40 41 42 43
NOTE: **Note**:
GitLab Enterprise Edition Starter supports a
[configurable sync time](https://docs.gitlab.com/ee/administration/auth/ldap-ee.html#adjusting-ldap-user-and-group-sync-schedules),
with a default of one hour.
Drew Blessing's avatar
Drew Blessing committed
44

45 46 47 48 49 50
## Git password authentication

LDAP-enabled users can always authenticate with Git using their GitLab username
or email and LDAP password, even if password authentication for Git is disabled
in the application settings.

Drew Blessing's avatar
Drew Blessing committed
51 52
## Configuration

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
53 54 55 56
NOTE: **Note**:
In GitLab Enterprise Edition Starter, you can configure multiple LDAP servers
to connect to one GitLab server.

57 58 59
For a complete guide on configuring LDAP with GitLab Community Edition, please check
the admin guide [How to configure LDAP with GitLab CE](how_to_configure_ldap_gitlab_ce/index.md).

Drew Blessing's avatar
Drew Blessing committed
60
To enable LDAP integration you need to add your LDAP server settings in
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
61 62
`/etc/gitlab/gitlab.rb` or `/home/git/gitlab/config/gitlab.yml` for Omnibus
GitLab and installations from source respectively.
Drew Blessing's avatar
Drew Blessing committed
63

64 65 66 67
There is a Rake task to check LDAP configuration. After configuring LDAP
using the documentation below, see [LDAP check Rake task](../raketasks/check.md#ldap-check)
for information on the LDAP check Rake task.

Drew Blessing's avatar
Drew Blessing committed
68 69 70 71 72 73 74 75 76 77
Prior to version 7.4, GitLab used a different syntax for configuring
LDAP integration. The old LDAP integration syntax still works but may be
removed in a future version. If your `gitlab.rb` or `gitlab.yml` file contains
LDAP settings in both the old syntax and the new syntax, only the __old__
syntax will be used by GitLab.

The configuration inside `gitlab_rails['ldap_servers']` below is sensitive to
incorrect indentation. Be sure to retain the indentation given in the example.
Copy/paste can sometimes cause problems.

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
78 79 80 81 82 83
NOTE: **Note:**
The `encryption` value `ssl` corresponds to 'Simple TLS' in the LDAP
library. `tls` corresponds to StartTLS, not to be confused with regular TLS.
Normally, if you specify `ssl` it will be on port 636, while `tls` (StartTLS)
would be on port 389. `plain` also operates on port 389.

Drew Blessing's avatar
Drew Blessing committed
84 85 86 87 88
**Omnibus configuration**

```ruby
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-EOS # remember to close this block with 'EOS' below
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
89 90 91 92 93 94 95 96 97 98
##
## 'main' is the GitLab 'provider ID' of this LDAP server
##
main:
  ##
  ## A human-friendly name for your LDAP server. It is OK to change the label later,
  ## for instance if you find out it is too large to fit on the web page.
  ##
  ## Example: 'Paris' or 'Acme, Ltd.'
  ##
Drew Blessing's avatar
Drew Blessing committed
99
  label: 'LDAP'
100

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
101 102 103
  ##
  ## Example: 'ldap.mydomain.com'
  ##
Drew Blessing's avatar
Drew Blessing committed
104
  host: '_your_ldap_server'
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
105 106 107 108 109

  ##
  ## This port is an example, it is sometimes different but it is always an
  ## integer and not a string.
  ##
110
  port: 389 # usually 636 for SSL
111 112
  uid: 'sAMAccountName' # This should be the attribute, not the value that maps to uid.

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
113 114 115
  ##
  ## Examples: 'america\\momo' or 'CN=Gitlab Git,CN=Users,DC=mydomain,DC=com'
  ##
Drew Blessing's avatar
Drew Blessing committed
116 117 118
  bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'
  password: '_the_password_of_the_bind_user'

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
119 120 121 122 123 124 125 126 127 128
  ##
  ## Encryption method. The "method" key is deprecated in favor of
  ## "encryption".
  ##
  ##   Examples: "start_tls" or "simple_tls" or "plain"
  ##
  ##   Deprecated values: "tls" was replaced with "start_tls" and "ssl" was
  ##   replaced with "simple_tls".
  ##
  ##
129 130
  encryption: 'plain'

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
131 132 133 134 135 136
  ##
  ## Enables SSL certificate verification if encryption method is
  ## "start_tls" or "simple_tls". Defaults to true since GitLab 10.0 for
  ## security. This may break installations upon upgrade to 10.0, that did
  ## not know their LDAP SSL certificates were not setup properly.
  ##
137
  verify_certificates: true
138

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
139 140 141 142 143 144 145
  ##
  ## Specifies the SSL version for OpenSSL to use, if the OpenSSL default
  ## is not appropriate.
  ##
  ##   Example: 'TLSv1_1'
  ##
  ##
146 147
  ssl_version: ''

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
148 149 150 151 152
  ##
  ## Set a timeout, in seconds, for LDAP queries. This helps avoid blocking
  ## a request if the LDAP server becomes unresponsive.
  ## A value of 0 means there is no timeout.
  ##
Drew Blessing's avatar
Drew Blessing committed
153 154
  timeout: 10

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
155 156 157 158 159
  ##
  ## This setting specifies if LDAP server is Active Directory LDAP server.
  ## For non AD servers it skips the AD specific queries.
  ## If your LDAP server is not AD, set this to false.
  ##
Drew Blessing's avatar
Drew Blessing committed
160 161
  active_directory: true

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
162 163 164 165 166 167 168 169 170 171 172
  ##
  ## If allow_username_or_email_login is enabled, GitLab will ignore everything
  ## after the first '@' in the LDAP username submitted by the user on login.
  ##
  ## Example:
  ## - the user enters 'jane.doe@example.com' and 'p@ssw0rd' as LDAP credentials;
  ## - GitLab queries the LDAP server with 'jane.doe' and 'p@ssw0rd'.
  ##
  ## If you are using "uid: 'userPrincipalName'" on ActiveDirectory you need to
  ## disable this setting, because the userPrincipalName contains an '@'.
  ##
Drew Blessing's avatar
Drew Blessing committed
173 174
  allow_username_or_email_login: false

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
175 176 177 178 179
  ##
  ## To maintain tight control over the number of active users on your GitLab installation,
  ## enable this setting to keep new users blocked until they have been cleared by the admin
  ## (default: false).
  ##
Drew Blessing's avatar
Drew Blessing committed
180 181
  block_auto_created_users: false

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
182 183 184 185 186 187
  ##
  ## Base where we can search for users
  ##
  ##   Ex. 'ou=People,dc=gitlab,dc=example' or 'DC=mydomain,DC=com'
  ##
  ##
Drew Blessing's avatar
Drew Blessing committed
188 189
  base: ''

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
190 191 192 193 194 195 196 197 198 199 200
  ##
  ## Filter LDAP users
  ##
  ##   Format: RFC 4515 https://tools.ietf.org/search/rfc4515
  ##   Ex. (employeeType=developer)
  ##
  ##   Note: GitLab does not support omniauth-ldap's custom filter syntax.
  ##
  ##   Example for getting only specific users:
  ##   '(&(objectclass=user)(|(samaccountname=momo)(samaccountname=toto)))'
  ##
Drew Blessing's avatar
Drew Blessing committed
201 202
  user_filter: ''

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
203 204 205 206 207 208
  ##
  ## LDAP attributes that GitLab will use to create an account for the LDAP user.
  ## The specified attribute can either be the attribute name as a string (e.g. 'mail'),
  ## or an array of attribute names to try in order (e.g. ['mail', 'email']).
  ## Note that the user's LDAP login will always be the attribute specified as `uid` above.
  ##
Drew Blessing's avatar
Drew Blessing committed
209
  attributes:
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
210 211 212 213 214 215 216
    ##
    ## The username will be used in paths for the user's own projects
    ## (like `gitlab.example.com/username/project`) and when mentioning
    ## them in issues, merge request and comments (like `@username`).
    ## If the attribute specified for `username` contains an email address,
    ## the GitLab username will be the part of the email address before the '@'.
    ##
Drew Blessing's avatar
Drew Blessing committed
217 218 219
    username: ['uid', 'userid', 'sAMAccountName']
    email:    ['mail', 'email', 'userPrincipalName']

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
220 221 222 223 224
    ##
    ## If no full name could be found at the attribute specified for `name`,
    ## the full name is determined using the attributes specified for
    ## `first_name` and `last_name`.
    ##
Drew Blessing's avatar
Drew Blessing committed
225 226 227 228
    name:       'cn'
    first_name: 'givenName'
    last_name:  'sn'

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
229 230 231
  ##
  ## If lowercase_usernames is enabled, GitLab will lower case the username.
  ##
232 233
  lowercase_usernames: false

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
234
  ##
235
  ## EE only
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
236
  ##
237

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
238 239 240 241
  ## Base where we can search for groups
  ##
  ##   Ex. ou=groups,dc=gitlab,dc=example
  ##
242 243
  group_base: ''

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
244 245 246 247 248 249
  ## The CN of a group containing GitLab administrators
  ##
  ##   Ex. administrators
  ##
  ##   Note: Not `cn=administrators` or the full DN
  ##
250 251
  admin_group: ''

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
252 253 254 255 256 257 258 259 260 261 262 263 264
  ## An array of CNs of groups containing users that should be considered external
  ##
  ##   Ex. ['interns', 'contractors']
  ##
  ##   Note: Not `cn=interns` or the full DN
  ##
  external_groups: []

  ##
  ## The LDAP attribute containing a user's public SSH key
  ##
  ##   Example: sshPublicKey
  ##
265
  sync_ssh_keys: false
Drew Blessing's avatar
Drew Blessing committed
266

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
267 268 269 270 271 272 273
## GitLab EE only: add more LDAP servers
## Choose an ID made of a-z and 0-9 . This ID will be stored in the database
## so that GitLab can remember which LDAP server a user belongs to.
#uswest2:
#  label:
#  host:
#  ....
Drew Blessing's avatar
Drew Blessing committed
274 275 276 277 278 279 280 281
EOS
```

**Source configuration**

Use the same format as `gitlab_rails['ldap_servers']` for the contents under
`servers:` in the example below:

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
282
```yaml
Drew Blessing's avatar
Drew Blessing committed
283 284 285 286 287
production:
  # snip...
  ldap:
    enabled: false
    servers:
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
288 289 290 291 292 293 294 295 296
      ##
      ## 'main' is the GitLab 'provider ID' of this LDAP server
      ##
      main:
        ##
        ## A human-friendly name for your LDAP server. It is OK to change the label later,
        ## for instance if you find out it is too large to fit on the web page.
        ##
        ## Example: 'Paris' or 'Acme, Ltd.'
Drew Blessing's avatar
Drew Blessing committed
297
        label: 'LDAP'
Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
298
        ## snip...
Drew Blessing's avatar
Drew Blessing committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
```

## Using an LDAP filter to limit access to your GitLab server

If you want to limit all GitLab access to a subset of the LDAP users on your
LDAP server, the first step should be to narrow the configured `base`. However,
it is sometimes necessary to filter users further. In this case, you can set up
an LDAP user filter. The filter must comply with
[RFC 4515](https://tools.ietf.org/search/rfc4515).

**Omnibus configuration**

```ruby
gitlab_rails['ldap_servers'] = YAML.load <<-EOS
main:
  # snip...
  user_filter: '(employeeType=developer)'
EOS
```

**Source configuration**

```yaml
production:
  ldap:
    servers:
      main:
        # snip...
        user_filter: '(employeeType=developer)'
```

Tip: If you want to limit access to the nested members of an Active Directory
James Lopez's avatar
James Lopez committed
331
group, you can use the following syntax:
Drew Blessing's avatar
Drew Blessing committed
332 333

```
334
(memberOf:1.2.840.113556.1.4.1941:=CN=My Group,DC=Example,DC=com)
Drew Blessing's avatar
Drew Blessing committed
335 336
```

337
Find more information about this "LDAP_MATCHING_RULE_IN_CHAIN" filter at
338
https://msdn.microsoft.com/en-us/library/aa746475(v=vs.85).aspx. Support for
339
nested members in the user filter should not be confused with
340 341
[group sync nested groups support (EE only)](https://docs.gitlab.com/ee/administration/auth/ldap-ee.html#supported-ldap-group-types-attributes).

Drew Blessing's avatar
Drew Blessing committed
342 343 344
Please note that GitLab does not support the custom filter syntax used by
omniauth-ldap.

Achilleas Pipinellis's avatar
Achilleas Pipinellis committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
### Escaping special characters

If the `user_filter` DN contains special characters. For example, a comma:

```
OU=GitLab, Inc,DC=gitlab,DC=com
```

This character needs to be escaped as documented in [RFC 4515](https://tools.ietf.org/search/rfc4515).

Due to the way the string is parsed, the special character needs to be converted
to hex and `\\5C\\` (`5C` = `\` in hex) added before it.
As an example the above DN would look like

```
OU=GitLab\\5C\\2C Inc,DC=gitlab,DC=com
```

Drew Blessing's avatar
Drew Blessing committed
363 364 365 366 367 368 369 370 371 372 373
## Enabling LDAP sign-in for existing GitLab users

When a user signs in to GitLab with LDAP for the first time, and their LDAP
email address is the primary email address of an existing GitLab user, then
the LDAP DN will be associated with the existing user. If the LDAP email
attribute is not found in GitLab's database, a new user is created.

In other words, if an existing GitLab user wants to enable LDAP sign-in for
themselves, they should check that their GitLab email address matches their
LDAP email address, and then sign into GitLab via their LDAP credentials.

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
## Enabling LDAP username lowercase

Some LDAP servers, depending on their configurations, can return uppercase usernames. This can lead to several confusing issues like, for example, creating links or namespaces with uppercase names.

GitLab can automatically lowercase usernames provided by the LDAP server by enabling
the configuration option `lowercase_usernames`. By default, this configuration option is `false`.

**Omnibus configuration**

1. Edit `/etc/gitlab/gitlab.rb`:

  ```ruby
  gitlab_rails['ldap_servers'] = YAML.load <<-EOS
  main:
    # snip...
    lowercase_usernames: true
  EOS
  ```

2. [Reconfigure GitLab](../restart_gitlab.md#omnibus-gitlab-reconfigure) for the changes to take effect.

**Source configuration**

1. Edit `config/gitlab.yaml`:

  ```yaml
  production:
    ldap:
      servers:
        main:
          # snip...
          lowercase_usernames: true
  ```
2. [Restart GitLab](../restart_gitlab.md#installations-from-source) for the changes to take effect.

409 410 411 412 413 414
## Encryption

### TLS Server Authentication

There are two encryption methods, `simple_tls` and `start_tls`.

Michael Kozono's avatar
Michael Kozono committed
415
For either encryption method, if setting `verify_certificates: false`, TLS
416 417 418
encryption is established with the LDAP server before any LDAP-protocol data is
exchanged but no validation of the LDAP server's SSL certificate is performed.

Michael Kozono's avatar
Michael Kozono committed
419
>**Note**: Before GitLab 9.5, `verify_certificates: false` is the default if
420 421
unspecified.

Drew Blessing's avatar
Drew Blessing committed
422 423 424 425 426 427 428 429 430 431 432
## Limitations

### TLS Client Authentication

Not implemented by `Net::LDAP`.
You should disable anonymous LDAP authentication and enable simple or SASL
authentication. The TLS client authentication setting in your LDAP server cannot
be mandatory and clients cannot be authenticated with the TLS protocol.

## Troubleshooting

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
### Debug LDAP user filter with ldapsearch

This example uses ldapsearch and assumes you are using ActiveDirectory. The
following query returns the login names of the users that will be allowed to
log in to GitLab if you configure your own user_filter.

```
ldapsearch -H ldaps://$host:$port -D "$bind_dn" -y bind_dn_password.txt  -b "$base" "$user_filter" sAMAccountName
```

- Variables beginning with a `$` refer to a variable from the LDAP section of
  your configuration file.
- Replace ldaps:// with ldap:// if you are using the plain authentication method.
  Port `389` is the default `ldap://` port and `636` is the default `ldaps://`
  port.
- We are assuming the password for the bind_dn user is in bind_dn_password.txt.


Drew Blessing's avatar
Drew Blessing committed
451 452 453 454 455 456 457 458
### Invalid credentials when logging in

- Make sure the user you are binding with has enough permissions to read the user's
tree and traverse it.
- Check that the `user_filter` is not blocking otherwise valid users.
- Run the following check command to make sure that the LDAP settings are
  correct and GitLab can see your users:

459 460 461
    ```bash
    # For Omnibus installations
    sudo gitlab-rake gitlab:ldap:check
Drew Blessing's avatar
Drew Blessing committed
462

463 464 465
    # For installations from source
    sudo -u git -H bundle exec rake gitlab:ldap:check RAILS_ENV=production
    ```
Drew Blessing's avatar
Drew Blessing committed
466 467 468 469

### Connection Refused

If you are getting 'Connection Refused' errors when trying to connect to the
470 471 472
LDAP server please double-check the LDAP `port` and `encryption` settings used by
GitLab. Common combinations are `encryption: 'plain'` and `port: 389`, OR
`encryption: 'simple_tls'` and `port: 636`.
473

474
### Troubleshooting
475

476 477 478 479 480 481
If a user account is blocked or unblocked due to the LDAP configuration, a
message will be logged to `application.log`.

If there is an unexpected error during an LDAP lookup (configuration error,
timeout), the login is rejected and a message will be logged to
`production.log`.