Kerberos协议及Habase相关参数

2020-04-06  本文已影响0人  todd5167

Kerberos

Kerberos是一种计算机网络认证协议,它允许某实体在非安全网络环境下通信,向另一个实体以一种安全的方式证明自己的身份,协议基于对称密码学,并需要一个值得信赖的第三方(KDC)。
它主要包含:认证服务器(AS)和票据授权服务器(TGS)组成的密钥分发中心(KDC),以及提供特定服务的SS。相关概念描述:

KDC持有一个密钥数据库;每个网络实体——无论是客户还是服务器——共享一套只有他自己和KDC知道的密钥。密钥的内容用于证明实体的身份。对于两个实体间的通信,KDC产生一个会话密钥,用来加密他们之间的交互信息。

Kerberos认证流程

image.pngimage.png
  1. 客户端认证
  1. 服务授权
  1. 服务请求

kdc集群:

安装包

相关文件:

[logging]
 default = FILE:/var/log/krb5libs.log
 kdc = FILE:/var/log/krb5kdc.log
 admin_server = FILE:/var/log/kadmind.log

[libdefaults]
 dns_lookup_realm = false
 ticket_lifetime = 24h
 renew_lifetime = 7d
 forwardable = true
 rdns = false
 pkinit_anchors = /etc/pki/tls/certs/ca-bundle.crt
 default_realm = EXAMPLE.COM
 default_ccache_name = KEYRING:persistent:%{uid}

[realms]
 EXAMPLE.COM = {
  kdc = k8s01:88
  admin_server = k8s01
 }

[domain_realm]
 .example.com = EXAMPLE.COM
  example.com = EXAMPLE.COM
[kdcdefaults]
 kdc_ports = 88
 kdc_tcp_ports = 88

[realms]
 EXAMPLE.COM = {
  #master_key_type = aes256-cts
  acl_file = /var/kerberos/krb5kdc/kadm5.acl
  dict_file = /usr/share/dict/words
  admin_keytab = /var/kerberos/krb5kdc/kadm5.keytab
  supported_enctypes = aes256-cts:normal aes128-cts:normal des3-hmac-sha1:normal arcfour-hmac:normal camellia256-cts:normal camellia128-cts:normal des-hmac-sha1:normal des-cbc-md5:normal des-cbc-crc:normal
 }
其中前一个*号是通配符,表示像名为“abc/admin”或“xxx/admin”的人都可以使用此工具(远程或本地)
管理kerberos数据库,后一个*跟权限有关,*表示所有权限

*/admin@EXAMPLE.COM *

术语

相关指令

创建完KDC数据库以及管理员后,从server端操作KDC.

Hbase认证参数

同步模式

必填参数:

选填参数:

FlinkStreamsql使用注意事项:

认证部分代码:

conf = HBaseConfiguration.create();     
conf.set("hbase.zookeeper.quorum", host);
// 注意没有hbase前缀,否则使用默认/hbase路径
conf.set("zookeeper.znode.parent", zkParent);

fillSyncKerberosConfig(conf, regionserverPrincipal, zookeeperSaslClient, securityKrb5Conf);

clientKeytabFile = System.getProperty("user.dir") + File.separator + clientKeytabFile;
clientPrincipal = !StringUtils.isEmpty(clientPrincipal) ? clientPrincipal : regionserverPrincipal;

// 使用客户端clientKeytabFile,clientPrincipal
UserGroupInformation userGroupInformation = HbaseConfigUtils.loginAndReturnUGI(conf, clientPrincipal, clientKeytabFile);
org.apache.hadoop.conf.Configuration finalConf = conf;
conn = userGroupInformation.doAs(new PrivilegedAction<Connection>() {
    @Override
    public Connection run() {
        try {
            // 服务端授权时,取出hbase/host@TDH中的  服务ID和host构建SASLClient
            // RpcClientImpl#Connection
            // HBaseSaslRpcClient
            return ConnectionFactory.createConnection(finalConf);
        } catch (IOException e) {
            LOG.error("Get connection fail with config:{}", finalConf);
            throw new RuntimeException(e);
        }
    }
});

