

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# LDAP 認証の使用
<a name="emr-jupyterhub-ldap-users"></a>

Lightweight Directory Access Protocol (LDAP) は、Active Directory または OpenLDAP サーバーのような LDAPとの互換性があるディレクトリサーバープロバイダーで保存されているユーザーやコンピュータなどのリソースに対応するオブジェクトのクエリや変更を行うアプリケーションプロトコルです。Amazon EMR の JupyterHub と [JupyterHub の LDAP 認証プラグイン](https://github.com/jupyterhub/ldapauthenticator/)を使用して、ユーザー認証に LDAP を使うことができます。プラグインは LDAP ユーザーのログインセッションを処理し、Jupyter にユーザー情報を提供します。これにより、ユーザーは LDAP と互換性のあるサーバーに保存している ID の認証情報を使用して JupyterHub とノートブックに接続できるようになります。

このセクションのステップは、JupyterHub の LDAP 認証プラグインを使用して LDAP をセットアップし有効にするための手順を示しています。このステップはマスターノードのコマンドラインに接続している状態で実行します。詳細については、「[マスターノードとノートブックサーバーに接続する](emr-jupyterhub-connect.md)」を参照してください。

1. ホスト IP アドレス、ポート、バインディング名など、LDAP サーバーに関する情報を使用して LDAP 設定ファイルを作成します。

1. `/etc/jupyter/conf/jupyterhub_config.py` を変更して、JupyterHub の LDAP 認証プラグインを有効にします。

1. `jupyterhub` コンテナ内で LDAP を設定するスクリプトを作成して実行します。

1. ユーザー用に LDAP をクエリしてから、各ユーザーのコンテナ内でホームディレクトリを作成します。ノートブックをホストするため、JupyterHub にはホームディレクトリが必要です。

1. JupyterHub を再開するスクリプトを実行する

**重要**  
LDAPを設定する前に、LDAP サーバーとクラスターマスターノードが必要に応じて通信できることを確認するため、ネットワークインフラストラクチャをテストしてください。TLS は通常、単純な TCP 接続でポート 389 を使用します。LDAP 接続が SSL を使用する場合の一般的な SSL の TCP ポートは 636 です。

## LDAP 設定ファイルを作成する
<a name="emr-jupyterhub-ldap-config"></a>

以下の例では、次のプレースホルダー設定の値を使用します。これらの値を実装と一致するパラメータに置き換えます。
+ LDAP サーバーはバージョン 3 を実行していて、ポート 389 で使用可能になっています。これは標準的な LDAP の非 SSL ポートです。
+ ベース識別子名 (DN) は `dc=example, dc=org` です。

テキストエディタを使用して、次のようなコンテンツを含む [ldap.conf](http://manpages.ubuntu.com/manpages/bionic/man5/ldap.conf.5.html) ファイルを作成します。LDAP 実装に適切な値を使用します。*host* を IP アドレスまたは LDAP サーバーの解決可能なホスト名に置き換えます。

```
base dc=example,dc=org
uri ldap://host
ldap_version 3
binddn cn=admin,dc=example,dc=org
bindpw admin
```

## JupyterHub の LDAP 認証プラグインを有効にする
<a name="emr-jupyterhub-ldap-plugin"></a>

テキストエディタを使用して `/etc/jupyter/conf/jupyterhub_config.py` ファイルを変更し、次のような [ldapauthenticator](https://github.com/jupyterhub/ldapauthenticator) プロパティを追加します。*host* を IP アドレスまたは LDAP サーバーの解決可能なホスト名に置き換えます。この例では、ユーザーオブジェクトが *people* という組織単位 (ou) にあり、先に `ldap.conf` を使って確立した識別名のコンポーネントを使用しているものとします。

```
c.JupyterHub.authenticator_class = 'ldapauthenticator.LDAPAuthenticator'
c.LDAPAuthenticator.use_ssl = False
c.LDAPAuthenticator.server_address = 'host' 
c.LDAPAuthenticator.bind_dn_template = 'cn={username},ou=people,dc=example,dc=org'
```

## コンテナ内で LDAP を設定する
<a name="emr-jupyterhub-ldap-container"></a>

テキストエディタを使用して、次のコンテンツを含む bash スクリプトを作成します。

```
#!/bin/bash

# Uncomment the following lines to install LDAP client libraries only if
# using Amazon EMR release version 5.14.0. Later versions install libraries by default.
# sudo docker exec jupyterhub bash -c "sudo apt-get update"
# sudo docker exec jupyterhub bash -c "sudo apt-get -y install libnss-ldap libpam-ldap ldap-utils nscd"
 
# Copy ldap.conf
sudo docker cp ldap.conf jupyterhub:/etc/ldap/
sudo docker exec jupyterhub bash -c "cat /etc/ldap/ldap.conf"
 
# configure nss switch
sudo docker exec jupyterhub bash -c "sed -i 's/\(^passwd.*\)/\1 ldap/g' /etc/nsswitch.conf"
sudo docker exec jupyterhub bash -c "sed -i 's/\(^group.*\)/\1 ldap/g' /etc/nsswitch.conf"
sudo docker exec jupyterhub bash -c "sed -i 's/\(^shadow.*\)/\1 ldap/g' /etc/nsswitch.conf"
sudo docker exec jupyterhub bash -c "cat /etc/nsswitch.conf"
 
# configure PAM to create home directories
sudo docker exec jupyterhub bash -c "echo 'session required        pam_mkhomedir.so skel=/etc/skel umask=077' >> /etc/pam.d/common-session"
sudo docker exec jupyterhub bash -c "cat /etc/pam.d/common-session"
 
# restart nscd service
sudo docker exec jupyterhub bash -c "sudo service nscd restart"
 
# Test
sudo docker exec jupyterhub bash -c "getent passwd"

# Install ldap plugin
sudo docker exec jupyterhub bash -c "pip install jupyterhub-ldapauthenticator"
```

マスターノードにスクリプトを保存し、マスターノードのコマンドラインから実行します。たとえば、`configure_ldap_client.sh` として保存されているスクリプトでファイルを実行可能にします。

```
chmod +x configure_ldap_client.sh
```

スクリプトを実行します。

```
./configure_ldap_client.sh
```

## Active Directory に属性を追加する
<a name="emr-jupyterhub-ldap-adproperties"></a>

ユーザーを見つけ、データベースに適切なエントリを作成するため、JupyterHub docker コンテナでは、Active Directory の該当するユーザーオブジェクトに次の UNIX プロパティが必要です。詳細については、記事「[Clarification regarding the status of identity management for Unix (IDMU) and NIS server role in Windows Server 2016 technical preview and beyond](https://blogs.technet.microsoft.com/activedirectoryua/2016/02/09/identity-management-for-unix-idmu-is-deprecated-in-windows-server/)」(Windows Server 2016テクニカルプレビュー以降でのUnix（IDMU）およびNISサーバーの役割のID管理のステータスに関する説明）、のセクション「*How do I continue to edit the GID/UID RFC 2307 attributes now that the Unix Attributes Plug-in is no longer available for the Active Directory Users and Computers MMC snap-in?*」(Unix属性プラグインがActiveDirectoryユーザーとコンピューターMMCスナップインで使用できなくなった場合、GID / UID RFC 2307属性を編集し続けるにはどうすればよいですか？) を参照してください。
+ `homeDirectory`

  これは、ユーザーのホームディレクトリへの場所 (通常は `/home/username`) です。
+ `gidNumber`

  これは、別のユーザーがすでに使用していない 60000 より大きい値です。使用中の GID については、`etc/passwd` ファイルを確認してください。
+ `uidNumber`

  これは、別のグループがすでに使用していない 60000 より大きい値です。使用中の UID については、`etc/group` ファイルを確認してください。
+ `uid`

  これは *username* と同じです。

## ユーザーのホームディレクトリを作成する
<a name="emr-jupyterhub-ldap-directories"></a>

LDAP ユーザーを認証しインスタンスデータを保存するため、JupyterHub はコンテナ内のホームディレクトリを必要とします。次の例は LDAP ディレクトリの*shirley* と *diego* という 2 人のユーザーを示しています。

まず、次の例のように [ldapsearch](http://manpages.ubuntu.com/manpages/xenial/man1/ldapsearch.1.html) を使用して、各ユーザーの ID とグループ ID 情報に LDAP サーバーをクエリします。*host* は LDAP サーバーの IP アドレスまたは解決可能なホスト名に置き換わります。

```
ldapsearch -x -H ldap://host \
 -D "cn=admin,dc=example,dc=org" \
 -w admin \
 -b "ou=people,dc=example,dc=org" \
 -s sub \
 "(objectclass=*)" uidNumber gidNumber
```

`ldapsearch` コマンドが *shirley* と *diego* といった 2 人のユーザーに見られるような LDIF 形式のレスポンスを返します。

```
# extended LDIF

# LDAPv3
# base <ou=people,dc=example,dc=org> with scope subtree
# filter: (objectclass=*)
# requesting: uidNumber gidNumber sn 

# people, example.org
dn: ou=people,dc=example,dc=org

# diego, people, example.org
dn: cn=diego,ou=people,dc=example,dc=org
sn: B
uidNumber: 1001
gidNumber: 100

# shirley, people, example.org
dn: cn=shirley,ou=people,dc=example,dc=org
sn: A
uidNumber: 1002
gidNumber: 100

# search result
search: 2
result: 0 Success

# numResponses: 4
# numEntries: 3
```

レスポンスからの情報を使用し、コンテナ内のコマンドを実行して各ユーザーの共通名 (`cn`) にホームディレクトリを作成します。`uidNumber` と `gidNumber` を使用して、そのユーザーのホームディレクトリの所有権を修正します。その操作を次の例のコマンドがユーザーの *shirley* に行います。

```
sudo docker container exec jupyterhub bash -c "mkdir /home/shirley"
sudo docker container exec jupyterhub bash -c "chown -R $uidNumber /home/shirley"
sudo docker container exec jupyterhub bash -c "sudo chgrp -R $gidNumber /home/shirley"
```

**注記**  
JupyterHub の LDAP オーセンティケータでは、ローカルユーザーの作成がサポートされていません。詳細については、「[LDAP authenticator configuration note on local user creation](https://github.com/jupyterhub/ldapauthenticator#configuration-note-on-local-user-creation)」を参照してください。  
ローカルユーザーを手動で作成するには、次のコマンドを使用します。  

```
sudo docker exec jupyterhub bash -c "echo 'shirley:x:$uidNumber:$gidNumber::/home/shirley:/bin/bash' >> /etc/passwd"
```

## JupyterHub コンテナの再起動
<a name="emr-jupyterhub-ldap-restart"></a>

以下のコマンドを実行して、`jupyterhub` コンテナを再起動します。

```
sudo docker stop jupyterhub
sudo docker start jupyterhub
```