本ページでは、HDFS サービスを例に取って、 MIT Kerberos 認証プロトコルの認証プロセスを説明します。
MIT Kerberos プロトコルを使用して認証された ID
EMR クラスターの Kerberos サービスはマスターノードで実行されます。 root アカウントを使って管理操作を実行する必要がありますが、 管理操作が実行できるのは emr-header-1 という名前のマスターノードのみです。
以下の例では、テストユーザーが HDFS サービスにアクセスする認証プロセスを説明します。
- ゲートウェイでの
hadoop fs -ls /
の実行- krb5.conf ファイルのダウンロード
ルートアカウントを使用してゲートウェイにログインします。 scp root@emr-header-1:/etc/krb5.conf /etc/
- プリンシパルの追加
- emr-header-1 ノードにログインし、root アカウントに切り替えます。 emr-header-2 ノードで高可用性 (HA) を設定できないため、ログインするマスターノードは emr-header-1 である必要があります。
- Kerberos の管理ツールを開きます。
-
sh /usr/lib/has-current/bin/hadmin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab HadminLocalTool.local: #Press Enter to view a list of commands and the usage of each command. HadminLocalTool.local: addprinc #Press Enter to view the usage of the command. HadminLocalTool.local: addprinc -pw 123456 test #Add a principal named test and specify 123456 as the password.
- keytab ファイルのエクスポート
emr-header-1 ノードにログインし、keytab ファイルをインポートします。 emr-header-2 ノードで HA を設定できないため、ノードは emr-header-1 でなければなりません。
Kerberos admin ツールを使用してプリンシパルに対応する keytab ファイルをエクスポートします。
HadminLocalTool.local: ktadd -k /root/test.keytab test #Export the keytab file for later use.
- kinit コマンドを使用したチケットの取得
HDFS コマンドをクライアント (ゲートウェイ) で実行します。
- Linux アカウント test を作成します。
useradd test
- MIT Kerberos クライアントをインストールします。
MIT Kerberos クライアントを使用して、kinit や klist などのコマンドを実行できます。 詳細については、「MIT Kerberos ドキュメント」をご参照ください。
yum install krb5-libs krb5-workstation -y
- test アカウントに切り替えて、kinit を実行します。
su test #If the keytab file does not exist, run the following command: kinit #Press Enter Password for test: 123456 #Enter the password. #If the keytab file exists, run the following command: kinit -kt test.keytab test #View a ticket klist
注 MIT Kerberos コマンドの例
- Linux アカウント test を作成します。
- HDFS コマンドの例
チケットを取得したら、HDFS コマンドを実行できます。
hadoop fs -ls / Found 5 items drwxr-xr-x - hadoop hadoop 0 2017-11-12 14:23 /apps drwx------ - hbase hadoop 0 2017-11-15 19:40 /hbase drwxrwx--t+ - hadoop hadoop 0 2017-11-15 17:51 /spark-history drwxrwxrwt - hadoop hadoop 0 2017-11-13 23:25 /tmp drwxr-x--t - hadoop hadoop 0 2017-11-13 16:12 /user
注 YARN ジョブを実行するには、クラスターのすべてのノードに対応する Linux アカウントを追加する必要があります。 詳細については、「EMR クラスターへのアカウントの追加」をご参照ください。
- krb5.conf ファイルのダウンロード
- Java スニペットを使用した HDFS へのアクセス
- チケットキャッシュの使用
注 チケットを取得するには、あらかじめ kinit コマンドを実行する必要があります。 アプリケーションが期限切れのチケットにアクセスしようとすると、エラーが発生します。
public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); //Load the configurations of HDFS. You can retrieve a copy of configurations from the EMR cluster. conf.addResource(new Path("/etc/ecm/hadoop-conf/hdfs-site.xml")); conf.addResource(new Path("/etc/ecm/hadoop-conf/core-site.xml")); //Run the kinit command by using the Linux account to obtain a ticket before running the Java snippet. UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromSubject(null); FileSystem fs = FileSystem.get(conf); FileStatus[] fsStatus = fs.listStatus(new Path("/")); for(int i = 0; i < fsStatus.length; i++){ System.out.println(fsStatus[i].getPath().toString()); } }
- (推奨) keytab ファイルの使用
注 keytab ファイルは長期間有効で、 ローカルチケットとは無関係です。
public static void main(String[] args) throws IOException { String keytab = args[0]; String principal = args[1]; Configuration conf = new Configuration(); //Load the configurations of HDFS. You can retrieve a copy of configurations from the EMR cluster. conf.addResource(new Path("/etc/ecm/hadoop-conf/hdfs-site.xml")); conf.addResource(new Path("/etc/ecm/hadoop-conf/core-site.xml")); //Use the keytab file. You can retrieve the keytab file from the master-1 node by running the kinit command. UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab(principal, keytab); FileSystem fs = FileSystem.get(conf); FileStatus[] fsStatus = fs.listStatus(new Path("/")); for(int i = 0; i < fsStatus.length; i++){ System.out.println(fsStatus[i].getPath().toString()); } }
以下の依存関係が pom.xml ファイルで指定されています。
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.2</version> </dependency> </dependencies>
- チケットキャッシュの使用