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

编程入门 行业动态 更新时间:2024-10-10 06:22:20

NodeJS在请求中使用Azure Key Vault的<a href=https://www.elefans.com/category/jswz/34/1771403.html style=客户端证书"/>

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

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

我可以使用npm模块@azure/keyvault-certificates@azure/keyvault-secrets(参考:)从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请求?

回答如下:

看来,对证书进行编码的正确方法是使用base64缓冲区并将证书作为机密读取(参考:https://github/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
})

更多推荐

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

本文发布于:2024-05-07 20:25:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   证书   Azure   NodeJS   Vault

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!