Skip to content

Commit

Permalink
Merge branch 'main' into access-token-callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyc authored Nov 3, 2022
2 parents e6da1ba + 1797e17 commit cd34338
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 158 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ dependencies {
implementation 'org.osgi:org.osgi.core:6.0.0',
'org.osgi:org.osgi.compendium:5.0.0'
compileOnly 'com.azure:azure-security-keyvault-keys:4.2.8',
'com.azure:azure-identity:1.3.3',
'com.azure:azure-identity:1.7.0-beta.2',
'org.antlr:antlr4-runtime:4.9.2',
'com.google.code.gson:gson:2.8.7',
'org.bouncycastle:bcprov-jdk15on:1.69',
Expand All @@ -152,7 +152,7 @@ dependencies {
'com.google.code.gson:gson:2.8.7',
'org.bouncycastle:bcprov-jdk15on:1.69',
'com.azure:azure-security-keyvault-keys:4.2.8',
'com.azure:azure-identity:1.3.3',
'com.azure:azure-identity:1.7.0-beta.2',
'com.microsoft.azure:adal4j:1.6.7',
'com.h2database:h2:1.4.200'
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@

<!-- Driver Dependencies -->
<org.osgi.core.version>6.0.0</org.osgi.core.version>
<azure-security-keyvault-keys.version>4.4.4</azure-security-keyvault-keys.version>
<azure-security-keyvault-keys.version>4.5.1</azure-security-keyvault-keys.version>
<azure-identity.version>1.7.0-beta.2</azure-identity.version>
<msal.version>1.13.0</msal.version>
<msal.version>1.13.3</msal.version>
<org.osgi.compendium.version>5.0.0</org.osgi.compendium.version>
<antlr-runtime.version>4.9.3</antlr-runtime.version>
<com.google.code.gson.version>2.9.0</com.google.code.gson.version>
Expand Down Expand Up @@ -109,7 +109,7 @@
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.13.0</version>
<version>1.13.3</version>
<optional>true</optional>
</dependency>

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,7 @@ enum Result {

// list of addresses for ip selection by type preference
private static ArrayList<InetAddress> addressList = new ArrayList<>();
private static final Lock ADDRESS_LIST_LOCK = new ReentrantLock();
private static final Lock addressListLock = new ReentrantLock();

/**
* Constructs a new SocketFinder object with appropriate traceId
Expand Down Expand Up @@ -2765,7 +2765,7 @@ private SocketFactory getSocketFactory() throws IOException {
*/
private InetSocketAddress getInetAddressByIPPreference(String hostName,
int portNumber) throws IOException, SQLServerException {
ADDRESS_LIST_LOCK.lock();
addressListLock.lock();
try {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (int i = 0; i < addressList.size(); i++) {
Expand All @@ -2775,7 +2775,7 @@ private InetSocketAddress getInetAddressByIPPreference(String hostName,
}
return addr;
} finally {
ADDRESS_LIST_LOCK.unlock();
addressListLock.unlock();
}
}

Expand Down Expand Up @@ -2838,7 +2838,7 @@ private Socket getSocketByIPPreference(String hostName, int portNumber, int time
// Note that Socket(host, port) throws an UnknownHostException if the host name
// cannot be resolved, but that InetSocketAddress(host, port) does not - it sets
// the returned InetSocketAddress as unresolved.
if (addr.isUnresolved()) {
if (addr != null && addr.isUnresolved()) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Failed to resolve host name: " + hostName
+ ". Using IP address from DNS cache.");
Expand All @@ -2859,7 +2859,7 @@ private Socket getSocketByIPPreference(String hostName, int portNumber, int time
* Boolean switch for IPv6 first
*/
private void fillAddressList(InetAddress[] addresses, boolean ipv6first) {
ADDRESS_LIST_LOCK.lock();
addressListLock.lock();
try {
addressList.clear();
if (ipv6first) {
Expand All @@ -2876,7 +2876,7 @@ private void fillAddressList(InetAddress[] addresses, boolean ipv6first) {
}
}
} finally {
ADDRESS_LIST_LOCK.unlock();
addressListLock.unlock();
}
}

Expand Down Expand Up @@ -4155,7 +4155,7 @@ void writeString(String value) throws SQLServerException {
int charsCopied = 0;
int length = value.length();
while (charsCopied < length) {
long bytesToCopy = 2 * (length - charsCopied);
long bytesToCopy = 2 * ((long) length - charsCopied);

if (bytesToCopy > valueBytes.length)
bytesToCopy = valueBytes.length;
Expand Down Expand Up @@ -6053,7 +6053,8 @@ private void writeScaledTemporal(GregorianCalendar cal, int subSecondNanos, int
+ 60 * 60 * cal.get(Calendar.HOUR_OF_DAY);

// Scale nanos since midnight to the desired scale, rounding the value as necessary
long divisor = Nanos.PER_MAX_SCALE_INTERVAL * (long) Math.pow(10, TDS.MAX_FRACTIONAL_SECONDS_SCALE - scale);
long divisor = Nanos.PER_MAX_SCALE_INTERVAL
* (long) Math.pow(10, TDS.MAX_FRACTIONAL_SECONDS_SCALE - (double) scale);

// The scaledNanos variable represents the fractional seconds of the value at the scale
// indicated by the scale variable. So, for example, scaledNanos = 3 means 300 nanoseconds
Expand Down Expand Up @@ -6195,7 +6196,8 @@ byte[] writeEncryptedScaledTemporal(GregorianCalendar cal, int subSecondNanos, i
+ 60 * 60 * cal.get(Calendar.HOUR_OF_DAY);

// Scale nanos since midnight to the desired scale, rounding the value as necessary
divisor = Nanos.PER_MAX_SCALE_INTERVAL * (long) Math.pow(10, TDS.MAX_FRACTIONAL_SECONDS_SCALE - scale);
divisor = Nanos.PER_MAX_SCALE_INTERVAL
* (long) Math.pow(10, TDS.MAX_FRACTIONAL_SECONDS_SCALE - (double) scale);

// The scaledNanos variable represents the fractional seconds of the value at the scale
// indicated by the scale variable. So, for example, scaledNanos = 3 means 300 nanoseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public void run() {
} else {
try {
if (connectRetryCount > 1) {
Thread.sleep(con.getRetryInterval() * 1000);
Thread.sleep((long) (con.getRetryInterval()) * 1000);
}
} catch (InterruptedException ie) {
// re-interrupt thread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ private int findColumn(String columnName) throws SQLServerException {

// 1. Search using case-sensitive non-locale specific (binary) compare first.
// 2. Search using case-insensitive, non-locale specific (binary) compare last.
Integer matchPos = parameterNames.get(columnNameWithSign);
Integer matchPos = (parameterNames != null) ? parameterNames.get(columnNameWithSign) : null;
if (null == matchPos) {
matchPos = insensitiveParameterNames.get(columnNameWithSign);
}
Expand Down
43 changes: 22 additions & 21 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
* lock instance for "this"
**/
private final Lock lock = new ReentrantLock();

/**
* static lock instance for the class
**/
private static final Lock LOCK = new ReentrantLock();
private static final Lock sLock = new ReentrantLock();

/**
* Return an existing cached SharedTimer associated with this Connection or create a new one.
Expand Down Expand Up @@ -938,7 +939,7 @@ public static void registerColumnEncryptionKeyStoreProviders(
loggerExternal.entering(loggingClassNameBase, "registerColumnEncryptionKeyStoreProviders",
"Registering Column Encryption Key Store Providers");

LOCK.lock();
sLock.lock();
try {
if (null == clientKeyStoreProviders) {
throw new SQLServerException(null, SQLServerException.getErrString("R_CustomKeyStoreProviderMapNull"),
Expand Down Expand Up @@ -979,7 +980,7 @@ public static void registerColumnEncryptionKeyStoreProviders(
globalCustomColumnEncryptionKeyStoreProviders.put(providerName, provider);
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "registerColumnEncryptionKeyStoreProviders",
Expand All @@ -995,14 +996,14 @@ public static void unregisterColumnEncryptionKeyStoreProviders() {
loggerExternal.entering(loggingClassNameBase, "unregisterColumnEncryptionKeyStoreProviders",
"Removing Column Encryption Key Store Provider");

LOCK.lock();
sLock.lock();
try {
if (null != globalCustomColumnEncryptionKeyStoreProviders) {
globalCustomColumnEncryptionKeyStoreProviders.clear();
globalCustomColumnEncryptionKeyStoreProviders = null;
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "unregisterColumnEncryptionKeyStoreProviders",
Expand Down Expand Up @@ -1224,15 +1225,15 @@ public static void setColumnEncryptionTrustedMasterKeyPaths(Map<String, List<Str
loggerExternal.entering(loggingClassNameBase, "setColumnEncryptionTrustedMasterKeyPaths",
"Setting Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.clear();
for (Map.Entry<String, List<String>> entry : trustedKeyPaths.entrySet()) {
columnEncryptionTrustedMasterKeyPaths.put(entry.getKey().toUpperCase(), entry.getValue());
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "setColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1251,12 +1252,12 @@ public static void updateColumnEncryptionTrustedMasterKeyPaths(String server, Li
loggerExternal.entering(loggingClassNameBase, "updateColumnEncryptionTrustedMasterKeyPaths",
"Updating Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.put(server.toUpperCase(), trustedKeyPaths);
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "updateColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1273,12 +1274,12 @@ public static void removeColumnEncryptionTrustedMasterKeyPaths(String server) {
loggerExternal.entering(loggingClassNameBase, "removeColumnEncryptionTrustedMasterKeyPaths",
"Removing Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.remove(server.toUpperCase());
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "removeColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1294,7 +1295,7 @@ public static Map<String, List<String>> getColumnEncryptionTrustedMasterKeyPaths
loggerExternal.entering(loggingClassNameBase, "getColumnEncryptionTrustedMasterKeyPaths",
"Getting Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
Map<String, List<String>> masterKeyPathCopy = new HashMap<>();

Expand All @@ -1307,12 +1308,12 @@ public static Map<String, List<String>> getColumnEncryptionTrustedMasterKeyPaths

return masterKeyPathCopy;
} finally {
LOCK.unlock();
sLock.unlock();
}
}

static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Boolean[] hasEntry) {
LOCK.lock();
sLock.lock();
try {
if (columnEncryptionTrustedMasterKeyPaths.containsKey(server)) {
hasEntry[0] = true;
Expand All @@ -1322,7 +1323,7 @@ static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Bool
return null;
}
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand All @@ -1331,11 +1332,11 @@ static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Bool
* request to acquire an access token.
*/
public static void clearUserTokenCache() {
LOCK.lock();
sLock.lock();
try {
PersistentTokenCacheAccessAspect.clearUserTokenCache();
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand Down Expand Up @@ -7469,7 +7470,7 @@ void doSecurityCheck() {
*/
public static void setColumnEncryptionKeyCacheTtl(int columnEncryptionKeyCacheTTL,
TimeUnit unit) throws SQLServerException {
LOCK.lock();
sLock.lock();
try {
if (columnEncryptionKeyCacheTTL < 0 || unit.equals(TimeUnit.MILLISECONDS)
|| unit.equals(TimeUnit.MICROSECONDS) || unit.equals(TimeUnit.NANOSECONDS)) {
Expand All @@ -7479,16 +7480,16 @@ public static void setColumnEncryptionKeyCacheTtl(int columnEncryptionKeyCacheTT

columnEncryptionKeyCacheTtl = TimeUnit.SECONDS.convert(columnEncryptionKeyCacheTTL, unit);
} finally {
LOCK.unlock();
sLock.unlock();
}
}

static long getColumnEncryptionKeyCacheTtl() {
LOCK.lock();
sLock.lock();
try {
return columnEncryptionKeyCacheTtl;
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand Down
Loading

0 comments on commit cd34338

Please sign in to comment.