http-proxy-middleware + express + node.js-无法在启用客户端证书认证的情况下重定向到端点

问题描述 投票:3回答:1

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

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

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

代理服务器中收到错误:

[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();
node.js ssl x509 client-certificates http-proxy-middleware
1个回答
1
投票

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

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

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);
    },
    ...
})
© www.soinside.com 2019 - 2024. All rights reserved.