节点j反向代理后面的Kibana网关错误

3htmauhk  于 2023-02-14  发布在  Kibana
关注(0)|答案(2)|浏览(171)

我有一个kibana示例背后的nodejs 16. x aws lambda反向代理。它的工作几乎一切正常,除了“发现”部分,当我添加2个或更多的过滤器为2个多月的时间范围,它给回坏网关错误。
错误的详细信息:

Wrapper@https://mydomain/_dashboards/909221/bundles/core/core.entry.js:6:4249
_createSuperInternal@https://mydomain/_dashboards/909221/bundles/core/core.entry.js:6:3388
...
...

实施:

function proxy(event, context, lambdaCallback) {
    delete(event.headers["accept-encoding"])

    var path = event.path;
    if (event.multiValueQueryStringParameters) {
        path += '?' + deserializeQueryString(event.multiValueQueryStringParameters)
    }

    // Calculate the options for the HTTPS request
    var opts = {
      host: my_es_endpoint,
      path: path,
      method: event.httpMethod,
      service: 'es',
      region: my_region,
      headers: event.headers
    }

    if ((event.httpMethod == 'POST') || (event.httpMethod == 'PUT')) {
        if (event.body) {
            var buff = new Buffer(event.body, "base64")
            var payload = buff.toString('utf-8')
            opts.body = payload
        }
    }

    // Use aws4 to sign the request so we can talk with ElasticSearch directly
    aws4.sign(opts);

    const req = https.request(opts, function (res) {
        var bodyParts = [];
        // We need to read all the incoming data
        res.on('data', (chunk) => {
            bodyParts.push(chunk)
        });
        res.on("end", () => {
            // We re-create the read content
            var body = Buffer.concat(bodyParts).toString()

            // We send back uncompressed data
            delete(res.headers['content-encoding'])
            res.headers['X-Frame-Options'] = 'DENY'
            // res.headers['content-security-policy'] = "default-src 'self'; frame-ancestors 'none'"
            res.headers['X-XSS-Protection'] = '1; mode=block'
            res.headers['X-Content-Type-Options'] = 'nosniff'

            var response = {
                statusCode: res.statusCode,
                body: body,
                headers: res.headers
            }

            lambdaCallback(null, response);
        })
    });

    req.on('error', (e) => {
        console.log(`Error caught when calling ElasticSearch: ${e}`)
    })

    // For POST/PUT request, we send the content of the paylod as well
    if ((event.httpMethod == 'POST') || (event.httpMethod == 'PUT')) {
        if (event.body) {
            var buff = new Buffer(event.body, "base64")
            var payload = buff.toString('utf-8')
            req.write(payload)
        }
    }

    req.end();
    return req;
}

我试过:

  • 请求时超时增加
  • 将lambda内存从128增加到512
  • 增加lambda超时
zphenhs4

zphenhs41#

我认为你可以尝试增加Lambda函数的内存分配,这样可能会解决这个问题。
或者更好,只是消除你所有的头痛.你应该使用亚马逊API网关,而不是反向代理在Lambda函数代理请求Kibana.你也可以增加超时Kibana请求.这将是更有效和可扩展到您的项目和更少的问题.
我试图优化您的代码,您可以检查这是否会改善或纠正您的问题?

const https = require('https');
const aws4 = require('aws4');

function deserializeQueryString(query) {
    const params = new URLSearchParams();
    for (const key of Object.keys(query)) {
        for (const value of query[key]) {
            params.append(key, value);
        }
    }
    return params.toString();
}

exports.handler = async (event, context, lambdaCallback) => {
    const { headers, path, httpMethod, multiValueQueryStringParameters, body } = event;

    // Remove accept-encoding header
    delete headers['accept-encoding'];

    // Append query string parameters to the path
    if (multiValueQueryStringParameters) {
        path += '?' + deserializeQueryString(multiValueQueryStringParameters);
    }

    const opts = {
        host: my_es_endpoint,
        path,
        method: httpMethod,
        service: 'es',
        region: my_region,
        headers
    };

    // Add request body for POST and PUT methods
    if (['POST', 'PUT'].includes(httpMethod) && body) {
        opts.body = Buffer.from(body, 'base64').toString('utf-8');
    }

    // Sign the request using aws4
    aws4.sign(opts);

    return new Promise((resolve, reject) => {
        const req = https.request(opts, res => {
            let body = '';
            res.on('data', chunk => {
                body += chunk;
            });
            res.on('end', () => {
                // Remove content-encoding header
                delete res.headers['content-encoding'];

                // Set security headers
                res.headers['X-Frame-Options'] = 'DENY';
                res.headers['X-XSS-Protection'] = '1; mode=block';
                res.headers['X-Content-Type-Options'] = 'nosniff';

                const response = {
                    statusCode: res.statusCode,
                    body,
                    headers: res.headers
                };
                resolve(response);
            });
        });

        req.on('error', error => {
            console.error(`Error caught when calling ElasticSearch: ${error}`);
            reject(error);
        });

        // Write request body for POST and PUT methods
        if (['POST', 'PUT'].includes(httpMethod) && body) {
            req.write(opts.body);
        }

        req.end();
    }).then(lambdaCallback)
        .catch(error => {
            lambdaCallback(error);
        });
};
6ss1mwsb

6ss1mwsb2#

我不确定错误是否与代理实现有关,但是我发现了一些有帮助的东西,现在我不再遇到这个问题了。
我按照建议减少了Kibana高级设置中discover:sampleSize的值here

相关问题