bwx-cli 2.3.0

Unofficial Bitwarden CLI with first-class macOS support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::commands;

#[derive(Debug, clap::Args)]
pub struct FindArgs {
    #[arg(help = "Name, URI or UUID of the entry to display", value_parser = commands::parse_needle)]
    pub needle: commands::Needle,
    #[arg(help = "Username of the entry to display")]
    pub user: Option<String>,
    #[arg(long, help = "Folder name to search in")]
    pub folder: Option<String>,
    #[arg(short, long, help = "Ignore case")]
    pub ignorecase: bool,
}

#[derive(Debug, clap::Parser)]
#[command(version, about = "Unofficial Bitwarden CLI")]
pub enum Opt {
    #[command(about = "Get or set configuration options")]
    Config {
        #[command(subcommand)]
        config: Config,
    },

    #[command(
        about = "Register this device with the Bitwarden server",
        long_about = "Register this device with the Bitwarden server\n\n\
            The official Bitwarden server includes bot detection to prevent \
            brute force attacks. In order to avoid being detected as bot \
            traffic, you will need to use this command to log in with your \
            personal API key (instead of your password) first before regular \
            logins will work."
    )]
    Register,

    #[command(about = "Log in to the Bitwarden server")]
    Login,

    #[command(about = "Unlock the local Bitwarden database")]
    Unlock,

    #[command(about = "Check if the local Bitwarden database is unlocked")]
    Unlocked,

    #[command(about = "Update the local copy of the Bitwarden database")]
    Sync,

    #[command(
        about = "List all entries in the local Bitwarden database",
        visible_alias = "ls"
    )]
    List {
        #[arg(
            long,
            help = "Fields to display. \
                Available options are id, name, user, folder, type. \
                Multiple fields will be separated by tabs.",
            default_value = "name",
            use_value_delimiter = true
        )]
        fields: Vec<String>,
        #[structopt(long, help = "Display output as JSON")]
        raw: bool,
    },

    #[command(about = "Display the password for a given entry")]
    Get {
        #[command(flatten)]
        find_args: FindArgs,
        #[arg(short, long, help = "Field to get")]
        field: Option<String>,
        #[arg(long, help = "Display the notes in addition to the password")]
        full: bool,
        #[structopt(long, help = "Display output as JSON")]
        raw: bool,
        #[cfg(feature = "clipboard")]
        #[structopt(short, long, help = "Copy result to clipboard")]
        clipboard: bool,
        #[structopt(short, long, help = "List fields in this entry")]
        list_fields: bool,
    },

    #[command(about = "Search for entries")]
    Search {
        #[arg(help = "Search term to locate entries")]
        term: String,
        #[arg(
            long,
            help = "Fields to display. \
                Available options are id, name, user, folder. \
                Multiple fields will be separated by tabs.",
            default_value = "name",
            use_value_delimiter = true
        )]
        fields: Vec<String>,
        #[arg(long, help = "Folder name to search in")]
        folder: Option<String>,
        #[structopt(long, help = "Display output as JSON")]
        raw: bool,
    },

    #[command(
        about = "Display the authenticator code for a given entry",
        visible_alias = "totp"
    )]
    Code {
        #[command(flatten)]
        find_args: FindArgs,
        #[cfg(feature = "clipboard")]
        #[structopt(long, help = "Copy result to clipboard")]
        clipboard: bool,
    },

    #[command(
        about = "Add a new password to the database",
        long_about = "Add a new password to the database\n\n\
            This command will open a text editor to enter \
            the password and notes. The editor to use is determined \
            by the value of the $VISUAL or $EDITOR environment variables.
            The first line will be saved as the password and the \
            remainder will be saved as a note."
    )]
    Add {
        #[arg(help = "Name of the password entry")]
        name: String,
        #[arg(help = "Username for the password entry")]
        user: Option<String>,
        #[arg(
            long,
            help = "URI for the password entry",
            number_of_values = 1
        )]
        uri: Vec<String>,
        #[arg(long, help = "Folder for the password entry")]
        folder: Option<String>,
    },

    #[command(
        about = "Generate a new password",
        long_about = "Generate a new password\n\n\
            If given a password entry name, also save the generated \
            password to the database.",
        visible_alias = "gen",
        group = clap::ArgGroup::new("password-type").args(&[
            "no_symbols",
            "only_numbers",
            "nonconfusables",
            "diceware",
        ])
    )]
    Generate {
        #[arg(help = "Length of the password to generate")]
        len: usize,
        #[arg(help = "Name of the password entry")]
        name: Option<String>,
        #[arg(help = "Username for the password entry")]
        user: Option<String>,
        #[arg(
            long,
            help = "URI for the password entry",
            number_of_values = 1
        )]
        uri: Vec<String>,
        #[arg(long, help = "Folder for the password entry")]
        folder: Option<String>,
        #[arg(
            long = "no-symbols",
            help = "Generate a password with no special characters"
        )]
        no_symbols: bool,
        #[arg(
            long = "only-numbers",
            help = "Generate a password consisting of only numbers"
        )]
        only_numbers: bool,
        #[arg(
            long,
            help = "Generate a password without visually similar \
                characters (useful for passwords intended to be \
                written down)"
        )]
        nonconfusables: bool,
        #[arg(
            long,
            help = "Generate a password of multiple dictionary \
                words chosen from the EFF word list. The len \
                parameter for this option will set the number \
                of words to generate, rather than characters."
        )]
        diceware: bool,
    },

    #[command(
        about = "Modify an existing password",
        long_about = "Modify an existing password\n\n\
            This command will open a text editor with the existing \
            password and notes of the given entry for editing. \
            The editor to use is determined  by the value of the \
            $VISUAL or $EDITOR environment variables. The first line \
            will be saved as the password and the remainder will be saved \
            as a note."
    )]
    Edit {
        #[command(flatten)]
        find_args: FindArgs,
    },

    #[command(about = "Remove a given entry", visible_alias = "rm")]
    Remove {
        #[command(flatten)]
        find_args: FindArgs,
    },

    #[command(about = "View the password history for a given entry")]
    History {
        #[command(flatten)]
        find_args: FindArgs,
    },

    #[command(
        about = "Run a command with vault fields injected as env vars",
        long_about = "Run a command with vault fields injected as \
            environment variables.\n\n\
            Each `--env VAR=ENTRY[#FIELD]` resolves a vault entry's \
            field (defaulting to `password`) and binds it to `VAR` for \
            the child process. The value is passed via execve() only — \
            it is never written to disk, and the parent zeroizes its \
            in-process copy as soon as the child is spawned. Useful as \
            a drop-in replacement for `direnv` + plaintext `.env` \
            files.\n\n\
            Examples:\n  \
                bwx exec --env DB_URL=db/prod#uri -- terraform apply\n  \
                bwx exec --env GH=github.com -- gh pr list"
    )]
    Exec {
        #[arg(
            long = "env",
            value_name = "VAR=ENTRY[#FIELD]",
            help = "Bind a vault field to an environment variable; \
                may be given multiple times",
            action = clap::ArgAction::Append,
            required = true,
        )]
        env: Vec<String>,
        #[arg(long, help = "Folder to scope entry lookups to")]
        folder: Option<String>,
        #[arg(short, long, help = "Ignore case when matching entry names")]
        ignorecase: bool,
        #[arg(
            trailing_var_arg = true,
            allow_hyphen_values = true,
            num_args = 1..,
            value_name = "CMD",
            help = "Command and arguments to execute (place after `--`)"
        )]
        command: Vec<String>,
    },

    #[command(about = "Lock the password database")]
    Lock,

    #[command(about = "Remove the local copy of the password database")]
    Purge,

    #[command(name = "stop-agent", about = "Terminate the background agent")]
    StopAgent,

    #[command(
        name = "ssh-public-key",
        about = "Print the OpenSSH public key of an SSH key entry"
    )]
    SshPublicKey {
        #[command(flatten)]
        find_args: FindArgs,
    },

    #[command(
        name = "ssh-allowed-signers",
        about = "Print an allowed_signers file for every SSH key in the vault",
        long_about = "Print an allowed_signers file suitable for \
            `ssh-keygen -Y verify`. Each line is `<email> <public-key>`. \
            Pipe into a file and point git's \
            `gpg.ssh.allowedSignersFile` at it."
    )]
    SshAllowedSigners,

    #[command(
        name = "ssh-socket",
        about = "Print the filesystem path of bwx-agent's ssh-agent socket",
        long_about = "Print the filesystem path of bwx-agent's ssh-agent \
            socket, suitable for export to `SSH_AUTH_SOCK`. Honors \
            `BWX_PROFILE` and `XDG_RUNTIME_DIR`."
    )]
    SshSocket,

    #[cfg(target_os = "macos")]
    #[command(name = "touchid", about = "Manage macOS Touch ID enrollment")]
    TouchId {
        #[command(subcommand)]
        cmd: TouchIdCmd,
    },

    #[cfg(target_os = "macos")]
    #[command(
        name = "setup-macos",
        about = "Install the bwx-agent LaunchAgent + set SSH_AUTH_SOCK \
            for GUI apps",
        long_about = "One-shot macOS environment setup. Writes a \
            LaunchAgent plist that exports `SSH_AUTH_SOCK` to the \
            current login session at every login, so Finder/Spotlight-\
            launched GUI apps (IDEs, git clients) see bwx-agent's ssh \
            socket. Also runs `launchctl setenv` for the current \
            session so existing GUI apps can be Cmd-Q'd and relaunched \
            without a logout."
    )]
    SetupMacos {
        #[arg(long, help = "Overwrite any existing LaunchAgent file")]
        force: bool,
    },

    #[cfg(target_os = "macos")]
    #[command(
        name = "teardown-macos",
        about = "Uninstall what `bwx setup-macos` created"
    )]
    TeardownMacos,

    #[command(
        name = "gen-completions",
        about = "Generate completion script for the given shell"
    )]
    GenCompletions { shell: CompletionShell },
}

