Passed
Branch master (2b673d)
by Matěj
03:01
created

ssg.checks.is_cce_valid()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 re
5
6 2
from .constants import XCCDF11_NS
7
8
9 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...
10
    """
11
    Given an OVAL check element, examine the ``xccdf_ns:check-content-ref``
12
13
    If it exists and it isn't remote, pass it as the return value.
14
    Otherwise, return None.
15
16
    ..see-also:: is_content_href_remote
17
    """
18
    checkcontentref = check.find("./{%s}check-content-ref" % XCCDF11_NS)
19
    if checkcontentref is None:
20
        return None
21
    if is_content_href_remote(checkcontentref):
22
        return None
23
    return checkcontentref
24
25
26 2
def is_content_href_remote(check_content_ref):
27
    """
28
    Given an OVAL check-content-ref element, examine the 'href' attribute.
29
30
    If it starts with 'http://' or 'https://', return True, otherwise
31
    return False.
32
33
    Raises RuntimeError if the ``href`` element doesn't exist.
34
    """
35
    hrefattr = check_content_ref.get("href")
36
    if hrefattr is None:
37
        # @href attribute of <check-content-ref> is required by XCCDF standard
38
        msg = "Invalid OVAL <check-content-ref> detected - missing the " \
39
              "'href' attribute!"
40
        raise RuntimeError(msg)
41
42
    return hrefattr.startswith("http://") or hrefattr.startswith("https://")
43
44
45 2
def is_cce_valid(cceid):
46
    """
47
    IF CCE ID IS IN VALID FORM (either 'CCE-XXXX-X' or 'CCE-XXXXX-X'
48
    where each X is a digit, and the final X is a check-digit)
49
    based on Requirement A17:
50
51
    http://people.redhat.com/swells/nist-scap-validation/scap-val-requirements-1.2.html
52
    """
53 2
    match = re.match(r'^CCE-\d{4,5}-\d$', cceid)
54
    return match is not None
55