| 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., 51 Franklin Street, Fifth Floor, |
| 19 |
# Boston, MA 02110-1301 USA |
| 20 |
|
| 21 |
''' oscap docker command ''' |
| 22 |
|
| 23 |
import argparse |
| 24 |
from oscap_docker_python.oscap_docker_util import OscapDockerScan |
| 25 |
|
| 26 |
import docker |
| 27 |
import traceback |
| 28 |
import sys |
| 29 |
from requests import exceptions |
| 30 |
|
| 31 |
|
| 32 |
if __name__ == '__main__': |
| 33 |
parser = argparse.ArgumentParser(description='oscap docker', |
| 34 |
epilog='See `man oscap` to learn \ |
| 35 |
more about OSCAP-ARGUMENTS') |
| 36 |
parser.add_argument('--oscap', dest='oscap_binary', default='', |
| 37 |
help='Set the oscap binary to use') |
| 38 |
subparser = parser.add_subparsers(help="commands") |
| 39 |
|
| 40 |
# Scan CVEs in image |
| 41 |
image_cve = subparser.add_parser('image-cve', help='Scan a docker image \ |
| 42 |
for known vulnerabilities.') |
| 43 |
image_cve.set_defaults(action="scan_cve", is_image=True) |
| 44 |
image_cve.add_argument('scan_target', help='Container or image to scan') |
| 45 |
|
| 46 |
# Scan an Image |
| 47 |
image = subparser.add_parser('image', help='Scan a docker image') |
| 48 |
image.add_argument('scan_target', |
| 49 |
help='Container or image to scan') |
| 50 |
|
| 51 |
image.set_defaults(action="scan", is_image=True) |
| 52 |
# Scan a container |
| 53 |
container = subparser.add_parser('container', help='Scan a running docker\ |
| 54 |
container of given name.') |
| 55 |
container.add_argument('scan_target', |
| 56 |
help='Container or image to scan') |
| 57 |
container.set_defaults(action="scan", is_image=False) |
| 58 |
|
| 59 |
# Scan CVEs in container |
| 60 |
container_cve = subparser.add_parser('container-cve', help='Scan a \ |
| 61 |
running container for known \ |
| 62 |
vulnerabilities.') |
| 63 |
|
| 64 |
container_cve.set_defaults(action="scan_cve", is_image=False) |
| 65 |
container_cve.add_argument('scan_target', |
| 66 |
help='Container or image to scan') |
| 67 |
|
| 68 |
args, leftover_args = parser.parse_known_args() |
| 69 |
|
| 70 |
if "action" not in args: |
| 71 |
parser.print_help() |
| 72 |
sys.exit(2) |
| 73 |
|
| 74 |
try: |
| 75 |
ODS = OscapDockerScan(args.scan_target, args.is_image, args.oscap_binary) |
| 76 |
if args.action == "scan": |
| 77 |
rc = OscapDockerScan.scan(ODS, leftover_args) |
| 78 |
elif args.action == "scan_cve": |
| 79 |
rc = OscapDockerScan.scan_cve(ODS, leftover_args) |
| 80 |
else: |
| 81 |
parser.print_help() |
| 82 |
sys.exit(2) |
| 83 |
|
| 84 |
except (ValueError, RuntimeError) as e: |
| 85 |
raise e |
| 86 |
sys.exit(255) |
| 87 |
except(FileNotFoundError) as e: |
| 88 |
sys.stderr.write("Target {0} not found.\n".format(target)) |
| 89 |
sys.exit(255) |
| 90 |
except Exception as exc: |
| 91 |
traceback.print_exc(file=sys.stdout) |
| 92 |
sys.stderr.write("!!! WARNING !!! This software has crashed, so you should " |
| 93 |
"check that no temporary container is still running\n") |
| 94 |
sys.exit(255) |
| 95 |
|
| 96 |
sys.exit(rc) |