23
23
import org .zowe .apiml .zaasclient .exception .ZaasConfigurationException ;
24
24
25
25
import javax .net .ssl .*;
26
- import java .io .File ;
27
26
import java .io .FileInputStream ;
28
27
import java .io .IOException ;
29
28
import java .io .InputStream ;
@@ -49,7 +48,7 @@ class ZaasHttpsClientProvider implements CloseableClientProvider {
49
48
50
49
private final CookieStore cookieStore = new BasicCookieStore ();
51
50
52
- private CloseableHttpClient httpsClientWithKeyStoreAndTrustStore ;
51
+ private CloseableHttpClient httpsClient ;
53
52
54
53
public ZaasHttpsClientProvider (ConfigProperties configProperties ) throws ZaasConfigurationException {
55
54
this .requestConfig = this .buildCustomRequestConfig ();
@@ -71,14 +70,13 @@ public void clearCookieStore() {
71
70
72
71
@ Override
73
72
public synchronized CloseableHttpClient getHttpClient () throws ZaasConfigurationException {
74
- if (httpsClientWithKeyStoreAndTrustStore == null ) {
75
- if (( kmf == null ) && ( keyStorePath != null ) ) {
73
+ if (httpsClient == null ) {
74
+ if (kmf == null ) {
76
75
initializeKeyStoreManagerFactory ();
77
76
}
78
- httpsClientWithKeyStoreAndTrustStore = sharedHttpClientConfiguration (getSSLContext ())
79
- .build ();
77
+ httpsClient = sharedHttpClientConfiguration (getSSLContext ()).build ();
80
78
}
81
- return httpsClientWithKeyStoreAndTrustStore ;
79
+ return httpsClient ;
82
80
}
83
81
84
82
private void initializeTrustManagerFactory (String trustStorePath , String trustStoreType , char [] trustStorePassword )
@@ -97,8 +95,14 @@ private void initializeTrustManagerFactory(String trustStorePath, String trustSt
97
95
98
96
private void initializeKeyStoreManagerFactory () throws ZaasConfigurationException {
99
97
try {
98
+ KeyStore keyStore ;
99
+ if (keyStorePath != null ) {
100
+ keyStore = getKeystore (keyStorePath , keyStoreType , keyStorePassword );
101
+ } else {
102
+ keyStore = getEmptyKeystore ();
103
+ }
104
+
100
105
kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
101
- KeyStore keyStore = getKeystore (keyStorePath , keyStoreType , keyStorePassword );
102
106
kmf .init (keyStore , keyStorePassword );
103
107
} catch (NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException e ) {
104
108
throw new ZaasConfigurationException (ZaasConfigurationErrorCodes .WRONG_CRYPTO_CONFIGURATION , e );
@@ -115,12 +119,20 @@ private KeyStore getKeystore(String uri, String keyStoreType, char[] storePasswo
115
119
}
116
120
}
117
121
122
+ // Necessary because IBM JDK will automatically add keyStore based on system variables when there is no keyStore
123
+ private KeyStore getEmptyKeystore () throws KeyStoreException , CertificateException , IOException , NoSuchAlgorithmException {
124
+ KeyStore emptyKeystore = KeyStore .getInstance (KeyStore .getDefaultType ());
125
+ emptyKeystore .load (null , null );
126
+
127
+ return emptyKeystore ;
128
+ }
129
+
118
130
private InputStream getCorrectInputStream (String uri ) throws IOException {
119
131
if (uri .startsWith (SAFKEYRING + ":////" )) {
120
132
URL url = new URL (replaceFourSlashes (uri ));
121
133
return url .openStream ();
122
134
}
123
- return new FileInputStream (new File ( uri ) );
135
+ return new FileInputStream (uri );
124
136
}
125
137
126
138
public static String replaceFourSlashes (String storeUri ) {
0 commit comments