Skip to content

Commit

Permalink
Merge pull request #211 from Honny1/resolve-references-to-variables-a…
Browse files Browse the repository at this point in the history
…nd-objects

Resolve references to OVAL endpoints
  • Loading branch information
Honny1 authored Sep 2, 2023
2 parents 19a23bd + b89378c commit ea44e09
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,35 @@ function get_table_body(objects) {
return tbody;
}

function generate_property_elements(table_div, endpoint, data) {
for (const [key, value] of Object.entries(data)) { // eslint-disable-line array-element-newline
if(Object.values(value).every(v => typeof v === "object")) {
const h1 = H1.cloneNode();
h1.textContent = `Element ${remove_uuid(key)} contains this elements:`;
h1.className = "pf-c-title pf-m-md";
table_div.appendChild(h1);

generate_property_elements(table_div, endpoint, value);
} else {
const h1 = H1.cloneNode();
h1.textContent = `Element ${remove_uuid(key)}:`;
h1.className = "pf-c-title pf-m-md";
table_div.appendChild(h1);

const table = TABLE.cloneNode();
table.className = "pf-c-table pf-m-compact pf-m-grid-md";
table.setAttribute("role", "grid");
table_div.appendChild(table);

const objects = [];
objects.push(filter_object(value, endpoint));

table.appendChild(get_table_header(objects));
table.appendChild(get_table_body(objects));
}
}
}

