1 | #!/usr/bin/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(): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
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 | |||
64 | if "multi_platform_all" in current_platforms: |
||
65 | return |
||
66 | |||
67 | new_platforms = set(current_platforms) |
||
68 | new_platforms.update(platforms) |
||
69 | |||
70 | print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
||
71 | print("New platforms: %s" % ','.join(sorted(new_platforms))) |
||
72 | |||
73 | new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
||
74 | new_platforms) |
||
75 | ssg.utils.write_list_file(oval_file, new_contents) |
||
76 | |||
77 | |||
78 | def remove_platforms(rule_obj, platforms): |
||
79 | oval_file, oval_contents = ssg.checks.get_oval_contents(rule_obj, 'shared') |
||
80 | current_platforms = ssg.oval.applicable_platforms(oval_file) |
||
81 | new_platforms = set(current_platforms).difference(platforms) |
||
82 | |||
83 | print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
||
84 | print("New platforms: %s" % ','.join(sorted(new_platforms))) |
||
85 | |||
86 | new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
||
87 | new_platforms) |
||
88 | ssg.utils.write_list_file(oval_file, new_contents) |
||
89 | |||
90 | |||
91 | View Code Duplication | def replace_platforms(rule_obj, platforms): |
|
0 ignored issues
–
show
|
|||
92 | oval_file, oval_contents = ssg.checks.get_oval_contents(rule_obj, 'shared') |
||
93 | current_platforms = ssg.oval.applicable_platforms(oval_file) |
||
94 | new_platforms = set(current_platforms) |
||
95 | |||
96 | for platform in platforms: |
||
97 | parsed_platform = platform.split('~') |
||
98 | if not len(parsed_platform) == 2: |
||
99 | print("Invalid platform replacement description: %s" % platform, |
||
100 | file=sys.stderr) |
||
101 | sys.exit(1) |
||
102 | |||
103 | match = ssg.rule_yaml.parse_prodtype(parsed_platform[0]) |
||
104 | replacement = ssg.rule_yaml.parse_prodtype(parsed_platform[1]) |
||
105 | |||
106 | if match.issubset(current_platforms): |
||
107 | new_platforms.difference_update(match) |
||
108 | new_platforms.update(replacement) |
||
109 | |||
110 | print("Current platforms: %s" % ','.join(sorted(current_platforms))) |
||
111 | print("New platforms: %s" % ','.join(sorted(new_platforms))) |
||
112 | |||
113 | new_contents = ssg.checks.set_applicable_platforms(oval_contents, |
||
114 | new_platforms) |
||
115 | ssg.utils.write_list_file(oval_file, new_contents) |
||
116 | |||
117 | |||
118 | def delete_ovals(rule_obj, products): |
||
119 | for product in products: |
||
120 | oval_file = ssg.checks.get_oval_path(rule_obj, product) |
||
121 | os.remove(oval_file) |
||
122 | print("Removed: %s" % oval_file) |
||
123 | |||
124 | |||
125 | def make_shared_oval(rule_obj, products): |
||
126 | if not products or len(products) > 1: |
||
127 | raise ValueError("Must pass exactly one product for the make_shared option.") |
||
128 | if 'ovals' not in rule_obj: |
||
129 | raise ValueError("Rule is missing all ovals.") |
||
130 | |||
131 | if 'shared.xml' in rule_obj['ovals']: |
||
132 | raise ValueError("Already have shared oval for rule_id:%s; refusing " |
||
133 | "to continue." % rule_obj['id']) |
||
134 | |||
135 | oval_file = ssg.checks.get_oval_path(rule_obj, products[0]) |
||
136 | shared_oval_file = os.path.join(rule_obj['dir'], 'oval', 'shared.xml') |
||
137 | os.rename(oval_file, shared_oval_file) |
||
138 | print("Moved %s -> %s" % (oval_file, shared_oval_file)) |
||
139 | |||
140 | |||
141 | def diff_ovals(rule_obj, products): |
||
142 | if not products or len(products) != 2 or products[0] == products[1]: |
||
143 | raise ValueError("Must pass exactly two products for the diff option.") |
||
144 | if 'ovals' not in rule_obj: |
||
145 | raise ValueError("Rule is missing all ovals.") |
||
146 | |||
147 | left_oval_file = ssg.checks.get_oval_path(rule_obj, products[0]) |
||
148 | right_oval_file = ssg.checks.get_oval_path(rule_obj, products[1]) |
||
149 | |||
150 | subprocess.run(['diff', '--color=always', left_oval_file, right_oval_file]) |
||
151 | |||
152 | |||
153 | def main(): |
||
154 | args = parse_args() |
||
155 | |||
156 | json_file = open(args.json, 'r') |
||
157 | known_rules = json.load(json_file) |
||
158 | |||
159 | if args.rule_id not in known_rules: |
||
160 | print("Error: rule_id:%s is not known!" % args.rule_id, file=sys.stderr) |
||
161 | print("If you think this is an error, try regenerating the JSON.", file=sys.stderr) |
||
162 | sys.exit(1) |
||
163 | |||
164 | if args.action != "list" and not args.products: |
||
165 | print("Error: expected a list of products or replace transformations but " |
||
166 | "none given.", file=sys.stderr) |
||
167 | sys.exit(1) |
||
168 | |||
169 | rule_obj = known_rules[args.rule_id] |
||
170 | print("rule_id:%s\n" % args.rule_id) |
||
171 | |||
172 | if args.action == "list": |
||
173 | list_platforms(rule_obj) |
||
174 | elif args.action == "add": |
||
175 | add_platforms(rule_obj, args.products) |
||
176 | elif args.action == "remove": |
||
177 | remove_platforms(rule_obj, args.products) |
||
178 | elif args.action == "replace": |
||
179 | replace_platforms(rule_obj, args.products) |
||
180 | elif args.action == 'delete': |
||
181 | delete_ovals(rule_obj, args.products) |
||
182 | elif args.action == 'make_shared': |
||
183 | make_shared_oval(rule_obj, args.products) |
||
184 | elif args.action == 'diff': |
||
185 | diff_ovals(rule_obj, args.products) |
||
186 | else: |
||
187 | print("Unknown option: %s" % args.action) |
||
188 | |||
189 | |||
190 | if __name__ == "__main__": |
||
191 | main() |
||
192 |