impl Opt {
    /// Human-readable context surfaced in Touch ID / pinentry prompts on
    /// the agent side. Includes the needle when one argument uniquely
    /// identifies the target.
    pub fn purpose(&self) -> String {
        match self {
            Self::Get { find_args, .. }
            | Self::Code { find_args, .. }
            | Self::Edit { find_args }
            | Self::Remove { find_args }
            | Self::History { find_args }
            | Self::SshPublicKey { find_args } => {
                format!("bwx {} {}", self.subcommand_name(), find_args.needle)
            }
            Self::Search { term, .. } => format!("bwx search {term}"),
            Self::Add { name, .. }
            | Self::Generate {
                name: Some(name), ..
            } => {
                format!("bwx {} {name}", self.subcommand_name())
            }
            _ => format!("bwx {}", self.subcommand_name()),
        }
    }

    pub fn subcommand_name(&self) -> String {
        match self {
            Self::Config { config } => {
                format!("config {}", config.subcommand_name())
            }
            Self::Register => "register".to_string(),
            Self::Login => "login".to_string(),
            Self::Unlock => "unlock".to_string(),
            Self::Unlocked => "unlocked".to_string(),
            Self::Sync => "sync".to_string(),
            Self::List { .. } => "list".to_string(),
            Self::Get { .. } => "get".to_string(),
            Self::Search { .. } => "search".to_string(),
            Self::Code { .. } => "code".to_string(),
            Self::Add { .. } => "add".to_string(),
            Self::Generate { .. } => "generate".to_string(),
            Self::Edit { .. } => "edit".to_string(),
            Self::Remove { .. } => "remove".to_string(),
            Self::History { .. } => "history".to_string(),
            Self::Exec { .. } => "exec".to_string(),
            Self::Lock => "lock".to_string(),
            Self::Purge => "purge".to_string(),
            Self::StopAgent => "stop-agent".to_string(),
            Self::SshPublicKey { .. } => "ssh-public-key".to_string(),
            Self::SshAllowedSigners => "ssh-allowed-signers".to_string(),
            Self::SshSocket => "ssh-socket".to_string(),
            #[cfg(target_os = "macos")]
            Self::TouchId { cmd } => {
                format!("touchid {}", cmd.subcommand_name())
            }
            #[cfg(target_os = "macos")]
            Self::SetupMacos { .. } => "setup-macos".to_string(),
            #[cfg(target_os = "macos")]
            Self::TeardownMacos => "teardown-macos".to_string(),
            Self::GenCompletions { .. } => "gen-completions".to_string(),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, clap::ValueEnum)]
pub enum CompletionShell {
    Bash,
    Zsh,
    Fish,
    Powershell,
    Elvish,
}

#[cfg(target_os = "macos")]
#[derive(Debug, clap::Parser)]
pub enum TouchIdCmd {
    #[command(
        about = "Enroll the current vault under a Touch ID-gated Keychain \
            wrapper key"
    )]
    Enroll,
    #[command(
        about = "Remove the Touch ID enrollment (Keychain item + blob)"
    )]
    Disable,
    #[command(about = "Show the current Touch ID enrollment status")]
    Status,
}

