Test Failed
Push — master ( 00964b...f887a2 )
by Jan
02:30 queued 12s
created

ssg.products   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 86.44%

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 108
ccs 51
cts 59
cp 0.8644
rs 10
c 0
b 0
f 0
wmc 16

5 Functions

Rating   Name   Duplication   Size   Complexity  
A _validate_product_oval_feed_url() 0 10 3
A get_all() 0 24 3
A load_product_yaml() 0 23 1
B _get_implied_properties() 0 17 7
A get_profile_files_from_root() 0 8 2
1 2
from __future__ import absolute_import
2 2
from __future__ import print_function
3
4 2
import os
5 2
from collections import namedtuple
6 2
from glob import glob
7
8 2
from .build_cpe import ProductCPEs
9 2
from .constants import (product_directories,
10
                        DEFAULT_UID_MIN,
11
                        PKG_MANAGER_TO_SYSTEM,
12
                        PKG_MANAGER_TO_CONFIG_FILE,
13
                        XCCDF_PLATFORM_TO_PACKAGE)
14 2
from .utils import merge_dicts, required_key
15 2
from .yaml import open_raw
16
17
18 2
def _validate_product_oval_feed_url(contents):
19 2
    if "oval_feed_url" not in contents:
20 2
        return
21 2
    url = contents["oval_feed_url"]
22 2
    if not url.startswith("https"):
23
        msg = (
24
            "OVAL feed of product '{product}' is not available through an encrypted channel: {url}"
25
            .format(product=contents["product"], url=url)
26
        )
27
        raise ValueError(msg)
28
29
30 2
def _get_implied_properties(existing_properties):
31 2
    result = dict()
32 2
    if "pkg_manager" in existing_properties:
33 2
        pkg_manager = existing_properties["pkg_manager"]
34 2
        if "pkg_system" not in existing_properties:
35 2
            result["pkg_system"] = PKG_MANAGER_TO_SYSTEM[pkg_manager]
36 2
        if "pkg_manager_config_file" not in existing_properties:
37 2
            if pkg_manager in PKG_MANAGER_TO_CONFIG_FILE:
38 2
                result["pkg_manager_config_file"] = PKG_MANAGER_TO_CONFIG_FILE[pkg_manager]
39
40 2
    if "uid_min" not in existing_properties:
41 2
        result["uid_min"] = DEFAULT_UID_MIN
42
43 2
    if "auid" not in existing_properties:
44 2
        result["auid"] = existing_properties.get("uid_min", DEFAULT_UID_MIN)
45
46 2
    return result
47
48
49 2
def load_product_yaml(product_yaml_path):
50
    """
51
    Reads a product data from disk and returns it.
52
    The returned product dictionary also contains derived useful information.
53
    """
54
55 2
    product_yaml = open_raw(product_yaml_path)
56 2
    _validate_product_oval_feed_url(product_yaml)
57
58
    # The product directory is necessary to get absolute paths to benchmark, profile and
59
    # cpe directories, which are all relative to the product directory
60 2
    product_yaml["product_dir"] = os.path.dirname(product_yaml_path)
61
62 2
    platform_package_overrides = product_yaml.get("platform_package_overrides", {})
63
    # Merge common platform package mappings, while keeping product specific mappings
64 2
    product_yaml["platform_package_overrides"] = merge_dicts(XCCDF_PLATFORM_TO_PACKAGE,
65
                                                             platform_package_overrides)
66 2
    product_yaml.update(_get_implied_properties(product_yaml))
67
68
    # The product_yaml should be aware of the ProductCPEs
69 2
    product_yaml["product_cpes"] = ProductCPEs(product_yaml)
70
71 2
    return product_yaml
72
73
74 2
def get_all(ssg_root):
75
    """
76
    Analyzes all products in the SSG root and sorts them into two categories:
77
    those which use linux_os and those which use their own directory. Returns
78
    a namedtuple of sets, (linux, other).
79
    """
80
81 2
    linux_products = set()
82 2
    other_products = set()
83
84 2
    for product in product_directories:
85 2
        product_yaml_path = os.path.join(ssg_root, product, "product.yml")
86 2
        product_yaml = load_product_yaml(product_yaml_path)
87
88 2
        guide_dir = os.path.join(product_yaml["product_dir"], product_yaml['benchmark_root'])
89 2
        guide_dir = os.path.abspath(guide_dir)
90
91 2
        if 'linux_os' in guide_dir:
92 2
            linux_products.add(product)
93
        else:
94 2
            other_products.add(product)
95
96 2
    products = namedtuple('products', ['linux', 'other'])
97 2
    return products(linux_products, other_products)
98
99
100 2
def get_profile_files_from_root(env_yaml, product_yaml):
101
    profile_files = []
102
    if env_yaml:
103
        base_dir = os.path.dirname(product_yaml)
104
        profiles_root = required_key(env_yaml, "profiles_root")
105
        profile_files = sorted(glob("{base_dir}/{profiles_root}/*.profile"
106
                               .format(profiles_root=profiles_root, base_dir=base_dir)))
107
    return profile_files
108