简体   繁体   中英

Error: Self-signed certificate, node.JS https client certificate authentication with request

I'd like to ask for your lights on the below: Using nodeJS, I'm trying to establish https connection between my server, which acts as a client in this case and an external server. We want to use client certificate validation, and the external server has provided self-signed private.key and client.crt certificate. My code is the below:

router.get('/mywebhook', function (req, res) {
console.log('/mywebhook');
var request = require("request");
//var options;
var options = { method: 'GET',
    url: 'https://externalserverURL'
    ,cert: fs.readFileSync('certs/client_crt.pem')
    ,key: fs.readFileSync('certs/key.pem')
}


request(options, function (error, response, body) {
    if (error){
        //throw new Error(error);
        console.log(error);
        return res.end(error); 
    }
    console.log('all ok')
    return res.end(body);
});

});

I am getting though, the below error: Error: self signed certificate code: 'DEPTH_ZERO_SELF_SIGNED_CERT'

Setting sst-strict = false is something I want to avoid. Am I missing something? Any help?

You may either add extra root certificates to your server or you may do Node spesific configuration with environment variable.

  • For Node to recognize your self signed certificate you may use NODE_EXTRA_CA_CERTS environment variable by giving path to file extra-ca-certs.pem file will make node add them:
env NODE_EXTRA_CA_CERTS=./rootCA.crt node client.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM