简体   繁体   English

使用 nodejs 从 Azure Keyvault 获取秘密

[英]Get Secret from Azure Keyvault using nodejs

I need to read the list of users in the Azure active directory.我需要阅读 Azure 活动目录中的用户列表。 The client has created a Graph API application but they do not want to share the client secret of the application, instead they asked us to use the Key vault.客户创建了一个 Graph API 应用程序,但他们不想共享应用程序的客户端密码,而是要求我们使用密钥库。 How to access from the node.js application the key to retrieve the list of users?如何从 node.js 应用程序访问密钥以检索用户列表?

I tried the below one but gave error and I am not sure how to authenticate.我尝试了下面的一个但给出了错误,我不确定如何进行身份验证。

const { DefaultAzureCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");

const credential = new DefaultAzureCredential();

const vaultName = "lsm-keyvault";
const url = `https://${vaultName}.vault.azure.net`;

const client = new SecretClient(url, credential);

const secretName = "Demo";

async function main() {
  const result = await client.setSecret(secretName, "MySecretValue", {
    enabled: false
  });

  console.log(result)
}

Well, if you run the code in local, the DefaultAzureCredential will use the environmental variables automatically.好吧,如果您在本地运行代码, DefaultAzureCredential将自动使用环境变量。

So in your case, you need to register an application with Azure AD , and get the tenant id , client id(ie application id) , client secret(ie application secret) , set the environmental variables , AZURE_CLIENT_ID , AZURE_CLIENT_SECRET , and AZURE_TENANT_ID .因此,在您的情况下,您需要使用 Azure AD 注册应用程序,并获取tenant idclient id(ie application id)client secret(ie application secret) ,设置环境变量AZURE_CLIENT_IDAZURE_CLIENT_SECRETAZURE_TENANT_ID

For the 403 error you got, I notice you said It added as a compound entity , based on my experience, you did not add the correct service principal related to the AD App correctly to the Access policies of the keyvault.对于您收到的 403 错误,我注意到您说It added as a compound entity ,根据我的经验,您没有将与 AD App 相关的正确服务主体正确添加到 keyvault 的Access policies中。 If you add it correctly, it will appear as APPLICATION , not COMPOUND IDENTITY .如果添加正确,它将显示为APPLICATION ,而不是COMPOUND IDENTITY

So when you add it, you could search for the client Id(ie application Id) or the name of your App Registration directly, make sure you add the correct one.所以当你添加它时,你可以直接搜索client Id(ie application Id)the name of your App Registration ,确保添加正确的。 I gave the details in this similar issue , you could refer to it.我在这个类似的问题中给出了详细信息,你可以参考它。

To retrieve the secret , the Get permission is enough, the code should be检索秘密Get权限就足够了,代码应该是

const retrievedSecret = await client.getSecret(secretName);

I notice you use client.setSecret in your code, it is used to save a secret , to use it, you may need the Set permission.我注意到您在代码中使用了client.setSecret ,它用于保存一个 secret ,要使用它,您可能需要Set权限。

For more details, see Quickstart: Azure Key Vault client library for Node.js (v4) .有关更多详细信息,请参阅快速入门:Azure Key Vault 客户端库,用于 Node.js (v4)

Update:更新:

I have to eventually need to deploy this but not in azure but in another environment.我最终必须部署它,但不是在 azure 中,而是在另一个环境中。 How do I set the environment variables and access it.如何设置环境变量并访问它。

If so, you need to change your code to authenticate, use the three values directly in the code.如果是这样,您需要更改代码进行身份验证,直接在代码中使用这三个值。

Change the lines换行

const { DefaultAzureCredential } = require("@azure/identity");
const credential = new DefaultAzureCredential();

To

const { ClientSecretCredential } = require("@azure/identity");
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);

See - https://www.npmjs.com/package/@azure/identity/v/1.0.3#authenticating-as-a-service-principal请参阅 - https://www.npmjs.com/package/@azure/identity/v/1.0.3#authenticating-as-a-service-principal

All you need to do is follow the below steps:您需要做的就是按照以下步骤操作:

  • Create an App in the Azure Active Directory (Service Principal) from App Registrations.通过应用注册在 Azure Active Directory(服务主体)中创建应用。
  • Go to Key Vault resource, Access Policy blade, assign read access to this Azure AD App (Service Principal) that we created in the above step. Go 到 Key Vault 资源,访问策略刀片,分配对我们在上述步骤中创建的此 Azure AD 应用程序(服务主体)的读取访问权限。
  • Set these 3 Environment variables AZURE_CLIENT_ID , AZURE_TENANT_ID , and AZURE_CLIENT_SECRET in your App Service.在您的应用服务中设置这 3 个环境变量AZURE_CLIENT_IDAZURE_TENANT_IDAZURE_CLIENT_SECRET Get the values of these variables from the app that we created in step 1.从我们在步骤 1 中创建的应用程序中获取这些变量的值。
  • Use DefaultAzureCredential that we are already using now.使用我们现在已经在使用的DefaultAzureCredential This will automatically pick the credentials from the environment variables that we defined in App Service for the authentication.这将自动从我们在应用服务中为身份验证定义的环境变量中选择凭据。

Another way is to obtain Key Vault token dynamically and use that token to get the secrets from the Key Vault - https://docs.microsoft.com/en-us/samples/azure-samples/app-service-msi-keyvault-node/app-service-msi-keyvault-node/另一种方法是动态获取 Key Vault 令牌并使用该令牌从 Key Vault 获取机密 - https://docs.microsoft.com/en-us/samples/azure-samples/app-service-msi-keyvault-节点/应用服务-msi-keyvault-node/

Helpful Reference:有用的参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从带有 MSI 的 linux vm 使用节点从 azure keyvault 获取秘密 - Getting a secret from azure keyvault with node from a linux vm with MSI Node.js天蓝色Keyvault证书 - nodejs azure keyvault certificate 从 Node.js 中的 Azure 密钥库获取正确的 pfx 文件 - Get correct pfx file from Azure keyvault in Node.js 登录并从 nodejs 中的 hashicorp 保险库获取机密 - Login and get secret from hashicorp vault in nodejs 如何等到我从 Node JS 中的 Keyvault 获得秘密值? - How to wait till I get the secret values from Keyvault in Node JS? 使用节点从 azure keyvault 中提取数据 - Pull data from azure keyvault with node 在Azure中使用来自Node.js的DiagnosticMonitor - using DiagnosticMonitor from nodejs in Azure 为什么在使用 @azure/keyvault-keys 和 @azure/identity 时需要租户,但在使用 azure-keyvault 时不需要? - Why do I need a tenant when using @azure/keyvault-keys with @azure/identity, but not when using azure-keyvault? Middy 没有从 NodeJS AWS Lambda 中的 Secret Manager 获得秘密 - Middy is not getting a secret from Secret Manager in a NodeJS AWS Lambda 如何使用 NodeJs 调用 REST API 的客户端 ID、租户 ID、azure AD 的客户端密码生成授权承载令牌? - How to generate Authorization Bearer token using client ID , tenant Id, Client secret of azure AD using NodeJs for calling REST API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM