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

Commit 4d7d611

Browse files
authored
change keyvault track1 to track2 (#26)
1 parent bac8579 commit 4d7d611

File tree

17 files changed

+120
-320
lines changed

17 files changed

+120
-320
lines changed

secure-app-model/keyvault/cpvsample/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ This sample demonstrates how a Control Panel Vendor partner can utilize the refr
99
The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified:
1010

1111
* **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored.
12-
* **keyvault.clientId** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
13-
* **keyvault.clientSecret** - The application secret associated with the application configured to access the instance of Azure Key Vault.
12+
* **AZURE_CLIENT_ID** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
13+
* **AZURE_CLIENT_SECRET** - The application secret associated with the application configured to access the instance of Azure Key Vault.
14+
* **AZURE_TENANT_ID** - The application tenant id associated with the application configured to access the instance of Azure Key Vault.
1415
* **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner.
1516
* **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API.
1617
* **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API.
1718
* **partnercenter.displayName** - The display name for the Azure AD application. This will be used during the consent process, so it must what is in Azure AD.
1819

19-
Please note that in production scenarios we recommend that you use certificate based authentication to access the instance of Azure Key Vault. The [confidential client flow](https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Confidential-client-applications-flows) has been used in the sample for simplicity.
20+
Please note that in production scenarios we recommend that you use certificate based authentication to access the instance of Azure Key Vault. The [confidential client flow](https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Confidential-client-applications-flows) has been used in the sample for simplicity.

secure-app-model/keyvault/cpvsample/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222
<version>1.18.0</version>
2323
</dependency>
2424
<dependency>
25-
<groupId>com.microsoft.azure</groupId>
26-
<artifactId>azure-keyvault</artifactId>
27-
<version>1.2.2</version>
25+
<groupId>com.azure</groupId>
26+
<artifactId>azure-identity</artifactId>
27+
<version>1.1.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.azure</groupId>
31+
<artifactId>azure-security-keyvault-secrets</artifactId>
32+
<version>4.2.1</version>
2833
</dependency>
2934
<dependency>
3035
<groupId>com.microsoft.graph</groupId>

secure-app-model/keyvault/cpvsample/src/main/java/com/microsoft/store/samples/secureappmodel/cpvsample/PropertyName.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public class PropertyName
2828
*/
2929
public static final String KEY_VAULT_CLIENT_ID = "keyvault.clientId";
3030

31+
/**
32+
* The name of the tenant Id property.
33+
*/
34+
public static final String KEY_VAULT_TENANT_ID = "keyvault.tenantId";
35+
3136
/**
3237
* The name of the client secret property.
3338
*/

secure-app-model/keyvault/cpvsample/src/main/java/com/microsoft/store/samples/secureappmodel/cpvsample/security/AccessTokenProvider.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ public AuthenticationResult getAccessTokenBySecureRefreshToken(String tenantId,
159159
throws ExecutionException, InterruptedException, MalformedURLException
160160
{
161161
IVaultProvider vault = new KeyVaultProvider(
162-
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL),
163-
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_ID),
164-
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_SECRET));
162+
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL));
165163

