World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#1831] fix(server-common,hive): Kerberos users should support to append realm (#1832)
qqqttt1236748132cee1a
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.javaindex a4600dd625..c63b79e8f5 100644--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java@@ -97,6 +97,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasprivate HiveSchemaPropertiesMetadata schemaPropertiesMetadata;private ScheduledThreadPoolExecutor checkTgtExecutor;+ private String kerberosRealm;private ProxyPlugin proxyPlugin;// Map that maintains the mapping of keys in Gravitino to that in Hive, for example, users@@ -201,6 +202,10 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasString catalogPrincipal = (String) catalogPropertiesMetadata.getOrDefault(conf, PRINCIPAL);Preconditions.checkArgument(StringUtils.isNotBlank(catalogPrincipal), "The principal can't be blank");+ String[] principalComponents = catalogPrincipal.split("@");+ Preconditions.checkArgument(+ principalComponents.length == 2, "The principal has the wrong format");+ this.kerberosRealm = principalComponents[1];checkTgtExecutor =new ScheduledThreadPoolExecutor(@@ -1097,6 +1102,10 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasreturn new ThreadFactoryBuilder().setDaemon(true).setNameFormat(factoryName + "-%d").build();}+ public String getKerberosRealm() {+ return kerberosRealm;+ }+void setProxyPlugin(HiveProxyPlugin hiveProxyPlugin) {this.proxyPlugin = hiveProxyPlugin;}diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveProxyPlugin.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveProxyPlugin.javaindex 74f3c55844..91a59bee4f 100644--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveProxyPlugin.java+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveProxyPlugin.java@@ -7,7 +7,6 @@ package com.datastrato.gravitino.catalog.hive;import com.datastrato.gravitino.catalog.CatalogOperations;import com.datastrato.gravitino.catalog.ProxyPlugin;import com.datastrato.gravitino.utils.Executable;-import com.datastrato.gravitino.utils.PrincipalUtils;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.UndeclaredThrowableException;@@ -23,12 +22,12 @@ import org.apache.hadoop.security.token.Token;class HiveProxyPlugin implements ProxyPlugin {- private final UserGroupInformation currentUser;+ private final UserGroupInformation realUser;private HiveCatalogOperations ops;HiveProxyPlugin() {try {- currentUser = UserGroupInformation.getCurrentUser();+ realUser = UserGroupInformation.getCurrentUser();} catch (IOException ioe) {throw new IllegalStateException("Fail to init HiveCatalogProxyPlugin");}@@ -39,28 +38,38 @@ class HiveProxyPlugin implements ProxyPlugin {Principal principal, Executable<Object, Exception> action, Map<String, String> properties)throws Throwable {try {- UserGroupInformation proxyUser =- UserGroupInformation.createProxyUser(- PrincipalUtils.getCurrentPrincipal().getName(), currentUser);+ UserGroupInformation proxyUser;- if (UserGroupInformation.isSecurityEnabled()) {- if (ops != null) {+ if (UserGroupInformation.isSecurityEnabled() && ops != null) {- String token =- ops.getClientPool()- .run(- client -> {- return client.getDelegationToken(- currentUser.getUserName(),- PrincipalUtils.getCurrentPrincipal().getName());- });-- Token<DelegationTokenIdentifier> delegationToken = new Token<DelegationTokenIdentifier>();- delegationToken.decodeFromUrlString(token);- delegationToken.setService(- new Text(ops.getHiveConf().getVar(HiveConf.ConfVars.METASTORE_TOKEN_SIGNATURE)));- proxyUser.addToken(delegationToken);+ // The Gravitino server may use multiple KDC servers.+ // The http authentication use one KDC server, the Hive catalog may use another KDC server.+ // The KerberosAuthenticator will remove realm of principal.+ // And then we add the realm of Hive catalog to the user.+ String proxyKerberosPrincipalName = principal.getName();+ if (!proxyKerberosPrincipalName.contains("@")) {+ proxyKerberosPrincipalName =+ String.format("%s@%s", proxyKerberosPrincipalName, ops.getKerberosRealm());}++ proxyUser = UserGroupInformation.createProxyUser(proxyKerberosPrincipalName, realUser);++ String token =+ ops.getClientPool()+ .run(+ client -> {+ return client.getDelegationToken(realUser.getUserName(), principal.getName());+ });++ Token<DelegationTokenIdentifier> delegationToken = new Token<DelegationTokenIdentifier>();+ delegationToken.decodeFromUrlString(token);+ delegationToken.setService(+ new Text(ops.getHiveConf().getVar(HiveConf.ConfVars.METASTORE_TOKEN_SIGNATURE)));++ proxyUser.addToken(delegationToken);+ } else {++ proxyUser = UserGroupInformation.createProxyUser(principal.getName(), realUser);}return proxyUser.doAs((PrivilegedExceptionAction<Object>) action::execute);diff --git a/docs/security.md b/docs/security.mdindex 3d509c760a..b65b8f5d6d 100644--- a/docs/security.md+++ b/docs/security.md@@ -79,6 +79,7 @@ GravitinoClient client = GravitinoClient.builder(uri):::infoNow Iceberg REST service doesn't support Kerberos authentication.+The URI must use the hostname of server instead of IP.:::### Server configurationdiff --git a/server-common/src/main/java/com/datastrato/gravitino/server/auth/KerberosAuthenticator.java b/server-common/src/main/java/com/datastrato/gravitino/server/auth/KerberosAuthenticator.javaindex 00c07ec641..082172160b 100644--- a/server-common/src/main/java/com/datastrato/gravitino/server/auth/KerberosAuthenticator.java+++ b/server-common/src/main/java/com/datastrato/gravitino/server/auth/KerberosAuthenticator.java@@ -125,6 +125,7 @@ public class KerberosAuthenticator implements Authenticator {}});} catch (Exception e) {+ LOG.warn("Fail to validate the token, exception: ", e);throw new UnauthorizedException("Fail to validate the token", AuthConstants.NEGOTIATE);}}@@ -163,12 +164,7 @@ public class KerberosAuthenticator implements Authenticator {throw new UnauthorizedException("Principal has wrong format", AuthConstants.NEGOTIATE);}- String[] userAndInstance = principalComponents[0].split("/");- if (userAndInstance.length > 2) {- throw new UnauthorizedException("Principal has wrong format", AuthConstants.NEGOTIATE);- }-- String user = userAndInstance[0];+ String user = principalComponents[0];// TODO: We will have KerberosUserPrincipal in the future.// We can put more information of Kerberos to the KerberosUserPrincipal// For example, we can put the token into the KerberosUserPrincipal,diff --git a/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestKerberosAuthenticator.java b/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestKerberosAuthenticator.javaindex 1856552d74..de5d9252fb 100644--- a/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestKerberosAuthenticator.java+++ b/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestKerberosAuthenticator.java@@ -52,6 +52,15 @@ public class TestKerberosAuthenticator extends KerberosSecurityTestcase {});Assertions.assertTrue(e.getMessage().contains("The value can't be blank"));+ config.set(PRINCIPAL, "xx@xxx@");+ e =+ Assertions.assertThrows(+ IllegalArgumentException.class,+ () -> {+ kerberosAuthenticator.initialize(config);+ });+ Assertions.assertTrue(e.getMessage().contains("Principal must starts with"));+config.set(PRINCIPAL, KerberosTestUtils.getServerPrincipal());e =Assertions.assertThrows(