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