Skip to content

Commit

Permalink
[Fix-7277][datasource] Support Kerberos auto renewal (#7277) (#7278)
Browse files Browse the repository at this point in the history
  • Loading branch information
Narcasserun authored Dec 9, 2021
1 parent 2a7d6b4 commit 8d68cf4
Showing 1 changed file with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,46 @@
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.zaxxer.hikari.HikariDataSource;

import sun.security.krb5.Config;

public class HiveDataSourceClient extends CommonDataSourceClient {

private static final Logger logger = LoggerFactory.getLogger(HiveDataSourceClient.class);

private ScheduledExecutorService kerberosRenewalService;

private Configuration hadoopConf;
protected HikariDataSource oneSessionDataSource;
private UserGroupInformation ugi;

public HiveDataSourceClient(BaseConnectionParam baseConnectionParam) {
super(baseConnectionParam);
}

@Override
protected void preInit() {
logger.info("PreInit in {}", getClass().getName());
this.kerberosRenewalService = Executors.newSingleThreadScheduledExecutor();
}

@Override
protected void initClient(BaseConnectionParam baseConnectionParam) {
logger.info("Create Configuration for hive configuration.");
this.hadoopConf = createHadoopConf();
logger.info("Create Configuration success.");

logger.info("Create UserGroupInformation.");
this.ugi = createUserGroupInformation(baseConnectionParam.getUser());
logger.info("Create ugi success.");
Expand All @@ -73,22 +91,54 @@ private void checkKerberosEnv() {
String krb5File = PropertyUtils.getString(JAVA_SECURITY_KRB5_CONF_PATH);
if (StringUtils.isNotBlank(krb5File)) {
System.setProperty(JAVA_SECURITY_KRB5_CONF, krb5File);
try {
Config.refresh();
Class<?> kerberosName = Class.forName("org.apache.hadoop.security.authentication.util.KerberosName");
Field field = kerberosName.getDeclaredField("defaultRealm");
field.setAccessible(true);
field.set(null, Config.getInstance().getDefaultRealm());
} catch (Exception e) {
throw new RuntimeException("Update Kerberos environment failed.", e);
}
}
}

private UserGroupInformation createUserGroupInformation(String username) {
String krb5File = PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH);
String keytab = PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH);
String principal = PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME);

try {
return CommonUtil.createUGI(getHadoopConf(), principal, keytab, krb5File, username);
UserGroupInformation ugi = CommonUtil.createUGI(getHadoopConf(), principal, keytab, krb5File, username);
try {
Field isKeytabField = ugi.getClass().getDeclaredField("isKeytab");
isKeytabField.setAccessible(true);
isKeytabField.set(ugi, true);
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.warn(e.getMessage());
}

kerberosRenewalService.scheduleWithFixedDelay(() -> {
try {
ugi.checkTGTAndReloginFromKeytab();
} catch (IOException e) {
logger.error("Check TGT and Renewal from Keytab error", e);
}
}, 5, 5, TimeUnit.MINUTES);
return ugi;
} catch (IOException e) {
throw new RuntimeException("createUserGroupInformation fail. ", e);
}
}

protected Configuration createHadoopConf() {
Configuration hadoopConf = new Configuration();
hadoopConf.setBoolean("ipc.client.fallback-to-simple-auth-allowed", true);
return hadoopConf;
}

protected Configuration getHadoopConf() {
return new Configuration();
return this.hadoopConf;
}

@Override
Expand All @@ -104,7 +154,10 @@ public Connection getConnection() {
@Override
public void close() {
super.close();

logger.info("close HiveDataSourceClient.");
kerberosRenewalService.shutdown();
this.ugi = null;

this.oneSessionDataSource.close();
this.oneSessionDataSource = null;
Expand Down

0 comments on commit 8d68cf4

Please sign in to comment.