安全认证原理和认证机制 基本概念 以下为常见的基本概念,可以帮助用户减少学习Kerberos框架所花费的时间,有助于更好的理解Kerberos业务。以HDFS安全认证为例: TGT 票据授权票据(TicketGranting Ticket),由Kerberos服务生成,提供给应用程序与Kerberos服务器建立认证安全会话,该票据的默认有效期为24小时,24小时后该票据自动过期。 TGT申请方式(以HDFS为例): 1. 通过HDFS提供的接口获取。 / login Kerberos to get TGT, if the cluster is in security mode @throws IOException if login is failed / private void login() throws IOException { // not security mode, just return if (! "kerberos".equalsIgnoreCase(conf.get("hadoop.security.authentication"))) { return; } //security mode System.setProperty("java.security.krb5.conf", PATHTOKRB5CONF); UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab(PRNCIPALNAME, PATHTOKEYTAB); } 2. 通过客户端shell命令以kinit方式获取。 ST 服务票据(Server Ticket),由Kerberos服务生成,提供给应用程序与应用服务建立安全会话,该票据一次性有效。 ST的生成在FusionInsight产品中,基于hadooprpc通信,由rpc底层自动向Kerberos服务端提交请求,由Kerberos服务端生成。 认证代码实例讲解 package com.xxx.bigdata.hdfs.examples; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.security.UserGroupInformation; public class KerberosTest { private static String PATHTOHDFSSITEXML KerberosTest.class.getClassLoader().getResource("hdfssite.xml") .getPath(); private static String PATHTOCORESITEXML KerberosTest.class.getClassLoader().getResource("coresite.xml") .getPath(); private static String PATHTOKEYTAB KerberosTest.class.getClassLoader().getResource("user.keytab").getPath(); private static String PATHTOKRB5CONF KerberosTest.class.getClassLoader().getResource("krb5.conf").getPath(); private static String PRNCIPALNAME "develop"; private FileSystem fs; private Configuration conf; / initialize Configuration / private void initConf() { conf new Configuration(); // add configuration files conf.addResource(new Path(PATHTOHDFSSITEXML)); conf.addResource(new Path(PATHTOCORESITEXML)); } / login Kerberos to get TGT, if the cluster is in security mode @throws IOException if login is failed / private void login() throws IOException { // not security mode, just return if (! "kerberos".equalsIgnoreCase(conf.get("hadoop.security.authentication"))) { return; } //security mode System.setProperty("java.security.krb5.conf", PATHTOKRB5CONF); UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab(PRNCIPALNAME, PATHTOKEYTAB); } / initialize FileSystem, and get ST from Kerberos @throws IOException / private void initFileSystem() throws IOException { fs FileSystem.get(conf); } / An example to access the HDFS @throws IOException / private void doSth() throws IOException { Path path new Path("/tmp"); FileStatus fStatus fs.getFileStatus(path); System.out.println("Status of " + path + " is " + fStatus); //other thing } public static void main(String[] args) throws Exception { KerberosTest test new KerberosTest(); test.initConf(); test.login(); test.initFileSystem(); test.doSth(); } } 说明 1. Kerberos认证时需要配置Kerberos认证所需要的文件参数,主要包含keytab路径,Kerberos认证的用户名称,Kerberos认证所需要的客户端配置krb5.conf文件。 2. 方法login()为调用hadoop的接口执行Kerberos认证,生成TGT票据。 3. 方法doSth()调用hadoop的接口访问文件系统,此时底层RPC会自动携带TGT去Kerberos认证,生成ST票据。