1
|
|
|
from __future__ import absolute_import |
2
|
|
|
from __future__ import print_function |
3
|
|
|
|
4
|
|
|
import os |
5
|
|
|
from collections import namedtuple |
6
|
|
|
from glob import glob |
7
|
|
|
|
8
|
|
|
from .build_cpe import ProductCPEs |
9
|
|
|
from .constants import (DEFAULT_PRODUCT, product_directories, |
10
|
|
|
DEFAULT_DCONF_GDM_DIR, |
11
|
|
|
DEFAULT_AIDE_CONF_PATH, |
12
|
|
|
DEFAULT_AIDE_BIN_PATH, |
13
|
|
|
DEFAULT_SSH_DISTRIBUTED_CONFIG, |
14
|
|
|
DEFAULT_CHRONY_CONF_PATH, |
15
|
|
|
DEFAULT_AUDISP_CONF_PATH, |
16
|
|
|
DEFAULT_FAILLOCK_PATH, |
17
|
|
|
DEFAULT_SYSCTL_REMEDIATE_DROP_IN_FILE, |
18
|
|
|
PKG_MANAGER_TO_SYSTEM, |
19
|
|
|
PKG_MANAGER_TO_CONFIG_FILE, |
20
|
|
|
XCCDF_PLATFORM_TO_PACKAGE, |
21
|
|
|
SSG_REF_URIS) |
22
|
|
|
from .utils import merge_dicts, required_key |
23
|
|
|
from .yaml import open_raw, ordered_dump, open_and_expand |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def _validate_product_oval_feed_url(contents): |
27
|
|
|
if "oval_feed_url" not in contents: |
28
|
|
|
return |
29
|
|
|
url = contents["oval_feed_url"] |
30
|
|
|
if not url.startswith("https"): |
31
|
|
|
msg = ( |
32
|
|
|
"OVAL feed of product '{product}' is not available through an encrypted channel: {url}" |
33
|
|
|
.format(product=contents["product"], url=url) |
34
|
|
|
) |
35
|
|
|
raise ValueError(msg) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def _get_implied_properties(existing_properties): |
39
|
|
|
result = existing_properties.copy() |
40
|
|
|
if "pkg_manager" in existing_properties: |
41
|
|
|
pkg_manager = existing_properties["pkg_manager"] |
42
|
|
|
if "pkg_system" not in existing_properties: |
43
|
|
|
result["pkg_system"] = PKG_MANAGER_TO_SYSTEM[pkg_manager] |
44
|
|
|
if "pkg_manager_config_file" not in existing_properties: |
45
|
|
|
if pkg_manager in PKG_MANAGER_TO_CONFIG_FILE: |
46
|
|
|
result["pkg_manager_config_file"] = PKG_MANAGER_TO_CONFIG_FILE[pkg_manager] |
47
|
|
|
|
48
|
|
|
if "groups" not in existing_properties: |
49
|
|
|
result["groups"] = dict() |
50
|
|
|
|
51
|
|
|
if "dconf_gdm_dir" not in existing_properties: |
52
|
|
|
result["dconf_gdm_dir"] = DEFAULT_DCONF_GDM_DIR |
53
|
|
|
|
54
|
|
|
if "aide_conf_path" not in existing_properties: |
55
|
|
|
result["aide_conf_path"] = DEFAULT_AIDE_CONF_PATH |
56
|
|
|
|
57
|
|
|
if "aide_bin_path" not in existing_properties: |
58
|
|
|
result["aide_bin_path"] = DEFAULT_AIDE_BIN_PATH |
59
|
|
|
|
60
|
|
|
if "sshd_distributed_config" not in existing_properties: |
61
|
|
|
result["sshd_distributed_config"] = DEFAULT_SSH_DISTRIBUTED_CONFIG |
62
|
|
|
|
63
|
|
|
if "product" not in existing_properties: |
64
|
|
|
result["product"] = DEFAULT_PRODUCT |
65
|
|
|
|
66
|
|
|
if "chrony_conf_path" not in existing_properties: |
67
|
|
|
result["chrony_conf_path"] = DEFAULT_CHRONY_CONF_PATH |
68
|
|
|
|
69
|
|
|
if "audisp_conf_path" not in existing_properties: |
70
|
|
|
result["audisp_conf_path"] = DEFAULT_AUDISP_CONF_PATH |
71
|
|
|
|
72
|
|
|
if "faillock_path" not in existing_properties: |
73
|
|
|
result["faillock_path"] = DEFAULT_FAILLOCK_PATH |
74
|
|
|
|
75
|
|
|
if "sysctl_remediate_drop_in_file" not in existing_properties: |
76
|
|
|
result["sysctl_remediate_drop_in_file"] = DEFAULT_SYSCTL_REMEDIATE_DROP_IN_FILE |
77
|
|
|
|
78
|
|
|
return result |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def product_yaml_path(ssg_root, product): |
82
|
|
|
return os.path.join(ssg_root, "products", product, "product.yml") |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
class Product(object): |
86
|
|
|
def __init__(self, filename): |
87
|
|
|
self._primary_data = dict() |
88
|
|
|
self._acquired_data = dict() |
89
|
|
|
self._load_from_filename(filename) |
90
|
|
|
if "basic_properties_derived" not in self._primary_data: |
91
|
|
|
self._derive_basic_properties(filename) |
92
|
|
|
|
93
|
|
|
@property |
94
|
|
|
def _data_as_dict(self): |
95
|
|
|
data = dict() |
96
|
|
|
data.update(self._acquired_data) |
97
|
|
|
data.update(self._primary_data) |
98
|
|
|
return data |
99
|
|
|
|
100
|
|
|
def write(self, filename): |
101
|
|
|
with open(filename, "w") as f: |
102
|
|
|
ordered_dump(self._data_as_dict, f) |
103
|
|
|
|
104
|
|
|
def __getitem__(self, key): |
105
|
|
|
return self._data_as_dict[key] |
106
|
|
|
|
107
|
|
|
def __contains__(self, key): |
108
|
|
|
return key in self._data_as_dict |
109
|
|
|
|
110
|
|
|
def __iter__(self): |
111
|
|
|
return iter(self._data_as_dict.items()) |
112
|
|
|
|
113
|
|
|
def __len__(self): |
114
|
|
|
return len(self._data_as_dict) |
115
|
|
|
|
116
|
|
|
def get(self, key, default=None): |
117
|
|
|
return self._data_as_dict.get(key, default) |
118
|
|
|
|
119
|
|
|
def _load_from_filename(self, filename): |
120
|
|
|
self._primary_data = open_raw(filename) |
121
|
|
|
|
122
|
|
|
def _derive_basic_properties(self, filename): |
123
|
|
|
_validate_product_oval_feed_url(self._primary_data) |
124
|
|
|
|
125
|
|
|
# The product directory is necessary to get absolute paths to benchmark, profile and |
126
|
|
|
# cpe directories, which are all relative to the product directory |
127
|
|
|
self._primary_data["product_dir"] = os.path.dirname(filename) |
128
|
|
|
|
129
|
|
|
platform_package_overrides = self._primary_data.get("platform_package_overrides", {}) |
130
|
|
|
# Merge common platform package mappings, while keeping product specific mappings |
131
|
|
|
self._primary_data["platform_package_overrides"] = merge_dicts( |
132
|
|
|
XCCDF_PLATFORM_TO_PACKAGE, platform_package_overrides) |
133
|
|
|
self._primary_data.update(_get_implied_properties(self._primary_data)) |
134
|
|
|
|
135
|
|
|
reference_uris = self._primary_data.get("reference_uris", {}) |
136
|
|
|
self._primary_data["reference_uris"] = merge_dicts(SSG_REF_URIS, reference_uris) |
137
|
|
|
|
138
|
|
|
self._primary_data["basic_properties_derived"] = True |
139
|
|
|
|
140
|
|
|
def expand_by_acquired_data(self, property_dict): |
141
|
|
|
for specified_key in property_dict: |
142
|
|
|
if specified_key in self: |
143
|
|
|
msg = ( |
144
|
|
|
"The property {name} is already defined, " |
145
|
|
|
"you can't define it once more elsewhere." |
146
|
|
|
.format(name=specified_key)) |
147
|
|
|
raise ValueError(msg) |
148
|
|
|
self._acquired_data.update(property_dict) |
149
|
|
|
|
150
|
|
|
@staticmethod |
151
|
|
|
def transform_default_and_overrides_mappings_to_mapping(mappings): |
152
|
|
|
result = dict() |
153
|
|
|
if not isinstance(mappings, dict): |
154
|
|
|
msg = ( |
155
|
|
|
"Expected a mapping, got {type}." |
156
|
|
|
.format(type=str(type(mappings)))) |
157
|
|
|
raise ValueError(msg) |
158
|
|
|
|
159
|
|
|
mapping = mappings.pop("default") |
160
|
|
|
if mapping: |
161
|
|
|
result.update(mapping) |
162
|
|
|
mapping = mappings.pop("overrides", dict()) |
163
|
|
|
if mapping: |
164
|
|
|
result.update(mapping) |
165
|
|
|
if len(mappings): |
166
|
|
|
msg = ( |
167
|
|
|
"The dictionary contains unwanted keys: {keys}" |
168
|
|
|
.format(keys=list(mappings.keys()))) |
169
|
|
|
raise ValueError(msg) |
170
|
|
|
return result |
171
|
|
|
|
172
|
|
|
def read_properties_from_directory(self, path): |
173
|
|
|
filenames = glob(path + "/*.yml") |
174
|
|
|
for f in sorted(filenames): |
175
|
|
|
substitutions_dict = dict() |
176
|
|
|
substitutions_dict.update(self) |
177
|
|
|
new_defs = open_and_expand(f, substitutions_dict) |
178
|
|
|
new_symbols = self.transform_default_and_overrides_mappings_to_mapping(new_defs) |
179
|
|
|
self.expand_by_acquired_data(new_symbols) |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
def load_product_yaml(product_yaml_path): |
183
|
|
|
""" |
184
|
|
|
Reads a product data from disk and returns it. |
185
|
|
|
The returned product dictionary also contains derived useful information. |
186
|
|
|
""" |
187
|
|
|
|
188
|
|
|
product_yaml = Product(product_yaml_path) |
189
|
|
|
return product_yaml |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
def get_all(ssg_root): |
193
|
|
|
""" |
194
|
|
|
Analyzes all products in the SSG root and sorts them into two categories: |
195
|
|
|
those which use linux_os and those which use their own directory. Returns |
196
|
|
|
a namedtuple of sets, (linux, other). |
197
|
|
|
""" |
198
|
|
|
|
199
|
|
|
linux_products = set() |
200
|
|
|
other_products = set() |
201
|
|
|
|
202
|
|
|
for product in product_directories: |
203
|
|
|
path = product_yaml_path(ssg_root, product) |
204
|
|
|
product_yaml = load_product_yaml(path) |
205
|
|
|
|
206
|
|
|
guide_dir = os.path.join(product_yaml["product_dir"], product_yaml['benchmark_root']) |
207
|
|
|
guide_dir = os.path.abspath(guide_dir) |
208
|
|
|
|
209
|
|
|
if 'linux_os' in guide_dir: |
210
|
|
|
linux_products.add(product) |
211
|
|
|
else: |
212
|
|
|
other_products.add(product) |
213
|
|
|
|
214
|
|
|
products = namedtuple('products', ['linux', 'other']) |
215
|
|
|
return products(linux_products, other_products) |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
def get_profiles_directory(env_yaml): |
219
|
|
|
profiles_root = None |
220
|
|
|
if env_yaml: |
221
|
|
|
profiles_root = required_key(env_yaml, "profiles_root") |
222
|
|
|
return profiles_root |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
def get_profile_files_from_root(env_yaml, product_yaml): |
226
|
|
|
profile_files = [] |
227
|
|
|
if env_yaml: |
228
|
|
|
profiles_root = get_profiles_directory(env_yaml) |
229
|
|
|
base_dir = product_yaml["product_dir"] |
230
|
|
|
profile_files = sorted(glob("{base_dir}/{profiles_root}/*.profile" |
231
|
|
|
.format(profiles_root=profiles_root, base_dir=base_dir))) |
232
|
|
|
return profile_files |
233
|
|
|
|