Refactor SQLServerColumnEncryptionAzureKeyVaultProvider usage in SQLServerConnection
This refactors the usage of SQLServerColumnEncryptionAzureKeyVaultProvider into private methods.
This is useful for the native-image compilation GraalVM's optimizations, allowing these methods to be dynamically substituted when the Azure classes are not in the classpath
See for more information
@@ -1749,6 +1749,22 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
KeyStoreAuthentication keyStoreAuthentication = KeyStoreAuthentication.valueOfString(keyStoreAuth);
switch (keyStoreAuthentication) {
case JavaKeyStorePassword:
+ setKeyStoreSecretAndLocation(keyStoreSecret, keyStoreLocation);
+ break;
+ case KeyVaultClientSecret:
+ this.setKeyVaultProvider(keyStorePrincipalId, keyStoreSecret);
+ break;
+ case KeyVaultManagedIdentity:
+ setKeyVaultProvider(keyStorePrincipalId);
+ break;
+ default:
+ // valueOfString would throw an exception if the keyStoreAuthentication is not valid.
+ break;
+ }
+ }
+ }
+
+ private void setKeyStoreSecretAndLocation(String keyStoreSecret, String keyStoreLocation) throws SQLServerException {
// both secret and location must be set for JKS.
if ((null == keyStoreSecret) || (null == keyStoreLocation)) {
throw new SQLServerException(
@@ -1758,18 +1774,9 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
keyStoreLocation, keyStoreSecret.toCharArray());
systemColumnEncryptionKeyStoreProvider.put(provider.getName(), provider);
}
- break;
- case KeyVaultClientSecret:
- // need a secret to use the secret method
- if (null == keyStoreSecret) {
- throw new SQLServerException(SQLServerException.getErrString("R_keyStoreSecretNotSet"), null);
- } else {
- SQLServerColumnEncryptionAzureKeyVaultProvider provider = new SQLServerColumnEncryptionAzureKeyVaultProvider(
- keyStorePrincipalId, keyStoreSecret);
- systemColumnEncryptionKeyStoreProvider.put(provider.getName(), provider);
}
- break;
- case KeyVaultManagedIdentity:
+
+ private void setKeyVaultProvider(String keyStorePrincipalId) throws SQLServerException {
SQLServerColumnEncryptionAzureKeyVaultProvider provider;
if (null != keyStorePrincipalId) {
provider = new SQLServerColumnEncryptionAzureKeyVaultProvider(keyStorePrincipalId);
@@ -1777,11 +1784,16 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
provider = new SQLServerColumnEncryptionAzureKeyVaultProvider();
}
systemColumnEncryptionKeyStoreProvider.put(provider.getName(), provider);
- break;
- default:
- // valueOfString would throw an exception if the keyStoreAuthentication is not valid.
- break;
}
+
+ private void setKeyVaultProvider(String keyStorePrincipalId, String keyStoreSecret) throws SQLServerException {
+ // need a secret to use the secret method
+ if (null == keyStoreSecret) {
+ throw new SQLServerException(SQLServerException.getErrString("R_keyStoreSecretNotSet"), null);
+ } else {
+ SQLServerColumnEncryptionAzureKeyVaultProvider provider = new SQLServerColumnEncryptionAzureKeyVaultProvider(
+ keyStorePrincipalId, keyStoreSecret);
+ systemColumnEncryptionKeyStoreProvider.put(provider.getName(), provider);
}
}
@@ -2053,9 +2065,7 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
SQLServerException.getErrString("R_keyVaultProviderClientKeyNotSet"), null);
}
String keyVaultColumnEncryptionProviderClientKey = sPropValue;
- SQLServerColumnEncryptionAzureKeyVaultProvider provider = new SQLServerColumnEncryptionAzureKeyVaultProvider(
- keyVaultColumnEncryptionProviderClientId, keyVaultColumnEncryptionProviderClientKey);
- systemColumnEncryptionKeyStoreProvider.put(provider.getName(), provider);
+ setKeyVaultProvider(keyVaultColumnEncryptionProviderClientId, keyVaultColumnEncryptionProviderClientKey);
}
sPropKey = SQLServerDriverBooleanProperty.MULTI_SUBNET_FAILOVER.toString();
