lazy-locker 0.0.5

A secure local secrets manager with TUI interface and SDK support
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
//! CLI headless commands for CI/CD and scripting
//!
//! Provides non-interactive commands for automation:
//! - `init --passphrase <PASS>` - Initialize a new locker
//! - `token add/get/list/remove` - Manage tokens
//! - `import` - Import from .env files

use anyhow::{Context, Result};
use std::collections::HashMap;
use std::io::{self, BufRead, Read};
use std::path::PathBuf;

use crate::core::crypto::decrypt;
use crate::core::init::Locker;
use crate::core::store::SecretsStore;

/// Environment variable for passphrase (more secure than CLI argument)
const PASSPHRASE_ENV_VAR: &str = "LAZY_LOCKER_PASSPHRASE";

/// Gets passphrase from argument or environment variable
/// Priority: argument > environment variable
pub fn get_passphrase(arg_passphrase: Option<&str>) -> Result<String> {
    if let Some(pass) = arg_passphrase {
        return Ok(pass.to_string());
    }

    std::env::var(PASSPHRASE_ENV_VAR).context(format!(
        "Passphrase required. Use --passphrase <PASS> or set {} environment variable",
        PASSPHRASE_ENV_VAR
    ))
}

/// Output format for list/get commands
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OutputFormat {
    Human,
    Json,
    Env,
}

impl OutputFormat {
    pub fn from_args(json: bool, env: bool) -> Self {
        if json {
            OutputFormat::Json
        } else if env {
            OutputFormat::Env
        } else {
            OutputFormat::Human
        }
    }
}

// ============================================================================
// INIT COMMAND
// ============================================================================

/// Initialize a new locker with the given passphrase
pub fn cmd_init(passphrase: &str, force: bool) -> Result<()> {
    let locker_dir = get_locker_dir()?;
    let salt_path = locker_dir.join("salt");

    if salt_path.exists() && !force {
        anyhow::bail!(
            "Locker already exists at {:?}. Use --force to overwrite.",
            locker_dir
        );
    }

    if force && salt_path.exists() {
        // Remove existing locker files
        std::fs::remove_file(locker_dir.join("salt")).ok();
        std::fs::remove_file(locker_dir.join("hash")).ok();
        std::fs::remove_file(locker_dir.join("secrets.json")).ok();
    }

    // Initialize with passphrase
    let _locker = Locker::init_or_load_with_passphrase(passphrase)?;

    println!("✅ Locker initialized at {:?}", locker_dir);
    Ok(())
}

// ============================================================================
// TOKEN COMMANDS
// ============================================================================

/// Add a new token
pub fn cmd_token_add(
    name: &str,
    value: Option<&str>,
    stdin: bool,
    expires_days: Option<u32>,
    passphrase: &str,
) -> Result<()> {
    let secret_value = if stdin {
        read_value_from_stdin()?
    } else if let Some(v) = value {
        v.to_string()
    } else {
        anyhow::bail!("Value required. Provide as argument or use --stdin");
    };

    let locker = Locker::init_or_load_with_passphrase(passphrase)?;
    let key = locker.get_key().context("Failed to get encryption key")?;
    let locker_dir = locker.base_dir().clone();

    let mut store = SecretsStore::load(&locker_dir, key)?;
    store.add_secret(
        name.to_string(),
        secret_value,
        expires_days,
        &locker_dir,
        key,
    )?;

    println!("✅ Token '{}' added", name);
    if let Some(days) = expires_days {
        println!("   Expires in {} days", days);
    }

    Ok(())
}

/// Get a token value
pub fn cmd_token_get(name: &str, format: OutputFormat, passphrase: &str) -> Result<()> {
    let locker = Locker::init_or_load_with_passphrase(passphrase)?;
    let key = locker.get_key().context("Failed to get encryption key")?;
    let locker_dir = locker.base_dir().clone();

    let store = SecretsStore::load(&locker_dir, key)?;
    let secret = store
        .get_secret(name)
        .context(format!("Token '{}' not found", name))?;

    if secret.is_expired() {
        anyhow::bail!("Token '{}' has expired", name);
    }

    let value = decrypt(&secret.encrypted_value, key)?;
    let value_str = String::from_utf8(value)?;

    match format {
        OutputFormat::Human => println!("{}", value_str),
        OutputFormat::Json => {
            let obj = serde_json::json!({
                "name": name,
                "value": value_str,
                "expires_at": secret.expires_at,
            });
            println!("{}", serde_json::to_string_pretty(&obj)?);
        }
        OutputFormat::Env => println!("{}={}", name, value_str),
    }

    Ok(())
}

