Skip to content

Commit 27884b2

Browse files
adapt oscap-docker.in + credits
1 parent 385ca19 commit 27884b2

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.libs/
66
.*.swp
77
tags
8+
*.pyc
89

910
CMakeLists.txt.user
1011
build/

utils/oscap-docker.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!@OSCAP_DOCKER_PYTHON@
22

33
# Copyright (C) 2015 Brent Baude <[email protected]>
4+
# Copyright (C) 2019 Dominique Blaze <[email protected]>
45
#
56
# This library is free software; you can redistribute it and/or
67
# modify it under the terms of the GNU Lesser General Public

utils/oscap-docker.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!@OSCAP_DOCKER_PYTHON@
2+
3+
# Copyright (C) 2015 Brent Baude <[email protected]>
4+
# Copyright (C) 2019 Dominique Blaze <[email protected]>
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the
18+
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19+
# Boston, MA 02111-1307, USA.
20+
21+
''' oscap docker command '''
22+
23+
import argparse
24+
from oscap_docker_python.oscap_docker_util import OscapAtomicScan,\
25+
OscapDockerScan, isAtomicLoaded
26+
import docker
27+
import sys
28+
from requests import exceptions
29+
30+
31+
def ping_docker():
32+
''' Simple check if the docker daemon is running '''
33+
# Class docker.Client was renamed to docker.APIClient in
34+
# python-docker-py 2.0.0.
35+
try:
36+
client = docker.APIClient()
37+
except AttributeError:
38+
client = docker.Client()
39+
client.ping()
40+
41+
42+
if __name__ == '__main__':
43+
parser = argparse.ArgumentParser(description='oscap docker',
44+
epilog='See `man oscap` to learn \
45+
more about OSCAP-ARGUMENTS')
46+
parser.add_argument('--oscap', dest='oscap_binary', default='', help='Set the oscap binary to use')
47+
parser.add_argument('--disable-atomic', dest='noatomic', action='store_true', help="Force to use native docker API instead of atomic")
48+
subparser = parser.add_subparsers(help="commands")
49+
50+
# Scan CVEs in image
51+
image_cve = subparser.add_parser('image-cve', help='Scan a docker image \
52+
for known vulnerabilities.')
53+
image_cve.set_defaults(action="scan_cve", is_image=True)
54+
image_cve.add_argument('scan_target', help='Container or image to scan')
55+
56+
# Scan an Image
57+
image = subparser.add_parser('image', help='Scan a docker image')
58+
image.add_argument('scan_target',
59+
help='Container or image to scan')
60+
61+
image.set_defaults(action="scan", is_image=True)
62+
# Scan a container
63+
container = subparser.add_parser('container', help='Scan a running docker\
64+
container of given name.')
65+
container.add_argument('scan_target',
66+
help='Container or image to scan')
67+
container.set_defaults(action="scan", is_image=False)
68+
69+
# Scan CVEs in container
70+
container_cve = subparser.add_parser('container-cve', help='Scan a \
71+
running container for known \
72+
vulnerabilities.')
73+
74+
container_cve.set_defaults(action="scan_cve", is_image=False)
75+
container_cve.add_argument('scan_target',
76+
help='Container or image to scan')
77+
78+
args, leftover_args = parser.parse_known_args()
79+
80+
if "action" not in args:
81+
parser.print_help()
82+
sys.exit(2)
83+
84+
try:
85+
ping_docker()
86+
87+
except exceptions.ConnectionError:
88+
print("The docker daemon does not appear to be running")
89+
sys.exit(1)
90+
91+
try:
92+
if isAtomicLoaded and not args.noatomic:
93+
OS = OscapAtomicScan(oscap_binary=args.oscap_binary)
94+
if args.action == "scan":
95+
rc = OscapAtomicScan.scan(OS, args.scan_target, leftover_args)
96+
elif args.action == "scan_cve":
97+
rc = OscapAtomicScan.scan_cve(OS, args.scan_target, leftover_args)
98+
else:
99+
parser.print_help()
100+
sys.exit(2)
101+
102+
else: # without atomic
103+
if args.noatomic:
104+
print("Running oscap-docker with native docker api instead of atomic ...")
105+
106+
ODS = OscapDockerScan(args.scan_target, args.is_image, args.oscap_binary)
107+
if args.action == "scan":
108+
rc = OscapDockerScan.scan(ODS, leftover_args)
109+
elif args.action == "scan_cve":
110+
print("Scan cve !")
111+
rc = OscapDockerScan.scan_cve(ODS, leftover_args)
112+
else:
113+
parser.print_help()
114+
sys.exit(2)
115+
116+
except ValueError as e:
117+
raise e
118+
sys.exit(255)
119+
except RuntimeError as e:
120+
raise e
121+
sys.exit(255)
122+
except Exception as exc:
123+
traceback.print_exc(file=sys.stdout)
124+
sys.stderr.write("!!! WARNING !!! This software have crash, so you should "
125+
"check that no temporary container is still running\n")
126+
sys.exit(255)
127+
128+
sys.exit(rc)

utils/oscap_docker_python/oscap_docker_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (C) 2015 Brent Baude <[email protected]>
2+
# Copyright (C) 2019 Dominique Blaze <[email protected]>
23
#
34
# This library is free software; you can redistribute it and/or
45
# modify it under the terms of the GNU Lesser General Public
@@ -28,7 +29,7 @@
2829
import sys
2930
import docker
3031
import collections
31-
from oscap_docker_util_noatomic import OscapDockerScan
32+
from oscap_docker_python.oscap_docker_util_noatomic import OscapDockerScan
3233

3334
atomic_loaded = False
3435

@@ -76,6 +77,7 @@ def __init__(self, message):
7677
"Failed to import \"Atomic.mount.DockerMount\". It seems Atomic has "
7778
"not been installed.\n"
7879
)
80+
7981
except AtomicError as err:
8082
sys.stderr.write(err.message)
8183

utils/oscap_docker_python/oscap_docker_util_noatomic.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# Copyright (C) 2015 Brent Baude <[email protected]>
2+
# Copyright (C) 2019 Dominique Blaze <[email protected]>
3+
#
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation; either
7+
# version 2 of the License, or (at your option) any later version.
8+
#
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Lesser General Public
15+
# License along with this library; if not, write to the
16+
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17+
# Boston, MA 02111-1307, USA.
18+
119
from __future__ import print_function
220

321
import os
@@ -9,6 +27,15 @@
927
import sys
1028
import docker
1129
import uuid
30+
import collections
31+
32+
33+
class OscapError(Exception):
34+
''' oscap Error'''
35+
pass
36+
37+
38+
OscapResult = collections.namedtuple("OscapResult", ("returncode", "stdout", "stderr"))
1239

1340

1441
class OscapDockerScan(object):
@@ -163,6 +190,7 @@ def scan_cve(self, scan_args):
163190
'''
164191
Wrapper function for scanning cve of a mounted container
165192
'''
193+
166194
tmp_dir = tempfile.mkdtemp()
167195

168196
# Figure out which RHEL dist is in the chroot

0 commit comments

Comments
 (0)