#[cfg(target_os = "macos")]
impl TouchIdCmd {
    fn subcommand_name(&self) -> &'static str {
        match self {
            Self::Enroll => "enroll",
            Self::Disable => "disable",
            Self::Status => "status",
        }
    }
}

/// Every config key recognised by `bwx config show/set/unset`. Kept in
/// sync with the match arms in `commands/config.rs`; surfaced here so
/// shell completions (`bwx gen-completions <shell>`) can offer the
/// valid keys without the user having to remember them.
pub const CONFIG_KEYS: &[&str] = &[
    "email",
    "sso_id",
    "base_url",
    "identity_url",
    "ui_url",
    "notifications_url",
    "client_cert_path",
    "lock_timeout",
    "sync_interval",
    "pinentry",
    "ssh_confirm_sign",
    "macos_unlock_dialog",
    "logging",
    "touchid_gate",
];

#[derive(Debug, clap::Parser)]
pub enum Config {
    #[command(
        about = "Show configuration settings",
        long_about = "Without arguments, print all configuration \
            settings as JSON. With a key argument, print just that \
            key's current value in plain text."
    )]
    Show {
        #[arg(
            help = "Configuration key to read (omit to dump all)",
            value_parser = clap::builder::PossibleValuesParser::new(CONFIG_KEYS),
        )]
        key: Option<String>,
    },
    #[command(about = "Set a configuration option")]
    Set {
        #[arg(
            help = "Configuration key to set",
            value_parser = clap::builder::PossibleValuesParser::new(CONFIG_KEYS),
        )]
        key: String,
        #[arg(help = "Value to set the configuration option to")]
        value: String,
    },
    #[command(about = "Reset a configuration option to its default")]
    Unset {
        #[arg(
            help = "Configuration key to unset",
            value_parser = clap::builder::PossibleValuesParser::new(CONFIG_KEYS),
        )]
        key: String,
    },
}

impl Config {
    fn subcommand_name(&self) -> String {
        match self {
            Self::Show { .. } => "show",
            Self::Set { .. } => "set",
            Self::Unset { .. } => "unset",
        }
        .to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser as _;

    #[test]
    fn config_set_accepts_each_known_key() {
        for key in CONFIG_KEYS {
            let res =
                Opt::try_parse_from(["bwx", "config", "set", key, "value"]);
            assert!(res.is_ok(), "key {key} was rejected: {res:?}");
        }
    }

    #[test]
    fn config_set_rejects_unknown_key() {
        let res = Opt::try_parse_from(["bwx", "config", "set", "bogus", "x"]);
        assert!(res.is_err());
    }

    #[test]
    fn config_unset_rejects_unknown_key() {
        let res = Opt::try_parse_from(["bwx", "config", "unset", "nope"]);
        assert!(res.is_err());
    }
}