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 Jul 8, 2024
1 parent 6d1f81a commit c00bb10
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ dependencies {
compile "io.grpc:grpc-netty:${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 @@ -35,17 +49,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 @@ -83,6 +90,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 {
// Shutdown channel connections
Expand Down

0 comments on commit c00bb10

Please sign in to comment.