blob: f45e3ee0e2368f21b042ba1e95c6f20c7a73d80a [file] [log] [blame]
Alex Kleinfa719c92018-10-15 14:04:22 -06001# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This script provides CLI access to run security tests on a Chrome OS images.
6
7The entry point is available as image_lib.SecurityTest. Call that directly when
8possible.
9
10Note: You probably will need an internal checkout by default for these
11 tests to be useful. You can provide your own baselines, but you
12 can certainly provide your own set of configs.
13
14Note: These tests will fail on dev images. They are designed to
15 check release recovery images only.
16
17Note: The --image argument can be a path or a basename. When a basename is
18 provided, the --board argument is always used to build the path.
19 Consequently, `./image_name.bin` and `image_name.bin` are treated
20 very differently.
21"""
22
Alex Kleinfa719c92018-10-15 14:04:22 -060023import re
24
25from chromite.lib import commandline
26from chromite.lib import cros_build_lib
27from chromite.lib import image_lib
28
29
30def GetParser():
31 """Build the Argument Parser."""
32 parser = commandline.ArgumentParser(description=__doc__)
33
34 parser.add_argument('--board', help='The board to test an image for.')
35 # Avoiding type='path' to allow the use of `./` to distinguish between a
36 # local image (e.g. `./image_name.bin`) and a basename (`image_name.bin`) in
37 # the board's build directory. The `./` would be normalized out of a
38 # type='path' argument, making it look like it's a basename.
39 parser.add_argument('--image',
40 help='Source release image to use (recovery_image.bin by '
41 'default). May be a path to an image or just the '
42 'basename of the image if a board is also provided.')
43 parser.add_argument('--baselines', type='path',
44 help='Directory to load security baselines from (default '
45 'from cros-signing).')
46 parser.add_argument('--vboot-hash',
47 help='The git rev of the vboot tree to checkout (default '
48 'to the signer hash).')
49
50 return parser
51
52
53def _ParseArgs(argv):
54 """Parse and validate arguments."""
55 parser = GetParser()
56 opts = parser.parse_args(argv)
57
58 # Need the board if no image provided or only the basename is provided so
59 # we can build out the full path to an image file.
60 opts.board = opts.board or cros_build_lib.GetDefaultBoard()
61 try:
62 opts.image = image_lib.BuildImagePath(opts.board, opts.image)
63 except image_lib.ImageDoesNotExistError as e:
64 # Replace |arg| with --arg, otherwise messages still relevant.
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040065 message = re.sub(r'\|(\w+)\|', r'--\1', str(e))
Alex Kleinfa719c92018-10-15 14:04:22 -060066 parser.error(message)
67
68 opts.Freeze()
69 return opts
70
71
72def main(argv):
73 cros_build_lib.AssertInsideChroot()
74 opts = _ParseArgs(argv)
75 try:
76 success = image_lib.SecurityTest(board=opts.board, image=opts.image,
77 baselines=opts.baselines,
78 vboot_hash=opts.vboot_hash)
79 except image_lib.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040080 cros_build_lib.Die(e)
Alex Kleinfa719c92018-10-15 14:04:22 -060081 else:
82 return 0 if success else 1