gpt4 book ai didi

node.js - 使用nodejs从Azure Keyvault获取Secret

转载 作者:行者123 更新时间:2023-12-03 00:36:28 25 4
gpt4 key购买 nike

我需要读取 Azure Active Directory 中的用户列表。客户已经创建了一个 Graph API 应用程序,但他们不想共享该应用程序的客户端 key ,而是要求我们使用 Key Vault。如何从 Node.js 应用程序访问 key 来检索用户列表?

我尝试了以下方法,但出现错误,并且我不知道如何进行身份验证。

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)
}

最佳答案

好吧,如果您在本地运行代码,DefaultAzureCredential将自动使用环境变量。

因此,就您而言,您需要 register an application with Azure AD ,并得到 tenant id , client id(i.e. application id) , client secret(i.e. application secret) ,设置environmental variables , AZURE_CLIENT_ID , AZURE_CLIENT_SECRET ,和AZURE_TENANT_ID .

对于您遇到的 403 错误,我注意到您说 It added as a compound entity ,根据我的经验,您没有将与AD App相关的正确服务主体正确添加到 Access policies的 keystore 。如果添加正确,它将显示为 APPLICATION ,不是COMPOUND IDENTITY .

所以当你添加它时,你可以搜索 client Id(i.e. application Id)the name of your App Registration直接,确保添加正确的。我在这个similar issue中给出了详细信息,你可以引用一下。

retrieve the secretGet权限足够了,代码应该是

const retrievedSecret = await client.getSecret(secretName);

我注意到你使用 client.setSecret在您的代码中,它用于 save a secret ,要使用它,您可能需要Set许可。

更多详情,请参阅 Quickstart: Azure Key Vault client library for Node.js (v4) .

更新:

I have to eventually need to deploy this but not in azure but in another environment. How do I set the environment variables and access it.

如果是这样,您需要更改代码进行身份验证,直接在代码中使用这三个值。

更改行

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

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

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

关于node.js - 使用nodejs从Azure Keyvault获取Secret,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63343346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com