From ed3a0fe870481e6c88e3e09d3affa2fb1fba3f44 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 18 Nov 2026 16:30:34 -0800 Subject: [PATCH 1/4] fix(core): stop splitting the workloadapi.internal package across modules The grpc-netty-* modules each shipped their own GrpcManagedChannelFactory and ManagedChannelWrapper in io.spiffe.workloadapi.internal, the same package core uses, and core picked whichever came first on the classpath. That split package breaks consumers on the Java module path. Move ManagedChannelWrapper into core with a plain close hook, add a GrpcChannelProvider interface, and have each grpc-netty module register its provider in its own package through META-INF/services. Core loads it with ServiceLoader and picks the one supporting the current OS and arch, falling back to the first one found so TCP addresses keep working. Core no longer needs a grpc-netty module on its compile classpath. Signed-off-by: Srinivas Chippagiri --- CHANGELOG.md | 1 + java-spiffe-core/build.gradle | 10 +- .../grpc-netty-linux/build.gradle | 4 + .../internal/GrpcManagedChannelFactory.java | 82 --------- .../internal/ManagedChannelWrapper.java | 51 ------ .../linux/LinuxGrpcChannelProvider.java | 74 ++++++++ ...e.workloadapi.internal.GrpcChannelProvider | 1 + .../linux/LinuxGrpcChannelProviderTest.java | 61 +++++++ .../grpc-netty-macos-aarch64/build.gradle | 4 + .../internal/GrpcManagedChannelFactory.java | 82 --------- .../internal/ManagedChannelWrapper.java | 51 ------ .../MacOsAarch64GrpcChannelProvider.java | 74 ++++++++ ...e.workloadapi.internal.GrpcChannelProvider | 1 + .../MacOsAarch64GrpcChannelProviderTest.java | 61 +++++++ .../grpc-netty-macos/build.gradle | 4 + .../internal/GrpcManagedChannelFactory.java | 82 --------- .../macos/MacOsGrpcChannelProvider.java | 75 ++++++++ ...e.workloadapi.internal.GrpcChannelProvider | 1 + .../macos/MacOsGrpcChannelProviderTest.java | 61 +++++++ .../internal/GrpcChannelProvider.java | 32 ++++ .../internal/GrpcManagedChannelFactory.java | 86 +++++++++ .../internal/ManagedChannelWrapper.java | 22 +-- .../GrpcManagedChannelFactoryTest.java | 172 ++++++++++++++++++ 23 files changed, 725 insertions(+), 367 deletions(-) delete mode 100644 java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java delete mode 100644 java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java create mode 100644 java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProvider.java create mode 100644 java-spiffe-core/grpc-netty-linux/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider create mode 100644 java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProviderTest.java delete mode 100644 java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java delete mode 100644 java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java create mode 100644 java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProvider.java create mode 100644 java-spiffe-core/grpc-netty-macos-aarch64/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider create mode 100644 java-spiffe-core/grpc-netty-macos-aarch64/src/test/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProviderTest.java delete mode 100644 java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java create mode 100644 java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProvider.java create mode 100644 java-spiffe-core/grpc-netty-macos/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider create mode 100644 java-spiffe-core/grpc-netty-macos/src/test/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProviderTest.java create mode 100644 java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcChannelProvider.java create mode 100644 java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java rename java-spiffe-core/{grpc-netty-macos => }/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java (54%) create mode 100644 java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 316039c7..23d7aa1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510) * Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512) * Serialize `java-spiffe-helper` KeyStore and TrustStore files to memory before writing them, so a store or encoding failure no longer leaves an empty or partial file (#490) +* Stop splitting the `io.spiffe.workloadapi.internal` package between `java-spiffe-core` and the `grpc-netty-*` modules, which broke builds on the Java module path. Each `grpc-netty-*` module now registers a channel provider in its own package that core loads with `ServiceLoader`, picking the one for the current OS and architecture (#108) ### Build diff --git a/java-spiffe-core/build.gradle b/java-spiffe-core/build.gradle index 96f7be32..629af24f 100644 --- a/java-spiffe-core/build.gradle +++ b/java-spiffe-core/build.gradle @@ -56,19 +56,17 @@ dependencies { def osArch = System.getProperty("os.arch") + // core finds the channel provider with ServiceLoader, so it only needs a grpc-netty module at test time if (osdetector.os.is('osx')) { if (osArch == "x86_64") { - compileOnly(project('grpc-netty-macos')) - testImplementation(project('grpc-netty-macos')) + testRuntimeOnly(project('grpc-netty-macos')) } else if (osArch == "aarch64") { - compileOnly(project('grpc-netty-macos-aarch64')) - testImplementation(project('grpc-netty-macos-aarch64')) + testRuntimeOnly(project('grpc-netty-macos-aarch64')) } else { throw new GradleException("Architecture not supported: ${osArch}") } } else { - compileOnly(project('grpc-netty-linux')) - testImplementation(project('grpc-netty-linux')) + testRuntimeOnly(project('grpc-netty-linux')) } implementation "io.grpc:grpc-protobuf:${grpcVersion}" diff --git a/java-spiffe-core/grpc-netty-linux/build.gradle b/java-spiffe-core/grpc-netty-linux/build.gradle index a76faebb..26c8f3a3 100644 --- a/java-spiffe-core/grpc-netty-linux/build.gradle +++ b/java-spiffe-core/grpc-netty-linux/build.gradle @@ -1,6 +1,10 @@ description = "Java SPIFFE Library GRPC-Netty Linux support module" dependencies { + // provided by the application, which always depends on java-spiffe-core + compileOnly(project(':java-spiffe-core')) + testImplementation(project(':java-spiffe-core')) + implementation "io.grpc:grpc-netty-shaded:${grpcVersion}" } diff --git a/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java b/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java deleted file mode 100644 index 0edfe53f..00000000 --- a/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java +++ /dev/null @@ -1,82 +0,0 @@ -package io.spiffe.workloadapi.internal; - -import io.grpc.ManagedChannel; -import io.grpc.netty.shaded.io.grpc.netty.NegotiationType; -import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; -import io.grpc.netty.shaded.io.netty.channel.ChannelOption; -import io.grpc.netty.shaded.io.netty.channel.EventLoopGroup; -import io.grpc.netty.shaded.io.netty.channel.epoll.EpollDomainSocketChannel; -import io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup; -import io.grpc.netty.shaded.io.netty.channel.unix.DomainSocketAddress; -import org.apache.commons.lang3.SystemUtils; - -import java.net.URI; -import java.util.Objects; -import java.util.concurrent.ExecutorService; - -/** - * Factory for creating ManagedChannel instances for Linux OS. - */ -public final class GrpcManagedChannelFactory { - - private static final String UNIX_SCHEME = "unix"; - private static final String TCP_SCHEME = "tcp"; - - private GrpcManagedChannelFactory() { - } - - /** - * Returns a ManagedChannelWrapper that contains a {@link ManagedChannel} to the SPIFFE Socket Endpoint provided. - * - * @param address URI representing the Workload API endpoint. - * @param executorService the executor to configure the event loop group - * @return a instance of a {@link ManagedChannelWrapper} - */ - public static ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { - Objects.requireNonNull(address, "address must not be null"); - - String scheme = address.getScheme(); - ManagedChannelWrapper result; - switch (scheme) { - case UNIX_SCHEME: - result = createNativeSocketChannel(address, executorService); - break; - case TCP_SCHEME: - result = createTcpChannel(address); - break; - default: - throw new IllegalArgumentException("Address Scheme not supported: "); - } - return result; - } - - // Create a Native Socket Channel pointing to the spiffeSocketPath - private static ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { - NettyChannelBuilder channelBuilder = NettyChannelBuilder. - forAddress(new DomainSocketAddress(address.getPath())); - EventLoopGroup eventLoopGroup = configureNativeSocketChannel(channelBuilder, executorService); - ManagedChannel managedChannel = channelBuilder.usePlaintext().build(); - return new ManagedChannelWrapper(managedChannel, eventLoopGroup); - } - - private static ManagedChannelWrapper createTcpChannel(URI address) { - ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) - .negotiationType(NegotiationType.PLAINTEXT) - .build(); - return new ManagedChannelWrapper(managedChannel); - } - - private static EventLoopGroup configureNativeSocketChannel(NettyChannelBuilder channelBuilder, ExecutorService executorService) { - if (SystemUtils.IS_OS_LINUX) { - // nThreads = 0 -> use Netty default - EpollEventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, executorService); - channelBuilder.eventLoopGroup(epollEventLoopGroup) - // avoid warning Unknown channel option 'SO_KEEPALIVE' - .withOption(ChannelOption.SO_KEEPALIVE, null) - .channelType(EpollDomainSocketChannel.class); - return epollEventLoopGroup; - } - - throw new IllegalStateException("Operating System is not supported."); - } -} \ No newline at end of file diff --git a/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java b/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java deleted file mode 100644 index 9985b69c..00000000 --- a/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.spiffe.workloadapi.internal; - -import io.grpc.ManagedChannel; -import io.grpc.netty.shaded.io.netty.channel.EventLoopGroup; - -import java.io.Closeable; - -/** - * Wraps a {@link ManagedChannel} along with the {@link EventLoopGroup} in order to - * have more control and be able to shutdown the channel properly - * calling the shutdownGracefully method on the EventLoopGroup to prevent - * that some threads remain active. - */ -public class ManagedChannelWrapper implements Closeable { - - private final ManagedChannel managedChannel; - private final EventLoopGroup eventLoopGroup; - - /** - * Constructor. - * - * @param managedChannel an instance of {@link ManagedChannel} - * @param eventLoopGroup an instance of {@link EventLoopGroup} - */ - public ManagedChannelWrapper(ManagedChannel managedChannel, EventLoopGroup eventLoopGroup) { - this.managedChannel = managedChannel; - this.eventLoopGroup = eventLoopGroup; - } - - /** - * Constructor. - * - * @param managedChannel a {@link ManagedChannel} - */ - public ManagedChannelWrapper(ManagedChannel managedChannel) { - this.managedChannel = managedChannel; - this.eventLoopGroup = null; - } - - @Override - public void close() { - if (eventLoopGroup != null) { - eventLoopGroup.shutdownGracefully(); - } - managedChannel.shutdown(); - } - - public ManagedChannel getChannel() { - return managedChannel; - } -} diff --git a/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProvider.java b/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProvider.java new file mode 100644 index 00000000..48200e44 --- /dev/null +++ b/java-spiffe-core/grpc-netty-linux/src/main/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProvider.java @@ -0,0 +1,74 @@ +package io.spiffe.workloadapi.internal.linux; + +import io.grpc.ManagedChannel; +import io.grpc.netty.shaded.io.grpc.netty.NegotiationType; +import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; +import io.grpc.netty.shaded.io.netty.channel.ChannelOption; +import io.grpc.netty.shaded.io.netty.channel.epoll.EpollDomainSocketChannel; +import io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup; +import io.grpc.netty.shaded.io.netty.channel.unix.DomainSocketAddress; +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.apache.commons.lang3.SystemUtils; + +import java.net.URI; +import java.util.Objects; +import java.util.concurrent.ExecutorService; + +/** + * {@link GrpcChannelProvider} for Linux, based on the Netty epoll transport. + */ +public final class LinuxGrpcChannelProvider implements GrpcChannelProvider { + + private static final String UNIX_SCHEME = "unix"; + private static final String TCP_SCHEME = "tcp"; + + @Override + public boolean isSupported() { + return SystemUtils.IS_OS_LINUX; + } + + @Override + public ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { + Objects.requireNonNull(address, "address must not be null"); + + String scheme = address.getScheme(); + ManagedChannelWrapper result; + switch (scheme) { + case UNIX_SCHEME: + result = createNativeSocketChannel(address, executorService); + break; + case TCP_SCHEME: + result = createTcpChannel(address); + break; + default: + throw new IllegalArgumentException("Address Scheme not supported: "); + } + return result; + } + + // Create a Native Socket Channel pointing to the spiffeSocketPath + private ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { + if (!isSupported()) { + throw new IllegalStateException("Operating System is not supported."); + } + + // nThreads = 0 -> use Netty default + EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(0, executorService); + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(new DomainSocketAddress(address.getPath())) + .eventLoopGroup(eventLoopGroup) + // avoid warning Unknown channel option 'SO_KEEPALIVE' + .withOption(ChannelOption.SO_KEEPALIVE, null) + .channelType(EpollDomainSocketChannel.class) + .usePlaintext() + .build(); + return new ManagedChannelWrapper(managedChannel, eventLoopGroup::shutdownGracefully); + } + + private ManagedChannelWrapper createTcpChannel(URI address) { + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) + .negotiationType(NegotiationType.PLAINTEXT) + .build(); + return new ManagedChannelWrapper(managedChannel); + } +} diff --git a/java-spiffe-core/grpc-netty-linux/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider b/java-spiffe-core/grpc-netty-linux/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider new file mode 100644 index 00000000..302f4e97 --- /dev/null +++ b/java-spiffe-core/grpc-netty-linux/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider @@ -0,0 +1 @@ +io.spiffe.workloadapi.internal.linux.LinuxGrpcChannelProvider diff --git a/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProviderTest.java b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProviderTest.java new file mode 100644 index 00000000..f02d1749 --- /dev/null +++ b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/LinuxGrpcChannelProviderTest.java @@ -0,0 +1,61 @@ +package io.spiffe.workloadapi.internal.linux; + +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class LinuxGrpcChannelProviderTest { + + @Test + void isRegisteredAsAServiceProvider() { + List providers = new ArrayList<>(); + for (GrpcChannelProvider provider : ServiceLoader.load(GrpcChannelProvider.class)) { + providers.add(provider); + } + + assertEquals(1, providers.size()); + assertEquals(LinuxGrpcChannelProvider.class, providers.get(0).getClass()); + } + + @Test + void newChannel_tcpAddress() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + ManagedChannelWrapper wrapper = new LinuxGrpcChannelProvider().newChannel(URI.create("tcp://127.0.0.1:8000"), executor); + + wrapper.close(); + assertTrue(wrapper.getChannel().isShutdown()); + } finally { + executor.shutdownNow(); + } + } + + @Test + void newChannel_unsupportedScheme() { + assertThrows(IllegalArgumentException.class, + () -> new LinuxGrpcChannelProvider().newChannel(URI.create("http://127.0.0.1:8000"), null)); + } + + @Test + void newChannel_unixAddressOnAnUnsupportedPlatform() { + LinuxGrpcChannelProvider provider = new LinuxGrpcChannelProvider(); + if (provider.isSupported()) { + return; + } + + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> provider.newChannel(URI.create("unix:/tmp/spire-agent/public/api.sock"), null)); + assertEquals("Operating System is not supported.", e.getMessage()); + } +} diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/build.gradle b/java-spiffe-core/grpc-netty-macos-aarch64/build.gradle index 5cb4db48..7bb6205d 100644 --- a/java-spiffe-core/grpc-netty-macos-aarch64/build.gradle +++ b/java-spiffe-core/grpc-netty-macos-aarch64/build.gradle @@ -1,6 +1,10 @@ description = "Java SPIFFE Library GRPC-Netty MacOS module" dependencies { + // provided by the application, which always depends on java-spiffe-core + compileOnly(project(':java-spiffe-core')) + testImplementation(project(':java-spiffe-core')) + implementation "io.grpc:grpc-netty:${grpcVersion}" // version must match the one in grpc-netty diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java deleted file mode 100644 index f677db1a..00000000 --- a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java +++ /dev/null @@ -1,82 +0,0 @@ -package io.spiffe.workloadapi.internal; - -import io.grpc.ManagedChannel; -import io.grpc.netty.NegotiationType; -import io.grpc.netty.NettyChannelBuilder; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.kqueue.KQueueDomainSocketChannel; -import io.netty.channel.kqueue.KQueueEventLoopGroup; -import io.netty.channel.unix.DomainSocketAddress; -import org.apache.commons.lang3.SystemUtils; - -import java.net.URI; -import java.util.Objects; -import java.util.concurrent.ExecutorService; - -/** - * Factory for creating ManagedChannel instances for Mac OS. - */ -public final class GrpcManagedChannelFactory { - - private static final String UNIX_SCHEME = "unix"; - private static final String TCP_SCHEME = "tcp"; - - private GrpcManagedChannelFactory() { - } - - /** - * Returns a ManagedChannelWrapper that contains a {@link ManagedChannel} to the SPIFFE Socket Endpoint provided. - * - * @param address URI representing the Workload API endpoint. - * @param executorService the executor to configure the event loop group - * @return a instance of a {@link ManagedChannelWrapper} - */ - public static ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { - Objects.requireNonNull(address, "address must not be null"); - - String scheme = address.getScheme(); - ManagedChannelWrapper result; - switch (scheme) { - case UNIX_SCHEME: - result = createNativeSocketChannel(address, executorService); - break; - case TCP_SCHEME: - result = createTcpChannel(address); - break; - default: - throw new IllegalArgumentException("Address Scheme not supported: "); - } - return result; - } - - // Create a Native Socket Channel pointing to the spiffeSocketPath - private static ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { - NettyChannelBuilder channelBuilder = NettyChannelBuilder. - forAddress(new DomainSocketAddress(address.getPath())); - EventLoopGroup eventLoopGroup = configureNativeSocketChannel(channelBuilder, executorService); - ManagedChannel managedChannel = channelBuilder.usePlaintext().build(); - return new ManagedChannelWrapper(managedChannel, eventLoopGroup); - } - - private static ManagedChannelWrapper createTcpChannel(URI address) { - ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) - .negotiationType(NegotiationType.PLAINTEXT) - .build(); - return new ManagedChannelWrapper(managedChannel); - } - - private static EventLoopGroup configureNativeSocketChannel(NettyChannelBuilder channelBuilder, ExecutorService executorService) { - if (SystemUtils.IS_OS_MAC) { - // nThreads = 0 -> use Netty default - KQueueEventLoopGroup eventLoopGroup = new KQueueEventLoopGroup(0, executorService); - channelBuilder.eventLoopGroup(eventLoopGroup) - // avoid warning Unknown channel option 'SO_KEEPALIVE' - .withOption(ChannelOption.SO_KEEPALIVE, null) - .channelType(KQueueDomainSocketChannel.class); - return eventLoopGroup; - } - - throw new IllegalStateException("Operating System is not supported."); - } -} \ No newline at end of file diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java deleted file mode 100644 index 4bef70de..00000000 --- a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.spiffe.workloadapi.internal; - -import io.grpc.ManagedChannel; -import io.netty.channel.EventLoopGroup; - -import java.io.Closeable; - -/** - * Wraps a {@link ManagedChannel} along with the {@link EventLoopGroup} in order to - * have more control and be able to shutdown the channel properly - * calling the shutdownGracefully method on the EventLoopGroup to prevent - * that some threads remain active. - */ -public class ManagedChannelWrapper implements Closeable { - - private final ManagedChannel managedChannel; - private final EventLoopGroup eventLoopGroup; - - /** - * Constructor. - * - * @param managedChannel an instance of {@link ManagedChannel} - * @param eventLoopGroup an instance of {@link EventLoopGroup} - */ - public ManagedChannelWrapper(ManagedChannel managedChannel, EventLoopGroup eventLoopGroup) { - this.managedChannel = managedChannel; - this.eventLoopGroup = eventLoopGroup; - } - - /** - * Constructor. - * - * @param managedChannel a {@link ManagedChannel} - */ - public ManagedChannelWrapper(ManagedChannel managedChannel) { - this.managedChannel = managedChannel; - this.eventLoopGroup = null; - } - - @Override - public void close() { - if (eventLoopGroup != null) { - eventLoopGroup.shutdownGracefully(); - } - managedChannel.shutdown(); - } - - public ManagedChannel getChannel() { - return managedChannel; - } -} diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProvider.java b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProvider.java new file mode 100644 index 00000000..bb34d3ad --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProvider.java @@ -0,0 +1,74 @@ +package io.spiffe.workloadapi.internal.macos.aarch64; + +import io.grpc.ManagedChannel; +import io.grpc.netty.NegotiationType; +import io.grpc.netty.NettyChannelBuilder; +import io.netty.channel.ChannelOption; +import io.netty.channel.kqueue.KQueueDomainSocketChannel; +import io.netty.channel.kqueue.KQueueEventLoopGroup; +import io.netty.channel.unix.DomainSocketAddress; +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.apache.commons.lang3.SystemUtils; + +import java.net.URI; +import java.util.Objects; +import java.util.concurrent.ExecutorService; + +/** + * {@link GrpcChannelProvider} for macOS on aarch64, based on the Netty kqueue transport. + */ +public final class MacOsAarch64GrpcChannelProvider implements GrpcChannelProvider { + + private static final String UNIX_SCHEME = "unix"; + private static final String TCP_SCHEME = "tcp"; + + @Override + public boolean isSupported() { + return SystemUtils.IS_OS_MAC && "aarch64".equals(SystemUtils.OS_ARCH); + } + + @Override + public ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { + Objects.requireNonNull(address, "address must not be null"); + + String scheme = address.getScheme(); + ManagedChannelWrapper result; + switch (scheme) { + case UNIX_SCHEME: + result = createNativeSocketChannel(address, executorService); + break; + case TCP_SCHEME: + result = createTcpChannel(address); + break; + default: + throw new IllegalArgumentException("Address Scheme not supported: "); + } + return result; + } + + // Create a Native Socket Channel pointing to the spiffeSocketPath + private ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { + if (!isSupported()) { + throw new IllegalStateException("Operating System is not supported."); + } + + // nThreads = 0 -> use Netty default + KQueueEventLoopGroup eventLoopGroup = new KQueueEventLoopGroup(0, executorService); + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(new DomainSocketAddress(address.getPath())) + .eventLoopGroup(eventLoopGroup) + // avoid warning Unknown channel option 'SO_KEEPALIVE' + .withOption(ChannelOption.SO_KEEPALIVE, null) + .channelType(KQueueDomainSocketChannel.class) + .usePlaintext() + .build(); + return new ManagedChannelWrapper(managedChannel, eventLoopGroup::shutdownGracefully); + } + + private ManagedChannelWrapper createTcpChannel(URI address) { + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) + .negotiationType(NegotiationType.PLAINTEXT) + .build(); + return new ManagedChannelWrapper(managedChannel); + } +} diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider new file mode 100644 index 00000000..2255da98 --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos-aarch64/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider @@ -0,0 +1 @@ +io.spiffe.workloadapi.internal.macos.aarch64.MacOsAarch64GrpcChannelProvider diff --git a/java-spiffe-core/grpc-netty-macos-aarch64/src/test/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProviderTest.java b/java-spiffe-core/grpc-netty-macos-aarch64/src/test/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProviderTest.java new file mode 100644 index 00000000..e0447aee --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos-aarch64/src/test/java/io/spiffe/workloadapi/internal/macos/aarch64/MacOsAarch64GrpcChannelProviderTest.java @@ -0,0 +1,61 @@ +package io.spiffe.workloadapi.internal.macos.aarch64; + +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MacOsAarch64GrpcChannelProviderTest { + + @Test + void isRegisteredAsAServiceProvider() { + List providers = new ArrayList<>(); + for (GrpcChannelProvider provider : ServiceLoader.load(GrpcChannelProvider.class)) { + providers.add(provider); + } + + assertEquals(1, providers.size()); + assertEquals(MacOsAarch64GrpcChannelProvider.class, providers.get(0).getClass()); + } + + @Test + void newChannel_tcpAddress() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + ManagedChannelWrapper wrapper = new MacOsAarch64GrpcChannelProvider().newChannel(URI.create("tcp://127.0.0.1:8000"), executor); + + wrapper.close(); + assertTrue(wrapper.getChannel().isShutdown()); + } finally { + executor.shutdownNow(); + } + } + + @Test + void newChannel_unsupportedScheme() { + assertThrows(IllegalArgumentException.class, + () -> new MacOsAarch64GrpcChannelProvider().newChannel(URI.create("http://127.0.0.1:8000"), null)); + } + + @Test + void newChannel_unixAddressOnAnUnsupportedPlatform() { + MacOsAarch64GrpcChannelProvider provider = new MacOsAarch64GrpcChannelProvider(); + if (provider.isSupported()) { + return; + } + + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> provider.newChannel(URI.create("unix:/tmp/spire-agent/public/api.sock"), null)); + assertEquals("Operating System is not supported.", e.getMessage()); + } +} diff --git a/java-spiffe-core/grpc-netty-macos/build.gradle b/java-spiffe-core/grpc-netty-macos/build.gradle index ac7fc0cb..6602a6ae 100644 --- a/java-spiffe-core/grpc-netty-macos/build.gradle +++ b/java-spiffe-core/grpc-netty-macos/build.gradle @@ -1,6 +1,10 @@ description = "Java SPIFFE Library GRPC-Netty MacOS module" dependencies { + // provided by the application, which always depends on java-spiffe-core + compileOnly(project(':java-spiffe-core')) + testImplementation(project(':java-spiffe-core')) + implementation "io.grpc:grpc-netty:${grpcVersion}" implementation "io.netty:netty-transport-native-kqueue:${nettyVersion}:osx-x86_64" } diff --git a/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java b/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java deleted file mode 100644 index f677db1a..00000000 --- a/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java +++ /dev/null @@ -1,82 +0,0 @@ -package io.spiffe.workloadapi.internal; - -import io.grpc.ManagedChannel; -import io.grpc.netty.NegotiationType; -import io.grpc.netty.NettyChannelBuilder; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.kqueue.KQueueDomainSocketChannel; -import io.netty.channel.kqueue.KQueueEventLoopGroup; -import io.netty.channel.unix.DomainSocketAddress; -import org.apache.commons.lang3.SystemUtils; - -import java.net.URI; -import java.util.Objects; -import java.util.concurrent.ExecutorService; - -/** - * Factory for creating ManagedChannel instances for Mac OS. - */ -public final class GrpcManagedChannelFactory { - - private static final String UNIX_SCHEME = "unix"; - private static final String TCP_SCHEME = "tcp"; - - private GrpcManagedChannelFactory() { - } - - /** - * Returns a ManagedChannelWrapper that contains a {@link ManagedChannel} to the SPIFFE Socket Endpoint provided. - * - * @param address URI representing the Workload API endpoint. - * @param executorService the executor to configure the event loop group - * @return a instance of a {@link ManagedChannelWrapper} - */ - public static ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { - Objects.requireNonNull(address, "address must not be null"); - - String scheme = address.getScheme(); - ManagedChannelWrapper result; - switch (scheme) { - case UNIX_SCHEME: - result = createNativeSocketChannel(address, executorService); - break; - case TCP_SCHEME: - result = createTcpChannel(address); - break; - default: - throw new IllegalArgumentException("Address Scheme not supported: "); - } - return result; - } - - // Create a Native Socket Channel pointing to the spiffeSocketPath - private static ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { - NettyChannelBuilder channelBuilder = NettyChannelBuilder. - forAddress(new DomainSocketAddress(address.getPath())); - EventLoopGroup eventLoopGroup = configureNativeSocketChannel(channelBuilder, executorService); - ManagedChannel managedChannel = channelBuilder.usePlaintext().build(); - return new ManagedChannelWrapper(managedChannel, eventLoopGroup); - } - - private static ManagedChannelWrapper createTcpChannel(URI address) { - ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) - .negotiationType(NegotiationType.PLAINTEXT) - .build(); - return new ManagedChannelWrapper(managedChannel); - } - - private static EventLoopGroup configureNativeSocketChannel(NettyChannelBuilder channelBuilder, ExecutorService executorService) { - if (SystemUtils.IS_OS_MAC) { - // nThreads = 0 -> use Netty default - KQueueEventLoopGroup eventLoopGroup = new KQueueEventLoopGroup(0, executorService); - channelBuilder.eventLoopGroup(eventLoopGroup) - // avoid warning Unknown channel option 'SO_KEEPALIVE' - .withOption(ChannelOption.SO_KEEPALIVE, null) - .channelType(KQueueDomainSocketChannel.class); - return eventLoopGroup; - } - - throw new IllegalStateException("Operating System is not supported."); - } -} \ No newline at end of file diff --git a/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProvider.java b/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProvider.java new file mode 100644 index 00000000..e337a5ea --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProvider.java @@ -0,0 +1,75 @@ +package io.spiffe.workloadapi.internal.macos; + +import io.grpc.ManagedChannel; +import io.grpc.netty.NegotiationType; +import io.grpc.netty.NettyChannelBuilder; +import io.netty.channel.ChannelOption; +import io.netty.channel.kqueue.KQueueDomainSocketChannel; +import io.netty.channel.kqueue.KQueueEventLoopGroup; +import io.netty.channel.unix.DomainSocketAddress; +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.apache.commons.lang3.SystemUtils; + +import java.net.URI; +import java.util.Objects; +import java.util.concurrent.ExecutorService; + +/** + * {@link GrpcChannelProvider} for macOS on x86_64, based on the Netty kqueue transport. + */ +public final class MacOsGrpcChannelProvider implements GrpcChannelProvider { + + private static final String UNIX_SCHEME = "unix"; + private static final String TCP_SCHEME = "tcp"; + + @Override + public boolean isSupported() { + return SystemUtils.IS_OS_MAC + && ("x86_64".equals(SystemUtils.OS_ARCH) || "amd64".equals(SystemUtils.OS_ARCH)); + } + + @Override + public ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { + Objects.requireNonNull(address, "address must not be null"); + + String scheme = address.getScheme(); + ManagedChannelWrapper result; + switch (scheme) { + case UNIX_SCHEME: + result = createNativeSocketChannel(address, executorService); + break; + case TCP_SCHEME: + result = createTcpChannel(address); + break; + default: + throw new IllegalArgumentException("Address Scheme not supported: "); + } + return result; + } + + // Create a Native Socket Channel pointing to the spiffeSocketPath + private ManagedChannelWrapper createNativeSocketChannel(URI address, ExecutorService executorService) { + if (!isSupported()) { + throw new IllegalStateException("Operating System is not supported."); + } + + // nThreads = 0 -> use Netty default + KQueueEventLoopGroup eventLoopGroup = new KQueueEventLoopGroup(0, executorService); + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(new DomainSocketAddress(address.getPath())) + .eventLoopGroup(eventLoopGroup) + // avoid warning Unknown channel option 'SO_KEEPALIVE' + .withOption(ChannelOption.SO_KEEPALIVE, null) + .channelType(KQueueDomainSocketChannel.class) + .usePlaintext() + .build(); + return new ManagedChannelWrapper(managedChannel, eventLoopGroup::shutdownGracefully); + } + + private ManagedChannelWrapper createTcpChannel(URI address) { + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(address.getHost(), address.getPort()) + .negotiationType(NegotiationType.PLAINTEXT) + .build(); + return new ManagedChannelWrapper(managedChannel); + } +} diff --git a/java-spiffe-core/grpc-netty-macos/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider b/java-spiffe-core/grpc-netty-macos/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider new file mode 100644 index 00000000..fd02e88a --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos/src/main/resources/META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider @@ -0,0 +1 @@ +io.spiffe.workloadapi.internal.macos.MacOsGrpcChannelProvider diff --git a/java-spiffe-core/grpc-netty-macos/src/test/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProviderTest.java b/java-spiffe-core/grpc-netty-macos/src/test/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProviderTest.java new file mode 100644 index 00000000..94dcbc33 --- /dev/null +++ b/java-spiffe-core/grpc-netty-macos/src/test/java/io/spiffe/workloadapi/internal/macos/MacOsGrpcChannelProviderTest.java @@ -0,0 +1,61 @@ +package io.spiffe.workloadapi.internal.macos; + +import io.spiffe.workloadapi.internal.GrpcChannelProvider; +import io.spiffe.workloadapi.internal.ManagedChannelWrapper; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MacOsGrpcChannelProviderTest { + + @Test + void isRegisteredAsAServiceProvider() { + List providers = new ArrayList<>(); + for (GrpcChannelProvider provider : ServiceLoader.load(GrpcChannelProvider.class)) { + providers.add(provider); + } + + assertEquals(1, providers.size()); + assertEquals(MacOsGrpcChannelProvider.class, providers.get(0).getClass()); + } + + @Test + void newChannel_tcpAddress() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + ManagedChannelWrapper wrapper = new MacOsGrpcChannelProvider().newChannel(URI.create("tcp://127.0.0.1:8000"), executor); + + wrapper.close(); + assertTrue(wrapper.getChannel().isShutdown()); + } finally { + executor.shutdownNow(); + } + } + + @Test + void newChannel_unsupportedScheme() { + assertThrows(IllegalArgumentException.class, + () -> new MacOsGrpcChannelProvider().newChannel(URI.create("http://127.0.0.1:8000"), null)); + } + + @Test + void newChannel_unixAddressOnAnUnsupportedPlatform() { + MacOsGrpcChannelProvider provider = new MacOsGrpcChannelProvider(); + if (provider.isSupported()) { + return; + } + + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> provider.newChannel(URI.create("unix:/tmp/spire-agent/public/api.sock"), null)); + assertEquals("Operating System is not supported.", e.getMessage()); + } +} diff --git a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcChannelProvider.java b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcChannelProvider.java new file mode 100644 index 00000000..4397db25 --- /dev/null +++ b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcChannelProvider.java @@ -0,0 +1,32 @@ +package io.spiffe.workloadapi.internal; + +import java.net.URI; +import java.util.concurrent.ExecutorService; + +/** + * Creates the gRPC channels used to talk to the Workload API. + *

+ * Implementations live in the platform specific {@code grpc-netty-*} modules and are registered in + * {@code META-INF/services/io.spiffe.workloadapi.internal.GrpcChannelProvider}, so they are found + * with {@link java.util.ServiceLoader} instead of by sharing a class name with core. + */ +public interface GrpcChannelProvider { + + /** + * Returns whether this provider can open Unix domain socket channels on the current OS and + * architecture. + * + * @return true if this provider supports the current platform + */ + boolean isSupported(); + + /** + * Returns a ManagedChannelWrapper that contains a {@link io.grpc.ManagedChannel} to the SPIFFE + * Socket Endpoint provided. + * + * @param address URI representing the Workload API endpoint + * @param executorService the executor to configure the event loop group + * @return an instance of a {@link ManagedChannelWrapper} + */ + ManagedChannelWrapper newChannel(URI address, ExecutorService executorService); +} diff --git a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java new file mode 100644 index 00000000..fa079b35 --- /dev/null +++ b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java @@ -0,0 +1,86 @@ +package io.spiffe.workloadapi.internal; + +import java.net.URI; +import java.util.Iterator; +import java.util.Objects; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; + +/** + * Factory for creating ManagedChannel instances, using the {@link GrpcChannelProvider} found on the + * classpath for the current platform. + */ +public final class GrpcManagedChannelFactory { + + private static volatile GrpcChannelProvider provider; + + private GrpcManagedChannelFactory() { + } + + /** + * Returns a ManagedChannelWrapper that contains a {@link io.grpc.ManagedChannel} to the SPIFFE + * Socket Endpoint provided. + * + * @param address URI representing the Workload API endpoint. + * @param executorService the executor to configure the event loop group + * @return a instance of a {@link ManagedChannelWrapper} + * @throws IllegalStateException if no grpc-netty module is on the classpath + */ + public static ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { + Objects.requireNonNull(address, "address must not be null"); + return getProvider().newChannel(address, executorService); + } + + private static GrpcChannelProvider getProvider() { + GrpcChannelProvider result = provider; + if (result == null) { + result = selectProvider(loadProviders()); + provider = result; + } + return result; + } + + private static Iterator loadProviders() { + return ServiceLoader.load(GrpcChannelProvider.class, GrpcChannelProvider.class.getClassLoader()).iterator(); + } + + /** + * Picks the first provider that supports the current platform. If none does, the first provider + * found is used, so TCP addresses keep working and Unix socket addresses fail with the provider's + * own error, as they did before providers were looked up. + */ + static GrpcChannelProvider selectProvider(Iterator providers) { + GrpcChannelProvider first = null; + while (hasNext(providers)) { + GrpcChannelProvider candidate; + try { + candidate = providers.next(); + } catch (ServiceConfigurationError e) { + // a provider that can't be loaded (e.g. a broken jar) shouldn't hide the others + continue; + } + if (candidate.isSupported()) { + return candidate; + } + if (first == null) { + first = candidate; + } + } + + if (first == null) { + throw new IllegalStateException("No gRPC channel provider found on the classpath. Add one of the " + + "io.spiffe:grpc-netty-linux, io.spiffe:grpc-netty-macos or io.spiffe:grpc-netty-macos-aarch64 " + + "modules as a runtime dependency."); + } + return first; + } + + private static boolean hasNext(Iterator providers) { + try { + return providers.hasNext(); + } catch (ServiceConfigurationError e) { + return false; + } + } +} diff --git a/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java similarity index 54% rename from java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java rename to java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java index 4bef70de..5b846a25 100644 --- a/java-spiffe-core/grpc-netty-macos/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java +++ b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/ManagedChannelWrapper.java @@ -1,30 +1,27 @@ package io.spiffe.workloadapi.internal; import io.grpc.ManagedChannel; -import io.netty.channel.EventLoopGroup; import java.io.Closeable; /** - * Wraps a {@link ManagedChannel} along with the {@link EventLoopGroup} in order to - * have more control and be able to shutdown the channel properly - * calling the shutdownGracefully method on the EventLoopGroup to prevent - * that some threads remain active. + * Wraps a {@link ManagedChannel} along with a hook that releases the transport resources behind it, + * such as the Netty event loop group, so that closing the channel doesn't leave threads behind. */ public class ManagedChannelWrapper implements Closeable { private final ManagedChannel managedChannel; - private final EventLoopGroup eventLoopGroup; + private final Runnable onClose; /** * Constructor. * * @param managedChannel an instance of {@link ManagedChannel} - * @param eventLoopGroup an instance of {@link EventLoopGroup} + * @param onClose called when this wrapper is closed, before the channel is shut down */ - public ManagedChannelWrapper(ManagedChannel managedChannel, EventLoopGroup eventLoopGroup) { + public ManagedChannelWrapper(ManagedChannel managedChannel, Runnable onClose) { this.managedChannel = managedChannel; - this.eventLoopGroup = eventLoopGroup; + this.onClose = onClose; } /** @@ -33,14 +30,13 @@ public ManagedChannelWrapper(ManagedChannel managedChannel, EventLoopGroup event * @param managedChannel a {@link ManagedChannel} */ public ManagedChannelWrapper(ManagedChannel managedChannel) { - this.managedChannel = managedChannel; - this.eventLoopGroup = null; + this(managedChannel, null); } @Override public void close() { - if (eventLoopGroup != null) { - eventLoopGroup.shutdownGracefully(); + if (onClose != null) { + onClose.run(); } managedChannel.shutdown(); } diff --git a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java new file mode 100644 index 00000000..848fabbd --- /dev/null +++ b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java @@ -0,0 +1,172 @@ +package io.spiffe.workloadapi.internal; + +import io.grpc.ManagedChannel; +import io.grpc.inprocess.InProcessChannelBuilder; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class GrpcManagedChannelFactoryTest { + + @Test + void selectProvider_picksTheProviderSupportingThePlatform() { + FakeProvider unsupported = new FakeProvider(false); + FakeProvider supported = new FakeProvider(true); + + GrpcChannelProvider result = + GrpcManagedChannelFactory.selectProvider(Arrays.asList(unsupported, supported).iterator()); + + assertSame(supported, result); + } + + @Test + void selectProvider_fallsBackToTheFirstProviderWhenNoneSupportsThePlatform() { + FakeProvider first = new FakeProvider(false); + FakeProvider second = new FakeProvider(false); + + GrpcChannelProvider result = + GrpcManagedChannelFactory.selectProvider(Arrays.asList(first, second).iterator()); + + assertSame(first, result); + } + + @Test + void selectProvider_skipsProvidersThatFailToLoad() { + FakeProvider supported = new FakeProvider(true); + List entries = new ArrayList<>(); + entries.add(new ServiceConfigurationError("broken provider")); + entries.add(supported); + + GrpcChannelProvider result = GrpcManagedChannelFactory.selectProvider(new FailingIterator(entries)); + + assertSame(supported, result); + } + + @Test + void selectProvider_failsWhenNoProviderIsOnTheClasspath() { + Iterator none = Collections.emptyList().iterator(); + + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> GrpcManagedChannelFactory.selectProvider(none)); + + assertTrue(e.getMessage().startsWith("No gRPC channel provider found on the classpath")); + } + + @Test + void serviceLoader_findsTheGrpcNettyModuleOnTheTestClasspath() { + List providers = new ArrayList<>(); + for (GrpcChannelProvider provider : ServiceLoader.load(GrpcChannelProvider.class)) { + providers.add(provider); + } + + assertEquals(1, providers.size()); + assertTrue(providers.get(0).isSupported()); + assertFalse(providers.get(0).getClass().getPackage().getName().equals(GrpcChannelProvider.class.getPackage().getName()), + "the provider must not share a package with core"); + } + + @Test + void newChannel_usesTheProviderFromTheClasspath() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + ManagedChannelWrapper wrapper = + GrpcManagedChannelFactory.newChannel(URI.create("tcp://127.0.0.1:8000"), executor); + + assertNotNull(wrapper.getChannel()); + wrapper.close(); + assertTrue(wrapper.getChannel().isShutdown()); + } finally { + executor.shutdownNow(); + } + } + + @Test + void newChannel_nullAddress() { + NullPointerException e = assertThrows(NullPointerException.class, + () -> GrpcManagedChannelFactory.newChannel(null, null)); + + assertEquals("address must not be null", e.getMessage()); + } + + @Test + void managedChannelWrapper_closeRunsTheHookAndShutsDownTheChannel() { + ManagedChannel channel = InProcessChannelBuilder.forName("wrapper-test").build(); + AtomicBoolean hookCalled = new AtomicBoolean(); + + new ManagedChannelWrapper(channel, () -> hookCalled.set(true)).close(); + + assertTrue(hookCalled.get()); + assertTrue(channel.isShutdown()); + } + + @Test + void managedChannelWrapper_closeWithoutHook() { + ManagedChannel channel = InProcessChannelBuilder.forName("wrapper-test-no-hook").build(); + + new ManagedChannelWrapper(channel).close(); + + assertTrue(channel.isShutdown()); + } + + private static final class FakeProvider implements GrpcChannelProvider { + private final boolean supported; + + FakeProvider(boolean supported) { + this.supported = supported; + } + + @Override + public boolean isSupported() { + return supported; + } + + @Override + public ManagedChannelWrapper newChannel(URI address, ExecutorService executorService) { + throw new UnsupportedOperationException(); + } + } + + // Mimics a ServiceLoader iterator whose next() throws for a provider that can't be instantiated. + private static final class FailingIterator implements Iterator { + private final Iterator entries; + + FailingIterator(List entries) { + this.entries = entries.iterator(); + } + + @Override + public boolean hasNext() { + return entries.hasNext(); + } + + @Override + public GrpcChannelProvider next() { + if (!entries.hasNext()) { + throw new NoSuchElementException(); + } + Object next = entries.next(); + if (next instanceof ServiceConfigurationError) { + throw (ServiceConfigurationError) next; + } + return (GrpcChannelProvider) next; + } + } +} From 63ee419bf2b254e3a6f4d31534597dd406ad8499 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 18 Nov 2026 16:36:05 -0800 Subject: [PATCH 2/4] fix(core): report provider load errors instead of dropping them Signed-off-by: Srinivas Chippagiri --- .../internal/GrpcManagedChannelFactory.java | 29 +++++++++++-------- .../GrpcManagedChannelFactoryTest.java | 12 ++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java index fa079b35..645f5939 100644 --- a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java +++ b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactory.java @@ -48,16 +48,29 @@ private static Iterator loadProviders() { /** * Picks the first provider that supports the current platform. If none does, the first provider * found is used, so TCP addresses keep working and Unix socket addresses fail with the provider's - * own error, as they did before providers were looked up. + * own error, as they did before providers were looked up. A provider that fails to load doesn't + * hide the others, but its error is reported if no provider could be loaded at all. */ static GrpcChannelProvider selectProvider(Iterator providers) { GrpcChannelProvider first = null; - while (hasNext(providers)) { + ServiceConfigurationError loadError = null; + while (true) { + try { + if (!providers.hasNext()) { + break; + } + } catch (ServiceConfigurationError e) { + // a malformed provider configuration file, the iterator can't move past it + loadError = loadError != null ? loadError : e; + break; + } + GrpcChannelProvider candidate; try { candidate = providers.next(); } catch (ServiceConfigurationError e) { - // a provider that can't be loaded (e.g. a broken jar) shouldn't hide the others + // a provider that can't be instantiated, the iterator has already moved past it + loadError = loadError != null ? loadError : e; continue; } if (candidate.isSupported()) { @@ -71,16 +84,8 @@ static GrpcChannelProvider selectProvider(Iterator provider if (first == null) { throw new IllegalStateException("No gRPC channel provider found on the classpath. Add one of the " + "io.spiffe:grpc-netty-linux, io.spiffe:grpc-netty-macos or io.spiffe:grpc-netty-macos-aarch64 " - + "modules as a runtime dependency."); + + "modules as a runtime dependency.", loadError); } return first; } - - private static boolean hasNext(Iterator providers) { - try { - return providers.hasNext(); - } catch (ServiceConfigurationError e) { - return false; - } - } } diff --git a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java index 848fabbd..2a7ea70c 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/internal/GrpcManagedChannelFactoryTest.java @@ -60,6 +60,18 @@ void selectProvider_skipsProvidersThatFailToLoad() { assertSame(supported, result); } + @Test + void selectProvider_reportsTheLoadErrorWhenNoProviderCanBeLoaded() { + ServiceConfigurationError loadError = new ServiceConfigurationError("broken provider"); + List entries = new ArrayList<>(); + entries.add(loadError); + + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> GrpcManagedChannelFactory.selectProvider(new FailingIterator(entries))); + + assertSame(loadError, e.getCause()); + } + @Test void selectProvider_failsWhenNoProviderIsOnTheClasspath() { Iterator none = Collections.emptyList().iterator(); From ed5165178632332babcfce6ac926ead90c19642d Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 18 Nov 2026 20:39:00 -0800 Subject: [PATCH 3/4] test(core): check core and the Linux transport on the module path Signed-off-by: Srinivas Chippagiri --- CHANGELOG.md | 2 +- .../grpc-netty-linux/build.gradle | 16 ++++ .../internal/linux/ModulePathTest.java | 86 +++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 23d7aa1c..e9d57b67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ * Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510) * Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512) * Serialize `java-spiffe-helper` KeyStore and TrustStore files to memory before writing them, so a store or encoding failure no longer leaves an empty or partial file (#490) -* Stop splitting the `io.spiffe.workloadapi.internal` package between `java-spiffe-core` and the `grpc-netty-*` modules, which broke builds on the Java module path. Each `grpc-netty-*` module now registers a channel provider in its own package that core loads with `ServiceLoader`, picking the one for the current OS and architecture (#108) +* Stop splitting the `io.spiffe.workloadapi.internal` package between `java-spiffe-core` and the `grpc-netty-*` modules, which broke builds on the Java module path. Each `grpc-netty-*` module now registers a channel provider in its own package that core loads with `ServiceLoader`, picking the one for the current OS and architecture (#108). Upgrade `java-spiffe-core` and the `grpc-netty-*` artifact together, since an older `grpc-netty-*` jar still contains the shared package ### Build diff --git a/java-spiffe-core/grpc-netty-linux/build.gradle b/java-spiffe-core/grpc-netty-linux/build.gradle index 26c8f3a3..07d12450 100644 --- a/java-spiffe-core/grpc-netty-linux/build.gradle +++ b/java-spiffe-core/grpc-netty-linux/build.gradle @@ -1,6 +1,15 @@ description = "Java SPIFFE Library GRPC-Netty Linux support module" +configurations { + // core, this module and their runtime dependencies, as an application would put them on the module path + modulePath +} + dependencies { + modulePath(project(':java-spiffe-core')) + modulePath(files(tasks.named('jar'))) + modulePath "io.grpc:grpc-netty-shaded:${grpcVersion}" + // provided by the application, which always depends on java-spiffe-core compileOnly(project(':java-spiffe-core')) testImplementation(project(':java-spiffe-core')) @@ -11,3 +20,10 @@ dependencies { jar { archiveClassifier = '' } + +tasks.named('test') { + inputs.files(configurations.modulePath) + doFirst { + systemProperty 'spiffe.modulepath', configurations.modulePath.files.join(File.pathSeparator) + } +} diff --git a/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java new file mode 100644 index 00000000..acfc0a9a --- /dev/null +++ b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java @@ -0,0 +1,86 @@ +package io.spiffe.workloadapi.internal.linux; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; + +import java.io.File; +import java.lang.module.Configuration; +import java.lang.module.FindException; +import java.lang.module.ModuleFinder; +import java.lang.module.ModuleReference; +import java.lang.reflect.Method; +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Puts java-spiffe-core, this transport module and their runtime dependencies on the module path, in a + * layer of their own, and opens a channel through core. Defining the layer fails if two modules contain the + * same package, which is what happened when core and the transport modules shared + * io.spiffe.workloadapi.internal. The channel is created through core, so the provider is also found by + * ServiceLoader in that layer. + */ +@EnabledForJreRange(min = JRE.JAVA_9) +class ModulePathTest { + + private static final String FACTORY = "io.spiffe.workloadapi.internal.GrpcManagedChannelFactory"; + + @Test + void channelCanBeCreatedAndClosedFromTheModulePath() throws Exception { + Path[] jars = Arrays.stream(System.getProperty("spiffe.modulepath").split(File.pathSeparator)) + .map(Paths::get) + .toArray(Path[]::new); + ModuleFinder finder = ModuleFinder.of(jars); + Set roots = finder.findAll().stream() + .map(ModuleReference::descriptor) + .map(descriptor -> descriptor.name()) + .collect(Collectors.toSet()); + + Configuration configuration; + try { + configuration = ModuleLayer.boot().configuration() + .resolve(finder, ModuleFinder.of(), roots); + } catch (FindException e) { + // nimbus-jose-jwt still requires jdk.crypto.ec, which JDK 24 folded into java.base + Assumptions.assumeFalse(e.getMessage().contains("jdk.crypto.ec"), e.getMessage()); + throw e; + } + // the platform loader as parent, so the classes of this test's own class path can't be picked up + ModuleLayer layer = ModuleLayer.boot() + .defineModulesWithOneLoader(configuration, ClassLoader.getPlatformClassLoader()); + + ClassLoader loader = layer.modules().stream() + .filter(module -> module.getPackages().contains("io.spiffe.workloadapi.internal")) + .findFirst() + .orElseThrow(() -> new AssertionError("java-spiffe-core is not in the layer")) + .getClassLoader(); + Class factory = loader.loadClass(FACTORY); + Method newChannel = factory.getMethod("newChannel", URI.class, ExecutorService.class); + + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Object wrapper = newChannel.invoke(null, URI.create("tcp://127.0.0.1:8000"), executor); + assertNotNull(wrapper); + + Object channel = wrapper.getClass().getMethod("getChannel").invoke(wrapper); + Method isShutdown = loader.loadClass("io.grpc.ManagedChannel").getMethod("isShutdown"); + assertFalse((Boolean) isShutdown.invoke(channel)); + + wrapper.getClass().getMethod("close").invoke(wrapper); + assertTrue((Boolean) isShutdown.invoke(channel)); + } finally { + executor.shutdownNow(); + } + } +} From e70e4255e66f2a729caffdb2fca00ce4e193f9dc Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Thu, 19 Nov 2026 13:09:28 -0800 Subject: [PATCH 4/4] test(core): resolve system modules and set the module path at configuration time Signed-off-by: Srinivas Chippagiri --- java-spiffe-core/grpc-netty-linux/build.gradle | 4 +--- .../io/spiffe/workloadapi/internal/linux/ModulePathTest.java | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/java-spiffe-core/grpc-netty-linux/build.gradle b/java-spiffe-core/grpc-netty-linux/build.gradle index 07d12450..e3dc4242 100644 --- a/java-spiffe-core/grpc-netty-linux/build.gradle +++ b/java-spiffe-core/grpc-netty-linux/build.gradle @@ -23,7 +23,5 @@ jar { tasks.named('test') { inputs.files(configurations.modulePath) - doFirst { - systemProperty 'spiffe.modulepath', configurations.modulePath.files.join(File.pathSeparator) - } + systemProperty 'spiffe.modulepath', configurations.modulePath.asPath } diff --git a/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java index acfc0a9a..46605683 100644 --- a/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java +++ b/java-spiffe-core/grpc-netty-linux/src/test/java/io/spiffe/workloadapi/internal/linux/ModulePathTest.java @@ -50,9 +50,9 @@ void channelCanBeCreatedAndClosedFromTheModulePath() throws Exception { Configuration configuration; try { configuration = ModuleLayer.boot().configuration() - .resolve(finder, ModuleFinder.of(), roots); + .resolve(finder, ModuleFinder.ofSystem(), roots); } catch (FindException e) { - // nimbus-jose-jwt still requires jdk.crypto.ec, which JDK 24 folded into java.base + // nimbus-jose-jwt still requires jdk.crypto.ec, which JDK 24 folded into java.base, so it is missing there Assumptions.assumeFalse(e.getMessage().contains("jdk.crypto.ec"), e.getMessage()); throw e; }