This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new b4d7430 Proposed changes to allow specify an instance of KeyStore explicitly (#5856) b4d7430 is described below commit b4d74307b3f52c406a6ccd29087f63827523306a Author: dk2k <d...@users.noreply.github.com> AuthorDate: Fri Jul 23 22:30:26 2021 +0300 Proposed changes to allow specify an instance of KeyStore explicitly (#5856) * Proposed changes to allow specify an instance of KeyStore explicitly * removed WARN log message Co-authored-by: dk2k <d...@ya.ru> --- .../camel/support/jsse/KeyStoreParameters.java | 52 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyStoreParameters.java b/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyStoreParameters.java index 5575cdb..33d1fe8a 100644 --- a/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyStoreParameters.java +++ b/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyStoreParameters.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.security.GeneralSecurityException; import java.security.KeyStore; +import java.security.KeyStoreException; import java.security.Security; import java.util.Enumeration; import java.util.LinkedList; @@ -53,6 +54,12 @@ public class KeyStoreParameters extends JsseParameters { protected String provider; /** + * The optional key store, which has higher priority then value in resource below. If keyStore is non-null, resource + * isn't taken into account. This is helpful say for in-memory KeyStore composed by the user "on the fly". + */ + protected KeyStore keyStore; + + /** * The optional file path, class path resource, or URL of the resource used to load the key store. */ protected String resource; @@ -126,6 +133,16 @@ public class KeyStoreParameters extends JsseParameters { } /** + * Sets the optional key store, which has higher priority then value in resource. NB Don't forget to call + * setPassword() for password of this KeyStore. + * + * @param keyStore the KeyStore (may be {@code null}) + */ + public void setKeyStore(KeyStore keyStore) { + this.keyStore = keyStore; + } + + /** * Creates a {@link KeyStoreParameters} instance based off of the configuration state of this instance. If * {@link #getType()} returns {@code null}, the default key store type is loaded, otherwise the type will be of that * specified. @@ -134,12 +151,22 @@ public class KeyStoreParameters extends JsseParameters { * returns {@code null}, the instance will be empty. The loading of the resource, if not {@code null}, is attempted * by treating the resource as a file path, a class path resource, and a URL in that order. An exception is thrown * if the resource cannot be resolved to readable input stream using any of the above methods. - * + * * @return a configured and loaded key store * @throws GeneralSecurityException if there is an error creating an instance with the given configuration * @throws IOException if there is an error resolving the configured resource to an input stream */ public KeyStore createKeyStore() throws GeneralSecurityException, IOException { + if (keyStore != null) { + if (LOG.isDebugEnabled()) { + List<String> aliases = extractAliases(keyStore); + LOG.debug( + "KeyStore [{}], initialized from [{}], is using provider [{}], has type [{}], and contains aliases {}.", + keyStore, this, keyStore.getProvider(), keyStore.getType(), aliases); + } + return keyStore; + } + LOG.trace("Creating KeyStore instance from KeyStoreParameters [{}].", this); String ksType = this.parsePropertyValue(this.type); @@ -167,13 +194,7 @@ public class KeyStoreParameters extends JsseParameters { } if (LOG.isDebugEnabled()) { - List<String> aliases = new LinkedList<>(); - - Enumeration<String> aliasEnum = ks.aliases(); - while (aliasEnum.hasMoreElements()) { - aliases.add(aliasEnum.nextElement()); - } - + List<String> aliases = extractAliases(ks); LOG.debug("KeyStore [{}], initialized from [{}], is using provider [{}], has type [{}], and contains aliases {}.", ks, this, ks.getProvider(), ks.getType(), aliases); } @@ -181,6 +202,21 @@ public class KeyStoreParameters extends JsseParameters { return ks; } + private List<String> extractAliases(KeyStore ks) { + List<String> aliases = new LinkedList<>(); + + Enumeration<String> aliasEnum = null; + try { + aliasEnum = ks.aliases(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } + while (aliasEnum.hasMoreElements()) { + aliases.add(aliasEnum.nextElement()); + } + return aliases; + } + @Override public String toString() { StringBuilder builder = new StringBuilder();