|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +use crate::secret_matcher::SecretMatcher; |
| 4 | + |
| 5 | +/// Deserializable configuration for a CLI secret-provider integration. |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 7 | +#[serde(deny_unknown_fields)] |
| 8 | +pub struct CliSecretProviderConfig { |
| 9 | + /// Executable basename used to select this integration. |
| 10 | + pub binary_name: String, |
| 11 | + /// Stable identifier recorded for secrets from this integration. |
| 12 | + pub provider_id: String, |
| 13 | + /// Environment variables forwarded to authenticate the provider CLI. |
| 14 | + pub credential_env_vars: Vec<String>, |
| 15 | + /// Options ignored while identifying command words. Their value arity is |
| 16 | + /// honored, and the options remain unchanged in the executed command. |
| 17 | + #[serde(default)] |
| 18 | + pub stripped_options: Vec<FlagSpec>, |
| 19 | + /// Options that make an otherwise permitted invocation unsafe. An |
| 20 | + /// invocation containing one is blocked rather than silently changed. |
| 21 | + #[serde(default)] |
| 22 | + pub forbidden_options: Vec<FlagSpec>, |
| 23 | + /// Rules that classify invocations and configure secret extraction. |
| 24 | + pub matchers: Vec<CliMatcherRuleConfig>, |
| 25 | +} |
| 26 | + |
| 27 | +/// A command-line option that a CLI integration needs to recognize. |
| 28 | +/// |
| 29 | +/// ```toml |
| 30 | +/// { name = "--format", takes_value = true } |
| 31 | +/// { name = "--offline", takes_value = false } |
| 32 | +/// { name = "-u", takes_value = true, allow_attached_value = true } |
| 33 | +/// ``` |
| 34 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 35 | +#[serde(deny_unknown_fields)] |
| 36 | +pub struct FlagSpec { |
| 37 | + /// The option's spelling, such as `--server-url` or `-u`. |
| 38 | + pub name: String, |
| 39 | + /// Whether the option consumes the following argument as its value. |
| 40 | + pub takes_value: bool, |
| 41 | + /// Whether the spelling accepts an attached value without `=`, such as |
| 42 | + /// `-uhttps://example.com`. |
| 43 | + #[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 44 | + pub allow_attached_value: bool, |
| 45 | +} |
| 46 | + |
| 47 | +impl FlagSpec { |
| 48 | + /// Creates a specification for an option whose value is a separate |
| 49 | + /// argument or follows `=`. |
| 50 | + #[must_use] |
| 51 | + pub fn value(name: &str) -> Self { |
| 52 | + Self { |
| 53 | + name: String::from(name), |
| 54 | + takes_value: true, |
| 55 | + allow_attached_value: false, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Creates a specification for an option that takes no value. |
| 60 | + #[must_use] |
| 61 | + pub fn valueless(name: &str) -> Self { |
| 62 | + Self { |
| 63 | + name: String::from(name), |
| 64 | + takes_value: false, |
| 65 | + allow_attached_value: false, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Creates a specification for an option that also accepts a value |
| 70 | + /// attached directly to its name. |
| 71 | + #[must_use] |
| 72 | + pub fn attached_value(name: &str) -> Self { |
| 73 | + Self { |
| 74 | + name: String::from(name), |
| 75 | + takes_value: true, |
| 76 | + allow_attached_value: true, |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// One candidate rule for an [`CliSecretProviderConfig`]. |
| 82 | +/// |
| 83 | +/// Tagged by `type` (`sensitive_command` / `safe_command` / `blocked_command`) |
| 84 | +/// so it nests as a flat TOML table. |
| 85 | +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] |
| 86 | +#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)] |
| 87 | +pub enum CliMatcherRuleConfig { |
| 88 | + /// Response whose body must be scanned and redacted using `matcher`. |
| 89 | + SensitiveCommand { |
| 90 | + /// Command words, excluding the binary name. |
| 91 | + argv: Vec<String>, |
| 92 | + /// Whether trailing positional arguments are accepted. |
| 93 | + #[serde(rename = "match", default)] |
| 94 | + match_kind: CommandMatch, |
| 95 | + /// Matcher used to extract secrets from the normalized output. |
| 96 | + matcher: SecretMatcher, |
| 97 | + /// Output-shaping options skipped during matching and removed before |
| 98 | + /// [`CliMatcherRuleConfig::SensitiveCommand::append_options`] is applied. |
| 99 | + #[serde(default)] |
| 100 | + stripped_options: Vec<FlagSpec>, |
| 101 | + /// Options and values added to normalize output into the expected form. |
| 102 | + /// They are inserted before an end-of-options (`--`) marker when present. |
| 103 | + #[serde(default)] |
| 104 | + append_options: Vec<String>, |
| 105 | + }, |
| 106 | + /// Known-safe path whose response never carries secrets; forwarded |
| 107 | + /// unredacted. |
| 108 | + SafeCommand { |
| 109 | + /// Command words, excluding the binary name. |
| 110 | + argv: Vec<String>, |
| 111 | + /// Whether trailing positional arguments are accepted. |
| 112 | + #[serde(rename = "match", default)] |
| 113 | + match_kind: CommandMatch, |
| 114 | + }, |
| 115 | + /// Path that must always be denied. |
| 116 | + BlockedCommand { |
| 117 | + /// Command words, excluding the binary name. |
| 118 | + argv: Vec<String>, |
| 119 | + /// Whether trailing positional arguments are accepted. |
| 120 | + #[serde(rename = "match", default)] |
| 121 | + match_kind: CommandMatch, |
| 122 | + }, |
| 123 | +} |
| 124 | + |
| 125 | +/// Whether a command pattern permits trailing positional arguments. |
| 126 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 127 | +#[serde(rename_all = "snake_case")] |
| 128 | +pub enum CommandMatch { |
| 129 | + /// Only the listed command words may occur. Options may be interspersed. |
| 130 | + Exact, |
| 131 | + /// Additional positional arguments may follow the listed command words. |
| 132 | + #[default] |
| 133 | + Prefix, |
| 134 | +} |
0 commit comments