function get_OVAL_object_info_heading(oval_object) {
const div = DIV.cloneNode();
const h1 = H1.cloneNode();
Expand All @@ -586,41 +615,25 @@ function get_OVAL_object_info_heading(oval_object) {
div.appendChild(BR.cloneNode());
div.appendChild(h1);


div.appendChild(get_label("pf-m-blue", `OVAL Object ID: ${oval_object.object_id}\u00A0`, undefined, "", "", oval_object.comment));
div.appendChild(get_label("pf-m-blue", `OVAL Object type: ${oval_object.object_type}\u00A0`));
div.appendChild(get_label("pf-m-blue", `Flag: ${oval_object.flag}\u00A0`));

return div;
}

function generate_OVAL_object(test_info, div) {
if (test_info.oval_object === undefined) {
function generate_OVAL_object(oval_object, div) {
if (oval_object === undefined) {
// eslint-disable-next-line no-console
console.error("Error: The test information has no OVAL Objects.");
return;
}
div.appendChild(get_OVAL_object_info_heading(test_info.oval_object));
div.appendChild(get_OVAL_object_info_heading(oval_object));
const table_div = DIV.cloneNode();
table_div.className = "pf-c-scroll-inner-wrapper oval-test-detail-table";
div.appendChild(table_div);

for (const [key, value] of Object.entries(test_info.oval_object.object_data)) { // eslint-disable-line array-element-newline
const h1 = H1.cloneNode();
h1.textContent = `Element ${key}:`;
h1.className = "pf-c-title pf-m-md";
table_div.appendChild(h1);

const table = TABLE.cloneNode();
table.className = "pf-c-table pf-m-compact pf-m-grid-md";
table.setAttribute("role", "grid");
table_div.appendChild(table);

const objects = [];
objects.push(filter_object(value, test_info.oval_object));
table.appendChild(get_table_header(objects));
table.appendChild(get_table_body(objects));
}
generate_property_elements(table_div, oval_object, oval_object.object_data);
}

function get_OVAL_state_heading() {
Expand Down Expand Up @@ -649,25 +662,33 @@ function generate_OVAL_state(oval_state, div) {
table_div.className = "pf-c-scroll-inner-wrapper oval-test-detail-table";
div.appendChild(table_div);

for (const [key, value] of Object.entries(oval_state.state_data)) { // eslint-disable-line array-element-newline
const h1 = H1.cloneNode();
h1.textContent = `Element ${key}:`;
h1.className = "pf-c-title pf-m-md";
table_div.appendChild(h1);

const table = TABLE.cloneNode();
table.className = "pf-c-table pf-m-compact pf-m-grid-md";
table.setAttribute("role", "grid");
table_div.appendChild(table);

const objects = [];
objects.push(filter_object(value, oval_state));
table.appendChild(get_table_header(objects));
table.appendChild(get_table_body(objects));
}
generate_property_elements(table_div, oval_state, oval_state.state_data);
}

function get_OVAL_variable_info_heading(oval_variable) {
const div = DIV.cloneNode();
const h1 = H1.cloneNode();
h1.textContent ='OVAL Variable definition: ';
h1.className = "pf-c-title pf-m-lg";
div.appendChild(BR.cloneNode());
div.appendChild(h1);

div.appendChild(get_label("pf-m-blue", `OVAL Variable ID: ${oval_variable.variable_id}\u00A0`, undefined, "", "", oval_variable.comment));
div.appendChild(get_label("pf-m-blue", `OVAL Variable type: ${oval_variable.variable_type}\u00A0`));
return div;
}

function generate_OVAL_variable(oval_variable, div) {
if (oval_variable === null) {
return;
}
div.appendChild(get_OVAL_variable_info_heading(oval_variable));
const table_div = DIV.cloneNode();
table_div.className = "pf-c-scroll-inner-wrapper oval-test-detail-table";
div.appendChild(table_div);

generate_property_elements(table_div, oval_variable, oval_variable.variable_data);
}

function generate_OVAL_error_message(test_info, div) {
div.appendChild(BR.cloneNode());
Expand Down Expand Up @@ -699,6 +720,33 @@ function generate_OVAL_error_message(test_info, div) {
div.appendChild(BR.cloneNode());
}

function generate_referenced_endpoints(test_info, div) {
if (Object.keys(test_info.referenced_oval_endpoints).length > 0) {
const h1 = H1.cloneNode();
h1.textContent ='Referenced endpoints: ';
h1.className = "pf-c-title pf-m-lg";
div.appendChild(BR.cloneNode());
div.appendChild(h1);
for (const [id, endpoint] of Object.entries(test_info.referenced_oval_endpoints)) { // eslint-disable-line array-element-newline
if(id.includes(":var:")) {
generate_OVAL_variable(endpoint, div);
} else if(id.includes(":obj:")) {
generate_OVAL_object(endpoint, div);
} else if(id.includes(":ste:")) {
div.appendChild(BR.cloneNode());
const heading = H1.cloneNode();
heading.textContent ='OVAL State definition: ';
heading.className = "pf-c-title pf-m-lg";
div.appendChild(heading);
generate_OVAL_state(endpoint, div);
} else {
// eslint-disable-next-line no-console
console.error("Not implemented endpoint type!");
}
}
}
}

function get_OVAL_test_info(test_info) {
const div = DIV.cloneNode();
div.className = "pf-c-accordion__expanded-content-body";
Expand All @@ -714,7 +762,7 @@ function get_OVAL_test_info(test_info) {
generate_OVAL_error_message(test_info, div);
}

generate_OVAL_object(test_info, div);
generate_OVAL_object(test_info.oval_object, div);

if (test_info.oval_states.length > 0) {
div.appendChild(get_OVAL_state_heading());
Expand All @@ -723,6 +771,7 @@ function get_OVAL_test_info(test_info) {
for (const oval_state of test_info.oval_states) {
generate_OVAL_state(oval_state, div);
}
generate_referenced_endpoints(test_info, div);
return div;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
SHORT_RESULT_TO_FULL_RESULT, OvalResult)
from .oval_state import OvalState
from .oval_test import OvalTest
from .oval_variable import OvalVariable
from .profile_info import ProfileInfo
from .reference import Reference
from .remediation import Remediation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

from dataclasses import asdict, dataclass, field
from typing import List
from typing import Dict, List, Union

from .oval_object import OvalObject
from .oval_state import OvalState
from .oval_variable import OvalVariable


@dataclass
class OvalTest:
class OvalTest: # pylint: disable=R0902
test_id: str
check_existence: str = ""
check: str = ""
test_type: str = ""
comment: str = ""
oval_object: OvalObject = None
oval_states: List[OvalState] = field(default_factory=list)
referenced_oval_endpoints: Dict[
str, Union[OvalObject, OvalState, OvalVariable]
] = field(default_factory=dict)

def as_dict(self):
return asdict(self)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

from dataclasses import asdict, dataclass, field
from typing import Dict


@dataclass
class OvalVariable:
variable_id: str
comment: str = ""
variable_type: str = ""
variable_data: Dict[str, str] = field(default_factory=dict)

def as_dict(self):
return asdict(self)
1 change: 1 addition & 0 deletions openscap_report/scap_results_parser/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .oval_result_parser import OVALResultParser
from .oval_state_parser import OVALStateParser
from .oval_test_parser import OVALTestParser
from .oval_variable_parser import OVALVariableParser
from .profile_info_parser import ProfileInfoParser
from .remediation_parser import RemediationParser
from .report_parser import ReportParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,16 @@


class OVALEndpointInformation:
def __init__(self, oval_var_id_to_value_id, ref_values):
self.oval_var_id_to_value_id = oval_var_id_to_value_id
self.ref_values = ref_values

def _get_oval_var_referenced_value(self, var_id, ids_of_oval_variable):
value_id = self.oval_var_id_to_value_id.get(var_id, "")
value = self.ref_values.get(value_id, var_id)
if value == var_id:
ids_of_oval_variable.append(var_id)
return value

def _parse_attributes(
self, id_in_items_of_test_property, element, element_dict, ids_of_oval_variable
self, id_in_items_of_test_property, element, element_dict
):
for key, value in element.attrib.items():
key = key[key.find("}") + 1:]
if key == "var_ref":
ref_value = self._get_oval_var_referenced_value(value, ids_of_oval_variable)
if ref_value == value:
element_dict[f"{key}@{id_in_items_of_test_property}"] = ref_value
else:
element_dict[f"value@{id_in_items_of_test_property}"] = ref_value
else:
element_dict[f"{key}@{id_in_items_of_test_property}"] = value
element_dict[f"{key}@{id_in_items_of_test_property}"] = value

def _get_items(self, xml_test_property):
items_of_test_property = {}
ids_of_oval_variable = []
for element in xml_test_property.iterchildren():
id_in_items_of_test_property = (
SharedStaticMethodsOfParser.get_unique_id_in_dict(
Expand All @@ -43,20 +25,14 @@ def _get_items(self, xml_test_property):
element_dict = {}
if element.text and element.text.strip():
element_dict[f"{id_in_items_of_test_property}@text"] = element.text
if "var_ref" in id_in_items_of_test_property:
element_dict[
f"value@{id_in_items_of_test_property}"
] = self._get_oval_var_referenced_value(
element.text, ids_of_oval_variable
)
if element.attrib:
self._parse_attributes(
id_in_items_of_test_property,
element,
element_dict,
ids_of_oval_variable,
element_dict
)
if len(element):
element_dict = self._get_items(element)

items_of_test_property[id_in_items_of_test_property] = element_dict
# TODO: Insert OVAL Variables
return items_of_test_property
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@


class OVALObjectParser(OVALEndpointInformation):
def __init__(
self,
objects,
collected_objects,
system_data,
oval_var_id_to_value_id,
ref_values,
): # pylint: disable=R0913
super().__init__(oval_var_id_to_value_id, ref_values)
def __init__(self, objects, collected_objects, system_data):
self.objects = objects
self.collected_objects = collected_objects
self.system_data = system_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@


class OVALStateParser(OVALEndpointInformation):
def __init__(self, states, oval_var_id_to_value_id, ref_values):
super().__init__(oval_var_id_to_value_id, ref_values)
def __init__(self, states):
self.states = states

def get_state(self, state_id):
Expand Down
Loading

0 comments on commit ea44e09

Please sign in to comment.