From 81ab472c579072229a61df32969cc027b0fa4b7f Mon Sep 17 00:00:00 2001 From: Evgeny Kolesnikov Date: Tue, 20 Oct 2020 08:55:32 +0200 Subject: [PATCH] probes/yamfilecontent: Fix field names for cases where key selection section is followed by a set section $.foo[:].bar[:], $.foo[:][:] and alike. --- .../independent/yamlfilecontent_probe.c | 31 ++++++++-- .../yamlfilecontent/openshift-logging.yaml | 12 ++++ .../test_probes_yamlfilecontent_array.sh | 2 +- .../test_probes_yamlfilecontent_array.xml | 45 ++++++++++++++ .../test_probes_yamlfilecontent_key.sh | 2 +- .../test_probes_yamlfilecontent_key.xml | 59 ++++++++++++++++++- 6 files changed, 143 insertions(+), 8 deletions(-) diff --git a/src/OVAL/probes/independent/yamlfilecontent_probe.c b/src/OVAL/probes/independent/yamlfilecontent_probe.c index 6f18abf83..17741a240 100644 --- a/src/OVAL/probes/independent/yamlfilecontent_probe.c +++ b/src/OVAL/probes/independent/yamlfilecontent_probe.c @@ -206,6 +206,7 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, str yaml_event_type_t event_type; bool sequence = false; bool mapping = false; + bool fake_mapping = false; int index = 0; char *key = strdup("#"); @@ -224,21 +225,39 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, str if (sequence) { if (event_type == YAML_SEQUENCE_END_EVENT) { - sequence = false; + if (fake_mapping) { + fake_mapping = false; + if (record && record->itemcount > 0) { + oscap_list_add(values, record); + } else { + // Do not collect empty records + oscap_htable_free0(record); + } + record = NULL; + } else { + sequence = false; + } } else if (event_type == YAML_SEQUENCE_START_EVENT) { - result_error("YAML path '%s' points to a multi-dimensional structure (sequence containing another sequence)", yaml_path_cstr); - goto cleanup; + if (mapping || fake_mapping) { + result_error("YAML path '%s' points to a multi-dimensional structure (a map or a sequence containing other sequences)", yaml_path_cstr); + goto cleanup; + } else { + fake_mapping = true; + record = oscap_htable_new(); + } } } else { if (event_type == YAML_SEQUENCE_START_EVENT) { sequence = true; + if (mapping) + index++; } } if (mapping) { if (event_type == YAML_MAPPING_END_EVENT) { mapping = false; - if (record->itemcount > 0) { + if (record && record->itemcount > 0) { oscap_list_add(values, record); } else { // Do not collect empty records @@ -255,6 +274,10 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, str result_error("YAML path '%s' points to an invalid structure (map containing another map)", yaml_path_cstr); goto cleanup; } + if (fake_mapping) { + result_error("YAML path '%s' points to a multi-dimensional structure (two-dimensional sequence containing a map)", yaml_path_cstr); + goto cleanup; + } mapping = true; sequence = false; index = 0; diff --git a/tests/probes/yamlfilecontent/openshift-logging.yaml b/tests/probes/yamlfilecontent/openshift-logging.yaml index fb6a9d8b6..581a700a3 100644 --- a/tests/probes/yamlfilecontent/openshift-logging.yaml +++ b/tests/probes/yamlfilecontent/openshift-logging.yaml @@ -3,6 +3,18 @@ kind: "LogForwarding" metadata: name: instance namespace: openshift-logging +arrs: +- [1, 2, 3] +- [4, 5, 6] +items: +- allowHostDirVolumePlugin: false + defaultAddCapabilities: null + requiredDropCapabilities: ['KILL', 'ALL'] + name: ['Name', 'Oth'] +- allowHostDirVolumePlugin: false + defaultAddCapabilities: null + requiredDropCapabilities: ['OPS', 'KILL', 'ALL'] + name: ['2 Name', '2 Oth'] spec: disableDefaultForwarding: true outputs: diff --git a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.sh b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.sh index fd5e47538..695a247b3 100755 --- a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.sh +++ b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.sh @@ -19,7 +19,7 @@ function test_probes_yamlfilecontent_array { $OSCAP oval eval --results $RF $DF if [ -f $RF ]; then - verify_results "def" $DF $RF 2 && verify_results "tst" $DF $RF 3 + verify_results "def" $DF $RF 3 && verify_results "tst" $DF $RF 5 ret_val=$? else ret_val=1 diff --git a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.xml b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.xml index c05c5fbb9..77f57cd47 100644 --- a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.xml +++ b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.xml @@ -31,6 +31,17 @@ + + + + + + + + + + + @@ -49,6 +60,16 @@ + + + + + + + + + + @@ -71,6 +92,18 @@ .spec.outputs[0] + + /tmp + openshift-logging.yaml + .arrs[:][:] + + + + /tmp + openshift-logging.yaml + .arrs + + @@ -87,6 +120,12 @@ + + + + + + @@ -99,5 +138,11 @@ + + + 1|2|3|4|5|6 + + + diff --git a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.sh b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.sh index fc1e0ae7e..a942552e9 100755 --- a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.sh +++ b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.sh @@ -19,7 +19,7 @@ function test_probes_yamlfilecontent_key { $OSCAP oval eval --results $RF $DF if [ -f $RF ]; then - verify_results "def" $DF $RF 6 && verify_results "tst" $DF $RF 7 + verify_results "def" $DF $RF 9 && verify_results "tst" $DF $RF 10 ret_val=$? else ret_val=1 diff --git a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.xml b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.xml index 05757d0c8..1697b54fd 100644 --- a/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.xml +++ b/tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.xml @@ -71,7 +71,7 @@ - + @@ -80,6 +80,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -116,9 +136,19 @@ - + + + + + + + + + + + @@ -170,6 +200,18 @@ openshift-logging.yaml .spec.outputs + + + /tmp + openshift-logging.yaml + .items[:]['requiredDropCapabilities','name','q','z'][:] + + + + /tmp + openshift-logging.yaml + .items[:].requiredDropCapabilities[:] + @@ -202,6 +244,19 @@ + + + ^KILL$ + Name + + + + + + ^KILL$ + + +