Debian Codegraph @

src/cli/keyring.rs

raw
1 //! Command-line parser for `sq keyring`.
2
3 use std::path::PathBuf;
4
5 use clap::{Args, Parser, Subcommand};
6
7 use sequoia_openpgp as openpgp;
8 use openpgp::KeyHandle;
9
10 use crate::cli::examples::*;
11 use crate::cli::types::ClapData;
12 use crate::cli::types::FileOrStdin;
13 use crate::cli::types::FileOrStdout;
14
15 #[derive(Parser, Debug)]
16 #[clap(
17 name = "keyring",
18 about = "Manage collections of keys or certs",
19 long_about =
20 "Manage collections of keys or certs
21
22 Collections of keys or certificates (also known as \"keyrings\" when \
23 they contain secret key material, and \"certrings\" when they don't) are \
24 any number of concatenated certificates. This subcommand provides \
25 tools to list, split, merge, and filter keyrings.
26
27 Note: In the documentation of this subcommand, we sometimes use the \
28 terms keys and certs interchangeably.
29 ",
30 subcommand_required = true,
31 arg_required_else_help = true,
32 disable_help_subcommand = true,
33 )]
34 pub struct Command {
35 #[clap(subcommand)]
36 pub subcommand: Subcommands,
37 }
38
39 #[derive(Debug, Subcommand)]
40 pub enum Subcommands {
41 List(ListCommand),
42 Split(SplitCommand),
43 Merge(MergeCommand),
44 Filter(FilterCommand),
45 }
46
47 #[derive(Debug, Args)]
48 #[clap(
49 about = "Join keys into a keyring applying a filter",
50 long_about =
51 "Join keys into a keyring applying a filter
52
53 This can be used to filter keys based on given predicates, \
54 e.g. whether they have a user id containing an email address with a \
55 certain domain. Additionally, the keys can be pruned to only include \
56 components matching the predicates.
57
58 If no filters are supplied, everything matches.
59
60 If multiple predicates are given, they are or'ed, i.e., a key matches \
61 if any of the predicates match. To require all predicates to match, \
62 chain multiple invocations of this command. See EXAMPLES for \
63 inspiration.
64
65 Note: this command is considered experimental and may change in future \
66 releases. To acknowledge this, you must give the `--experimental` \
67 flag when invoking this command.
68 ",
69 after_help = FILTER_EXAMPLES,
70 )]
71 pub struct FilterCommand {
72 #[clap(
73 long = "experimental",
74 required = true,
75 help = "Opt-in to using an experimental feature",
76 long_help = "\
77 Opt-in to using an experimental feature
78
79 This command is considered experimental and may change in future \
80 releases. To acknowledge this, you must give the `--experimental` \
81 flag when invoking this command.
82
83 In the future, we may stabilize this command. When that happens, \
84 `--experimental` will no longer be required, but will be ignored \
85 silently.
86 ",
87 )]
88 pub _experimental: bool,
89
90 #[clap(value_name = "FILE", help = "Read from FILE or stdin if omitted")]
91 pub input: Vec<PathBuf>,
92 #[clap(
93 default_value_t = FileOrStdout::default(),
94 help = FileOrStdout::HELP_OPTIONAL,
95 long,
96 value_name = FileOrStdout::VALUE_NAME,
97 )]
98 pub output: FileOrStdout,
99 #[clap(
100 long = "userid",
101 value_name = "USERID",
102 help = "Match on USERID",
103 long_help = "Match on USERID
104
105 Case-sensitively matches on the \
106 user ID, requiring an exact match.",
107 )]
108 pub userid: Vec<String>,
109 #[clap(
110 long = "name",
111 value_name = "NAME",
112 help = "Match on NAME",
113 long_help = "Match on NAME
114
115 Parse user IDs into name and email \
116 and case-sensitively matches on the \
117 name, requiring an exact match.",
118 )]
119 pub name: Vec<String>,
120 #[clap(
121 long = "email",
122 value_name = "ADDRESS",
123 help = "Match on email ADDRESS",
124 long_help = "Match on email ADDRESS
125
126 Parse user IDs into name and email \
127 address and case-sensitively matches \
128 on the email address, requiring an exact match.",
129 )]
130 pub email: Vec<String>,
131 #[clap(
132 long = "domain",
133 value_name = "FQDN",
134 help = "Match on email domain FQDN",
135 long_help =
136 "Match on email domain FQDN
137
138 Parse user IDs into name and email \
139 address and case-sensitively matches \
140 on the domain of the email address, \
141 requiring an exact match.",
142 )]
143 pub domain: Vec<String>,
144
145 #[clap(
146 long = "cert",
147 value_name = "FINGERPRINT|KEYID",
148 help = "Match on certificate fingerprints and key IDs",
149 long_help =
150 "Match on certificate fingerprints and key IDs
151
152 Match on primary keys, \
153 including those certificates that match the \
154 given fingerprint or key ID.",
155 )]
156 pub cert: Vec<KeyHandle>,
157
158 #[clap(
159 long = "key",
160 value_name = "FINGERPRINT|KEYID",
161 help = "Match on (sub)key fingerprints and key IDs",
162 long_help =
163 "Match on (sub)key fingerprints and key IDs
164
165 Match on both primary keys and subkeys, \
166 including those certificates that match the \
167 given fingerprint or key ID.",
168 )]
169 pub key: Vec<KeyHandle>,
170
171 #[clap(
172 long = "prune-certs",
173 help = "Remove certificate components not matching the filter",
174 )]
175 pub prune_certs: bool,
176
177 #[clap(
178 long = "to-cert",
179 help = "Convert any keys in the input to certificates",
180 long_help =
181 "Convert any keys in the input to certificates
182
183 Converting a key to a \
184 certificate removes secret key material \
185 from the key thereby turning it into \
186 a certificate.",
187 )]
188 pub to_certificate: bool,
189 }
190
191 const FILTER_EXAMPLES: Actions = Actions {
192 actions: &[
193 Action::setup().command(&[
194 "sq", "keyring", "merge",
195 "--output=certs.pgp",
196 "bob.pgp", "romeo.pgp",
197 ]).build(),
198
199 Action::example().comment(
200 "Convert all keys to certificates (i.e. remove any secret key material).",
201 ).command(&[
202 "sq", "keyring", "filter",
203 "--experimental",
204 "--to-cert",
205 "certs.pgp",
206 ]).build(),
207
208 Action::example().comment(
209 "Get all certificates with a user ID on example.org.",
210 ).command(&[
211 "sq", "keyring", "filter",
212 "--experimental",
213 "--domain=example.org",
214 "certs.pgp",
215 ]).build(),
216
217 Action::example().comment(
218 "Get all certificates with a user ID on example.org or example.net.",
219 ).command(&[
220 "sq", "keyring", "filter",
221 "--experimental",
222 "--domain=example.org",
223 "--domain=example.net",
224 "certs.pgp",
225 ]).build(),
226
227 Action::example().comment(
228 "Get all certificates with a name user ID matching Romeo.",
229 ).command(&[
230 "sq", "keyring", "filter",
231 "--experimental",
232 "--name=Romeo",
233 "certs.pgp",
234 ]).build(),
235
236 Action::example().comment(
237 "Get all certificates with a name user ID matching Romeo on example.org.",
238 ).command(&[
239 "sq", "keyring", "filter",
240 "--experimental",
241 "--domain=example.org",
242 "certs.pgp",
243 "|", "sq", "keyring", "filter",
244 "--experimental",
245 "--name=Romeo",
246 ]).build(),
247
248 Action::example().comment(
249 "Get all certificates with a user ID on example.org, pruning other user IDs.",
250 ).command(&[
251 "sq", "keyring", "filter",
252 "--experimental",
253 "--domain=example.org",
254 "--prune-certs",
255 "certs.pgp",
256 ]).build(),
257 ],
258 };
259 test_examples!(sq_keyring_filter, FILTER_EXAMPLES);
260
261 #[derive(Debug, Args)]
262 #[clap(
263 about = "Merge keys or keyrings into a single keyring",
264 long_about =
265 "Merge keys or keyrings into a single keyring
266
267 Multiple \
268 versions of the same certificate are merged together. Where data is \
269 replaced (e.g., secret key material), data from the later certificate \
270 is preferred.
271 ",
272 after_help = MERGE_EXAMPLES,
273 )]
274 pub struct MergeCommand {
275 #[clap(value_name = "FILE", help = "Read from FILE or stdin if omitted")]
276 pub input: Vec<PathBuf>,
277 #[clap(
278 default_value_t = FileOrStdout::default(),
279 help = FileOrStdout::HELP_OPTIONAL,
280 long,
281 value_name = FileOrStdout::VALUE_NAME,
282 )]
283 pub output: FileOrStdout,
284 }
285
286 const MERGE_EXAMPLES: Actions = Actions {
287 actions: &[
288 Action::setup().command(&[
289 "sq", "packet", "dearmor",
290 "--output=bob-updates.pgp",
291 "bob.pgp",
292 ]).build(),
293
294 Action::example().comment(
295 "Merge certificate updates.",
296 ).command(&[
297 "sq", "keyring", "merge",
298 "bob.pgp", "bob-updates.pgp",
299 ]).build(),
300 ],
301 };
302 test_examples!(sq_keyring_merge, MERGE_EXAMPLES);
303
304 #[derive(Debug, Args)]
305 #[clap(
306 about = "List keys in a keyring",
307 long_about =
308 "List keys in a keyring
309
310 Prints the fingerprint as well as the primary userid for every \
311 certificate encountered in the keyring.
312 ",
313 after_help = LIST_EXAMPLES,
314 )]
315 pub struct ListCommand {
316 #[clap(
317 default_value_t = FileOrStdin::default(),
318 help = FileOrStdin::HELP_OPTIONAL,
319 value_name = FileOrStdin::VALUE_NAME,
320 )]
321 pub input: FileOrStdin,
322 #[clap(
323 long = "all-userids",
324 help = "List all user IDs",
325 long_help = "List all user IDs
326
327 Include user IDs that are \
328 expired, revoked, or not valid under the \
329 standard policy.",
330 )]
331 pub all_userids: bool,
332 }
333
334 const LIST_EXAMPLES: Actions = Actions {
335 actions: &[
336 Action::setup().command(&[
337 "sq", "keyring", "merge",
338 "--output=certs.pgp",
339 "bob.pgp", "romeo.pgp",
340 ]).build(),
341
342 Action::example().comment(
343 "List all certificates.",
344 ).command(&[
345 "sq", "keyring", "list",
346 "certs.pgp",
347 ]).build(),
348
349 Action::example().comment(
350 "List all certificates with a user ID on example.org.",
351 ).command(&[
352 "sq", "keyring", "filter",
353 "--experimental",
354 "--domain=example.org",
355 "certs.pgp",
356 "|", "sq", "keyring", "list",
357 ]).build(),
358 ],
359 };
360 test_examples!(sq_keyring_list, LIST_EXAMPLES);
361
362 #[derive(Debug, Args)]
363 #[clap(
364 about = "Split a keyring into individual keys",
365 long_about =
366 "Split a keyring into individual keys
367
368 Splitting up a keyring into individual keys helps with curating a \
369 keyring.
370
371 The converse operation is `sq keyring merge`.
372 ",
373 after_help = SPLIT_EXAMPLES,
374 )]
375 pub struct SplitCommand {
376 #[clap(
377 default_value_t = FileOrStdin::default(),
378 help = FileOrStdin::HELP_OPTIONAL,
379 value_name = FileOrStdin::VALUE_NAME,
380 )]
381 pub input: FileOrStdin,
382 #[clap(
383 long = "prefix",
384 value_name = "PREFIX",
385 help = "Write to files with PREFIX \
386 [defaults: `FILE-` if FILE is set, or `output-` if read from stdin]",
387 )]
388 pub prefix: Option<String>,
389 }
390
391 const SPLIT_EXAMPLES: Actions = Actions {
392 actions: &[
393 Action::setup().command(&[
394 "sq", "keyring", "merge",
395 "--output=certs.pgp",
396 "bob.pgp", "romeo.pgp",
397 ]).build(),
398
399 Action::example().comment(
400 "Split all certificates.",
401 ).command(&[
402 "sq", "keyring", "split",
403 "certs.pgp",
404 ]).build(),
405
406 Action::example().comment(
407 "Split all certificates, merging them first to avoid duplicates.",
408 ).command(&[
409 "sq", "keyring", "merge",
410 "certs.pgp",
411 "|", "sq", "keyring", "split",
412 ]).build(),
413 ],
414 };
415 test_examples!(sq_keyring_split, SPLIT_EXAMPLES);