change keyvault track1 to track2 (#26)

This commit is contained in:
Yun Lu (MSFT) 2020-10-26 21:23:41 +08:00 коммит произвёл GitHub
Родитель bac85794b8
Коммит 4d7d611cfe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
17 изменённых файлов: 120 добавлений и 320 удалений

Просмотреть файл

@ -9,8 +9,9 @@ This sample demonstrates how a Control Panel Vendor partner can utilize the refr
The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified: The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified:
* **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored. * **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored.
* **keyvault.clientId** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault. * **AZURE_CLIENT_ID** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
* **keyvault.clientSecret** - The application secret associated with the application configured to access the instance of Azure Key Vault. * **AZURE_CLIENT_SECRET** - The application secret associated with the application configured to access the instance of Azure Key Vault.
* **AZURE_TENANT_ID** - The application tenant id associated with the application configured to access the instance of Azure Key Vault.
* **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner. * **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner.
* **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API. * **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API.
* **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API. * **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API.

Просмотреть файл

@ -22,9 +22,14 @@
<version>1.18.0</version> <version>1.18.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.azure</groupId> <groupId>com.azure</groupId>
<artifactId>azure-keyvault</artifactId> <artifactId>azure-identity</artifactId>
<version>1.2.2</version> <version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.2.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.graph</groupId> <groupId>com.microsoft.graph</groupId>

Просмотреть файл

@ -28,6 +28,11 @@ public class PropertyName
*/ */
public static final String KEY_VAULT_CLIENT_ID = "keyvault.clientId"; public static final String KEY_VAULT_CLIENT_ID = "keyvault.clientId";
/**
* The name of the tenant Id property.
*/
public static final String KEY_VAULT_TENANT_ID = "keyvault.tenantId";
/** /**
* The name of the client secret property. * The name of the client secret property.
*/ */

Просмотреть файл

@ -159,9 +159,7 @@ public class AccessTokenProvider implements IAccessTokenProvider
throws ExecutionException, InterruptedException, MalformedURLException throws ExecutionException, InterruptedException, MalformedURLException
{ {
IVaultProvider vault = new KeyVaultProvider( IVaultProvider vault = new KeyVaultProvider(
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL), properties.getProperty(PropertyName.KEY_VAULT_BASE_URL));
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_ID),
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_SECRET));
return getAccessTokenByRefreshToken( return getAccessTokenByRefreshToken(
tenantId, tenantId,

Просмотреть файл

@ -3,18 +3,10 @@
package com.microsoft.store.samples.secureappmodel.cpvsample.security; package com.microsoft.store.samples.secureappmodel.cpvsample.security;
import java.net.MalformedURLException; import com.azure.identity.DefaultAzureCredentialBuilder;
import java.util.concurrent.ExecutionException; import com.azure.security.keyvault.secrets.SecretClient;
import java.util.concurrent.ExecutorService; import com.azure.security.keyvault.secrets.SecretClientBuilder;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.KeyVaultClientCustom;
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
/** /**
* Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault. * Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault.
@ -22,12 +14,12 @@ import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
public class KeyVaultProvider implements IVaultProvider public class KeyVaultProvider implements IVaultProvider
{ {
/** /**
* The client used to interact with the Azure Key Vault service. * The client used to manage Secrets in the Azure KeyVault by interacting with the Azure Key Vault service.
*/ */
private KeyVaultClientCustom client; private SecretClient client;
/** /**
* The vault name, e.g. https://myvault.vault.azure.net * The Vault URL, e.g. https://myvault.vault.azure.net
*/ */
private String vaultBaseUrl; private String vaultBaseUrl;
@ -35,117 +27,49 @@ public class KeyVaultProvider implements IVaultProvider
* Initializes a new instance of the {@link KeyVaultProvider} class. * Initializes a new instance of the {@link KeyVaultProvider} class.
* *
* @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net * @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*/ */
public KeyVaultProvider(String vaultBaseUrl, String clientId, String clientSecret) public KeyVaultProvider(String vaultBaseUrl)
{ {
client = getKeyVaultClient(clientId, clientSecret); client = getKeyVaultClient();
this.vaultBaseUrl = vaultBaseUrl; this.vaultBaseUrl = vaultBaseUrl;
} }
/** /**
* Gets the specified value from the vault. * Gets the value of the specified secret from the Azure Key Vault..
* *
* @param secretName Identifier of the value to be retrieved. * @param secretName Identifier of the value to be retrieved.
* @return The value for the specified secret. * @return The value for the specified secret.
*/ */
public String getSecret(String secretName) public String getSecret(String secretName)
{ {
return client.getSecret(vaultBaseUrl, secretName).value(); return client.getSecret(secretName).getValue();
} }
/** /**
* Stores the specified value in the vault. * Adds a secret with the specified {@code secretName} and {@code value} to the key vault if it does not exist.
* If the named secret exists, a new version of the secret is created.
* *
* @param secretName Identifier of the value to be stored. * @param secretName Identifier of the value to be stored.
* @param value The value to be stored. * @param value The value to be stored.
*/ */
public void setSecret(String secretName, String value) public void setSecret(String secretName, String value)
{ {
client.setSecret(vaultBaseUrl, secretName, value); client.setSecret(secretName, value);
} }
/** /**
* Gets an access token from the authority. * Gets the Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* *
* @param authorization Address of the authority to issue the token. * @return The Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* @param resource Identifier of the target resource that is the recipient of the requested token.
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
* @return An instance of {@link AuthenticationResult} that contians an access token and refresh token.
*
* @throws ExecutionException {@link ExecutionException}
* @throws InterruptedException {@link InterruptedException}
* @throws MalformedURLException {@link MalformedURLException}
*/ */
private AuthenticationResult getAccessToken(String authorization, String resource, String clientId, String clientSecret) private SecretClient getKeyVaultClient()
throws ExecutionException, InterruptedException, MalformedURLException
{ {
AuthenticationContext authContext; client = new SecretClientBuilder()
AuthenticationResult authResult; .vaultUrl(vaultBaseUrl)
ExecutorService service = null; .credential(new DefaultAzureCredentialBuilder().build())
Future<AuthenticationResult> future; .buildClient();
try return client;
{
service = Executors.newFixedThreadPool(1);
authContext = new AuthenticationContext(authorization, true, service);
future = authContext.acquireToken(
resource,
new ClientCredential(
clientId,
clientSecret),
null);
authResult = future.get();
return authResult;
}
finally
{
service.shutdown();
}
}
/**
* Gets a client that is capable of interacting with the Azure Key Vault service.
*
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*
* @return A client that is capable of interacting with the Azure Key Vault service.
*/
private KeyVaultClientCustom getKeyVaultClient(String clientId, String clientSecret)
{
return new KeyVaultClient(new KeyVaultCredentials()
{
/**
* @param authorization Address of the authority to issue the token.
* @param resource Identifier of the target resource that is the recipient of the requested token, a URL.
* @param scope The scope of the authentication request.
*
* @return Access token to be used with Azure Key Vault operations.
*/
@Override
public String doAuthenticate(String authorization, String resource, String scope)
{
AuthenticationResult authResult;
try
{
authResult = getAccessToken(authorization, resource, clientId, clientSecret);
return authResult.getAccessToken();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
});
} }
} }

Просмотреть файл

@ -2,6 +2,7 @@ azuread.authority=https://login.microsoftonline.com
keyvault.baseurl= keyvault.baseurl=
keyvault.clientId= keyvault.clientId=
keyvault.clientSecret= keyvault.clientSecret=
keyvault.tenantId=
partnercenter.accountId= partnercenter.accountId=
partnercenter.clientId= partnercenter.clientId=
partnercenter.clientSecret= partnercenter.clientSecret=

Просмотреть файл

@ -9,8 +9,9 @@ This sample demonstrates how a Cloud Solution Provider partner can utilize the r
The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified: The following configurations in the [application.properties](src/main/resources/application.properties) file need to be modified:
* **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored. * **keyvault.baseurl** - The base address for the instance of Azure Key Vault where the refresh token has been stored.
* **keyvault.clientId** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault. * **AZURE_CLIENT_ID** - The identifier for the Azure AD application that has been allowed access to the instance of Azure Key Vault.
* **keyvault.clientSecret** - The application secret associated with the application configured to access the instance of Azure Key Vault. * **AZURE_CLIENT_SECRET** - The application secret associated with the application configured to access the instance of Azure Key Vault.
* **AZURE_TENANT_ID** - The application tenant id associated with the application configured to access the instance of Azure Key Vault.
* **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner. * **partnercenter.accountId** - The account identifier, also known as the Azure AD tenant identifier, for the partner.
* **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API. * **partnercenter.clientId** - The application identifier for the Azure AD application configured for use with the Partner Center API.
* **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API. * **partnercenter.clientSecret** - The application secret associated with the application configured to access the Partner Center API.

Просмотреть файл

@ -22,9 +22,14 @@
<version>1.18.0</version> <version>1.18.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.azure</groupId> <groupId>com.azure</groupId>
<artifactId>azure-keyvault</artifactId> <artifactId>azure-identity</artifactId>
<version>1.2.2</version> <version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.2.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.graph</groupId> <groupId>com.microsoft.graph</groupId>

Просмотреть файл

@ -33,6 +33,11 @@ public class PropertyName
*/ */
public static final String KEY_VAULT_CLIENT_SECRET = "keyvault.clientSecret"; public static final String KEY_VAULT_CLIENT_SECRET = "keyvault.clientSecret";
/**
* The name of the tenant Id property.
*/
public static final String KEY_VAULT_TENANT_ID = "keyvault.tenantId";
/** /**
* The name of the Partner Center account identifier property. * The name of the Partner Center account identifier property.
*/ */

Просмотреть файл

@ -159,9 +159,7 @@ public class AccessTokenProvider implements IAccessTokenProvider
throws ExecutionException, InterruptedException, MalformedURLException throws ExecutionException, InterruptedException, MalformedURLException
{ {
IVaultProvider vault = new KeyVaultProvider( IVaultProvider vault = new KeyVaultProvider(
properties.getProperty(PropertyName.KEY_VAULT_BASE_URL), properties.getProperty(PropertyName.KEY_VAULT_BASE_URL));
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_ID),
properties.getProperty(PropertyName.KEY_VAULT_CLIENT_SECRET));
return getAccessTokenByRefreshToken( return getAccessTokenByRefreshToken(
tenantId, tenantId,

Просмотреть файл

@ -3,18 +3,10 @@
package com.microsoft.store.samples.secureappmodel.cspsample.security; package com.microsoft.store.samples.secureappmodel.cspsample.security;
import java.net.MalformedURLException; import com.azure.identity.DefaultAzureCredentialBuilder;
import java.util.concurrent.ExecutionException; import com.azure.security.keyvault.secrets.SecretClient;
import java.util.concurrent.ExecutorService; import com.azure.security.keyvault.secrets.SecretClientBuilder;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.KeyVaultClientCustom;
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
/** /**
* Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault. * Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault.
@ -22,12 +14,12 @@ import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
public class KeyVaultProvider implements IVaultProvider public class KeyVaultProvider implements IVaultProvider
{ {
/** /**
* The client used to interact with the Azure Key Vault service. * The client used to manage Secrets in the Azure KeyVault by interacting with the Azure Key Vault service.
*/ */
private KeyVaultClientCustom client; private SecretClient client;
/** /**
* The vault name, e.g. https://myvault.vault.azure.net * The Vault URL, e.g. https://myvault.vault.azure.net
*/ */
private String vaultBaseUrl; private String vaultBaseUrl;
@ -35,117 +27,49 @@ public class KeyVaultProvider implements IVaultProvider
* Initializes a new instance of the {@link KeyVaultProvider} class. * Initializes a new instance of the {@link KeyVaultProvider} class.
* *
* @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net * @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*/ */
public KeyVaultProvider(String vaultBaseUrl, String clientId, String clientSecret) public KeyVaultProvider(String vaultBaseUrl)
{ {
client = getKeyVaultClient(clientId, clientSecret); client = getKeyVaultClient();
this.vaultBaseUrl = vaultBaseUrl; this.vaultBaseUrl = vaultBaseUrl;
} }
/** /**
* Gets the specified value from the vault. * Gets the value of the specified secret from the Azure Key Vault..
* *
* @param secretName Identifier of the value to be retrieved. * @param secretName Identifier of the value to be retrieved.
* @return The value for the specified secret. * @return The value for the specified secret.
*/ */
public String getSecret(String secretName) public String getSecret(String secretName)
{ {
return client.getSecret(vaultBaseUrl, secretName).value(); return client.getSecret(secretName).getValue();
} }
/** /**
* Stores the specified value in the vault. * Adds a secret with the specified {@code secretName} and {@code value} to the key vault if it does not exist.
* If the named secret exists, a new version of the secret is created.
* *
* @param secretName Identifier of the value to be stored. * @param secretName Identifier of the value to be stored.
* @param value The value to be stored. * @param value The value to be stored.
*/ */
public void setSecret(String secretName, String value) public void setSecret(String secretName, String value)
{ {
client.setSecret(vaultBaseUrl, secretName, value); client.setSecret(secretName, value);
} }
/** /**
* Gets an access token from the authority. * Gets the Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* *
* @param authorization Address of the authority to issue the token. * @return The Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* @param resource Identifier of the target resource that is the recipient of the requested token.
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
* @return An instance of {@link AuthenticationResult} that contians an access token and refresh token.
*
* @throws ExecutionException {@link ExecutionException}
* @throws InterruptedException {@link InterruptedException}
* @throws MalformedURLException {@link MalformedURLException}
*/ */
private AuthenticationResult getAccessToken(String authorization, String resource, String clientId, String clientSecret) private SecretClient getKeyVaultClient()
throws ExecutionException, InterruptedException, MalformedURLException
{ {
AuthenticationContext authContext; client = new SecretClientBuilder()
AuthenticationResult authResult; .vaultUrl(vaultBaseUrl)
ExecutorService service = null; .credential(new DefaultAzureCredentialBuilder().build())
Future<AuthenticationResult> future; .buildClient();
try return client;
{
service = Executors.newFixedThreadPool(1);
authContext = new AuthenticationContext(authorization, true, service);
future = authContext.acquireToken(
resource,
new ClientCredential(
clientId,
clientSecret),
null);
authResult = future.get();
return authResult;
}
finally
{
service.shutdown();
}
}
/**
* Gets a client that is capable of interacting with the Azure Key Vault service.
*
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*
* @return A client that is capable of interacting with the Azure Key Vault service.
*/
private KeyVaultClientCustom getKeyVaultClient(String clientId, String clientSecret)
{
return new KeyVaultClient(new KeyVaultCredentials()
{
/**
* @param authorization Address of the authority to issue the token.
* @param resource Identifier of the target resource that is the recipient of the requested token, a URL.
* @param scope The scope of the authentication request.
*
* @return Access token to be used with Azure Key Vault operations.
*/
@Override
public String doAuthenticate(String authorization, String resource, String scope)
{
AuthenticationResult authResult;
try
{
authResult = getAccessToken(authorization, resource, clientId, clientSecret);
return authResult.getAccessToken();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
});
} }
} }

Просмотреть файл

@ -2,6 +2,7 @@ azuread.authority=https://login.microsoftonline.com
keyvault.baseurl= keyvault.baseurl=
keyvault.clientId= keyvault.clientId=
keyvault.clientSecret= keyvault.clientSecret=
keyvault.tenantId=
partnercenter.accountId= partnercenter.accountId=
partnercenter.clientId= partnercenter.clientId=
partnercenter.clientSecret= partnercenter.clientSecret=

Просмотреть файл

@ -11,7 +11,8 @@ The required configurations for this sample are found in the [web.xml](src/main/
* **client_id** - The application identifier that represent your application. * **client_id** - The application identifier that represent your application.
* **client_secret** - The secret associated with the application that represents your application. * **client_secret** - The secret associated with the application that represents your application.
* **keyvault_url** - The base address for the instance of Azure Key Vault you have deployed (e.g. <https://myvault.vault.azure.net/>). * **keyvault_url** - The base address for the instance of Azure Key Vault you have deployed (e.g. <https://myvault.vault.azure.net/>).
* **keyvault_cient_id** - The application identifier that you have configured to access the instance of Azure Key Vault. * **AZURE_CLIENT_ID** - The application identifier that you have configured to access the instance of Azure Key Vault.
* **keyvault_client_secret** - The application secret associated with the application configured to access the instance of Azure Key Vault. * **AZURE_CLIENT_SECRET** - The application secret associated with the application configured to access the instance of Azure Key Vault.
* **AZURE_TENANT_ID** - The application tenant id associated with the application configured to access the instance of Azure Key Vault.
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. 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.

Просмотреть файл

@ -34,9 +34,14 @@
<version>1.7.30</version> <version>1.7.30</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.azure</groupId> <groupId>com.azure</groupId>
<artifactId>azure-keyvault</artifactId> <artifactId>azure-identity</artifactId>
<version>1.2.2</version> <version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.2.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>

Просмотреть файл

@ -233,9 +233,7 @@ public class AuthenticationFilter implements Filter
redirectUrl = getConfigValue(config, "redirect_url"); redirectUrl = getConfigValue(config, "redirect_url");
vault = new KeyVaultProvider( vault = new KeyVaultProvider(
getConfigValue(config, "keyvault_base_url"), getConfigValue(config, "keyvault_base_url"));
getConfigValue(config, "keyvault_client_id"),
getConfigValue(config, "keyvault_client_secret"));
} }
/** /**

Просмотреть файл

@ -3,18 +3,10 @@
package com.microsoft.store.samples.partnerconsent.security; package com.microsoft.store.samples.partnerconsent.security;
import java.net.MalformedURLException; import com.azure.identity.DefaultAzureCredentialBuilder;
import java.util.concurrent.ExecutionException; import com.azure.security.keyvault.secrets.SecretClient;
import java.util.concurrent.ExecutorService; import com.azure.security.keyvault.secrets.SecretClientBuilder;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.KeyVaultClientCustom;
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
/** /**
* Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault. * Provides a secure mechanism for retrieving and store sensitive information using Azure Key Vault.
@ -22,12 +14,12 @@ import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
public class KeyVaultProvider implements IVaultProvider public class KeyVaultProvider implements IVaultProvider
{ {
/** /**
* The client used to interact with the Azure Key Vault service. * The client used to manage Secrets in the Azure KeyVault by interacting with the Azure Key Vault service.
*/ */
private KeyVaultClientCustom client; private SecretClient client;
/** /**
* The vault name, e.g. https://myvault.vault.azure.net * The Vault URL, e.g. https://myvault.vault.azure.net
*/ */
private String vaultBaseUrl; private String vaultBaseUrl;
@ -35,117 +27,49 @@ public class KeyVaultProvider implements IVaultProvider
* Initializes a new instance of the {@link KeyVaultProvider} class. * Initializes a new instance of the {@link KeyVaultProvider} class.
* *
* @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net * @param vaultBaseUrl The vault name, e.g. https://myvault.vault.azure.net
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*/ */
public KeyVaultProvider(String vaultBaseUrl, String clientId, String clientSecret) public KeyVaultProvider(String vaultBaseUrl)
{ {
client = getKeyVaultClient(clientId, clientSecret); client = getKeyVaultClient();
this.vaultBaseUrl = vaultBaseUrl; this.vaultBaseUrl = vaultBaseUrl;
} }
/** /**
* Gets the specified value from the vault. * Gets the value of the specified secret from the Azure Key Vault..
* *
* @param secretName Identifier of the value to be retrieved. * @param secretName Identifier of the value to be retrieved.
* @return The value for the specified secret. * @return The value for the specified secret.
*/ */
public String getSecret(String secretName) public String getSecret(String secretName)
{ {
return client.getSecret(vaultBaseUrl, secretName).value(); return client.getSecret(secretName).getValue();
} }
/** /**
* Stores the specified value in the vault. * Adds a secret with the specified {@code secretName} and {@code value} to the key vault if it does not exist.
* If the named secret exists, a new version of the secret is created.
* *
* @param secretName Identifier of the value to be stored. * @param secretName Identifier of the value to be stored.
* @param value The value to be stored. * @param value The value to be stored.
*/ */
public void setSecret(String secretName, String value) public void setSecret(String secretName, String value)
{ {
client.setSecret(vaultBaseUrl, secretName, value); client.setSecret(secretName, value);
} }
/** /**
* Gets an access token from the authority. * Gets the Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* *
* @param authorization Address of the authority to issue the token. * @return The Secret Client, capable of managing Secrets in the Azure Key Vault by interacting with Azure Key Vault service.
* @param resource Identifier of the target resource that is the recipient of the requested token.
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
* @return An instance of {@link AuthenticationResult} that contains an access token and refresh token.
*
* @throws ExecutionException {@link ExecutionException}
* @throws InterruptedException {@link InterruptedException}
* @throws MalformedURLException {@link MalformedURLException}
*/ */
private AuthenticationResult getAccessToken(String authorization, String resource, String clientId, String clientSecret) private SecretClient getKeyVaultClient()
throws ExecutionException, InterruptedException, MalformedURLException
{ {
AuthenticationContext authContext; client = new SecretClientBuilder()
AuthenticationResult authResult; .vaultUrl(vaultBaseUrl)
ExecutorService service = null; .credential(new DefaultAzureCredentialBuilder().build())
Future<AuthenticationResult> future; .buildClient();
try return client;
{
service = Executors.newFixedThreadPool(1);
authContext = new AuthenticationContext(authorization, true, service);
future = authContext.acquireToken(
resource,
new ClientCredential(
clientId,
clientSecret),
null);
authResult = future.get();
return authResult;
}
finally
{
service.shutdown();
}
}
/**
* Gets a client that is capable of interacting with the Azure Key Vault service.
*
* @param clientId The identifier of the client requesting the token.
* @param clientSecret The secure secret of the client requesting the token.
*
* @return A client that is capable of interacting with the Azure Key Vault service.
*/
private KeyVaultClientCustom getKeyVaultClient(String clientId, String clientSecret)
{
return new KeyVaultClient(new KeyVaultCredentials()
{
/**
* @param authorization Address of the authority to issue the token.
* @param resource Identifier of the target resource that is the recipient of the requested token, a URL.
* @param scope The scope of the authentication request.
*
* @return Access token to be used with Azure Key Vault operations.
*/
@Override
public String doAuthenticate(String authorization, String resource, String scope)
{
AuthenticationResult authResult;
try
{
authResult = getAccessToken(authorization, resource, clientId, clientSecret);
return authResult.getAccessToken();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
});
} }
} }

Просмотреть файл

@ -35,6 +35,10 @@
<param-name>keyvault_client_secret</param-name> <param-name>keyvault_client_secret</param-name>
<param-value></param-value> <param-value></param-value>
</init-param> </init-param>
<init-param>
<param-name>keyvault_tenant_id</param-name>
<param-value></param-value>
</init-param>
<init-param> <init-param>
<param-name>keyvault_certifcate_path</param-name> <param-name>keyvault_certifcate_path</param-name>
<param-value></param-value> <param-value></param-value>