public static UserGroupInformation loginAndReturnUGI(Configuration conf, String username)
    throws IOException {
    String hostname = InetAddress.getLocalHost().getHostName();
    String keyTabFileConfKey = "hbase." + username + ".keytab.file";
    String keyTabFileLocation = conf.get(keyTabFileConfKey);
    String principalConfKey = "hbase." + username + ".kerberos.principal";
    String principal = org.apache.hadoop.security.SecurityUtil
      .getServerPrincipal(conf.get(principalConfKey), hostname);
    if (keyTabFileLocation == null || principal == null) {
      LOG.warn(
        "Principal or key tab file null for : " + principalConfKey + ", " + keyTabFileConfKey);
    }
    UserGroupInformation ugi =
      UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keyTabFileLocation);
    return ugi;
  }
Logs in a user using Kerberos keytab and returns the UserGroupInformation (UGI) instance.
@param conf     the configuration object
@param username the username for which the keytab file and principal are configured.
@return the UserGroupInformation instance for the logged-in user.
@throws IOException If an I/O error occurs during login.
hbase-diagnostics/src/main/java/org/apache/hadoop/hbase/util/KerberosUtils.java