Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 5ccf7e6

Browse files
committed
Merge pull request #86 from emgerner-msft/master
Java Storage Client Library 4.1.0
2 parents 21ed037 + d149471 commit 5ccf7e6

File tree

70 files changed

+7848
-831
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+7848
-831
lines changed

ChangeLog.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2015.10.05 Version 4.1.0
2+
* Added support for client side encryption for blobs, queues and tables.
3+
* Since the encryption preview, added functionality where uploading encrypted blobs can be done with just PutBlob, not PutBlock + PutBlockList, if the blob is small enough.
4+
* Since the encryption preview, fixed bugs in the Table Service where APIs such as 'CreateTable' were trying to encrypt their payload. Encryption is only supported on entities.
5+
16
2015.10.05 Version 4.0.0
27
* Removed deprecated table AtomPub support.
38
* Removed deprecated constructors which take service clients in favor of constructors which take credentials.

microsoft-azure-storage-samples/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
<dependency>
2727
<groupId>com.microsoft.azure</groupId>
2828
<artifactId>azure-storage</artifactId>
29-
<version>4.0.0</version>
29+
<version>4.1.0</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.microsoft.azure</groupId>
33+
<artifactId>azure-keyvault-extensions</artifactId>
34+
<version>0.8.0</version>
3035
</dependency>
3136
</dependencies>
3237
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.blob.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+
28+
import com.microsoft.azure.keyvault.extensions.RsaKey;
29+
import com.microsoft.azure.storage.CloudStorageAccount;
30+
import com.microsoft.azure.storage.StorageException;
31+
import com.microsoft.azure.storage.blob.BlobEncryptionPolicy;
32+
import com.microsoft.azure.storage.blob.BlobRequestOptions;
33+
import com.microsoft.azure.storage.blob.CloudBlobClient;
34+
import com.microsoft.azure.storage.blob.CloudBlobContainer;
35+
import com.microsoft.azure.storage.blob.CloudBlockBlob;
36+
import com.microsoft.azure.storage.util.LocalResolver;
37+
import com.microsoft.azure.storage.util.Utility;
38+
39+
/**
40+
* Demonstrates how to use encryption with the Azure Blob service.
41+
*/
42+
public class BlobGettingStarted {
43+
44+
public static void main(String[] args) throws InvalidKeyException,
45+
URISyntaxException, StorageException, NoSuchAlgorithmException,
46+
IOException {
47+
Utility.printSampleStartInfo("BlobBasicsEncryption");
48+
49+
// Retrieve storage account information from connection string
50+
// How to create a storage connection string -
51+
// https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/
52+
CloudStorageAccount account = CloudStorageAccount
53+
.parse(Utility.storageConnectionString);
54+
CloudBlobClient blobClient = account.createCloudBlobClient();
55+
56+
// Get a reference to a container
57+
// The container name must be lower case
58+
// Append a random UUID to the end of the container name so that
59+
// this sample can be run more than once in quick succession.
60+
CloudBlobContainer container = blobClient
61+
.getContainerReference("blobencryptioncontainer"
62+
+ UUID.randomUUID().toString().replace("-", ""));
63+
64+
try {
65+
// Create the container if it does not exist
66+
container.createIfNotExists();
67+
68+
int size = 5 * 1024 * 1024;
69+
byte[] buffer = new byte[size];
70+
71+
Random rand = new Random();
72+
rand.nextBytes(buffer);
73+
74+
CloudBlockBlob blob = container.getBlockBlobReference("blockBlob");
75+
76+
// Create the IKey used for encryption.
77+
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
78+
keyGen.initialize(1024);
79+
final KeyPair wrapKey = keyGen.generateKeyPair();
80+
RsaKey key = new RsaKey("rsaKey1", wrapKey);
81+
82+
// Create the encryption policy to be used for upload.
83+
BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy(key,
84+
null);
85+
86+
// Set the encryption policy on the request options.
87+
BlobRequestOptions uploadOptions = new BlobRequestOptions();
88+
uploadOptions.setEncryptionPolicy(uploadPolicy);
89+
90+
System.out.println("Uploading the encrypted blob.");
91+
92+
// Upload the encrypted contents to the blob.
93+
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
94+
blob.upload(inputStream, size, null, uploadOptions, null);
95+
96+
// Download the encrypted blob.
97+
// For downloads, a resolver can be set up that will help pick the
98+
// key based on the key id.
99+
// Create the encryption policy to be used for download.
100+
LocalResolver resolver = new LocalResolver();
101+
resolver.add(key);
102+
BlobEncryptionPolicy downloadPolicy = new BlobEncryptionPolicy(
103+
null, resolver);
104+
105+
// Set the decryption policy on the request options.
106+
BlobRequestOptions downloadOptions = new BlobRequestOptions();
107+
downloadOptions.setEncryptionPolicy(downloadPolicy);
108+
109+
System.out.println("Downloading the encrypted blob.");
110+
111+
// Download and decrypt the encrypted contents from the blob.
112+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
113+
blob.download(outputStream, null, downloadOptions, null);
114+
} finally {
115+
// Delete the container
116+
container.deleteIfExists();
117+
Utility.printSampleCompleteInfo("BlobBasicsEncryption");
118+
}
119+
}
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)