Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor KeystoreUtils #4139

Draft
wants to merge 1 commit into
base: 4.10.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
public class KeystoreUtils {

private static Log LOG = LogFactory.getLog(KeystoreUtils.class);
private static final String FALLBACK_TENANTED_KEYSTORE_FILE_TYPE = "JKS";
private static final String KEY_STORES = "/repository/security/key-stores";

/**
Expand All @@ -51,7 +50,6 @@ public enum StoreFileType {
PKCS12(".p12");

private final String extension;
private static final String defaultFileType = "PKCS12";

StoreFileType(String extension) {

Expand Down Expand Up @@ -79,7 +77,7 @@ public static String defaultFileType() {
if (StringUtils.isNotBlank(keystoreTypesForNewTenants)) {
return keystoreTypesForNewTenants;
}
return FALLBACK_TENANTED_KEYSTORE_FILE_TYPE;
return JKS.name();
}

/**
Expand Down Expand Up @@ -142,23 +140,25 @@ public static String getKeyStoreFileLocation(String tenantDomain) {
*/
public static String getKeyStoreFileType(String tenantDomain) {

String keystoreType = CarbonUtils.getServerConfiguration().getFirstProperty("Security.KeyStore.Type");
try {
StoreFileType.validateFileType(keystoreType);
} catch (CarbonException e) {
LOG.error("Unsupported file type for key store file", e);
}

String keystoreType;
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
return keystoreType;
}

String ksName = tenantDomain.trim().replace(".", "-");
if (isFileExistInRegistry(ksName + getExtensionByFileType(keystoreType))) {
return keystoreType;
keystoreType = CarbonUtils.getServerConfiguration().getFirstProperty("Security.KeyStore.Type");
try {
StoreFileType.validateFileType(keystoreType);
return keystoreType;
} catch (CarbonException e) {
LOG.error("Unsupported file type for key store file: " + keystoreType, e);
}
} else {
keystoreType = StoreFileType.defaultFileType();
String keystoreExtension = getExtensionByFileType(keystoreType);
String ksName = tenantDomain.trim().replace(".", "-");
if (StoreFileType.PKCS12.name().equals(keystoreType) &&
isFileExistInRegistry(ksName + keystoreExtension)) {
return keystoreType;
}
}

return FALLBACK_TENANTED_KEYSTORE_FILE_TYPE;
return StoreFileType.JKS.name();
}

/**
Expand Down Expand Up @@ -233,7 +233,7 @@ private static boolean isFileExistInRegistry(String keyStoreName) {
*/
public static KeyStore getKeystoreInstance(String keyStoreType) throws KeyStoreException, NoSuchProviderException {

if (StoreFileType.defaultFileType.equals(keyStoreType)) {
if (StoreFileType.PKCS12.name().equals(keyStoreType)) {
return KeyStore.getInstance(keyStoreType, getJCEProvider());
} else {
return KeyStore.getInstance(keyStoreType);
Expand Down