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

Commit 8f6c29f

Browse files
authored
Merge pull request #286 from rickle-msft/updateSamples
Samples Updated to use Key Vault 1.0
2 parents 511d1bb + 4db58fb commit 8f6c29f

File tree

13 files changed

+105
-87
lines changed

13 files changed

+105
-87
lines changed

microsoft-azure-storage-samples/pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@
2626
<dependency>
2727
<groupId>com.microsoft.azure</groupId>
2828
<artifactId>azure-storage</artifactId>
29-
<version>6.1.0</version>
29+
<version>7.0.0</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>com.microsoft.azure</groupId>
3333
<artifactId>azure-keyvault-extensions</artifactId>
34-
<version>0.8.0</version>
34+
<version>1.0.0</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.microsoft.rest</groupId>
38+
<artifactId>client-runtime</artifactId>
39+
<version>1.2.1</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.microsoft.aad</groupId>
43+
<artifactId>adal4j</artifactId>
44+
<version>0.0.2</version>
3545
</dependency>
3646
</dependencies>
3747
</project>

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/blob/gettingstarted/BlobGettingStarted.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.Random;
2626
import java.util.UUID;
2727

28-
import com.microsoft.azure.keyvault.extensions.RsaKey;
28+
import com.microsoft.azure.keyvault.cryptography.RsaKey;
2929
import com.microsoft.azure.storage.CloudStorageAccount;
3030
import com.microsoft.azure.storage.StorageException;
3131
import com.microsoft.azure.storage.blob.BlobEncryptionPolicy;

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/keyvault/gettingstarted/KeyVaultGettingStarted.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.microsoft.azure.keyvault.extensions.AggregateKeyResolver;
3131
import com.microsoft.azure.keyvault.extensions.CachingKeyResolver;
3232
import com.microsoft.azure.keyvault.extensions.KeyVaultKeyResolver;
33-
import com.microsoft.azure.keyvault.extensions.RsaKey;
33+
import com.microsoft.azure.keyvault.cryptography.RsaKey;
3434
import com.microsoft.azure.storage.CloudStorageAccount;
3535
import com.microsoft.azure.storage.StorageException;
3636
import com.microsoft.azure.storage.blob.BlobEncryptionPolicy;
@@ -44,9 +44,7 @@
4444

