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 5 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 @@ -2189,6 +2189,18 @@

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.
//

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

Check warning on line 2202 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#L2202

Added line #L2202 was not covered by tests

for (int connectRetryAttempt = 0, tlsRetryAttempt = 0;;) {
try {
if (0 == elapsedSeconds || elapsedSeconds < loginTimeoutSeconds) {
Expand Down Expand Up @@ -8913,6 +8925,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 8931 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#L8931

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

Check warning on line 8933 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#L8933

Added line #L8933 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 All @@ -87,7 +89,7 @@ public final class SQLServerException extends java.sql.SQLException {
static final int DATA_CLASSIFICATION_NOT_EXPECTED = 11;
static final int DATA_CLASSIFICATION_INVALID_LABEL_INDEX = 12;
static final int DATA_CLASSIFICATION_INVALID_INFORMATION_TYPE_INDEX = 13;

static final java.util.logging.Logger exLogger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.SQLServerException");

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