Skip to content

Commit dea2010

Browse files
authored
Update FedAuth tests to use ManagedIdentity (#2629)
1 parent f0f59f6 commit dea2010

File tree

6 files changed

+34
-47
lines changed

6 files changed

+34
-47
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.Random;
1111
import java.util.concurrent.atomic.AtomicReference;
1212

13-
import com.microsoft.sqlserver.jdbc.TestUtils;
1413
import org.junit.jupiter.api.BeforeAll;
1514
import org.junit.jupiter.api.Tag;
1615
import org.junit.jupiter.api.Test;
@@ -23,7 +22,6 @@
2322

2423
@RunWith(JUnitPlatform.class)
2524
@Tag(Constants.fedAuth)
26-
@Tag(Constants.requireSecret)
2725
public class ConcurrentLoginTest extends FedauthCommon {
2826

2927
final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionEncryptionTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.sql.DriverManager;
1212
import java.sql.SQLException;
1313
import java.sql.Statement;
14-
import java.text.MessageFormat;
1514

1615
import org.junit.jupiter.api.AfterAll;
1716
import org.junit.jupiter.api.BeforeAll;
@@ -29,7 +28,6 @@
2928

3029
@RunWith(JUnitPlatform.class)
3130
@Tag(Constants.fedAuth)
32-
@Tag(Constants.requireSecret)
3331
public class ConnectionEncryptionTest extends FedauthCommon {
3432

3533
static String charTable = TestUtils.escapeSingleQuotes(

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
@RunWith(JUnitPlatform.class)
2727
@Tag(Constants.fedAuth)
28-
@Tag(Constants.requireSecret)
2928
public class ErrorMessageTest extends FedauthCommon {
3029

3130
String badUserName = "abc" + azureUserName;

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88
import static org.junit.jupiter.api.Assertions.fail;
99

10+
import com.azure.core.credential.AccessToken;
11+
import com.azure.core.credential.TokenRequestContext;
12+
import com.azure.identity.ManagedIdentityCredential;
13+
import com.azure.identity.ManagedIdentityCredentialBuilder;
1014
import com.microsoft.aad.msal4j.ClientCredentialFactory;
1115
import com.microsoft.aad.msal4j.ClientCredentialParameters;
1216
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
@@ -21,6 +25,7 @@
2125
import java.sql.ResultSet;
2226
import java.sql.SQLException;
2327
import java.sql.Statement;
28+
import java.util.Collections;
2429
import java.util.Date;
2530
import java.util.HashSet;
2631
import java.util.Locale;
@@ -216,25 +221,21 @@ public static void getConfigs() throws Exception {
216221
static void getFedauthInfo() {
217222
int retry = 0;
218223
long interval = THROTTLE_RETRY_INTERVAL;
224+
ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder()
225+
.clientId(akvProviderManagedClientId).build();
226+
219227
while (retry <= THROTTLE_RETRY_COUNT) {
220228
try {
221-
Set<String> scopes = new HashSet<>();
222-
scopes.add(spn + "/.default");
223-
if (null == fedauthClientApp) {
224-
IClientCredential credential = ClientCredentialFactory.createFromSecret(applicationKey);
225-
fedauthClientApp = ConfidentialClientApplication.builder(applicationClientID, credential)
226-
.executorService(Executors.newFixedThreadPool(1))
227-
.setTokenCacheAccessAspect(FedauthTokenCache.getInstance()).authority(stsurl).build();
228-
}
229+
TokenRequestContext requestContext = new TokenRequestContext()
230+
.setScopes(Collections.singletonList(spn + "/.default"));
229231

230-
final CompletableFuture<IAuthenticationResult> future = fedauthClientApp
231-
.acquireToken(ClientCredentialParameters.builder(scopes).build());
232+
AccessToken token = credential.getToken(requestContext).block();
232233

233-
final IAuthenticationResult authenticationResult = future.get();
234-
235-
secondsBeforeExpiration = TimeUnit.MILLISECONDS
236-
.toSeconds(authenticationResult.expiresOnDate().getTime() - new Date().getTime());
237-
accessToken = authenticationResult.accessToken();
234+
if (token != null) {
235+
secondsBeforeExpiration = TimeUnit.MILLISECONDS
236+
.toSeconds(token.getExpiresAt().toInstant().toEpochMilli() - new Date().getTime());
237+
accessToken = token.getToken();
238+
}
238239

239240
retry = THROTTLE_RETRY_COUNT + 1;
240241
} catch (MsalThrottlingException te) {

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
@RunWith(JUnitPlatform.class)
4444
@Tag(Constants.fedAuth)
45-
@Tag(Constants.requireSecret)
4645
public class FedauthTest extends FedauthCommon {
4746
static String charTable = TestUtils
4847
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("JDBC_FedAuthTest")));
@@ -286,6 +285,7 @@ public void testAADPasswordApplicationName() throws Exception {
286285
*/
287286
@Deprecated
288287
@Test
288+
@Tag(Constants.requireSecret)
289289
public void testAADServicePrincipalAuthDeprecated() {
290290
String url = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
291291
+ SqlAuthentication.ActiveDirectoryServicePrincipal + ";AADSecurePrincipalId=" + applicationClientID
@@ -308,6 +308,7 @@ public void testAADServicePrincipalAuthDeprecated() {
308308
* encryption.
309309
*/
310310
@Test
311+
@Tag(Constants.requireSecret)
311312
public void testAADServicePrincipalAuth() {
312313
String url = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
313314
+ SqlAuthentication.ActiveDirectoryServicePrincipal + ";Username=" + applicationClientID + ";Password="
@@ -326,6 +327,7 @@ public void testAADServicePrincipalAuth() {
326327
}
327328

328329
@Test
330+
@Tag(Constants.requireSecret)
329331
public void testAADServicePrincipalAuthFailureOnSubsequentConnectionsWithInvalidatedTokenCacheWithInvalidSecret() throws Exception {
330332
String url = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
331333
+ SqlAuthentication.ActiveDirectoryServicePrincipal + ";Username=" + applicationClientID + ";Password="
@@ -364,6 +366,7 @@ public void testActiveDirectoryPasswordFailureOnSubsequentConnectionsWithInvalid
364366
}
365367

366368
@Test
369+
@Tag(Constants.requireSecret)
367370
public void testAADServicePrincipalCertAuthFailureOnSubsequentConnectionsWithInvalidatedTokenCacheWithInvalidPassword() throws Exception {
368371
// Should succeed on valid cert field values
369372
String url = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
@@ -389,6 +392,7 @@ public void testAADServicePrincipalCertAuthFailureOnSubsequentConnectionsWithInv
389392
* Test invalid connection property combinations when using AAD Service Principal Authentication.
390393
*/
391394
@Test
395+
@Tag(Constants.requireSecret)
392396
public void testAADServicePrincipalAuthWrong() {
393397
String baseUrl = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
394398
+ SqlAuthentication.ActiveDirectoryServicePrincipal + ";";
@@ -426,6 +430,7 @@ public void testAADServicePrincipalAuthWrong() {
426430
* encryption.
427431
*/
428432
@Test
433+
@Tag(Constants.requireSecret)
429434
public void testAADServicePrincipalCertAuth() {
430435
// certificate from AKV has no password
431436
String url = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
@@ -449,6 +454,7 @@ public void testAADServicePrincipalCertAuth() {
449454
* Test invalid connection property combinations when using AAD Service Principal Certificate Authentication.
450455
*/
451456
@Test
457+
@Tag(Constants.requireSecret)
452458
public void testAADServicePrincipalCertAuthWrong() {
453459
String baseUrl = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";authentication="
454460
+ SqlAuthentication.ActiveDirectoryServicePrincipalCertificate + ";userName="
@@ -488,23 +494,6 @@ public void testAccessTokenCallbackClassConnection() throws Exception {
488494
try (Connection conn1 = DriverManager.getConnection(cs)) {}
489495
}
490496

491-
@Test
492-
public void testAccessTokenCache() {
493-
try {
494-
SilentParameters silentParameters = SilentParameters.builder(Collections.singleton(spn + "/.default"))
495-
.build();
496-
497-
// this will fail if not cached
498-
CompletableFuture<IAuthenticationResult> future = fedauthClientApp.acquireTokenSilently(silentParameters);
499-
IAuthenticationResult authenticationResult = future.get();
500-
assertNotNull(authenticationResult.accessToken());
501-
assertTrue(authenticationResult.accessToken().equals(accessToken), accessToken);
502-
} catch (Exception e) {
503-
fail(e.getMessage());
504-
}
505-
506-
}
507-
508497
private static void validateException(String url, String resourceKey) {
509498
try (Connection conn = DriverManager.getConnection(url)) {
510499
fail(TestResource.getResource("R_expectedFailPassed"));

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthWithAE.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.platform.runner.JUnitPlatform;
2424
import org.junit.runner.RunWith;
2525

26+
import com.azure.identity.ManagedIdentityCredential;
27+
import com.azure.identity.ManagedIdentityCredentialBuilder;
2628
import com.microsoft.sqlserver.jdbc.RandomUtil;
2729
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider;
2830
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider;
@@ -37,7 +39,6 @@
3739

3840
@RunWith(JUnitPlatform.class)
3941
@Tag(Constants.fedAuth)
40-
@Tag(Constants.requireSecret)
4142
public class FedauthWithAE extends FedauthCommon {
4243

4344
static String cmkName1 = Constants.CMK_NAME + "fedauthAE1";
@@ -282,16 +283,17 @@ private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_JKS() th
282283

283284
private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_AKV() throws SQLServerException {
284285
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();
285-
return registerAKVProvider(
286-
new SQLServerColumnEncryptionAzureKeyVaultProvider(applicationClientID, applicationKey));
286+
return registerAKVProvider();
287287
}
288288

289-
private SQLServerColumnEncryptionKeyStoreProvider registerAKVProvider(
290-
SQLServerColumnEncryptionKeyStoreProvider provider) throws SQLServerException {
291-
Map<String, SQLServerColumnEncryptionKeyStoreProvider> map1 = new HashMap<String, SQLServerColumnEncryptionKeyStoreProvider>();
292-
map1.put(provider.getName(), provider);
293-
SQLServerConnection.registerColumnEncryptionKeyStoreProviders(map1);
294-
return provider;
289+
private SQLServerColumnEncryptionKeyStoreProvider registerAKVProvider() throws SQLServerException {
290+
Map<String, SQLServerColumnEncryptionKeyStoreProvider> map = new HashMap<String, SQLServerColumnEncryptionKeyStoreProvider>();
291+
ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder()
292+
.clientId(akvProviderManagedClientId).build();
293+
akvProvider = new SQLServerColumnEncryptionAzureKeyVaultProvider(credential);
294+
map.put(Constants.AZURE_KEY_VAULT_NAME, akvProvider);
295+
SQLServerConnection.registerColumnEncryptionKeyStoreProviders(map);
296+
return akvProvider;
295297
}
296298

297299
private void createCMK(String cmkName, String keyStoreName, String keyPath, Statement stmt) throws SQLException {

0 commit comments

Comments
 (0)