Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mitigate federate bug - overhead in schema computation phase on /siren endpoint #10

Merged
merged 7 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ server.route({
console.log('doing some aditional stuff before redirecting');
callback(null, 'https://some.upstream.service.com/');
},
onResponse: function (err, res, request, reply, settings, ttl, data) {
onResponse: function (err, res, request, reply, settings, ttl) {

console.log('receiving the response from the upstream.');
Wreck.read(res, { json: true }, function (err, payload) {
Expand All @@ -191,3 +191,11 @@ server.route({
});

```

### Tagging new release

Before tagging make sure the version in package.json was bumped and commited

```
git tag -a 5.4.0-kibi-5 -m 'version 5.4.0-kibi-5' && git push origin 5.4.0-kibi-5
```
103 changes: 90 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Hoek = require('hoek');
const Joi = require('joi');
const Wreck = require('wreck');
const Boom = require('boom');
const URL = require('url')
const URL = require('url');


// Declare internals
Expand Down Expand Up @@ -157,10 +157,11 @@ internals.handler = function (route, handlerOptions) {

// Encoding request path
const _encodeURL = (url) => {
const uri = URL.parse(url);
if (uri.pathname && decodeURI(uri.pathname) === uri.pathname) {
uri.pathname = encodeURI(uri.pathname);
return URL.format(uri);

const tempUri = URL.parse(url);
if (tempUri.pathname && decodeURI(tempUri.pathname) === tempUri.pathname) {
tempUri.pathname = encodeURI(tempUri.pathname);
return URL.format(tempUri);
}
return url;
};
Expand Down Expand Up @@ -205,16 +206,92 @@ internals.handler = function (route, handlerOptions) {
if (onBeforeSendRequest) {
onBeforeSendRequest(request)
.then((requestUpdates) => {
Hoek.merge(options, requestUpdates);
_sendRequest();

Hoek.merge(options, requestUpdates);

// Note:
// Below change is to mitigate federate bug - unnecessary overhead in schema computation phase
// on indices with many shards
//
// We do NOT send requests without joins to /siren endpoint
if (requestUpdates.payload) {
const payload = requestUpdates.payload.toString();
const myUri = URL.parse(uri);
let containJoin = false;
let isSearchOrMsearch = false;

const reviver = function (key, value) {

if (key === 'join' && value.on !== undefined) {
containJoin = true;
}
return value;
};

if (myUri.pathname && myUri.pathname.indexOf('/_msearch') !== -1) {
isSearchOrMsearch = true;
const lines = payload.split('\n');
for (let i = 1; i < lines.length - 1; i = i + 2 ) {
if (containJoin) {
continue;
}
if (lines[i] !== '') {
try {
JSON.parse(lines[i], reviver);
} catch (e) {
console.log('Error parsing _msearch request payload [' + lines[i] + ']', e);
}
}
}
} else if (myUri.pathname && myUri.pathname.indexOf('/_search') !== -1){
isSearchOrMsearch = true;
if (payload !== '') {
try {
JSON.parse(payload, reviver);
} catch (e) {
console.log('Error parsing _search request payload [' + payload + ']', e);
}
}
}

if (isSearchOrMsearch && !containJoin) {
const pathWithNoSiren = myUri.pathname.replace(/^\/siren\//, '/');
myUri.pathname = pathWithNoSiren;

let search = myUri.search;
if (search && search.indexOf('preference')) {
if (search.startsWith('?')) {
search = search.substring(1);
}
const pairs = search.split('&');
for (let i = pairs.length - 1; i >= 0; i = i - 1) {
if (pairs[i].startsWith('preference=')) {
pairs.splice(i, 1);
}
}
search = pairs.join('&');
myUri.search = search;
}

try {
uri = URL.format(myUri);
} catch (e) {
console.log('Error when formatting the URL', e);
}
}
}
// end of mitigation code

_sendRequest();
})
.catch((err) => {
let msg = 'Failed request';
if (err.message) {
msg = err.message;
}
return _onError(Boom.badRequest(msg, err));
});

let msg = 'Failed request';
if (err.message) {
msg = err.message;
}
return _onError(Boom.badRequest(msg, err));
});
} else {
_sendRequest();
}
Expand Down
Loading