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

Commit c63dd83

Browse files
committed
FAB-5892 Facilitate Integration with configuration
Change-Id: Id571d1e30a57bcfe3db671d000cbf2d82c66a86f Signed-off-by: rickr <cr22rc@gmail.com>
1 parent f94d5ff commit c63dd83

File tree

14 files changed

+376
-56
lines changed

14 files changed

+376
-56
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>com.google.protobuf</groupId>
100100
<artifactId>protobuf-java</artifactId>
101-
<version>3.3.1</version>
101+
<version>3.4.0</version>
102102
</dependency>
103103
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
104104
<dependency>

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515
package org.hyperledger.fabric.sdk;
1616

17-
import java.io.File;
17+
import java.io.ByteArrayInputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
1820
import java.lang.reflect.InvocationTargetException;
1921
import java.lang.reflect.Method;
22+
import java.nio.charset.StandardCharsets;
2023
import java.nio.file.Files;
2124
import java.nio.file.Path;
2225
import java.nio.file.Paths;
@@ -44,7 +47,6 @@
4447
import org.bouncycastle.asn1.x500.style.BCStyle;
4548
import org.bouncycastle.asn1.x500.style.IETFUtils;
4649
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
47-
import org.hyperledger.fabric.sdk.helper.Utils;
4850
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
4951

