| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright New York University and the in-toto contributors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
"""CLI to check local artifacts. |
| 7 |
""" |
| 8 |
import argparse |
| 9 |
import logging |
| 10 |
import sys |
| 11 |
|
| 12 |
from in_toto.common_args import ( |
| 13 |
LSTRIP_PATHS_ARGS, |
| 14 |
LSTRIP_PATHS_KWARGS, |
| 15 |
VERBOSE_ARGS, |
| 16 |
VERBOSE_KWARGS, |
| 17 |
sort_action_groups, |
| 18 |
title_case_action_groups, |
| 19 |
) |
| 20 |
from in_toto.models.metadata import Metadata |
| 21 |
from in_toto.runlib import in_toto_match_products as match_products |
| 22 |
|
| 23 |
LOG = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
def create_parser(): |
| 27 |
"""Create parser.""" |
| 28 |
parser = argparse.ArgumentParser( |
| 29 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 30 |
description="Check if local artifacts match products in passed link.", |
| 31 |
) |
| 32 |
required = parser.add_argument_group("required named arguments") |
| 33 |
required.add_argument( |
| 34 |
"-l", |
| 35 |
"--link", |
| 36 |
type=str, |
| 37 |
required=True, |
| 38 |
metavar="<path>", |
| 39 |
help="path to link metadata file.", |
| 40 |
) |
| 41 |
parser.add_argument( |
| 42 |
"-p", |
| 43 |
"--paths", |
| 44 |
type=str, |
| 45 |
required=False, |
| 46 |
nargs="+", |
| 47 |
metavar="<path>", |
| 48 |
help="file or directory paths to local artifacts. Default is CWD.", |
| 49 |
) |
| 50 |
|
| 51 |
parser.add_argument( |
| 52 |
"-e", |
| 53 |
"--exclude", |
| 54 |
required=False, |
| 55 |
metavar="<pattern>", |
| 56 |
nargs="+", |
| 57 |
help="gitignore-style patterns to exclude artifacts from matching.", |
| 58 |
) |
| 59 |
|
| 60 |
parser.add_argument(*LSTRIP_PATHS_ARGS, **LSTRIP_PATHS_KWARGS) |
| 61 |
parser.add_argument(*VERBOSE_ARGS, **VERBOSE_KWARGS) |
| 62 |
|
| 63 |
title_case_action_groups(parser) |
| 64 |
sort_action_groups(parser) |
| 65 |
|
| 66 |
return parser |
| 67 |
|
| 68 |
|
| 69 |
def main(): |
| 70 |
"""CLI.""" |
| 71 |
parser = create_parser() |
| 72 |
args = parser.parse_args() |
| 73 |
|
| 74 |
metadata = Metadata.load(args.link) |
| 75 |
only_products, not_in_products, differ = match_products( |
| 76 |
metadata.signed, |
| 77 |
paths=args.paths, |
| 78 |
exclude_patterns=args.exclude, |
| 79 |
lstrip_paths=args.lstrip_paths, |
| 80 |
) |
| 81 |
# raise Exception("test") |
| 82 |
if only_products or not_in_products or differ: |
| 83 |
if args.verbose: |
| 84 |
print("Local artifacts don't match products in passed link.") |
| 85 |
for name in only_products: |
| 86 |
print(f"Only in products: {name}") |
| 87 |
|
| 88 |
for name in not_in_products: |
| 89 |
print(f"Not in products: {name}") |
| 90 |
|
| 91 |
for name in differ: |
| 92 |
print(f"Hashes differ: {name}") |
| 93 |
|
| 94 |
sys.exit(1) |
| 95 |
|
| 96 |
if args.verbose: |
| 97 |
print("Local artifacts match products in passed link.") |
| 98 |
sys.exit(0) |
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__": |
| 102 |
main() |