TECHSTEP

ITインフラ関連の記事を公開してます。

GitLabとActive Directoryを連携する

今回はGitLabとActive Directoryとを連携したユーザー管理の紹介です。

docs.gitlab.com

背景

以前GitLabのユーザーについて紹介しましたが、GitLabはLDAPと統合したユーザー認証機能を提供しています。GitLabとActive Directoryの連携はLDAPによるユーザー認証の1つです。Active Directoryと連携することで、組織に導入済みの認証基盤を利用しつつGitLabも利用できます。

検証

ここから実際にGitLabとADとの連携を行います。検証時の条件は以下の通りです。

  • GitLab: Self-managed版をAmazon EC2に構築
    • OS: Amazon Linux 2
    • サイズ: t3.xlarge
    • Session Managerでアクセスできるよう設定
  • Active DIrectory: Amazon EC2に構築
    • OS: Windows Server 2019
    • サイズ: t3.xlarge
    • ドメイン名: gitlab.domain.example.com
    • ユーザー: test01 test02 を作成
  • その他
    • GitLab / ADは同じサブネット上に作成

GitLabの設定変更

GitLabとLDAPを統合するには /etc/gitlab/gitlab.rb を修正し設定変更を行う必要があります。

# バックアップ用にコピーを作成
$ sudo cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb_20240218

$ sudo vi /etc/gitlab/gitlab.rb

(以下のように修正)

### LDAP Settings
###! Docs: https://docs.gitlab.com/ee/administration/auth/ldap/index.html
###! **Be careful not to break the indentation in the ldap_servers block. It is
###!   in yaml format and the spaces must be retained. Using tabs will not work.**

gitlab_rails['ldap_enabled'] = true
gitlab_rails['prevent_ldap_sign_in'] = false

###! **remember to close this block with 'EOS' below**
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
  main: # 'main' is the GitLab 'provider ID' of this LDAP server
    label: 'LDAP'
    host: '<ADサーバーのIPアドレスを指定>'
    port: 389
    uid: 'sAMAccountName'
    bind_dn: 'CN=test01,CN=Users,DC=gitlab,DC=domain,DC=example,DC=com'
    password: '<bind_dnのユーザーのパスワードを指定>'
    encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
    verify_certificates: false
    smartcard_auth: false
    active_directory: true
    allow_username_or_email_login: true
    lowercase_usernames: false
    block_auto_created_users: false
    base: 'DC=gitlab,DC=domain,DC=example,DC=com'
    user_filter: ''
    ## EE only
    group_base: ''
    admin_group: ''
    sync_ssh_keys: false
#
#   secondary: # 'secondary' is the GitLab 'provider ID' of second LDAP server
#     label: 'LDAP'
#     host: '_your_ldap_server'
#     port: 389
#     uid: 'sAMAccountName'
#     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'
#     password: '_the_password_of_the_bind_user'
#     encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
#     verify_certificates: true
#     smartcard_auth: false
#     active_directory: true
#     allow_username_or_email_login: false
#     lowercase_usernames: false
#     block_auto_created_users: false
#     base: ''
#     user_filter: ''
#     ## EE only
#     group_base: ''
#     admin_group: ''
#     sync_ssh_keys: false
EOS

修正箇所を抜粋します。

  • host: ADのIPアドレス / ドメイン名を指定
  • bind_dn: BindするユーザーのDNを指定
  • password: bind_dnで指定したユーザーのパスワードを指定
  • verify_certificates: encryption が "start_tls" or "simple_tls"の場合はtrueに設定
  • allow_username_or_email_login: ログイン時にユーザーが送信したLDAPユーザー名の@以降を無視する
  • base: ベースとなるDNを指定

docs.gitlab.com

上記修正後、GitLabに反映します。

$ sudo gitlab-ctl reconfigure

(割愛)

gitlab Reconfigured!

GitLabへの反映を完了後にログイン画面を見ると、LDAPという項目が追加されています。

試しに test01 のログインを試すと、問題なくログインできます。

Authentication Logを見るとLDAPでの認証が行われた様子も記録されています。

rootユーザーで Admin Area からユーザーを確認すると、ログインした test01 が表示されます。

指定したLDAPの情報なども確認できます。