World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#12032] fix(iceberg): Avoid leaking Kerberos client in KerberosAwareIcebergCatalogProxy (#14812)
Jerry Shao3433743d03f7
diff --git a/LICENSE.bin b/LICENSE.binindex 899b0e1dbd..f304880f09 100644--- a/LICENSE.bin+++ b/LICENSE.bin@@ -354,6 +354,7 @@JBoss LoggingRoaringBitmapcglib+ ObjenesisjmxutilsSQLite JDBCSnakeYAMLdiff --git a/NOTICE.bin b/NOTICE.binindex dedddb7fc6..75e31ff936 100644--- a/NOTICE.bin+++ b/NOTICE.bin@@ -36,6 +36,9 @@ Copyright 2017-2026 The Apache Software FoundationApache KafkaCopyright 2011-2023 The Apache Software Foundation+Objenesis+Copyright 2006-2022 Joe Walnes, Henri Tremblay, Leonardo Mesquita+Apache ArrowCopyright 2016-2024 The Apache Software Foundationdiff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.javaindex 1ec59bceaa..2bf7d4aa8a 100644--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java@@ -119,7 +119,7 @@ final class IcebergBackend implements AutoCloseable {this.wrapper =authenticationConfig.isKerberosAuth()&& rawWrapper.getCatalog() instanceof SupportsKerberos- ? new KerberosAwareIcebergCatalogProxy(rawWrapper).getProxy(icebergConfig)+ ? new KerberosAwareIcebergCatalogProxy(rawWrapper).getProxy(): rawWrapper;this.helper = new IcebergCatalogWrapperHelper(wrapper.getCatalog());this.views = new IcebergViewCatalogOperations(wrapper);diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 2d31540213..d129d189db 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -152,6 +152,7 @@ paimon4flink22 = "1.4.1"# cglib upgraded from 2. to 3.3.0 for Java 17 compatibility.# Explicit ASM dependency (9.8) is required due to cglib's internal use and Java 17 module system.cglib = "3.3.0"+objenesis = "3.3"asm = "9.8"ranger = "2.8.0"zookeeper = "3.8.8"@@ -371,6 +372,7 @@ kafka-clients = { group = "org.apache.kafka", name = "kafka-clients", version.rekafka = { group = "org.apache.kafka", name = "kafka_2.12", version.ref = "kafka" }curator-test = { group = "org.apache.curator", name = "curator-test", version.ref = "curator"}cglib = { group = "cglib", name = "cglib", version.ref = "cglib"}+objenesis = { group = "org.objenesis", name = "objenesis", version.ref = "objenesis" }asm = { group = "org.ow2.asm", name = "asm", version.ref = "asm"}woodstox-core = { group = "com.fasterxml.woodstox", name = "woodstox-core", version.ref = "woodstox-core"}thrift = { group = "org.apache.thrift", name = "libthrift", version.ref = "thrift"}diff --git a/iceberg/iceberg-common/build.gradle.kts b/iceberg/iceberg-common/build.gradle.ktsindex 399d7dfc66..95d7fb8fd5 100644--- a/iceberg/iceberg-common/build.gradle.kts+++ b/iceberg/iceberg-common/build.gradle.kts@@ -59,6 +59,8 @@ dependencies {exclude("org.ow2.asm")}implementation(libs.asm)+ // Create cglib proxies without running the wrapped class constructors+ implementation(libs.objenesis)implementation(libs.commons.lang3)implementation(libs.guava)implementation(libs.iceberg.aliyun)diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/KerberosAwareIcebergCatalogProxy.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/KerberosAwareIcebergCatalogProxy.javaindex c749c1af28..9cbe9a5418 100644--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/KerberosAwareIcebergCatalogProxy.java+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/KerberosAwareIcebergCatalogProxy.java@@ -20,18 +20,23 @@package org.apache.gravitino.iceberg.common.ops;import java.lang.reflect.Method;+import net.sf.cglib.proxy.Callback;import net.sf.cglib.proxy.Enhancer;+import net.sf.cglib.proxy.Factory;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;-import org.apache.gravitino.iceberg.common.IcebergConfig;import org.apache.gravitino.iceberg.common.authentication.SupportsKerberos;import org.apache.iceberg.catalog.Catalog;+import org.objenesis.Objenesis;+import org.objenesis.ObjenesisStd;/*** A proxy class for IcebergCatalogWrapper to handle Kerberos authentication or other cross-cutting* concerns.*/public class KerberosAwareIcebergCatalogProxy implements MethodInterceptor {+ private static final Objenesis OBJENESIS = new ObjenesisStd();+private final IcebergCatalogWrapper target;private final Catalog catalog;@@ -51,31 +56,24 @@ public class KerberosAwareIcebergCatalogProxy implements MethodInterceptor {return method.invoke(target, objects);}- public IcebergCatalogWrapper getProxy(IcebergConfig config) {- Enhancer e = new Enhancer();- e.setClassLoader(target.getClass().getClassLoader());- e.setSuperclass(target.getClass());- e.setCallback(this);-- Class<?>[] argClass = new Class[] {IcebergConfig.class};- return (IcebergCatalogWrapper) e.create(argClass, new Object[] {config});- }-/**- * Create a proxy instance with catalogName and config constructor. It's used for class- * CatalogWrapperForREST or its subclass.+ * Create a proxy instance that delegates every call to the wrapped target. The proxy instance is+ * allocated without running any constructor of the wrapper class, so it does not own a duplicate+ * set of wrapper resources (for example a second credential manager, scan plan cache or, on+ * versions that load the catalog eagerly in the constructor, a second catalog and Kerberos+ * client). Closing the proxy closes the target exactly once.*- * @param catalogName Name of the catalog.- * @param config Iceberg configuration.* @return The proxy instance.*/- public IcebergCatalogWrapper getProxy(String catalogName, IcebergConfig config) {+ public IcebergCatalogWrapper getProxy() {+ Class<?> targetClass = target.getClass();Enhancer e = new Enhancer();- e.setClassLoader(target.getClass().getClassLoader());- e.setSuperclass(target.getClass());- e.setCallback(this);-- Class<?>[] argClass = new Class[] {String.class, IcebergConfig.class};- return (IcebergCatalogWrapper) e.create(argClass, new Object[] {catalogName, config});+ e.setClassLoader(targetClass.getClassLoader());+ e.setSuperclass(targetClass);+ e.setCallbackType(KerberosAwareIcebergCatalogProxy.class);+ Class<?> proxyClass = e.createClass();+ Factory proxy = (Factory) OBJENESIS.newInstance(proxyClass);+ proxy.setCallbacks(new Callback[] {this});+ return (IcebergCatalogWrapper) proxy;}}diff --git a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.javanew file mode 100644index 0000000000..1f05b0d7bd--- /dev/null+++ b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java@@ -0,0 +1,129 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package org.apache.gravitino.iceberg.common.ops;++import java.util.HashMap;+import java.util.Map;+import java.util.concurrent.atomic.AtomicInteger;+import org.apache.gravitino.iceberg.common.IcebergConfig;+import org.apache.gravitino.iceberg.common.authentication.SupportsKerberos;+import org.apache.iceberg.catalog.Catalog;+import org.apache.iceberg.inmemory.InMemoryCatalog;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++public class TestKerberosAwareIcebergCatalogProxy {++ private static final AtomicInteger WRAPPER_CONSTRUCTIONS = new AtomicInteger();+ private static final AtomicInteger CATALOG_CONSTRUCTIONS = new AtomicInteger();+ private static final AtomicInteger CATALOG_CLOSES = new AtomicInteger();+ private static final AtomicInteger KERBEROS_OPERATIONS = new AtomicInteger();++ /** A catalog that counts how often it is created, closed and used under Kerberos. */+ public static class CountingKerberosCatalog extends InMemoryCatalog implements SupportsKerberos {+ public CountingKerberosCatalog() {+ CATALOG_CONSTRUCTIONS.incrementAndGet();+ }++ @Override+ public <R> R doKerberosOperations(Executable<R> executable) throws Throwable {+ KERBEROS_OPERATIONS.incrementAndGet();+ return executable.execute();+ }++ @Override+ public void close() {+ CATALOG_CLOSES.incrementAndGet();+ }+ }++ /** A wrapper that creates one {@link CountingKerberosCatalog} per constructed instance. */+ public static class CountingWrapper extends IcebergCatalogWrapper {+ private Catalog countingCatalog;++ public CountingWrapper(IcebergConfig config) {+ super(config);+ WRAPPER_CONSTRUCTIONS.incrementAndGet();+ }++ @Override+ public synchronized Catalog getCatalog() {+ // Load lazily like the real wrapper, so the catalog is only created on first access.+ if (countingCatalog == null) {+ countingCatalog = new CountingKerberosCatalog();+ }+ return countingCatalog;+ }++ @Override+ public void close() throws Exception {+ ((AutoCloseable) getCatalog()).close();+ }+ }++ @BeforeEach+ public void resetCounters() {+ WRAPPER_CONSTRUCTIONS.set(0);+ CATALOG_CONSTRUCTIONS.set(0);+ CATALOG_CLOSES.set(0);+ KERBEROS_OPERATIONS.set(0);+ }++ private static IcebergConfig memoryConfig() {+ Map<String, String> conf = new HashMap<>();+ conf.put(IcebergConfig.CATALOG_BACKEND.getKey(), "memory");+ return new IcebergConfig(conf);+ }++ @Test+ public void testProxyDoesNotRunWrapperConstructor() {+ CountingWrapper target = new CountingWrapper(memoryConfig());+ Assertions.assertEquals(1, WRAPPER_CONSTRUCTIONS.get());+ Assertions.assertEquals(0, CATALOG_CONSTRUCTIONS.get());++ IcebergCatalogWrapper proxy = new KerberosAwareIcebergCatalogProxy(target).getProxy();++ Assertions.assertNotSame(target, proxy);+ Assertions.assertInstanceOf(CountingWrapper.class, proxy);+ Assertions.assertEquals(1, WRAPPER_CONSTRUCTIONS.get());+ Assertions.assertEquals(1, CATALOG_CONSTRUCTIONS.get());+ }++ @Test+ public void testProxyDelegatesToTargetCatalogUnderKerberos() {+ CountingWrapper target = new CountingWrapper(memoryConfig());+ IcebergCatalogWrapper proxy = new KerberosAwareIcebergCatalogProxy(target).getProxy();++ Assertions.assertSame(target.getCatalog(), proxy.getCatalog());+ Assertions.assertTrue(KERBEROS_OPERATIONS.get() > 0);+ }++ @Test+ public void testCloseProxyClosesTargetCatalogOnce() throws Exception {+ CountingWrapper target = new CountingWrapper(memoryConfig());+ IcebergCatalogWrapper proxy = new KerberosAwareIcebergCatalogProxy(target).getProxy();++ proxy.close();++ Assertions.assertEquals(1, CATALOG_CLOSES.get());+ Assertions.assertEquals(1, CATALOG_CONSTRUCTIONS.get());+ }+}diff --git a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCatalogWrapperManager.java b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCatalogWrapperManager.javaindex 0bc3af66b4..f4438a9fa7 100644--- a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCatalogWrapperManager.java+++ b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCatalogWrapperManager.java@@ -174,8 +174,7 @@ public class IcebergCatalogWrapperManager implements AutoCloseable {AuthenticationConfig authenticationConfig =new AuthenticationConfig(enrichedConfig.getAllConfig());if (authenticationConfig.isKerberosAuth() && rest.getCatalog() instanceof SupportsKerberos) {- return (CatalogWrapperForREST)- new KerberosAwareIcebergCatalogProxy(rest).getProxy(catalogName, enrichedConfig);+ return (CatalogWrapperForREST) new KerberosAwareIcebergCatalogProxy(rest).getProxy();}return rest;diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestKerberosAwareCatalogWrapperForREST.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestKerberosAwareCatalogWrapperForREST.javanew file mode 100644index 0000000000..ad8e932412--- /dev/null+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestKerberosAwareCatalogWrapperForREST.java@@ -0,0 +1,83 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package org.apache.gravitino.iceberg.service;++import com.google.common.collect.ImmutableMap;+import java.util.concurrent.atomic.AtomicInteger;+import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;+import org.apache.gravitino.iceberg.common.IcebergConfig;+import org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper;+import org.apache.gravitino.iceberg.common.ops.KerberosAwareIcebergCatalogProxy;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++/**+ * Verifies that proxying a {@link CatalogWrapperForREST} does not run its constructor again, which+ * would create a second {@code CatalogCredentialManager}, scan plan cache and Hadoop configuration+ * that are never closed.+ */+public class TestKerberosAwareCatalogWrapperForREST {++ private static final AtomicInteger CONSTRUCTIONS = new AtomicInteger();+ private static final AtomicInteger CLOSES = new AtomicInteger();++ /** A wrapper that counts constructor runs and close calls. */+ public static class CountingRESTWrapper extends CatalogWrapperForREST {+ public CountingRESTWrapper(String catalogName, IcebergConfig config) {+ super(catalogName, config);+ CONSTRUCTIONS.incrementAndGet();+ }++ @Override+ public void close() throws Exception {+ CLOSES.incrementAndGet();+ super.close();+ }+ }++ @BeforeEach+ public void resetCounters() {+ CONSTRUCTIONS.set(0);+ CLOSES.set(0);+ }++ private static IcebergConfig memoryConfig() {+ return new IcebergConfig(ImmutableMap.of(IcebergConstants.CATALOG_BACKEND, "memory"));+ }++ @Test+ public void testProxyDoesNotRunWrapperConstructor() throws Exception {+ CountingRESTWrapper target = new CountingRESTWrapper("proxied", memoryConfig());+ IcebergCatalogWrapper proxy = new KerberosAwareIcebergCatalogProxy(target).getProxy();++ Assertions.assertNotSame(target, proxy);+ Assertions.assertInstanceOf(CountingRESTWrapper.class, proxy);+ Assertions.assertEquals(1, CONSTRUCTIONS.get());+ // The proxy must not own its own credential manager and scan plan cache.+ Assertions.assertNull(((CatalogWrapperForREST) proxy).catalogCredentialManager);++ Assertions.assertSame(target.getCatalog(), proxy.getCatalog());+ Assertions.assertTrue(((CatalogWrapperForREST) proxy).supportsAsyncPurge());++ proxy.close();+ Assertions.assertEquals(1, CLOSES.get());+ }+}