Below is the Java code to get Azure Key Vault Secret key Value.
Azure Key Vault is a cloud service that provides secure storage for secrets, such as passwords and database connection strings.
The Azure Key Vault Secrets client library allows you to securely store and tightly control the access to tokens, passwords, API keys, and other secrets. This library offers operations to create, retrieve, update, delete, purge, backup, restore, and list the secrets and its versions.
The following information is required to access the Azure Key Vault:
- Key Vault URL
- Client Id
- Client Key
Use the Azure Key Vault Secrets client library to create and manage secrets.
<dependency> <groupId>com.azure</groupId> <artifactId>azure-security-keyvault-secrets</artifactId> <version>4.2.7</version> </dependency>
Other Required Dependency.
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.12.3</version> </dependency>
import java.util.concurrent.ExecutorService; 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.authentication.KeyVaultCredentials; import com.microsoft.azure.keyvault.models.SecretBundle; /** * @author pushkarkhosla * */ public class AzureKeyVaultJavaConfigurationExample extends KeyVaultCredentials { private String clientId; private String clientKey; public AzureKeyVaultJavaConfigurationExample(String clientId, String clientKey) { this.clientId = clientId; this.clientKey = clientKey; } @Override public String doAuthenticate(String authorization, String resource, String scope) { AuthenticationResult token = getAccessTokenFromClientCredentials(authorization, resource, clientId, clientKey); return token.getAccessToken(); } private static AuthenticationResult getAccessTokenFromClientCredentials(String authorization, String resource, String clientId, String clientKey) { AuthenticationContext context = null; AuthenticationResult result = null; ExecutorService service = null; try { service = Executors.newFixedThreadPool(1); context = new AuthenticationContext(authorization, false, service); ClientCredential credentials = new ClientCredential(clientId, clientKey); Future<AuthenticationResult> future = context.acquireToken(resource, credentials, null); result = future.get(); } catch (Exception e) { throw new RuntimeException(e); } finally { service.shutdown(); } if (result == null) { throw new RuntimeException("authentication result was null"); } return result; } public static void main(String[] args) { try { KeyVaultClient client = new KeyVaultClient(new AzureKeyVaultJavaConfigurationExample( "Client Id", "Client Key")); SecretBundle secret = client.getSecret("Azure Key Vault Url", "Secret Key"); System.out.println("------" + secret.value()); } catch (Exception e) { e.printStackTrace(); } } }
Java I/O Tutorial
Thank you, for this information
ReplyDeleteThe blog is very informative. looking forward to reading more from you thank you
cbse class 10 tuition