166164
return getAccessTokenByRefreshToken(
167165
tenantId,

secure-app-model/keyvault/cpvsample/src/main/java/com/microsoft/store/samples/secureappmodel/cpvsample/security/KeyVaultProvider.java

Lines changed: 22 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,149 +3,73 @@
33

44
package com.microsoft.store.samples.secureappmodel.cpvsample.security;
55

6-
import java.net.MalformedURLException;
7-
import java.util.concurrent.ExecutionException;
8-
import java.util.concurrent.ExecutorService;
9-
import java.util.concurrent.Executors;
10-
import java.util.concurrent.Future;
6+
import com.azure.identity.DefaultAzureCredentialBuilder;
7+
import com.azure.security.keyvault.secrets.SecretClient;
8+
import com.azure.security.keyvault.secrets.SecretClientBuilder;
119

12-
import com.microsoft.aad.adal4j.AuthenticationContext;
13-
import com.microsoft.aad.adal4j.AuthenticationResult;
14-
import com.microsoft.aad.adal4j.ClientCredential;
15-
import com.microsoft.azure.keyvault.KeyVaultClient;
16-
import com.microsoft.azure.keyvault.KeyVaultClientCustom;
17-
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
1810

1911
/**
2012
* Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault.
2113
*/
2214
public class KeyVaultProvider implements IVaultProvider
2315
{
2416
/**
25-
* The client used to interact with the Azure Key Vault service.
17+
* The client used to manage Secrets in the Azure KeyVault by interacting with the Azure Key Vault service.
2618
*/
27-
private KeyVaultClientCustom client;
19+
private SecretClient client;
2820

2921
/**
30-
* The vault name, e.g. https://myvault.vault.azure.net
22+
* The Vault URL, e.g. https://myvault.vault.azure.net
3123
*/
3224
private String vaultBaseUrl;
3325

3426
/**
3527
* Initializes a new instance of the {@link KeyVaultProvider} class.
3628
*
3729
* @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net
38-
* @param clientId The identifier of the client requesting the token.
39-
* @param clientSecret The secure secret of the client requesting the token.
4030
*/
41-
public KeyVaultProvider(String vaultBaseUrl, String clientId, String clientSecret)
31+
public KeyVaultProvider(String vaultBaseUrl)
4232
{
43-
client = getKeyVaultClient(clientId, clientSecret);
33+
client = getKeyVaultClient();
4434
this.vaultBaseUrl = vaultBaseUrl;
4535
}
4636

4737
/**
48-
* Gets the specified value from the vault.
38+
* Gets the value of the specified secret from the Azure Key Vault..
4939
*
5040
* @param secretName Identifier of the value to be retrieved.
5141
* @return The value for the specified secret.
5242
*/
5343
public String getSecret(String secretName)
5444
{
55-
return client.getSecret(vaultBaseUrl, secretName).value();
45+
return client.getSecret(secretName).getValue();
5646
}
5747

5848
/**
59-
* Stores the specified value in the vault.
49+
* Adds a secret with the specified {@code secretName} and {@code value} to the key vault if it does not exist.
50+
* If the named secret exists, a new version of the secret is created.
6051
*
6152
* @param secretName Identifier of the value to be stored.
6253
* @param value The value to be stored.
6354
*/
6455
public void setSecret(String secretName, String value)
6556
{
66-
client.setSecret(vaultBaseUrl, secretName, value);
57+
client.setSecret(secretName, value);
6758
}
6859

6960
/**
70-
* Gets an access token from the authority.
61+
* Gets the Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
7162
*
72-
* @param authorization Address of the authority to issue the token.
73-
* @param resource Identifier of the target resource that is the recipient of the requested token.
74-
* @param clientId The identifier of the client requesting the token.
75-
* @param clientSecret The secure secret of the client requesting the token.
76-
* @return An instance of {@link AuthenticationResult} that contians an access token and refresh token.
77-
*
78-
* @throws ExecutionException {@link ExecutionException}
79-
* @throws InterruptedException {@link InterruptedException}
80-
* @throws MalformedURLException {@link MalformedURLException}
63+
* @return The Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
8164
*/
82-
private AuthenticationResult getAccessToken(String authorization, String resource, String clientId, String clientSecret)
83-
throws ExecutionException, InterruptedException, MalformedURLException
65+
private SecretClient getKeyVaultClient()
8466
{
85-
AuthenticationContext authContext;
86-
AuthenticationResult authResult;
87-
ExecutorService service = null;
88-
Future<AuthenticationResult> future;
89-
90-
try
91-
{
92-
service = Executors.newFixedThreadPool(1);
93-
authContext = new AuthenticationContext(authorization, true, service);
94-
95-
future = authContext.acquireToken(
96-
resource,
97-
new ClientCredential(
98-
clientId,
99-
clientSecret),
100-
null);
101-
102-
authResult = future.get();
103-
104-
return authResult;
105-
}
106-
finally
107-
{
108-
service.shutdown();
109-
}
110-
}
67+
client = new SecretClientBuilder()
68+
.vaultUrl(vaultBaseUrl)
69+
.credential(new DefaultAzureCredentialBuilder().build())
70+
.buildClient();
11171

112-
/**
113-
* Gets a client that is capable of interacting with the Azure Key Vault service.
114-
*
115-
* @param clientId The identifier of the client requesting the token.
116-
* @param clientSecret The secure secret of the client requesting the token.
117-
*
118-
* @return A client that is capable of interacting with the Azure Key Vault service.
119-
*/
120-
private KeyVaultClientCustom getKeyVaultClient(String clientId, String clientSecret)
121-
{
122-
return new KeyVaultClient(new KeyVaultCredentials()
123-
{
124-
/**
125-
* @param authorization Address of the authority to issue the token.
126-
* @param resource Identifier of the target resource that is the recipient of the requested token, a URL.
127-
* @param scope The scope of the authentication request.
128-
*
129-
* @return Access token to be used with Azure Key Vault operations.
130-
*/
131-
@Override
132-
public String doAuthenticate(String authorization, String resource, String scope)
133-
{
134-
AuthenticationResult authResult;
135-
136-
try
137-
{
138-
authResult = getAccessToken(authorization, resource, clientId, clientSecret);
139-
140-
return authResult.getAccessToken();
141-
}
142-
catch(Exception ex)
143-
{
144-
ex.printStackTrace();
145-
}
146-
147-
return "";
148-
}
149-
});
72+
return client;
73+
15074
}
15175
}

secure-app-model/keyvault/cpvsample/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ azuread.authority=https://login.microsoftonline.com
22
keyvault.baseurl=
33
keyvault.clientId=
44
keyvault.clientSecret=
5+
keyvault.tenantId=
56
partnercenter.accountId=
67
partnercenter.clientId=
78
partnercenter.clientSecret=

secure-app-model/keyvault/cspsample/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ This sample demonstrates how a Cloud Solution Provider partner can utilize the r
99
The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified:
1010

1111
* **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored.
12-
* **keyvault.clientId** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
13-
* **keyvault.clientSecret** - The application secret associated with the application configured to access the instance of Azure Key Vault.
12+
* **AZURE_CLIENT_ID** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
13+
* **AZURE_CLIENT_SECRET** - The application secret associated with the application configured to access the instance of Azure Key Vault.
14+
* **AZURE_TENANT_ID** - The application tenant id associated with the application configured to access the instance of Azure Key Vault.
1415
* **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner.
1516
* **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API.
1617
* **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API.
1718

18-
Please note that in production scenarios we recommend that you use certificate based authentication to access the instance of Azure Key Vault. The [confidential client flow](https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Confidential-client-applications-flows) has been used in the sample for simplicity.
19+
Please note that in production scenarios we recommend that you use certificate based authentication to access the instance of Azure Key Vault. The [confidential client flow](https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Confidential-client-applications-flows) has been used in the sample for simplicity.

secure-app-model/keyvault/cspsample/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222
<version>1.18.0</version>
2323
</dependency>
2424
<dependency>
25-
<groupId>com.microsoft.azure</groupId>
26-
<artifactId>azure-keyvault</artifactId>
27-
<version>1.2.2</version>
25+
<groupId>com.azure</groupId>
26+
<artifactId>azure-identity</artifactId>
27+
<version>1.1.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.azure</groupId>
31+
<artifactId>azure-security-keyvault-secrets</artifactId>
32+
<version>4.2.1</version>
2833
</dependency>
2934
<dependency>
3035
<groupId>com.microsoft.graph</groupId>

secure-app-model/keyvault/cspsample/src/main/java/com/microsoft/store/samples/secureappmodel/cspsample/PropertyName.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class PropertyName
3333
*/
3434
public static final String KEY_VAULT_CLIENT_SECRET = "keyvault.clientSecret";
3535

36+
/**
37+
* The name of the tenant Id property.
38+
*/
39+
public static final String KEY_VAULT_TENANT_ID = "keyvault.tenantId";
40+
3641
/**
3742
* The name of the Partner Center account identifier property.
3843
*/

secure-app-model/keyvault/cspsample/src/main/java/com/microsoft/store/samples/secureappmodel/cspsample/security/AccessTokenProvider.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ public AuthenticationResult getAccessTokenBySecureRefreshToken(String tenantId,
159159
throws ExecutionException, InterruptedException, MalformedURLException
160160
{
161161
IVaultProvider vault = new KeyVaultProvider(
162-
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL),
163-
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_ID),
164-
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_SECRET));
162+
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL));
165163

166164
return getAccessTokenByRefreshToken(
167165
tenantId,

0 commit comments

Comments
 (0)