Skip to content

Commit

Permalink
Add more tests for Managed Identities authentication for AKV to incre…
Browse files Browse the repository at this point in the history
…ase code coverage (#1305)
  • Loading branch information
lilgreenbird authored Apr 6, 2020
1 parent f0e48ff commit 947b537
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ protected Object[][] getContents() {
"\"keyStoreAuthentication\" connection string keyword must be specified, if \"{0}\" is specified."},
{"R_keyStoreSecretOrLocationNotSet",
"Both \"keyStoreSecret\" and \"keyStoreLocation\" must be set, if \"keyStoreAuthentication=JavaKeyStorePassword\" has been specified in the connection string."},
{"R_keyStoreSecretnNotSet",
{"R_keyStoreSecretNotSet",
"\"keyStoreSecret\" must be set, if \"keyStoreAuthentication=KeyVaultClientSecret\" has been specified in the connection string."},
{"R_certificateStoreInvalidKeyword",
"Cannot set \"keyStoreSecret\", if \"keyStoreAuthentication=CertificateStore\" has been specified in the connection string."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.microsoft.sqlserver.jdbc.AlwaysEncrypted;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
Expand Down Expand Up @@ -159,6 +160,21 @@ public void testCharAkvWithCred() throws SQLException {
testCharAkv(connStr);
}

/*
* Test AKV with with credentials using deprecated properties
*/
@Test
public void testCharAkvWithCredDeprecated() throws SQLException {
// unregister the custom providers registered in AESetup
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();

// add deprecated connection properties
String connStr = AETestConnectionString;
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYVAULTPROVIDER_CLIENTID, keyStorePrincipalId);
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYVAULTPROVIDER_CLIENTKEY, keyStoreSecret);
testCharAkv(connStr);
}

/*
* Test AKV with MSI
*/
Expand Down Expand Up @@ -190,6 +206,82 @@ public void testCharAkvWithMSIandPrincipalId() throws SQLException {
testCharAkv(connStr);
}

/*
* Test AKV with with missing credentials
*/
@Test
public void testNumericAkvMissingCred() throws SQLException {
// unregister the custom providers registered in AESetup
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();

// set auth type to key vault client secret but do not provide secret
String connStr = AETestConnectionString;
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYSTORE_AUTHENTICATION, "KeyVaultClientSecret");
try {
testNumericAKV(connStr);
fail(TestResource.getResource("R_expectedFailPassed"));
} catch (Exception e) {
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_keyStoreSecretNotSet")));
}
}

/*
* Test AKV with with keyStoreSecret secret but no keyStoreAuthentication
*/
@Test
public void testNumericAkvSecretNoAuth() throws SQLException {
// unregister the custom providers registered in AESetup
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();

// set key store secret but do not specify authentication type
String connStr = AETestConnectionString;
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYSTORE_SECRET, keyStoreSecret);
try {
testNumericAKV(connStr);
fail(TestResource.getResource("R_expectedFailPassed"));
} catch (Exception e) {
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_keyStoreAuthenticationNotSet")));
}
}

/*
* Test AKV with with keyStorePrincipalId but no keyStoreAuthentication
*/
@Test
public void testNumericAkvPrincipalIdNoAuth() throws SQLException {
// unregister the custom providers registered in AESetup
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();

// set principal id but do not specify authentication type
String connStr = AETestConnectionString;
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYSTORE_PRINCIPALID, keyStorePrincipalId);
try {
testNumericAKV(connStr);
fail(TestResource.getResource("R_expectedFailPassed"));
} catch (Exception e) {
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_keyStoreAuthenticationNotSet")));
}
}

/*
* Test AKV with with keyStoreLocation but no keyStoreAuthentication
*/
@Test
public void testNumericAkvLocationNoAuth() throws SQLException {
// unregister the custom providers registered in AESetup
SQLServerConnection.unregisterColumnEncryptionKeyStoreProviders();

// set key store location but do not specify authentication type
String connStr = AETestConnectionString;
connStr = TestUtils.addOrOverrideProperty(connStr, Constants.KEYSTORE_LOCATION, "location");
try {
testNumericAKV(connStr);
fail(TestResource.getResource("R_expectedFailPassed"));
} catch (Exception e) {
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_keyStoreAuthenticationNotSet")));
}
}

/*
* Test AKV with with bad credentials
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private Constants() {}
public static final String KEYSTORE_AUTHENTICATION = "KEYSTOREAUTHENTICATION";
public static final String KEYSTORE_PRINCIPALID = "KEYSTOREPRINCIPALID";
public static final String KEYSTORE_SECRET = "KEYSTORESECRET";
public static final String KEYSTORE_LOCATION = "KEYSTORELOCATION";
public static final String CLIENT_CERTIFICATE = "CLIENTCERTIFICATE";
public static final String CLIENT_KEY = "CLIENTKEY";
public static final String CLIENT_KEY_PASSWORD = "CLIENTKEYPASSWORD";
Expand Down

0 comments on commit 947b537

Please sign in to comment.