Thursday, 13 April 2017

UNABLE_TO_VERIFY_LEAF_SIGNATURE while using http-proxy in Node

All,

I am trying to create a simple proxy which forwards all requests verbatum to another server. To do this I'm using the "http-proxy" npm. I am trying to go from local to a cloud server. At first when I setup the http-proxy I saw an error "unable to verify the first certificate". After some research online I found it's probably related to the fact that I have a self-signed certificate. Because it's self-signed it's not in the certificate store and so can't be validated. But, beacause I don't need this during development, I added "secure: false" to ignore certificate verification. I know that's unsafe from production, but I'm just trying to get around this for now. This update actually got around this error.

Now, I am getting another error "UNABLE_TO_VERIFY_LEAF_SIGNATURE".

Can any one help me figure out how to get rid of this error? I've tried adding this: process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

But that still shows the error. I see the error in the event emitted by the http-proxy (See code below for this event). If I drill down itno the 'proxyRes' I can see this error in the proxyRes -> connection -> authorizationError -> UNABLE_TO_VERIFY_LEAF_SIGNATURE

Here is my code below:

'use strict'
require('dotenv').config({silent: true})
var util = require('util');
const loggerFactory = require('./utils/logger')
const express = require('express')
const defaultRouter = require('./routes/default')
var logger = loggerFactory.consoleLogger
const proxy = require('http-proxy');

module.exports = (config) => {
const app = express()
// app.use(loggerFactory.requestLogger())
app.set('json spaces', 2)
app.set('port', config.express.port)

app.use('', defaultRouter)

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'
var apiProxy = proxy.createProxyServer({});
var proxyUrl = process.env.HOMEINSPECTIONSERVER_URL;

app.use((req,res,next) => {
apiProxy.web(req, res,
    {
      target: proxyUrl,
      secure: false,
    }
);

apiProxy.on('error', function(e) {
  logger.error("Error during proxy call!")
  logger.error("This is the error : " + e)
  next('route')
});

apiProxy.on('proxyReq', function(proxyReq, req, res, options) {
  logger.info("---REQUEST---")
  console.log("---REQUEST---")
  // logger.info(util.inspect(proxyReq))
   proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

apiProxy.on('proxyRes', function (proxyRes, req, res) {
  // logger.info("---RESPONSE---")
  // logger.info(util.inspect(proxyRes))
  // logger.info("---RESPONSEEND---")
  logger.info('RAW Response from the target', 
JSON.stringify(proxyRes.headers, true, 2));
});

apiProxy.on('open', function (proxySocket) {
  proxySocket.on('data', hybiParseAndLogMessage);
});

apiProxy.on('close', function (res, socket, head) {
  console.log('Client disconnected');
});

apiProxy.on('start', function (req, res, target) {
  // console.log('Started Request!');
});
})

app.use((req, res) => {
// logger.info('starting request...')
res.json(res.locals.standardResponse)
})
app.use((err, req, res, next) => {
var statusCode = 500

if (res.locals.standardResponse) {
  res.locals.standardResponse.error = err
  statusCode = err.statusCode || 600
  logger.error(err)
  res.status(statusCode).json(res.locals.standardResponse)
}

if (err.error !== undefined && err.error.httpStatus !== undefined) {
  statusCode = err.error.httpStatus
} else {
  statusCode = err.statusCode || 600
}

logger.error(err)
res.status(statusCode).json(res.body)
})

return app

}



via Ian Michel

No comments:

Post a Comment