Skip to content

Commit 3e76bf1

Browse files
jjteo74ejona86
authored andcommitted
netty: Use Java 9 ALPN if available
1 parent b1d62b7 commit 3e76bf1

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

netty/src/main/java/io/grpc/netty/GrpcSslContexts.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ private static ApplicationProtocolConfig selectApplicationProtocolConfig(SslProv
156156
if (JettyTlsUtil.isJettyNpnConfigured()) {
157157
return NPN;
158158
}
159+
if (JettyTlsUtil.isJava9AlpnAvailable()) {
160+
return ALPN;
161+
}
159162
// Use the ALPN cause since it is prefered.
160163
throw new IllegalArgumentException(
161164
"ALPN is not configured properly. See https://github.com/grpc/grpc-java/blob/master/SECURITY.md#troubleshooting"

netty/src/main/java/io/grpc/netty/JettyTlsUtil.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
package io.grpc.netty;
1818

19+
import java.lang.reflect.Method;
20+
import java.security.AccessController;
21+
import java.security.PrivilegedExceptionAction;
22+
import javax.net.ssl.SSLContext;
23+
import javax.net.ssl.SSLEngine;
24+
1925
/**
2026
* Utility class for determining support for Jetty TLS ALPN/NPN.
2127
*/
@@ -26,6 +32,30 @@ private JettyTlsUtil() {
2632
private static Throwable jettyAlpnUnavailabilityCause;
2733
private static Throwable jettyNpnUnavailabilityCause;
2834

35+
private static class Java9AlpnUnavailabilityCauseHolder {
36+
37+
static final Throwable cause = checkAlpnAvailability();
38+
39+
static Throwable checkAlpnAvailability() {
40+
try {
41+
SSLContext context = SSLContext.getInstance("TLS");
42+
context.init(null, null, null);
43+
SSLEngine engine = context.createSSLEngine();
44+
Method getApplicationProtocol =
45+
AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
46+
@Override
47+
public Method run() throws Exception {
48+
return SSLEngine.class.getMethod("getApplicationProtocol");
49+
}
50+
});
51+
getApplicationProtocol.invoke(engine);
52+
return null;
53+
} catch (Throwable t) {
54+
return t;
55+
}
56+
}
57+
}
58+
2959
/**
3060
* Indicates whether or not the Jetty ALPN jar is installed in the boot classloader.
3161
*/
@@ -67,4 +97,15 @@ static synchronized Throwable getJettyNpnUnavailabilityCause() {
6797
}
6898
return jettyNpnUnavailabilityCause;
6999
}
100+
101+
/**
102+
* Indicates whether Java 9 ALPN is available.
103+
*/
104+
static boolean isJava9AlpnAvailable() {
105+
return getJava9AlpnUnavailabilityCause() == null;
106+
}
107+
108+
static Throwable getJava9AlpnUnavailabilityCause() {
109+
return Java9AlpnUnavailabilityCauseHolder.cause;
110+
}
70111
}

netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ static void logSslEngineDetails(Level level, ChannelHandlerContext ctx, String m
374374
builder.append(" Jetty ALPN");
375375
} else if (JettyTlsUtil.isJettyNpnConfigured()) {
376376
builder.append(" Jetty NPN");
377+
} else if (JettyTlsUtil.isJava9AlpnAvailable()) {
378+
builder.append(" JDK9 ALPN");
377379
}
378380
builder.append("\n TLS Protocol: ");
379381
builder.append(engine.getSession().getProtocol());

0 commit comments

Comments
 (0)