Compare commits

...

4 Commits

Author SHA1 Message Date
Vicent Llopis 98d2029582 arreglso 2023-02-02 14:57:50 +01:00
Vicent Llopis 617b44d90e arreglos 2023-02-02 08:15:10 +01:00
Vicent Llopis 4f6636b51c algunos errores corregidos 2023-02-01 15:16:15 +01:00
Vicent Llopis 79faeed8b3 test 2023-01-31 14:59:57 +01:00
10 changed files with 36239 additions and 322 deletions

38
.eslintrc.yml Normal file
View File

@ -0,0 +1,38 @@
extends: [eslint:recommended, google, plugin:jasmine/recommended]
parserOptions:
ecmaVersion: 2018
sourceType: "module"
plugins:
- jasmine
env:
jasmine: true
rules:
require-jsdoc: 0
no-undef: 0
max-len: ["error", {code: 120}]
eqeqeq: 0
operator-linebreak: 0
radix: 0
guard-for-in: 0
camelcase: 0
default-case: 0
no-eq-null: 0
no-console: ["error"]
no-warning-comments: 0
no-empty: [error, allowEmptyCatch: true]
complexity: 0
max-depth: 0
comma-dangle: 0
bracketSpacing: 0
space-infix-ops: 1
no-invalid-this: 0
space-before-function-paren: [error, never]
prefer-const: 0
curly: [error, multi-or-nest]
indent: [error, 4]
arrow-parens: [error, as-needed]
no-multiple-empty-lines: ["error", { "max": 1, "maxEOF": 1 }]
space-in-parens: ["error", "never"]
jasmine/no-focused-tests: 0
jasmine/prefer-toHaveBeenCalledWith: 0
arrow-spacing: ["error", { "before": true, "after": true }]

View File

@ -1,5 +1,5 @@
{
"grafanaUrl": "https://grafana.example.com",
"user": "user",
"password": "1234"
"grafanaUrl": "https://grafana.verdnatura.es",
"user": "vicent",
"password": "llopis.19263"
}

18
.vscode/launch.json vendored
View File

@ -1,7 +1,17 @@
{
// Use IntelliSense para saber los atributos posibles.
// Mantenga el puntero para ver las descripciones de los existentes atributos.
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"restart": true,
"timeout": 50000
}, {
"type": "node",
"request": "attach",
"name": "Attach by process ID",
"processId": "${command:PickProcess}"
}
]
}

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
// Coloque su configuración en este archivo para sobrescribir la configuración predeterminada y de usuario.
{
// Carácter predeterminado de final de línea.
"files.eol": "\n",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}

View File

