Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed scheme from URI before fetching path for CRL path check #2622

Merged
merged 6 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 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);

Check warning on line 319 in src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java#L316-L319

Added lines #L316 - L319 were not covered by tests
} 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
Loading