> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 認証とロールマッピングに LDAP を使用するよう ClickHouse を設定する

> 認証とロールマッピングに LDAP を使用するよう ClickHouse を設定する方法を説明します

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud では利用できません
        </div>;
};

<CloudNotSupportedBadge />

<Note>
  このページは [ClickHouse Cloud](https://clickhouse.com/cloud) には適用されません。ここで説明している機能は ClickHouse Cloud サービスではご利用いただけません。
  詳しくは、ClickHouse の [Cloud Compatibility](/docs/ja/products/cloud/guides/cloud-compatibility) ガイドを参照してください。
</Note>

ClickHouse は、ClickHouse のデータベースユーザーの認証に LDAP を使用するよう設定できます。このガイドでは、公開されているディレクトリに対して認証を行う LDAP システムと ClickHouse を統合する簡単な例を紹介します。

<Steps>
  <Step title="ClickHouse で LDAP 接続設定を行う" id="1-configure-ldap-connection-settings-in-clickhouse">
    1. この公開 LDAP サーバーへの接続をテストします。

       ```bash theme={null}
       $ ldapsearch -x -b dc=example,dc=com -H ldap://ldap.forumsys.com
       ```

       応答は次のようになります。

       ```response theme={null}
       # 拡張 LDIF
       #
       # LDAPv3
       # base <dc=example,dc=com>、スコープ subtree
       # フィルタ: (objectclass=*)
       # 要求: ALL
       #

       # example.com
       dn: dc=example,dc=com
       objectClass: top
       objectClass: dcObject
       objectClass: organization
       o: example.com
       dc: example
       ...
       ```

    2. `config.xml` ファイルを編集し、LDAP を設定するために次を追加します。
       ```xml theme={null}
       <ldap_servers>
           <test_ldap_server>
           <host>ldap.forumsys.com</host>
           <port>389</port>
           <bind_dn>uid={user_name},dc=example,dc=com</bind_dn>
           <enable_tls>no</enable_tls>
           <tls_require_cert>never</tls_require_cert>
           </test_ldap_server>
       </ldap_servers>
       ```

    <Note>
      `<test_ldap_server>` タグは、特定の LDAP サーバーを識別するための任意のラベルです。
    </Note>

    上記で使用している基本設定は次のとおりです。

    | Parameter          | Description               | Example                             |
    | ------------------ | ------------------------- | ----------------------------------- |
    | host               | LDAP サーバーのホスト名または IP      | ldap.forumsys.com                   |
    | port               | LDAP サーバーのディレクトリポート       | 389                                 |
    | bind\_dn           | ユーザーを指定するテンプレートパス         | `uid={user_name},dc=example,dc=com` |
    | enable\_tls        | セキュアな LDAP を使用するかどうか      | no                                  |
    | tls\_require\_cert | connection に証明書を必須にするかどうか | never                               |

    <Note>
      この例では、公開サーバーが 389 を使用しておりセキュアポートを使用していないため、デモ目的で TLS を無効にしています。
    </Note>

    <Note>
      LDAP 設定の詳細については、[LDAP doc page](/docs/ja/concepts/features/security/external-authenticators/ldap)を参照してください。
    </Note>

    3. ユーザーのロールマッピングを設定するため、`<user_directories>` セクションに `<ldap>` セクションを追加します。このセクションでは、ユーザーの認証方法と、認証後に付与されるロールを定義します。この基本例では、LDAP で認証されたすべてのユーザーに `scientists_role` が付与されます。これは後の手順で ClickHouse に定義します。セクションは次のようになります。

       ```xml theme={null}
       <user_directories>
           <users_xml>
               <path>users.xml</path>
           </users_xml>
           <local_directory>
               <path>/var/lib/clickhouse/access/</path>
           </local_directory>
           <ldap>
                 <server>test_ldap_server</server>
                 <roles>
                    <scientists_role />
                 </roles>
                 <role_mapping>
                    <base_dn>dc=example,dc=com</base_dn>
                    <search_filter>(&amp;(objectClass=groupOfUniqueNames)(uniqueMember={bind_dn}))</search_filter>
                    <attribute>cn</attribute>
                 </role_mapping>
           </ldap>
       </user_directories>
       ```

       上記で使用している基本設定は次のとおりです。

       | Parameter      | Description                          | Example                                                       |
       | -------------- | ------------------------------------ | ------------------------------------------------------------- |
       | server         | 前の ldap\_servers セクションで定義したラベル       | test\_ldap\_server                                            |
       | roles          | ユーザーのマッピング先となる、ClickHouse で定義されたロール名 | scientists\_role                                              |
       | base\_dn       | ユーザーに関連付けられたグループの検索を開始するベースパス        | dc=example,dc=com                                             |
       | search\_filter | ユーザーをマッピングする対象グループを識別する LDAP 検索フィルタ  | `(&(objectClass=groupOfUniqueNames)(uniqueMember={bind_dn}))` |
       | attribute      | どの attribute 名の値を返すか                 | cn                                                            |

    4. 設定を反映するために ClickHouse server を再起動します。
  </Step>

  <Step title="ClickHouse データベースのロールと権限を設定する" id="2-configure-clickhouse-database-roles-and-permissions">
    <Note>
      このセクションの手順では、ClickHouse で SQL Access Control and Account Management が有効になっていることを前提としています。有効にするには、[SQL Users and Roles ガイド](/docs/ja/concepts/features/security/access-rights)を参照してください。
    </Note>

    1. `config.xml` ファイルのロールマッピングセクションで使用したものと同じ名前のロールを ClickHouse に作成します
       ```sql theme={null}
       CREATE ROLE scientists_role;
       ```

    2. ロールに必要な権限を付与します。次のステートメントでは、LDAP 経由で認証できるすべてのユーザーに管理者権限を付与します。
       ```sql theme={null}
       GRANT ALL ON *.* TO scientists_role;
       ```
  </Step>

  <Step title="LDAP 設定をテストする" id="3-test-the-ldap-configuration">
    1. ClickHouse clientでログインします
       ```bash theme={null}
       $ clickhouse-client --user einstein --password password
       ClickHouse client version 22.2.2.1.
       Connecting to localhost:9000 as user einstein.
       Connected to ClickHouse server version 22.2.2 revision 54455.

       chnode1 :)
       ```

    <Note>
      手順 1 で `ldapsearch` コマンドを使用すると、directory 内で利用可能なすべてのユーザーを確認できます。なお、すべてのユーザーのパスワードは `password` です
    </Note>

    2. ユーザーが `scientists_role` ロールに正しくマッピングされ、管理者権限を持っていることを確認します

       ```sql theme={null}
       SHOW DATABASES
       ```

       ```response theme={null}
       Query id: 93b785ff-1482-4eda-95b0-b2d68b2c5e0f

       ┌─name───────────────┐
       │ INFORMATION_SCHEMA │
       │ db1_mysql          │
       │ db2                │
       │ db3                │
       │ db4_mysql          │
       │ db5_merge          │
       │ default            │
       │ information_schema │
       │ system             │
       └────────────────────┘

       9 rows in set. Elapsed: 0.004 sec.
       ```
  </Step>
</Steps>

<div id="summary">
  ## まとめ
</div>

この記事では、ClickHouse を LDAP サーバーに対する認証用に設定し、ロールにマッピングする基本的な方法を説明しました。ClickHouse では個別のユーザーを設定しつつ、自動 ロールマッピング を構成せずに、それらのユーザーを LDAP で認証することも可能です。LDAP モジュールは、Active Directory への接続にも利用できます。
