File: oscap-docker.in

package info (click to toggle)
openscap 1.4.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,040 kB
  • sloc: xml: 527,109; ansic: 91,390; sh: 19,789; python: 2,515; perl: 444; makefile: 49
file content (96 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (2)
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
#!@OSCAP_DOCKER_PYTHON@

# Copyright (C) 2015 Brent Baude <bbaude@redhat.com>
# Copyright (C) 2019 Dominique Blaze <contact@d0m.tech>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

''' oscap docker command '''

import argparse
from oscap_docker_python.oscap_docker_util import OscapDockerScan

import docker
import traceback
import sys
from requests import exceptions


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='oscap docker',
                                     epilog='See `man oscap` to learn \
                                     more about OSCAP-ARGUMENTS')
    parser.add_argument('--oscap', dest='oscap_binary', default='',
                        help='Set the oscap binary to use')
    subparser = parser.add_subparsers(help="commands")

    # Scan CVEs in image
    image_cve = subparser.add_parser('image-cve', help='Scan a docker image \
                                    for known vulnerabilities.')
    image_cve.set_defaults(action="scan_cve", is_image=True)
    image_cve.add_argument('scan_target', help='Container or image to scan')

    # Scan an Image
    image = subparser.add_parser('image', help='Scan a docker image')
    image.add_argument('scan_target',
                       help='Container or image to scan')

    image.set_defaults(action="scan", is_image=True)
    # Scan a container
    container = subparser.add_parser('container', help='Scan a running docker\
                                      container of given name.')
    container.add_argument('scan_target',
                           help='Container or image to scan')
    container.set_defaults(action="scan", is_image=False)

    # Scan CVEs in container
    container_cve = subparser.add_parser('container-cve', help='Scan a \
                                         running container for known \
                                         vulnerabilities.')

    container_cve.set_defaults(action="scan_cve", is_image=False)
    container_cve.add_argument('scan_target',
                               help='Container or image to scan')

    args, leftover_args = parser.parse_known_args()

    if "action" not in args:
        parser.print_help()
        sys.exit(2)

    try:
        ODS = OscapDockerScan(args.scan_target, args.is_image, args.oscap_binary)
        if args.action == "scan":
            rc = OscapDockerScan.scan(ODS, leftover_args)
        elif args.action == "scan_cve":
            rc = OscapDockerScan.scan_cve(ODS, leftover_args)
        else:
            parser.print_help()
            sys.exit(2)

    except (ValueError, RuntimeError) as e:
        raise e
        sys.exit(255)
    except(FileNotFoundError) as e:
        sys.stderr.write("Target {0} not found.\n".format(target))
        sys.exit(255)
    except Exception as exc:
        traceback.print_exc(file=sys.stdout)
        sys.stderr.write("!!! WARNING !!! This software has crashed, so you should "
                         "check that no temporary container is still running\n")
        sys.exit(255)

    sys.exit(rc)