|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
| 3 | +//! Item-spec grammar for `vault exec` / `vault config exec`. |
| 4 | +//! |
| 5 | +//! An `[exec.profiles.*]` mapping stores `ENV_VAR = "<item spec>"`. The spec |
| 6 | +//! names a vault item and, optionally, which field to pull the value from: |
| 7 | +//! `<item name>` (defaults to the password field) or `<item name>#<field>` |
| 8 | +//! where `<field>` is `password`/`username`/`notes`/`totp`/`custom:<name>`. |
| 9 | +//! This module only parses that string into a [`vault_ipc::proto::Field`] |
| 10 | +//! selector — resolving it against the agent is `cmd_exec`'s job. |
| 11 | +
|
| 12 | +use vault_ipc::proto::Field; |
| 13 | + |
| 14 | +/// A parsed item spec: which item, and which of its fields. |
| 15 | +#[derive(Debug, PartialEq, Eq)] |
| 16 | +pub struct FieldSpec { |
| 17 | + /// Item name (decrypted form), matched like every other CLI selector. |
| 18 | + pub name: String, |
| 19 | + /// Which field to pull the value from. |
| 20 | + pub field: Field, |
| 21 | +} |
| 22 | + |
| 23 | +/// Parse an item spec (`"<item name>"` or `"<item name>#<field>"`). |
| 24 | +/// |
| 25 | +/// # Errors |
| 26 | +/// |
| 27 | +/// Returns a user-facing message when the item name is empty or the field |
| 28 | +/// keyword after `#` isn't one of `password`/`username`/`notes`/`totp`/ |
| 29 | +/// `custom:<name>`. |
| 30 | +pub fn parse_item_spec(raw: &str) -> Result<FieldSpec, String> { |
| 31 | + let (name, field_str) = match raw.split_once('#') { |
| 32 | + Some((name, field_str)) => (name.trim(), Some(field_str.trim())), |
| 33 | + None => (raw.trim(), None), |
| 34 | + }; |
| 35 | + if name.is_empty() { |
| 36 | + return Err(format!("empty item name in exec spec '{raw}'")); |
| 37 | + } |
| 38 | + let field = field_str.map_or(Ok(Field::Password), parse_field)?; |
| 39 | + Ok(FieldSpec { |
| 40 | + name: name.to_owned(), |
| 41 | + field, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +fn parse_field(spec: &str) -> Result<Field, String> { |
| 46 | + let lower = spec.to_ascii_lowercase(); |
| 47 | + match lower.as_str() { |
| 48 | + "password" => Ok(Field::Password), |
| 49 | + "username" => Ok(Field::Username), |
| 50 | + "notes" => Ok(Field::Notes), |
| 51 | + "totp" => Ok(Field::Totp), |
| 52 | + _ if lower.starts_with("custom:") => { |
| 53 | + let custom_name = spec["custom:".len()..].trim(); |
| 54 | + if custom_name.is_empty() { |
| 55 | + Err("custom field spec must include a name, e.g. 'custom:api_key'".to_owned()) |
| 56 | + } else { |
| 57 | + Ok(Field::Custom(custom_name.to_owned())) |
| 58 | + } |
| 59 | + } |
| 60 | + _ => Err(format!( |
| 61 | + "unknown exec field '{spec}' (expected password/username/notes/totp/custom:<name>)" |
| 62 | + )), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn bare_name_defaults_to_password() { |
| 72 | + let spec = parse_item_spec("Anthropic API Key").expect("parse"); |
| 73 | + assert_eq!(spec.name, "Anthropic API Key"); |
| 74 | + assert_eq!(spec.field, Field::Password); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn explicit_field_keywords_parse() { |
| 79 | + assert_eq!( |
| 80 | + parse_item_spec("Item#username").expect("parse").field, |
| 81 | + Field::Username |
| 82 | + ); |
| 83 | + assert_eq!( |
| 84 | + parse_item_spec("Item#notes").expect("parse").field, |
| 85 | + Field::Notes |
| 86 | + ); |
| 87 | + assert_eq!( |
| 88 | + parse_item_spec("Item#totp").expect("parse").field, |
| 89 | + Field::Totp |
| 90 | + ); |
| 91 | + // Case-insensitive keyword matching. |
| 92 | + assert_eq!( |
| 93 | + parse_item_spec("Item#PASSWORD").expect("parse").field, |
| 94 | + Field::Password |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn custom_field_preserves_name_case() { |
| 100 | + let spec = parse_item_spec("Brave Search#custom:API_Key").expect("parse"); |
| 101 | + assert_eq!(spec.field, Field::Custom("API_Key".to_owned())); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn whitespace_around_name_and_field_is_trimmed() { |
| 106 | + let spec = parse_item_spec(" Item Name # username ").expect("parse"); |
| 107 | + assert_eq!(spec.name, "Item Name"); |
| 108 | + assert_eq!(spec.field, Field::Username); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn rejects_empty_name_and_unknown_field() { |
| 113 | + assert!(parse_item_spec("#password").is_err()); |
| 114 | + assert!(parse_item_spec("Item#bogus").is_err()); |
| 115 | + assert!(parse_item_spec("Item#custom:").is_err()); |
| 116 | + } |
| 117 | +} |
0 commit comments