使用用户名和密码的 Keyvault 身份验证

Keyvault Authentication using username and password

客户已创建密钥保管库并存储凭据。为了验证 key vault,我在节点中创建了应用程序并使用 client id 和 client secret,我能够读取这些秘密。但是现在客户不想使用 client id 和 client secret ,而是使用 AZURE 的用户名和密码访问程序中的 keyvault。它是一个没有 MFA 的 keyvault 访问专用用户。

我不确定我们是否可以从节点 js 使用用户名和密码访问密钥库。欢迎推荐。

谢谢

对于这个需求,我也认为使用username-password流是不必要的,客户端凭证流应该更好(如评论中提到的juunas)。但是如果客户还是想用username-password流程来实现,我可以提供一个示例如下供大家参考:

1. 您应该在 AD 中使用本机平台而不是 Web 平台注册应用程序。

并且请检查是否启用了“将应用程序视为 public 客户端”。

如果您的应用程序是网络平台,当您 运行 节点 js 代码时,即使您使用 username-password 流,它也会显示错误消息要求您提供“客户端密码”。

2. 您需要为您的应用程序添加 Azure Key Vault 权限。 并且不要忘记为此授予管理员同意。

3.那你可以参考下面的代码获取secret值。

const KeyVault = require('azure-keyvault');
const { AuthenticationContext } = require('adal-node');

const clientId = '<clientId>';
const username = '<username>';
const password = '<password>';
var secretAuthenticator = function (challenge, callback) {
    var context = new AuthenticationContext(challenge.authorization);
    return context.acquireTokenWithUsernamePassword(challenge.resource, username, password, clientId, function(
        err,
        tokenResponse,
    ) {
        if (err) throw err;

        var authorizationValue = tokenResponse.tokenType + ' ' + tokenResponse.accessToken;
        return callback(null, authorizationValue);
    });
};

var credentials = new KeyVault.KeyVaultCredentials(secretAuthenticator);
var client = new KeyVault.KeyVaultClient(credentials);
client.getSecret("https://<keyvaultname>.vault.azure.net/", "<secret name>", "<secret version>", function (err, result) {
    if (err) throw err;

    console.log("secret value is: " + result.value);
});