JavaScript response filter - Cassandra

The JavaScript request filter is the most flexible of all response filters. It can potentially be called for any packet type, and can modify traffic in any way it sees fit.

Parameters

Example: change a column name in a result set

We can intercept packets of type ResultRows and change them so that a column will show up differently.

The JavaScript response filter will have the parameter:

Packet types: ResultRows

Query pattern: regex:select.*mydb.mytable.*

The filter's code might then look like:

let colspec = context.packet.getColumnSpecByName("badColumnName");

colspec.columnName = "goodColumnName";

Obviously, you must ensure that this type of logic does not break the database clients.

Example: record database errors

You can catch response packets of type Error and log them to a file or some other system to keep track of certain problems:

let FileWriter = Java.type("java.io.FileWriter");
let BufferedWriter = Java.type("java.io.BufferedWriter");
let outFile = new FileWriter("/var/log/ErrorTrace.log", true);
let out = new BufferedWriter(outFile);
out.write("" + new Date() + " - Error packet received: " + context.packet + "\n");
out.close();

Note that, if performance is critical, it may be a good idea to store the out object in the connection context to avoid opening and closing it all the time:

let out = context.connectionContext.ErrorLogFilter_out;
if ( ! out) {
    let FileWriter = Java.type("java.io.FileWriter");
    let outFile = new FileWriter("/var/log/ErrorTrace.log", true);
    let BufferedWriter = Java.type("java.io.BufferedWriter");
    out = new BufferedWriter(outFile);
    context.connectionContext.ErrorLogFilter_out = out;
}
out.write("" + new Date() + " - Error packet received: " + context.packet + "\n");
out.flush();