|
| 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) |
0 commit comments