| 1 |
# Copyright New York University and the in-toto contributors |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
""" |
| 5 |
<Program Name> |
| 6 |
common_args.py |
| 7 |
|
| 8 |
<Author> |
| 9 |
Lukas Puehringer <[email protected]> |
| 10 |
|
| 11 |
<Started> |
| 12 |
Mar 09, 2018 |
| 13 |
|
| 14 |
<Copyright> |
| 15 |
See LICENSE for licensing information. |
| 16 |
|
| 17 |
<Purpose> |
| 18 |
Provides a collection of constants that can be used as `*args` or `**kwargs` |
| 19 |
to argparse.ArgumentParser.add_argument() for cli tools with common |
| 20 |
command line arguments. |
| 21 |
|
| 22 |
Example Usage: |
| 23 |
|
| 24 |
``` |
| 25 |
form in_toto.common_args import EXCLUDE_ARGS, EXCLUDE_KWARGS |
| 26 |
parser = argparse.ArgumentParser() |
| 27 |
parser.add_argument(*EXCLUDE_KWARGS, **EXCLUDE_KWARGS) |
| 28 |
``` |
| 29 |
|
| 30 |
""" |
| 31 |
import sys |
| 32 |
|
| 33 |
from in_toto import ( |
| 34 |
KEY_TYPE_ECDSA, |
| 35 |
KEY_TYPE_ED25519, |
| 36 |
KEY_TYPE_RSA, |
| 37 |
SUPPORTED_KEY_TYPES, |
| 38 |
) |
| 39 |
|
| 40 |
EXCLUDE_ARGS = ["--exclude"] |
| 41 |
EXCLUDE_KWARGS = { |
| 42 |
"dest": "exclude_patterns", |
| 43 |
"required": False, |
| 44 |
"metavar": "<pattern>", |
| 45 |
"nargs": "+", |
| 46 |
"help": ( |
| 47 |
"path patterns to match paths that should not be recorded as" |
| 48 |
" 'materials' or 'products'. Passed patterns override patterns" |
| 49 |
" defined in environment variables or config files. See Config docs" |
| 50 |
" for details." |
| 51 |
), |
| 52 |
} |
| 53 |
|
| 54 |
BASE_PATH_ARGS = ["--base-path"] |
| 55 |
BASE_PATH_KWARGS = { |
| 56 |
"dest": "base_path", |
| 57 |
"required": False, |
| 58 |
"metavar": "<path>", |
| 59 |
"help": ( |
| 60 |
"base path for relative paths passed via '--materials' and" |
| 61 |
" '--products'. It is used to locate and record artifacts, and is" |
| 62 |
" not included in the resulting link metadata. Default is the" |
| 63 |
" current working directory." |
| 64 |
), |
| 65 |
} |
| 66 |
|
| 67 |
LSTRIP_PATHS_ARGS = ["--lstrip-paths"] |
| 68 |
LSTRIP_PATHS_KWARGS = { |
| 69 |
"dest": "lstrip_paths", |
| 70 |
"required": False, |
| 71 |
"nargs": "+", |
| 72 |
"metavar": "<path>", |
| 73 |
"help": ( |
| 74 |
"path prefixes used to left-strip artifact paths before storing" |
| 75 |
" them to the resulting link metadata. If multiple prefixes are" |
| 76 |
" specified, only a single prefix can match the path of any" |
| 77 |
" artifact and that is then left-stripped. All prefixes are checked" |
| 78 |
" to ensure none of them are a left substring of another." |
| 79 |
), |
| 80 |
} |
| 81 |
|
| 82 |
KEY_ARGS = ["-k", "--key"] |
| 83 |
KEY_KWARGS = { |
| 84 |
"type": str, |
| 85 |
"metavar": "<path>", |
| 86 |
"help": ( |
| 87 |
"path to a private key file to sign the resulting link metadata." |
| 88 |
" The keyid prefix is used as an infix for the link metadata" |
| 89 |
" filename, i.e. '<name>.<keyid prefix>.link'. See '--key-type' for" |
| 90 |
" available formats. Passing one of '--key' or '--gpg' is required." |
| 91 |
), |
| 92 |
} |
| 93 |
|
| 94 |
KEY_TYPE_ARGS = ["-t", "--key-type"] |
| 95 |
KEY_TYPE_KWARGS = { |
| 96 |
"dest": "key_type", |
| 97 |
"type": str, |
| 98 |
"choices": SUPPORTED_KEY_TYPES, |
| 99 |
"default": KEY_TYPE_RSA, |
| 100 |
"help": ( |
| 101 |
"type of key specified by the '--key' option. '{rsa}' keys are" |
| 102 |
" expected in a 'PEM' format. '{ed25519}' and '{ecdsa}' are" |
| 103 |
" expected to be in a custom 'securesystemslib/json' format." |
| 104 |
" Default is '{rsa}'.".format( |
| 105 |
rsa=KEY_TYPE_RSA, ed25519=KEY_TYPE_ED25519, ecdsa=KEY_TYPE_ECDSA |
| 106 |
) |
| 107 |
), |
| 108 |
} |
| 109 |
|
| 110 |
KEY_PASSWORD_ARGS = ["-P", "--password"] |
| 111 |
KEY_PASSWORD_KWARGS = { |
| 112 |
"nargs": "?", |
| 113 |
"const": True, |
| 114 |
"metavar": "<password>", |
| 115 |
"help": ( |
| 116 |
"password for encrypted key specified with '--key'. Passing '-P'" |
| 117 |
" without <password> opens a prompt. If no password is passed, or" |
| 118 |
" entered on the prompt, the key is treated as unencrypted. (Do " |
| 119 |
" not confuse with '-p/--products'!)" |
| 120 |
), |
| 121 |
} |
| 122 |
OPTS_TITLE = "Optional Arguments" if sys.version_info < (3, 10) else "Options" |
| 123 |
|
| 124 |
|
| 125 |
def parse_password_and_prompt_args(args): |
| 126 |
"""Parse -P/--password optional arg (nargs=?, const=True).""" |
| 127 |
# --P was provided without argument (True) |
| 128 |
if args.password is True: |
| 129 |
password = None |
| 130 |
prompt = True |
| 131 |
# --P was not provided (None), or provided with argument (<password>) |
| 132 |
else: |
| 133 |
password = args.password |
| 134 |
prompt = False |
| 135 |
|
| 136 |
return password, prompt |
| 137 |
|
| 138 |
|
| 139 |
GPG_ARGS = ["-g", "--gpg"] |
| 140 |
GPG_KWARGS = { |
| 141 |
"nargs": "?", |
| 142 |
"const": True, |
| 143 |
"metavar": "<id>", |
| 144 |
"help": ( |
| 145 |
"GPG keyid to sign the resulting link metadata. When '--gpg' is" |
| 146 |
" passed without the keyid, the default GPG key is used. The keyid" |
| 147 |
" prefix is used as an infix for the link metadata filename, i.e." |
| 148 |
" '<name>.<keyid prefix>.link'. Passing one of '--key' or '--gpg'" |
| 149 |
" is required." |
| 150 |
), |
| 151 |
} |
| 152 |
|
| 153 |
GPG_HOME_ARGS = ["--gpg-home"] |
| 154 |
GPG_HOME_KWARGS = { |
| 155 |
"dest": "gpg_home", |
| 156 |
"type": str, |
| 157 |
"metavar": "<path>", |
| 158 |
"help": ( |
| 159 |
"path to a GPG home directory used to load a GPG key identified" |
| 160 |
" by '--gpg'. If '--gpg-home' is not passed, the default GPG home" |
| 161 |
" directory is used." |
| 162 |
), |
| 163 |
} |
| 164 |
|
| 165 |
VERBOSE_ARGS = ["-v", "--verbose"] |
| 166 |
VERBOSE_KWARGS = { |
| 167 |
"dest": "verbose", |
| 168 |
"action": "store_true", |
| 169 |
"help": "show more output", |
| 170 |
} |
| 171 |
|
| 172 |
QUIET_ARGS = ["-q", "--quiet"] |
| 173 |
QUIET_KWARGS = { |
| 174 |
"dest": "quiet", |
| 175 |
"action": "store_true", |
| 176 |
"help": "suppress all output", |
| 177 |
} |
| 178 |
|
| 179 |
METADATA_DIRECTORY_ARGS = ["-d", "--metadata-directory"] |
| 180 |
METADATA_DIRECTORY_KWARGS = { |
| 181 |
"required": False, |
| 182 |
"type": str, |
| 183 |
"metavar": "<directory>", |
| 184 |
"help": ( |
| 185 |
"path to a directory to dump metadata. If '--metadata-directory'" |
| 186 |
" is not passed, the current working direcotry is used." |
| 187 |
), |
| 188 |
} |
| 189 |
|
| 190 |
DSSE_ARGS = ["--use-dsse"] |
| 191 |
DSSE_KWARGS = { |
| 192 |
"dest": "use_dsse", |
| 193 |
"default": False, |
| 194 |
"action": "store_true", |
| 195 |
"help": ("generate metadata using dsse (experimental)."), |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
def title_case_action_groups(parser): |
| 200 |
"""Capitalize the first character of all words in the title of each action |
| 201 |
group of the passed parser. |
| 202 |
|
| 203 |
This is useful for consistency when using the sphinx argparse extension, |
| 204 |
which title-cases default action groups only. |
| 205 |
|
| 206 |
""" |
| 207 |
for ( |
| 208 |
action_group |
| 209 |
) in parser._action_groups: # pylint: disable=protected-access |
| 210 |
action_group.title = action_group.title.title() |
| 211 |
|
| 212 |
|
| 213 |
def sort_action_groups(parser, title_order=None): |
| 214 |
"""Sort action groups of passed parser by their titles according to the |
| 215 |
passed (or a default) order. |
| 216 |
|
| 217 |
""" |
| 218 |
if title_order is None: |
| 219 |
title_order = [ |
| 220 |
"Required Named Arguments", |
| 221 |
"Positional Arguments", |
| 222 |
OPTS_TITLE, |
| 223 |
] |
| 224 |
|
| 225 |
action_group_dict = {} |
| 226 |
for ( |
| 227 |
action_group |
| 228 |
) in parser._action_groups: # pylint: disable=protected-access |
| 229 |
action_group_dict[action_group.title] = action_group |
| 230 |
|
| 231 |
ordered_action_groups = [] |
| 232 |
for title in title_order: |
| 233 |
ordered_action_groups.append(action_group_dict[title]) |
| 234 |
|
| 235 |
parser._action_groups = ( # pylint: disable=protected-access |
| 236 |
ordered_action_groups |
| 237 |
) |