Skip to content

Commit bea2089

Browse files
authored
Merge pull request #3251 from Cookiezaurs/master
[SER-153] [SER-147] Redirect functionality moved to core. Data migration fix.
2 parents 5a01bfe + f332d34 commit bea2089

File tree

3 files changed

+117
-90
lines changed

3 files changed

+117
-90
lines changed

api/utils/requestProcessor.js

+116-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const validateUserForDataReadAPI = validateRead;
2222
const validateUserForDataWriteAPI = validateUserForWrite;
2323
const validateUserForGlobalAdmin = validateGlobalAdmin;
2424
const validateUserForMgmtReadAPI = validateUser;
25+
const request = require('request');
2526

2627
var loaded_configs_time = 0;
2728

@@ -2778,6 +2779,82 @@ const checksumSaltVerification = (params) => {
27782779
};
27792780

27802781

2782+
//Function check if there is app redirect set
2783+
//In that case redirect data and sets up params to know that request is getting redirected
2784+
/**
2785+
* @param {object} ob - params object
2786+
* @returns {Boolean} - false if redirected
2787+
*/
2788+
function validateRedirect(ob) {
2789+
var params = ob.params,
2790+
app = ob.app;
2791+
if (!params.cancelRequest && app.redirect_url && app.redirect_url !== '') {
2792+
var newPath = params.urlParts.path;
2793+
2794+
//check if we have query part
2795+
if (newPath.indexOf('?') === -1) {
2796+
newPath += "?";
2797+
}
2798+
2799+
var opts = {
2800+
uri: app.redirect_url + newPath + '&ip_address=' + params.ip_address,
2801+
method: 'GET'
2802+
};
2803+
2804+
//should we send post request
2805+
if (params.req.method.toLowerCase() === 'post') {
2806+
opts.method = "POST";
2807+
//check if we have body from post method
2808+
if (params.req.body) {
2809+
opts.json = true;
2810+
opts.body = params.req.body;
2811+
}
2812+
}
2813+
2814+
request(opts, function(error, response, body) {
2815+
var code = 400;
2816+
var message = "Redirect error. Tried to redirect to:" + app.redirect_url;
2817+
2818+
if (response && response.statusCode) {
2819+
code = response.statusCode;
2820+
}
2821+
2822+
2823+
if (response && response.body) {
2824+
try {
2825+
var resp = JSON.parse(response.body);
2826+
message = resp.result || resp;
2827+
}
2828+
catch (e) {
2829+
if (response.result) {
2830+
message = response.result;
2831+
}
2832+
else {
2833+
message = response.body;
2834+
}
2835+
}
2836+
}
2837+
if (error) { //error
2838+
log.e("Redirect error", error, body, opts, app, params);
2839+
}
2840+
2841+
if (plugins.getConfig("api", params.app && params.app.plugins, true).safe) {
2842+
common.returnMessage(params, code, message);
2843+
}
2844+
});
2845+
params.cancelRequest = "Redirected: " + app.redirect_url;
2846+
params.waitForResponse = false;
2847+
if (plugins.getConfig("api", params.app && params.app.plugins, true).safe) {
2848+
params.waitForResponse = true;
2849+
}
2850+
return false;
2851+
}
2852+
else {
2853+
return true;
2854+
}
2855+
}
2856+
2857+
27812858
/**
27822859
* Validate App for Write API
27832860
* Checks app_key from the http request against "apps" collection.
@@ -2870,37 +2947,53 @@ const validateAppForWriteAPI = (params, done, try_times) => {
28702947
console.log('Parse metrics JSON failed', params.qstring.metrics, params.req.url, params.req.body);
28712948
}
28722949
}
2873-
28742950
plugins.dispatch("/sdk/pre", {
28752951
params: params,
28762952
app: app
28772953
}, () => {
2878-
plugins.dispatch("/sdk", {
2879-
params: params,
2880-
app: app
2881-
}, () => {
2954+
var processMe = validateRedirect({params: params, app: app});
2955+
/*
2956+
Keeping option open to add some request cancelation on /sdk for different cases than redirect.
2957+
(That is why duplicate code)
2958+
*/
2959+
if (!processMe) {
28822960
plugins.dispatch("/sdk/log", {params: params});
2883-
if (!params.cancelRequest) {
2884-
processUser(params, validateAppForWriteAPI, done, try_times).then((userErr) => {
2885-
if (userErr) {
2886-
if (!params.res.finished) {
2887-
common.returnMessage(params, 400, userErr);
2961+
//params.cancelRequest is true
2962+
if (!params.res.finished && !params.waitForResponse) {
2963+
common.returnOutput(params, {result: 'Success', info: 'Request ignored: ' + params.cancelRequest});
2964+
//common.returnMessage(params, 200, 'Request ignored: ' + params.cancelRequest);
2965+
}
2966+
common.log("request").i('Request ignored: ' + params.cancelRequest, params.req.url, params.req.body);
2967+
return done ? done() : false;
2968+
}
2969+
else {
2970+
plugins.dispatch("/sdk", {
2971+
params: params,
2972+
app: app
2973+
}, () => {
2974+
plugins.dispatch("/sdk/log", {params: params});
2975+
if (!params.cancelRequest) {
2976+
processUser(params, validateAppForWriteAPI, done, try_times).then((userErr) => {
2977+
if (userErr) {
2978+
if (!params.res.finished) {
2979+
common.returnMessage(params, 400, userErr);
2980+
}
28882981
}
2982+
else {
2983+
processRequestData(params, app, done);
2984+
}
2985+
});
2986+
}
2987+
else {
2988+
if (!params.res.finished && !params.waitForResponse) {
2989+
common.returnOutput(params, {result: 'Success', info: 'Request ignored: ' + params.cancelRequest});
2990+
//common.returnMessage(params, 200, 'Request ignored: ' + params.cancelRequest);
28892991
}
2890-
else {
2891-
processRequestData(params, app, done);
2892-
}
2893-
});
2894-
}
2895-
else {
2896-
if (!params.res.finished && !params.waitForResponse) {
2897-
common.returnOutput(params, {result: 'Success', info: 'Request ignored: ' + params.cancelRequest});
2898-
//common.returnMessage(params, 200, 'Request ignored: ' + params.cancelRequest);
2992+
common.log("request").i('Request ignored: ' + params.cancelRequest, params.req.url, params.req.body);
2993+
return done ? done() : false;
28992994
}
2900-
common.log("request").i('Request ignored: ' + params.cancelRequest, params.req.url, params.req.body);
2901-
return done ? done() : false;
2902-
}
2903-
});
2995+
});
2996+
}
29042997
});
29052998
});
29062999
});

