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

RBQL doesn't support print statements or file open/close in UDF files for Python #159

Open
ynain opened this issue Jan 12, 2024 · 0 comments

Comments

@ynain
Copy link

ynain commented Jan 12, 2024

I tried defining a UDF which would allow me to cache queries before running or reference previously cached ones from a file:

import json
import os
def save_query(*args, **kwargs):
    desc = f"{datetime.now()}: {kwargs['desc']}"
    qf = f"{os.getenv('TOOLING_DIR')}/QueryFile"
    print(qf)
    with open(qf, "w") as qryFile:
        cached_queries = json.loads(qryFile.read())
        query_entry = {
            kwargs['key']: {
                "query": f"{args}",
                "description": desc
            }}
        cached_queries.update(query_entry)
        qryFile.write(json.dumps(cached_queries))
    return args
        
custom_query = lambda : eval("<custom query as string>")

def run_query(qry_key):
    with open("f"{os.getenv('TOOLING_DIR')}/QueryFile", "r") as qryFile:
        cached_queries = json.loads(qryFile.read())
        if qry_key not in cached_queries.keys():
            raise KeyError("Undefined query")
        qry = cached_queries[qry_key]
    return qry['query']

This does not appear to work - it either fails with this error, or a similar one. I may be doing this incorrectly, as I assume that the RBQL queries are in a tuple format.

Screenshot 2024-01-11 at 7 21 19 PM

Is this because the extension is just reading from stdout?

async function run_command_and_parse_output(cmd, args) {
    let execution_result = null;
    try {
        execution_result = await command_async_wrapper(cmd, args);
...
    let json_report = execution_result.stdout;
    if (!json_report || execution_result.stderr) {
        let error_msg = execution_result.stderr || 'empty error';
        return {error_type: 'Integration', error_msg: error_msg};
    }
    try {
        return JSON.parse(json_report);
    } catch (e) {
        return {error_type: 'Integration', error_msg: 'Unable to parse JSON report'};
    }
}
...
function command_async_wrapper(cmd, args) {
    return new Promise(function (resolve, reject) {
        let stdout_data = '';
        let stderr_data = '';
        let process = child_process.spawn(cmd, args, {'windowsHide': true});
        process.stdout.on('data', function(data) {
            stdout_data += data.toString();
        });
        process.stderr.on('data', function(data) {
            stderr_data += data.toString();
        });
        process.on('close', function (code) { // Consider replacing 'close' with 'exit'.
            resolve({'exit_code': code, 'stdout': stdout_data, 'stderr': stderr_data});
        });
        process.on('error', function (err) {
            reject(err);
        });
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant