Passed
Push — master ( 8b9f7f...914492 )
by Marek
02:15
created

ssg.checks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 106
ccs 22
cts 44
cp 0.5
rs 10
c 0
b 0
f 0
wmc 15

6 Functions

Rating   Name   Duplication   Size   Complexity  
A is_cce_valid() 0 10 1
A is_content_href_remote() 0 17 2
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
B get_oval_path() 0 15 6
1 2
from __future__ import absolute_import
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2 2
from __future__ import print_function
3
4 2
import os
5 2
import re
6
7 2
from .constants import XCCDF11_NS
8 2
from .oval import parse_affected
9 2
from .utils import read_file_list
10
11
12 2
def get_content_ref_if_exists_and_not_remote(check):
0 ignored issues
show
Coding Style Naming introduced by
The name get_content_ref_if_exists_and_not_remote does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
13
    """
14
    Given an OVAL check element, examine the ``xccdf_ns:check-content-ref``
15
16
    If it exists and it isn't remote, pass it as the return value.
17
    Otherwise, return None.
18
19
    ..see-also:: is_content_href_remote
20
    """
21
    checkcontentref = check.find("./{%s}check-content-ref" % XCCDF11_NS)
22
    if checkcontentref is None:
23
        return None
24
    if is_content_href_remote(checkcontentref):
25
        return None
26
    return checkcontentref
27
28
29 2
def is_content_href_remote(check_content_ref):
30
    """
31
    Given an OVAL check-content-ref element, examine the 'href' attribute.
32
33
    If it starts with 'http://' or 'https://', return True, otherwise
34
    return False.
35
36
    Raises RuntimeError if the ``href`` element doesn't exist.
37
    """
38
    hrefattr = check_content_ref.get("href")
39
    if hrefattr is None:
40
        # @href attribute of <check-content-ref> is required by XCCDF standard
41
        msg = "Invalid OVAL <check-content-ref> detected - missing the " \
42
              "'href' attribute!"
43
        raise RuntimeError(msg)
44
45
    return hrefattr.startswith("http://") or hrefattr.startswith("https://")
46
47
48 2
def is_cce_valid(cceid):
49
    """
50
    IF CCE ID IS IN VALID FORM (either 'CCE-XXXX-X' or 'CCE-XXXXX-X'
51
    where each X is a digit, and the final X is a check-digit)
52
    based on Requirement A17:
53
54
    http://people.redhat.com/swells/nist-scap-validation/scap-val-requirements-1.2.html
55
    """
56 2
    match = re.match(r'^CCE-\d{4,5}-\d$', cceid)
57 2
    return match is not None
58
59
60 2
def get_oval_path(rule_obj, oval_id):
61
    """
62
    For the given oval_id or product, return the full path to the check in the
63
    given rule.
64
    """
65 2
    if not oval_id.endswith(".xml"):
66 2
        oval_id += ".xml"
67
68 2
    if 'dir' not in rule_obj or 'id' not in rule_obj:
69 2
        raise ValueError("Malformed rule_obj")
70
71 2
    if 'ovals' not in rule_obj or oval_id not in rule_obj['ovals']:
72 2
        raise ValueError("Unknown oval_id:%s for rule_id" % oval_id)
73
74 2
    return os.path.join(rule_obj['dir'], 'oval', oval_id)
75
76
77 2
def get_oval_contents(rule_obj, oval_id):
78
    """
79
    Returns the tuple (path, contents) of the check described by the given
80
    oval_id or product.
81
    """
82
83
    oval_path = get_oval_path(rule_obj, oval_id)
84
    oval_contents = read_file_list(oval_path)
85
86
    return oval_path, oval_contents
87
88
89 2
def set_applicable_platforms(oval_contents, new_platforms):
90
    """
91
    Returns a modified contents which updates the platforms to the new
92
    platforms.
93
    """
94
95
    start_affected, end_affected, indent = parse_affected(oval_contents)
96
97
    platforms = sorted(new_platforms)
98
    new_platforms_xml = map(lambda x: indent + "<platform>%s</platform>" % x, platforms)
99
    new_platforms_xml = list(new_platforms_xml)
100
101
    new_contents = oval_contents[0:start_affected+1]
102
    new_contents.extend(new_platforms_xml)
103
    new_contents.extend(oval_contents[end_affected:])
104
105
    return new_contents
106