wazuh-kibana-app/public/controllers/dev-tools.js

265 lines
9.7 KiB
JavaScript
Raw Normal View History

/*
2018-05-09 09:49:50 +00:00
* Wazuh app - Dev tools controller
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import * as modules from 'ui/modules'
2018-05-04 16:03:35 +00:00
import beautifier from 'plugins/wazuh/utils/json-beautifier'
import CodeMirror from 'plugins/wazuh/utils/codemirror/lib/codemirror'
2018-05-09 11:45:21 +00:00
import jsonLint from 'plugins/wazuh/utils/codemirror/json-lint.js'
2018-05-04 16:03:35 +00:00
const app = modules.get('app/wazuh', []);
// Logs controller
2018-05-08 15:34:33 +00:00
app.controller('devToolsController', function($scope, $rootScope, errorHandler, apiReq, $window, appState) {
let groups = [];
2018-05-08 16:06:35 +00:00
const apiInputBox = CodeMirror.fromTextArea(document.getElementById('api_input'),{
2018-05-09 09:49:50 +00:00
lineNumbers : true,
matchBrackets : true,
mode : { name: "javascript", json: true },
theme : 'ttcn',
foldGutter : true,
styleSelectedText: true,
gutters : ["CodeMirror-foldgutter"]
2018-05-08 16:06:35 +00:00
});
const analyzeGroups = () => {
try{
const currentState = apiInputBox.getValue().toString();
appState.setCurrentDevTools(currentState)
2018-05-08 16:06:35 +00:00
const tmpgroups = [];
const splitted = currentState.split(/[\r\n]+(?=(?:GET|PUT|POST|DELETE)\b)/gm)
let start = 0;
let end = 0;
const slen = splitted.length;
for(let i=0; i < slen; i++){
2018-05-10 13:37:13 +00:00
let tmp = splitted[i].split('\n');
if(Array.isArray(tmp)) tmp = tmp.filter(item => !item.includes('#'))
const cursor = apiInputBox.getSearchCursor(splitted[i],null,{multiline:true})
if(cursor.findNext()) start = cursor.from().line
2018-05-09 16:12:18 +00:00
else return [];
end = start + tmp.length;
2018-05-08 16:06:35 +00:00
const tmpRequestText = tmp[0];
let tmpRequestTextJson = '';
const tmplen = tmp.length;
for(let j = 1; j < tmplen; ++j){
2018-05-10 13:37:13 +00:00
if(!!tmp[j] && !tmp[j].includes('#')){
tmpRequestTextJson += tmp[j];
2018-05-08 16:06:35 +00:00
}
}
2018-05-09 09:49:50 +00:00
if(tmpRequestTextJson && typeof tmpRequestTextJson === 'string'){
let rtjlen = tmp.length;
while(rtjlen--){
2018-05-10 09:48:59 +00:00
if(tmp[rtjlen].trim() === '}') break;
else end -= 1;
}
}
if(!tmpRequestTextJson && tmp.length > 1) {
tmp = [tmp[0]]
end = start + 1
}
if(i === slen-1 && !tmpRequestTextJson){
if(tmp.length > 1) end -= (tmp.length - 1)
}
end--;
2018-05-08 16:06:35 +00:00
tmpgroups.push({
requestText : tmpRequestText,
requestTextJson: tmpRequestTextJson,
start,
end
})
2018-05-04 16:16:01 +00:00
}
2018-05-08 16:06:35 +00:00
return tmpgroups;
} catch(error){
2018-05-09 16:12:18 +00:00
return [];
2018-05-04 16:03:35 +00:00
}
}
apiInputBox.on('change',() => {
groups = analyzeGroups();
const currentState = apiInputBox.getValue().toString();
appState.setCurrentDevTools(currentState)
const currentGroup = calculateWhichGroup();
if(currentGroup){
const hasWidget = widgets.filter(item => item.start === currentGroup.start)
if(hasWidget.length) apiInputBox.removeLineWidget(hasWidget[0].widget)
2018-05-09 16:12:18 +00:00
setTimeout(() => checkJsonParseError(),450)
}
2018-05-09 09:49:50 +00:00
})
2018-05-09 11:45:21 +00:00
let linesWithClass = [], widgets = [];
2018-05-09 09:49:50 +00:00
const highlightGroup = group => {
for(const line of linesWithClass){
apiInputBox.removeLineClass(line,'background',"CodeMirror-styled-background")
}
linesWithClass = [];
if(group) {
if(!group.requestTextJson) {
linesWithClass.push(apiInputBox.addLineClass(group.start,'background',"CodeMirror-styled-background"))
return;
}
for(let i=group.start; i<=group.end; i++){
linesWithClass.push(apiInputBox.addLineClass(i,'background',"CodeMirror-styled-background"))
}
}
}
const checkJsonParseError = () => {
const affectedGroups = [];
2018-05-09 11:45:21 +00:00
for(const widget of widgets){
apiInputBox.removeLineWidget(widget.widget)
2018-05-09 11:45:21 +00:00
}
widgets = [];
for(const item of groups){
if(item.requestTextJson){
try {
jsonLint.parse(item.requestTextJson)
} catch(error) {
affectedGroups.push(item.requestText);
2018-05-09 16:12:18 +00:00
const msg = document.createElement("div");
msg.id = new Date().getTime()/1000;
const icon = msg.appendChild(document.createElement("div"));
2018-05-09 11:45:21 +00:00
icon.className = "lint-error-icon";
2018-05-09 16:12:18 +00:00
icon.id = new Date().getTime()/1000;
icon.onmouseover = () => {
const advice = msg.appendChild(document.createElement("span"));
advice.id = new Date().getTime()/1000;
advice.innerText = error.message || 'Error parsing query'
advice.className = 'lint-block-wz'
2018-05-09 11:45:21 +00:00
}
2018-05-09 16:12:18 +00:00
icon.onmouseleave = () => {
2018-05-09 11:45:21 +00:00
msg.removeChild(msg.lastChild)
}
2018-05-09 16:12:18 +00:00
widgets.push({start:item.start,widget:apiInputBox.addLineWidget(item.start, msg, {coverGutter: false, noHScroll: true})});
2018-05-09 11:45:21 +00:00
}
}
}
return affectedGroups;
2018-05-09 09:49:50 +00:00
}
apiInputBox.on('cursorActivity',() => {
const currentGroup = calculateWhichGroup();
highlightGroup(currentGroup);
})
const init = () => {
apiInputBox.setSize('auto','100%')
apiOutputBox.setSize('auto','100%')
const currentState = appState.getCurrentDevTools();
if(!currentState){
2018-05-10 13:37:13 +00:00
const demoStr = 'GET /\n\n# Comment here\nGET /agents\n' + JSON.stringify({limit:1},null,2);
appState.setCurrentDevTools(demoStr);
apiInputBox.getDoc().setValue(demoStr);
} else {
apiInputBox.getDoc().setValue(currentState)
}
2018-05-08 16:06:35 +00:00
groups = analyzeGroups();
2018-05-09 09:49:50 +00:00
const currentGroup = calculateWhichGroup();
highlightGroup(currentGroup);
}
const apiOutputBox = CodeMirror.fromTextArea(document.getElementById('api_output'),{
lineNumbers : true,
matchBrackets : true,
mode : { name: "javascript", json: true },
readOnly : true,
lineWrapping : true,
styleActiveLine: true,
theme : 'ttcn',
foldGutter : true,
gutters : ["CodeMirror-foldgutter"]
});
const calculateWhichGroup = () => {
const selection = apiInputBox.getCursor()
const desiredGroup = groups.filter(item => item.end >= selection.line && item.start <= selection.line);
return desiredGroup ? desiredGroup[0] : null;
}
$scope.send = async (firstTime) => {
try {
groups = analyzeGroups();
const desiredGroup = calculateWhichGroup();
if(!desiredGroup) throw Error('not desired');
2018-05-09 16:12:18 +00:00
const affectedGroups = checkJsonParseError();
const filteredAffectedGroups = affectedGroups.filter(item => item === desiredGroup.requestText);
if(filteredAffectedGroups.length) {apiOutputBox.setValue('Error parsing JSON query'); return;}
const method = desiredGroup.requestText.startsWith('GET') ?
2018-05-04 16:03:35 +00:00
'GET' :
desiredGroup.requestText.startsWith('POST') ?
2018-05-04 16:03:35 +00:00
'POST' :
desiredGroup.requestText.startsWith('PUT') ?
2018-05-04 16:03:35 +00:00
'PUT' :
desiredGroup.requestText.startsWith('DELETE') ?
2018-05-04 16:03:35 +00:00
'DELETE' :
'GET';
const requestCopy = desiredGroup.requestText.includes(method) ?
desiredGroup.requestText.split(method)[1].trim() :
desiredGroup.requestText;
2018-05-04 16:03:35 +00:00
const req = requestCopy ?
requestCopy.startsWith('/') ?
requestCopy :
`/${requestCopy}` :
'/';
2018-05-04 16:03:35 +00:00
let validJSON = true, JSONraw = {};
try {
JSONraw = JSON.parse(desiredGroup.requestTextJson);
2018-05-04 16:03:35 +00:00
} catch(error) {
validJSON = false;
}
const path = req.includes('?') ? req.split('?')[0] : req;
const params = { devTools: true }
if(typeof JSONraw === 'object') JSONraw.devTools = true;
2018-05-04 16:03:35 +00:00
const output = await apiReq.request(method, path, validJSON && !req.includes('?') ? JSONraw : params)
apiOutputBox.setValue(
JSON.stringify(output.data.data,null,2)
)
} catch(error) {
2018-05-08 16:06:35 +00:00
error && error.data ?
apiOutputBox.setValue(JSON.stringify(error.data)) :
apiOutputBox.setValue('Empty')
}
}
2018-05-04 16:03:35 +00:00
2018-05-07 10:07:52 +00:00
$scope.help = () => {
$window.open('https://documentation.wazuh.com/current/user-manual/api/reference.html');
}
init();
$scope.send(true);
});