use of org.apache.kafka.clients.admin.CreateDelegationTokenResult in project hive by apache.
the class DagUtils method getKafkaDelegationTokenForBrokers.
private void getKafkaDelegationTokenForBrokers(DAG dag, JobConf conf, String kafkaBrokers) {
LOG.info("Getting kafka credentials for brokers: {}", kafkaBrokers);
String keytab = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_KEYTAB);
String principal = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_PRINCIPAL);
try {
principal = SecurityUtil.getServerPrincipal(principal, "0.0.0.0");
} catch (IOException e) {
throw new RuntimeException(e);
}
Properties config = new Properties();
config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBrokers);
config.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
String jaasConfig = String.format("%s %s %s %s serviceName=\"%s\" keyTab=\"%s\" principal=\"%s\";", "com.sun.security.auth.module.Krb5LoginModule required", "debug=true", "useKeyTab=true", "storeKey=true", "kafka", keytab, principal);
config.put(SaslConfigs.SASL_JAAS_CONFIG, jaasConfig);
LOG.debug("Jaas config for requesting kafka credentials: {}", jaasConfig);
AdminClient admin = AdminClient.create(config);
CreateDelegationTokenOptions createDelegationTokenOptions = new CreateDelegationTokenOptions();
CreateDelegationTokenResult createResult = admin.createDelegationToken(createDelegationTokenOptions);
DelegationToken token;
try {
token = createResult.delegationToken().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Exception while getting kafka delegation tokens", e);
}
LOG.info("Got kafka delegation token: {}", token);
dag.getCredentials().addToken(KAFKA_DELEGATION_TOKEN_KEY, new Token<>(token.tokenInfo().tokenId().getBytes(), token.hmac(), null, new Text("kafka")));
}
Aggregations