diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java index d3c77a77f5d..d6ebfbb01a5 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java @@ -52,7 +52,9 @@ public class JdbcConnectionParams { public static final String AUTH_KYUUBI_CLIENT_TICKET_CACHE = "kyuubiClientTicketCache"; public static final String AUTH_PASSWD = "password"; public static final String AUTH_KERBEROS_AUTH_TYPE = "kerberosAuthType"; + public static final String AUTH_KERBEROS_AUTH_TYPE_FROM_KEYTAB = "fromKeytab"; public static final String AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT = "fromSubject"; + public static final String AUTH_KERBEROS_AUTH_TYPE_FROM_TICKET_CACHE = "fromTicketCache"; public static final String ANONYMOUS_USER = "anonymous"; public static final String ANONYMOUS_PASSWD = "anonymous"; public static final String USE_SSL = "ssl"; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java index 077def43b9c..e186f9aa8e8 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java @@ -843,28 +843,68 @@ private boolean isHadoopUserGroupInformationDoAs() { } } + private boolean isForciblyFromKeytabAuthMode() { + return AUTH_KERBEROS_AUTH_TYPE_FROM_KEYTAB.equalsIgnoreCase( + sessConfMap.get(AUTH_KERBEROS_AUTH_TYPE)); + } + + private boolean isForciblyFromSubjectAuthMode() { + return AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT.equalsIgnoreCase( + sessConfMap.get(AUTH_KERBEROS_AUTH_TYPE)); + } + + private boolean isForciblyTgtCacheAuthMode() { + return AUTH_KERBEROS_AUTH_TYPE_FROM_TICKET_CACHE.equalsIgnoreCase( + sessConfMap.get(AUTH_KERBEROS_AUTH_TYPE)); + } + private boolean isKeytabAuthMode() { - return isSaslAuthMode() - && hasSessionValue(AUTH_PRINCIPAL) + // handle explicit cases first + if (isForciblyFromSubjectAuthMode() || isForciblyTgtCacheAuthMode()) { + return false; + } + if (isKerberosAuthMode() && isForciblyFromKeytabAuthMode()) { + return true; + } + if (isKerberosAuthMode() + && hasSessionValue(AUTH_KYUUBI_CLIENT_KEYTAB) + && !hasSessionValue(AUTH_KYUUBI_CLIENT_PRINCIPAL)) { + throw new IllegalArgumentException( + AUTH_KYUUBI_CLIENT_KEYTAB + + " is set but " + + AUTH_KYUUBI_CLIENT_PRINCIPAL + + " is not set"); + } + // handle implicit cases then + return isKerberosAuthMode() && hasSessionValue(AUTH_KYUUBI_CLIENT_PRINCIPAL) && hasSessionValue(AUTH_KYUUBI_CLIENT_KEYTAB); } private boolean isFromSubjectAuthMode() { - return isSaslAuthMode() - && hasSessionValue(AUTH_PRINCIPAL) - && !hasSessionValue(AUTH_KYUUBI_CLIENT_PRINCIPAL) + // handle explicit cases first + if (isForciblyFromKeytabAuthMode() || isForciblyTgtCacheAuthMode()) { + return false; + } + if (isKerberosAuthMode() && isForciblyFromSubjectAuthMode()) { + return true; + } + // handle implicit cases then + return isKerberosAuthMode() && !hasSessionValue(AUTH_KYUUBI_CLIENT_KEYTAB) - && (AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT.equalsIgnoreCase( - sessConfMap.get(AUTH_KERBEROS_AUTH_TYPE)) - || isHadoopUserGroupInformationDoAs()); + && isHadoopUserGroupInformationDoAs(); } private boolean isTgtCacheAuthMode() { - return isSaslAuthMode() - && hasSessionValue(AUTH_PRINCIPAL) - && !hasSessionValue(AUTH_KYUUBI_CLIENT_PRINCIPAL) - && !hasSessionValue(AUTH_KYUUBI_CLIENT_KEYTAB); + // handle explicit cases first + if (isForciblyFromKeytabAuthMode() || isForciblyFromSubjectAuthMode()) { + return false; + } + if (isKerberosAuthMode() && isForciblyTgtCacheAuthMode()) { + return true; + } + // handle implicit cases then + return isKerberosAuthMode() && !hasSessionValue(AUTH_KYUUBI_CLIENT_KEYTAB); } private boolean isPlainSaslAuthMode() { diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthentication.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthentication.java index a137fbb9946..77456943755 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthentication.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthentication.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.net.InetAddress; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; @@ -106,6 +107,9 @@ private static Configuration createLoginFromTgtCacheConfiguration(String ticketC if (StringUtils.isBlank(ticketCache)) { ticketCache = System.getenv("KRB5CCNAME"); } + if (!Files.exists(Paths.get(ticketCache))) { + LOG.warn("TicketCache {} does not exist", ticketCache); + } if (StringUtils.isNotBlank(ticketCache)) { optionsBuilder.put("ticketCache", ticketCache); }