|
| 1 | +/** |
| 2 | + * Copyright Microsoft Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package com.microsoft.azure.storage.encryption.keyvault.gettingstarted; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.security.InvalidKeyException; |
| 22 | +import java.security.KeyPair; |
| 23 | +import java.security.KeyPairGenerator; |
| 24 | +import java.security.NoSuchAlgorithmException; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.UUID; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | + |
| 29 | +import com.microsoft.azure.keyvault.core.IKey; |
| 30 | +import com.microsoft.azure.keyvault.extensions.AggregateKeyResolver; |
| 31 | +import com.microsoft.azure.keyvault.extensions.CachingKeyResolver; |
| 32 | +import com.microsoft.azure.keyvault.extensions.KeyVaultKeyResolver; |
| 33 | +import com.microsoft.azure.keyvault.extensions.RsaKey; |
| 34 | +import com.microsoft.azure.storage.CloudStorageAccount; |
| 35 | +import com.microsoft.azure.storage.StorageException; |
| 36 | +import com.microsoft.azure.storage.blob.BlobEncryptionPolicy; |
| 37 | +import com.microsoft.azure.storage.blob.BlobRequestOptions; |
| 38 | +import com.microsoft.azure.storage.blob.CloudBlobClient; |
| 39 | +import com.microsoft.azure.storage.blob.CloudBlobContainer; |
| 40 | +import com.microsoft.azure.storage.blob.CloudBlockBlob; |
| 41 | +import com.microsoft.azure.storage.util.KeyVaultUtility; |
| 42 | +import com.microsoft.azure.storage.util.LocalResolver; |
| 43 | +import com.microsoft.azure.storage.util.Utility; |
| 44 | + |
| 45 | +public class KeyVaultGettingStarted { |
| 46 | + |
| 47 | + public static void main(String[] args) throws StorageException, |
| 48 | + NoSuchAlgorithmException, InterruptedException, ExecutionException, |
| 49 | + URISyntaxException, InvalidKeyException, IOException { |
| 50 | + Utility.printSampleStartInfo("KeyVaultGettingStarted"); |
| 51 | + |
| 52 | + // Get the key ID from App.config if it exists. |
| 53 | + String keyID = Utility.keyVaultKeyID; |
| 54 | + |
| 55 | + // If no key ID was specified, we will create a new secret in Key Vault. |
| 56 | + // To create a new secret, this client needs full permission to Key |
| 57 | + // Vault secrets. |
| 58 | + // Once the secret is created, its ID can be added to App.config. Once |
| 59 | + // this is done, |
| 60 | + // this client only needs read access to secrets. |
| 61 | + if (keyID == null || keyID.isEmpty()) { |
| 62 | + keyID = KeyVaultUtility.createSecret("KVGettingStartedSecret"); |
| 63 | + } |
| 64 | + |
| 65 | + // Retrieve storage account information from connection string |
| 66 | + // How to create a storage connection string - |
| 67 | + // https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/ |
| 68 | + CloudStorageAccount storageAccount = CloudStorageAccount |
| 69 | + .parse(Utility.storageConnectionString); |
| 70 | + |
| 71 | + CloudBlobClient client = storageAccount.createCloudBlobClient(); |
| 72 | + CloudBlobContainer container = client |
| 73 | + .getContainerReference("blobencryptioncontainer" |
| 74 | + + UUID.randomUUID().toString().replace("-", "")); |
| 75 | + |
| 76 | + // Construct a resolver capable of looking up keys and secrets stored in |
| 77 | + // Key Vault. |
| 78 | + |
| 79 | + KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver( |
| 80 | + KeyVaultUtility.GetKeyVaultClient()); |
| 81 | + |
| 82 | + // To demonstrate how multiple different types of key can be used, we |
| 83 | + // also create a local key and resolver. |
| 84 | + // This key is temporary and won't be persisted. |
| 85 | + final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); |
| 86 | + keyGen.initialize(1024); |
| 87 | + final KeyPair wrapKey = keyGen.generateKeyPair(); |
| 88 | + |
| 89 | + RsaKey rsaKey = new RsaKey("rsaKey1", wrapKey); |
| 90 | + LocalResolver resolver = new LocalResolver(); |
| 91 | + resolver.add(rsaKey); |
| 92 | + |
| 93 | + // If there are multiple key sources like Azure Key Vault and local KMS, |
| 94 | + // set up an aggregate resolver as follows. |
| 95 | + // This helps users to define a plug-in model for all the different key |
| 96 | + // providers they support. |
| 97 | + AggregateKeyResolver aggregateResolver = new AggregateKeyResolver(); |
| 98 | + aggregateResolver.Add(resolver); |
| 99 | + aggregateResolver.Add(cloudResolver); |
| 100 | + |
| 101 | + // Set up a caching resolver so the secrets can be cached on the client. |
| 102 | + // This is the recommended usage |
| 103 | + // pattern since the throttling targets for Storage and Key Vault |
| 104 | + // services are orders of magnitude |
| 105 | + // different. |
| 106 | + CachingKeyResolver cachingResolver = new CachingKeyResolver(2, |
| 107 | + aggregateResolver); |
| 108 | + |
| 109 | + // Create a key instance corresponding to the key ID. This will cache |
| 110 | + // the secret. |
| 111 | + IKey cloudKey = cachingResolver.resolveKeyAsync(keyID).get(); |
| 112 | + |
| 113 | + try { |
| 114 | + container.createIfNotExists(); |
| 115 | + int size = 5 * 1024 * 1024; |
| 116 | + byte[] buffer = new byte[size]; |
| 117 | + |
| 118 | + Random rand = new Random(); |
| 119 | + rand.nextBytes(buffer); |
| 120 | + |
| 121 | + // The first blob will use the key stored in Azure Key Vault. |
| 122 | + CloudBlockBlob blob = container.getBlockBlobReference("blockblob1"); |
| 123 | + |
| 124 | + // Create the encryption policy using the secret stored in Azure Key |
| 125 | + // Vault to be used for upload. |
| 126 | + BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy( |
| 127 | + cloudKey, null); |
| 128 | + |
| 129 | + // Set the encryption policy on the request options. |
| 130 | + BlobRequestOptions uploadOptions = new BlobRequestOptions(); |
| 131 | + uploadOptions.setEncryptionPolicy(uploadPolicy); |
| 132 | + |
| 133 | + System.out.println("Uploading the 1st encrypted blob."); |
| 134 | + |
| 135 | + // Upload the encrypted contents to the blob. |
| 136 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); |
| 137 | + blob.upload(inputStream, size, null, uploadOptions, null); |
| 138 | + |
| 139 | + // Download the encrypted blob. |
| 140 | + BlobEncryptionPolicy downloadPolicy = new BlobEncryptionPolicy( |
| 141 | + null, cachingResolver); |
| 142 | + |
| 143 | + // Set the decryption policy on the request options. |
| 144 | + BlobRequestOptions downloadOptions = new BlobRequestOptions(); |
| 145 | + downloadOptions.setEncryptionPolicy(downloadPolicy); |
| 146 | + |
| 147 | + System.out.println("Downloading the 1st encrypted blob."); |
| 148 | + |
| 149 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 150 | + blob.download(outputStream, null, downloadOptions, null); |
| 151 | + |
| 152 | + // Upload second blob using the local key. |
| 153 | + blob = container.getBlockBlobReference("blockblob2"); |
| 154 | + |
| 155 | + // Create the encryption policy using the local key. |
| 156 | + uploadPolicy = new BlobEncryptionPolicy(rsaKey, null); |
| 157 | + |
| 158 | + // Set the encryption policy on the request options. |
| 159 | + uploadOptions = new BlobRequestOptions(); |
| 160 | + uploadOptions.setEncryptionPolicy(uploadPolicy); |
| 161 | + |
| 162 | + System.out.println("Uploading the 2nd encrypted blob."); |
| 163 | + |
| 164 | + // Upload the encrypted contents to the blob. |
| 165 | + inputStream = new ByteArrayInputStream(buffer); |
| 166 | + blob.upload(inputStream, size, null, uploadOptions, null); |
| 167 | + |
| 168 | + // Download the encrypted blob. The same policy and options created |
| 169 | + // before can be used because the aggregate resolver contains both |
| 170 | + // resolvers and will pick the right one based on the key ID stored |
| 171 | + // in blob metadata on the service. |
| 172 | + System.out.println("Downloading the 2nd encrypted blob."); |
| 173 | + |
| 174 | + // Download and decrypt the encrypted contents from the blob. |
| 175 | + outputStream = new ByteArrayOutputStream(); |
| 176 | + blob.download(outputStream, null, downloadOptions, null); |
| 177 | + } finally { |
| 178 | + container.deleteIfExists(); |
| 179 | + Utility.printSampleCompleteInfo("KeyVaultGettingStarted"); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments