Skip to content

Commit 691036a

Browse files
authored
fix: Zaas client to not send a certificate during authentication (#1763)
* Fix usage of empty keystore in https client Signed-off-by: JirkaAichler <jiri.aichler@broadcom.com> * Add unit test Signed-off-by: JirkaAichler <jiri.aichler@broadcom.com> * Comment code Signed-off-by: JirkaAichler <jiri.aichler@broadcom.com>
1 parent cc612e5 commit 691036a

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

zaas-client/src/main/java/org/zowe/apiml/zaasclient/service/internal/ZaasHttpsClientProvider.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.zowe.apiml.zaasclient.exception.ZaasConfigurationException;
2424

2525
import javax.net.ssl.*;
26-
import java.io.File;
2726
import java.io.FileInputStream;
2827
import java.io.IOException;
2928
import java.io.InputStream;
@@ -49,7 +48,7 @@ class ZaasHttpsClientProvider implements CloseableClientProvider {
4948

5049
private final CookieStore cookieStore = new BasicCookieStore();
5150

52-
private CloseableHttpClient httpsClientWithKeyStoreAndTrustStore;
51+
private CloseableHttpClient httpsClient;
5352

5453
public ZaasHttpsClientProvider(ConfigProperties configProperties) throws ZaasConfigurationException {
5554
this.requestConfig = this.buildCustomRequestConfig();
@@ -71,14 +70,13 @@ public void clearCookieStore() {
7170

7271
@Override
7372
public synchronized CloseableHttpClient getHttpClient() throws ZaasConfigurationException {
74-
if (httpsClientWithKeyStoreAndTrustStore == null) {
75-
if ((kmf == null) && (keyStorePath != null)) {
73+
if (httpsClient == null) {
74+
if (kmf == null) {
7675
initializeKeyStoreManagerFactory();
7776
}
78-
httpsClientWithKeyStoreAndTrustStore = sharedHttpClientConfiguration(getSSLContext())
79-
.build();
77+
httpsClient = sharedHttpClientConfiguration(getSSLContext()).build();
8078
}
81-
return httpsClientWithKeyStoreAndTrustStore;
79+
return httpsClient;
8280
}
8381

8482
private void initializeTrustManagerFactory(String trustStorePath, String trustStoreType, char[] trustStorePassword)
@@ -97,8 +95,14 @@ private void initializeTrustManagerFactory(String trustStorePath, String trustSt
9795

9896
private void initializeKeyStoreManagerFactory() throws ZaasConfigurationException {
9997
try {
98+
KeyStore keyStore;
99+
if (keyStorePath != null) {
100+
keyStore = getKeystore(keyStorePath, keyStoreType, keyStorePassword);
101+
} else {
102+
keyStore = getEmptyKeystore();
103+
}
104+
100105
kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
101-
KeyStore keyStore = getKeystore(keyStorePath, keyStoreType, keyStorePassword);
102106
kmf.init(keyStore, keyStorePassword);
103107
} catch (NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException e) {
104108
throw new ZaasConfigurationException(ZaasConfigurationErrorCodes.WRONG_CRYPTO_CONFIGURATION, e);
@@ -115,12 +119,20 @@ private KeyStore getKeystore(String uri, String keyStoreType, char[] storePasswo
115119
}
116120
}
117121

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+
118130
private InputStream getCorrectInputStream(String uri) throws IOException {
119131
if (uri.startsWith(SAFKEYRING + ":////")) {
120132
URL url = new URL(replaceFourSlashes(uri));
121133
return url.openStream();
122134
}
123-
return new FileInputStream(new File(uri));
135+
return new FileInputStream(uri);
124136
}
125137

126138
public static String replaceFourSlashes(String storeUri) {

zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasHttpsClientProviderTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ void testGetHttpsClientWithKeyStoreAndTrustStore() throws ZaasConfigurationExcep
7272
assertNotNull(zaasHttpsClientProvider.getHttpClient());
7373
}
7474

75+
@Test
76+
void giveNoKeyStorePath_whenTheClientIsConstructed_thenEmptyKeyStoreIsUsed() throws ZaasConfigurationException, IOException {
77+
ConfigProperties config = getConfigProperties();
78+
config.setKeyStorePath(null);
79+
ZaasHttpsClientProvider provider = new ZaasHttpsClientProvider(config);
80+
81+
assertNotNull(provider.getHttpClient());
82+
}
83+
7584
@Test
7685
void whenGetHttpsClientWithKeyStoreAndTrustStore_thenIdenticalClientReturned() throws ZaasConfigurationException {
7786
CloseableHttpClient client1 = zaasHttpsClientProvider.getHttpClient();

0 commit comments

Comments
 (0)