private void fillSyncKerberosConfig(org.apache.hadoop.conf.Configuration config, String regionserverPrincipal,
                                    String zookeeperSaslClient, String securityKrb5Conf) throws IOException {

    config.set("hbase.master.kerberos.principal", regionserverPrincipal);
    config.set("hbase.regionserver.kerberos.principal", regionserverPrincipal);
    config.set("hbase.security.authorization", "true");
    config.set("hbase.security.authentication", "kerberos");

    if (!StringUtils.isEmpty(zookeeperSaslClient)) {
        System.setProperty("zookeeper.sasl.client", zookeeperSaslClient);
    }

    if (!StringUtils.isEmpty(securityKrb5Conf)) {
        // 默认已经上传到container
        String krb5ConfPath = System.getProperty("user.dir") + File.separator + securityKrb5Conf;
        System.setProperty("java.security.krb5.conf", krb5ConfPath);
    }
}

异步模式

必填参数

jaas.conf文件格式样例:
Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
useTicketCache=false
keyTab="/path/to/keytab.keytab" ## 客户端认证参数
principal="myhbaseuser@MY.HADOOP.DOMAIN"; ## 客户端认证参数
};

选填参数:

flinkStreamsql使用注意事项:

认证部分代码:

Config config = new Config();
config.overrideConfig("hbase.zookeeper.quorum", hbaseSideTableInfo.getHost());
config.overrideConfig("hbase.zookeeper.znode.parent", hbaseSideTableInfo.getParent());
fillAsyncKerberosConfig(config, hbaseSideTableInfo);
hBaseClient = new HBaseClient(config, executorService);

private void fillAsyncKerberosConfig(Config config, HbaseSideTableInfo hbaseSideTableInfo) throws IOException {
    AuthUtil.JAASConfig jaasConfig = HbaseConfigUtils.buildJaasConfig(hbaseSideTableInfo);

    String jaasFilePath = AuthUtil.creatJaasFile("JAAS", ".conf", jaasConfig);
    config.overrideConfig("java.security.auth.login.config", jaasFilePath);
    config.overrideConfig("hbase.security.auth.enable", "true");
    config.overrideConfig("hbase.sasl.clientconfig", "Client");
    config.overrideConfig("hbase.security.authentication" "kerberos");

    String regionserverPrincipal = hbaseSideTableInfo.getRegionserverPrincipal();
    config.overrideConfig("hbase.kerberos.regionserver.principal", regionserverPrincipal);

    if (!StringUtils.isEmpty(hbaseSideTableInfo.getZookeeperSaslClient())) {
        System.setProperty("zookeeper.sasl.client", hbaseSideTableInfo.getZookeeperSaslClient());
    }

    if (!StringUtils.isEmpty(hbaseSideTableInfo.getSecurityKrb5Conf())) {
        String krb5ConfPath = System.getProperty("user.dir") + File.separator + hbaseSideTableInfo.getSecurityKrb5Conf();
        LOG.info("krb5ConfPath:{}", krb5ConfPath);
        System.setProperty("java.security.krb5.conf", krb5ConfPath);
    }
}

相关问题记录

  1. 从IDEA提交到Kerberos集群异常。需要从当前环境变量配置krb5.conf路径。
image.pngimage.png
  1. /hbase节点找不到,同步模式下zookeeper.znode.parent默认路径为Hbase,确认参数没有hbase.前缀.
image.pngimage.png
  1. 服务授权阶段hbase.master.kerberos.principal使用的principal和client的principal为一个,且格式不正确,必须为xxxx/_HOST@XXX格式。
image.pngimage.png
上一篇 下一篇

猜你喜欢

热点阅读