Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit dbc41c1

Browse files
committed
FABJ-406 Service Discovery TLS Error Using Cert Bytes
Change-Id: I20d71f72ce2a19de51c903f230514eb6a56c8507 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent d69047f commit dbc41c1

File tree

3 files changed

+126
-31
lines changed

3 files changed

+126
-31
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ On the Peer or Orderer add the property `grpc.NettyChannelBuilderOption.maxInbou
282282
See [End2endIT's constructChannel](https://github.com/hyperledger/fabric-sdk-java/blob/b649868113e969d851720c972f660114b64247bc/src/test/java/org/hyperledger/fabric/sdkintegration/End2endIT.java#L846)
283283

284284

285+
### Configuration and setting default values - timeouts etc
286+
287+
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)
288+
The [config.properties](https://github.com/hyperledger/fabric-sdk-java/blob/a2140f9bba57a63c58d9ee8579fea7164bf3beb2/config.properties)
289+
also has some descriptions on what they do. Most server timeout request can be overridden with the specific request too.
290+
285291
### java.security.InvalidKeyException: Illegal key size
286292

287293
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.

src/main/java/org/hyperledger/fabric/sdk/Channel.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
641641
if (peerOptions.getPeerRoles().contains(PeerRole.SERVICE_DISCOVERY)) {
642642

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

1416-
String protocol = findClientProp(config, "protocol", mspid, endpoint, "grpcs:");
1416+
String protocol = (String) findClientProp(config, "protocol", mspid, endpoint, "grpcs:");
14171417

1418-
String clientCertFile = findClientProp(config, "clientCertFile", mspid, endpoint, null);
1418+
String clientCertFile = (String) findClientProp(config, "clientCertFile", mspid, endpoint, null);
14191419

14201420
if (null != clientCertFile) {
14211421
properties.put("clientCertFile", clientCertFile);
14221422
}
14231423

1424-
String clientKeyFile = findClientProp(config, "clientKeyFile", mspid, endpoint, null);
1424+
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
14251425
if (null != clientKeyFile) {
14261426
properties.put("clientKeyFile", clientKeyFile);
14271427
}
14281428

1429-
String clientCertBytes = findClientProp(config, "clientCertBytes", mspid, endpoint, null);
1429+
byte[] clientCertBytes = (byte[]) findClientProp(config, "clientCertBytes", mspid, endpoint, null);
14301430
if (null != clientCertBytes) {
14311431
properties.put("clientCertBytes", clientCertBytes);
14321432
}
14331433

1434-
String clientKeyBytes = findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
1434+
byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
14351435
if (null != clientKeyBytes) {
14361436
properties.put("clientKeyBytes", clientKeyBytes);
14371437
}
14381438

1439-
String hostnameOverride = findClientProp(config, "hostnameOverride", mspid, endpoint, null);
1439+
String hostnameOverride = (String) findClientProp(config, "hostnameOverride", mspid, endpoint, null);
14401440
if (null != hostnameOverride) {
14411441
properties.put("hostnameOverride", hostnameOverride);
14421442
}
@@ -1470,36 +1470,32 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
14701470
final String endpoint = sdPeerAddition.getEndpoint();
14711471
final String mspid = sdPeerAddition.getMspId();
14721472

1473-
String protocol = findClientProp(config, "protocol", mspid, endpoint, "grpcs:");
1474-
1475-
String clientCertFile = findClientProp(config, "clientCertFile", mspid, endpoint, null);
1473+
String protocol = (String) findClientProp(config, "protocol", mspid, endpoint, "grpcs:");
14761474

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

14811479
}
14821480

1483-
if (null != clientCertFile) {
1484-
properties.put("clientCertFile", clientCertFile);
1485-
}
1481+
String clientCertFile = (String) findClientProp(config, "clientCertFile", mspid, endpoint, null);
14861482

1487-
String clientKeyFile = findClientProp(config, "clientKeyFile", mspid, endpoint, null);
1488-
if (null != clientKeyFile) {
1489-
properties.put("clientKeyFile", clientKeyFile);
1490-
}
1491-
1492-
String clientCertBytes = findClientProp(config, "clientCertBytes", mspid, endpoint, null);
1483+
byte[] clientCertBytes = (byte[]) findClientProp(config, "clientCertBytes", mspid, endpoint, null);
14931484
if (null != clientCertBytes) {
14941485
properties.put("clientCertBytes", clientCertBytes);
1486+
} else if (null != clientCertFile) {
1487+
properties.put("clientCertFile", clientCertFile);
14951488
}
14961489

1497-
String clientKeyBytes = findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
1490+
byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
1491+
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
14981492
if (null != clientKeyBytes) {
14991493
properties.put("clientKeyBytes", clientKeyBytes);
1494+
} else if (null != clientKeyFile) {
1495+
properties.put("clientKeyFile", clientKeyFile);
15001496
}
15011497

1502-
String hostnameOverride = findClientProp(config, "hostnameOverride", mspid, endpoint, null);
1498+
String hostnameOverride = (String) findClientProp(config, "hostnameOverride", mspid, endpoint, null);
15031499
if (null != hostnameOverride) {
15041500
properties.put("hostnameOverride", hostnameOverride);
15051501
}
@@ -1520,16 +1516,15 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
15201516
}
15211517
}
15221518

1523-
static String findClientProp(Properties config, final String prop, final String mspid, final String endpoint, String def) {
1519+
static Object findClientProp(Properties config, final String prop, final String mspid, final String endpoint, String def) {
15241520
final String[] split = endpoint.split(":");
15251521
final String endpointHost = split[0];
15261522

1527-
String ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.default." + prop, def);
1528-
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.mspid." + prop + "." + mspid, ret);
1529-
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpointHost, ret);
1530-
ret = config.getProperty("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpoint, ret);
1523+
Object ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.default." + prop, def);
1524+
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.mspid." + prop + "." + mspid, ret);
1525+
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpointHost, ret);
1526+
ret = config.getOrDefault("org.hyperledger.fabric.sdk.discovery.endpoint." + prop + "." + endpoint, ret);
15311527
return ret;
1532-
15331528
}
15341529

15351530
/**

src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
//CHECKSTYLE.OFF: IllegalImport
1919

2020
import java.io.File;
21+
import java.lang.reflect.Constructor;
2122
import java.lang.reflect.Field;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.EnumSet;
27+
import java.util.HashMap;
2628
import java.util.LinkedList;
2729
import java.util.Map;
30+
import java.util.Properties;
2831
import java.util.concurrent.CompletableFuture;
2932

3033
import com.google.common.util.concurrent.ListenableFuture;
@@ -59,6 +62,7 @@
5962
import static org.hyperledger.fabric.sdk.testutils.TestUtils.matchesRegex;
6063
import static org.hyperledger.fabric.sdk.testutils.TestUtils.setField;
6164
import static org.hyperledger.fabric.sdk.testutils.TestUtils.tarBytesToEntryArrayList;
65+
import static org.junit.Assert.assertArrayEquals;
6266
import static org.junit.Assert.assertFalse;
6367
import static org.junit.Assert.assertTrue;
6468

@@ -225,7 +229,6 @@ public void testChannelAddNullOrder() {
225229

226230
}
227231

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

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

317320
}
318321

319-
320-
321322
@Test
322323
public void testChannelShutdownJoinPeer() throws Exception {
323324

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

467468
}
468469

470+
@Test
471+
public void testSD() throws Exception {
472+
473+
Channel sd = createRunningChannel("testTwoChannelsSameName", null);
474+
475+
Class<?>[] declaredClasses = Channel.class.getDeclaredClasses();
476+
Class n = null;
477+
for (Class c : declaredClasses) {
478+
479+
if ("org.hyperledger.fabric.sdk.Channel$SDOPeerDefaultAddition".equals(c.getName())) {
480+
n = c;
481+
break;
482+
}
483+
484+
}
485+
Constructor declaredConstructor = n.getDeclaredConstructor(Properties.class);
486+
Properties properties1 = new Properties();
487+
properties1.put("org.hyperledger.fabric.sdk.discovery.default.clientKeyBytes", new byte[] {1, 2, 3});
488+
properties1.put("org.hyperledger.fabric.sdk.discovery.default.clientCertBytes", new byte[] {1, 2, 4});
489+
properties1.put("org.hyperledger.fabric.sdk.discovery.endpoint.clientKeyBytes.2.1.3.4", new byte[] {9, 2, 4});
490+
properties1.put("org.hyperledger.fabric.sdk.discovery.endpoint.clientKeyBytes.2.1.3.4:88", new byte[] {88, 2, 4});
491+
properties1.put("org.hyperledger.fabric.sdk.discovery.mspid.clientCertBytes.SPECIAL", new byte[] {1, 2, 9});
492+
Object o1 = declaredConstructor.newInstance(properties1);
493+
494+
setField(sd, "sdPeerAddition", o1);
495+
setField(sd, "initialized", false);
496+
497+
// invokeMethod(Channel.class, "init", null);
498+
// new Channel.SDOPeerDefaultAddition(null);
499+
final String[] discoveredEndpoint = new String[] {"1.1.1.1:10"};
500+
final String[] discoveredMSPID = new String[] {"MSPID"};
501+
502+
final Channel.SDPeerAdditionInfo sdPeerAdditionInfo = new Channel.SDPeerAdditionInfo() {
503+
@Override
504+
public String getMspId() {
505+
return discoveredMSPID[0];
506+
}
507+
508+
@Override
509+
public String getEndpoint() {
510+
return discoveredEndpoint[0];
511+
}
512+
513+
@Override
514+
public Channel getChannel() {
515+
return sd;
516+
}
517+
518+
@Override
519+
public HFClient getClient() {
520+
return hfclient;
521+
}
522+
523+
@Override
524+
public byte[][] getTLSCerts() {
525+
return new byte[0][];
526+
}
527+
528+
@Override
529+
public byte[][] getTLSIntermediateCerts() {
530+
return new byte[0][];
531+
}
532+
533+
@Override
534+
public Map<String, Peer> getEndpointMap() {
535+
return new HashMap<>();
536+
}
537+
};
538+
539+
Peer peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
540+
Properties properties = peer.getProperties();
541+
542+
assertArrayEquals(new byte[] {1, 2, 3}, (byte[]) properties.get("clientKeyBytes"));
543+
assertArrayEquals(new byte[] {1, 2, 4}, (byte[]) properties.get("clientCertBytes"));
544+
discoveredEndpoint[0] = "1.1.1.3:33";
545+
546+
discoveredMSPID[0] = "SPECIAL";
547+
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
548+
properties = peer.getProperties();
549+
assertArrayEquals(new byte[] {1, 2, 9}, (byte[]) properties.get("clientCertBytes"));
550+
551+
discoveredEndpoint[0] = "2.1.3.4:99";
552+
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
553+
properties = peer.getProperties();
554+
assertArrayEquals(new byte[] {9, 2, 4}, (byte[]) properties.get("clientKeyBytes"));
555+
556+
discoveredEndpoint[0] = "2.1.3.4:88";
557+
peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);
558+
properties = peer.getProperties();
559+
assertArrayEquals(new byte[] {88, 2, 4}, (byte[]) properties.get("clientKeyBytes"));
560+
561+
}
562+
469563
static final String CHANNEL_NAME2 = "channel";
470564

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

0 commit comments

Comments
 (0)