Skip to content

Commit

Permalink
Remove updateTruststore method from KeystoreManager
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Oct 15, 2024
1 parent cca1bc9 commit 9be0b9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,18 @@ public void addKeyStore(byte[] content, String filename, String password, String
}
}

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

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

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

try {
keyStoreManager.addTrustStore(content, filename, password, provider, type);
keyStoreManager.addKeyStore(content, filename, password, provider, type, null);
} catch (CarbonException e) {
String msg = "Error when adding a trustStore";
log.error(msg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ public void addKeyStore(String fileData, String filename, String password, Strin
@Override
public void addTrustStore(String fileData, String filename, String password, String provider,
String type) throws SecurityConfigException {

KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(
CarbonContext.getThreadLocalCarbonContext().getTenantId());
try {
keyStoreManager.addTrustStore(Base64.decode(fileData), filename, password, provider, type);
} catch (CarbonException e) {
throw new SecurityConfigException(e.getMessage());
}
KeyStoreAdmin admin = new KeyStoreAdmin(CarbonContext.getThreadLocalCarbonContext().getTenantId(),
getGovernanceSystemRegistry());
admin.addTrustStore(fileData, filename, password, provider, type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public class KeyStoreAdminTest extends IdentityBaseTest {
private KeyStoreManager keyStoreManager;
@Mock
private Registry registry;
@Mock
private CryptoUtil cryptoUtil;
@Mock
private Resource resource;
private KeyStoreAdmin keyStoreAdmin;
private final int tenantID = -1234;

Expand Down Expand Up @@ -102,14 +106,22 @@ public void testAddTrustStore() throws Exception {

byte[] keyStoreContent = readBytesFromFile(createPath(KEYSTORE_NAME).toString());

try (MockedStatic<KeyStoreManager> keyStoreManager = mockStatic(KeyStoreManager.class);
try (MockedStatic<CryptoUtil>cryptoUtilMockedStatic = mockStatic(CryptoUtil.class);
MockedStatic<KeyStoreManager> keyStoreManager = mockStatic(KeyStoreManager.class);
MockedStatic<KeyStoreUtil> keyStoreUtil = mockStatic(KeyStoreUtil.class)) {

keyStoreManager.when(() -> KeyStoreManager.getInstance(anyInt())).thenReturn(this.keyStoreManager);

keyStoreUtil.when(() -> KeyStoreUtil.isPrimaryStore(any())).thenReturn(false);
keyStoreUtil.when(() -> KeyStoreUtil.isTrustStore(any())).thenReturn(true);

// Mocking Registry interactions
when(registry.newResource()).thenReturn(resource);

// Mocking password encryption
cryptoUtilMockedStatic.when(CryptoUtil::getDefaultCryptoUtil).thenReturn(cryptoUtil);
when(cryptoUtil.encryptAndBase64Encode(any())).thenReturn("encryptedPassword");

keyStoreAdmin = new KeyStoreAdmin(tenantID, registry);
keyStoreAdmin.addTrustStore(keyStoreContent, "new_truststore.jks", KEYSTORE_PASSWORD, " ", "JKS");
}
Expand Down

0 comments on commit 9be0b9a

Please sign in to comment.