/// List all tokens
pub fn cmd_token_list(format: OutputFormat, passphrase: &str) -> Result<()> {
    let locker = Locker::init_or_load_with_passphrase(passphrase)?;
    let key = locker.get_key().context("Failed to get encryption key")?;
    let locker_dir = locker.base_dir().clone();

    let store = SecretsStore::load(&locker_dir, key)?;
    let secrets = store.list_secrets();

    match format {
        OutputFormat::Human => {
            if secrets.is_empty() {
                println!("No tokens found.");
                return Ok(());
            }

            println!("{:<30} {:<20} STATUS", "NAME", "EXPIRES");
            println!("{:-<60}", "");

            for secret in secrets {
                let status = if secret.is_expired() {
                    "⚠️ EXPIRED"
                } else {
                    ""
                };
                println!(
                    "{:<30} {:<20} {}",
                    secret.name,
                    secret.expiration_display(),
                    status
                );
            }
        }
        OutputFormat::Json => {
            let list: Vec<_> = secrets
                .iter()
                .map(|s| {
                    serde_json::json!({
                        "name": s.name,
                        "expires_at": s.expires_at,
                        "is_expired": s.is_expired(),
                        "days_remaining": s.days_until_expiration(),
                    })
                })
                .collect();
            println!("{}", serde_json::to_string_pretty(&list)?);
        }
        OutputFormat::Env => {
            // For env format, we need to decrypt and output all values
            for secret in secrets {
                if !secret.is_expired() {
                    let value = decrypt(&secret.encrypted_value, key)?;
                    let value_str = String::from_utf8(value)?;
                    println!("{}={}", secret.name, value_str);
                }
            }
        }
    }

    Ok(())
}

/// Remove a token
pub fn cmd_token_remove(name: &str, passphrase: &str) -> Result<()> {
    let locker = Locker::init_or_load_with_passphrase(passphrase)?;
    let key = locker.get_key().context("Failed to get encryption key")?;
    let locker_dir = locker.base_dir().clone();

    let mut store = SecretsStore::load(&locker_dir, key)?;

    if store.get_secret(name).is_none() {
        anyhow::bail!("Token '{}' not found", name);
    }

    store.delete_secret(name, &locker_dir, key)?;
    println!("✅ Token '{}' removed", name);

    Ok(())
}

// ============================================================================
// IMPORT COMMAND
// ============================================================================

/// Import tokens from a .env file or stdin
pub fn cmd_import(
    file: Option<&str>,
    stdin: bool,
    format: &str,
    expires_days: Option<u32>,
    passphrase: &str,
) -> Result<()> {
    let content = if stdin {
        let mut buf = String::new();
        io::stdin().read_to_string(&mut buf)?;
        buf
    } else if let Some(path) = file {
        std::fs::read_to_string(path).context(format!("Failed to read file: {}", path))?
    } else {
        anyhow::bail!("Provide a file path or use --stdin");
    };

    let secrets = match format {
        "env" => parse_env_format(&content)?,
        "json" => parse_json_format(&content)?,
        _ => anyhow::bail!("Unknown format: {}. Supported: env, json", format),
    };

    if secrets.is_empty() {
        println!("⚠️  No secrets found in input");
        return Ok(());
    }

    let locker = Locker::init_or_load_with_passphrase(passphrase)?;
    let key = locker.get_key().context("Failed to get encryption key")?;
    let locker_dir = locker.base_dir().clone();

    let mut store = SecretsStore::load(&locker_dir, key)?;
    let mut count = 0;

    for (name, value) in secrets {
        store.add_secret(name.clone(), value, expires_days, &locker_dir, key)?;
        count += 1;
    }

    println!("✅ Imported {} tokens", count);
    if let Some(days) = expires_days {
        println!("   All tokens expire in {} days", days);
    }

    Ok(())
}

// ============================================================================
// EXPORT COMMAND (bonus)
// ============================================================================

