diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/ssl/impl/SSLConfigCertProvider.java b/dubbo-common/src/main/java/org/apache/dubbo/common/ssl/impl/SSLConfigCertProvider.java index 23923c45ed3b..39924845bf5a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/ssl/impl/SSLConfigCertProvider.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/ssl/impl/SSLConfigCertProvider.java @@ -28,6 +28,7 @@ import org.apache.dubbo.common.utils.IOUtils; import java.io.IOException; +import java.io.InputStream; import java.util.Objects; @Activate(order = Integer.MAX_VALUE - 10000) @@ -53,10 +54,10 @@ public ProviderCert getProviderConnectionConfig(URL localAddress) { .map(sslConfig -> { try { return new ProviderCert( - IOUtils.toByteArray(sslConfig.getServerKeyCertChainPathStream()), - IOUtils.toByteArray(sslConfig.getServerPrivateKeyPathStream()), + readAll(sslConfig.getServerKeyCertChainPathStream()), + readAll(sslConfig.getServerPrivateKeyPathStream()), sslConfig.getServerTrustCertCollectionPath() != null - ? IOUtils.toByteArray(sslConfig.getServerTrustCertCollectionPathStream()) + ? readAll(sslConfig.getServerTrustCertCollectionPathStream()) : null, sslConfig.getServerKeyPassword(), AuthPolicy.CLIENT_AUTH); @@ -84,10 +85,10 @@ public Cert getConsumerConnectionConfig(URL remoteAddress) { .map(sslConfig -> { try { return new Cert( - IOUtils.toByteArray(sslConfig.getClientKeyCertChainPathStream()), - IOUtils.toByteArray(sslConfig.getClientPrivateKeyPathStream()), + readAll(sslConfig.getClientKeyCertChainPathStream()), + readAll(sslConfig.getClientPrivateKeyPathStream()), sslConfig.getClientTrustCertCollectionPath() != null - ? IOUtils.toByteArray(sslConfig.getClientTrustCertCollectionPathStream()) + ? readAll(sslConfig.getClientTrustCertCollectionPathStream()) : null, sslConfig.getClientKeyPassword()); } catch (IOException e) { @@ -102,4 +103,15 @@ public Cert getConsumerConnectionConfig(URL remoteAddress) { }) .orElse(null); } + + /** + * Reads the stream into memory and closes it. The streams returned by {@code SslConfig} are + * file-backed; leaving them open keeps the underlying files locked on Windows and blocks + * deletion of the containing directory. + */ + private byte[] readAll(InputStream inputStream) throws IOException { + try (InputStream stream = inputStream) { + return IOUtils.toByteArray(stream); + } + } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/isolation/spring/annotation/AnnotationIsolationTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/isolation/spring/annotation/AnnotationIsolationTest.java index 1ce76f3cb142..7567752e4669 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/isolation/spring/annotation/AnnotationIsolationTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/isolation/spring/annotation/AnnotationIsolationTest.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.concurrent.Executor; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -40,6 +41,13 @@ public class AnnotationIsolationTest extends BaseTest { + // SysProps set inside the test would otherwise leak into other test classes running in the + // same JVM: Dubbo resolves dubbo.metrics.* from System properties with the highest precedence. + @AfterEach + public void afterEach() { + SysProps.clear(); + } + @Test public void test() throws Exception { SysProps.clear(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/metrics/SpringBootConfigMetricsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/metrics/SpringBootConfigMetricsTest.java index 8f7fc61e7f89..806bf3e5af8d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/metrics/SpringBootConfigMetricsTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/metrics/SpringBootConfigMetricsTest.java @@ -60,6 +60,14 @@ public class SpringBootConfigMetricsTest { @BeforeAll public static void beforeAll() { + // Clear JVM System properties that other tests in the same JVM may have left set to + // disable metrics. Dubbo resolves dubbo.metrics.* from the System properties with the + // highest precedence, which would otherwise override this test's @SpringBootTest + // property dubbo.metrics.protocol=prometheus and fail the assertion below. + // See SpringBootConfigPropsTest for the same guard and AnnotationIsolationTest, which + // previously leaked these properties (now cleaned up in its @AfterEach). + System.clearProperty("dubbo.metrics.protocol"); + System.clearProperty("dubbo.metrics.enabled"); DubboBootstrap.reset(); }