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

Add Capability to retrieve Custom KeyStores #4025

Draft
wants to merge 6 commits into
base: 4.10.x
Choose a base branch
from

Conversation

Binara-Sachin
Copy link

@Binara-Sachin Binara-Sachin commented Jul 4, 2024

Purpose

  • This PR enables retrieving custom keystores from the KeyStoreManager class.
  • Custom KeyStores can be configured in the Carbon.xml file which can then be retrieved using KeyStoreManager

Problem Statement: wso2-enterprise/iam-product-management#67
Git Issue: wso2/product-is#20564

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@@ -160,6 +160,20 @@ public static final class SecurityManagement {
public static final String SERVER_INTERNAL_PRIVATE_KEY_PASSWORD = "Security.InternalKeyStore.KeyPassword";
public static final String SERVER_INTERNAL_KEYSTORE_TYPE = "Security.InternalKeyStore.Type";

//Custom KeyStore related constants

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//Custom KeyStore related constants
// Custom KeyStore related constants.

@@ -124,39 +133,21 @@ public static KeyStoreManager getInstance(int tenantId,
* @return KeyStore object
* @throws Exception If there is not a key store with the given name
*/
public KeyStore getKeyStore(String keyStoreName) throws Exception {
public KeyStore getKeyStore(String keyStoreName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change the public method definitions


return getTenantKeyStore(keyStoreName);
} catch (Exception e) {
log.error("Error loading the key store : " + keyStoreName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log the error here, since we should be printing the error message from the place where the exception is handled

@@ -111,4 +117,53 @@ public static Certificate getCertificate(String alias, KeyStore store) throws Ax
}
}

public static boolean isCustomKeyStore(String keyStoreName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add javadoc comment for the method

return false;
}

public static QName getQNameWithCarbonNS(String localPart) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc comments are required for all the public methods. Specially if they are a util function.

Comment on lines +477 to +478


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary new lines

*/
public PrivateKey getCustomKeyStorePrivateKey(String keyStoreName) throws Exception {

OMElement config = KeyStoreUtil.getCustomKeyStoreConfig(keyStoreName, this.getServerConfigService());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a validation to the keystore name to verify it's a customkeystore

Comment on lines +620 to +625
/**
* This method is used to get public certificates of custom keystores
*
* @return Public certificate of a given custom keystore
* @throws Exception Permission denied for accessing primary key store
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This method is used to get public certificates of custom keystores
*
* @return Public certificate of a given custom keystore
* @throws Exception Permission denied for accessing primary key store
*/
/**
* This method is used to get public certificates of custom keystores.
*
* @return Public certificate of a given custom keystore.
* @throws Exception Permission denied for accessing the primary key store.
*/

Please check other similar places

* @return custom key store object
* @throws Exception Carbon Exception for tenants other than tenant 0
*/
public KeyStore getCustomKeyStore(String keyStoreName) throws Exception {
Copy link

@pamodaaw pamodaaw Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have this method public? It would be better to add the validation for the keystore name to ensure that it is a custom keystore. Otherwise it will cause unexpected behavior

@@ -160,6 +160,20 @@ public static final class SecurityManagement {
public static final String SERVER_INTERNAL_PRIVATE_KEY_PASSWORD = "Security.InternalKeyStore.KeyPassword";
public static final String SERVER_INTERNAL_KEYSTORE_TYPE = "Security.InternalKeyStore.Type";

//Custom KeyStore related constants
public static final class CustomKeyStore {
public static final String KEYSTORE_PREFIX = "CustomKeyStore/";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we have the prefix as CUSTOM/ only? so that the keystore name would be appended.

* @param alias alias of the private key
* @return private key corresponding to the alias
*/
public PrivateKey getTenantPrivateKey(String keyStoreName, String alias) throws Exception{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect to call this method outside this class?

Comment on lines +499 to +503
* This method loads the private key of a given tenant key store
*
* @param keyStoreName name of the tenant key store
* @param alias alias of the private key
* @return private key corresponding to the alias

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This method loads the private key of a given tenant key store
*
* @param keyStoreName name of the tenant key store
* @param alias alias of the private key
* @return private key corresponding to the alias
* This method loads the private key of a given tenant key store.
*
* @param keyStoreName Name of the tenant key store.
* @param alias Alias of the private key.
* @return Private key corresponding to the alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants