ssg.checks   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 93
rs 10
c 0
b 0
f 0
ccs 0
cts 40
cp 0
wmc 14

5 Functions

Rating   Name   Duplication   Size   Complexity  
A is_content_href_remote() 0 17 2
B get_oval_path() 0 15 6
A get_oval_contents() 0 10 1
A get_content_ref_if_exists_and_not_remote() 0 15 3
A set_applicable_platforms() 0 17 2
1
from __future__ import absolute_import
2
from __future__ import print_function
3
4
import os
5
6
from .constants import XCCDF12_NS
7
from .oval import parse_affected
8
from .utils import read_file_list
9
10
11
def get_content_ref_if_exists_and_not_remote(check):
12
    """
13
    Given an OVAL check element, examine the ``xccdf_ns:check-content-ref``
14
15
    If it exists and it isn't remote, pass it as the return value.
16
    Otherwise, return None.
17
18
    ..see-also:: is_content_href_remote
19
    """
20
    checkcontentref = check.find("./{%s}check-content-ref" % XCCDF12_NS)
21
    if checkcontentref is None:
22
        return None
23
    if is_content_href_remote(checkcontentref):
24
        return None
25
    return checkcontentref
26
27
28
def is_content_href_remote(check_content_ref):
29
    """
30
    Given an OVAL check-content-ref element, examine the 'href' attribute.
31
32
    If it starts with 'http://' or 'https://', return True, otherwise
33
    return False.
34
35
    Raises RuntimeError if the ``href`` element doesn't exist.
36
    """
37
    hrefattr = check_content_ref.get("href")
38
    if hrefattr is None:
39
        # @href attribute of <check-content-ref> is required by XCCDF standard
40
        msg = "Invalid OVAL <check-content-ref> detected - missing the " \
41
              "'href' attribute!"
42
        raise RuntimeError(msg)
43
44
    return hrefattr.startswith("http://") or hrefattr.startswith("https://")
45
46
47
def get_oval_path(rule_obj, oval_id):
48
    """
49
    For the given oval_id or product, return the full path to the check in the
50
    given rule.
51
    """
52
    if not oval_id.endswith(".xml"):
53
        oval_id += ".xml"
54
55
    if 'dir' not in rule_obj or 'id' not in rule_obj:
56
        raise ValueError("Malformed rule_obj")
57
58
    if 'ovals' not in rule_obj or oval_id not in rule_obj['ovals']:
59
        raise ValueError("Unknown oval_id:%s for rule_id" % oval_id)
60
61
    return os.path.join(rule_obj['dir'], 'oval', oval_id)
62
63
64
def get_oval_contents(rule_obj, oval_id):
65
    """
66
    Returns the tuple (path, contents) of the check described by the given
67
    oval_id or product.
68
    """
69
70
    oval_path = get_oval_path(rule_obj, oval_id)
71
    oval_contents = read_file_list(oval_path)
72
73
    return oval_path, oval_contents
74
75
76
def set_applicable_platforms(oval_contents, new_platforms):
77
    """
78
    Returns a modified contents which updates the platforms to the new
79
    platforms.
80
    """
81
82
    start_affected, end_affected, indent = parse_affected(oval_contents)
83
84
    platforms = sorted(new_platforms)
85
    new_platforms_xml = map(lambda x: indent + "<platform>%s</platform>" % x, platforms)
86
    new_platforms_xml = list(new_platforms_xml)
87
88
    new_contents = oval_contents[0:start_affected+1]
89
    new_contents.extend(new_platforms_xml)
90
    new_contents.extend(oval_contents[end_affected:])
91
92
    return new_contents
93