客户端证书"/>
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的客户端证书
发布评论