mirror of https://github.com/grpc/grpc-java.git
okhttp: Enable TLS for Http2OkHttpTest.
This commit is contained in:
parent
71447ce7af
commit
2c7536c8fd
|
|
@ -38,20 +38,11 @@ import io.grpc.transport.netty.NettyChannelBuilder;
|
||||||
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
|
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
|
||||||
import io.netty.handler.ssl.SslContext;
|
import io.netty.handler.ssl.SslContext;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.KeyStore;
|
|
||||||
import java.security.cert.CertificateFactory;
|
|
||||||
import java.security.cert.X509Certificate;
|
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import javax.net.ssl.TrustManagerFactory;
|
|
||||||
import javax.security.auth.x500.X500Principal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application that starts a client for the {@link TestServiceGrpc.TestService} and runs through a
|
* Application that starts a client for the {@link TestServiceGrpc.TestService} and runs through a
|
||||||
|
|
@ -242,7 +233,10 @@ public class TestServiceClient {
|
||||||
}
|
}
|
||||||
if (useTls) {
|
if (useTls) {
|
||||||
try {
|
try {
|
||||||
builder.sslSocketFactory(getSslSocketFactory());
|
SSLSocketFactory factory = useTestCa
|
||||||
|
? Util.getSslSocketFactoryForCertainCert(Util.loadCert("ca.pem"))
|
||||||
|
: (SSLSocketFactory) SSLSocketFactory.getDefault();
|
||||||
|
builder.sslSocketFactory(factory);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
@ -250,27 +244,5 @@ public class TestServiceClient {
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SSLSocketFactory getSslSocketFactory() throws Exception {
|
|
||||||
if (!useTestCa) {
|
|
||||||
return (SSLSocketFactory) SSLSocketFactory.getDefault();
|
|
||||||
}
|
|
||||||
File certChainFile = Util.loadCert("ca.pem");
|
|
||||||
KeyStore ks = KeyStore.getInstance("JKS");
|
|
||||||
ks.load(null, null);
|
|
||||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
|
||||||
X509Certificate cert = (X509Certificate) cf.generateCertificate(
|
|
||||||
new BufferedInputStream(new FileInputStream(certChainFile)));
|
|
||||||
X500Principal principal = cert.getSubjectX500Principal();
|
|
||||||
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
|
|
||||||
|
|
||||||
// Set up trust manager factory to use our key store.
|
|
||||||
TrustManagerFactory trustManagerFactory =
|
|
||||||
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
||||||
trustManagerFactory.init(ks);
|
|
||||||
SSLContext context = SSLContext.getInstance("TLS");
|
|
||||||
context.init(null, trustManagerFactory.getTrustManagers(), null);
|
|
||||||
return context.getSocketFactory();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,14 +38,24 @@ import io.grpc.protobuf.ProtoUtils;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.cert.CertificateFactory;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
import javax.security.auth.x500.X500Principal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility methods to support integration testing.
|
* Utility methods to support integration testing.
|
||||||
*/
|
*/
|
||||||
|
|
@ -119,4 +129,26 @@ public class Util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a SSLSocketFactory which uses the certificate specified in certChainFile.
|
||||||
|
*/
|
||||||
|
public static SSLSocketFactory getSslSocketFactoryForCertainCert(File certChainFile)
|
||||||
|
throws Exception {
|
||||||
|
KeyStore ks = KeyStore.getInstance("JKS");
|
||||||
|
ks.load(null, null);
|
||||||
|
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||||
|
X509Certificate cert = (X509Certificate) cf.generateCertificate(
|
||||||
|
new BufferedInputStream(new FileInputStream(certChainFile)));
|
||||||
|
X500Principal principal = cert.getSubjectX500Principal();
|
||||||
|
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
|
||||||
|
|
||||||
|
// Set up trust manager factory to use our key store.
|
||||||
|
TrustManagerFactory trustManagerFactory =
|
||||||
|
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||||
|
trustManagerFactory.init(ks);
|
||||||
|
SSLContext context = SSLContext.getInstance("TLS");
|
||||||
|
context.init(null, trustManagerFactory.getTrustManagers(), null);
|
||||||
|
return context.getSocketFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@
|
||||||
package io.grpc.testing.integration;
|
package io.grpc.testing.integration;
|
||||||
|
|
||||||
import io.grpc.ChannelImpl;
|
import io.grpc.ChannelImpl;
|
||||||
|
import io.grpc.transport.netty.GrpcSslContexts;
|
||||||
import io.grpc.transport.netty.NettyServerBuilder;
|
import io.grpc.transport.netty.NettyServerBuilder;
|
||||||
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
|
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
|
||||||
|
|
||||||
|
|
@ -40,6 +41,8 @@ import org.junit.BeforeClass;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for GRPC over Http2 using the OkHttp framework.
|
* Integration tests for GRPC over Http2 using the OkHttp framework.
|
||||||
*/
|
*/
|
||||||
|
|
@ -47,9 +50,16 @@ import org.junit.runners.JUnit4;
|
||||||
public class Http2OkHttpTest extends AbstractTransportTest {
|
public class Http2OkHttpTest extends AbstractTransportTest {
|
||||||
private static int serverPort = Util.pickUnusedPort();
|
private static int serverPort = Util.pickUnusedPort();
|
||||||
|
|
||||||
|
/** Starts the server with HTTPS. */
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void startServer() throws Exception {
|
public static void startServer() throws Exception {
|
||||||
startStaticServer(NettyServerBuilder.forPort(serverPort));
|
try {
|
||||||
|
startStaticServer(NettyServerBuilder.forPort(serverPort)
|
||||||
|
.sslContext(GrpcSslContexts.forServer(
|
||||||
|
Util.loadCert("server1.pem"), Util.loadCert("server1.key")).build()));
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
|
|
@ -59,6 +69,13 @@ public class Http2OkHttpTest extends AbstractTransportTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ChannelImpl createChannel() {
|
protected ChannelImpl createChannel() {
|
||||||
return OkHttpChannelBuilder.forAddress("127.0.0.1", serverPort).build();
|
OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("127.0.0.1", serverPort)
|
||||||
|
.overrideHostForAuthority("foo.test.google.fr");
|
||||||
|
try {
|
||||||
|
builder.sslSocketFactory(Util.getSslSocketFactoryForCertainCert(Util.loadCert("ca.pem")));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue