11---
22title : " Azure Key Vault Sample Version 6.0.0 | Microsoft Docs"
33ms.custom : " "
4- ms.date : " 02/28 /2018"
4+ ms.date : " 07/31 /2018"
55ms.prod : sql
66ms.prod_service : connectivity
77ms.reviewer : " "
@@ -16,10 +16,12 @@ ms.author: genemi
1616manager : craigg
1717---
1818# Azure Key Vault Sample Version 6.0.0
19+
1920[ !INCLUDE[ Driver_JDBC_Download] ( ../../includes/driver_jdbc_download.md )]
2021
21- ## Sample application using Azure Key Vault feature
22- This application is runnable using JDBC Driver 6.0.0 and Azure-Keyvault (version 0.9.7), Adal4j (version 1.3.0), and their dependencies. The underlying dependencies can be resolved by adding these libraries to the pom file of the project as described [ here] ( ../../connect/jdbc/feature-dependencies-of-microsoft-jdbc-driver-for-sql-server.md ) :
22+ ## Sample application using Azure Key Vault feature
23+
24+ This application is runnable using JDBC Driver 6.0.0 and Azure-Keyvault (version 0.9.7), Adal4j (version 1.3.0), and their dependencies. The underlying dependencies can be resolved by adding these libraries to the pom file of the project as described [ here] ( ../../connect/jdbc/feature-dependencies-of-microsoft-jdbc-driver-for-sql-server.md ) :
2325
2426``` java
2527import java.net.URISyntaxException ;
@@ -45,17 +47,18 @@ import com.microsoft.sqlserver.jdbc.SQLServerConnection;
4547import com.microsoft.sqlserver.jdbc.SQLServerException ;
4648import com.microsoft.sqlserver.jdbc.SQLServerKeyVaultAuthenticationCallback ;
4749
48- public class AE_AKV_Maven {
50+ public class AKV_600 {
4951
5052 private static String connectionUrl = " jdbc:sqlserver://localhost;integratedSecurity=true;database=test;columnEncryptionSetting=enabled" ;
51- static String applicationClientID = " Your Client ID" ;
53+ static String applicationClientID = " Your Client ID" ;
5254 static String applicationKey = " Your Application Key" ;
53- static String keyID = " Your Key ID" ;
55+ static String keyID = " Your Key ID" ;
5456 static String cmkName = " AKV_CMK_JDBC" ;
5557 static String cekName = " AKV_CEK_JDBC" ;
5658 static String akvTable = " akvTable" ;
5759
58- static String createTableSQL = " create table " + akvTable + " (" + " PlainNvarcharMax nvarchar(max) null,"
60+ static String createTableSQL = " create table " + akvTable + " ("
61+ + " PlainNvarcharMax nvarchar(max) null,"
5962 + " RandomizedNvarcharMax nvarchar(max) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (ENCRYPTION_TYPE = RANDOMIZED, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = "
6063 + cekName + " ) NULL,"
6164 + " DeterministicNvarcharMax nvarchar(max) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = "
@@ -68,13 +71,16 @@ public class AE_AKV_Maven {
6871 SQLServerKeyVaultAuthenticationCallback authenticationCallback = new SQLServerKeyVaultAuthenticationCallback () {
6972
7073 @Override
71- public String getAccessToken (String authority , String resource , String scope ) {
74+ public String getAccessToken (String authority , String resource ,
75+ String scope ) {
7276 AuthenticationResult result = null ;
7377 try {
74- AuthenticationContext context = new AuthenticationContext (authority, false , service);
75- ClientCredential cred = new ClientCredential (applicationClientID, applicationKey);
76-
77- Future<AuthenticationResult > future = context. acquireToken(resource, cred, null );
78+ AuthenticationContext context = new AuthenticationContext (
79+ authority, false , service);
80+ ClientCredential cred = new ClientCredential (
81+ applicationClientID, applicationKey);
82+ Future<AuthenticationResult > future = context
83+ .acquireToken(resource, cred, null );
7884 result = future. get();
7985 } catch (Exception e) {
8086 e. printStackTrace();
@@ -88,28 +94,24 @@ public class AE_AKV_Maven {
8894 return akvProvider;
8995 }
9096
91- public static void main (String [] args ) throws ClassNotFoundException , Exception {
97+ public static void main (String [] args )
98+ throws ClassNotFoundException , Exception {
9299 Class . forName(" com.microsoft.sqlserver.jdbc.SQLServerDriver" );
93100 try (Connection connection = DriverManager . getConnection(connectionUrl);
94101 Statement statement = connection. createStatement()) {
95-
96102 statement. execute(" DBCC FREEPROCCACHE" );
97-
98103 SQLServerColumnEncryptionAzureKeyVaultProvider akvProvider = tryAuthenticationCallback();
99-
100104 setupKeyStoreProviders(akvProvider. getName(), akvProvider);
101-
102- testAKV(akvProvider. getName(), akvProvider, statement);
103-
104- connection. close();
105+ testAKV(akvProvider. getName(), akvProvider, connection, statement);
105106 }
106107 }
107108
108- private static void testAKV (String CUSTOM_AKV_PROVIDER_NAME , SQLServerColumnEncryptionKeyStoreProvider akvProvider ,
109- Statement statement ) throws SQLException , SQLServerException , InterruptedException {
109+ private static void testAKV (String CUSTOM_AKV_PROVIDER_NAME ,
110+ SQLServerColumnEncryptionKeyStoreProvider akvProvider ,
111+ Connection connection , Statement statement )
112+ throws SQLException , SQLServerException , InterruptedException {
110113
111114 dropTable(statement);
112-
113115 dropKeys(statement);
114116
115117 System . out. println(" createCMK" );
@@ -122,10 +124,10 @@ public class AE_AKV_Maven {
122124 statement. execute(createTableSQL);
123125
124126 System . out. println(" populate" );
125- populateCharNormalCase();
127+ populateCharNormalCase(connection );
126128
127- System . out. println(" test" );
128- testChar();
129+ System . out. println(" run the test" );
130+ testChar(statement );
129131 }
130132
131133 /**
@@ -136,7 +138,8 @@ public class AE_AKV_Maven {
136138 * @throws SQLServerException
137139 */
138140 private static void setupKeyStoreProviders (String CUSTOM_AKV_PROVIDER_NAME ,
139- SQLServerColumnEncryptionKeyStoreProvider akvProvider ) throws SQLServerException {
141+ SQLServerColumnEncryptionKeyStoreProvider akvProvider )
142+ throws SQLServerException {
140143 Map<String , SQLServerColumnEncryptionKeyStoreProvider > map1 = new HashMap<String , SQLServerColumnEncryptionKeyStoreProvider > ();
141144 map1. put(CUSTOM_AKV_PROVIDER_NAME , akvProvider);
142145 SQLServerConnection . registerColumnEncryptionKeyStoreProviders(map1);
@@ -148,7 +151,8 @@ public class AE_AKV_Maven {
148151 * @throws SQLException
149152 */
150153 private static void dropTable (Statement statement ) throws SQLException {
151- statement. executeUpdate(" if object_id('" + akvTable + " ','U') is not null" + " drop table " + akvTable);
154+ statement. executeUpdate(" if object_id('" + akvTable
155+ + " ','U') is not null" + " drop table " + akvTable);
152156 }
153157
154158 /**
@@ -157,11 +161,14 @@ public class AE_AKV_Maven {
157161 * @throws SQLException
158162 */
159163 private static void dropKeys (Statement statement ) throws SQLException {
160- statement. executeUpdate(" if exists (SELECT name from sys.column_encryption_keys where name='" + cekName + " ')"
161- + " begin" + " drop column encryption key " + cekName + " end" );
162-
163- statement. executeUpdate(" if exists (SELECT name from sys.column_master_keys where name='" + cmkName + " ')"
164- + " begin" + " drop column master key " + cmkName + " end" );
164+ statement. executeUpdate(
165+ " if exists (SELECT name from sys.column_encryption_keys where name='"
166+ + cekName + " ')" + " begin"
167+ + " drop column encryption key " + cekName + " end" );
168+ statement. executeUpdate(
169+ " if exists (SELECT name from sys.column_master_keys where name='"
170+ + cmkName + " ')" + " begin" + " drop column master key "
171+ + cmkName + " end" );
165172 }
166173
167174 /**
@@ -170,10 +177,11 @@ public class AE_AKV_Maven {
170177 * @param CUSTOM_AKV_PROVIDER_NAME
171178 * @throws SQLException
172179 */
173- private static void createCMK (String CUSTOM_AKV_PROVIDER_NAME , Statement statement ) throws SQLException {
180+ private static void createCMK (String CUSTOM_AKV_PROVIDER_NAME ,
181+ Statement statement ) throws SQLException {
174182 String _createColumnMasterKeyTemplate = String . format(
175- " CREATE COLUMN MASTER KEY [%s] WITH ( KEY_STORE_PROVIDER_NAME = '%s', KEY_PATH = '%s');" , cmkName,
176- CUSTOM_AKV_PROVIDER_NAME , keyID);
183+ " CREATE COLUMN MASTER KEY [%s] WITH ( KEY_STORE_PROVIDER_NAME = '%s', KEY_PATH = '%s');" ,
184+ cmkName, CUSTOM_AKV_PROVIDER_NAME , keyID);
177185 statement. execute(_createColumnMasterKeyTemplate);
178186 }
179187
@@ -184,19 +192,18 @@ public class AE_AKV_Maven {
184192 * @throws SQLServerException
185193 * @throws SQLException
186194 */
187- private static void createCEK (SQLServerColumnEncryptionKeyStoreProvider storeProvider , Statement statement )
188- throws SQLServerException , SQLException {
189-
195+ private static void createCEK (
196+ SQLServerColumnEncryptionKeyStoreProvider storeProvider ,
197+ Statement statement ) throws SQLServerException , SQLException {
190198 String letters = " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ;
191199 byte [] valuesDefault = letters. getBytes();
192-
193- byte [] key = storeProvider . encryptColumnEncryptionKey(keyID, " RSA_OAEP " , valuesDefault);
194-
195- String cekSql = " CREATE COLUMN ENCRYPTION KEY " + cekName + " WITH VALUES " + " (COLUMN_MASTER_KEY = " + cmkName
196- + " , ALGORITHM = 'RSA_OAEP', ENCRYPTED_VALUE = 0x" + bytesToHexString(key, key . length) + " ) " + " ; " ;
197-
200+ byte [] key = storeProvider . encryptColumnEncryptionKey(keyID, " RSA_OAEP " ,
201+ valuesDefault);
202+ String cekSql = " CREATE COLUMN ENCRYPTION KEY " + cekName
203+ + " WITH VALUES " + " (COLUMN_MASTER_KEY = " + cmkName
204+ + " , ALGORITHM = 'RSA_OAEP', ENCRYPTED_VALUE = 0x"
205+ + bytesToHexString(key, key . length) + " ) " + " ; " ;
198206 statement. execute(cekSql);
199-
200207 }
201208
202209 /**
@@ -207,7 +214,8 @@ public class AE_AKV_Maven {
207214 * length of the array
208215 * @return
209216 */
210- final static char [] hexChars = { ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
217+ final static char [] hexChars = {' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' ,
218+ ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
211219
212220 private static String bytesToHexString (byte [] b , int length ) {
213221 StringBuilder sb = new StringBuilder (length * 2 );
@@ -224,15 +232,13 @@ public class AE_AKV_Maven {
224232 *
225233 * @throws SQLException
226234 */
227- private static void populateCharNormalCase () throws SQLException {
235+ private static void populateCharNormalCase (Connection connection )
236+ throws SQLException {
228237 String sql = " insert into " + akvTable + " values(?,?,?)" ;
229- try (Connection connection = DriverManager . getConnection(connectionUrl);
230- PreparedStatement pstmt = connection. prepareStatement(sql)) {
231-
238+ try (PreparedStatement pstmt = connection. prepareStatement(sql)) {
232239 for (int i = 1 ; i <= 3 ; i++ ) {
233240 pstmt. setNString(i, " hello world" );
234241 }
235-
236242 pstmt. execute();
237243 }
238244 }
@@ -242,15 +248,13 @@ public class AE_AKV_Maven {
242248 *
243249 * @throws SQLException
244250 */
245- private static void testChar () throws SQLException {
246- try (Connection connection = DriverManager . getConnection(connectionUrl);
247- ResultSet rs = connection . createStatement() . executeQuery(" select * from " + akvTable);) {
251+ private static void testChar (Statement statement ) throws SQLException {
252+ try (ResultSet rs = statement
253+ .executeQuery(" select * from " + akvTable);) {
248254 int numberOfColumns = rs. getMetaData(). getColumnCount();
249-
250255 while (rs. next()) {
251256 testGetString(rs, numberOfColumns);
252257 }
253-
254258 }
255259 }
256260
@@ -261,23 +265,21 @@ public class AE_AKV_Maven {
261265 * @param numberOfColumns
262266 * @throws SQLException
263267 */
264- private static void testGetString (ResultSet rs , int numberOfColumns ) throws SQLException {
268+ private static void testGetString (ResultSet rs , int numberOfColumns )
269+ throws SQLException {
265270 for (int i = 1 ; i <= numberOfColumns; i = i + 3 ) {
266271 String stringValue1 = " " + rs. getString(i);
267272 String stringValue2 = " " + rs. getString(i + 1 );
268273 String stringValue3 = " " + rs. getString(i + 2 );
269-
270274 System . out. println(stringValue1);
271275 System . out. println(stringValue2);
272276 System . out. println(stringValue3);
273277 }
274278 }
275-
276279}
277-
278-
279-
280280```
281281
282- ## See Also
283- [ Azure Key Vault Sample Version 6.2.2] ( ../../connect/jdbc/azure-key-vault-sample-version-6.2.2.md )
282+ ## See Also
283+
284+ [ Azure Key Vault Sample Version 7.0.0] ( ../../connect/jdbc/azure-key-vault-sample-version-7-0-0.md )
285+ [ Azure Key Vault Sample Version 6.2.2] ( ../../connect/jdbc/azure-key-vault-sample-version-6.2.2.md )
0 commit comments