public static void loginForMasterService(CConfiguration cConf) throws IOException, LoginException {
String principal = getMasterPrincipal(cConf);
String keytabPath = getMasterKeytabURI(cConf);
if (UserGroupInformation.isSecurityEnabled()) {
Path keytabFile = Paths.get(keytabPath);
Preconditions.checkArgument(Files.isReadable(keytabFile),
"Keytab file is not a readable file: %s", keytabFile);
String expandedPrincipal = expandPrincipal(principal);
LOG.info("Logging in as: principal={}, keytab={}", principal, keytabPath);
UserGroupInformation.loginUserFromKeytab(expandedPrincipal, keytabPath);
long delaySec = cConf.getLong(Constants.Security.KERBEROS_KEYTAB_RELOGIN_INTERVAL);
Executors.newSingleThreadScheduledExecutor(Threads.createDaemonThreadFactory("Kerberos keytab renewal"))
.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
UserGroupInformation.getLoginUser().checkTGTAndReloginFromKeytab();
} catch (IOException e) {
LOG.error("Failed to relogin from keytab", e);
}
}
}, delaySec, delaySec, TimeUnit.SECONDS);
}
}