From b8be6434f67702ae9f87157e08930dd46588bbc7 Mon Sep 17 00:00:00 2001 From: Jeff Wasty Date: Mon, 3 Mar 2025 15:55:21 -0800 Subject: [PATCH] Remove scheme from URI before fetching path for CRL path check (#2622) * Remove scheme from URI before fetching path for CRL path check * Update src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Committed nitpick mentioned by copilot * Added additional check + greater clarity * The issue was with jar:file:/ NOT jar:/, corrected. * Forgot a case where there would be no scheme --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../jdbc/ConfigurableRetryLogic.java | 25 ++++++++++++++++--- .../sqlserver/jdbc/SQLServerResource.java | 3 ++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java b/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java index da8facdd8..0a1bd4e44 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java @@ -13,6 +13,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Paths; import java.text.MessageFormat; import java.util.Collections; @@ -39,9 +40,11 @@ public class ConfigurableRetryLogic { private static final String SEMI_COLON = ";"; private static final String COMMA = ","; private static final String EQUALS_SIGN = "="; + private static final String FORWARD_SLASH = "/"; private static final String RETRY_EXEC = "retryExec"; private static final String RETRY_CONN = "retryConn"; private static final String STATEMENT = "statement"; + private static final String CLASS_FILES_SUFFIX = "target/classes/"; private static boolean replaceFlag = false; // Are we replacing the list of transient errors? /** * The time the properties file was last modified. @@ -284,20 +287,36 @@ private static void createConnectionRules(LinkedList listOfRules) throws private static String getCurrentClassPath() throws SQLServerException { String location = ""; String className = ""; + String uriToString = ""; try { className = new Object() {}.getClass().getEnclosingClass().getName(); location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath(); + URI uri = ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().getLocation() + .toURI(); + + uriToString = uri.toString(); + + int initialIndexOfForwardSlash = uriToString.indexOf(FORWARD_SLASH); + + if (!uri.getScheme().isEmpty() && initialIndexOfForwardSlash > 0) { + // If the URI has a scheme, i.e. jar:file:, jar:, or file: then we create a substring from the + // forward slash onwards. + uriToString = uriToString.substring(initialIndexOfForwardSlash + 1); + } - if (Files.isDirectory(Paths - .get(ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) { + if (Files.isDirectory(Paths.get(uriToString))) { // We check if the Path we get from the CodeSource location is a directory. If so, we are running // from class files and should remove a suffix (i.e. the props file is in a different location from the // location returned) - location = location.substring(0, location.length() - ("target/classes/").length()); + location = location.substring(0, location.length() - CLASS_FILES_SUFFIX.length()); } return new URI(location).getPath() + DEFAULT_PROPS_FILE; // TODO: Allow custom paths + } catch (InvalidPathException e) { + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_PathInvalid")); + Object[] msgArgs = {uriToString}; + throw new SQLServerException(form.format(msgArgs), null, 0, e); } catch (URISyntaxException e) { MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid")); Object[] msgArgs = {location}; diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java index 942bfdc26..435dd286b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java @@ -369,7 +369,8 @@ protected Object[][] getContents() { {"R_ForceEncryptionTrue_HonorAETrue_UnencryptedColumn", "Cannot execute statement or procedure {0} because Force Encryption was set as true for parameter {1} and the database expects this parameter to be sent as plaintext. This may be due to a configuration error."}, {"R_ForceEncryptionTrue_HonorAEFalseRS", "Cannot set Force Encryption to true for parameter {0} because encryption is not enabled for the statement or procedure."}, {"R_ForceEncryptionTrue_HonorAETrue_UnencryptedColumnRS", "Cannot execute update because Force Encryption was set as true for parameter {0} and the database expects this parameter to be sent as plaintext. This may be due to a configuration error."}, - {"R_NullValue", "{0} cannot be null."}, + {"R_NullValue", "{0} cannot be null."}, + {"R_PathInvalid", "Invalid path specified: {0}."}, {"R_URLInvalid", "Invalid URL specified: {0}."}, {"R_AKVPathNull", "Azure Key Vault key path cannot be null."}, {"R_AKVMasterKeyPathInvalid", "Invalid Azure Key Vault key path specified: {0}."},