secretspec 0.9.1

Declarative secrets, every environment, any provider
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
use crate::provider::{Provider, ProviderUrl};
use crate::{Result, SecretSpecError};
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::process::{Command, Stdio};

/// Configuration for the LastPass provider.
///
/// This struct contains the configuration options for interacting with LastPass
/// through the `lpass` CLI tool.
///
/// # Examples
///
/// ```ignore
/// use secretspec::provider::lastpass::LastPassConfig;
///
/// // Create a default configuration
/// let config = LastPassConfig::default();
///
/// // Create a configuration with a folder prefix
/// let config = LastPassConfig {
///     folder_prefix: Some("my-company".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LastPassConfig {
    /// Optional folder prefix format string for organizing secrets in LastPass.
    ///
    /// Supports placeholders: {project}, {profile}, and {key}.
    /// Defaults to "secretspec/{project}/{profile}/{key}" if not specified.
    pub folder_prefix: Option<String>,
}

impl Default for LastPassConfig {
    /// Creates a default LastPassConfig with no folder prefix.
    fn default() -> Self {
        Self {
            folder_prefix: None,
        }
    }
}

impl TryFrom<&ProviderUrl> for LastPassConfig {
    type Error = SecretSpecError;

    /// Creates a LastPassConfig from a URL.
    ///
    /// Parses a URL in the format `lastpass://[folder]` where the folder
    /// component is optional. The folder can be specified either as the
    /// authority or the path component of the URL.
    fn try_from(url: &ProviderUrl) -> std::result::Result<Self, Self::Error> {
        if url.scheme() != "lastpass" {
            return Err(SecretSpecError::ProviderOperationFailed(format!(
                "Invalid scheme '{}' for lastpass provider",
                url.scheme()
            )));
        }

        let mut config = Self::default();

        if let Some(host) = url.host() {
            config.folder_prefix = Some(format!("{}{}", host, url.path()));
        }

        Ok(config)
    }
}

/// LastPass provider implementation for SecretSpec.
///
/// This provider integrates with LastPass password manager through the `lpass` CLI tool.
/// It stores secrets in a hierarchical structure within LastPass using a configurable
/// format string that defaults to: `secretspec/{project}/{profile}/{key}`.
///
/// # Requirements
///
/// The LastPass CLI (`lpass`) must be installed and the user must be logged in:
/// - macOS: `brew install lastpass-cli`
/// - Linux: Use your package manager (e.g., `apt install lastpass-cli`)
/// - NixOS: `nix-env -iA nixpkgs.lastpass-cli`
///
/// After installation, authenticate with: `lpass login <your-email>`
///
/// # Examples
///
/// ```ignore
/// use secretspec::provider::lastpass::{LastPassProvider, LastPassConfig};
///
/// // Create provider with default config
/// let provider = LastPassProvider::default();
///
/// // Create provider with custom config
/// let config = LastPassConfig {
///     folder_prefix: Some("work".to_string()),
/// };
/// let provider = LastPassProvider::new(config);
/// ```
pub struct LastPassProvider {
    #[allow(dead_code)]
    config: LastPassConfig,
}

crate::register_provider! {
    struct: LastPassProvider,
    config: LastPassConfig,
    name: "lastpass",
    description: "LastPass password manager",
    schemes: ["lastpass"],
    examples: ["lastpass://", "lastpass://Shared-SecretSpec"],
    preflight: check_auth,
}

impl LastPassProvider {
    /// Creates a new LastPassProvider with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The LastPass configuration to use
    pub fn new(config: LastPassConfig) -> Self {
        Self { config }
    }

