Skip to content

Commit

Permalink
Set appropriate value to requestedEncryptionLevel for encrypt=STRICT (#…
Browse files Browse the repository at this point in the history
…2597)

* Set appropriate value to requestedEncryptionLevel for encrypt=STRICT

* Added test case testManagedIdentityWithEncryptStrict

---------

Co-authored-by: Muskan Gupta <muskgupta@microsoft.com>
  • Loading branch information
machavan and muskan124947 authored Feb 19, 2025
1 parent 4a0a7bc commit 2c3db81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4147,7 +4147,11 @@ void prelogin(String serverName, int portNumber) throws SQLServerException {
final byte fedAuthOffset;
if (fedAuthRequiredByUser) {
messageLength = TDS.B_PRELOGIN_MESSAGE_LENGTH_WITH_FEDAUTH;
requestedEncryptionLevel = TDS.ENCRYPT_ON;
if (encryptOption.compareToIgnoreCase(EncryptOption.STRICT.toString()) == 0) {
requestedEncryptionLevel = TDS.ENCRYPT_NOT_SUP;
} else {
requestedEncryptionLevel = TDS.ENCRYPT_ON;
}

// since we added one more line for prelogin option with fedauth,
// we also needed to modify the offsets above, by adding 5 to each offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.microsoft.sqlserver.jdbc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -1370,4 +1371,35 @@ public void testGetSqlFedAuthTokenFailureNagativeWaiting() throws SQLException {
}
}

@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLMI)
@Tag(Constants.xSQLv11)
@Tag(Constants.xSQLv12)
@Tag(Constants.xSQLv14)
@Tag(Constants.xSQLv15)
@Tag(Constants.xSQLv16)
public void testManagedIdentityWithEncryptStrict() {
SQLServerDataSource ds = new SQLServerDataSource();

String connectionUrl = connectionString;
if (connectionUrl.contains("user=")) {
connectionUrl = TestUtils.removeProperty(connectionUrl, "user");
}
if (connectionUrl.contains("password=")) {
connectionUrl = TestUtils.removeProperty(connectionUrl, "password");
}

ds.setURL(connectionUrl);
ds.setAuthentication("ActiveDirectoryMSI");
ds.setEncrypt("strict");
ds.setHostNameInCertificate("*.database.windows.net");

try (Connection con = ds.getConnection()) {
assertNotNull(con);
} catch (SQLException e) {
fail("Connection failed: " + e.getMessage());
}
}

}

0 comments on commit 2c3db81

Please sign in to comment.