Skip to content

Commit

Permalink
FABJ-406 Service Discovery TLS Error Using Cert Bytes
Browse files Browse the repository at this point in the history
Change-Id: I20d71f72ce2a19de51c903f230514eb6a56c8507
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Mar 1, 2019
1 parent d69047f commit dbc41c1
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 31 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ On the Peer or Orderer add the property `grpc.NettyChannelBuilderOption.maxInbou
See [End2endIT's constructChannel](https://github.com/hyperledger/fabric-sdk-java/blob/b649868113e969d851720c972f660114b64247bc/src/test/java/org/hyperledger/fabric/sdkintegration/End2endIT.java#L846)


### Configuration and setting default values - timeouts etc

The SDK's defaults are all in the file [Config.java](https://github.com/hyperledger/fabric-sdk-java/blob/a2140f9bba57a63c58d9ee8579fea7164bf3beb2/src/main/java/org/hyperledger/fabric/sdk/helper/Config.java#L33-L40)
The [config.properties](https://github.com/hyperledger/fabric-sdk-java/blob/a2140f9bba57a63c58d9ee8579fea7164bf3beb2/config.properties)
also has some descriptions on what they do. Most server timeout request can be overridden with the specific request too.

### java.security.InvalidKeyException: Illegal key size

If you get this error, this means your JDK does not capable of handling unlimited strength crypto algorithms. To fix this issue, You will need to download the JCE libraries for your version of JDK. Please follow the instructions <a href="http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters">here</a> to download and install the JCE for your version of the JDK.
Expand Down
51 changes: 23 additions & 28 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
if (peerOptions.getPeerRoles().contains(PeerRole.SERVICE_DISCOVERY)) {

final Properties properties = peer.getProperties();
if ((properties == null) || (isNullOrEmpty(properties.getProperty("clientCertFile")) &&
isNullOrEmpty(properties.getProperty("clientCertBytes")))) {
if ((properties == null) || properties.isEmpty() || (isNullOrEmpty(properties.getProperty("clientCertFile")) &&
!properties.containsKey("clientCertBytes"))) {
TLSCertificateBuilder tlsCertificateBuilder = new TLSCertificateBuilder();
TLSCertificateKeyPair tlsCertificateKeyPair = tlsCertificateBuilder.clientCert();
peer.setTLSCertificateKeyPair(tlsCertificateKeyPair);
Expand Down Expand Up @@ -1413,30 +1413,30 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In
final String endpoint = sdOrdererAdditionInfo.getEndpoint();
final String mspid = sdOrdererAdditionInfo.getMspId();

String protocol = findClientProp(config, "protocol", mspid, endpoint, "grpcs:");
String protocol = (String) findClientProp(config, "protocol", mspid, endpoint, "grpcs:");

String clientCertFile = findClientProp(config, "clientCertFile", mspid, endpoint, null);
String clientCertFile = (String) findClientProp(config, "clientCertFile", mspid, endpoint, null);

if (null != clientCertFile) {
properties.put("clientCertFile", clientCertFile);
}

String clientKeyFile = findClientProp(config, "clientKeyFile", mspid, endpoint, null);
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
if (null != clientKeyFile) {
properties.put("clientKeyFile", clientKeyFile);
}

String clientCertBytes = findClientProp(config, "clientCertBytes", mspid, endpoint, null);
byte[] clientCertBytes = (byte[]) findClientProp(config, "clientCertBytes", mspid, endpoint, null);
if (null != clientCertBytes) {
properties.put("clientCertBytes", clientCertBytes);
}

String clientKeyBytes = findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
if (null != clientKeyBytes) {
properties.put("clientKeyBytes", clientKeyBytes);
}

String hostnameOverride = findClientProp(config, "hostnameOverride", mspid, endpoint, null);
String hostnameOverride = (String) findClientProp(config, "hostnameOverride", mspid, endpoint, null);
if (null != hostnameOverride) {
properties.put("hostnameOverride", hostnameOverride);
}
Expand Down Expand Up @@ -1470,36 +1470,32 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
final String endpoint = sdPeerAddition.getEndpoint();
final String mspid = sdPeerAddition.getMspId();

String protocol = findClientProp(config, "protocol", mspid, endpoint, "grpcs:");

String clientCertFile = findClientProp(config, "clientCertFile", mspid, endpoint, null);
String protocol = (String) findClientProp(config, "protocol", mspid, endpoint, "grpcs:");

Peer peer = sdPeerAddition.getEndpointMap().get(endpoint); // maybe there already.
if (null != peer) {
return peer;

}

if (null != clientCertFile) {
properties.put("clientCertFile", clientCertFile);
}
String clientCertFile = (String) findClientProp(config, "clientCertFile", mspid, endpoint, null);

String clientKeyFile = findClientProp(config, "clientKeyFile", mspid, endpoint, null);
if (null != clientKeyFile) {
properties.put("clientKeyFile", clientKeyFile);
}

String clientCertBytes = findClientProp(config, "clientCertBytes", mspid, endpoint, null);
byte[] clientCertBytes = (byte[]) findClientProp(config, "clientCertBytes", mspid, endpoint, null);
if (null != clientCertBytes) {
properties.put("clientCertBytes", clientCertBytes);
} else if (null != clientCertFile) {
properties.put("clientCertFile", clientCertFile);
}

String clientKeyBytes = findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
if (null != clientKeyBytes) {
properties.put("clientKeyBytes", clientKeyBytes);
} else if (null != clientKeyFile) {
properties.put("clientKeyFile", clientKeyFile);
}

String hostnameOverride = findClientProp(config, "hostnameOverride", mspid, endpoint, null);
String hostnameOverride = (String) findClientProp(config, "hostnameOverride", mspid, endpoint, null);
if (null != hostnameOverride) {
properties.put("hostnameOverride", hostnameOverride);
}
Expand All @@ -1520,16 +1516,15 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
}
}

static String findClientProp(Properties config, final String prop, final String mspid, final String endpoint, String def) {
static Object findClientProp(Properties config, final String prop, final String mspid, final String endpoint, String def) {
final String[] split = endpoint.split(":");
final String endpointHost = split[0];

String ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.default." + prop, def);
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.mspid." + prop + "." + mspid, ret);
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpointHost, ret);
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpoint, ret);
Object ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.default." + prop, def);
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.mspid." + prop + "." + mspid, ret);
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpointHost, ret);
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpoint, ret);
return ret;

}

/**
Expand Down
100 changes: 97 additions & 3 deletions src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
//CHECKSTYLE.OFF: IllegalImport

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;

import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -59,6 +62,7 @@
import static org.hyperledger.fabric.sdk.testutils.TestUtils.matchesRegex;
import static org.hyperledger.fabric.sdk.testutils.TestUtils.setField;
import static org.hyperledger.fabric.sdk.testutils.TestUtils.tarBytesToEntryArrayList;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -225,7 +229,6 @@ public void testChannelAddNullOrder() {

}


@Test
public void testChannelInitialize() throws Exception { //test may not be doable once initialize is done

Expand Down Expand Up @@ -316,8 +319,6 @@ public void testChannelShutdownAddOrderer() throws Exception {

}



@Test
public void testChannelShutdownJoinPeer() throws Exception {

Expand Down Expand Up @@ -466,6 +467,99 @@ public void testTwoChannelsSameName() throws Exception {

}

@Test
public void testSD() throws Exception {

Channel sd = createRunningChannel("testTwoChannelsSameName", null);

Class<?>[] declaredClasses = Channel.class.getDeclaredClasses();
Class n = null;
for (Class c : declaredClasses) {

if ("org.hyperledger.fabric.sdk.Channel$SDOPeerDefaultAddition".equals(c.getName())) {
n = c;
break;
}

}
Constructor declaredConstructor = n.getDeclaredConstructor(Properties.class);
Properties properties1 = new Properties();
properties1.put("org.hyperledger.fabric.sdk.discovery.default.clientKeyBytes", new byte[] {1, 2, 3});
properties1.put("org.hyperledger.fabric.sdk.discovery.default.clientCertBytes", new byte[] {1, 2, 4});
properties1.put("org.hyperledger.fabric.sdk.discovery.endpoint.clientKeyBytes.2.1.3.4", new byte[] {9, 2, 4});
properties1.put("org.hyperledger.fabric.sdk.discovery.endpoint.clientKeyBytes.2.1.3.4:88", new byte[] {88, 2, 4});
properties1.put("org.hyperledger.fabric.sdk.discovery.mspid.clientCertBytes.SPECIAL", new byte[] {1, 2, 9});
Object o1 = declaredConstructor.newInstance(properties1);

setField(sd, "sdPeerAddition", o1);
setField(sd, "initialized", false);

// invokeMethod(Channel.class, "init", null);
// new Channel.SDOPeerDefaultAddition(null);
final String[] discoveredEndpoint = new String[] {"1.1.1.1:10"};
final String[] discoveredMSPID = new String[] {"MSPID"};

final Channel.SDPeerAdditionInfo sdPeerAdditionInfo = new Channel.SDPeerAdditionInfo() {
@Override
public String getMspId() {
return discoveredMSPID[0];
}

@Override
public String getEndpoint() {
return discoveredEndpoint[0];
}

@Override
public Channel getChannel() {
return sd;
}

@Override
public HFClient getClient() {
return hfclient;
}

@Override
public byte[][] getTLSCerts() {
return new byte[0][];
}

@Override
public byte[][] getTLSIntermediateCerts() {
return new byte[0][];
}

@Override
public Map<String, Peer> getEndpointMap() {
return new HashMap<>();
}
};

Peer peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
Properties properties = peer.getProperties();

assertArrayEquals(new byte[] {1, 2, 3}, (byte[]) properties.get("clientKeyBytes"));
assertArrayEquals(new byte[] {1, 2, 4}, (byte[]) properties.get("clientCertBytes"));
discoveredEndpoint[0] = "1.1.1.3:33";

discoveredMSPID[0] = "SPECIAL";
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
properties = peer.getProperties();
assertArrayEquals(new byte[] {1, 2, 9}, (byte[]) properties.get("clientCertBytes"));

discoveredEndpoint[0] = "2.1.3.4:99";
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
properties = peer.getProperties();
assertArrayEquals(new byte[] {9, 2, 4}, (byte[]) properties.get("clientKeyBytes"));

discoveredEndpoint[0] = "2.1.3.4:88";
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
properties = peer.getProperties();
assertArrayEquals(new byte[] {88, 2, 4}, (byte[]) properties.get("clientKeyBytes"));

}

static final String CHANNEL_NAME2 = "channel";

public static Channel createRunningChannel(Collection<Peer> peers) throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException {
Expand Down

0 comments on commit dbc41c1

Please sign in to comment.