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

Parameterizing Painless script literals #9770

Merged
merged 11 commits into from
Dec 28, 2018
9 changes: 6 additions & 3 deletions filebeat/module/auditd/log/ingest/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@
},
{
"convert": {
"field" : "auditd.log.sequence",
"field": "auditd.log.sequence",
"type": "integer",
"ignore_missing": true
}
},
{
"script": {
"lang": "painless",
ycombinator marked this conversation as resolved.
Show resolved Hide resolved
"inline": " String trimQuotes(def v) {\n if (v.startsWith(\"'\") || v.startsWith('\"')) {\n v = v.substring(1, v.length());\n }\n if (v.endsWith(\"'\") || v.endsWith('\"')) {\n v = v.substring(0, v.length()-1);\n } \n return v;\n }\n \n boolean isHexAscii(String v) {\n def len = v.length();\n if (len == 0 || len % 2 != 0) {\n return false; \n }\n \n for (int i = 0 ; i < len ; i++) {\n if (Character.digit(v.charAt(i), 16) == -1) {\n return false;\n }\n }\n\n return true;\n }\n \n String convertHexToString(String hex) {\n\t StringBuilder sb = new StringBuilder();\n\n for (int i=0; i < hex.length() - 1; i+=2) {\n String output = hex.substring(i, (i + 2));\n int decimal = Integer.parseInt(output, 16);\n sb.append((char)decimal);\n }\n\n return sb.toString();\n }\n \n def possibleHexKeys = ['exe', 'cmd'];\n \n def audit = ctx.auditd.get(\"log\");\n Iterator entries = audit.entrySet().iterator();\n while (entries.hasNext()) {\n def e = entries.next();\n def k = e.getKey();\n def v = e.getValue(); \n\n // Remove entries whose value is ?\n if (v == \"?\" || v == \"(null)\" || v == \"\") {\n entries.remove();\n continue;\n }\n \n // Convert hex values to ASCII.\n if (possibleHexKeys.contains(k) && isHexAscii(v)) {\n v = convertHexToString(v);\n audit.put(k, v);\n }\n \n // Trim quotes.\n if (v instanceof String) {\n v = trimQuotes(v);\n audit.put(k, v);\n }\n \n // Convert arch.\n if (k == \"arch\" && v == \"c000003e\") {\n audit.put(k, \"x86_64\");\n }\n }"
"source": " String trimQuotes(def singleQuote, def doubleQuote, def v) {\n if (v.startsWith(singleQuote) || v.startsWith(doubleQuote)) {\n v = v.substring(1, v.length());\n }\n if (v.endsWith(singleQuote) || v.endsWith(doubleQuote)) {\n v = v.substring(0, v.length()-1);\n } \n return v;\n }\n \n boolean isHexAscii(String v) {\n def len = v.length();\n if (len == 0 || len % 2 != 0) {\n return false; \n }\n \n for (int i = 0 ; i < len ; i++) {\n if (Character.digit(v.charAt(i), 16) == -1) {\n return false;\n }\n }\n\n return true;\n }\n \n String convertHexToString(String hex) {\n\t StringBuilder sb = new StringBuilder();\n\n for (int i=0; i < hex.length() - 1; i+=2) {\n String output = hex.substring(i, (i + 2));\n int decimal = Integer.parseInt(output, 16);\n sb.append((char)decimal);\n }\n\n return sb.toString();\n }\n \n def possibleHexKeys = ['exe', 'cmd'];\n \n def audit = ctx.auditd.get(\"log\");\n Iterator entries = audit.entrySet().iterator();\n while (entries.hasNext()) {\n def e = entries.next();\n def k = e.getKey();\n def v = e.getValue(); \n\n // Remove entries whose value is ?\n if (v == \"?\" || v == \"(null)\" || v == \"\") {\n entries.remove();\n continue;\n }\n \n // Convert hex values to ASCII.\n if (possibleHexKeys.contains(k) && isHexAscii(v)) {\n v = convertHexToString(v);\n audit.put(k, v);\n }\n \n // Trim quotes.\n if (v instanceof String) {\n v = trimQuotes(params.single_quote, params.double_quote, v);\n audit.put(k, v);\n }\n \n // Convert arch.\n if (k == \"arch\" && v == \"c000003e\") {\n audit.put(k, \"x86_64\");\n }\n }",
"params": {
"single_quote": "'",
"double_quote": "\""
}
ycombinator marked this conversation as resolved.
Show resolved Hide resolved
}
},
{
Expand Down
5 changes: 4 additions & 1 deletion filebeat/module/nginx/access/ingest/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
}, {
"script": {
"lang": "painless",
"inline": "boolean isPrivate(def ip) { try { StringTokenizer tok = new StringTokenizer(ip, '.'); int firstByte = Integer.parseInt(tok.nextToken()); int secondByte = Integer.parseInt(tok.nextToken()); if (firstByte == 10) { return true; } if (firstByte == 192 && secondByte == 168) { return true; } if (firstByte == 172 && secondByte >= 16 && secondByte <= 31) { return true; } if (firstByte == 127) { return true; } return false; } catch (Exception e) { return false; } } def found = false; for (def item : ctx.network.forwarded_ip) { if (!isPrivate(item)) { ctx.source.ip = item; found = true; break; } } if (!found) { ctx.source.ip = ctx.network.forwarded_ip[0]; }"
"source": "boolean isPrivate(def dot, def ip) { try { StringTokenizer tok = new StringTokenizer(ip, dot); int firstByte = Integer.parseInt(tok.nextToken()); int secondByte = Integer.parseInt(tok.nextToken()); if (firstByte == 10) { return true; } if (firstByte == 192 && secondByte == 168) { return true; } if (firstByte == 172 && secondByte >= 16 && secondByte <= 31) { return true; } if (firstByte == 127) { return true; } return false; } catch (Exception e) { return false; } } def found = false; for (def item : ctx.network.forwarded_ip) { if (!isPrivate(params.dot, item)) { ctx.source.ip = item; found = true; break; } } if (!found) { ctx.source.ip = ctx.network.forwarded_ip[0]; }",
"params": {
"dot": "."
}
}
}, {
"remove":{
"field": "message"
Expand Down
126 changes: 78 additions & 48 deletions filebeat/module/redis/log/ingest/pipeline.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
{
"description": "Pipeline for parsing redis logs",
"processors": [
{
"grok": {
"field": "message",
"patterns": [
"(%{POSINT:process.pid:long}:%{CHAR:redis.log.role} )?%{REDISTIMESTAMP:redis.log.timestamp} %{REDISLEVEL:log.level} %{GREEDYDATA:message}",
"%{POSINT:process.pid:long}:signal-handler \\(%{POSINT:redis.log.timestamp}\\) %{GREEDYDATA:message}"
],
"pattern_definitions": {
"CHAR": "[a-zA-Z]",
"REDISLEVEL": "[.\\-*#]"
"description": "Pipeline for parsing redis logs",
ycombinator marked this conversation as resolved.
Show resolved Hide resolved
"processors": [
{
"grok": {
"field": "message",
"patterns": [
"(%{POSINT:process.pid:long}:%{CHAR:redis.log.role} )?%{REDISTIMESTAMP:redis.log.timestamp} %{REDISLEVEL:log.level} %{GREEDYDATA:message}",
"%{POSINT:process.pid:long}:signal-handler \\(%{POSINT:redis.log.timestamp}\\) %{GREEDYDATA:message}"
],
"pattern_definitions": {
"CHAR": "[a-zA-Z]",
"REDISLEVEL": "[.\\-*#]"
}
}
},
{
"script": {
"source": "if (ctx.log.level == params.dot) {\n ctx.log.level = params.debug;\n } else if (ctx.log.level == params.dash) {\n ctx.log.level = params.verbose;\n } else if (ctx.log.level == params.asterisk) {\n ctx.log.level = params.notice;\n } else if (ctx.log.level == params.hash) {\n ctx.log.level = params.warning;\n }",
"params": {
"dot": ".",
"debug": "debug",
"dash": "-",
"verbose": "verbose",
"asterisk": "*",
"notice": "notice",
"hash": "#",
"warning": "warning"
}
}
},
{
"script": {
"lang": "painless",
"source": "if (ctx.redis.log.role == params.master_abbrev) {\n ctx.redis.log.role = params.master;\n } else if (ctx.redis.log.role == params.slave_abbrev) {\n ctx.redis.log.role = params.slave;\n } else if (ctx.redis.log.role == params.child_abbrev) {\n ctx.redis.log.role = params.child;\n } else if (ctx.redis.log.role == params.sentinel_abbrev) {\n ctx.redis.log.role = params.sentinel;\n }\n ",
"params": {
"master_abbrev": "M",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems odd that we have to extract all these constants which are only used once but I don't think there is a better way. Perhaps @jakelandis can comment?

"master": "master",
"slave_abbrev": "S",
"slave": "slave",
"child_abbrev": "C",
"child": "child",
"sentinel_abbrev": "X",
"sentinel": "sentinel"
}
}
},
{
"rename": {
"field": "@timestamp",
"target_field": "read_timestamp"
}
},
{
"date": {
"field": "redis.log.timestamp",
"target_field": "@timestamp",
"formats": [
"dd MMM H:m:s.SSS",
"dd MMM H:m:s",
"UNIX"
],
"ignore_failure": true
}
},
{
"remove": {
"field": "redis.log.timestamp",
"ignore_failure": true
}
}
}
}, {
"script": {
"lang": "painless",
"inline": "if (ctx.log.level == '.') {\n ctx.log.level = 'debug';\n } else if (ctx.log.level == '-') {\n ctx.log.level = 'verbose';\n } else if (ctx.log.level == '*') {\n ctx.log.level = 'notice';\n } else if (ctx.log.level == '#') {\n ctx.log.level = 'warning';\n }"
}
}, {
"script": {
"lang": "painless",
"inline": "if (ctx.redis.log.role == 'M') {\n ctx.redis.log.role = 'master';\n } else if (ctx.redis.log.role == 'S') {\n ctx.redis.log.role = 'slave';\n } else if (ctx.redis.log.role == 'C') {\n ctx.redis.log.role = 'child';\n } else if (ctx.redis.log.role == 'X') {\n ctx.redis.log.role = 'sentinel';\n }\n "
}
}, {
"rename": {
"field": "@timestamp",
"target_field": "read_timestamp"
}
}, {
"date": {
"field": "redis.log.timestamp",
"target_field": "@timestamp",
"formats": ["dd MMM H:m:s.SSS", "dd MMM H:m:s", "UNIX"],
"ignore_failure": true
}
}, {
"remove": {
"field": "redis.log.timestamp",
"ignore_failure": true
}
}
],
"on_failure" : [{
"set" : {
"field" : "error.message",
"value" : "{{ _ingest.on_failure_message }}"
}
}]
],
"on_failure": [
{
"set": {
"field": "error.message",
"value": "{{ _ingest.on_failure_message }}"
}
}
]
}