@ -1,277 +0,0 @@
#!/usr/bin/env node
const packageJson = require('./package.json');
const fetch = require('node-fetch');
const colors = require('colors');
const os = require('os');
const fs = require('fs');
const getopts = require('getopts');
const error = `[ERROR]: `.bold;
console.log(
`Grafana-Find (${packageJson.description})`.yellow.bold,
`v${packageJson.version}`.cyan.bold
);
const usage = {
description: 'Utility to find strings in dashboards',
params: {
user: 'Grafana username',
version: 'Display the version number and exit',
help: 'Display this help message'
}
};
const opts = getopts(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h'
},
boolean: [
'version',
'help'
]
});
if (opts.version) {
process.exit();
}
if (opts.help) {
console.log(`Usage:`.gray, `grafana-find`, `<string to search>`.magenta);
process.exit();
}
if (!opts._[0]) {
console.error(`${error}The string to search for is missing`.red);
process.exit(1);
}
if (opts._.length > 1) {
console.error(`${error}This command doesn't allow more parameters`.red);
process.exit(1);
}
let config;
const configPaths = [
os.homedir(),
`${__dirname}`
];
for (const configPath of configPaths) {
const configFile = `${configPath}/.grafana-find.json`;
if (fs.existsSync(configFile)) {
config = require(configFile);
break;
}
}
if (!config) {
console.error(`${error}Configuration file not found, search paths: .grafana-find.json: ${configPaths.join(':')}\n`.red);
process.exit(1);
}
const findAll = opts._[0];
const grafanaUrl = config.grafanaUrl;
let user = config.user;
let passw = config.password;
const grafanaApi = `${grafanaUrl}/api`;
const urlOrganizations = `${grafanaUrl}/api/orgs`;
const urlDashboards = `${grafanaApi}/search?orgId=`;
const urlUID = `${grafanaApi}/dashboards/uid/`;
let numberOfDashboards = 0;
let totalDashboards = 0;
let numberOfPanels = 0;
let numberOfVariables = 0;
let numberOfObjects = 0;
let titlePanels = new Array;
let nameVariables = new Array;
const regexRawSQL = new RegExp(findAll, 'i');
async function main(){
if (!user) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const answer = await new Promise(resolve => {
rl.question(colors.green('Enter your user: '), resolve);
});
user = `${answer}`;
if (!answer) {
console.error(`\n${error}You need to put a user\n`.red);
process.exit(0);
}
rl.close();
}
if (!passw) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.stdoutMuted = true;
const answer = await new Promise(resolve => {
rl.question(colors.green('Enter your password: '), resolve);
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
});
passw = `${answer}`;
if (!answer) {
console.error(`\n${error}You need to put a password\n`.red);
process.exit(0);
}
rl.close();
}
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
try {
var response = await fetch(urlOrganizations, {
method: "GET",
headers: {
Authorization: credentials
}
});
} catch (notfound) {
console.error(`${error}Server '${config.grafanaUrl}' not found`.red);
process.exit(1);
};
let AllOrganizations = await response.json();
if (AllOrganizations.message==='invalid username or password') {
console.error(`\n${error}Invalid username or password\n`.red);
process.exit(1);
}
console.clear();
console.log(
`--------- Grafana-Find (${packageJson.description})`.yellow.bold,
`v${packageJson.version}`.cyan.bold,
`--------`.yellow.bold,
);
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
for (let x in AllOrganizations) {
console.log(colors.gray.bold(`(Organization: ${AllOrganizations[x].name})\n`));
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
method: "GET",
headers: {
Authorization: credentials
},
redirect: 'manual'
});
if (response.status === 302) {
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
method: 'GET',
headers: {
Accept: 'application/json'
},
redirect: 'manual'
});
}
let allUID = await response.json();
for (let i in allUID) {
let url = `${urlUID}${allUID[i].uid}`;
response = await fetch(url, {
method: "GET",
headers: {
Authorization: credentials,
},
redirect: 'manual'
});
if (response.status === 404) {
response = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json'
},
redirect: 'manual'
});
}
let data = await response.json();
let isFound = false;
let isFoundSomething = false;
const dashboard = data.dashboard;
if (dashboard) {
if (dashboard.panels)
for (const panel of dashboard.panels) {
if (panel.targets)
for (const target of panel.targets) {
isFound = regexRawSQL.test(target.rawSql);
if (isFound) {
if (panel.title)
titlePanels.push(panel.title);
else
titlePanels.push(`null`);
numberOfPanels++;
isFoundSomething=true;
}
}
}
if (dashboard.templating)
for (const list of dashboard.templating.list) {
isFound = regexRawSQL.test(list.query);
if (isFound) {
nameVariables.push(list.name)
numberOfVariables++;
isFoundSomething=true;
}
}
}
if (isFoundSomething) {
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}?orgId=${AllOrganizations[x].id}`;
console.log((linkUrl).yellow.underline, dashboard.title);
if (numberOfPanels) {
console.log(colors.cyan.bold(`> ${numberOfPanels} panels`));
console.log(colors.cyan(titlePanels.toString().split(",")));
}
if (numberOfVariables) {
console.log(colors.magenta.bold(`> ${numberOfVariables} variables`));
console.log(colors.magenta(nameVariables.toString().split(",")));
}
console.log('')
numberOfDashboards++;
}
titlePanels = [];
nameVariables= [];
numberOfObjects = numberOfPanels + numberOfVariables + numberOfObjects;
numberOfPanels = 0;
numberOfVariables = 0;
}
totalDashboards = numberOfDashboards + totalDashboards;
if (!numberOfDashboards)
console.log(`No results found\n`.green);
numberOfDashboards = 0;
};
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${totalDashboards} dashboards -------\n`));
if (!response) {
console.log(`${error}The server don't exists`);
process.exit(1);
}
process.exit();
}
main();

