1
|
|
|
from __future__ import print_function |
2
|
|
|
|
3
|
|
|
import argparse |
4
|
|
|
import logging |
5
|
|
|
import sys |
6
|
|
|
import os.path |
7
|
|
|
from glob import glob |
8
|
|
|
|
9
|
|
|
import ssg.build_profile |
10
|
|
|
import ssg.build_yaml |
11
|
|
|
import ssg.utils |
12
|
|
|
import ssg.controls |
13
|
|
|
import ssg.products |
14
|
|
|
import ssg.environment |
15
|
|
|
|
16
|
|
|
def create_parser(): |
17
|
|
|
parser = argparse.ArgumentParser() |
18
|
|
|
parser.add_argument( |
19
|
|
|
"--build-config-yaml", required=True, |
20
|
|
|
help="YAML file with information about the build configuration. " |
21
|
|
|
"e.g.: ~/scap-security-guide/build/build_config.yml " |
22
|
|
|
"needed for autodetection of profile root" |
23
|
|
|
) |
24
|
|
|
parser.add_argument( |
25
|
|
|
"--product-yaml", required=True, |
26
|
|
|
help="YAML file with information about the product we are building. " |
27
|
|
|
"e.g.: ~/scap-security-guide/products/rhel7/product.yml " |
28
|
|
|
"needed for autodetection of profile root" |
29
|
|
|
) |
30
|
|
|
parser.add_argument( |
31
|
|
|
"--resolved-base", required=True, |
32
|
|
|
help="To which directory to put processed rule/group/value YAMLs.") |
33
|
|
|
parser.add_argument( |
34
|
|
|
"--controls-dir", |
35
|
|
|
help="Directory that contains control files with policy controls. " |
36
|
|
|
"e.g.: ~/scap-security-guide/controls", |
37
|
|
|
) |
38
|
|
|
parser.add_argument( |
39
|
|
|
"--sce-metadata", |
40
|
|
|
help="Combined SCE metadata to read." |
41
|
|
|
) |
42
|
|
|
return parser |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def get_env_yaml(build_config_yaml, product_yaml): |
46
|
|
|
if build_config_yaml is None or product_yaml is None: |
47
|
|
|
return None |
48
|
|
|
|
49
|
|
|
env_yaml = ssg.environment.open_environment(build_config_yaml, product_yaml) |
50
|
|
|
return env_yaml |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def get_all_content_directories(env_yaml, product_yaml): |
54
|
|
|
relative_benchmark_root = ssg.utils.required_key(env_yaml, "benchmark_root") |
55
|
|
|
benchmark_root = os.path.join(os.path.dirname(product_yaml), relative_benchmark_root) |
56
|
|
|
|
57
|
|
|
add_content_dirs = get_additional_content_directories(env_yaml) |
58
|
|
|
return [benchmark_root] + add_content_dirs |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def get_additional_content_directories(env_yaml): |
62
|
|
|
# we assume that the project root is one directory above build-scripts |
63
|
|
|
project_root = os.path.dirname(os.path.dirname(__file__)) |
64
|
|
|
additional_content_directories = env_yaml.get("additional_content_directories", []) |
65
|
|
|
|
66
|
|
|
absolute_additional_content_dirs = [] |
67
|
|
|
for dirname in additional_content_directories: |
68
|
|
|
if not os.path.isabs(dirname): |
69
|
|
|
dirname = os.path.join(project_root, dirname) |
70
|
|
|
absolute_additional_content_dirs.append(dirname) |
71
|
|
|
return absolute_additional_content_dirs |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def load_benchmark_source_data_from_directory_tree(loader, env_yaml, product_yaml): |
75
|
|
|
relevant_benchmark_sources = get_all_content_directories(env_yaml, product_yaml) |
76
|
|
|
loader.process_directory_trees(relevant_benchmark_sources) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def dump_compiled_profile(base_dir, profile): |
80
|
|
|
dest = os.path.join(base_dir, "profiles", "{name}.profile".format(name=profile.id_)) |
81
|
|
|
profile.dump_yaml(dest) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def get_all_resolved_profiles_by_id(env_yaml, product_yaml, loader, controls_dir=None): |
85
|
|
|
controls_manager = None |
86
|
|
|
if controls_dir: |
87
|
|
|
controls_manager = ssg.controls.ControlsManager(controls_dir, env_yaml) |
88
|
|
|
controls_manager.load() |
89
|
|
|
|
90
|
|
|
profile_files = ssg.products.get_profile_files_from_root(env_yaml, product_yaml) |
91
|
|
|
profiles_by_id = load_resolve_and_validate_profiles(env_yaml, profile_files, loader, controls_manager) |
92
|
|
|
return profiles_by_id |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def load_resolve_and_validate_profiles(env_yaml, profile_files, loader, controls_manager): |
96
|
|
|
profiles_by_id = ssg.build_profile.make_name_to_profile_mapping(profile_files, env_yaml) |
97
|
|
|
|
98
|
|
|
for p in profiles_by_id.values(): |
99
|
|
|
p.resolve(profiles_by_id, loader.all_rules, controls_manager) |
100
|
|
|
|
101
|
|
|
p.validate_variables(loader.all_values.values()) |
102
|
|
|
p.validate_rules(loader.all_rules.values(), loader.all_groups.values()) |
103
|
|
|
p.validate_refine_rules(loader.all_rules.values()) |
104
|
|
|
return profiles_by_id |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def save_everything(base_dir, loader, profiles): |
108
|
|
|
loader.save_all_entities(base_dir) |
109
|
|
|
for p in profiles: |
110
|
|
|
dump_compiled_profile(base_dir, p) |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def main(): |
114
|
|
|
parser = create_parser() |
115
|
|
|
args = parser.parse_args() |
116
|
|
|
|
117
|
|
|
env_yaml = get_env_yaml(args.build_config_yaml, args.product_yaml) |
118
|
|
|
|
119
|
|
|
build_root = os.path.dirname(args.build_config_yaml) |
120
|
|
|
|
121
|
|
|
logfile = "{build_root}/{product}/control_profiles.log".format( |
122
|
|
|
build_root=build_root, |
123
|
|
|
product=env_yaml["product"]) |
124
|
|
|
logging.basicConfig(filename=logfile, level=logging.INFO) |
125
|
|
|
|
126
|
|
|
loader = ssg.build_yaml.BuildLoader( |
127
|
|
|
None, env_yaml, args.sce_metadata) |
128
|
|
|
load_benchmark_source_data_from_directory_tree(loader, env_yaml, args.product_yaml) |
129
|
|
|
|
130
|
|
|
profiles_by_id = get_all_resolved_profiles_by_id(env_yaml, args.product_yaml, loader, args.controls_dir) |
131
|
|
|
|
132
|
|
|
save_everything(args.resolved_base, loader, profiles_by_id.values()) |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
if __name__ == "__main__": |
136
|
|
|
main() |
137
|
|
|
|