1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
|
3
|
|
|
import sys |
4
|
|
|
import os |
5
|
|
|
import argparse |
6
|
|
|
import subprocess |
7
|
|
|
import json |
8
|
|
|
|
9
|
|
|
import ssg.checks |
10
|
|
|
import ssg.oval |
11
|
|
|
import ssg.rule_yaml |
12
|
|
|
import ssg.utils |
13
|
|
|
|
14
|
|
|
SSG_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
15
|
|
|
|
16
|
|
|
|
17
|
|
View Code Duplication |
def parse_args(): |
|
|
|
|
18
|
|
|
parser = argparse.ArgumentParser() |
19
|
|
|
parser.add_argument("-j", "--json", type=str, action="store", default="build/rule_dirs.json", |
20
|
|
|
help="File to read json output of rule_dir_json from (defaults " |
21
|
|
|
"to build/rule_dirs.json)") |
22
|
|
|
parser.add_argument("rule_id", type=str, help="Rule to change, by id") |
23
|
|
|
parser.add_argument("action", choices=['add', 'remove', 'list', 'replace', 'delete', 'make_shared', 'diff'], |
24
|
|
|
help="Rule to change, by id") |
25
|
|
|
parser.add_argument("products", type=str, nargs='*', |
26
|
|
|
help="Products or platforms to perform action with on rule_id. For replace, " |
27
|
|
|
"the expected format is " |
28
|
|
|
"platform[,other_platform]~platform[,other_platform] " |
29
|
|
|
"where the first half is the platforms that are required to " |
30
|
|
|
"match, and are replaced by the platforms in the second half " |
31
|
|
|
"if all match. Add and remove require platforms and only apply " |
32
|
|
|
"to the shared OVAL; delete, make_shared, and diff require products.") |
33
|
|
|
return parser.parse_args() |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def list_platforms(rule_obj): |
37
|
|
|
print("Computed products:") |
38
|
|
|
for oval_id in sorted(rule_obj.get('ovals', {})): |
39
|
|
|
oval = rule_obj['ovals'][oval_id] |
40
|
|
|
|
41
|
|
|
print(" - %s" % oval_id) |
42
|
|
|
for product in sorted(oval.get('products', [])): |
43
|
|
|
print(" - %s" % product) |
44
|
|
|
|
45
|
|
|
print("") |
46
|
|
|
|
47
|
|
|
print("Actual platforms:") |
48
|
|
|
for oval_id in sorted(rule_obj.get('ovals', {})): |
49
|
|
|
oval = rule_obj['ovals'][oval_id] |
50
|
|
|
oval_file = ssg.checks.get_oval_path(rule_obj, oval_id) |
51
|
|
|
platforms = ssg.oval.applicable_platforms(oval_file) |
52
|
|
|
|
53
|
|
|
print(" - %s" % oval_id) |
54
|
|
|
for platform in platforms: |
55
|
|
|
print(" - %s" % platform) |
56
|
|
|
|
57
|
|
|
print("") |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def add_platforms(rule_obj, platforms): |
61
|
|
|
oval_file, oval_contents = ssg.checks.get_oval_contents(rule_obj, 'shared') |
62
|
|
|
current_platforms = ssg.oval.applicable_platforms(oval_file) |
63
|
|
|
new_platforms = set(current_platforms) |
64
|
|
|
new_platforms.update(platforms) |
65
|
|
|
|
66
|
|
|
print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
67
|
|
|
print("New platforms: %s" % ','.join(sorted(new_platforms))) |
68
|
|
|
|
69
|
|
|
new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
70
|
|
|
new_platforms) |
71
|
|
|
ssg.utils.write_list_file(oval_file, new_contents) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def remove_platforms(rule_obj, platforms): |
75
|
|
|
oval_file, oval_contents = ssg.checks.get_oval_contents(rule_obj, 'shared') |
76
|
|
|
current_platforms = ssg.oval.applicable_platforms(oval_file) |
77
|
|
|
new_platforms = set(current_platforms).difference(platforms) |
78
|
|
|
|
79
|
|
|
print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
80
|
|
|
print("New platforms: %s" % ','.join(sorted(new_platforms))) |
81
|
|
|
|
82
|
|
|
new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
83
|
|
|
new_platforms) |
84
|
|
|
ssg.utils.write_list_file(oval_file, new_contents) |
85
|
|
|
|
86
|
|
|
|
87
|
|
View Code Duplication |
def replace_platforms(rule_obj, platforms): |
|
|
|
|
88
|
|
|
oval_file, oval_contents = ssg.checks.get_oval_contents(rule_obj, 'shared') |
89
|
|
|
current_platforms = ssg.oval.applicable_platforms(oval_file) |
90
|
|
|
new_platforms = set(current_platforms) |
91
|
|
|
|
92
|
|
|
for platform in platforms: |
93
|
|
|
parsed_platform = platform.split('~') |
94
|
|
|
if not len(parsed_platform) == 2: |
95
|
|
|
print("Invalid platform replacement description: %s" % product, |
|
|
|
|
96
|
|
|
file=sys.stderr) |
97
|
|
|
sys.exit(1) |
98
|
|
|
|
99
|
|
|
match = ssg.rule_yaml.parse_prodtype(parsed_platform[0]) |
100
|
|
|
replacement = ssg.rule_yaml.parse_prodtype(parsed_platform[1]) |
101
|
|
|
|
102
|
|
|
if match.issubset(current_platforms): |
103
|
|
|
new_platforms.difference_update(match) |
104
|
|
|
new_platforms.update(replacement) |
105
|
|
|
|
106
|
|
|
print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
107
|
|
|
print("New platforms: %s" % ','.join(sorted(new_platforms))) |
108
|
|
|
|
109
|
|
|
new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
110
|
|
|
new_platforms) |
111
|
|
|
ssg.utils.write_list_file(oval_file, new_contents) |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def delete_ovals(rule_obj, products): |
115
|
|
|
for product in products: |
116
|
|
|
oval_file = ssg.checks.get_oval_path(rule_obj, product) |
117
|
|
|
os.remove(oval_file) |
118
|
|
|
print("Removed: %s" % oval_file) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def make_shared_oval(rule_obj, products): |
122
|
|
|
if not products or len(products) > 1: |
123
|
|
|
raise ValueError("Must pass exactly one product for the make_shared option.") |
124
|
|
|
if 'ovals' not in rule_obj: |
125
|
|
|
raise ValueError("Rule is missing all ovals.") |
126
|
|
|
|
127
|
|
|
if 'shared.xml' in rule_obj['ovals']: |
128
|
|
|
raise ValueError("Already have shared oval for rule_id:%s; refusing " |
129
|
|
|
"to continue." % rule_obj['id']) |
130
|
|
|
|
131
|
|
|
oval_file = ssg.checks.get_oval_path(rule_obj, products[0]) |
132
|
|
|
shared_oval_file = os.path.join(rule_obj['dir'], 'oval', 'shared.xml') |
133
|
|
|
os.rename(oval_file, shared_oval_file) |
134
|
|
|
print("Moved %s -> %s" % (oval_file, shared_oval_file)) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def diff_ovals(rule_obj, products): |
138
|
|
|
if not products or len(products) != 2 or products[0] == products[1]: |
139
|
|
|
raise ValueError("Must pass exactly two products for the diff option.") |
140
|
|
|
if 'ovals' not in rule_obj: |
141
|
|
|
raise ValueError("Rule is missing all ovals.") |
142
|
|
|
|
143
|
|
|
left_oval_file = ssg.checks.get_oval_path(rule_obj, products[0]) |
144
|
|
|
right_oval_file = ssg.checks.get_oval_path(rule_obj, products[1]) |
145
|
|
|
|
146
|
|
|
subprocess.run(['diff', '--color=always', left_oval_file, right_oval_file]) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def main(): |
150
|
|
|
args = parse_args() |
151
|
|
|
|
152
|
|
|
json_file = open(args.json, 'r') |
153
|
|
|
known_rules = json.load(json_file) |
154
|
|
|
|
155
|
|
|
if not args.rule_id in known_rules: |
156
|
|
|
print("Error: rule_id:%s is not known!" % args.rule_id, file=sys.stderr) |
157
|
|
|
print("If you think this is an error, try regenerating the JSON.", file=sys.stderr) |
158
|
|
|
sys.exit(1) |
159
|
|
|
|
160
|
|
|
if args.action != "list" and not args.products: |
161
|
|
|
print("Error: expected a list of products or replace transformations but " |
162
|
|
|
"none given.", file=sys.stderr) |
163
|
|
|
sys.exit(1) |
164
|
|
|
|
165
|
|
|
rule_obj = known_rules[args.rule_id] |
166
|
|
|
print("rule_id:%s\n" % args.rule_id) |
167
|
|
|
|
168
|
|
|
if args.action == "list": |
169
|
|
|
list_platforms(rule_obj) |
170
|
|
|
elif args.action == "add": |
171
|
|
|
add_platforms(rule_obj, args.products) |
172
|
|
|
elif args.action == "remove": |
173
|
|
|
remove_platforms(rule_obj, args.products) |
174
|
|
|
elif args.action == "replace": |
175
|
|
|
replace_platforms(rule_obj, args.products) |
176
|
|
|
elif args.action == 'delete': |
177
|
|
|
delete_ovals(rule_obj, args.products) |
178
|
|
|
elif args.action == 'make_shared': |
179
|
|
|
make_shared_oval(rule_obj, args.products) |
180
|
|
|
elif args.action == 'diff': |
181
|
|
|
diff_ovals(rule_obj, args.products) |
182
|
|
|
else: |
183
|
|
|
print("Unknown option: %s" % args.action) |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
if __name__ == "__main__": |
187
|
|
|
main() |
188
|
|
|
|