Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/microsoft/mssql-jdbc into d…
Browse files Browse the repository at this point in the history
…ev/machavan/issue#2550
  • Loading branch information
machavan committed Jan 20, 2025
2 parents 18c96b5 + 2a3d372 commit b57799b
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


/**
* Provides an interface to the {@link SQLServerConnection} and {@link SQLServerConnectionPoolProxy} classes.
* Provides an interface to the {@link SQLServerConnection} class.
*/
public interface ISQLServerConnection extends java.sql.Connection {

Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2446,7 +2446,7 @@ Connection connectInternal(Properties propsIn,
if (null != sPropValue)
validateMaxSQLLoginName(sPropKey, sPropValue);
else
activeConnectionProperties.setProperty(sPropKey, SQLServerDriver.DEFAULT_APP_NAME);
activeConnectionProperties.setProperty(sPropKey, SQLServerDriver.constructedAppName);

sPropKey = SQLServerDriverBooleanProperty.LAST_UPDATE_COUNT.toString();
sPropValue = activeConnectionProperties.getProperty(sPropKey);
Expand Down Expand Up @@ -6110,10 +6110,11 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
}

while (true) {
int millisecondsRemaining = timerRemaining(timerExpire);
if (authenticationString.equalsIgnoreCase(SqlAuthentication.ACTIVE_DIRECTORY_PASSWORD.toString())) {
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthToken(fedAuthInfo, user,
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
authenticationString);
authenticationString, millisecondsRemaining);

// Break out of the retry loop in successful case.
break;
Expand All @@ -6125,12 +6126,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw

if (null != managedIdentityClientId && !managedIdentityClientId.isEmpty()) {
fedAuthToken = SQLServerSecurityUtility.getManagedIdentityCredAuthToken(fedAuthInfo.spn,
managedIdentityClientId);
managedIdentityClientId, millisecondsRemaining);
break;
}

fedAuthToken = SQLServerSecurityUtility.getManagedIdentityCredAuthToken(fedAuthInfo.spn,
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()));
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()), millisecondsRemaining);

// Break out of the retry loop in successful case.
break;
Expand All @@ -6141,12 +6142,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
if (aadPrincipalID != null && !aadPrincipalID.isEmpty() && aadPrincipalSecret != null
&& !aadPrincipalSecret.isEmpty()) {
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenPrincipal(fedAuthInfo, aadPrincipalID,
aadPrincipalSecret, authenticationString);
aadPrincipalSecret, authenticationString, millisecondsRemaining);
} else {
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenPrincipal(fedAuthInfo,
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.USER.toString()),
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
authenticationString);
authenticationString, millisecondsRemaining);
}

// Break out of the retry loop in successful case.
Expand All @@ -6159,7 +6160,7 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.USER.toString()),
servicePrincipalCertificate,
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
servicePrincipalCertificateKey, servicePrincipalCertificatePassword, authenticationString);
servicePrincipalCertificateKey, servicePrincipalCertificatePassword, authenticationString, millisecondsRemaining);

// Break out of the retry loop in successful case.
break;
Expand Down Expand Up @@ -6194,7 +6195,7 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
throw new SQLServerException(form.format(msgArgs), null);
}

int millisecondsRemaining = timerRemaining(timerExpire);
millisecondsRemaining = timerRemaining(timerExpire);
if (ActiveDirectoryAuthentication.GET_ACCESS_TOKEN_TRANSIENT_ERROR != errorCategory
|| timerHasExpired(timerExpire) || (fedauthSleepInterval >= millisecondsRemaining)) {

Expand Down Expand Up @@ -6240,15 +6241,15 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
Object[] msgArgs = {SQLServerDriver.AUTH_DLL_NAME, authenticationString};
throw new SQLServerException(form.format(msgArgs), null, 0, null);
}
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenIntegrated(fedAuthInfo, authenticationString);
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenIntegrated(fedAuthInfo, authenticationString, millisecondsRemaining);
}
// Break out of the retry loop in successful case.
break;
} else if (authenticationString
.equalsIgnoreCase(SqlAuthentication.ACTIVE_DIRECTORY_INTERACTIVE.toString())) {
// interactive flow
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenInteractive(fedAuthInfo, user,
authenticationString);
authenticationString, millisecondsRemaining);

// Break out of the retry loop in successful case.
break;
Expand All @@ -6258,12 +6259,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw

