|
|
37228b |
From eb2299a0897577048205e4d8a331168d82ce09d0 Mon Sep 17 00:00:00 2001
|
|
|
37228b |
From: Noriko Hosoi <nhosoi@momo7.localdomain>
|
|
|
37228b |
Date: Thu, 26 Jul 2018 17:18:38 -0700
|
|
|
37228b |
Subject: [PATCH] Add a parameter skipempty to the json field type.
|
|
|
37228b |
|
|
|
37228b |
If skipempty is set as follows, empty json objects are dropped from
|
|
|
37228b |
the parsed result.
|
|
|
37228b |
%field_name:json:skipempty%
|
|
|
37228b |
|
|
|
37228b |
If any parameter other than "skipempty" is given ("bogus" in this
|
|
|
37228b |
example), an error message "invalid flag for JSON parser: bogus"
|
|
|
37228b |
is issued.
|
|
|
37228b |
---
|
|
|
37228b |
src/parser.c | 127 ++++++++++++++++++++++++++++++++++++++++++
|
|
|
37228b |
src/parser.h | 2 +-
|
|
|
37228b |
src/pdag.c | 2 +-
|
|
|
37228b |
3 files changed, 129 insertions(+), 2 deletions(-)
|
|
|
37228b |
|
|
|
37228b |
diff --git a/src/parser.c b/src/parser.c
|
|
|
37228b |
index 77407c6..6736c6f 100644
|
|
|
37228b |
--- a/src/parser.c
|
|
|
37228b |
+++ b/src/parser.c
|
|
|
37228b |
@@ -2325,6 +2325,85 @@ PARSER_Parse(v2IPTables)
|
|
|
37228b |
return r;
|
|
|
37228b |
}
|
|
|
37228b |
|
|
|
37228b |
+/*
|
|
|
37228b |
+ * Delete children of the given object if it has children and they are empty.
|
|
|
37228b |
+ *
|
|
|
37228b |
+ * return 0 if object is not empty
|
|
|
37228b |
+ * return 1 if object is empty
|
|
|
37228b |
+ * return < 0 if error
|
|
|
37228b |
+ *
|
|
|
37228b |
+ * Caller should do this:
|
|
|
37228b |
+ * if (jsonSkipEmpty(obj) > 0) {
|
|
|
37228b |
+ * json_object_put(obj);
|
|
|
37228b |
+ * obj = NULL;
|
|
|
37228b |
+ * }
|
|
|
37228b |
+ * or otherwise not use obj if jsonSkipEmpty returns > 0.
|
|
|
37228b |
+ */
|
|
|
37228b |
+static int
|
|
|
37228b |
+jsonSkipEmpty(struct json_object *__restrict__ json)
|
|
|
37228b |
+{
|
|
|
37228b |
+ int rc = 0;
|
|
|
37228b |
+ struct json_object *val = NULL;
|
|
|
37228b |
+
|
|
|
37228b |
+ if(json == NULL) {
|
|
|
37228b |
+ rc = 1;
|
|
|
37228b |
+ goto finalize_it;
|
|
|
37228b |
+ }
|
|
|
37228b |
+
|
|
|
37228b |
+ switch (json_object_get_type(json)) {
|
|
|
37228b |
+ case json_type_string:
|
|
|
37228b |
+ rc = json_object_get_string_len(json) == 0;
|
|
|
37228b |
+ break;
|
|
|
37228b |
+ case json_type_array:
|
|
|
37228b |
+ {
|
|
|
37228b |
+ int i;
|
|
|
37228b |
+ int arrayLen = json_object_array_length(json);
|
|
|
37228b |
+ for (i = 0 ; i < arrayLen ; ++i) {
|
|
|
37228b |
+ val = json_object_array_get_idx(json, i);
|
|
|
37228b |
+ if ((rc = jsonSkipEmpty(val)) > 0) {
|
|
|
37228b |
+ /* delete the empty item and reset the index and arrayLen */
|
|
|
37228b |
+ json_object_array_del_idx(json, i--);
|
|
|
37228b |
+ arrayLen = json_object_array_length(json);
|
|
|
37228b |
+ } else if (rc < 0) {
|
|
|
37228b |
+ goto finalize_it;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ }
|
|
|
37228b |
+ rc = json_object_array_length(json) == 0;
|
|
|
37228b |
+ break;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ case json_type_object:
|
|
|
37228b |
+ {
|
|
|
37228b |
+ struct json_object_iterator it = json_object_iter_begin(json);
|
|
|
37228b |
+ struct json_object_iterator itEnd = json_object_iter_end(json);
|
|
|
37228b |
+ while (!json_object_iter_equal(&it, &itEnd)) {
|
|
|
37228b |
+ val = json_object_iter_peek_value(&it);
|
|
|
37228b |
+ if ((rc = jsonSkipEmpty(val)) > 0) {
|
|
|
37228b |
+ json_object_object_del(json, json_object_iter_peek_name(&it);;
|
|
|
37228b |
+ } else if (rc < 0) {
|
|
|
37228b |
+ goto finalize_it;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ json_object_iter_next(&it);
|
|
|
37228b |
+ }
|
|
|
37228b |
+ rc = json_object_object_length(json) == 0;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ case json_type_null:
|
|
|
37228b |
+ case json_type_boolean:
|
|
|
37228b |
+ case json_type_double:
|
|
|
37228b |
+ case json_type_int:
|
|
|
37228b |
+ default: break;
|
|
|
37228b |
+ }
|
|
|
37228b |
+finalize_it:
|
|
|
37228b |
+ return rc;
|
|
|
37228b |
+}
|
|
|
37228b |
+
|
|
|
37228b |
+/*
|
|
|
37228b |
+ * Parameters for field type json
|
|
|
37228b |
+ * skipempty - skips empty json objects.
|
|
|
37228b |
+ * - %field_name:json:skipempty%
|
|
|
37228b |
+ */
|
|
|
37228b |
+struct data_JSON {
|
|
|
37228b |
+ int skipempty;
|
|
|
37228b |
+};
|
|
|
37228b |
/**
|
|
|
37228b |
* Parse JSON. This parser tries to find JSON data inside a message.
|
|
|
37228b |
* If it finds valid JSON, it will extract it. Extra data after the
|
|
|
37228b |
@@ -2340,6 +2419,7 @@ PARSER_Parse(v2IPTables)
|
|
|
37228b |
PARSER_Parse(JSON)
|
|
|
37228b |
const size_t i = *offs;
|
|
|
37228b |
struct json_tokener *tokener = NULL;
|
|
|
37228b |
+ struct data_JSON *const data = (struct data_JSON*) pdata;
|
|
|
37228b |
|
|
|
37228b |
if(npb->str[i] != '{' && npb->str[i] != ']') {
|
|
|
37228b |
/* this can't be json, see RFC4627, Sect. 2
|
|
|
37228b |
@@ -2368,6 +2448,20 @@ PARSER_Parse(JSON)
|
|
|
37228b |
if(value == NULL) {
|
|
|
37228b |
json_object_put(json);
|
|
|
37228b |
} else {
|
|
|
37228b |
+ if (data && data->skipempty) {
|
|
|
37228b |
+ int rc = jsonSkipEmpty(json);
|
|
|
37228b |
+ if (rc < 0) {
|
|
|
37228b |
+ json_object_put(json);
|
|
|
37228b |
+ FAIL(LN_WRONGPARSER);
|
|
|
37228b |
+ } else if (rc > 0) {
|
|
|
37228b |
+ /*
|
|
|
37228b |
+ * json value is empty.
|
|
|
37228b |
+ * E.g., {"message":""}, {"message":[]}, {"message":{}}
|
|
|
37228b |
+ */
|
|
|
37228b |
+ json_object_put(json);
|
|
|
37228b |
+ FAIL(0);
|
|
|
37228b |
+ }
|
|
|
37228b |
+ }
|
|
|
37228b |
*value = json;
|
|
|
37228b |
}
|
|
|
37228b |
|
|
|
37228b |
@@ -2376,7 +2470,40 @@ PARSER_Parse(JSON)
|
|
|
37228b |
json_tokener_free(tokener);
|
|
|
37228b |
return r;
|
|
|
37228b |
}
|
|
|
37228b |
+PARSER_Construct(JSON)
|
|
|
37228b |
+{
|
|
|
37228b |
+ int r = 0;
|
|
|
37228b |
+ struct json_object *ed;
|
|
|
37228b |
+ struct data_JSON *data = NULL;
|
|
|
37228b |
+ char *flag;
|
|
|
37228b |
|
|
|
37228b |
+ if(json == NULL)
|
|
|
37228b |
+ goto done;
|
|
|
37228b |
+
|
|
|
37228b |
+ if(json_object_object_get_ex(json, "extradata", &ed) == 0) {
|
|
|
37228b |
+ /* No JSON parameter */
|
|
|
37228b |
+ goto done;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ data = (struct data_JSON*) calloc(1, sizeof(struct data_JSON));
|
|
|
37228b |
+ flag = json_object_get_string(ed);
|
|
|
37228b |
+ if (strcasecmp(flag, "skipempty") == 0) {
|
|
|
37228b |
+ data->skipempty = 1;
|
|
|
37228b |
+ } else {
|
|
|
37228b |
+ ln_errprintf(ctx, 0, "invalid flag for JSON parser: %s", flag);
|
|
|
37228b |
+ r = LN_BADCONFIG;
|
|
|
37228b |
+ goto done;
|
|
|
37228b |
+ }
|
|
|
37228b |
+ *pdata = data;
|
|
|
37228b |
+done:
|
|
|
37228b |
+ if(r != 0) {
|
|
|
37228b |
+ free(data);
|
|
|
37228b |
+ }
|
|
|
37228b |
+ return r;
|
|
|
37228b |
+}
|
|
|
37228b |
+PARSER_Destruct(JSON)
|
|
|
37228b |
+{
|
|
|
37228b |
+ free(pdata);
|
|
|
37228b |
+}
|
|
|
37228b |
|
|
|
37228b |
/* check if a char is valid inside a name of a NameValue list
|
|
|
37228b |
* The set of valid characters may be extended if there is good
|
|
|
37228b |
diff --git a/src/parser.h b/src/parser.h
|
|
|
37228b |
index 38be62d..5b4a821 100644
|
|
|
37228b |
--- a/src/parser.h
|
|
|
37228b |
+++ b/src/parser.h
|
|
|
37228b |
@@ -70,7 +70,7 @@ PARSERDEF_NO_DATA(Time24hr);
|
|
|
37228b |
PARSERDEF_NO_DATA(Duration);
|
|
|
37228b |
PARSERDEF_NO_DATA(IPv4);
|
|
|
37228b |
PARSERDEF_NO_DATA(IPv6);
|
|
|
37228b |
-PARSERDEF_NO_DATA(JSON);
|
|
|
37228b |
+PARSERDEF(JSON);
|
|
|
37228b |
PARSERDEF_NO_DATA(CEESyslog);
|
|
|
37228b |
PARSERDEF_NO_DATA(v2IPTables);
|
|
|
37228b |
PARSERDEF_NO_DATA(CiscoInterfaceSpec);
|
|
|
37228b |
diff --git a/src/pdag.c b/src/pdag.c
|
|
|
37228b |
index 0768e99..9feb755 100644
|
|
|
37228b |
--- a/src/pdag.c
|
|
|
37228b |
+++ b/src/pdag.c
|
|
|
37228b |
@@ -90,7 +90,7 @@ static struct ln_parser_info parser_lookup_table[] = {
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("duration", Duration, 16),
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("cisco-interface-spec", CiscoInterfaceSpec, 4),
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("name-value-list", NameValue, 8),
|
|
|
37228b |
- PARSER_ENTRY_NO_DATA("json", JSON, 4),
|
|
|
37228b |
+ PARSER_ENTRY("json", JSON, 4),
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("cee-syslog", CEESyslog, 4),
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("mac48", MAC48, 16),
|
|
|
37228b |
PARSER_ENTRY_NO_DATA("cef", CEF, 4),
|