| 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_record.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Nov 28, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Provides a command line interface for runlib.in_toto_record_start and |
| 21 |
runlib.in_toto_record_stop. |
| 22 |
|
| 23 |
<Return Codes> |
| 24 |
2 if an exception occurred during argument parsing |
| 25 |
1 if an exception occurred |
| 26 |
0 if no exception occurred |
| 27 |
|
| 28 |
""" |
| 29 |
import argparse |
| 30 |
import logging |
| 31 |
import sys |
| 32 |
|
| 33 |
from securesystemslib import interface |
| 34 |
|
| 35 |
import in_toto.runlib |
| 36 |
from in_toto import __version__ |
| 37 |
from in_toto.common_args import ( |
| 38 |
BASE_PATH_ARGS, |
| 39 |
BASE_PATH_KWARGS, |
| 40 |
DSSE_ARGS, |
| 41 |
DSSE_KWARGS, |
| 42 |
EXCLUDE_ARGS, |
| 43 |
EXCLUDE_KWARGS, |
| 44 |
GPG_ARGS, |
| 45 |
GPG_HOME_ARGS, |
| 46 |
GPG_HOME_KWARGS, |
| 47 |
GPG_KWARGS, |
| 48 |
KEY_ARGS, |
| 49 |
KEY_KWARGS, |
| 50 |
KEY_PASSWORD_ARGS, |
| 51 |
KEY_PASSWORD_KWARGS, |
| 52 |
KEY_TYPE_ARGS, |
| 53 |
KEY_TYPE_KWARGS, |
| 54 |
LSTRIP_PATHS_ARGS, |
| 55 |
LSTRIP_PATHS_KWARGS, |
| 56 |
METADATA_DIRECTORY_ARGS, |
| 57 |
METADATA_DIRECTORY_KWARGS, |
| 58 |
OPTS_TITLE, |
| 59 |
QUIET_ARGS, |
| 60 |
QUIET_KWARGS, |
| 61 |
VERBOSE_ARGS, |
| 62 |
VERBOSE_KWARGS, |
| 63 |
parse_password_and_prompt_args, |
| 64 |
sort_action_groups, |
| 65 |
title_case_action_groups, |
| 66 |
) |
| 67 |
|
| 68 |
# Command line interfaces should use in_toto base logger (c.f. in_toto.log) |
| 69 |
LOG = logging.getLogger("in_toto") |
| 70 |
|
| 71 |
|
| 72 |
def create_parser(): |
| 73 |
"""Create and return configured ArgumentParser instance.""" |
| 74 |
|
| 75 |
parser = argparse.ArgumentParser( |
| 76 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 77 |
description=""" |
| 78 |
in-toto-record creates a signed link metadata file in two steps, in order to |
| 79 |
provide evidence for supply chain steps that cannot be carried out by a single |
| 80 |
command (for which 'in-toto-run' should be used). It returns a non-zero value |
| 81 |
on failure and zero otherwise.""", |
| 82 |
) |
| 83 |
|
| 84 |
parser.epilog = """EXAMPLE USAGE |
| 85 |
|
| 86 |
Create link metadata file in two commands, signing it with the private key |
| 87 |
loaded from 'key_file', recording all files in the CWD as materials (on |
| 88 |
start), and as products (on stop). |
| 89 |
|
| 90 |
{prog} start -n edit-files -k path/to/key_file -m . |
| 91 |
{prog} stop -n edit-files -k path/to/key_file -p . |
| 92 |
|
| 93 |
|
| 94 |
Create link metadata file signed with the default GPG key from the default |
| 95 |
GPG home directory and record a file named 'foo' as material and product. |
| 96 |
|
| 97 |
{prog} start -n edit-foo --gpg -m path/to/foo |
| 98 |
{prog} stop -n edit-foo --gpg -p path/to/foo |
| 99 |
|
| 100 |
|
| 101 |
Create link metadata file signed with the private key loaded from 'key_file', |
| 102 |
record all files in the CWD as material and product, and dump finished link |
| 103 |
file to the target directory (on stop). |
| 104 |
|
| 105 |
{prog} start -n edit-files -k path/to/key_file -m . |
| 106 |
{prog} stop -d path/to/target/dir -n edit-files -k path/to/key_file -p . |
| 107 |
|
| 108 |
""".format( |
| 109 |
prog=parser.prog |
| 110 |
) |
| 111 |
|
| 112 |
# The subparsers inherit the arguments from the parent parser |
| 113 |
parent_parser = argparse.ArgumentParser(add_help=False) |
| 114 |
subparsers = parser.add_subparsers(dest="command") |
| 115 |
|
| 116 |
# Workaround to make subcommands mandatory in Python>=3.3 |
| 117 |
# https://bugs.python.org/issue9253#msg186387 |
| 118 |
subparsers.required = True |
| 119 |
|
| 120 |
parent_named_args = parent_parser.add_argument_group( |
| 121 |
"required named arguments" |
| 122 |
) |
| 123 |
|
| 124 |
# FIXME: Do we limit the allowed characters for the name? |
| 125 |
parent_named_args.add_argument( |
| 126 |
"-n", |
| 127 |
"--step-name", |
| 128 |
type=str, |
| 129 |
required=True, |
| 130 |
metavar="<name>", |
| 131 |
help=( |
| 132 |
"name for the resulting link metadata file. It is also used to associate" |
| 133 |
" the link with a step defined in an in-toto layout." |
| 134 |
), |
| 135 |
) |
| 136 |
|
| 137 |
parent_named_args.add_argument(*KEY_ARGS, **KEY_KWARGS) |
| 138 |
parent_parser.add_argument(*KEY_TYPE_ARGS, **KEY_TYPE_KWARGS) |
| 139 |
parent_parser.add_argument(*KEY_PASSWORD_ARGS, **KEY_PASSWORD_KWARGS) |
| 140 |
|
| 141 |
parent_named_args.add_argument(*GPG_ARGS, **GPG_KWARGS) |
| 142 |
parent_parser.add_argument(*GPG_HOME_ARGS, **GPG_HOME_KWARGS) |
| 143 |
|
| 144 |
parent_parser.add_argument(*EXCLUDE_ARGS, **EXCLUDE_KWARGS) |
| 145 |
parent_parser.add_argument(*BASE_PATH_ARGS, **BASE_PATH_KWARGS) |
| 146 |
parent_parser.add_argument(*LSTRIP_PATHS_ARGS, **LSTRIP_PATHS_KWARGS) |
| 147 |
parent_parser.add_argument(*DSSE_ARGS, **DSSE_KWARGS) |
| 148 |
|
| 149 |
verbosity_args = parent_parser.add_mutually_exclusive_group(required=False) |
| 150 |
verbosity_args.add_argument(*VERBOSE_ARGS, **VERBOSE_KWARGS) |
| 151 |
verbosity_args.add_argument(*QUIET_ARGS, **QUIET_KWARGS) |
| 152 |
|
| 153 |
subparser_start = subparsers.add_parser( |
| 154 |
"start", |
| 155 |
parents=[parent_parser], |
| 156 |
help=( |
| 157 |
"creates a preliminary link file recording the paths and hashes of" |
| 158 |
" the passed materials and signs it with the passed functionary's" |
| 159 |
" key. The resulting link file is stored as" |
| 160 |
" '.<name>.<keyid prefix>.link-unfinished'." |
| 161 |
), |
| 162 |
) |
| 163 |
|
| 164 |
subparser_stop = subparsers.add_parser( |
| 165 |
"stop", |
| 166 |
parents=[parent_parser], |
| 167 |
help=( |
| 168 |
"expects preliminary link file '.<name>.<keyid prefix>.link-unfinished'" |
| 169 |
" in the CWD, signed by the passed functionary's key. If found, it" |
| 170 |
" records and adds the paths and hashes of the passed products to the" |
| 171 |
" link metadata file, updates the signature and renames the file to" |
| 172 |
" '<name>.<keyid prefix>.link'." |
| 173 |
), |
| 174 |
) |
| 175 |
|
| 176 |
subparser_start.add_argument( |
| 177 |
"-m", |
| 178 |
"--materials", |
| 179 |
type=str, |
| 180 |
required=False, |
| 181 |
nargs="+", |
| 182 |
metavar="<path>", |
| 183 |
help=( |
| 184 |
"paths to files or directories, whose paths and hashes are stored in the" |
| 185 |
" resulting link metadata's material section when running the 'start'" |
| 186 |
" subcommand. Symlinks to files are followed." |
| 187 |
), |
| 188 |
) |
| 189 |
|
| 190 |
subparser_stop.add_argument( |
| 191 |
"-p", |
| 192 |
"--products", |
| 193 |
type=str, |
| 194 |
required=False, |
| 195 |
nargs="+", |
| 196 |
metavar="<path>", |
| 197 |
help=( |
| 198 |
"paths to files or directories, whose paths and hashes are stored in the" |
| 199 |
" resulting link metadata's product section when running the 'stop'" |
| 200 |
" subcommand. Symlinks to files are followed." |
| 201 |
), |
| 202 |
) |
| 203 |
|
| 204 |
subparser_stop.add_argument( |
| 205 |
*METADATA_DIRECTORY_ARGS, **METADATA_DIRECTORY_KWARGS |
| 206 |
) |
| 207 |
|
| 208 |
parser.add_argument( |
| 209 |
"--version", |
| 210 |
action="version", |
| 211 |
version="{} {}".format(parser.prog, __version__), |
| 212 |
) |
| 213 |
|
| 214 |
for _parser, _order in [ |
| 215 |
(parser, ["Positional Arguments", OPTS_TITLE]), |
| 216 |
(subparser_start, None), |
| 217 |
(subparser_stop, None), |
| 218 |
]: |
| 219 |
title_case_action_groups(_parser) |
| 220 |
sort_action_groups(_parser, _order) |
| 221 |
|
| 222 |
return parser |
| 223 |
|
| 224 |
|
| 225 |
def main(): |
| 226 |
"""Parse arguments, load key from disk (if passed) and call |
| 227 |
either runlib.in_toto_record_start or runlib.in_toto_record_stop depending |
| 228 |
on the specified subcommand.""" |
| 229 |
|
| 230 |
parser = create_parser() |
| 231 |
args = parser.parse_args() |
| 232 |
|
| 233 |
LOG.setLevelVerboseOrQuiet(args.verbose, args.quiet) |
| 234 |
|
| 235 |
# Regular signing and GPG signing are mutually exclusive |
| 236 |
if (args.key is None) == (args.gpg is None): |
| 237 |
parser.print_usage() |
| 238 |
parser.error("Specify either '--key <key path>' or '--gpg [<keyid>]'") |
| 239 |
|
| 240 |
password, prompt = parse_password_and_prompt_args(args) |
| 241 |
|
| 242 |
# If `--gpg` was set without argument it has the value `True` and |
| 243 |
# we will try to sign with the default key |
| 244 |
gpg_use_default = args.gpg is True |
| 245 |
|
| 246 |
# Otherwise gpg_keyid stays either None or gets the passed argument assigned |
| 247 |
gpg_keyid = None |
| 248 |
if not gpg_use_default and args.gpg: |
| 249 |
gpg_keyid = args.gpg |
| 250 |
|
| 251 |
try: |
| 252 |
# We load the key here because it might prompt the user for a password in |
| 253 |
# case the key is encrypted. Something that should not happen in the lib. |
| 254 |
key = None |
| 255 |
if args.key: |
| 256 |
key = interface.import_privatekey_from_file( |
| 257 |
args.key, |
| 258 |
key_type=args.key_type, |
| 259 |
password=password, |
| 260 |
prompt=prompt, |
| 261 |
) |
| 262 |
|
| 263 |
if args.command == "start": |
| 264 |
in_toto.runlib.in_toto_record_start( |
| 265 |
args.step_name, |
| 266 |
args.materials, |
| 267 |
signing_key=key, |
| 268 |
gpg_keyid=gpg_keyid, |
| 269 |
gpg_use_default=gpg_use_default, |
| 270 |
gpg_home=args.gpg_home, |
| 271 |
exclude_patterns=args.exclude_patterns, |
| 272 |
base_path=args.base_path, |
| 273 |
lstrip_paths=args.lstrip_paths, |
| 274 |
use_dsse=args.use_dsse, |
| 275 |
) |
| 276 |
|
| 277 |
# Mutually exclusiveness is guaranteed by argparser |
| 278 |
else: # args.command == "stop": |
| 279 |
in_toto.runlib.in_toto_record_stop( |
| 280 |
args.step_name, |
| 281 |
args.products, |
| 282 |
signing_key=key, |
| 283 |
gpg_keyid=gpg_keyid, |
| 284 |
gpg_use_default=gpg_use_default, |
| 285 |
gpg_home=args.gpg_home, |
| 286 |
exclude_patterns=args.exclude_patterns, |
| 287 |
base_path=args.base_path, |
| 288 |
lstrip_paths=args.lstrip_paths, |
| 289 |
metadata_directory=args.metadata_directory, |
| 290 |
) |
| 291 |
|
| 292 |
except Exception as e: |
| 293 |
LOG.error( |
| 294 |
"(in-toto-record {0}) {1}: {2}".format( |
| 295 |
args.command, type(e).__name__, e |
| 296 |
) |
| 297 |
) |
| 298 |
sys.exit(1) |
| 299 |
|
| 300 |
sys.exit(0) |
| 301 |
|
| 302 |
|
| 303 |
if __name__ == "__main__": |
| 304 |
main() |