kerberosjava-securityspring-security-kerberoshive-metastore

How to fix Keberos Authentication prompt while running Java application


I wrote a Java application to connect with hive-metastore. It works fine but when I run the jar file on Linux it asks for Kerberos Username and password . I already specified the Kerberos principal and keytab file in my code. And I don't want to use additional jass.config file. Is there any way to resolve that problem?

Here is my source code

    HiveConf conf = new HiveConf();
    MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://HOSTNAME:9083");
    MetastoreConf.setBoolVar(conf, ConfVars.USE_THRIFT_SASL, true);
    MetastoreConf.setVar(conf, ConfVars.KERBEROS_PRINCIPAL, "hive/HOSTNAME@example.com");
    MetastoreConf.setVar(conf, ConfVars.KERBEROS_KEYTAB_FILE, "hive.keytab");
    System.setProperty("java.security.krb5.realm", "EXAMPLE.COM");
    System.setProperty("java.security.krb5.kdc", "HOSTNAME");
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    HiveMetaStoreClient client = new HiveMetaStoreClient(conf);
    client.close();     

Expected Result- It should verify the connection successfully

Actual result-

java -jar Application

Kerberos username [root]: Kerberos password [root]:

I want to bypass this kerberos terminal prompt authentication. Is there any way to do that?


Solution

  •           final String user = "hive/HOSTNAME@EXAMPLE.COM";
              final String keyPath = "hive.keytab";
              Configuration conf = new Configuration();
              System.out.println("In case of kerberos authentication");
              conf.set("hadoop.security.authentication", "kerberos");
              System.setProperty("java.security.krb5.kdc", "HOSTNAME");
              System.setProperty("java.security.krb5.realm", "EXAMPLE.COM");
    
              UserGroupInformation.setConfiguration(conf);
              UserGroupInformation.loginUserFromKeytab(user, keyPath);
    
              HiveConf conf1 = new HiveConf(); MetastoreConf.setVar(conf1,
              ConfVars.THRIFT_URIS, "thrift://HOSTNAME:9083"); HiveMetaStoreClient
              client = new HiveMetaStoreClient(conf1);
              client.close();
    

    It works fine now