| 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 |
""" |
| 7 |
<Program Name> |
| 8 |
in_toto_mock.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Shikher Verma <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
June 12, 2017 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Provides a command line interface for runlib.in_toto_mock. |
| 21 |
|
| 22 |
<Return Codes> |
| 23 |
2 if an exception occurred during argument parsing |
| 24 |
1 if an exception occurred |
| 25 |
0 if no exception occurred |
| 26 |
|
| 27 |
""" |
| 28 |
import argparse |
| 29 |
import logging |
| 30 |
import sys |
| 31 |
|
| 32 |
import in_toto.runlib |
| 33 |
from in_toto import __version__ |
| 34 |
from in_toto.common_args import ( |
| 35 |
DSSE_ARGS, |
| 36 |
DSSE_KWARGS, |
| 37 |
sort_action_groups, |
| 38 |
title_case_action_groups, |
| 39 |
) |
| 40 |
|
| 41 |
# Command line interfaces should use in_toto base logger (c.f. in_toto.log) |
| 42 |
LOG = logging.getLogger("in_toto") |
| 43 |
|
| 44 |
|
| 45 |
def create_parser(): |
| 46 |
"""Parse arguments and call in_toto_mock.""" |
| 47 |
parser = argparse.ArgumentParser( |
| 48 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 49 |
description=""" |
| 50 |
in-toto-mock is a variant of 'in-toto-run' that can be used to create unsigned |
| 51 |
link metadata, using defaults for many of the 'in-toto-run' arguments. |
| 52 |
in-toto-mock verbosely executes the passed command, records all files in the |
| 53 |
current working directory as materials and products, and generates a link file |
| 54 |
under '<name>.link'. |
| 55 |
|
| 56 |
This is useful for trying out how to generate a link without the need for a |
| 57 |
key, or knowledge about all 'in-toto-run' arguments. It can also be used to |
| 58 |
quickly generate link metadata, inspect it and sign it retroactively. |
| 59 |
|
| 60 |
""", |
| 61 |
) |
| 62 |
|
| 63 |
parser.usage = "%(prog)s [-h] --name <name> -- <command> [args]" |
| 64 |
|
| 65 |
parser.epilog = """EXAMPLE USAGE |
| 66 |
|
| 67 |
Generate unsigned link metadata 'foo.link' for the activity of creating file |
| 68 |
'bar', inspect it, and sign it with 'mykey' |
| 69 |
|
| 70 |
# Generate unsigned link |
| 71 |
{prog} --name foo -- touch bar |
| 72 |
# Inspect and/or update unsigned link metadata |
| 73 |
vi foo.link |
| 74 |
# Sign the link, attesting to its validity, and write it to |
| 75 |
# 'foo.<mykey keyid prefix>.link'. |
| 76 |
in-toto-sign -k mykey -f foo.link |
| 77 |
|
| 78 |
""".format( |
| 79 |
prog=parser.prog |
| 80 |
) |
| 81 |
|
| 82 |
named_args = parser.add_argument_group("required named arguments") |
| 83 |
|
| 84 |
# FIXME: Do we limit the allowed characters for the name? |
| 85 |
named_args.add_argument( |
| 86 |
"-n", |
| 87 |
"--name", |
| 88 |
type=str, |
| 89 |
required=True, |
| 90 |
metavar="<name>", |
| 91 |
help=( |
| 92 |
"name for the resulting link metadata file, which is written to" |
| 93 |
" '<name>.link'. It is also used to associate the link with a step" |
| 94 |
" defined in an in-toto layout." |
| 95 |
), |
| 96 |
) |
| 97 |
|
| 98 |
parser.add_argument(*DSSE_ARGS, **DSSE_KWARGS) |
| 99 |
|
| 100 |
# FIXME: This is not yet ideal. |
| 101 |
# What should we do with tokens like > or ; ? |
| 102 |
parser.add_argument( |
| 103 |
"link_cmd", |
| 104 |
nargs="+", |
| 105 |
metavar="<command>", |
| 106 |
help=( |
| 107 |
"command to be executed. It is separated from named and optional" |
| 108 |
" arguments by a double dash '--'." |
| 109 |
), |
| 110 |
) |
| 111 |
|
| 112 |
parser.add_argument( |
| 113 |
"--version", |
| 114 |
action="version", |
| 115 |
version="{} {}".format(parser.prog, __version__), |
| 116 |
) |
| 117 |
|
| 118 |
title_case_action_groups(parser) |
| 119 |
sort_action_groups(parser) |
| 120 |
|
| 121 |
return parser |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
"""Parse arguments and call in_toto_mock.""" |
| 126 |
parser = create_parser() |
| 127 |
args = parser.parse_args() |
| 128 |
|
| 129 |
# in-toto-mock should not be used to secure the supply chain but only to try |
| 130 |
# out in-toto-run with max. user feedback, hence we set a verbose log level |
| 131 |
LOG.setLevel(logging.INFO) |
| 132 |
|
| 133 |
try: |
| 134 |
in_toto.runlib.in_toto_mock(args.name, args.link_cmd, args.use_dsse) |
| 135 |
|
| 136 |
except Exception as e: |
| 137 |
LOG.error("(in-toto-mock) {0}: {1}".format(type(e).__name__, e)) |
| 138 |
sys.exit(1) |
| 139 |
|
| 140 |
sys.exit(0) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
main() |