Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Oct 16, 2024
1 parent f23cfbc commit ea1251d
Showing 1 changed file with 48 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,33 +182,22 @@ public KeyStoreData[] getKeyStores(boolean isSuperTenant) throws SecurityConfigE
}
}

@Deprecated
public void addKeyStoreWithFilePath(String filePath, String filename, String password,
String provider, String type, String pvtkeyPass) throws SecurityConfigException {

try {
keyStoreManager.addKeyStore(readBytesFromFile(filePath), filename, password, provider, type, pvtkeyPass);
} catch (SecurityException | IOException e) {
addKeyStore(readBytesFromFile(filePath), filename, password, provider, type, pvtkeyPass);
} catch (IOException e) {
throw new SecurityConfigException("Error while loading keystore from file " + filePath, e);
}

}

@Deprecated
public void addKeyStore(String fileData, String filename, String password, String provider,
String type, String pvtkeyPass) throws SecurityConfigException {

byte[] content = Base64.decode(fileData);
try {
keyStoreManager.addKeyStore(content, filename, password, provider, type, pvtkeyPass);
} catch (SecurityException e) {
String msg = "Error when adding a keyStore";
log.error(msg, e);
throw new SecurityConfigException(msg, e);
}
addKeyStore(content, filename, password, provider, type, pvtkeyPass);
}

@Deprecated
public void addKeyStore(byte[] content, String filename, String password, String provider,
String type, String pvtkeyPass) throws SecurityConfigException {

Expand All @@ -223,7 +212,6 @@ public void addKeyStore(byte[] content, String filename, String password, String

public void addTrustStore(String fileData, String filename, String password, String provider,
String type) throws SecurityConfigException {

byte[] content = Base64.decode(fileData);
addTrustStore(content, filename, password, provider, type);
}
Expand All @@ -240,7 +228,6 @@ public void addTrustStore(byte[] content, String filename, String password, Stri
}
}

@Deprecated
public void deleteStore(String keyStoreName) throws SecurityConfigException {

try {
Expand Down Expand Up @@ -337,7 +324,6 @@ public void removeCertFromStore(String alias, String keyStoreName)
}

ks.deleteEntry(alias);

this.keyStoreManager.updateKeyStore(keyStoreName, ks);

if (KeyStoreUtil.isTrustStore(keyStoreName)) {
Expand Down Expand Up @@ -420,12 +406,53 @@ public KeyStoreData getKeystoreInfo(String keyStoreName) throws SecurityConfigEx
keyStore = this.keyStoreManager.getKeyStore(keyStoreName);
keyStoreType = resource.getProperty(SecurityConstants.PROP_TYPE);

String encPass = resource.getProperty(SecurityConstants.PROP_PRIVATE_KEY_PASS);
if (StringUtils.isNotBlank(encPass)) {
privateKeyPassword = new String(CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(encPass));
String encpass = resource.getProperty(SecurityConstants.PROP_PRIVATE_KEY_PASS);
if (encpass != null) {
CryptoUtil util = CryptoUtil.getDefaultCryptoUtil();
privateKeyPassword = new String(util.base64DecodeAndDecrypt(encpass));
}
}
// Fill the information about the certificates
Enumeration<String> aliases = keyStore.aliases();
List<org.wso2.carbon.security.keystore.service.CertData> certDataList = new ArrayList<>();
Format formatter = new SimpleDateFormat("dd/MM/yyyy");

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isCertificateEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
certDataList.add(fillCertData(cert, alias, formatter));
}
}

// Create a cert array
CertData[] certs = certDataList.toArray(new CertData[certDataList.size()]);

// Create a KeyStoreData bean, set the name and fill in the cert information
KeyStoreData keyStoreData = new KeyStoreData();
keyStoreData.setKeyStoreName(keyStoreName);
keyStoreData.setCerts(certs);
keyStoreData.setKeyStoreType(keyStoreType);

aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
// There be only one entry in WSAS related keystores
if (keyStore.isKeyEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
keyStoreData.setKey(fillCertData(cert, alias, formatter));
PrivateKey key = (PrivateKey) keyStore.getKey(alias, privateKeyPassword
.toCharArray());
String pemKey;
pemKey = "-----BEGIN PRIVATE KEY-----\n";
pemKey += Base64.encode(key.getEncoded());
pemKey += "\n-----END PRIVATE KEY-----";
keyStoreData.setKeyValue(pemKey);
break;

}
}
return createKeyStoreData(keyStoreName, keyStoreType, privateKeyPassword, keyStore);
return keyStoreData;
} catch (Exception e) {
String msg = "Error has encounted while loading the keystore to the given keystore name "
+ keyStoreName;
Expand All @@ -435,50 +462,6 @@ public KeyStoreData getKeystoreInfo(String keyStoreName) throws SecurityConfigEx

}

private KeyStoreData createKeyStoreData(String keyStoreName, String keyStoreType, String privateKeyPassword,
KeyStore keyStore) throws Exception {

// Fill the information about the certificates
Enumeration<String> aliases = keyStore.aliases();
List<CertData> certDataList = new ArrayList<>();
Format formatter = new SimpleDateFormat("dd/MM/yyyy");

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isCertificateEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
certDataList.add(fillCertData(cert, alias, formatter));
}
}

// Create a cert array
CertData[] certs = certDataList.toArray(new CertData[0]);

// Create a KeyStoreData bean, set the name and fill in the cert information
KeyStoreData keyStoreData = new KeyStoreData();
keyStoreData.setKeyStoreName(keyStoreName);
keyStoreData.setCerts(certs);
keyStoreData.setKeyStoreType(keyStoreType);

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
// There be only one entry in WSAS related keystores
if (keyStore.isKeyEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
keyStoreData.setKey(fillCertData(cert, alias, formatter));
PrivateKey key = (PrivateKey) keyStore.getKey(alias, privateKeyPassword
.toCharArray());
String pemKey;
pemKey = "-----BEGIN PRIVATE KEY-----\n";
pemKey += Base64.encode(key.getEncoded());
pemKey += "\n-----END PRIVATE KEY-----";
keyStoreData.setKeyValue(pemKey);
break;
}
}
return keyStoreData;
}

public Key getPrivateKey(String alias, boolean isSuperTenant) throws SecurityConfigException {
KeyStoreData[] keystores = getKeyStores(isSuperTenant);
KeyStore keyStore = null;
Expand Down Expand Up @@ -842,10 +825,7 @@ private PaginatedCertData filterAndPaginateCerts(List<CertData> certDataList, St
*
* @return trust store object
* @throws SecurityConfigException if retrieving the truststore fails.
*
* @deprecated Use {@link KeyStoreManager#getTrustStore()} instead.
*/
@Deprecated
public KeyStore getTrustStore() throws SecurityConfigException {

try {
Expand All @@ -861,10 +841,7 @@ public KeyStore getTrustStore() throws SecurityConfigException {
* @param keyStoreName name of the keystore.
* @return {@link KeyStore} object.
* @throws Exception if retrieving the keystore fails.
*
* @deprecated Use {@link KeyStoreManager#getKeyStore(String)} instead.
*/
@Deprecated
public KeyStore getKeyStore(String keyStoreName) throws Exception {

return this.keyStoreManager.getKeyStore(keyStoreName);
Expand Down

0 comments on commit ea1251d

Please sign in to comment.