{"id":10691,"date":"2021-10-16T14:41:53","date_gmt":"2021-10-16T11:41:53","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10691"},"modified":"2024-03-18T13:33:33","modified_gmt":"2024-03-18T10:33:33","slug":"integrate-gitlab-with-openldap-for-authentication","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/integrate-gitlab-with-openldap-for-authentication\/","title":{"rendered":"Integrate Gitlab with OpenLDAP for Authentication"},"content":{"rendered":"\n<p>This tutorial will take you through how to integrate Gitlab with OpenLDAP for authentication. <a href=\"https:\/\/docs.gitlab.com\/ee\/administration\/auth\/ldap\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gitlab supports LDAP authentication<\/a>.<\/p>\n\n\n\n<p>In our previous tutorials, we learnt how to install Gitlab on Debian\/Ubuntu;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-gitlab-ce-on-debian-11\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Gitlab CE on Debian 11<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-gitlab-with-ssl-tls-certificate-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Gitlab with SSL\/TLS Certificate on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p>We also have tutorials on setting up OpenLDAP;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-setup-openldap-server-on-debian-11\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and Setup OpenLDAP Server on Debian 11<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-setup-openldap-on-rocky-linux-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and Setup OpenLDAP on Rocky Linux 8<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Integrating Gitlab with OpenLDAP for Authentication<\/h2>\n\n\n\n<p>Assuming you already have a Gitlab and OpenLDAP servers setup and running, let us proceed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Gitlab Users OpenLDAP Member Group<\/h3>\n\n\n\n<p>In order to ensure only specific users can login to the Gitlab server via OpenLDAP, we will create a member group on our OpenLDAP server.<\/p>\n\n\n\n<p>How to create OpenLDAP member groups has been extensively described in the guide below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-create-openldap-member-groups\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create OpenLDAP Member Groups<\/a><\/p>\n\n\n\n<p>Let us assume we have the following users we want to allow them login to Gitlab server via OpenLDAP;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ldapsearch -H ldapi:\/\/\/ -Y EXTERNAL -LLL -b \"dc=ldapmaster,dc=kifarunix-demo,dc=com\" uid=* dn -Q<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>dn: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\n\ndn: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com<\/code><\/pre>\n\n\n\n<p>Hence, create a group with any name of your preference. For example, let us create a group called <strong><code>gitlab<\/code><\/strong>.<\/p>\n\n\n\n<p>In our current LDAP server, we have a Group OU,&nbsp;<code>ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com<\/code><\/p>\n\n\n\n<p>To create a <strong>gitlab<\/strong> group and add the users, janedoe and johndoe, create an LDIF config file as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; gitlab-group.ldif &lt;&lt; EOL\ndn: cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com\nobjectClass: groupOfNames\ncn: gitlab\nmember: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\nmember: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\nEOL<\/code><\/pre>\n\n\n\n<p>Update the above as per your organization&#8217;s OpenLDAP server setup.<\/p>\n\n\n\n<p>Next, run the command below on the OpenLDAP server terminal to create the group alongside the members of that group.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ldapadd -Y EXTERNAL -H ldapi:\/\/\/ -Q -f gitlab-group.ldif<\/code><\/pre>\n\n\n\n<p>Once the command has run, you can verify the group is available and members;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ldapsearch -H ldapi:\/\/\/ -Y EXTERNAL -LLL -b \"dc=ldapmaster,dc=kifarunix-demo,dc=com\" cn=gitlab -Q<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>dn: cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com\nobjectClass: groupOfNames\ncn: gitlab\nmember: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\nmember: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com<\/code><\/pre>\n\n\n\n<p>You can <a href=\"https:\/\/kifarunix.com\/how-to-create-openldap-member-groups\/#add-users-to-ldap-group\" target=\"_blank\" rel=\"noreferrer noopener\">add more members to the group as described on the post above<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Integrating Gitlab with OpenLDAP<\/h3>\n\n\n\n<p>Once you have setup OpenLDAP group and added members, you can now configure Gitlab to use OpenLDAP for authentication.<\/p>\n\n\n\n<p>Open Gitlab configuration file for editing;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/gitlab\/gitlab.rb<\/code><\/pre>\n\n\n\n<p>Scroll down to LDAP settings configuration section;<\/p>\n\n\n\n<p>By default, LDAP configuration settings are commented out;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n### LDAP Settings\n###! Docs: https:\/\/docs.gitlab.com\/omnibus\/settings\/ldap.html\n###! **Be careful not to break the indentation in the ldap_servers block. It is\n###!   in yaml format and the spaces must be retained. Using tabs will not work.**\n# gitlab_rails['ldap_enabled'] = false\n# gitlab_rails['prevent_ldap_sign_in'] = false\n\n###! **remember to close this block with 'EOS' below**\n# gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'\n#   main: # 'main' is the GitLab 'provider ID' of this LDAP server\n#     label: 'LDAP'\n#     host: '_your_ldap_server'\n#     port: 389\n#     uid: 'sAMAccountName'\n#     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'\n#     password: '_the_password_of_the_bind_user'\n#     encryption: 'plain' # \"start_tls\" or \"simple_tls\" or \"plain\"\n#     verify_certificates: true\n#     smartcard_auth: false\n#     active_directory: true\n#     allow_username_or_email_login: false\n#     lowercase_usernames: false\n#     block_auto_created_users: false\n#     base: ''\n#     user_filter: ''\n#     ## EE only\n#     group_base: ''\n#     admin_group: ''\n#     sync_ssh_keys: false\n#\n#   secondary: # 'secondary' is the GitLab 'provider ID' of second LDAP server\n#     label: 'LDAP'\n#     host: '_your_ldap_server'\n#     port: 389\n#     uid: 'sAMAccountName'\n#     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'\n#     password: '_the_password_of_the_bind_user'\n#     encryption: 'plain' # \"start_tls\" or \"simple_tls\" or \"plain\"\n#     verify_certificates: true\n#     smartcard_auth: false\n#     active_directory: true\n#     allow_username_or_email_login: false\n#     lowercase_usernames: false\n#     block_auto_created_users: false\n#     base: ''\n#     user_filter: ''\n#     ## EE only\n#     group_base: ''\n#     admin_group: ''\n#     sync_ssh_keys: false\n# EOS\n<\/code><\/pre>\n\n\n\n<p>We will update this configuration section such that it looks like;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n### LDAP Settings\n\ngitlab_rails['ldap_enabled'] = true\ngitlab_rails['prevent_ldap_sign_in'] = false\n\ngitlab_rails['ldap_servers'] = {\n'main' => {\n  'label' => 'LDAP',\n  'host' =>  'ldap.kifarunix-demo.com',\n  'port' => 389,\n  'uid' => 'uid',\n  'encryption' => 'start_tls',\n  'verify_certificates' => false,\n  'tls_options' => {\n    'ca_file' => '\/etc\/ssl\/certs\/ldapca.pem'\n   # 'ssl_version' => '',\n   # 'ciphers' => '',\n   # 'cert' => '',\n   # 'key' => ''\n  },\n  'bind_dn' => 'cn=readonly,ou=system,dc=ldapmaster,dc=kifarunix-demo,dc=com',\n  'password' => 'hacker',\n  'timeout' => 10,\n  'active_directory' => false,\n  'allow_username_or_email_login' => true,\n  'block_auto_created_users' => true,\n  'base' => 'dc=ldapmaster,dc=kifarunix-demo,dc=com',\n  'user_filter' => '(memberOf=cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com)',\n  'attributes' => {\n    'username' => ['uid'],\n    'email' => ['Email'],\n    'name' => 'ui',\n    'first_name' => 'cn',\n    'last_name' => 'sn'\n  },\n  'lowercase_usernames' => false,\n\n  # EE Only\n  'group_base' => '',\n  'admin_group' => '',\n  'external_groups' => [],\n  'sync_ssh_keys' => false\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>Be sure to replace:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>host<\/code><\/strong>: address of the LDAP server, IP or resolvable hostname<\/li>\n\n\n\n<li><strong><code>bind_dn<\/code><\/strong>: The DN of the LDAP user to bind with.<\/li>\n\n\n\n<li><strong><code>bind_dn password<\/code><\/strong>: The password of the bind user.<\/li>\n\n\n\n<li><code><strong>base<\/strong><\/code>: Base where to search for users.<\/li>\n\n\n\n<li><strong><code>user_filter<\/code><\/strong>: Filter to specify the users allowed to login to Gitlab via OpenLDAP. In our case it the group we created above.<\/li>\n\n\n\n<li>The user attributes to match your LDAP setup.<\/li>\n<\/ul>\n\n\n\n<p><strong>NOTE for SSL configurations, we are using self-signed ssl for demo purposes. As such, we have set the verification to false.<\/strong><\/p>\n\n\n\n<p>Once you are done making changes to the configuration, reconfigure the Gitlab application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gitlab-ctl reconfigure<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Check the&nbsp;<code>bind_dn<\/code>&nbsp;and&nbsp;<code>password<\/code>&nbsp;credentials<\/h3>\n\n\n\n<p>Next, run the command below to \"<em>tests the&nbsp;<code>bind_dn<\/code>&nbsp;and&nbsp;<code>password<\/code>&nbsp;credentials (if configured) and lists a sample of LDAP users<\/em>\";<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gitlab-rake gitlab:ldap:check<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Checking LDAP ...\n\nLDAP: ... Server: ldapmain\nnot verifying SSL hostname of LDAPS server 'ldap.kifarunix-demo.com:389'\nLDAP authentication... Success\nLDAP users with access to your GitLab server (only showing the first 100 results)\n\tDN: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\t uid: johndoe\n\tDN: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\t uid: janedoe\n\nChecking LDAP ... Finished<\/code><\/pre>\n\n\n\n<p>Seems all is well, -:).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Login to Gitlab with OpenLDAP Credentials<\/h3>\n\n\n\n<p>Next, navigate to the Gitlab web interface. You will notice that on the login page, you have two options to login: LDAP and Standard.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1380\" height=\"708\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-login-page.png\" alt=\"Integrate Gitlab with OpenLDAP for Authentication\" class=\"wp-image-10697\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-login-page.png?v=1634383732 1380w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-login-page-768x394.png?v=1634383732 768w\" sizes=\"(max-width: 1380px) 100vw, 1380px\" \/><\/figure><\/div>\n\n\n<p>Login with an LDAP user. If the user account has to be validated by the admin before one can login, then you will see such a message;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Your account is pending approval from your GitLab administrator and hence blocked. Please contact your GitLab administrator if you think this is an error.<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1284\" height=\"706\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending-account-verification.png\" alt=\"\" class=\"wp-image-10698\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending-account-verification.png?v=1634383775 1284w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending-account-verification-768x422.png?v=1634383775 768w\" sizes=\"(max-width: 1284px) 100vw, 1284px\" \/><\/figure>\n\n\n\n<p>Login as the default Gitlab administrative user  using the standard account and verify the LDAP user account above.<\/p>\n\n\n\n<p>Navigate to <strong>Admin &gt; Users &gt; Pending Approval<\/strong>. You should see the LDAP user account. Click on the gear button adjacent to the user and click <strong>Approve<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1893\" height=\"720\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending_account-verification.png\" alt=\"\" class=\"wp-image-10705\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending_account-verification.png?v=1634384222 1893w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending_account-verification-768x292.png?v=1634384222 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/pending_account-verification-1536x584.png?v=1634384222 1536w\" sizes=\"(max-width: 1893px) 100vw, 1893px\" \/><\/figure>\n\n\n\n<p>User is now listed on Active users.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"673\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-active-users.png\" alt=\"\" class=\"wp-image-10704\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-active-users.png?v=1634384151 1920w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-active-users-768x269.png?v=1634384151 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/gitlab-active-users-1536x538.png?v=1634384151 1536w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<p>Now let the LDAP user login;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1892\" height=\"718\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/ldap-user-login.png\" alt=\"\" class=\"wp-image-10701\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/ldap-user-login.png?v=1634383857 1892w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/ldap-user-login-768x291.png?v=1634383857 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/10\/ldap-user-login-1536x583.png?v=1634383857 1536w\" sizes=\"(max-width: 1892px) 100vw, 1892px\" \/><\/figure>\n\n\n\n<p>And there you go.<\/p>\n\n\n\n<p>That marks the end of our tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Read more<\/h3>\n\n\n\n<p><a href=\"https:\/\/docs.gitlab.com\/ee\/administration\/auth\/ldap\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gitlab LDAP Documentation page<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-configure-dokuwiki-openldap-authentication\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Configure DokuWiki OpenLDAP Authentication<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will take you through how to integrate Gitlab with OpenLDAP for authentication. Gitlab supports LDAP authentication. In our previous tutorials, we learnt how<\/p>\n","protected":false},"author":3,"featured_media":10706,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,285,1099],"tags":[2916,4193,4192,4191,1248],"class_list":["post-10691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-directory-server","category-openldap","tag-gitlab","tag-gitlab-ldap-login","tag-gitlab-openldap-authentication","tag-integrate-gitlab-with-openldap-for-authentication","tag-ldap-authentication","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10691"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=10691"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10691\/revisions"}],"predecessor-version":[{"id":21636,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/10691\/revisions\/21636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/10706"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=10691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=10691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=10691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}