Skip to content

core, netty: allow InputStream based certs #4316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2018
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
17 changes: 17 additions & 0 deletions core/src/main/java/io/grpc/ServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc;

import java.io.File;
import java.io.InputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -150,6 +151,22 @@ public T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
*/
public abstract T useTransportSecurity(File certChain, File privateKey);

/**
* Makes the server use TLS.
*
* @param certChain InputStream containing the full certificate chain
* @param privateKey InputStream containing the private key
*
* @return this
* @throws UnsupportedOperationException if the server does not support TLS, or does not support
* reading these files from an InputStream.
* @since 1.12.0
*/
public T useTransportSecurity(InputStream certChain, InputStream privateKey) {
throw new UnsupportedOperationException();
}


/**
* Set the decompression registry for use in the channel. This is an advanced API call and
* shouldn't be used unless you are using custom message encoding. The default supported
Expand Down
22 changes: 22 additions & 0 deletions netty/src/main/java/io/grpc/netty/GrpcSslContexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.Provider;
Expand Down Expand Up @@ -140,6 +141,27 @@ public static SslContextBuilder forServer(
return configure(SslContextBuilder.forServer(keyCertChainFile, keyFile, keyPassword));
}

/**
* Creates a SslContextBuilder with ciphers and APN appropriate for gRPC.
*
* @see SslContextBuilder#forServer(InputStream, InputStream)
* @see #configure(SslContextBuilder)
*/
public static SslContextBuilder forServer(InputStream keyCertChain, InputStream key) {
return configure(SslContextBuilder.forServer(keyCertChain, key));
}

/**
* Creates a SslContextBuilder with ciphers and APN appropriate for gRPC.
*
* @see SslContextBuilder#forServer(InputStream, InputStream, String)
* @see #configure(SslContextBuilder)
*/
public static SslContextBuilder forServer(
InputStream keyCertChain, InputStream key, String keyPassword) {
return configure(SslContextBuilder.forServer(keyCertChain, key, keyPassword));
}

/**
* Set ciphers and APN appropriate for gRPC. Precisely what is set is permitted to change, so if
* an application requires particular settings it should override the options set here.
Expand Down
12 changes: 12 additions & 0 deletions netty/src/main/java/io/grpc/netty/NettyServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslContext;
import java.io.File;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.HashMap;
Expand Down Expand Up @@ -438,4 +439,15 @@ public NettyServerBuilder useTransportSecurity(File certChain, File privateKey)
}
return this;
}

@Override
public NettyServerBuilder useTransportSecurity(InputStream certChain, InputStream privateKey) {
try {
sslContext = GrpcSslContexts.forServer(certChain, privateKey).build();
} catch (SSLException e) {
// This should likely be some other, easier to catch exception.
throw new RuntimeException(e);
}
return this;
}
}
1 change: 0 additions & 1 deletion netty/src/test/java/io/grpc/netty/TlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public void basicClientServerIntegrationTest() throws Exception {
client.unaryRpc(SimpleRequest.getDefaultInstance());
}


/**
* Tests that a server configured to require client authentication refuses to accept connections
* from a client that has an untrusted certificate.
Expand Down