/// Export all tokens to stdout
pub fn cmd_export(format: OutputFormat, passphrase: &str) -> Result<()> {
    // Reuse token list with env format for export
    cmd_token_list(format, passphrase)
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn get_locker_dir() -> Result<PathBuf> {
    use directories::BaseDirs;

    let base_dirs = BaseDirs::new().context("Unable to determine user directories")?;
    let config_dir = base_dirs.config_dir();

    #[cfg(unix)]
    let sub_dir = ".lazy-locker";
    #[cfg(not(unix))]
    let sub_dir = "lazy-locker";

    let locker_dir = config_dir.join(sub_dir);
    std::fs::create_dir_all(&locker_dir)?;

    Ok(locker_dir)
}

fn read_value_from_stdin() -> Result<String> {
    let stdin = io::stdin();
    let mut value = String::new();

    // Read first line only (trim newline)
    stdin.lock().read_line(&mut value)?;

    // Remove trailing newline
    if value.ends_with('\n') {
        value.pop();
    }
    if value.ends_with('\r') {
        value.pop();
    }

    if value.is_empty() {
        anyhow::bail!("No value provided on stdin");
    }

    Ok(value)
}

fn parse_env_format(content: &str) -> Result<HashMap<String, String>> {
    let mut secrets = HashMap::new();

    for line in content.lines() {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Parse KEY=VALUE or KEY="VALUE" or KEY='VALUE'
        if let Some(eq_pos) = line.find('=') {
            let key = line[..eq_pos].trim().to_string();
            let mut value = line[eq_pos + 1..].trim().to_string();

            // Remove surrounding quotes
            if (value.starts_with('"') && value.ends_with('"'))
                || (value.starts_with('\'') && value.ends_with('\''))
            {
                value = value[1..value.len() - 1].to_string();
            }

            if !key.is_empty() {
                secrets.insert(key, value);
            }
        }
    }

    Ok(secrets)
}

fn parse_json_format(content: &str) -> Result<HashMap<String, String>> {
    // Support both object format and array format
    let json: serde_json::Value = serde_json::from_str(content)?;
    let mut secrets = HashMap::new();

    match json {
        serde_json::Value::Object(obj) => {
            for (key, value) in obj {
                if let Some(v) = value.as_str() {
                    secrets.insert(key, v.to_string());
                }
            }
        }
        serde_json::Value::Array(arr) => {
            for item in arr {
                if let (Some(name), Some(value)) = (
                    item.get("name").and_then(|v| v.as_str()),
                    item.get("value").and_then(|v| v.as_str()),
                ) {
                    secrets.insert(name.to_string(), value.to_string());
                }
            }
        }
        _ => anyhow::bail!("JSON must be an object or array"),
    }

    Ok(secrets)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_env_format() {
        let content = r#"
# Comment
DATABASE_URL=postgres://localhost/db
API_KEY="sk-123456"
SECRET='my_secret'
EMPTY=

SPACES = value with spaces
"#;

        let secrets = parse_env_format(content).unwrap();

        assert_eq!(
            secrets.get("DATABASE_URL"),
            Some(&"postgres://localhost/db".to_string())
        );
        assert_eq!(secrets.get("API_KEY"), Some(&"sk-123456".to_string()));
        assert_eq!(secrets.get("SECRET"), Some(&"my_secret".to_string()));
        assert_eq!(secrets.get("EMPTY"), Some(&"".to_string()));
        assert_eq!(
            secrets.get("SPACES"),
            Some(&"value with spaces".to_string())
        );
    }

    #[test]
    fn test_parse_json_object_format() {
        let content = r#"{"API_KEY": "sk-123", "DB_URL": "postgres://localhost"}"#;

        let secrets = parse_json_format(content).unwrap();

        assert_eq!(secrets.get("API_KEY"), Some(&"sk-123".to_string()));
        assert_eq!(
            secrets.get("DB_URL"),
            Some(&"postgres://localhost".to_string())
        );
    }

    #[test]
    fn test_parse_json_array_format() {
        let content = r#"[
            {"name": "API_KEY", "value": "sk-123"},
            {"name": "DB_URL", "value": "postgres://localhost"}
        ]"#;

        let secrets = parse_json_format(content).unwrap();

        assert_eq!(secrets.get("API_KEY"), Some(&"sk-123".to_string()));
        assert_eq!(
            secrets.get("DB_URL"),
            Some(&"postgres://localhost".to_string())
        );
    }

    #[test]
    fn test_output_format_from_args() {
        assert_eq!(OutputFormat::from_args(false, false), OutputFormat::Human);
        assert_eq!(OutputFormat::from_args(true, false), OutputFormat::Json);
        assert_eq!(OutputFormat::from_args(false, true), OutputFormat::Env);
        // JSON takes priority if both are set
        assert_eq!(OutputFormat::from_args(true, true), OutputFormat::Json);
    }
}