Use Hadoop Auth Server (HAS) Kerberos authentication to access HDFS from a gateway node or Java application.
Prerequisites
You have created a Hadoop cluster that runs EMR-3.40 or earlier, or EMR-4.10.1 or earlier. For more information, see Create a cluster.
Access HDFS using the hadoop command
The following steps use test as an example user.
- Configure the krb5.conf file on the gateway node.
scp root@emr-header-1:/etc/krb5.conf /etc/ - Set the hadoop.security.authentication.use.has parameter to false.
- Log on to the
emr-header-1node of the cluster.Note For an HA cluster, you must also log on to theemr-header-1node.For more information, see Log on to a cluster.
- Run the following command to edit the core-site.xml file.
vim /etc/ecm/hadoop-conf/core-site.xml - Check the value of hadoop.security.authentication.use.has.
- If the value is true, change the value of hadoop.security.authentication.use.has to false.
- If the value is false, proceed to Step 3.
- Log on to the
- Add a principal.
- Run the following command to start the Kerberos administration tool.
- For EMR-3.30.0 or later and EMR-4.5.1 or later:
sh /usr/lib/has-current/bin/admin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab - For versions earlier than EMR-3.30.0 and EMR-4.5.1:
sh /usr/lib/has-current/bin/hadmin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab
- For EMR-3.30.0 or later and EMR-4.5.1 or later:
- Run the following command to add the
testprincipal.In this example, the password is set to
123456.addprinc -pw 123456 test - Run the following command to export the keytab file.
ktadd -k /root/test.keytab test
- Run the following command to start the Kerberos administration tool.
- Obtain a ticket.
Run the following commands on the client where you run HDFS commands. A gateway node is used in this example.
- Create a Linux account named
test.useradd test - Install the MIT Kerberos client tools.
MIT Kerberos provides commands such as
kinitandklist. For more information, see MIT Kerberos.yum install krb5-libs krb5-workstation -y - Switch to the
testaccount and runkinit.su test- If you do not have a keytab file, run
kinitand enter the password123456when prompted. - If you have a keytab file, run the following commands.
# Authenticate with the specified principal in the specified keytab file. kinit -kt test.keytab test # View the ticket lifetime. klistRunklist. Sample output:Valid starting Expires Service principal 03/30/2021 10:48:47 03/31/2021 10:48:47 krbtgt/EMR.209749.COM@EMR.209749.COM renew until 03/31/2021 10:48:47
- If you do not have a keytab file, run
- Optional: To set the ticket lifetime:
- Set the ticket lifetime.
kinit -l 5d - Run the
klistcommand to view the ticket lifetime.Valid starting Expires Service principal 03/30/2021 10:50:51 04/04/2021 10:50:51 krbtgt/EMR.209749.COM@EMR.209749.COM renew until 04/01/2021 10:50:51
- Set the ticket lifetime.
- Create a Linux account named
- Run the following command on the gateway node to set the environment variable.
export HADOOP_CONF_DIR=/etc/has/hadoop-conf - Run an HDFS command.
hadoop fs -ls /Sample output:Found 6 items drwxr-xr-x - hadoop hadoop 0 2021-03-29 11:16 /apps drwxrwxrwx - flowagent hadoop 0 2021-03-29 11:18 /emr-flow drwxr-x--- - has hadoop 0 2021-03-29 11:16 /emr-sparksql-udf drwxrwxrwt - hadoop hadoop 0 2021-03-29 11:17 /spark-history drwxr-x--- - hadoop hadoop 0 2021-03-29 11:16 /tmp drwxrwxrwt - hadoop hadoop 0 2021-03-29 11:17 /user
Access HDFS using Java code
- Use a local ticket cache Note You must run
kinitbeforehand to obtain a ticket. After the ticket expires, the application can no longer access HDFS.public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); // Load HDFS configurations. You need to copy the HDFS 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")); // You must run kinit beforehand under the Linux account to obtain a ticket. 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()); } } - Use a keytab file (Recommended) Note A keytab file is valid long term and does not depend on the local ticket.
public static void main(String[] args) throws IOException { String keytab = args[0]; String principal = args[1]; Configuration conf = new Configuration(); // Load HDFS configurations. You need to copy the HDFS 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 obtained from the emr-header-1 node of the EMR cluster. 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()); } }The following Maven dependencies are required.<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>x.x.x</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>x.x.x</version> </dependency> </dependencies>Note Replacex.x.xwith your cluster's Hadoop version.