2024年【kerberos】hadoop集群使用keytab认证的逻辑_centos 8 hadoop-2,2024年最新看完直接跪服

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Specifies the location of the Kerberos configuration file. The default is SYSCONFDIR/krb5.conf. Multiple filenames can be specified, separated by a colon; all files which are present will be read.

KRB5_KDC_PROFILE
Specifies the location of the KDC configuration file, which contains additional configuration directives for the Key Distribution Center daemon and associated programs. The default is LOCALSTATEDIR/krb5kdc/kdc.conf.

KRB5RCACHENAME
(New in release 1.18) Specifies the location of the default replay cache, in the form type:residual. The file2 type with a pathname residual specifies a replay cache file in the version-2 format in the specified location. The none type (residual is ignored) disables the replay cache. The dfl type (residual is ignored) indicates the default, which uses a file2 replay cache in a temporary directory. The default is dfl:.

KRB5RCACHETYPE
Specifies the type of the default replay cache, if KRB5RCACHENAME is unspecified. No residual can be specified, so none and dfl are the only useful types.

KRB5RCACHEDIR
Specifies the directory used by the dfl replay cache type. The default is the value of the TMPDIR environment variable, or /var/tmp if TMPDIR is not set.

KRB5_TRACE
Specifies a filename to write trace log output to. Trace logs can help illuminate decisions made internally by the Kerberos libraries. For example, env KRB5_TRACE=/dev/stderr kinit would send tracing information for kinit to /dev/stderr. The default is not to write trace log output anywhere.

KRB5_CLIENT_KTNAME
Default client keytab file name. If unset, DEFCKTNAME will be used).

KPROP_PORT
kprop port to use. Defaults to 754.

GSS_MECH_CONFIG
Specifies a filename containing GSSAPI mechanism module configuration. The default is to read SYSCONFDIR/gss/mech and files with a .conf suffix within the directory SYSCONFDIR/gss/mech.d.

以上环境变量常用的有
KRB5_CONFIG :krb5.conf或krb5.ini文件路径
KRB5CCNAME:kerberos cache文件路径(注:此文件可由MIT kerberos客户端生成)

二、具体认证步骤

1、krb5.conf信息配置

注意:UserGroupInformation中设置KRB5_CONFIG是没有用的,必须要设置java.security.krb5.conf

如下方法都可以:
(1)项目启动指定java vm变量:-Djava.security.krb5.conf=D:/xxx/xxx/krb5.conf
(2)程序中指定:System.setProperty("java.security.krb5.conf", "D:/xxx/xxx/krb5.conf")

如果不指定程序会找不到kdc,报异常,如下:

org.apache.hadoop.security.KerberosAuthException: failure to login: for principal: xxx@HADOOP.COM from keytab D:\xxx\xxx\xxx.keytab javax.security.auth.login.LoginException: null (68)
Caused by: javax.security.auth.login.LoginException: null (68)
Caused by: KrbException: null (68) 
Caused by: KrbException: Identifier doesn't match expected value (906)

2、hadoop conf信息配置

Hadoop configuration配置(类org.apache.hadoop.conf.Configuration
文档中明确了:会默认加载类路径下的core-default.xml文件内容。

Unless explicitly turned off, Hadoop by default specifies two resources, loaded in-order from the classpath:
core-default.xml: Read-only defaults for hadoop.
core-site.xml: Site-specific configuration for a given hadoop installation.

core-default.xml中含有hadoop的安全配置hadoop.security.authentication,在UserGroupInformation中依据此项配置,查询集群是否启动kerberos。
HADOOP_SECURITY_AUTHENTICATION路径如下:
org.apache.hadoop.fs.CommonConfigurationKeysPublic#HADOOP_SECURITY_AUTHENTICATION

  public static AuthenticationMethod getAuthenticationMethod(Configuration conf) {
    String value = conf.get(HADOOP\_SECURITY\_AUTHENTICATION, "simple");
    try {
      return Enum.valueOf(AuthenticationMethod.class,
          StringUtils.toUpperCase(value));
    } catch (IllegalArgumentException iae) {
      throw new IllegalArgumentException("Invalid attribute value for " +
          HADOOP\_SECURITY\_AUTHENTICATION + " of " + value);
    }
  }

所以如果在环境变量中配置了HADOOP_HOME或者HADOOP_CONF_DIR对于UserGroupInformation是没有用的。
必须将core-site.xml放在类路径下,或者直接调用org.apache.hadoop.security.UserGroupInformation#setConfiguration设置加载过core-site.xml的conf对象。

3、UserGroupInformation认证
3.1 、apache原生的UserGroupInformation验证:

UserGroupInformation类中使用静态变量存放hadoop conf和已认证用户信息,所以只需要程序中认证一次,不同类不需要传递认证的user,只需要都到UserGroupInformation取即可。

    private static Configuration conf;
    private static UserGroupInformation loginUser = null;
    private static String keytabPrincipal = null;
    private static String keytabFile = null;

调用org.apache.hadoop.security.UserGroupInformation#loginUserFromKeytab传入principal和keytab就可以完成认证。

3.2、cloudera改良过的UserGroupInformation验证:

当然,可以调用原生的loginUserFromKeytab也可以。

改良内容就是通过配置环境变量的方法,隐性完成kerberos用户认证。无需UserGroupInformation认证,在调用getLoginUser可以自动完成认证。
具体过程如下:
org.apache.hadoop.security.UserGroupInformation#getLoginUser方法获取用户

  public static UserGroupInformation getLoginUser() throws IOException {
  ...
    if (loginUser == null) {
      UserGroupInformation newLoginUser = createLoginUser(null);
      ... 
      }
  }

实际是调用了doSubjectLogin(null, null)

  UserGroupInformation createLoginUser(Subject subject) throws IOException {


![img](https://img-blog.csdnimg.cn/img_convert/8b0df376c10a54f685e568c89ce74c6d.png)
![img](https://img-blog.csdnimg.cn/img_convert/32cc476761cc837d369118c1571c9662.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**



**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 20
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值