4545
public class KeyVaultGettingStarted {
4646

47-
public static void main(String[] args) throws StorageException,
48-
NoSuchAlgorithmException, InterruptedException, ExecutionException,
49-
URISyntaxException, InvalidKeyException, IOException {
47+
public static void main(String[] args) throws Exception {
5048
Utility.printSampleStartInfo("KeyVaultGettingStarted");
5149

5250
// Get the key ID from App.config if it exists.
@@ -86,7 +84,7 @@ public static void main(String[] args) throws StorageException,
8684
keyGen.initialize(1024);
8785
final KeyPair wrapKey = keyGen.generateKeyPair();
8886

89-
RsaKey rsaKey = new RsaKey("rsaKey1", wrapKey);
87+
RsaKey rsaKey = new RsaKey(keyID, wrapKey);
9088
LocalResolver resolver = new LocalResolver();
9189
resolver.add(rsaKey);
9290

@@ -95,8 +93,8 @@ public static void main(String[] args) throws StorageException,
9593
// This helps users to define a plug-in model for all the different key
9694
// providers they support.
9795
AggregateKeyResolver aggregateResolver = new AggregateKeyResolver();
98-
aggregateResolver.Add(resolver);
99-
aggregateResolver.Add(cloudResolver);
96+
aggregateResolver.add(resolver);
97+
aggregateResolver.add(cloudResolver);
10098

10199
// Set up a caching resolver so the secrets can be cached on the client.
102100
// This is the recommended usage

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/keyvault/keyrotation/gettingstarted/KeyRotationGettingStarted.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939

4040
public class KeyRotationGettingStarted {
4141

42-
public static void main(String[] args) throws StorageException,
43-
InterruptedException, ExecutionException, URISyntaxException,
44-
NoSuchAlgorithmException, InvalidKeyException, IOException {
42+
public static void main(String[] args) throws Exception {
4543
Utility.printSampleStartInfo("KeyRotationGettingStarted");
4644

4745
// Create two secrets and obtain their IDs. This is normally a one-time

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/queue/gettingstarted/QueueGettingStarted.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.EnumSet;
2222
import java.util.UUID;
2323

24-
import com.microsoft.azure.keyvault.extensions.RsaKey;
24+
import com.microsoft.azure.keyvault.cryptography.RsaKey;
2525
import com.microsoft.azure.storage.CloudStorageAccount;
2626
import com.microsoft.azure.storage.StorageException;
2727
import com.microsoft.azure.storage.queue.CloudQueue;

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/table/gettingstarted/attributes/TableGettingStartedAttributes.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
import java.net.URISyntaxException;
1818
import java.security.InvalidKeyException;
19+
import java.security.KeyPair;
20+
import java.security.KeyPairGenerator;
1921
import java.security.NoSuchAlgorithmException;
2022
import java.util.Date;
2123
import java.util.UUID;
2224

23-
import com.microsoft.azure.keyvault.extensions.RsaKey;
25+
import com.microsoft.azure.keyvault.cryptography.RsaKey;
2426
import com.microsoft.azure.storage.CloudStorageAccount;
2527
import com.microsoft.azure.storage.StorageException;
2628
import com.microsoft.azure.storage.table.CloudTable;
@@ -51,7 +53,10 @@ public static void main(String[] args) throws URISyntaxException,
5153
table.createIfNotExists();
5254

5355
// Create the IKey used for encryption.
54-
RsaKey key = new RsaKey("private:key1");
56+
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
57+
keyGen.initialize(1024);
58+
final KeyPair wrapKey = keyGen.generateKeyPair();
59+
RsaKey key = new RsaKey("private:key1", wrapKey);
5560

5661
EncryptedEntity ent = new EncryptedEntity(UUID.randomUUID()
5762
.toString(), String.valueOf(new Date().getTime()));

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/encryption/table/gettingstarted/resolver/TableGettingStartedResolver.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.net.URISyntaxException;
1818
import java.security.InvalidKeyException;
19+
import java.security.KeyPair;
20+
import java.security.KeyPairGenerator;
1921
import java.security.NoSuchAlgorithmException;
2022
import java.util.Date;
2123
import java.util.UUID;
@@ -31,7 +33,7 @@
3133
import com.microsoft.azure.storage.table.TableResult;
3234
import com.microsoft.azure.storage.util.LocalResolver;
3335
import com.microsoft.azure.storage.util.Utility;
34-
import com.microsoft.azure.keyvault.extensions.RsaKey;
36+
import com.microsoft.azure.keyvault.cryptography.RsaKey;
3537
import com.microsoft.azure.storage.CloudStorageAccount;
3638
import com.microsoft.azure.storage.StorageException;
3739

@@ -54,7 +56,10 @@ public static void main(String[] args) throws StorageException,
5456
table.createIfNotExists();
5557

5658
// Create the IKey used for encryption.
57-
RsaKey key = new RsaKey("private:key1");
59+
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
60+
keyGen.initialize(1024);
61+
final KeyPair wrapKey = keyGen.generateKeyPair();
62+
RsaKey key = new RsaKey("rsaKey1", wrapKey);
5863

5964
DynamicTableEntity ent = new DynamicTableEntity();
6065
ent.setPartitionKey(UUID.randomUUID().toString());

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/logging/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
<dependency>
2727
<groupId>com.microsoft.azure</groupId>
2828
<artifactId>azure-storage</artifactId>
29-
<version>6.1.0</version>
29+
<version>7.0.0</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>com.microsoft.azure</groupId>
3333
<artifactId>azure-keyvault-extensions</artifactId>
34-
<version>0.8.0</version>
34+
<version>1.0.0</version>
3535
</dependency>
3636
<dependency>
3737
<groupId>org.slf4j</groupId>

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/table/payloadformat/PayloadFormat.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static void main(String[] args) throws InvalidKeyException, URISyntaxExce
8484
// Submit the operation to the table service.
8585
table.execute(insertCustomer1);
8686

87-
// When using JsonNoMetadata the client library will infer the property types(int, double,
87+
// When using JsonNoMetadata the client library will infer the property types(int, double,
8888
// String and some booleans) by inspecting the type information on the POJO entity type
8989
// provided by the client. Additionally, in some scenarios clients may wish to provide
9090
// the property type information at runtime such as when querying with the DynamicTableEntity
@@ -97,15 +97,15 @@ class PropertyResolverClass implements PropertyResolver {
9797
public EdmType propertyResolver(String pk, String rk, String key,
9898
String value) {
9999
if (key.equals("Email")) {
100-
return EdmType.STRING;
101-
}
102-
else if (key.equals("PhoneNumber")) {
103-
return EdmType.STRING;
104-
}
105-
else if (key.equals("Id")) {
106-
return EdmType.GUID;
107-
}
108-
return null;
100+
return EdmType.STRING;
101+
}
102+
else if (key.equals("PhoneNumber")) {
103+
return EdmType.STRING;
104+
}
105+
else if (key.equals("Id")) {
106+
return EdmType.GUID;
107+
}
108+
return null;
109109
}
110110
}
111111

microsoft-azure-storage-samples/src/com/microsoft/azure/storage/util/KVCredentials.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@
1414
*/
1515
package com.microsoft.azure.storage.util;
1616

17-
import java.util.Map;
1817
import java.util.concurrent.ExecutorService;
1918
import java.util.concurrent.Executors;
2019
import java.util.concurrent.Future;
2120

22-
import org.apache.http.Header;
23-
import org.apache.http.message.BasicHeader;
24-
2521
import com.microsoft.aad.adal4j.AuthenticationContext;
2622
import com.microsoft.aad.adal4j.AuthenticationResult;
2723
import com.microsoft.aad.adal4j.ClientCredential;
2824
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
29-
import com.microsoft.windowsazure.core.pipeline.filter.ServiceRequestContext;
3025

3126
/**
3227
* A class that stores KeyVault credentials and knows how to respond to
@@ -52,27 +47,6 @@ public KVCredentials(String clientID, String clientSecret) {
5247
this.authClientSecret = clientSecret;
5348
}
5449

55-
/**
56-
* Actually do the authentication. This method will be called by the super
57-
* class.
58-
*
59-
* @param request
60-
* The request being sent
61-
* @param challenge
62-
* Information about the challenge from the service.
63-
*/
64-
@Override
65-
public Header doAuthenticate(ServiceRequestContext request,
66-
Map<String, String> challenge) {
67-
String authorization = challenge.get("authorization");
68-
String resource = challenge.get("resource");
69-
String clientId = this.authClientID;
70-
String clientKey = this.authClientSecret;
71-
AuthenticationResult token = getAccessTokenFromClientCredentials(
72-
authorization, resource, clientId, clientKey);
73-
return new BasicHeader("Authorization", token.getAccessTokenType()
74-
+ " " + token.getAccessToken());
75-
}
7650

7751
/**
7852
* Creates the access token
@@ -112,4 +86,24 @@ private static AuthenticationResult getAccessTokenFromClientCredentials(
11286
}
11387
return result;
11488
}
89+
90+
/**
91+
* Actually do the authentication. This method will be called by the super
92+
* class.
93+
*
94+
* @param authorization
95+
* Identifier of the authority, a URL.
96+
* @param resource
97+
* Identifier of the target resource that is the recipient of the requested toke, a URL.
98+
* @param scope
99+
* The scope of the authentication request.
100+
*/
101+
@Override
102+
public String doAuthenticate(String authorization, String resource, String scope) {
103+
String clientId = this.authClientID;
104+
String clientKey = this.authClientSecret;
105+
AuthenticationResult token = getAccessTokenFromClientCredentials(
106+
authorization, resource, clientId, clientKey);
107+
return token.getAccessToken();
108+
}
115109
}

0 commit comments

Comments
 (0)