if (null != managedIdentityClientId && !managedIdentityClientId.isEmpty()) {
fedAuthToken = SQLServerSecurityUtility.getDefaultAzureCredAuthToken(fedAuthInfo.spn,
managedIdentityClientId);
managedIdentityClientId, millisecondsRemaining);
break;
}

fedAuthToken = SQLServerSecurityUtility.getDefaultAzureCredAuthToken(fedAuthInfo.spn,
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()));
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()), millisecondsRemaining);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void setApplicationName(String applicationName) {
@Override
public String getApplicationName() {
return getStringProperty(connectionProps, SQLServerDriverStringProperty.APPLICATION_NAME.toString(),
SQLServerDriverStringProperty.APPLICATION_NAME.getDefaultValue());
SQLServerDriver.constructedAppName);
}

/**
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,32 @@ public final class SQLServerDriver implements java.sql.Driver {
static final String AUTH_DLL_NAME = "mssql-jdbc_auth-" + SQLJdbcVersion.MAJOR + "." + SQLJdbcVersion.MINOR + "."
+ SQLJdbcVersion.PATCH + "." + Util.getJVMArchOnWindows() + SQLJdbcVersion.RELEASE_EXT;
static final String DEFAULT_APP_NAME = "Microsoft JDBC Driver for SQL Server";
static final String APP_NAME_TEMPLATE = "Microsoft JDBC - %s, %s - %s";
static final String constructedAppName;
static {
constructedAppName = getAppName();
}

/**
* Constructs the application name using system properties for OS, platform, and architecture.
* If any of the properties cannot be fetched, it falls back to the default application name.
* Format -> Microsoft JDBC - {OS}, {Platform} - {architecture}
*
* @return the constructed application name or the default application name if properties are not available
*/
static String getAppName() {
String osName = System.getProperty("os.name", "");
String osArch = System.getProperty("os.arch", "");
String javaVmName = System.getProperty("java.vm.name", "");
String javaVmVersion = System.getProperty("java.vm.version", "");
String platform = javaVmName.isEmpty() || javaVmVersion.isEmpty() ? "" : javaVmName + " " + javaVmVersion;

if (osName.isEmpty() && platform.isEmpty() && osArch.isEmpty()) {
return DEFAULT_APP_NAME;
}
return String.format(APP_NAME_TEMPLATE, osName, platform, osArch);
}

private static final String[] TRUE_FALSE = {"true", "false"};

private static final SQLServerDriverPropertyInfo[] DRIVER_PROPERTIES = {
Expand All @@ -741,7 +766,7 @@ public final class SQLServerDriver implements java.sql.Driver {
SQLServerDriverStringProperty.APPLICATION_INTENT.getDefaultValue(), false,
new String[] {ApplicationIntent.READ_ONLY.toString(), ApplicationIntent.READ_WRITE.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.APPLICATION_NAME.toString(),
SQLServerDriverStringProperty.APPLICATION_NAME.getDefaultValue(), false, null),
SQLServerDriverStringProperty.APPLICATION_NAME.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.COLUMN_ENCRYPTION.toString(),
SQLServerDriverStringProperty.COLUMN_ENCRYPTION.getDefaultValue(), false,
new String[] {ColumnEncryptionSetting.DISABLED.toString(),
Expand Down Expand Up @@ -1028,6 +1053,9 @@ String getClassNameLogging() {
drLogger.finer("Error registering driver: " + e);
}
}
if (loggerExternal.isLoggable(Level.FINE)) {
loggerExternal.log(Level.FINE, "Application Name: " + SQLServerDriver.constructedAppName);
}
}

// Check for jdk.net.ExtendedSocketOptions to set TCP keep-alive options for idle connection resiliency
Expand Down Expand Up @@ -1266,6 +1294,9 @@ public java.sql.Connection connect(String url, Properties suppliedProperties) th
Properties connectProperties = parseAndMergeProperties(url, suppliedProperties);
if (connectProperties != null) {
result = DriverJDBCVersion.getSQLServerConnection(toString());
if (connectProperties.getProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString()) == null) {
connectProperties.setProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString(), SQLServerDriver.constructedAppName);
}
result.connect(connectProperties, null);
}
loggerExternal.exiting(getClassNameLogging(), "connect", result);
Expand Down
Loading

0 comments on commit b57799b

Please sign in to comment.