This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgenerate-dockerfile.py
executable file
·401 lines (322 loc) · 14.7 KB
/
generate-dockerfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/python
import argparse
import collections
import contextlib
INDENTATION = " "
COMMAND_DELIMITER = " \\\n{}&& ".format(INDENTATION)
labels = [
("com.redhat.component", "openscap-container"),
("name", "openscap"),
("version", "testing"),
("usage", "The OpenSCAP container image is used by the Atomic Management Tool. See 'atomic scan --help' for usage summary."),
("architecture", "x86_64"),
("summary", "OpenSCAP container image that provides security/compliance scanning capabilities for 'atomic scan'"),
("description", "This image can be used by 'atomic scan' to perform two types of scans: Scanning for vulnerabilities using RHEL CVE feeds, which are already part of the image, informs you about installed applications that have known security issues. Scanning for configuration compliance can confirm that the scanned system complies to a given security profile. In cases that it does not comply, you can try to fix the failing rules by passing '--remediate' as an atomic scan argument."),
("io.k8s.display-name", "OpenSCAP"),
("io.k8s.description", "This image can be used by 'atomic scan' to perform two types of scans: Scanning for vulnerabilities using RHEL CVE feeds, which are already part of the image, informs you about installed applications that have known security issues. Scanning for configuration compliance can confirm that the scanned system complies to a given security profile. In cases that it does not comply, you can try to fix the failing rules by passing '--remediate' as an atomic scan argument."),
("io.openshift.tags", "security openscap scan"),
("install", "docker run --rm --privileged -v /:/host/ IMAGE sh /root/install.sh IMAGE"),
("run", "docker run -it --rm -v /:/host/ IMAGE sh /root/run.sh"),
("help", "docker run -it --rm IMAGE sh /root/help.sh"),
]
packages = {
"openssh-clients",
"wget",
"bzip2",
}
files = [
("container/install.sh", "/root/"),
("container/run.sh", "/root/"),
("container/openscap", "/root/"),
("container/config.ini", "/root/"),
("container/remediate.py", "/root/"),
("container/help.sh", "/root/"),
("Dockerfile", "/root/"),
]
env_variables = [
("container", "oci")
]
download_cve_feeds_command = [
"wget --no-verbose -P /var/lib/oscapd/cve_feeds/ "
"https://www.redhat.com/security/data/oval/com.redhat.rhsa-RHEL{5,6,7,8}.xml.bz2",
"bzip2 -dk /var/lib/oscapd/cve_feeds/com.redhat.rhsa-RHEL{5,6,7,8}.xml.bz2",
"ln -s /var/lib/oscapd/cve_feeds/ /var/tmp/image-scanner",
]
openscap_build_command = [
"git clone -b maint-1.2 https://github.com/OpenSCAP/openscap.git",
"pushd /openscap",
"./autogen.sh",
"./configure --enable-sce --prefix=/usr",
"make -j 4 install",
"popd",
]
ssg_build_command = [
"git clone https://github.com/OpenSCAP/scap-security-guide.git",
"pushd /scap-security-guide/build",
"cmake -DCMAKE_INSTALL_DATADIR=/usr/share ..",
"make -j 4 install",
"popd",
]
daemon_local_build_command = [
"pushd /openscap-daemon",
"python setup.py install",
"popd",
]
def make_parser():
parser = argparse.ArgumentParser(description="Builds an image with OpenSCAP Daemon")
openscap_group = parser.add_mutually_exclusive_group(required=False)
parser.add_argument(
"--base", type=str, default="fedora",
help="Base image name (default is fedora)")
openscap_group.add_argument(
"--openscap-from-git", action="store_true",
default=False, help="Use OpenSCAP from upstream instead of package")
openscap_group.add_argument(
"--openscap-from-koji", type=str,
help="Use OpenSCAP from Koji based on build ID (Fedora only)")
ssg_group = parser.add_mutually_exclusive_group(required=False)
ssg_group.add_argument(
"--ssg-from-koji", type=str,
help="Use SCAP Security Guide from Koji based on build ID (Fedora only)")
ssg_group.add_argument(
"--ssg-from-git", action="store_true", default=False,
help="Use SCAP Security Guide from upstream instead of package")
daemon_group = parser.add_mutually_exclusive_group(required=False)
daemon_group.add_argument(
"--daemon-from-local", action="store_true", default=False,
help="Use OpenSCAP Daemon from local working tree instead of package")
daemon_group.add_argument(
"--daemon-from-koji", type=str,
help="Use OpenSCAP Daemon from Koji based on build ID (Fedora only)")
return parser
def output_baseimage_line(baseimage_name):
return "FROM {0}\n\n".format(baseimage_name)
def output_labels_lines(label_value_pairs):
label_value_lines = [
'{}="{}"'.format(label, value)
for label, value in label_value_pairs]
label_value_lines = ['LABEL'] + label_value_lines
label_statement = " \\\n{}".format(INDENTATION).join(label_value_lines)
return label_statement
def output_env_lines(env_value_pairs):
envvar_value_lines = [
'{}="{}"'.format(envvar, value)
for envvar, value in env_value_pairs]
envvar_value_lines = ['ENV'] + envvar_value_lines
env_statement = " \\\n{}".format(INDENTATION).join(envvar_value_lines)
return env_statement
def _aggregate_by_destination(src_dest_pairs):
destinations = collections.defaultdict(set)
for src, dest in src_dest_pairs:
destinations[dest].add(src)
return destinations
def _output_copy_lines_for_destination(sources, destination):
elements = ['COPY'] + list(sources) + [destination]
if len(sources) == 1:
copy_statement = " ".join(elements)
else:
copy_statement = " \\\n{}".format(INDENTATION).join(elements)
return copy_statement
def output_copy_lines(src_dest_pairs):
destinations = _aggregate_by_destination(src_dest_pairs)
copy_statements = []
for dest, sources in destinations.items():
statement = _output_copy_lines_for_destination(sources, dest)
copy_statements.append(statement)
return "\n".join(copy_statements)
class PackageEnv(object):
def __init__(self):
self.install_command_beginning = None
self.remove_command_beginning = None
self.clear_cache = None
self.builddep_package = None
self.builddep_command_beginning = None
self.additional_repositories_were_enabled = False
def _assert_class_is_complete(self):
assert (
self.install_command_beginning is not None
and self.remove_command_beginning is not None
and self.clear_cache is not None
and self.builddep_package is not None
and self.builddep_command_beginning is not None
), "The class {} is not complete, use a fully defined child."
def install_command_element(self, packages_string):
return "{} {}".format(self.install_command_beginning, packages_string)
def remove_command_element(self, packages_string):
return "{} {}".format(self.remove_command_beginning, packages_string)
def _enable_additional_repositories_command_element(self):
return []
def get_enable_additional_repositories_command_element(self):
if not self.additional_repositories_were_enabled:
return self._enable_additional_repositories_command_element()
else:
return []
self.additional_repositories_were_enabled = True
def _get_install_commands(self, packages_string):
self._assert_class_is_complete()
commands = self.get_enable_additional_repositories_command_element()
commands.append(self.install_command_element(packages_string))
return commands
@contextlib.contextmanager
def install_then_clean_all(self, packages_string):
commands = self._get_install_commands(packages_string)
yield commands
commands.append(self.clear_cache)
@contextlib.contextmanager
def install_then_remove(self, packages_string, clear_cache_afterwards=False):
commands = self._get_install_commands(packages_string)
yield commands
commands.append(self.remove_command_element(packages_string))
if clear_cache_afterwards:
commands.append(self.clear_cache)
class RhelEnv(PackageEnv):
def __init__(self):
super(RhelEnv, self).__init__()
self.install_command_beginning = "yum install -y"
self.remove_command_beginning = "yum remove -y"
self.clear_cache = "yum clean all"
self.builddep_command_beginning = "yum-builddep -y"
self.builddep_package = "yum-utils"
def _enable_additional_repositories_command_element(self):
commands = super(RhelEnv, self)._enable_additional_repositories_command_element()
commands.append(
"rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm")
return commands
class FedoraEnv(PackageEnv):
def __init__(self):
super(FedoraEnv, self).__init__()
self.install_command_beginning = "dnf install -y"
self.remove_command_beginning = "dnf remove -y"
self.clear_cache = "dnf clean all"
self.builddep_command_beginning = "dnf -y builddep"
self.builddep_package = "'dnf-command(builddep)'"
def choose_pkg_env_class(baseimage):
if baseimage.startswith("fedora"):
return FedoraEnv
else:
return RhelEnv
class TasksRecorder(object):
def __init__(self, builddep_package):
self.builddep_package = builddep_package
self._build_from_source = []
self._build_commands = []
self._install_from_koji = []
self._koji_commands = []
def merge(self, rhs):
self._build_from_source.extend(rhs._build_from_source)
self._build_commands.extend(rhs._build_commands)
self._koji_commands.extend(rhs._koji_commands)
self._install_from_koji.extend(rhs._install_from_koji)
def build_from_source(self, what, how=None):
packages.add(self.builddep_package)
self._build_from_source.append(what)
if how is not None:
self._build_commands.extend(how)
def install_from_koji(self, what, how=None):
self._install_from_koji.append(what)
if how is not None:
self._koji_commands.extend(how)
def install_build_deps(self, builddep_command):
if len(self._build_from_source) == 0:
return []
build_deps_string = " ".join(self._build_from_source)
command = "{0} {1}".format(builddep_command, build_deps_string)
return [command]
def add_commands_for_building_from_custom_sources(self):
return self._build_commands
def add_koji_commands(self):
return self._koji_commands
def decide_about_getting_openscap(args, pkg_env):
tasks = TasksRecorder(pkg_env.builddep_package)
if args.openscap_from_git:
packages.update({"git", "libtool", "automake"})
tasks.build_from_source("openscap", openscap_build_command)
elif args.openscap_from_koji is not None:
packages.add("koji")
openscap_koji_command = [
"koji download-build -a x86_64 {0}".format(args.openscap_from_koji),
"koji download-build -a noarch {0}".format(args.openscap_from_koji),
pkg_env.install_command_element(
"openscap-[0-9]*.rpm openscap-scanner*.rpm "
"openscap-utils*.rpm openscap-containers*.rpm"),
"rm -f openscap-*.rpm",
]
tasks.install_from_koji("openscap", openscap_koji_command)
else:
packages.add("openscap-utils")
return tasks
def decide_about_getting_ssg(args, pkg_env):
tasks = TasksRecorder(pkg_env.builddep_package)
if args.ssg_from_git:
packages.add("git")
tasks.build_from_source("scap-security-guide", ssg_build_command)
elif args.ssg_from_koji is not None:
packages.add("koji")
ssg_koji_command = [
"koji download-build -a noarch {0}".format(args.ssg_from_koji),
pkg_env.install_command_element("scap-security-guide-[0-9]*.rpm"),
"rm -f scap-security-guide*.rpm",
]
tasks.install_from_koji("scap-security-guide", ssg_koji_command)
else:
packages.add("scap-security-guide")
return tasks
def decide_about_getting_openscap_daemon(args, pkg_env):
tasks = TasksRecorder(pkg_env.builddep_package)
if args.daemon_from_local:
tasks.build_from_source("openscap-daemon", daemon_local_build_command)
files.append((".", "/openscap-daemon/"))
elif args.daemon_from_koji is not None:
packages.add("koji")
daemon_koji_command = [
"koji download-build -a noarch {0}".format(args.daemon_from_koji),
pkg_env.install_command_element("openscap-daemon*.rpm"),
"rm -f openscap-daemon*.rpm",
]
tasks.install_from_koji("openscap-daemon", daemon_koji_command)
else:
packages.add("openscap-daemon")
return tasks
def output_run_directive(commands):
commands_string = COMMAND_DELIMITER.join(["true"] + commands + ["true"])
return "RUN {}\n\n".format(commands_string)
def main():
parser = make_parser()
args = parser.parse_args()
pkg_env = choose_pkg_env_class(args.base)()
if (not isinstance(pkg_env, FedoraEnv)) and (
args.openscap_from_koji is not None
or args.ssg_from_koji is not None
or args.daemon_from_koji is not None):
parser.error("Koji builds can be used only with fedora base image")
with open("Dockerfile", "w") as f:
# write out the Dockerfile
f.write(output_baseimage_line(args.base))
f.write(output_labels_lines(labels))
f.write("\n\n")
f.write(output_env_lines(env_variables))
f.write("\n\n")
install_steps = decide_about_getting_openscap(args, pkg_env)
install_steps.merge(decide_about_getting_ssg(args, pkg_env))
install_steps.merge(decide_about_getting_openscap_daemon(args, pkg_env))
# inject files
f.write(output_copy_lines(files))
f.write("\n\n")
run_commands = []
packages_string = " ".join(packages)
with pkg_env.install_then_clean_all(packages_string) as commands:
commands.extend(
install_steps.install_build_deps(pkg_env.builddep_command_beginning))
commands.extend(
install_steps.add_commands_for_building_from_custom_sources())
commands.extend(
install_steps.add_koji_commands())
run_commands.extend(commands)
f.write(output_run_directive(run_commands))
f.write(output_run_directive(download_cve_feeds_command))
# add CMD instruction to the Dockerfile, including a comment
f.write("# It doesn't matter what is in the line below, atomic will change the CMD\n")
f.write("# before running it\n")
f.write('CMD ["/root/run.sh"]\n')
if __name__ == "__main__":
main()