Skip to content

Commit

Permalink
Remove scheme from URI before fetching path for CRL path check (#2622)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Jeffery-Wasty and Copilot authored Mar 3, 2025
1 parent 8a77b15 commit b8be643
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -284,20 +287,36 @@ private static void createConnectionRules(LinkedList<String> 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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}."},
Expand Down

0 comments on commit b8be643

Please sign in to comment.