Test Failed
Push — master ( 79e936...04af7d )
by Jan
02:32 queued 12s
created

ssg.products.product_yaml_path()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

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