plugins/data_migration/api/api.js

-66
Original file line numberDiff line numberDiff line change
@@ -955,72 +955,6 @@ function trim_ending_slashes(address) {
955955
return true;
956956
});
957957

958-
//for redirect
959-
plugins.register("/sdk", function(ob) {
960-
var params = ob.params,
961-
app = ob.app;
962-
if (!params.cancelRequest && app.redirect_url && app.redirect_url !== '') {
963-
var newPath = params.urlParts.path;
964-
965-
//check if we have query part
966-
if (newPath.indexOf('?') === -1) {
967-
newPath += "?";
968-
}
969-
970-
var opts = {
971-
uri: app.redirect_url + newPath + '&ip_address=' + params.ip_address,
972-
method: 'GET'
973-
};
974-
975-
//should we send post request
976-
if (params.req.method.toLowerCase() === 'post') {
977-
opts.method = "POST";
978-
//check if we have body from post method
979-
if (params.req.body) {
980-
opts.json = true;
981-
opts.body = params.req.body;
982-
}
983-
}
984-
985-
request(opts, function(error, response, body) {
986-
987-
var code = 400;
988-
var message = "Redirect error. Tried to redirect to:" + app.redirect_url;
989-
990-
if (response && response.statusCode) {
991-
code = response.statusCode;
992-
}
993-
994-
995-
if (response && response.body) {
996-
try {
997-
var resp = JSON.parse(response.body);
998-
message = resp.result || resp;
999-
}
1000-
catch (e) {
1001-
if (response.result) {
1002-
message = response.result;
1003-
}
1004-
else {
1005-
message = response.body;
1006-
}
1007-
}
1008-
}
1009-
if (error) { //error
1010-
console.log("Redirect error", error, body, opts, app, params);
1011-
}
1012-
1013-
1014-
if (plugins.getConfig("api", params.app && params.app.plugins, true).safe) {
1015-
common.returnMessage(params, code, message);
1016-
}
1017-
});
1018-
params.cancelRequest = "Redirected: " + app.redirect_url;
1019-
params.waitForResponse = true;
1020-
return false;
1021-
}
1022-
return false;
1023-
});
1024958
}(pluginOb));
1025959

1026960
module.exports = pluginOb;

plugins/data_migration/frontend/public/templates/drawer-export.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</span>
4040
<cly-tooltip-icon icon="ion-help-circled"></cly-tooltip-icon>
4141
</div>
42-
<validation-provider name="target_path" rules="required" v-slot="v">
42+
<validation-provider name="target_path" rules="" v-slot="v">
4343
<el-input
4444
:placeholder="i18n('data-migration.enter-your-export-folder')"
4545
v-model="drawerScope.editedObject.target_path">

0 commit comments

Comments
 (0)