fleet/frontend/utilities/sql_tools.js

75 lines
2.1 KiB
JavaScript

import sqliteParser from "sqlite-parser";
import { intersection } from "lodash";
import { osqueryTables } from "utilities/osquery_tables";
const platformsByTableDictionary = osqueryTables.reduce(
(dictionary, osqueryTable) => {
dictionary[osqueryTable.name] = osqueryTable.platforms;
return dictionary;
},
{}
);
// The isNode and visit functionality is informed by https://lihautan.com/manipulating-ast-with-javascript/#traversing-an-ast
const _isNode = (node) => {
// TODO: Improve type checking against shape of AST generated by sqliteParser
return typeof node === "object";
};
const _visit = (abstractSyntaxTree, callback) => {
if (abstractSyntaxTree) {
callback(abstractSyntaxTree);
Object.keys(abstractSyntaxTree).forEach((key) => {
const childNode = abstractSyntaxTree[key];
if (Array.isArray(childNode)) {
childNode.forEach((grandchildNode) => _visit(grandchildNode, callback));
} else if (_isNode(childNode)) {
_visit(childNode, callback);
}
});
}
};
export const listCompatiblePlatforms = (tablesList) => {
if (
tablesList[0] === "Invalid query" ||
tablesList[0] === "No tables in query AST"
) {
return tablesList;
}
const compatiblePlatforms = intersection(
...tablesList?.map((tableName) => platformsByTableDictionary[tableName])
);
return compatiblePlatforms.length ? compatiblePlatforms : ["None"];
};
export const parseSqlTables = (sqlString) => {
const tablesList = [];
const _callback = (node) => {
if (node) {
if (node.variant === "recursive") {
throw new Error(
"Invalid usage: `recursive` is not supported by `parseSqlTables`"
);
} else if (node.variant === "table") {
tablesList.push(node.name);
}
}
};
try {
const sqlTree = sqliteParser(sqlString);
_visit(sqlTree, _callback);
return tablesList.length ? tablesList : ["No tables in query AST"];
} catch (err) {
// console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);
return ["Invalid query"];
}
};
export default { listCompatiblePlatforms, parseSqlTables };