5052
import static org.hyperledger.fabric.sdk.helper.Utils.parseGrpcUrl;
@@ -64,10 +66,10 @@ class Endpoint {
6466
logger.trace(String.format("Creating endpoint for url %s", url));
6567
this.url = url;
6668

67-
String pem = null;
6869
String cn = null;
6970
String sslp = null;
7071
String nt = null;
72+
byte[] pemBytes = null;
7173

7274
Properties purl = parseGrpcUrl(url);
7375
String protocol = purl.getProperty("protocol");
@@ -76,33 +78,42 @@ class Endpoint {
7678

7779
if (properties != null) {
7880
if ("grpcs".equals(protocol)) {
79-
try {
80-
pem = properties.getProperty("pemFile");
81-
cn = properties.getProperty("hostnameOverride");
82-
83-
if (cn == null && "true".equals(properties.getProperty("trustServerCertificate"))) {
81+
if (properties.containsKey("pemFile") && properties.containsKey("pemBytes")) {
82+
throw new RuntimeException("Properties \"pemBytes\" and \"pemBytes\" can not be both set.");
83+
}
84+
if (properties.containsKey("pemFile")) {
85+
Path path = Paths.get(properties.getProperty("pemFile"));
86+
try {
87+
pemBytes = Files.readAllBytes(path);
88+
} catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
} else if (properties.containsKey("pemBytes")) {
92+
pemBytes = (byte[]) properties.get("pemBytes");
93+
}
94+
if (null != pemBytes) {
95+
try {
96+
cn = properties.getProperty("hostnameOverride");
8497

85-
File pemF = new File(pem);
86-
final String cnKey = pemF.getAbsolutePath() + pemF.length() + pemF.lastModified();
98+
if (cn == null && "true".equals(properties.getProperty("trustServerCertificate"))) {
99+
final String cnKey = new String(pemBytes, StandardCharsets.UTF_8);
87100

88-
cn = CN_CACHE.get(cnKey);
89-
if (cn == null) {
90-
Path path = Paths.get(pem);
91-
byte[] data = Files.readAllBytes(path);
101+
cn = CN_CACHE.get(cnKey);
102+
if (cn == null) {
103+
CryptoPrimitives cp = new CryptoPrimitives();
92104

93-
CryptoPrimitives cp = new CryptoPrimitives();
105+
X500Name x500name = new JcaX509CertificateHolder((X509Certificate) cp.bytesToCertificate(pemBytes)).getSubject();
106+
RDN rdn = x500name.getRDNs(BCStyle.CN)[0];
107+
cn = IETFUtils.valueToString(rdn.getFirst().getValue());
108+
CN_CACHE.put(cnKey, cn);
109+
}
94110

95-
X500Name x500name = new JcaX509CertificateHolder((X509Certificate) cp.bytesToCertificate(data)).getSubject();
96-
RDN rdn = x500name.getRDNs(BCStyle.CN)[0];
97-
cn = IETFUtils.valueToString(rdn.getFirst().getValue());
98-
CN_CACHE.put(cnKey, cn);
99111
}
112+
} catch (Exception e) {
113+
/// Mostly a development env. just log it.
114+
logger.error("Error getting Subject CN from certificate. Try setting it specifically with hostnameOverride property. " + e.getMessage());
100115

101116
}
102-
} catch (Exception e) {
103-
/// Mostly a development env. just log it.
104-
logger.error("Error getting Subject CN from certificate. Try setting it specifically with hostnameOverride property. " + e.getMessage());
105-
106117
}
107118

108119
sslp = properties.getProperty("sslProvider");
@@ -130,7 +141,7 @@ class Endpoint {
130141
.usePlaintext(true);
131142
addNettyBuilderProps(channelBuilder, properties);
132143
} else if (protocol.equalsIgnoreCase("grpcs")) {
133-
if (Utils.isNullOrEmpty(pem)) {
144+
if (pemBytes == null) {
134145
// use root certificate
135146
this.channelBuilder = NettyChannelBuilder.forAddress(addr, port);
136147
addNettyBuilderProps(channelBuilder, properties);
@@ -140,8 +151,9 @@ class Endpoint {
140151
SslProvider sslprovider = sslp.equals("openSSL") ? SslProvider.OPENSSL : SslProvider.JDK;
141152
NegotiationType ntype = nt.equals("TLS") ? NegotiationType.TLS : NegotiationType.PLAINTEXT;
142153

154+
InputStream myInputStream = new ByteArrayInputStream(pemBytes);
143155
SslContext sslContext = GrpcSslContexts.forClient()
144-
.trustManager(new File(pem))
156+
.trustManager(myInputStream)
145157
.sslProvider(sslprovider)
146158
.build();
147159
this.channelBuilder = NettyChannelBuilder.forAddress(addr, port)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void onCompleted() {
303303

304304
}
305305

306-
logger.info(format("Eventhub %s connect is done with connect status: %b ", name, connected));
306+
logger.debug(format("Eventhub %s connect is done with connect status: %b ", name, connected));
307307

308308
if (connected) {
309309
eventStream = eventStreamLocal;

src/main/java/org/hyperledger/fabric/sdk/security/CryptoPrimitives.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,47 @@ public void addCACertificateToTrustStore(File caCertPem, String alias) throws Cr
257257
throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store");
258258
}
259259

260-
261260
BufferedInputStream bis;
262261
try {
263262

264263
bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem)));
265264
Certificate caCert = cf.generateCertificate(bis);
266-
this.addCACertificateToTrustStore(caCert, alias);
265+
addCACertificateToTrustStore(caCert, alias);
267266
} catch (CertificateException | IOException e) {
268267
throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
269268
}
270269
}
271270

271+
/**
272+
* addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
273+
*
274+
* @param bytes an X.509 certificate in PEM format in bytes
275+
* @param alias an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
276+
* @throws CryptoException
277+
* @throws InvalidArgumentException
278+
*/
279+
public void addCACertificateToTrustStore(byte[] bytes, String alias) throws CryptoException, InvalidArgumentException {
280+
281+
if (bytes == null) {
282+
throw new InvalidArgumentException("The certificate cannot be null");
283+
}
284+
285+
if (alias == null || alias.isEmpty()) {
286+
throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store");
287+
}
288+
289+
BufferedInputStream bis;
290+
try {
291+
292+
bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
293+
Certificate caCert = cf.generateCertificate(bis);
294+
addCACertificateToTrustStore(caCert, alias);
295+
296+
} catch (CertificateException e) {
297+
throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
298+
}
299+
}
300+
272301
/**
273302
* addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
274303
*

0 commit comments

Comments
 (0)