0
votes

How can I fetch the secret key from azure key vault in reactjs application? I am passing my secret key in headers like below

 headers: {
  "subscription-key": "xxxxxxxxxxxxx",
  "content-type": "application/json"
  }

How can I get the "subscription-key" value from azure key vault? I added my subscription-key value in the azure key vault.

1
Any update now? If it helps you, please accept it as asnwer. - Joey Cai

1 Answers

0
votes

In local, you can sue loginWithServicePrincipalSecret with clientid and secret to get the credential.

function getKeyVaultCredentials(){
    return msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain);
}

function getKeyVaultSecret(credentials) {
    let keyVaultClient = new KeyVault.KeyVaultClient(credentials);
    return keyVaultClient.getSecret(KEY_VAULT_URI, 'secret', "");
}

getKeyVaultCredentials().then(
    getKeyVaultSecret
).then(function (secret){
    console.log(`Your secret value is: ${secret.value}.`);
}).catch(function (err) {
    throw (err);
});

Also, you could use the loginWithAppServiceMSI() method from ms-rest-azure will autodetect if you're on a WebApp and get the token from the MSI endpoint. So you must host your code on Azure webapp. Refer to this article for more details.

function getKeyVaultCredentials(){
    return msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'});   
}