【问题标题】:http-proxy-middleware + express + node.js - unable to redirect to end-point with client side certificate authentication enabledhttp-proxy-middleware + express + node.js - 无法重定向到启用客户端证书身份验证的端点
【发布时间】:2020-05-09 13:40:46
【问题描述】:

我正在使用 http-proxy-middleware (https://www.npmjs.com/package/http-proxy-middleware) 来实现另一个 REST API 的代理,该 API 启用了基于客户端证书的身份验证(requestCert:true,rejectUnauthorized:true)。

客户端调用代理 API (https://localhost:3000/auth),其中配置了 http-proxy-middleware,并且应该将其代理到另一个启用了基于客户端证书的身份验证的 REST API (https://localhost:3002/auth) (requestCert: true , rejectUnauthorized: true)。

我不希望在代理上进行任何特定的身份验证。当我使用基于客户端证书的身份验证路由到此目标端点的路径调用代理时,它失败并显示错误消息:

代理服务器收到错误:

[HPM] Rewriting path from "/auth" to ""
[HPM] GET /auth ~> https://localhost:3002/auth

RAW REQUEST from the target {
  "host": "localhost:3000",
  "connection": "close"
}
redirecting to auth
[HPM] Error occurred while trying to proxy request  from localhost:3000 to https://localhost:3002/auth (EPROTO) (https://nodejs.org/api/errors.html#errors_common_system_errors)

客户端收到错误:

Proxy error: Error: write EPROTO 28628:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:c:\ws\deps\openssl\openssl\ssl\record\rec_layer_s3.c:1536:SSL alert number 40

我不需要代理以任何方式验证/处理与传入请求一起出现的客户端证书(我为此设置了安全:false),而只是将其转发到目标端点。我们看到从客户端收到的证书没有被传递/代理/转发到目标端点,因此基于证书的身份验证在目标端点上失败。

客户端请求直接发送到目标端点时有效,但通过 http-proxy-middleware 代理发送时无效。

我的测试服务器,客户端代码如下供参考。

是否有某种方法可以配置 http-proxy-middleware 以便它将从客户端接收到的客户端证书转发/代理到目标端点,以便客户端发送的客户端证书可用于在目标 REST 端点上进行基于证书的验证?

您能否指导我如何使用 http-proxy-middleware 包或任何其他合适的方式来做到这一点?提前致谢。

服务器代码

// Certificate based HTTPS Server

var authOptions = {
    key: fs.readFileSync('./certs/server-key.pem'),
    cert: fs.readFileSync('./certs/server-crt.pem'),
    ca: fs.readFileSync('./certs/ca-crt.pem'),
    requestCert: true,
    rejectUnauthorized: true
};  

var authApp = express();
authApp.get('/auth', function (req, res) {
    res.send("data from auth");
});

var authServer = https.createServer(authOptions, authApp);
authServer.listen(3002);


// HTTP Proxy Middleware

var authProxyConfig = proxy({
    target: 'https://localhost:3002/auth',
    pathRewrite: {
        '^/auth': '' // rewrite path
    },
    changeOrigin: true,
    logLevel: 'debug',
    secure: false,
    onProxyReq: (proxyReq, req, res) => {
        // Incoming request ( req ) : Not able to see the certificate that was passed by client.
        // Refer the following client code for the same
    },
    onError: (err, req, res) => {
         res.end(`Proxy error: ${err}.`);
    }
});

proxyApp.use('/auth', authProxyConfig);

var unAuthOptions = {
    key: fs.readFileSync('./certs/server-key.pem'),
    cert: fs.readFileSync('./certs/server-crt.pem'),
    ca: fs.readFileSync('./certs/ca-crt.pem'),
    requestCert: false,
    rejectUnauthorized: false
};

var proxyServer = https.createServer(unAuthOptions, proxyApp);
proxyServer.listen(3000);

客户端代码

var fs = require('fs');
var https = require('https');

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var options = {
    hostname: 'localhost',
    port: 3000,
    path: '/auth',
    method: 'GET',
    key: fs.readFileSync('./certs/client1-key.pem'),
    cert: fs.readFileSync('./certs/client1-crt.pem'),
    ca: fs.readFileSync('./certs/ca-crt.pem')
};

var req = https.request(options, function (res) {
    res.on('data', function (data) {
        process.stdout.write(data);
    });
});
req.end();

【问题讨论】:

  • 您是否尝试通过 httpagent 从客户端传递证书?这行得通吗?
  • 感谢分享提示。是的,我尝试通过 HTTP 代理传递证书,但它不起作用,客户端收到与发布的相同的错误。
  • @Neeraj 你有没有找到解决问题的方法?我遇到了同样的问题。

标签: node.js ssl x509 client-certificates http-proxy-middleware


【解决方案1】:

你具体说的是// Incoming request ( req ) : Not able to see the certificate that was passed by client.,所以也许你已经看过getPeerCertificate,发现连接关闭了。

也就是说,在onProxyReq 处理程序中,您可以尝试使用getPeerCertificate 方法(docs)将证书从req 添加到proxyReq

SO answer + comment 展示了如何获取并转换为有效证书。

const parseReqCert = (req) => {
   const { socket } = req; 
   const prefix = '-----BEGIN CERTIFICATE-----'; 
   const postfix = '-----END CERTIFICATE-----'; 
   const pemText = socket.getPeerCertificate(true).raw.toString('base64').match(/.{0,64}/g);

   return [prefix, pemText, postfix].join("\n");
}

const addClientCert = (proxyReq, req) => {
   proxyReq.cert = parseReqCert(req);
   return proxyReq;
}

const authProxyConfig = proxy({
    ...
    onProxyReq: (proxyReq, req, res) => {
       addClientCert(proxyReq, req);
    },
    ...
})

【讨论】:

  • 后人注意:我在没有时间独立验证它是否有效的情况下授予了这个答案的赏金。那时我已经从这个问题中走了出来。但是 nrako 付出的努力和回答似乎提供了信息,所以由于他是唯一一个这样做的人,所以默认情况下赏金给他。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 2013-10-07
  • 2019-11-06
  • 2022-06-23
  • 2018-02-12
  • 2012-09-01
相关资源
最近更新 更多