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

ssg.products.load_product_yaml()   A

Complexity

Conditions 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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