Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). Upgrade `java-spiffe-core` and the `grpc-netty-*` artifact together, since an older `grpc-netty-*` jar still contains the shared package

### Build

Expand Down
10 changes: 4 additions & 6 deletions java-spiffe-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
18 changes: 18 additions & 0 deletions java-spiffe-core/grpc-netty-linux/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
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'))

implementation "io.grpc:grpc-netty-shaded:${grpcVersion}"
}

jar {
archiveClassifier = ''
}

tasks.named('test') {
inputs.files(configurations.modulePath)
systemProperty 'spiffe.modulepath', configurations.modulePath.asPath
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.spiffe.workloadapi.internal.linux.LinuxGrpcChannelProvider
Original file line number Diff line number Diff line change
@@ -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<GrpcChannelProvider> 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());
}
}
Loading
Loading