From b2a8da9df306b3856c96d39036e28aeba6a039b0 Mon Sep 17 00:00:00 2001 From: Ahmad Vazirna Date: Tue, 23 Jan 2024 14:21:30 +0200 Subject: [PATCH] Rename IEncrypitonKeyProvider to IKeyStoreEncryptionKeyProvider --- .../commcare/util/EncryptionKeyHelper.java | 12 +++---- .../util/EncryptionKeyServiceProvider.java | 36 ------------------- ...va => IKeyStoreEncryptionKeyProvider.java} | 2 +- .../KeyStoreEncryptionKeyServiceProvider.java | 36 +++++++++++++++++++ 4 files changed, 43 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/org/commcare/util/EncryptionKeyServiceProvider.java rename src/main/java/org/commcare/util/{IEncryptionKeyProvider.java => IKeyStoreEncryptionKeyProvider.java} (90%) create mode 100644 src/main/java/org/commcare/util/KeyStoreEncryptionKeyServiceProvider.java diff --git a/src/main/java/org/commcare/util/EncryptionKeyHelper.java b/src/main/java/org/commcare/util/EncryptionKeyHelper.java index beef3b1c2..664c0927a 100644 --- a/src/main/java/org/commcare/util/EncryptionKeyHelper.java +++ b/src/main/java/org/commcare/util/EncryptionKeyHelper.java @@ -18,7 +18,7 @@ public class EncryptionKeyHelper { public static final String CC_KEY_ALGORITHM_RSA = "RSA"; public static final String CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS = "cc-in-memory-encryption-key-alias"; - private static IEncryptionKeyProvider encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl(); + private static IKeyStoreEncryptionKeyProvider keyStoreEncryptionKeyProvider = KeyStoreEncryptionKeyServiceProvider.getInstance().serviceImpl(); /** * Converts a Base64 encoded key into a SecretKey depending on the algorithm @@ -46,8 +46,8 @@ public static EncryptionKeyAndTransformation getKey(String base64encodedKey) } public static boolean isKeyStoreAvailable() { - return encryptionKeyProvider != null && - Security.getProvider(encryptionKeyProvider.getKeyStoreName()) != null; + return keyStoreEncryptionKeyProvider != null && + Security.getProvider(keyStoreEncryptionKeyProvider.getKeyStoreName()) != null; } private static KeyStore keystoreSingleton = null; @@ -55,7 +55,7 @@ public static boolean isKeyStoreAvailable() { private static KeyStore getKeyStore() throws EncryptionKeyException { if (keystoreSingleton == null) { try { - keystoreSingleton = KeyStore.getInstance(encryptionKeyProvider.getKeyStoreName()); + keystoreSingleton = KeyStore.getInstance(keyStoreEncryptionKeyProvider.getKeyStoreName()); keystoreSingleton.load(null); } catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) { @@ -92,13 +92,13 @@ public static EncryptionKeyAndTransformation retrieveKeyFromKeyStore(String keyA key = ((KeyStore.SecretKeyEntry)keyEntry).getSecretKey(); } } else { - key = encryptionKeyProvider.generateCryptographicKeyInKeyStore(keyAlias, cryptographicOperation); + key = keyStoreEncryptionKeyProvider.generateCryptographicKeyInKeyStore(keyAlias, cryptographicOperation); } } catch (KeyStoreException| NoSuchAlgorithmException | UnrecoverableEntryException e) { throw new EncryptionKeyException("Error retrieving key from KeyStore: ", e); } if (key != null) { - return new EncryptionKeyAndTransformation(key, encryptionKeyProvider.getTransformationString()); + return new EncryptionKeyAndTransformation(key, keyStoreEncryptionKeyProvider.getTransformationString()); } else { throw new EncryptionKeyException("Key couldn't be found in the keyStore"); } diff --git a/src/main/java/org/commcare/util/EncryptionKeyServiceProvider.java b/src/main/java/org/commcare/util/EncryptionKeyServiceProvider.java deleted file mode 100644 index a6bbe8c1d..000000000 --- a/src/main/java/org/commcare/util/EncryptionKeyServiceProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.commcare.util; - -import java.util.ServiceLoader; - -/** - * Utility class responsible for finding implementations of IEncryptionKeyProvider during runtime - * and loading them in memory - * - * @author avazirna - */ - -public class EncryptionKeyServiceProvider { - private static EncryptionKeyServiceProvider serviceProvider; - private ServiceLoader loader; - - private EncryptionKeyServiceProvider() { - loader = ServiceLoader.load(IEncryptionKeyProvider.class); - } - - public static EncryptionKeyServiceProvider getInstance() { - if (serviceProvider == null) { - serviceProvider = new EncryptionKeyServiceProvider(); - } - return serviceProvider; - } - - public IEncryptionKeyProvider serviceImpl() { - IEncryptionKeyProvider service = null; - if (loader.iterator().hasNext()) { - service = loader.iterator().next(); - } - - return service; - } -} - diff --git a/src/main/java/org/commcare/util/IEncryptionKeyProvider.java b/src/main/java/org/commcare/util/IKeyStoreEncryptionKeyProvider.java similarity index 90% rename from src/main/java/org/commcare/util/IEncryptionKeyProvider.java rename to src/main/java/org/commcare/util/IKeyStoreEncryptionKeyProvider.java index a3d713175..727eed2c5 100644 --- a/src/main/java/org/commcare/util/IEncryptionKeyProvider.java +++ b/src/main/java/org/commcare/util/IKeyStoreEncryptionKeyProvider.java @@ -8,7 +8,7 @@ * @author avazirna */ -public interface IEncryptionKeyProvider { +public interface IKeyStoreEncryptionKeyProvider { Key generateCryptographicKeyInKeyStore(String keyAlias, EncryptionHelper.CryptographicOperation cryptographicOperation) diff --git a/src/main/java/org/commcare/util/KeyStoreEncryptionKeyServiceProvider.java b/src/main/java/org/commcare/util/KeyStoreEncryptionKeyServiceProvider.java new file mode 100644 index 000000000..c7847ec39 --- /dev/null +++ b/src/main/java/org/commcare/util/KeyStoreEncryptionKeyServiceProvider.java @@ -0,0 +1,36 @@ +package org.commcare.util; + +import java.util.ServiceLoader; + +/** + * Utility class responsible for finding implementations of IEncryptionKeyProvider during runtime + * and loading them in memory + * + * @author avazirna + */ + +public class KeyStoreEncryptionKeyServiceProvider { + private static KeyStoreEncryptionKeyServiceProvider serviceProvider; + private ServiceLoader loader; + + private KeyStoreEncryptionKeyServiceProvider() { + loader = ServiceLoader.load(IKeyStoreEncryptionKeyProvider.class); + } + + public static KeyStoreEncryptionKeyServiceProvider getInstance() { + if (serviceProvider == null) { + serviceProvider = new KeyStoreEncryptionKeyServiceProvider(); + } + return serviceProvider; + } + + public IKeyStoreEncryptionKeyProvider serviceImpl() { + IKeyStoreEncryptionKeyProvider service = null; + if (loader.iterator().hasNext()) { + service = loader.iterator().next(); + } + + return service; + } +} +