    /// Executes a LastPass CLI command and returns its output.
    ///
    /// This is the core method for interacting with the LastPass CLI. It handles
    /// command execution, error detection, and provides helpful error messages
    /// for common issues like missing CLI installation or authentication.
    ///
    /// # Arguments
    ///
    /// * `args` - Command line arguments to pass to `lpass`
    ///
    /// # Returns
    ///
    /// Returns the command's stdout as a String on success, or an error with
    /// detailed information about what went wrong.
    ///
    /// # Errors
    ///
    /// - Returns an error if the `lpass` CLI is not installed
    /// - Returns an error if the user is not logged in to LastPass
    /// - Returns an error if the command fails for any other reason
    fn execute_lpass_command(&self, args: &[&str]) -> Result<String> {
        let mut cmd = Command::new("lpass");
        cmd.args(args);

        let output = match cmd.output() {
            Ok(output) => output,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                return Err(SecretSpecError::ProviderOperationFailed(
                    "LastPass CLI (lpass) is not installed.\n\nTo install it:\n  - macOS: brew install lastpass-cli\n  - Linux: Check your package manager (apt install lastpass-cli, yum install lastpass-cli, etc.)\n  - NixOS: nix-env -iA nixpkgs.lastpass-cli\n\nAfter installation, run 'lpass login <your-email>' to authenticate.".to_string(),
                ));
            }
            Err(e) => return Err(e.into()),
        };

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            if error_msg.contains("Could not find decryption key")
                || error_msg.contains("Not logged in")
            {
                return Err(SecretSpecError::ProviderOperationFailed(
                    "LastPass authentication required. Please run 'lpass login' first.".to_string(),
                ));
            }
            return Err(SecretSpecError::ProviderOperationFailed(
                error_msg.to_string(),
            ));
        }

        String::from_utf8(output.stdout)
            .map_err(|e| SecretSpecError::ProviderOperationFailed(e.to_string()))
    }

    /// Formats the item name for storage in LastPass.
    ///
    /// Creates a hierarchical path for organizing secrets within LastPass.
    /// Uses folder_prefix as a format string with {project}, {profile}, and {key} placeholders.
    /// Defaults to "secretspec/{project}/{profile}/{key}" if not configured.
    ///
    /// # Arguments
    ///
    /// * `project` - The project name
    /// * `key` - The secret key name
    /// * `profile` - The profile name (e.g., "default", "production", "staging")
    ///
    /// # Returns
    ///
    /// A formatted string representing the full path to the secret in LastPass.
    fn format_item_name(&self, project: &str, key: &str, profile: &str) -> String {
        let format_string = self
            .config
            .folder_prefix
            .as_deref()
            .unwrap_or("secretspec/{project}/{profile}/{key}");

        format_string
            .replace("{project}", project)
            .replace("{profile}", profile)
            .replace("{key}", key)
    }

    /// Checks the current LastPass login status.
    ///
    /// Executes `lpass status` to determine if the user is currently logged in.
    ///
    /// # Returns
    ///
    /// Returns `Ok(true)` if logged in, `Ok(false)` if not logged in, or an error
    /// if the status check itself fails.
    fn check_login_status(&self) -> Result<bool> {
        match self.execute_lpass_command(&["status"]) {
            Ok(output) => Ok(!output.contains("Not logged in")),
            Err(SecretSpecError::ProviderOperationFailed(msg))
                if msg.contains("Not logged in")
                    || msg.contains("LastPass authentication required") =>
            {
                Ok(false)
            }
            Err(e) => Err(e),
        }
    }

    /// Checks that the user is logged in to LastPass.
    /// Called by the preflight guard before any provider operations.
    pub(crate) fn check_auth(&self) -> Result<()> {
        if !self.check_login_status()? {
            return Err(SecretSpecError::ProviderOperationFailed(
                "LastPass authentication required. Please run 'lpass login <your-email>' first."
                    .to_string(),
            ));
        }
        Ok(())
    }
}

