Adding support for TLS connections for gRPC clients
This patch uses BoringSSL to negotiate TLS sessions under the hood.
Change-Id: I45b6871eb341bd67a230035a930f6a016d105e97
diff --git a/core/store/dist/BUILD b/core/store/dist/BUILD
index 98eefbb..a902a8c 100644
--- a/core/store/dist/BUILD
+++ b/core/store/dist/BUILD
@@ -3,10 +3,10 @@
"//utils/rest:onlab-rest",
"//core/store/serializers:onos-core-serializers",
"@io_netty_netty_transport//jar",
- "@io_netty_netty_codec//jar",
- "@io_netty_netty_handler//jar",
"@io_netty_netty_transport_native_epoll//jar",
"@io_netty_netty_transport_native_unix_common//jar",
+ "@io_netty_netty_codec//jar",
+ "@io_netty_netty_handler//jar",
"@io_netty_netty_resolver//jar",
"@commons_math3//jar",
"//incubator/api:onos-incubator-api",
diff --git a/lib/BUCK b/lib/BUCK
index 788ada0..1ad907a 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Thu, 18 Oct 2018 20:41:34 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Thu, 13 Dec 2018 23:59:38 GMT. Do not edit this file manually. *****
# ***** Use onos-lib-gen *****
pass_thru_pom(
@@ -1069,6 +1069,15 @@
)
remote_jar (
+ name = 'io_netty_netty_tcnative_boringssl',
+ out = 'netty-tcnative-boringssl-static-2.0.12.Final.jar',
+ url = 'mvn:io.netty:netty-tcnative-boringssl-static:jar:2.0.12.Final',
+ sha1 = 'b884be1450a7fd0854b98743836b8ccb0dfd75a4',
+ maven_coords = 'io.netty:netty-tcnative-boringssl-static:2.0.12.Final',
+ visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
name = 'io_netty_netty_transport',
out = 'netty-transport-4.1.27.Final.jar',
url = 'mvn:io.netty:netty-transport:jar:4.1.27.Final',
diff --git a/lib/deps.json b/lib/deps.json
index af2c465..4946550 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -234,6 +234,7 @@
"io_netty_netty_common": "mvn:io.netty:netty-common:4.1.27.Final",
"io_netty_netty_handler": "mvn:io.netty:netty-handler:4.1.27.Final",
"io_netty_netty_handler_proxy": "mvn:io.netty:netty-handler-proxy:4.1.27.Final",
+ "io_netty_netty_tcnative_boringssl": "mvn:io.netty:netty-tcnative-boringssl-static:2.0.12.Final",
"io_netty_netty_transport": "mvn:io.netty:netty-transport:4.1.27.Final",
"io_netty_netty_transport_native_unix_common": "mvn:io.netty:netty-transport-native-unix-common:4.1.27.Final",
"io_netty_netty_transport-native-epoll": "mvn:io.netty:netty-transport-native-epoll:4.1.27.Final",
diff --git a/protocols/grpc/BUILD b/protocols/grpc/BUILD
index a01155b..155fe4e 100644
--- a/protocols/grpc/BUILD
+++ b/protocols/grpc/BUILD
@@ -18,18 +18,11 @@
"@io_opencensus_opencensus_api//jar",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
"@com_google_code_gson_gson//jar",
- # Lazily adding all netty-related packages.
- # Some of them might not be necessary.
- "@io_netty_netty//jar",
- "@io_netty_netty_buffer//jar",
- "@io_netty_netty_codec//jar",
+ # Adding Netty http and http2 codecs
+ # The rest of Netty is loaded in the onos-thirdparty-base feature
+ # See tools/package/features/BUILD for more details
"@io_netty_netty_codec_http//jar",
"@io_netty_netty_codec_http2//jar",
- "@io_netty_netty_common//jar",
- "@io_netty_netty_handler//jar",
- "@io_netty_netty_transport//jar",
- "@io_netty_netty_transport_native_epoll//jar",
- "@io_netty_netty_resolver//jar",
]
onos_app(
diff --git a/protocols/grpc/ctl/BUILD b/protocols/grpc/ctl/BUILD
index ac0703d..475a90e 100644
--- a/protocols/grpc/ctl/BUILD
+++ b/protocols/grpc/ctl/BUILD
@@ -3,6 +3,7 @@
"//protocols/grpc/proto:onos-protocols-grpc-proto",
"@io_grpc_grpc_java//core",
"@io_grpc_grpc_java//netty",
+ "@io_netty_netty_handler//jar",
]
osgi_jar(
diff --git a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/AbstractGrpcClientController.java b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/AbstractGrpcClientController.java
index f82d2f8..0be2148 100644
--- a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/AbstractGrpcClientController.java
+++ b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/AbstractGrpcClientController.java
@@ -19,8 +19,11 @@
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Striped;
import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
+import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
+import io.netty.handler.ssl.NotSslRecordException;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -37,12 +40,14 @@
import org.onosproject.net.DeviceId;
import org.slf4j.Logger;
+import javax.net.ssl.SSLException;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.function.Supplier;
import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
import static org.slf4j.LoggerFactory.getLogger;
/**
@@ -93,10 +98,15 @@
@Override
public boolean createClient(K clientKey) {
checkNotNull(clientKey);
- return withDeviceLock(() -> doCreateClient(clientKey), clientKey.deviceId());
+ /*
+ FIXME we might want to move "useTls" and "fallback" to properties of the netcfg and clientKey
+ For now, we will first try to connect with TLS (accepting any cert), then fall back to
+ plaintext for every device
+ */
+ return withDeviceLock(() -> doCreateClient(clientKey, true, true), clientKey.deviceId());
}
- private boolean doCreateClient(K clientKey) {
+ private boolean doCreateClient(K clientKey, boolean useTls, boolean fallbackToPlainText) {
DeviceId deviceId = clientKey.deviceId();
String serverAddr = clientKey.serverAddr();
int serverPort = clientKey.serverPort();
@@ -114,20 +124,57 @@
doRemoveClient(deviceId);
}
}
- log.info("Creating client for {} (server={}:{})...",
- deviceId, serverAddr, serverPort);
+ log.info("Creating client for {} (server={}:{})...", deviceId, serverAddr, serverPort);
+
+ SslContext sslContext = null;
+ if (useTls) {
+ try {
+ // Accept any server certificate; this is insecure and should not be used in production
+ sslContext = GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
+ } catch (SSLException e) {
+ log.error("Failed to build SSL Context", e);
+ return false;
+ }
+ }
+
GrpcChannelId channelId = GrpcChannelId.of(clientKey.deviceId(), clientKey.toString());
- ManagedChannelBuilder channelBuilder = NettyChannelBuilder
+ NettyChannelBuilder channelBuilder = NettyChannelBuilder
.forAddress(serverAddr, serverPort)
- .maxInboundMessageSize(DEFAULT_MAX_INBOUND_MSG_SIZE * MEGABYTES)
- .usePlaintext();
+ .maxInboundMessageSize(DEFAULT_MAX_INBOUND_MSG_SIZE * MEGABYTES);
+ if (sslContext != null) {
+ log.debug("Using SSL for gRPC connection to {}", deviceId);
+ channelBuilder
+ .sslContext(sslContext)
+ .useTransportSecurity();
+ } else {
+ checkState(!useTls,
+ "Not authorized to use plaintext for gRPC connection to {}", deviceId);
+ log.debug("Using plaintext TCP for gRPC connection to {}", deviceId);
+ channelBuilder.usePlaintext();
+ }
ManagedChannel channel;
try {
channel = grpcChannelController.connectChannel(channelId, channelBuilder);
} catch (IOException e) {
- log.warn("Unable to connect to gRPC server of {}: {}",
- clientKey.deviceId(), e.getMessage());
+ for (Throwable cause = e; cause != null; cause = cause.getCause()) {
+ if (useTls && cause instanceof NotSslRecordException) {
+ // Likely root cause is that server is using plaintext
+ log.info("Failed to connect to server (device={}) using TLS", deviceId);
+ log.debug("TLS connection exception", e);
+ if (fallbackToPlainText) {
+ log.info("Falling back to plaintext for connection to {}", deviceId);
+ return doCreateClient(clientKey, false, false);
+ }
+ }
+ if (!useTls && "Connection reset by peer".equals(cause.getMessage())) {
+ // Not a great signal, but could indicate the server is expected a TLS connection
+ log.error("Failed to connect to server (device={}) using plaintext TCP; is the server using TLS?",
+ deviceId);
+ break;
+ }
+ }
+ log.warn("Unable to connect to gRPC server for {}", deviceId, e);
return false;
}
diff --git a/tools/build/bazel/generate_workspace.bzl b/tools/build/bazel/generate_workspace.bzl
index 7981ee9..cde07d5 100644
--- a/tools/build/bazel/generate_workspace.bzl
+++ b/tools/build/bazel/generate_workspace.bzl
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Thu, 18 Oct 2018 20:40:56 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Thu, 13 Dec 2018 23:58:49 GMT. Do not edit this file manually. *****
# ***** Use onos-lib-gen *****
load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION")
@@ -705,6 +705,12 @@
jar_sha256 = "84b00dd1cd25a99b88bd598577825b4be9ad592e2d78b08bd703e7e999fe3498",
licenses = ["notice"],
jar_urls = ["http://repo1.maven.org/maven2/io/netty/netty-handler-proxy/4.1.27.Final/netty-handler-proxy-4.1.27.Final.jar"], )
+ if "io_netty_netty_tcnative_boringssl" not in native.existing_rules():
+ java_import_external(
+ name = "io_netty_netty_tcnative_boringssl",
+ jar_sha256 = "3df756e569504137e90ff368c2fe09f1f953efeddb717d47ed391dfa6ba8b7e3",
+ licenses = ["notice"],
+ jar_urls = ["http://repo1.maven.org/maven2/io/netty/netty-tcnative-boringssl-static/2.0.12.Final/netty-tcnative-boringssl-static-2.0.12.Final.jar"], )
if "io_netty_netty_transport" not in native.existing_rules():
java_import_external(
name = "io_netty_netty_transport",
@@ -1419,6 +1425,7 @@
artifact_map["@io_netty_netty_common//:io_netty_netty_common"] = "mvn:io.netty:netty-common:jar:4.1.27.Final"
artifact_map["@io_netty_netty_handler//:io_netty_netty_handler"] = "mvn:io.netty:netty-handler:jar:4.1.27.Final"
artifact_map["@io_netty_netty_handler_proxy//:io_netty_netty_handler_proxy"] = "mvn:io.netty:netty-handler-proxy:jar:4.1.27.Final"
+artifact_map["@io_netty_netty_tcnative_boringssl//:io_netty_netty_tcnative_boringssl"] = "mvn:io.netty:netty-tcnative-boringssl-static:jar:2.0.12.Final"
artifact_map["@io_netty_netty_transport//:io_netty_netty_transport"] = "mvn:io.netty:netty-transport:jar:4.1.27.Final"
artifact_map["@io_netty_netty_transport_native_unix_common//:io_netty_netty_transport_native_unix_common"] = "mvn:io.netty:netty-transport-native-unix-common:jar:4.1.27.Final"
artifact_map["@io_netty_netty_transport_native_epoll//:io_netty_netty_transport_native_epoll"] = "mvn:io.netty:netty-transport-native-epoll:jar:4.1.27.Final"
diff --git a/tools/package/features/BUILD b/tools/package/features/BUILD
index 1ebd65c..fab4bcf 100644
--- a/tools/package/features/BUILD
+++ b/tools/package/features/BUILD
@@ -19,9 +19,10 @@
"@io_netty_netty//jar",
"@io_netty_netty_common//jar",
"@io_netty_netty_buffer//jar",
- "@io_netty_netty_transport//jar",
"@io_netty_netty_handler//jar",
+ "@io_netty_netty_tcnative_boringssl//jar",
"@io_netty_netty_codec//jar",
+ "@io_netty_netty_transport//jar",
"@io_netty_netty_transport_native_epoll//jar",
"@io_netty_netty_transport_native_unix_common//jar",
"@io_netty_netty_resolver//jar",