Skip to content

Invalidate existing enclave session during connection (re)connect #2638

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

Merged
merged 7 commits into from
May 8, 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 @@ -2195,6 +2195,11 @@

long elapsedSeconds = 0;
long start = System.currentTimeMillis();

// Any existing enclave session would be invalid, make sure it is invalidated.
// For example, if this is a session recovery reconnect.
//
invalidateEnclaveSessionCache();
for (int connectRetryAttempt = 0, tlsRetryAttempt = 0;;) {
try {
if (0 == elapsedSeconds || elapsedSeconds < loginTimeoutSeconds) {
Expand Down Expand Up @@ -8969,6 +8974,15 @@
parameterNames);
}

void invalidateEnclaveSessionCache() {
if (enclaveProvider != null) {
if (connectionlogger.isLoggable(Level.FINE)) {
connectionlogger.fine("Invalidating existing enclave session for enclave provider : " + enclaveProvider);

Check warning on line 8980 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java#L8980

Added line #L8980 was not covered by tests
}
enclaveProvider.invalidateEnclaveSession();

Check warning on line 8982 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java#L8982

Added line #L8982 was not covered by tests
}
}

boolean enclaveEstablished() {
return (null != enclaveProvider.getEnclaveSession());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public final class SQLServerException extends java.sql.SQLException {
// Built-in function '%.*ls' in impersonation context is not supported in this version of SQL Server.
static final int IMPERSONATION_CONTEXT_NOT_SUPPORTED = 40529;

static final int INVAID_ENCLAVE_SESSION_HANDLE_ERROR = 33195;

// Facility for driver-specific error codes
static final int DRIVER_ERROR_NONE = 0;
static final int DRIVER_ERROR_FROM_DATABASE = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,12 @@
startResults();
getNextResult(true);
} catch (SQLException e) {
if (connection.isAEv2() && (e.getErrorCode() == SQLServerException.INVAID_ENCLAVE_SESSION_HANDLE_ERROR)) {
//If the exception received is as below then just invalidate the cache
//code = '33195', SQL state = 'S0001': Internal enclave error. Enclave was provided with an invalid session handle. For more information, contact Customer Support Services..
//
connection.invalidateEnclaveSessionCache();

Check warning on line 694 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java#L694

Added line #L694 was not covered by tests
}
if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare, false)) {
continue;
} else if (!inRetry && connection.doesServerSupportEnclaveRetry()) {
Expand Down Expand Up @@ -3119,6 +3125,12 @@
assert numBatchesExecuted == numBatchesPrepared;
}
} catch (SQLException e) {
if (connection.isAEv2() && (e.getErrorCode() == SQLServerException.INVAID_ENCLAVE_SESSION_HANDLE_ERROR)) {
//If the exception received is as below then just invalidate the cache
//code = '33195', SQL state = 'S0001': Internal enclave error. Enclave was provided with an invalid session handle. For more information, contact Customer Support Services..
//
connection.invalidateEnclaveSessionCache();

Check warning on line 3132 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java#L3132

Added line #L3132 was not covered by tests
}
if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare, true)
&& connection.isStatementPoolingEnabled()) {
// Reset number of batches prepared.
Expand Down
Loading