NodeJS在请求中使用Azure Key Vault的客户端证书

问题描述 投票:0回答:1

我正在尝试对需要客户端证书身份验证的API执行post请求。证书以pfx格式上传到Azure密钥保管库。

我可以使用npm模块@azure/keyvault-certificates@azure/keyvault-secrets(参考:https://github.com/Azure/azure-sdk-for-js/issues/7647#issuecomment-594935307)从Key Vault中检索证书

问题是,我不确定如何从这里开始。我正在使用axios执行服务器端请求,并将证书附加在自定义httpsAgent中,如下所示:

const { certPfx, passphrase, endpoint } = options
const headers = constructHeaders(options)

const axiosInst = axios.create({
    httpsAgent: new Agent({
        pfx: certPfx,
        // pfx: Buffer.from(certPfx) // Alternative using the `cer` property of the returned certificate
        passphrase // Set from env var (I've verified that this is correct)
    })
})

return axiosInst.post(endpoint, constructBody(), {
    headers
})

certPfx变量如果通过Uint8Array检索证书,则为keyvault-certificates;如果使用keyvault-secrets,则为base64字符串。

问题是,无论我使用哪种组合,一旦尝试发布请求,都会收到错误Error: wrong tag和失败。我尝试了使用和不使用密码短语以及使用cer Uint8Array和base64版本,但似乎没有任何效果。

是否有人使用Azure Key Vault的客户端证书来使用节点/打字稿进行https请求?

node.js https azure-keyvault client-certificates
1个回答
0
投票

看来,对证书进行编码的正确方法是使用base64缓冲区并将证书作为机密读取(参考:https://github.com/Azure/azure-sdk-for-js/issues/7647#issuecomment-594935307

因此,假设您拥有保存了证书的适当SecretClient和Key Vault,则可以执行此操作:

const secret = await client.getSecret(certName) // The secret name will be the same as the cert name
const certPfx = secret.value // assuming this is set.

const axiosInst = axios.create({
    httpsAgent: new Agent({
        pfx: Buffer.from(certPfx, "base64")
        passphrase: ""
    })
})

// Now perform the request
axiosInst.post(endpoint, constructBody(), {
    headers
})
© www.soinside.com 2019 - 2024. All rights reserved.