How to extract Secret key from Azure key vault in Azure Function App on Nodejs stack

て烟熏妆下的殇ゞ 提交于 2020-07-22 05:19:48

问题


I have created an Azure Function app in Nodejs version 12. My hosting environment is windows. What is the easiest way to capture the username and password which are saved in Azure key vault inside my function. Also I am using Inline code Editor so how should be capture the credentials in code.

Thanks


回答1:


The node SDK used in above answer is going to be deprecated and won't have new feature and releases. Instead, the new versions are released here:

https://www.npmjs.com/package/@azure/keyvault-secrets

Here are the detailed steps to retrieve the secret value for your reference.

1.Enable system assigned managed identity in your function.

2.Add this service principal to the access policy of your key vault.

3.Install the dependencies to your function.

  "dependencies": {
    "@azure/identity": "^1.0.3",
    "@azure/keyvault-secrets": "^4.0.4"
  }

4.Here is my testing function code

module.exports = async function (context, req) {

const { DefaultAzureCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");
const keyVaultName = "tonykeyvault20190801";
const KVUri = "https://" + keyVaultName + ".vault.azure.net";

const credential = new DefaultAzureCredential();
const client = new SecretClient(KVUri, credential);

const retrievedSecret = await client.getSecret("username");
const username=retrievedSecret.value;
context.log(username);
  context.res = {
      body: username 
  };
}

5.The execution result.



来源:https://stackoverflow.com/questions/62518433/how-to-extract-secret-key-from-azure-key-vault-in-azure-function-app-on-nodejs-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!