I have a azure key vault and i am trying to get the secret from that vault and print it in console in node js. This is the code i got from documentation,
const { DefaultAzureCredential} = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");
const credential = new DefaultAzureCredential();
// Build the URL to reach your key vault
const vaultName = "web-designer";
const url = `https://${vaultName}.vault.azure.net`;
// Lastly, create our secrets client and connect to the service
const client = new SecretClient(url, credential);
const secretName = "web-designer-secret";
client.getSecret(secretName).then((res)=>{
console.log(res);
}).catch((err)=>{
console.log("error",err);
})
Here they are using DefaultAzurecredential to validate account. But instead of that i want to use client id and client secret to validate my key vault. How to do this without DefaultAzureCredential?
I tried ManagedIdentityCredential but not working.
CodePudding user response:
After reproducing from my end, I could able to achieve Authentication using ClientSecretCredential
following the below code.
const { ClientSecretCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");
const credential = new ClientSecretCredential("<TENANTID>","<CLIENTID>","<CLIENTSECRET>");
const vaultName = "<VAULTNAME>";
const url = `https://${vaultName}.vault.azure.net`;
const client = new SecretClient(url, credential);
const secretName = "<SECRETNAME>";
client.getSecret(secretName).then((res)=>{
console.log(res);
}).catch((err)=>{
console.log("error",err);
})
RESULTS: