| 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_run.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
June 27, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Provides a command line interface for runlib.in_toto_run. |
| 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 |
|
| 29 |
import argparse |
| 30 |
import logging |
| 31 |
import sys |
| 32 |
|
| 33 |
from securesystemslib import interface |
| 34 |
|
| 35 |
from in_toto import __version__, runlib |
| 36 |
from in_toto.common_args import ( |
| 37 |
BASE_PATH_ARGS, |
| 38 |
BASE_PATH_KWARGS, |
| 39 |
DSSE_ARGS, |
| 40 |
DSSE_KWARGS, |
| 41 |
EXCLUDE_ARGS, |
| 42 |
EXCLUDE_KWARGS, |
| 43 |
GPG_ARGS, |
| 44 |
GPG_HOME_ARGS, |
| 45 |
GPG_HOME_KWARGS, |
| 46 |
GPG_KWARGS, |
| 47 |
KEY_ARGS, |
| 48 |
KEY_KWARGS, |
| 49 |
KEY_PASSWORD_ARGS, |
| 50 |
KEY_PASSWORD_KWARGS, |
| 51 |
KEY_TYPE_ARGS, |
| 52 |
KEY_TYPE_KWARGS, |
| 53 |
LSTRIP_PATHS_ARGS, |
| 54 |
LSTRIP_PATHS_KWARGS, |
| 55 |
METADATA_DIRECTORY_ARGS, |
| 56 |
METADATA_DIRECTORY_KWARGS, |
| 57 |
OPTS_TITLE, |
| 58 |
QUIET_ARGS, |
| 59 |
QUIET_KWARGS, |
| 60 |
VERBOSE_ARGS, |
| 61 |
VERBOSE_KWARGS, |
| 62 |
parse_password_and_prompt_args, |
| 63 |
sort_action_groups, |
| 64 |
title_case_action_groups, |
| 65 |
) |
| 66 |
|
| 67 |
# Command line interfaces should use in_toto base logger (c.f. in_toto.log) |
| 68 |
LOG = logging.getLogger("in_toto") |
| 69 |
|
| 70 |
|
| 71 |
def create_parser(): |
| 72 |
"""Create and return configured ArgumentParser instance.""" |
| 73 |
parser = argparse.ArgumentParser( |
| 74 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 75 |
description=""" |
| 76 |
in-toto-run is the main command line interface for generating link |
| 77 |
metadata while carrying out a supply chain step. To do this, it wraps the |
| 78 |
passed command, and attempts to track all relevant information about the |
| 79 |
wrapped command's execution. It records paths and hashes of 'materials' (files |
| 80 |
before command execution) and 'products' (files after command execution) and |
| 81 |
writes them together with other information (executed command, return value, |
| 82 |
stdout and stderr) to a link metadata file, which is signed with the passed |
| 83 |
key. It returns a non-zero value on failure and zero otherwise.""", |
| 84 |
) |
| 85 |
|
| 86 |
parser.usage = ( |
| 87 |
"%(prog)s <named arguments> [{}] \\\n\t -- <command> [args]".format( |
| 88 |
OPTS_TITLE.lower() |
| 89 |
) |
| 90 |
) |
| 91 |
|
| 92 |
parser.epilog = """EXAMPLE USAGE |
| 93 |
|
| 94 |
Tag a git repo, storing files in CWD as products, signing the resulting link |
| 95 |
file with the private key loaded from 'key_file'. |
| 96 |
|
| 97 |
{prog} -n tag -p . -k key_file -- git tag v1.0 |
| 98 |
|
| 99 |
|
| 100 |
Create a tarball, storing files in 'project' directory as materials and the |
| 101 |
tarball as product, signing the link file with a GPG key '...7E0C8A17'. |
| 102 |
|
| 103 |
{prog} -n package -m project -p project.tar.gz \\ |
| 104 |
-g 8465A1E2E0FB2B40ADB2478E18FB3F537E0C8A17 \\ |
| 105 |
-- tar czf project.tar.gz project |
| 106 |
|
| 107 |
|
| 108 |
Not all supply chain steps require that a command be executed. in-toto can |
| 109 |
still generate signed attestations, e.g. for review work. In that case, files |
| 110 |
may be marked as materials for the manual review process and the command be |
| 111 |
omitted. |
| 112 |
|
| 113 |
{prog} -n review -k key_file -m document.pdf -x |
| 114 |
|
| 115 |
|
| 116 |
If an artifact that should be recorded is not in the current working directory |
| 117 |
(or one of its subdirectories) it can be located using the base path option. |
| 118 |
Note that in this example only the relative path, 'document.pdf' is stored |
| 119 |
along with its hash in the resulting link metadata file. |
| 120 |
|
| 121 |
{prog} -n review -k key_file -m document.pdf \\ |
| 122 |
--base-path /my/review/docs/ -x |
| 123 |
|
| 124 |
|
| 125 |
Similarly, it is possible to pass the full path to the artifact that should |
| 126 |
be recorded together with a left-strip path, to only store a relative path, |
| 127 |
e.g. 'document.pdf'. |
| 128 |
|
| 129 |
{prog} -n review -k key_file \\ |
| 130 |
-m /tmp/my/review/docs/document.pdf \\ |
| 131 |
--lstrip-paths /tmp/my/review/docs/ -x |
| 132 |
|
| 133 |
|
| 134 |
""".format( |
| 135 |
prog=parser.prog |
| 136 |
) |
| 137 |
|
| 138 |
named_args = parser.add_argument_group("required named arguments") |
| 139 |
|
| 140 |
# FIXME: Do we limit the allowed characters for the name? |
| 141 |
named_args.add_argument( |
| 142 |
"-n", |
| 143 |
"--step-name", |
| 144 |
type=str, |
| 145 |
required=True, |
| 146 |
metavar="<name>", |
| 147 |
help=( |
| 148 |
"name for the resulting link metadata file, which is written to" |
| 149 |
" '<name>.<keyid prefix>.link'. It is also used to associate the link" |
| 150 |
" with a step defined in an in-toto layout." |
| 151 |
), |
| 152 |
) |
| 153 |
|
| 154 |
parser.add_argument( |
| 155 |
"-m", |
| 156 |
"--materials", |
| 157 |
type=str, |
| 158 |
required=False, |
| 159 |
nargs="+", |
| 160 |
metavar="<path>", |
| 161 |
help=( |
| 162 |
"paths to files or directories, for which paths and hashes are stored in" |
| 163 |
" the resulting link metadata before the command is executed. Symlinks" |
| 164 |
" to files are followed." |
| 165 |
), |
| 166 |
) |
| 167 |
|
| 168 |
parser.add_argument( |
| 169 |
"-p", |
| 170 |
"--products", |
| 171 |
type=str, |
| 172 |
required=False, |
| 173 |
nargs="+", |
| 174 |
metavar="<path>", |
| 175 |
help=( |
| 176 |
"paths to files or directories, for which paths and hashes are stored in" |
| 177 |
" the resulting link metadata after the command is executed. Symlinks to" |
| 178 |
" files are followed." |
| 179 |
), |
| 180 |
) |
| 181 |
|
| 182 |
parser.add_argument( |
| 183 |
"-s", |
| 184 |
"--record-streams", |
| 185 |
dest="record_streams", |
| 186 |
default=False, |
| 187 |
action="store_true", |
| 188 |
help=( |
| 189 |
"duplicate 'stdout' and 'stderr' of the executed command and store the" |
| 190 |
" contents in the resulting link metadata. Do not use with interactive" |
| 191 |
" commands." |
| 192 |
), |
| 193 |
) |
| 194 |
|
| 195 |
parser.add_argument( |
| 196 |
"-x", |
| 197 |
"--no-command", |
| 198 |
dest="no_command", |
| 199 |
default=False, |
| 200 |
action="store_true", |
| 201 |
help=( |
| 202 |
"generate link metadata without executing a command, e.g. for a" |
| 203 |
" signed-off-by step." |
| 204 |
), |
| 205 |
) |
| 206 |
|
| 207 |
named_args.add_argument(*KEY_ARGS, **KEY_KWARGS) |
| 208 |
parser.add_argument(*KEY_TYPE_ARGS, **KEY_TYPE_KWARGS) |
| 209 |
parser.add_argument(*KEY_PASSWORD_ARGS, **KEY_PASSWORD_KWARGS) |
| 210 |
|
| 211 |
named_args.add_argument(*GPG_ARGS, **GPG_KWARGS) |
| 212 |
parser.add_argument(*GPG_HOME_ARGS, **GPG_HOME_KWARGS) |
| 213 |
|
| 214 |
parser.add_argument(*EXCLUDE_ARGS, **EXCLUDE_KWARGS) |
| 215 |
parser.add_argument(*BASE_PATH_ARGS, **BASE_PATH_KWARGS) |
| 216 |
parser.add_argument(*LSTRIP_PATHS_ARGS, **LSTRIP_PATHS_KWARGS) |
| 217 |
parser.add_argument(*METADATA_DIRECTORY_ARGS, **METADATA_DIRECTORY_KWARGS) |
| 218 |
parser.add_argument(*DSSE_ARGS, **DSSE_KWARGS) |
| 219 |
|
| 220 |
verbosity_args = parser.add_mutually_exclusive_group(required=False) |
| 221 |
verbosity_args.add_argument(*VERBOSE_ARGS, **VERBOSE_KWARGS) |
| 222 |
verbosity_args.add_argument(*QUIET_ARGS, **QUIET_KWARGS) |
| 223 |
|
| 224 |
# FIXME: This is not yet ideal. |
| 225 |
# What should we do with tokens like > or ; ? |
| 226 |
parser.add_argument( |
| 227 |
"link_cmd", |
| 228 |
nargs="*", |
| 229 |
metavar="<command>", |
| 230 |
help=( |
| 231 |
"command to be executed. It is separated from named and optional" |
| 232 |
" arguments by a double dash '--'." |
| 233 |
), |
| 234 |
) |
| 235 |
|
| 236 |
parser.add_argument( |
| 237 |
"--version", |
| 238 |
action="version", |
| 239 |
version="{} {}".format(parser.prog, __version__), |
| 240 |
) |
| 241 |
|
| 242 |
title_case_action_groups(parser) |
| 243 |
sort_action_groups(parser) |
| 244 |
|
| 245 |
return parser |
| 246 |
|
| 247 |
|
| 248 |
def main(): |
| 249 |
"""Parse arguments, load key from disk (prompts for password if key is |
| 250 |
encrypted) and call in_toto_run.""" |
| 251 |
parser = create_parser() |
| 252 |
args = parser.parse_args() |
| 253 |
|
| 254 |
LOG.setLevelVerboseOrQuiet(args.verbose, args.quiet) |
| 255 |
|
| 256 |
# Regular signing and GPG signing are mutually exclusive |
| 257 |
if (args.key is None) == (args.gpg is None): |
| 258 |
parser.print_usage() |
| 259 |
parser.error("Specify either '--key <key path>' or '--gpg [<keyid>]'") |
| 260 |
|
| 261 |
password, prompt = parse_password_and_prompt_args(args) |
| 262 |
|
| 263 |
# If `--gpg` was set without argument it has the value `True` and |
| 264 |
# we will try to sign with the default key |
| 265 |
gpg_use_default = args.gpg is True |
| 266 |
|
| 267 |
# Otherwise we interpret it as actual keyid |
| 268 |
gpg_keyid = None |
| 269 |
if args.gpg is not True: |
| 270 |
gpg_keyid = args.gpg |
| 271 |
|
| 272 |
# If no_command is specified run in_toto_run without executing a command |
| 273 |
if args.no_command: |
| 274 |
args.link_cmd = [] |
| 275 |
|
| 276 |
elif not args.link_cmd: # pragma: no branch |
| 277 |
parser.print_usage() |
| 278 |
parser.error( |
| 279 |
"No command specified." |
| 280 |
" Please specify (or use the --no-command option)" |
| 281 |
) |
| 282 |
|
| 283 |
try: |
| 284 |
# We load the key here because it might prompt the user for a password in |
| 285 |
# case the key is encrypted. Something that should not happen in the lib. |
| 286 |
key = None |
| 287 |
if args.key: |
| 288 |
key = interface.import_privatekey_from_file( |
| 289 |
args.key, |
| 290 |
key_type=args.key_type, |
| 291 |
password=password, |
| 292 |
prompt=prompt, |
| 293 |
) |
| 294 |
|
| 295 |
runlib.in_toto_run( |
| 296 |
args.step_name, |
| 297 |
args.materials, |
| 298 |
args.products, |
| 299 |
args.link_cmd, |
| 300 |
record_streams=args.record_streams, |
| 301 |
signing_key=key, |
| 302 |
gpg_keyid=gpg_keyid, |
| 303 |
gpg_use_default=gpg_use_default, |
| 304 |
gpg_home=args.gpg_home, |
| 305 |
exclude_patterns=args.exclude_patterns, |
| 306 |
base_path=args.base_path, |
| 307 |
lstrip_paths=args.lstrip_paths, |
| 308 |
metadata_directory=args.metadata_directory, |
| 309 |
use_dsse=args.use_dsse, |
| 310 |
) |
| 311 |
|
| 312 |
except Exception as e: |
| 313 |
LOG.error("(in-toto-run) {0}: {1}".format(type(e).__name__, e)) |
| 314 |
sys.exit(1) |
| 315 |
|
| 316 |
sys.exit(0) |
| 317 |
|
| 318 |
|
| 319 |
if __name__ == "__main__": |
| 320 |
main() |