Test Failed
Push — master ( ad184d...ba033d )
by Milan
01:39 queued 15s
created

ssg.products.get_all()   A

Complexity

Conditions 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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