Test Failed
Pull Request — master (#7011)
by Matěj
02:06
created

ssg.products   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 86.15%

Importance

Changes 0
Metric Value
wmc 19
eloc 76
dl 0
loc 120
ccs 56
cts 65
cp 0.8615
rs 10
c 0
b 0
f 0

6 Functions

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