Skip to content

Commit f0a2c7a

Browse files
committed
load certificate from the inputStream (#293)
1 parent b7a4010 commit f0a2c7a

File tree

5 files changed

+134
-45
lines changed

5 files changed

+134
-45
lines changed

sdk-core/src/main/java/org/fisco/bcos/sdk/config/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static ConfigOption load(String tomlConfigFile, int cryptoType) throws Co
4646
return configOption;
4747
} catch (Exception e) {
4848
throw new ConfigException(
49-
"parse Config " + tomlConfigFile + " failed, error info: " + e.getMessage(), e);
49+
"parse Config " + tomlConfigFile + " failed, error info: ", e);
5050
}
5151
}
5252
}

sdk-core/src/main/java/org/fisco/bcos/sdk/config/model/ConfigProperty.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717

1818
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1919
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
2023
import java.io.UnsupportedEncodingException;
2124
import java.net.URL;
2225
import java.net.URLDecoder;
2326
import java.util.List;
2427
import java.util.Map;
2528
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2631

2732
/**
2833
* ConfigOption is the java object of the config file.
@@ -31,6 +36,7 @@
3136
*/
3237
@JsonIgnoreProperties(ignoreUnknown = true)
3338
public class ConfigProperty {
39+
private static Logger logger = LoggerFactory.getLogger(ConfigProperty.class);
3440
public Map<String, Object> cryptoMaterial;
3541
public Map<String, Object> network;
3642
public List<AmopTopic> amop;
@@ -85,11 +91,41 @@ public static String getValue(Map<String, Object> config, String key, String def
8591
return (String) config.get(key);
8692
}
8793

94+
public static InputStream getConfigInputStream(String configFilePath) throws ConfigException {
95+
if (configFilePath == null) {
96+
return null;
97+
}
98+
InputStream inputStream = null;
99+
try {
100+
inputStream = new FileInputStream(configFilePath);
101+
if (inputStream != null) {
102+
return inputStream;
103+
}
104+
} catch (IOException e) {
105+
logger.warn(
106+
"Load config from {} failed, trying to load from the classpath, e: {}",
107+
configFilePath,
108+
e);
109+
if (inputStream != null) {
110+
try {
111+
inputStream.close();
112+
} catch (IOException error) {
113+
logger.warn("close InputStream failed, error:", e);
114+
}
115+
}
116+
}
117+
// try to load from the class path
118+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
119+
inputStream = classLoader.getResourceAsStream(configFilePath);
120+
return inputStream;
121+
}
122+
88123
public static String getConfigFilePath(String configFilePath) throws ConfigException {
89124
try {
90125
if (configFilePath == null) {
91126
return null;
92127
}
128+
93129
File file = new File(configFilePath);
94130
if (file.exists()) {
95131
return configFilePath;

sdk-core/src/main/java/org/fisco/bcos/sdk/config/model/CryptoMaterialConfig.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.fisco.bcos.sdk.config.model;
1717

1818
import java.io.File;
19+
import java.io.InputStream;
1920
import java.util.Map;
2021
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
2122
import org.fisco.bcos.sdk.model.CryptoType;
@@ -31,6 +32,13 @@ public class CryptoMaterialConfig {
3132
private String sdkPrivateKeyPath;
3233
private String enSSLCertPath;
3334
private String enSSLPrivateKeyPath;
35+
36+
private InputStream caInputStream;
37+
private InputStream sdkCertInputStream;
38+
private InputStream sdkPrivateKeyInputStream;
39+
private InputStream enSSLCertInputStream;
40+
private InputStream enSSLPrivateKeyInputStream;
41+
3442
private int sslCryptoType;
3543

3644
protected CryptoMaterialConfig() {}
@@ -74,6 +82,37 @@ public CryptoMaterialConfig(ConfigProperty configProperty, int cryptoType)
7482
cryptoMaterialProperty,
7583
"enSslKey",
7684
defaultCryptoMaterialConfig.getEnSSLPrivateKeyPath()));
85+
// load the input stream
86+
this.caInputStream =
87+
ConfigProperty.getConfigInputStream(
88+
ConfigProperty.getValue(
89+
cryptoMaterialProperty,
90+
"caCert",
91+
defaultCryptoMaterialConfig.getCaCertPath()));
92+
this.sdkCertInputStream =
93+
ConfigProperty.getConfigInputStream(
94+
ConfigProperty.getValue(
95+
cryptoMaterialProperty,
96+
"sslCert",
97+
defaultCryptoMaterialConfig.getSdkCertPath()));
98+
this.sdkPrivateKeyInputStream =
99+
ConfigProperty.getConfigInputStream(
100+
ConfigProperty.getValue(
101+
cryptoMaterialProperty,
102+
"sslKey",
103+
defaultCryptoMaterialConfig.getSdkPrivateKeyPath()));
104+
this.enSSLCertInputStream =
105+
ConfigProperty.getConfigInputStream(
106+
ConfigProperty.getValue(
107+
cryptoMaterialProperty,
108+
"enSslCert",
109+
defaultCryptoMaterialConfig.getEnSSLCertPath()));
110+
this.enSSLPrivateKeyInputStream =
111+
ConfigProperty.getConfigInputStream(
112+
ConfigProperty.getValue(
113+
cryptoMaterialProperty,
114+
"enSslKey",
115+
defaultCryptoMaterialConfig.getEnSSLPrivateKeyPath()));
77116
logger.debug(
78117
"Load cryptoMaterial, caCertPath: {}, sdkCertPath: {}, sdkPrivateKeyPath:{}, enSSLCertPath: {}, enSSLPrivateKeyPath:{}",
79118
this.getCaCertPath(),
@@ -167,6 +206,46 @@ public void setSslCryptoType(int sslCryptoType) {
167206
this.sslCryptoType = sslCryptoType;
168207
}
169208

209+
public InputStream getCaInputStream() {
210+
return caInputStream;
211+
}
212+
213+
public void setCaInputStream(InputStream caInputStream) {
214+
this.caInputStream = caInputStream;
215+
}
216+
217+
public InputStream getSdkCertInputStream() {
218+
return sdkCertInputStream;
219+
}
220+
221+
public void setSdkCertInputStream(InputStream sdkCertInputStream) {
222+
this.sdkCertInputStream = sdkCertInputStream;
223+
}
224+
225+
public InputStream getSdkPrivateKeyInputStream() {
226+
return sdkPrivateKeyInputStream;
227+
}
228+
229+
public void setSdkPrivateKeyInputStream(InputStream sdkPrivateKeyInputStream) {
230+
this.sdkPrivateKeyInputStream = sdkPrivateKeyInputStream;
231+
}
232+
233+
public InputStream getEnSSLCertInputStream() {
234+
return enSSLCertInputStream;
235+
}
236+
237+
public void setEnSSLCertInputStream(InputStream enSSLCertInputStream) {
238+
this.enSSLCertInputStream = enSSLCertInputStream;
239+
}
240+
241+
public InputStream getEnSSLPrivateKeyInputStream() {
242+
return enSSLPrivateKeyInputStream;
243+
}
244+
245+
public void setEnSSLPrivateKeyInputStream(InputStream enSSLPrivateKeyInputStream) {
246+
this.enSSLPrivateKeyInputStream = enSSLPrivateKeyInputStream;
247+
}
248+
170249
@Override
171250
public String toString() {
172251
return "CryptoMaterialConfig{"

sdk-core/src/main/java/org/fisco/bcos/sdk/network/ConnectionManager.java

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import io.netty.handler.ssl.SslProvider;
3434
import io.netty.handler.timeout.IdleStateHandler;
3535
import io.netty.util.concurrent.Future;
36-
import java.io.File;
37-
import java.io.FileInputStream;
38-
import java.io.FileNotFoundException;
3936
import java.io.IOException;
4037
import java.security.NoSuchAlgorithmException;
4138
import java.security.NoSuchProviderException;
@@ -52,7 +49,6 @@
5249
import java.util.concurrent.ScheduledExecutorService;
5350
import java.util.concurrent.ScheduledThreadPoolExecutor;
5451
import java.util.concurrent.TimeUnit;
55-
import javax.net.ssl.SSLException;
5652
import org.fisco.bcos.sdk.config.ConfigOption;
5753
import org.fisco.bcos.sdk.model.CryptoType;
5854
import org.fisco.bcos.sdk.model.RetCode;
@@ -229,28 +225,21 @@ private SslContext initSslContext(ConfigOption configOption) throws NetworkExcep
229225
Security.setProperty("jdk.disabled.namedCurves", "");
230226
System.setProperty("jdk.sunec.disableNative", "false");
231227
// Get file, file existence is already checked when check config file.
232-
FileInputStream caCert =
233-
new FileInputStream(
234-
new File(configOption.getCryptoMaterialConfig().getCaCertPath()));
235-
FileInputStream sslCert =
236-
new FileInputStream(
237-
new File(configOption.getCryptoMaterialConfig().getSdkCertPath()));
238-
FileInputStream sslKey =
239-
new FileInputStream(
240-
new File(
241-
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath()));
242-
243228
// Init SslContext
244229
logger.info(" build ECDSA ssl context with configured certificates ");
245230
SslContext sslCtx =
246231
SslContextBuilder.forClient()
247-
.trustManager(caCert)
248-
.keyManager(sslCert, sslKey)
232+
.trustManager(configOption.getCryptoMaterialConfig().getCaInputStream())
233+
.keyManager(
234+
configOption.getCryptoMaterialConfig().getSdkCertInputStream(),
235+
configOption
236+
.getCryptoMaterialConfig()
237+
.getSdkPrivateKeyInputStream())
249238
.sslProvider(SslProvider.OPENSSL)
250239
// .sslProvider(SslProvider.JDK)
251240
.build();
252241
return sslCtx;
253-
} catch (FileNotFoundException | SSLException e) {
242+
} catch (IOException e) {
254243
logger.error(
255244
"initSslContext failed, caCert: {}, sslCert: {}, sslKey: {}, error: {}, e: {}",
256245
configOption.getCryptoMaterialConfig().getCaCertPath(),
@@ -273,29 +262,14 @@ private SslContext initSslContext(ConfigOption configOption) throws NetworkExcep
273262
private SslContext initSMSslContext(ConfigOption configOption) throws NetworkException {
274263
try {
275264
// Get file, file existence is already checked when check config file.
276-
FileInputStream caCert =
277-
new FileInputStream(
278-
new File(configOption.getCryptoMaterialConfig().getCaCertPath()));
279-
FileInputStream sslCert =
280-
new FileInputStream(
281-
new File(configOption.getCryptoMaterialConfig().getSdkCertPath()));
282-
FileInputStream sslKey =
283-
new FileInputStream(
284-
new File(
285-
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath()));
286-
FileInputStream enCert =
287-
new FileInputStream(
288-
new File(configOption.getCryptoMaterialConfig().getEnSSLCertPath()));
289-
FileInputStream enKey =
290-
new FileInputStream(
291-
new File(
292-
configOption
293-
.getCryptoMaterialConfig()
294-
.getEnSSLPrivateKeyPath()));
295-
296265
// Init SslContext
297266
logger.info(" build SM ssl context with configured certificates ");
298-
return SMSslClientContextFactory.build(caCert, enCert, enKey, sslCert, sslKey);
267+
return SMSslClientContextFactory.build(
268+
configOption.getCryptoMaterialConfig().getCaInputStream(),
269+
configOption.getCryptoMaterialConfig().getEnSSLCertInputStream(),
270+
configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyInputStream(),
271+
configOption.getCryptoMaterialConfig().getSdkCertInputStream(),
272+
configOption.getCryptoMaterialConfig().getSdkPrivateKeyInputStream());
299273
} catch (IOException
300274
| CertificateException
301275
| NoSuchAlgorithmException

sdk-core/src/main/java/org/fisco/bcos/sdk/network/NetworkImp.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ private CheckCertExistenceResult checkCertExistence(boolean isSM) {
109109
result.setCheckPassed(true);
110110
String errorMessage = "";
111111
errorMessage = errorMessage + "Please make sure ";
112-
if (!new File(configOption.getCryptoMaterialConfig().getCaCertPath()).exists()) {
112+
if (configOption.getCryptoMaterialConfig().getCaInputStream() == null) {
113113
result.setCheckPassed(false);
114114
errorMessage =
115115
errorMessage + configOption.getCryptoMaterialConfig().getCaCertPath() + " ";
116116
}
117-
if (!new File(configOption.getCryptoMaterialConfig().getSdkCertPath()).exists()) {
117+
if (configOption.getCryptoMaterialConfig().getSdkCertInputStream() == null) {
118118
result.setCheckPassed(false);
119119
errorMessage =
120120
errorMessage + configOption.getCryptoMaterialConfig().getSdkCertPath() + " ";
121121
}
122-
if (!new File(configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath()).exists()) {
122+
if (configOption.getCryptoMaterialConfig().getSdkPrivateKeyInputStream() == null) {
123123
result.setCheckPassed(false);
124124
errorMessage =
125125
errorMessage
@@ -131,12 +131,12 @@ private CheckCertExistenceResult checkCertExistence(boolean isSM) {
131131
result.setErrorMessage(errorMessage);
132132
return result;
133133
}
134-
if (!new File(configOption.getCryptoMaterialConfig().getEnSSLCertPath()).exists()) {
134+
if (configOption.getCryptoMaterialConfig().getEnSSLCertInputStream() == null) {
135135
errorMessage =
136136
errorMessage + configOption.getCryptoMaterialConfig().getEnSSLCertPath() + " ";
137137
result.setCheckPassed(false);
138138
}
139-
if (!new File(configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyPath()).exists()) {
139+
if (configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyInputStream() == null) {
140140
errorMessage =
141141
errorMessage
142142
+ configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyPath()

0 commit comments

Comments
 (0)