214
main.js
View File

@ -6,6 +6,31 @@ const colors = require('colors');
const os = require('os');
const fs = require('fs');
const getopts = require('getopts');
const mysql = require('mysql2/promise');
const conGrafana = mysql.createPool({
host: "dev-db.verdnatura.es",
user: "grafanaPermissions",
password: "grafana",
port : 3307,
queryTimeout: 100
});
const conVicent = mysql.createPool({
host: "dev-db.verdnatura.es",
user: "vicent",
password: "llopis.19263",
port : 3307
});
// con.connect(function(err) {
// if (err) {
// return console.error('error: ' + err.message);
// }
// console.log('Connected to the MySQL server.');
// });
const error = `[ERROR]: `.bold;
@ -14,15 +39,6 @@ console.log(
`v${packageJson.version}`.cyan.bold
);
const usage = {
description: 'Utility to find strings in dashboards',
params: {
user: 'Grafana username',
version: 'Display the version number and exit',
help: 'Display this help message'
}
};
const opts = getopts(process.argv.slice(2), {
alias: {
version: 'v',
@ -32,7 +48,7 @@ const opts = getopts(process.argv.slice(2), {
'version',
'help'
]
});
});
if (opts.version) {
process.exit();
@ -144,7 +160,7 @@ async function main(){
} catch (notfound) {
console.error(`${error}Server '${config.grafanaUrl}' not found`.red);
process.exit(1);
};
}
let AllOrganizations = await response.json();
if (AllOrganizations.message==='invalid username or password') {
@ -154,7 +170,7 @@ async function main(){
console.clear();
console.log(
`───────── Grafana-Find (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold
`───────── Grafana-Find LOCAL (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold
);
console.log(colors.green.bold(`──────────────────── Starting process ────────────────────\n`));
@ -212,28 +228,23 @@ async function main(){
for (const panel of dashboard.panels) {
if (panel.targets)
for (const target of panel.targets) {
isFound = regexRawSQL.test(target.rawSql);
if (isFound) {
if (panel.title)
if (panel.title==' ')
titlePanels.push(`(null)`.italic);
else
titlePanels.push(panel.title);
else
titlePanels.push(`(undefined)`.italic);
numberOfPanels++;
isFoundSomething=true;
}
const firstWordAfterFrom = getFirstWordAfterFrom(target.rawSql);
if(firstWordAfterFrom == 'ost_ticket' || firstWordAfterFrom == 'osticket'|| firstWordAfterFrom == 'redmine') continue;
const sql = replaceVariables(target.rawSql);
await queryAndGrant(sql);
}
}
if (dashboard.templating)
for (const list of dashboard.templating.list) {
isFound = regexRawSQL.test(list.query);
if (isFound) {
nameVariables.push(list.name)
numberOfVariables++;
isFoundSomething=true;
if (dashboard.templating){
for (const list of dashboard.templating.list) {
const firstWord = getFirstWord(list.query);
if (firstWord != 'SELECT' || firstWord != 'WITH') continue;
const sql = replaceVariables(list.query);
await queryAndGrant(sql);
}
}
}
@ -275,7 +286,7 @@ async function main(){
if (!numberOfDashboards)
console.log(`No results found\n`.green);
numberOfDashboards = 0;
};
}
console.log(colors.green.bold(`──────── Have been found ${numberOfObjects} results in ${totalDashboards} dashboards ────────\n`));
@ -287,4 +298,141 @@ async function main(){
process.exit();
}
main();
main();
function getFirstWord(text) {
const words = text.split(" ");
if (words.length > 0) {
return words[0];
}
return null;
}
function getLastWord(text) {
const words = text.split(" ");
if (words.length > 0) {
return words[words.length - 1];
}
return null;
}
function getFirstWordBetweenQuotes(text) {
const pattern = /'([^']+)'/;
const match = text.match(pattern);
if (match) {
return match[1];
}
return null;
}
function replaceVariables(text){
let sql = text;
sql = sql.replaceAll('$__timeGroupAlias', 'util.mock2');
sql = sql.replaceAll('$__timeGroup', 'util.mock2');
sql = sql.replaceAll('$__timeFilter', 'util.mock');
sql = sql.replaceAll('$__timeFrom()', `'2022-01-01'`);
sql = sql.replaceAll('$__timeTo()', `'2022-01-01'`);
sql = sql.replaceAll('${__from}', `'2022-01-01'`);
sql = sql.replaceAll('${__to}', `'2022-01-01'`);
sql = sql.replaceAll(/\$\w+/g, 1);
return sql;
}
function getFirstWordAfterFrom(text) {
const pattern = /FROM\s+(\w+)/;
const match = text.match(pattern);
if (match) {
return match[1];
}
return null;
}
function addVn(query) {
return query.replace(/(FROM|JOIN)\s+(?!\w+\.)(\w+)/g, '$1 vn.$2');
}
function addVnInFunctions(str){
let words = str.split(" ");
for (let i = 0; i < words.length; i++) {
if (words[i].startsWith("firstDayOfWeek") || words[i].startsWith("lastDayOfWeek")) {
words[i] = "vn." + words[i];
}
}
str = words.join(" ");
return str;
}
function errorFn() {
throw 'hola'
}
async function queryAndGrant(sql){
console.log(sql);
let entry;
try{
console.log('ENTRY ', new Date())
entry = new Date().getTime();
//const timeout = setTimeout(errorFn, 1000)
const value = await conGrafana.query({
sql: sql,
timeout: 1000 // 1 second
});
//clearTimeout(timeout)
console.log('VALUE ', value, ' ', new Date().getTime() - entry, 's')
console.log('STOP ', new Date())
} catch (err) {
if(err.code == 'ER_TABLEACCESS_DENIED_ERROR') {
const table = getLastWord(err.sqlMessage); // p.e: `account`.`user`
const user = getFirstWordBetweenQuotes(err.sqlMessage); // p.e: grafanaPermissions
const grant = `GRANT SELECT ON ${table} TO '${user}'@'%';`;
console.log(grant);
try{
await conVicent.query(grant);
} catch (err){
// console.log(err);
if(err.code != 'ER_NO_SUCH_TABLE') await queryAndGrant(sql);
}
} else if(err.code == 'ER_PROCACCESS_DENIED_ERROR'){
const routine = getLastWord(err.sqlMessage); // p.e: 'util.mock'
const test = routine.replace(/'/g, "");
const user = getFirstWordBetweenQuotes(err.sqlMessage); // p.e: grafanaPermissions
const grant = `GRANT EXECUTE ON FUNCTION ${test} TO '${user}'@'%';`;
console.log(grant);
try{
await conVicent.query(grant);
} catch (err){
// console.log(err);
await queryAndGrant(sql);
}
} else if(err.code == 'ER_NO_DB_ERROR'){
console.log(err);
if (!err.sql.includes("users")) {
sql = addVn(sql);
await queryAndGrant(sql);
}
} else if(err.code == 'ER_SP_DOES_NOT_EXIST'){
console.log(err);
const sqlComplete = addVnInFunctions(sql);
await queryAndGrant(sqlComplete);
} else if(err.code == 'PROTOCOL_SEQUENCE_TIMEOUT'){
console.log('NEXT POR TIMEOUT', new Date().getTime() - entry, 's')
} else {
console.log(err);
throw 'ELSE ' + err;
}
}
}

1195
node_modules/.package-lock.json generated vendored

File diff suppressed because it is too large Load Diff

2049
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,10 @@
"license": "GPL-3.0",
"dependencies": {
"colors": "^1.4.0",
"eslint-config-google": "^0.14.0",
"getopts": "^2.3.0",
"mysql": "^2.18.1",
"mysql2": "^3.1.0",
"node-fetch": "^2.6.7",
"object-hash": "^3.0.0"
},
@ -14,5 +17,8 @@
"grafana-find": "main.js",
"gfind": "main.js"
},
"main": "main.js"
"main": "main.js",
"devDependencies": {
"eslint": "^8.33.0"
}
}

32748
test.txt Normal file

File diff suppressed because it is too large Load Diff