impl Provider for LastPassProvider {
    fn name(&self) -> &'static str {
        Self::PROVIDER_NAME
    }

    fn uri(&self) -> String {
        // LastPass can be "lastpass" (default) or "lastpass://folder" or "lastpass://Folder/Subfolder"
        if let Some(ref prefix) = self.config.folder_prefix {
            // The folder_prefix might be something like "SecretSpec/{project}/{profile}/{key}"
            // We want to extract just the folder part for the URI
            if let Some(folder) = prefix.split('/').next() {
                if folder.is_empty() || folder == "Shared" {
                    "lastpass".to_string()
                } else {
                    format!("lastpass://{}", ProviderUrl::encode(folder))
                }
            } else {
                "lastpass".to_string()
            }
        } else {
            "lastpass".to_string()
        }
    }

    /// Retrieves a secret from LastPass.
    ///
    /// Fetches the value of a secret stored in LastPass at the path
    /// determined by the folder_prefix format string. Uses `lpass show` with
    /// the `--sync=now` flag to ensure fresh data from the server.
    ///
    /// # Arguments
    ///
    /// * `project` - The project name
    /// * `key` - The secret key to retrieve
    /// * `profile` - The profile name
    ///
    /// # Returns
    ///
    /// - `Ok(Some(value))` if the secret exists and has a value
    /// - `Ok(None)` if the secret doesn't exist or has an empty value
    /// - `Err` if there's an error accessing LastPass
    ///
    /// # Errors
    ///
    /// - Returns an error if not logged in to LastPass
    /// - Returns an error if the LastPass CLI fails
    fn get(&self, project: &str, key: &str, profile: &str) -> Result<Option<SecretString>> {
        let item_name = self.format_item_name(project, key, profile);

        match self.execute_lpass_command(&["show", "--sync=now", "--password", &item_name]) {
            Ok(output) => {
                let password = output.trim();
                if password.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(SecretString::new(password.to_string().into())))
                }
            }
            Err(SecretSpecError::ProviderOperationFailed(msg))
                if msg.contains("Could not find specified account") =>
            {
                Ok(None)
            }
            Err(e) => Err(e),
        }
    }

    /// Stores a secret in LastPass.
    ///
    /// Creates or updates a secret in LastPass at the path
    /// determined by the folder_prefix format string. The method first checks if
    /// the item exists to determine whether to use `lpass edit` (for updates)
    /// or `lpass add` (for new items).
    ///
    /// # Arguments
    ///
    /// * `project` - The project name
    /// * `key` - The secret key to store
    /// * `value` - The secret value to store
    /// * `profile` - The profile name
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on success, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// - Returns an error if not logged in to LastPass
    /// - Returns an error if the LastPass CLI command fails
    ///
    /// # Implementation Details
    ///
    /// The method uses non-interactive mode and disables pinentry to avoid
    /// GUI prompts. The secret value is passed via stdin to avoid exposing
    /// it in the process list.
    fn set(&self, project: &str, key: &str, value: &SecretString, profile: &str) -> Result<()> {
        let item_name = self.format_item_name(project, key, profile);

        // Check if item exists
        if self.get(project, key, profile)?.is_some() {
            // Update existing item
            let args = vec![
                "edit",
                "--sync=now",
                &item_name,
                "--password",
                "--non-interactive",
            ];

            let mut cmd = Command::new("lpass");
            cmd.args(&args);
            cmd.env("LPASS_DISABLE_PINENTRY", "1");

            let mut child = cmd
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?;

            if let Some(stdin) = child.stdin.as_mut() {
                stdin.write_all(value.expose_secret().as_bytes())?;
            }

            let output = child.wait_with_output()?;
            if !output.status.success() {
                let error_msg = String::from_utf8_lossy(&output.stderr);
                return Err(SecretSpecError::ProviderOperationFailed(
                    error_msg.to_string(),
                ));
            }
        } else {
            // Create new item using lpass add
            let args = vec![
                "add",
                "--sync=now",
                &item_name,
                "--password",
                "--non-interactive",
            ];

            let mut cmd = Command::new("lpass");
            cmd.args(&args);
            cmd.env("LPASS_DISABLE_PINENTRY", "1");

            let mut child = cmd
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?;

            if let Some(stdin) = child.stdin.as_mut() {
                stdin.write_all(value.expose_secret().as_bytes())?;
            }

            let output = child.wait_with_output()?;
            if !output.status.success() {
                let error_msg = String::from_utf8_lossy(&output.stderr);
                return Err(SecretSpecError::ProviderOperationFailed(
                    error_msg.to_string(),
                ));
            }
        }

        Ok(())
    }
}

impl Default for LastPassProvider {
    /// Creates a LastPassProvider with default configuration.
    ///
    /// This is equivalent to calling `LastPassProvider::new(LastPassConfig::default())`.
    fn default() -> Self {
        Self::new(LastPassConfig::default())
    }
}