LoginSignup
0
0

More than 1 year has passed since last update.

docker コンテナで構築するGitlabでLDAP連携(OpenLDAP)する方法

Last updated at Posted at 2023-03-31

環境

  • Ubuntu 20.04.4
  • Docker version 20.10.14
  • Docker Compose version v2.4.1
  • gitlab/gitlab-ee:15.9.4-ee.0

ソース

yamlの配置は以下を想定しています。

.
├── docker-compose.yaml
└── openldap
    └── testuser.ldif
version: '3'
services:
  gitlab:
    image: 'gitlab/gitlab-ee:15.9.4-ee.0'
    restart: always
    hostname: 'gitlab'
    container_name: 'gitlab'
    environment:
      # 初回ログインに利用するrootユーザーのパスワード
      GITLAB_ROOT_PASSWORD: "sutjg667"
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://localhost:18000'
        letsencrypt['enable'] = false
        gitlab_rails['omniauth_enabled'] = false
        gitlab_rails['ldap_enabled'] = true
        gitlab_rails['prevent_ldap_sign_in'] = false
        gitlab_rails['ldap_sync_worker_cron'] = "*/5 * * * *"
        gitlab_rails['time_zone'] = 'Asia/Tokyo'
        gitlab_rails['ldap_servers'] = YAML.load <<-EOS
          main:
            label: 'My Ldap'
            host: 'ldap-server'
            port: 389
            uid: 'cn'
            encryption: false
            verify_certificates: false
            bind_dn: 'cn=admin,dc=my-company,dc=com'
            password: 'admin'
            # tls_options:
            # ca_file: ''
            # ssl_version: ''
            # ciphers: ''
            # cert: ''
            # key: ''
            active_directory: false
            allow_username_or_email_login: true
            block_auto_created_users: true
            base: 'dc=my-company,dc=com'
            user_filter: ''
            lowercase_usernames: true
            # attributes:
            #   name: 'cn'
            # group_base : 'ou=gitlab_groups,ou=groups,dc=internal'
            # admin_group : 'gitlab_admins'
            # external_groups : []
            # sync_ssh_keys : false
        EOS
    ports:
      - '18000:18000'
    volumes:
      - ./volumes/gitlab-data/etc/gitlab:/etc/gitlab
      - ./volumes/gitlab-data/var/log/gitlab:/var/log/gitlab
      - ./volumes/gitlab-data/var/opt/gitlab:/var/opt/gitlab
    tty: true

  ldap-server:
    image: osixia/openldap:latest
    restart: always
    container_name: ldap-server
    environment:
      LDAP_ORGANISATION: "My Company"
      LDAP_DOMAIN: "my-company.com"
      LDAP_ADMIN_PASSWORD: "admin"
      LDAP_READONLY_USER: "true"
      LDAP_READONLY_USER_USERNAME: "readonly"
      LDAP_READONLY_USER_PASSWORD: "readonly_password"
      LDAP_TLS_VERIFY_CLIENT: "never" 
    ports:
      - "389:389"
    volumes:
      - ./volumes/openldap/var/lib/ldap:/var/lib/ldap
      - ./volumes/openldap/etc/ldap/slapd.d:/etc/ldap/slapd.d
      - ./openldap/testuser.ldif:/slapd/assets/custome/testuser.ldif
  
  ldap-admin:
    image: osixia/phpldapadmin:latest
    container_name: ldap-admin
    restart: always
    environment:
      PHPLDAPADMIN_LDAP_HOSTS: "ldap"
      PHPLDAPADMIN_HTTPS: "false"
    ports:
      - "8080:80"
    links:
      - "ldap-server:ldap"
  • GITLAB_ROOT_PASSWORDに指定するrootユーザーのパスワードが推測しやすいもの("passowrd"など)はブロックされるため、ログインできなかった。以下のドキュメントにその旨が記載されていた。

  • block_auto_created_userstrueにすると、LDAP認証を通過したユーザーは管理者権限を持ったユーザーの承認が無いとログインできないようにできる。逆にfalseにすると、認証が通ると勝手にユーザーが追加されてしまうので注意。

OpenLDAPに登録するテストユーザーの設定ファイルです。
test001ユーザーのパスワードはtest001test002ユーザーのパスワードはtest002です。

testuser.ldif
dn: cn=test001,dc=my-company,dc=com
givenName:test001
sn:test001
cn:test001
mail:test001@my-company.com
userPassword::dGVzdDAwMQ==
objectClass: inetOrgPerson
objectClass: top

dn: cn=test002,dc=my-company,dc=com
givenName:test002
sn:test002
cn:test002
mail:test002@my-company.com
userPassword::dGVzdDAwMg==
objectClass: inetOrgPerson
objectClass: top

コンテナ起動

docker-compose up -d

ログイン画面にアクセスできるまでに数分かかる。

OpenLDAPにテストユーザーを登録

以下のコマンドでテストユーザーを登録する。

docker exec ldap-server ldapadd -x -D "cn=admin,dc=my-company,dc=com" -w admin -f /slapd/assets/custome/testuser.ldif -ZZ

アクセス確認

login.png

rootユーザーでログイン

ログインすると以下の画面に遷移した。

login-root.png

test001ユーザーでログイン

承認待ち状態になる。

pending-approval.png

rootユーザーでログインして、
Main menu >> Admin >> View latest users >> Pending approval >> User administration
と進んで承認する。

test001.png

確認されるので承認する。

approve-test001.png

再度ログイン画面からtest001ユーザーでログインする。
以下の画面に遷移する。

test001-login.png

セットアップは以上。

まとめ

GitlabでLDAP連携する方法をまとめました。
ログインと同時にユーザーを作成するかどうかのオプション(block_auto_created_users)には注意が必要です。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0