| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import argparse |
| 15 |
import logging |
| 16 |
import os |
| 17 |
import sys |
| 18 |
import textwrap |
| 19 |
|
| 20 |
from oslo_utils.imageutils import format_inspector |
| 21 |
from oslo_utils.version import version_info |
| 22 |
|
| 23 |
|
| 24 |
def main() -> None: |
| 25 |
"""Run image security checks and give feedback. |
| 26 |
|
| 27 |
Runs the image format detector and related security checks against |
| 28 |
a provided image. |
| 29 |
|
| 30 |
Usage: |
| 31 |
python -m oslo_utils.imageutils -i /path/to/image [-v|--verbose] |
| 32 |
|
| 33 |
Default behavior is to communicate status via exit code: |
| 34 |
- Exit code of 0 indicates a safe image. |
| 35 |
- Exit code of 1 indicates an unsafe or missing image. |
| 36 |
|
| 37 |
If verbose mode is enabled, KEY=VALUE is output for several useful |
| 38 |
pieces of information about the image and imageutils: |
| 39 |
- SAFETY_CHECK_PASSED (bool) |
| 40 |
- VIRTUAL_SIZE (virtual disk size) |
| 41 |
- ACTUAL_SIZE (actual size of image) |
| 42 |
- IMAGE_FORMAT (format of image) |
| 43 |
- OSLO_UTILS_VERSION (version of oslo_utils currently in use) |
| 44 |
- FAILURE_REASONS (reasons for failure, if failed safety check) |
| 45 |
""" |
| 46 |
logging.basicConfig(level=logging.CRITICAL) |
| 47 |
oslo_utils_version = str(version_info) |
| 48 |
parser = argparse.ArgumentParser( |
| 49 |
prog='oslo.utils.imageutils', |
| 50 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 51 |
description=( |
| 52 |
textwrap.dedent('''\ |
| 53 |
oslo.utils.imageutils image checking program. |
| 54 |
* Exit code of 0 indicates image passes safety check |
| 55 |
* Exit code of 1 indicates image fails safety check |
| 56 |
''') |
| 57 |
), |
| 58 |
epilog=f"Testing using oslo.utils version {oslo_utils_version}", |
| 59 |
) |
| 60 |
parser.add_argument( |
| 61 |
'-v', |
| 62 |
'--verbose', |
| 63 |
action='store_true', |
| 64 |
help=( |
| 65 |
"Print detailed information about the image in " |
| 66 |
"KEY=VALUE format. Defaults to no output." |
| 67 |
), |
| 68 |
) |
| 69 |
parser.add_argument( |
| 70 |
'-i', |
| 71 |
'--image', |
| 72 |
action='store', |
| 73 |
required=True, |
| 74 |
metavar="IMG", |
| 75 |
help="Path to an image you wish to inspect.", |
| 76 |
) |
| 77 |
args = parser.parse_args() |
| 78 |
image = args.image |
| 79 |
verbose = args.verbose |
| 80 |
|
| 81 |
if not os.path.exists(image) or not os.path.isfile(image): |
| 82 |
print(f'Image path {image} provided does not exist', file=sys.stderr) |
| 83 |
sys.exit(1) |
| 84 |
|
| 85 |
inspector = format_inspector.detect_file_format(image) |
| 86 |
if inspector is None: |
| 87 |
print('Could not find format inspector for image', file=sys.stderr) |
| 88 |
sys.exit(1) |
| 89 |
|
| 90 |
safe = True |
| 91 |
try: |
| 92 |
inspector.safety_check() |
| 93 |
except format_inspector.SafetyCheckFailed as e: |
| 94 |
safe = False |
| 95 |
failure_reasons = [] |
| 96 |
for exc in e.failures.items(): |
| 97 |
failure_reasons.append(f"{exc[0]}: {exc[1]}") |
| 98 |
|
| 99 |
virtual_size = inspector.virtual_size |
| 100 |
actual_size = inspector.actual_size |
| 101 |
fmt = str(inspector) |
| 102 |
|
| 103 |
if verbose: |
| 104 |
print(f"SAFETY_CHECK_PASSED={safe}") |
| 105 |
print(f"VIRTUAL_SIZE={virtual_size}") |
| 106 |
print(f"ACTUAL_SIZE={actual_size}") |
| 107 |
print(f"IMAGE_FORMAT=\"{fmt}\"") |
| 108 |
print(f"OSLO_UTILS_VERSION=\"{oslo_utils_version}\"") |
| 109 |
if safe: |
| 110 |
sys.exit(0) |
| 111 |
|
| 112 |
if verbose: |
| 113 |
print('FAILURE_REASONS=\'{}\''.format(','.join(failure_reasons))) |
| 114 |
|
| 115 |
sys.exit(1) |