Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
Comparing source compatibility of opentelemetry-exporter-otlp-1.69.0-SNAPSHOT.jar against opentelemetry-exporter-otlp-1.68.0.jar
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder setEnabledTlsNamedGroups(java.util.List<java.lang.String>)
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
Comparing source compatibility of opentelemetry-sdk-common-1.69.0-SNAPSHOT.jar against opentelemetry-sdk-common-1.68.0.jar
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.common.export.GrpcSenderConfig (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.util.List<java.lang.String> getEnabledTlsNamedGroups()
+++ NEW ANNOTATION: javax.annotation.Nullable
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.common.export.HttpSenderConfig (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.util.List<java.lang.String> getEnabledTlsNamedGroups()
+++ NEW ANNOTATION: javax.annotation.Nullable
7 changes: 7 additions & 0 deletions exporters/common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import net.ltgt.gradle.errorprone.errorprone

plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
Expand Down Expand Up @@ -115,6 +117,11 @@ tasks {
dependsOn(testing.suites)
}

withType<JavaCompile>().configureEach {
// This check activates when gauvaa is available at compile time. Deactivate because guava is only present due to a transitive compileOnly dependency.
options.errorprone.disable("PreferPreconditions")
}

withType<Test> {
// Allow VarHandle access to String internals
// generally users won't do this and so won't get the VarHandle implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

package io.opentelemetry.exporter.internal;

import static io.opentelemetry.api.internal.Utils.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyStore;
Expand All @@ -23,11 +28,17 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import javax.annotation.Nullable;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509KeyManager;
Expand All @@ -45,6 +56,23 @@ public final class TlsUtil {
private static final String PEM_KEY_FOOTER = "-----END PRIVATE KEY-----";
private static final List<KeyFactory> SUPPORTED_KEY_FACTORIES;

// SSLParameters#setNamedGroups(String[]) was added in JDK 20. Null on older JDKs.
@Nullable private static final Method SET_NAMED_GROUPS_METHOD;
@Nullable private static final Method GET_NAMED_GROUPS_METHOD;

static {
Method setter = null;
Method getter = null;
try {
setter = SSLParameters.class.getMethod("setNamedGroups", String[].class);
getter = SSLParameters.class.getMethod("getNamedGroups");
} catch (NoSuchMethodException e) {
// JDK < 20
}
SET_NAMED_GROUPS_METHOD = setter;
GET_NAMED_GROUPS_METHOD = getter;
}

static {
SUPPORTED_KEY_FACTORIES = new ArrayList<>();
try {
Expand All @@ -61,6 +89,159 @@ public final class TlsUtil {

private TlsUtil() {}

/**
* Returns true if the current JVM supports configuring TLS named groups via {@code
* SSLParameters#setNamedGroups(String[])} (JDK 20+).
*/
public static boolean namedGroupsSupported() {
return SET_NAMED_GROUPS_METHOD != null;
}

/**
* Throws {@link UnsupportedOperationException} if the current JVM does not support configuring
* TLS named groups (requires JDK 20+).
*/
public static void requireNamedGroupsSupported() {
if (SET_NAMED_GROUPS_METHOD == null) {
throw new UnsupportedOperationException(
"Setting TLS named groups requires JDK 20+ (SSLParameters#setNamedGroups is unavailable on this JVM).");
}
}

/** Invokes {@code SSLParameters#setNamedGroups(String[])} reflectively. Requires JDK 20+. */
public static void applyNamedGroups(SSLParameters params, List<String> namedGroups) {
checkArgument(!namedGroups.isEmpty(), "enabledTlsNamedGroups must not be empty");
requireNamedGroupsSupported();
Method setNamedGroups = SET_NAMED_GROUPS_METHOD;
if (setNamedGroups == null) {
// Unreachable: requireNamedGroupsSupported() above throws when null.
throw new UnsupportedOperationException();
}
try {
setNamedGroups.invoke(params, (Object) namedGroups.toArray(new String[0]));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Failed to invoke SSLParameters#setNamedGroups", e);
}
}

/** Throws if the TLS implementation did not retain the configured named groups. */
public static void verifyNamedGroups(SSLParameters params, List<String> namedGroups) {
Method getNamedGroups = GET_NAMED_GROUPS_METHOD;
if (getNamedGroups == null) {
throw new UnsupportedOperationException("Reading TLS named groups requires JDK 20+");
}
String[] actual;
try {
actual = (String[]) getNamedGroups.invoke(params);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Failed to invoke SSLParameters#getNamedGroups", e);
}
if (!Arrays.equals(namedGroups.toArray(new String[0]), actual)) {
throw new IllegalStateException(
"TLS implementation did not retain configured named groups: expected "
+ namedGroups
+ " but was "
+ Arrays.toString(actual));
}
}

/**
* Returns an {@link SSLSocketFactory} that wraps {@code delegate} and configures the given TLS
* named groups on every {@link SSLSocket} it produces. Requires JDK 20+.
*/
public static SSLSocketFactory namedGroupsSslSocketFactory(
SSLSocketFactory delegate, List<String> namedGroups) {
applyNamedGroups(new SSLParameters(), namedGroups);
return new NamedGroupsSslSocketFactory(delegate, namedGroups.toArray(new String[0]));
}

/**
* Returns the platform default {@link SSLSocketFactory} by way of {@link
* SSLContext#getDefault()}.
*/
public static SSLSocketFactory defaultSslSocketFactory() throws SSLException {
try {
return SSLContext.getDefault().getSocketFactory();
} catch (NoSuchAlgorithmException e) {
throw new SSLException("Could not obtain default SSLContext", e);
}
}

private static final class NamedGroupsSslSocketFactory extends SSLSocketFactory {
private final SSLSocketFactory delegate;
private final String[] namedGroups;

NamedGroupsSslSocketFactory(SSLSocketFactory delegate, String[] namedGroups) {
this.delegate = delegate;
this.namedGroups = namedGroups;
}

private Socket configure(Socket socket) {
try {
if (!(socket instanceof SSLSocket)) {
throw new IllegalStateException("Cannot configure TLS named groups on a non-SSL socket");
}
SSLSocket sslSocket = (SSLSocket) socket;
SSLParameters params = sslSocket.getSSLParameters();
List<String> groups = Arrays.asList(namedGroups);
applyNamedGroups(params, groups);
sslSocket.setSSLParameters(params);
verifyNamedGroups(sslSocket.getSSLParameters(), groups);
return socket;
} catch (RuntimeException e) {
try {
socket.close();
} catch (IOException closeException) {
e.addSuppressed(closeException);
}
throw e;
}
}

@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}

@Override
public Socket createSocket() throws IOException {
return configure(delegate.createSocket());
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose)
throws IOException {
return configure(delegate.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException {
return configure(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException {
return configure(delegate.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return configure(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(
InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return configure(delegate.createSocket(address, port, localAddress, localPort));
}
}

/**
* Creates {@link KeyManager} initiated by keystore containing single private key with matching
* certificate chain.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.linecorp.armeria.testing.junit5.server.SelfSignedCertificateExtension;
import java.net.InetAddress;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@EnabledForJreRange(minVersion = 20)
class TlsNamedGroupsTest {

@RegisterExtension
static final SelfSignedCertificateExtension certificate = new SelfSignedCertificateExtension();

private static final List<String> GROUPS = Collections.singletonList("secp256r1");

@Test
void socketFactory_rejectsDiscardedGroupsAndClosesSocket() throws Exception {
SSLSocketFactory delegate = mock(SSLSocketFactory.class);
SSLSocket socket = mock(SSLSocket.class);
when(delegate.createSocket()).thenReturn(socket);
when(socket.getSSLParameters()).thenReturn(new SSLParameters(), new SSLParameters());

SSLSocketFactory factory = TlsUtil.namedGroupsSslSocketFactory(delegate, GROUPS);
assertThatThrownBy(factory::createSocket)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("did not retain configured named groups");
verify(socket).close();
}

@Test
void socketFactory_rejectsEmptyGroups() throws Exception {
assertThatThrownBy(() ->
TlsUtil.namedGroupsSslSocketFactory(
SSLContext.getDefault().getSocketFactory(), Collections.emptyList()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("enabledTlsNamedGroups must not be empty");
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void socketFactory_enforcesNamedGroups(boolean matchingGroups) throws Exception {
SSLContext serverContext = SSLContext.getInstance("TLS");
serverContext.init(
new KeyManager[] {
TlsUtil.keyManager(
certificate.privateKey().getEncoded(), certificate.certificate().getEncoded())
},
null,
null);
SSLContext clientContext = SSLContext.getInstance("TLS");
clientContext.init(
null,
new TrustManager[] {TlsUtil.trustManager(certificate.certificate().getEncoded())},
null);

try (SSLServerSocket server =
(SSLServerSocket)
serverContext
.getServerSocketFactory()
.createServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
server.setSoTimeout(5000);
SSLParameters params = server.getSSLParameters();
params.setProtocols(new String[] {"TLSv1.3"});
TlsUtil.applyNamedGroups(params, GROUPS);
server.setSSLParameters(params);
CompletableFuture<Void> serverHandshake = new CompletableFuture<>();
Thread serverThread =
new Thread(() -> {
try (SSLSocket socket = (SSLSocket) server.accept()) {
socket.setSoTimeout(5000);
socket.startHandshake();
serverHandshake.complete(null);
} catch (Exception e) {
serverHandshake.completeExceptionally(e);
}
});
serverThread.setDaemon(true);
serverThread.start();

SSLSocketFactory factory =
TlsUtil.namedGroupsSslSocketFactory(
clientContext.getSocketFactory(),
matchingGroups ? GROUPS : Collections.singletonList("secp384r1"));
try (SSLSocket socket =
(SSLSocket)
factory.createSocket(InetAddress.getLoopbackAddress(), server.getLocalPort())) {
socket.setSoTimeout(5000);
socket.setEnabledProtocols(new String[] {"TLSv1.3"});
if (matchingGroups) {
socket.startHandshake();
assertThat(serverHandshake.get(5, TimeUnit.SECONDS)).isNull();
} else {
assertThatThrownBy(socket::startHandshake).isInstanceOf(SSLException.class);
assertThatThrownBy(() -> serverHandshake.get(5, TimeUnit.SECONDS))
.hasRootCauseInstanceOf(SSLException.class);
}
}
}
}
}
Loading
Loading