How to extract Kerberos TGT from UserGroupInformation

If you need to view lifetime of your Kerberos ticket in Hadoop’s UserGroupInformation or see Kerberos ticket expiration date in UserGroupInformation (and do other things), you need extract Kerberos TGT from UserGroupInformation.

This piece of code will be very useful for you:

public KerberosTicket extractTgtFromUGI(UserGroupInformation ugi) {
    Method method = null;
    try {
        method = ugi.getClass().getDeclaredMethod("getTGT");
        method.setAccessible(true);
        KerberosTicket tgt = (KerberosTicket) method.invoke(ugi);
        log.info("Kerberos ticket info. startTime={}, endTime={}, renewTill={}", tgt.getStartTime(), tgt.getEndTime(), tgt.getRenewTill());
        return tgt;
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
}

If you still have any questions, feel free to ask me in the comments under this article, or write me on promark33@gmail.com.

Leave a Reply

Your email address will not be published.