Skip to content

Commit

Permalink
[DO NOT MERGE] Convert PKCS#1 to PKCS#8 into Java code only
Browse files Browse the repository at this point in the history
We depend on external libraries that we are not confident about yet
in order to do the conversion. Not sure, whether this is a good idea.
  • Loading branch information
mangalaman93 committed Feb 17, 2020
1 parent 85cfd64 commit 8b38008
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ dependencies {
compile "org.slf4j:slf4j-simple:1.7.21"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"

testRuntime 'org.bouncycastle:bcprov-jdk15on:1.58'
testCompile 'com.github.martinpaljak:esteid:17.11.26.1'
testRuntime 'io.netty:netty-tcnative-boringssl-static:2.0.28.Final'
testCompile "io.opencensus:opencensus-api:${openCensusVersion}"
testCompile "io.opencensus:opencensus-exporter-trace-jaeger:${openCensusVersion}"
testRuntime "io.opencensus:opencensus-impl:${openCensusVersion}"
Expand Down
79 changes: 68 additions & 11 deletions src/test/java/io/dgraph/DgraphIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@
import io.dgraph.DgraphProto.Operation;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.concurrent.TimeUnit;

import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterClass;
Expand All @@ -34,17 +48,10 @@ public abstract class DgraphIntegrationTest {
private static ManagedChannel channel1, channel2, channel3;

@BeforeClass
public static void beforeClass() throws InterruptedException {
channel1 = ManagedChannelBuilder.forAddress("localhost", 9180).usePlaintext().build();
DgraphGrpc.DgraphStub stub1 = DgraphGrpc.newStub(channel1);

channel2 = ManagedChannelBuilder.forAddress("localhost", 9182).usePlaintext().build();
DgraphGrpc.DgraphStub stub2 = DgraphGrpc.newStub(channel2);

channel3 = ManagedChannelBuilder.forAddress("localhost", 9183).usePlaintext().build();
DgraphGrpc.DgraphStub stub3 = DgraphGrpc.newStub(channel3);

dgraphClient = new DgraphClient(stub1, stub2, stub3);
public static void beforeClass() throws InterruptedException, IOException {
String baseCertPath = "/home/aman/gocode/src/github.com/dgraph-io/dgraph/tlstest/tls";
setupTLSClient(baseCertPath);
// setupClient();

boolean succeed = false;
boolean retry;
Expand Down Expand Up @@ -82,6 +89,56 @@ public static void beforeClass() throws InterruptedException {
dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());
}

private static void setupTLSClient(String baseCertPath) throws IOException {
// convert PKCS#1 to PKCS#8
PEMParser pemParser = new PEMParser(new FileReader(baseCertPath + "/client.acl.key"));
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
Object object = pemParser.readObject();
KeyPair pair = converter.getKeyPair((PEMKeyPair) object);
PrivateKey priv = pair.getPrivate();
byte[] privBytes = priv.getEncoded();

// PEM object from PKCS#8
PemObject pemObject = new PemObject("RSA PRIVATE KEY", privBytes);
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.close();
String pemString = stringWriter.toString();

// Setup SSL context with keys and certificates
SslContextBuilder builder = GrpcSslContexts.forClient();
builder.trustManager(new File(baseCertPath + "/ca.crt"));
builder.keyManager(
new FileInputStream(baseCertPath + "/client.acl.crt"),
new ByteArrayInputStream(pemString.getBytes(StandardCharsets.UTF_8)));
SslContext sslContext = builder.build();

channel1 = NettyChannelBuilder.forAddress("localhost", 9180).sslContext(sslContext).build();
DgraphGrpc.DgraphStub stub1 = DgraphGrpc.newStub(channel1);

channel2 = NettyChannelBuilder.forAddress("localhost", 9182).sslContext(sslContext).build();
DgraphGrpc.DgraphStub stub2 = DgraphGrpc.newStub(channel2);

channel3 = NettyChannelBuilder.forAddress("localhost", 9183).sslContext(sslContext).build();
DgraphGrpc.DgraphStub stub3 = DgraphGrpc.newStub(channel3);

dgraphClient = new DgraphClient(stub1, stub2, stub3);
}

private static void setupClient() {
channel1 = ManagedChannelBuilder.forAddress("localhost", 9180).usePlaintext().build();
DgraphGrpc.DgraphStub stub1 = DgraphGrpc.newStub(channel1);

channel2 = ManagedChannelBuilder.forAddress("localhost", 9182).usePlaintext().build();
DgraphGrpc.DgraphStub stub2 = DgraphGrpc.newStub(channel2);

channel3 = ManagedChannelBuilder.forAddress("localhost", 9183).usePlaintext().build();
DgraphGrpc.DgraphStub stub3 = DgraphGrpc.newStub(channel3);

dgraphClient = new DgraphClient(stub1, stub2, stub3);
}

@AfterClass
public static void afterClass() throws InterruptedException {
channel1.shutdown().awaitTermination(5, TimeUnit.SECONDS);
Expand Down

0 comments on commit 8b38008

Please sign in to comment.