Skip to content

Commit

Permalink
Merge pull request #4 from srnagar/keyvault-upgrade
Browse files Browse the repository at this point in the history
Fix compilation errors after merging from master
  • Loading branch information
JonathanGiles authored Aug 12, 2020
2 parents 6d014b5 + dcf2f79 commit 62a83e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.microsoft.aad.msal4j.IAuthenticationResult;
import com.microsoft.aad.msal4j.PublicClientApplication;
import com.microsoft.aad.msal4j.UserNamePasswordParameters;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.LogManager;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.sqlserver.testframework.Constants;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.jdbc.TestResource;
Expand Down Expand Up @@ -142,11 +144,23 @@ public static void getConfigs() throws Exception {
*/
static void getFedauthInfo() {
try {
AuthenticationContext context = new AuthenticationContext(stsurl, false, Executors.newFixedThreadPool(1));
Future<AuthenticationResult> future = context.acquireToken(spn, fedauthClientId, azureUserName,
azurePassword, null);
secondsBeforeExpiration = future.get().getExpiresAfter();
accessToken = future.get().getAccessToken();

final PublicClientApplication clientApplication = PublicClientApplication
.builder(fedauthClientId)
.executorService(Executors.newFixedThreadPool(1))
.authority(stsurl)
.build();
final CompletableFuture<IAuthenticationResult> future = clientApplication.acquireToken(
UserNamePasswordParameters.builder(
Set.of(spn + "/.default"),
azureUserName,
azurePassword.toCharArray()
).build());

final IAuthenticationResult authenticationResult = future.get();

secondsBeforeExpiration = authenticationResult.expiresOnDate().getTime();
accessToken = authenticationResult.accessToken();
} catch (Exception e) {
fail(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -26,17 +23,13 @@
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionKeyStoreProvider;
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.jdbc.SQLServerKeyVaultAuthenticationCallback;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.Constants;
Expand Down Expand Up @@ -128,7 +121,7 @@ public void testFedAuthWithAE_AKV() throws SQLException {
dropCMK(stmt, cmkName3);
setupCMK_AKVOld(cmkName3, stmt);

createCEK(cmkName3, setupKeyStoreProvider_AKVOld(), stmt, keyIDs[0]);
createCEK(cmkName3, setupKeyStoreProvider_AKVNew(), stmt, keyIDs[0]);
createCharTable(stmt, charTableOld);

populateCharNormalCase(charValues, connection, charTableOld);
Expand Down Expand Up @@ -304,30 +297,30 @@ private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_JKS() th
private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_AKVNew() throws SQLServerException {
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();
return registerAKVProvider(
new SQLServerColumnEncryptionAzureKeyVaultProvider(applicationClientID, applicationKey));
new SQLServerColumnEncryptionAzureKeyVaultProvider(applicationClientID, applicationKey, tenantID));
}

private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_AKVOld() throws SQLServerException {
ExecutorService service = Executors.newFixedThreadPool(2);
SQLServerKeyVaultAuthenticationCallback authenticationCallback = new SQLServerKeyVaultAuthenticationCallback() {
@Override
public String getAccessToken(String authority, String resource, String scope) {
AuthenticationResult result = null;
try {
AuthenticationContext context = new AuthenticationContext(authority, false, service);
ClientCredential cred = new ClientCredential(applicationClientID, applicationKey);

Future<AuthenticationResult> future = context.acquireToken(resource, cred, null);
result = future.get();
return result.getAccessToken();
} catch (Exception e) {
fail(e.getMessage());
return null;
}
}
};
return new SQLServerColumnEncryptionAzureKeyVaultProvider(authenticationCallback);
}
// private SQLServerColumnEncryptionKeyStoreProvider setupKeyStoreProvider_AKVOld() throws SQLServerException {
// ExecutorService service = Executors.newFixedThreadPool(2);
// SQLServerKeyVaultAuthenticationCallback authenticationCallback = new SQLServerKeyVaultAuthenticationCallback() {
// @Override
// public String getAccessToken(String authority, String resource, String scope) {
// AuthenticationResult result = null;
// try {
// AuthenticationContext context = new AuthenticationContext(authority, false, service);
// ClientCredential cred = new ClientCredential(applicationClientID, applicationKey);
//
// Future<AuthenticationResult> future = context.acquireToken(resource, cred, null);
// result = future.get();
// return result.getAccessToken();
// } catch (Exception e) {
// fail(e.getMessage());
// return null;
// }
// }
// };
// return new SQLServerColumnEncryptionAzureKeyVaultProvider(authenticationCallback);
// }

private SQLServerColumnEncryptionKeyStoreProvider registerAKVProvider(
SQLServerColumnEncryptionKeyStoreProvider provider) throws SQLServerException {
Expand Down

0 comments on commit 62a83e4

Please sign in to comment.