node.js - http-proxy-middleware + express + node.js - 无法重定向到启用客户端证书身份验证的端点

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

我正在使用 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

我不需要代理以任何方式验证/处理传入请求附带的客户端证书(我为此设置了 secure: 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();

最佳答案

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

也就是说,在 onProxyReq 处理程序中,您可以尝试使用 getPeerCertificate 从 req 将证书添加到 proxyReq 方法 ( 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);
    },
    ...
})

关于node.js - http-proxy-middleware + express + node.js - 无法重定向到启用客户端证书身份验证的端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59875929/

相关文章:

css - 静态 css 不显示在 express jade 中

amazon-web-services - AWS CodeDeploy 未在 Lightsail 基本设置 SSL 上部署

java - Maven:无法找到请求目标的有效证书路径

java - SSLException: Received fatal alert: internal_error (在 tomcat 下抛出,但在桌面上工作正常)

c# - 使用 .NET OpenSSL 包装器签署文件

javascript - 提高 MongoDB 文本搜索性能

javascript - 如何为Azure存储生成帐户SAS token ?

javascript - SequelizeDatabaseError : You need JSON-C for ST_GeomFromGeoJSON when I have JSON-C installed on centos

Spring Security 使用 X.509 身份验证并定期更新授权用户详细信息

java - 如何比较 java.security.cert.X509Certificate 的不同实现