cred 0.3.0

A command-line tool to manage secrets and environment variables locally and remotely.
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Global configuration and keystore utilities for cred.
//! Handles `~/.config/cred/global.toml` plus pluggable secret storage backends.

use anyhow::{Context, Result};
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
use chacha20poly1305::{
    ChaCha20Poly1305, Key, Nonce,
    aead::{Aead, KeyInit},
};
use keyring::Entry;
use rand::Rng;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use toml::Value;

/// Versioning information for the global config.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct CredMeta {
    pub version: String,
    pub config_version: u32,
}

/// Machine identity hints (optional).
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Machine {
    pub id: Option<String>,
    pub hostname: Option<String>,
}

/// User-facing CLI preferences.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Preferences {
    pub default_target: Option<String>,
    pub confirm_destructive: Option<bool>,
    pub color_output: Option<bool>,
}

/// Target-specific configuration (auth reference, default flag).
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct TargetConfig {
    pub auth_ref: Option<String>,
    pub default: Option<bool>,
}

/// Root of the global configuration file.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GlobalConfig {
    pub cred: CredMeta,
    pub machine: Option<Machine>,
    pub preferences: Preferences,
    pub targets: HashMap<String, TargetConfig>,
}

/// Get the config directory depending on the operating system.
fn resolve_config_dir() -> Result<PathBuf> {
    if cfg!(target_os = "macos") {
        let home = dirs::home_dir().context("Could not determine home directory")?;
        return Ok(home.join(".config").join("cred"));
    }
    let config = dirs::config_dir().context("Could not determine config directory")?;
    Ok(config.join("cred"))
}

/// Ensure the global config file exists (creating default if missing) and return its path.
pub fn ensure_global_config_exists() -> Result<PathBuf> {
    let config_dir = resolve_config_dir()?;
    ensure_config_at(&config_dir)
}

/// Ensure a config exists at the given directory, writing defaults on first run.
pub fn ensure_config_at(config_dir: &Path) -> Result<PathBuf> {
    if !config_dir.exists() {
        fs::create_dir_all(config_dir).context("Failed to create config dir")?;
    }
    let file_path = config_dir.join("global.toml");
    if !file_path.exists() {
        let default_config = default_config();
        let content = toml::to_string_pretty(&default_config)?;
        fs::write(&file_path, content)?;
    }
    Ok(file_path)
}

/// Load the typed global config, backfilling required fields if absent.
pub fn load() -> Result<GlobalConfig> {
    let config_path = ensure_global_config_exists()?;
    let content = fs::read_to_string(&config_path).context("Failed to read global config")?;
    let mut config: GlobalConfig = toml::from_str(&content).unwrap_or_else(|_| default_config());
    // Backfill cred if missing
    if config.cred.version.is_empty() {
        config.cred.version = "0.1.0".to_string();
    }
    if config.cred.config_version == 0 {
        config.cred.config_version = 1;
    }
    Ok(config)
}

/// Load the raw TOML as a `Value` (used for arbitrary path edits).
fn load_raw() -> Result<Value> {
    let config_path = ensure_global_config_exists()?;
    let content = fs::read_to_string(&config_path).unwrap_or_default();
    let val: Value =
        toml::from_str(&content).unwrap_or_else(|_| Value::Table(toml::map::Map::new()));
    Ok(val)
}

/// Persist a raw TOML `Value` to disk.
fn save_raw(val: &Value) -> Result<()> {
    let config_path = ensure_global_config_exists()?;
    let toml_string = toml::to_string_pretty(val)?;
    fs::write(&config_path, toml_string)?;
    Ok(())
}

mod toml_path {
    use super::*;

    /// Coerce string input into TOML types (bool, int, float, or string).
    pub fn parse_value(input: &str) -> Value {
        if input.eq_ignore_ascii_case("true") {
            return Value::Boolean(true);
        }
        if input.eq_ignore_ascii_case("false") {
            return Value::Boolean(false);
        }
        if let Ok(i) = input.parse::<i64>() {
            return Value::Integer(i);
        }
        if let Ok(f) = input.parse::<f64>() {
            return Value::Float(f);
        }
        Value::String(input.to_string())
    }

    /// Set a dotted path in a TOML `Value`, creating tables as needed.
    pub fn set_path(root: &mut Value, path: &[&str], value: Value) {
        if path.is_empty() {
            return;
        }
        let mut current = root;
        for seg in path[..path.len() - 1].iter() {
            if !current.is_table() {
                *current = Value::Table(toml::map::Map::new());
            }
            let tbl = current.as_table_mut().unwrap();
            current = tbl
                .entry(seg.to_string())
                .or_insert(Value::Table(toml::map::Map::new()));
        }
        if let Some(last) = path.last() {
            if !current.is_table() {
                *current = Value::Table(toml::map::Map::new());
            }
            let tbl = current.as_table_mut().unwrap();
            tbl.insert(last.to_string(), value);
        }
    }

    /// Remove a dotted path from a TOML `Value` if it exists.
    pub fn unset_path(root: &mut Value, path: &[&str]) {
        if path.is_empty() {
            return;
        }
        let mut current = root;
        for seg in path[..path.len() - 1].iter() {
            if let Some(tbl) = current.as_table_mut() {
                if let Some(next) = tbl.get_mut(*seg) {
                    current = next;
                } else {
                    return;
                }
            } else {
                return;
            }
        }
        if let Some(last) = path.last() {
            if let Some(tbl) = current.as_table_mut() {
                tbl.remove(*last);
            }
        }
    }

    /// Fetch a dotted path from a TOML `Value`.
    pub fn get_path<'a>(root: &'a Value, path: &[&str]) -> Option<&'a Value> {
        let mut current = root;
        for seg in path {
            match current {
                Value::Table(t) => {
                    current = t.get(*seg)?;
                }
                _ => return None,
            }
        }
        Some(current)
    }
}

/// Set a config value at a dotted path, coercing primitive types.
pub fn config_set(key_path: &str, val: &str) -> Result<()> {
    let mut root = load_raw()?;
    let parts: Vec<&str> = key_path.split('.').filter(|s| !s.is_empty()).collect();
    if parts.is_empty() {
        anyhow::bail!("Invalid key path");
    }
    let value = toml_path::parse_value(val);
    toml_path::set_path(&mut root, &parts, value);
    save_raw(&root)
}

/// Get a config value at a dotted path.
pub fn config_get(key_path: &str) -> Result<Option<Value>> {
    let root = load_raw()?;
    let parts: Vec<&str> = key_path.split('.').filter(|s| !s.is_empty()).collect();
    if parts.is_empty() {
        return Ok(None);
    }
    Ok(toml_path::get_path(&root, &parts).cloned())
}

/// Remove a config value at a dotted path.
pub fn config_unset(key_path: &str) -> Result<()> {
    let mut root = load_raw()?;
    let parts: Vec<&str> = key_path.split('.').filter(|s| !s.is_empty()).collect();
    if parts.is_empty() {
        return Ok(());
    }
    toml_path::unset_path(&mut root, &parts);
    save_raw(&root)
}

/// Dump the entire config as pretty TOML.
pub fn config_list() -> Result<String> {
    let root = load_raw()?;
    let toml_string = toml::to_string_pretty(&root)?;
    Ok(toml_string)
}

/// Generate a default config with a random machine ID and safe defaults.
fn default_config() -> GlobalConfig {
    let mut id = [0u8; 8];
    rand::rng().fill_bytes(&mut id);
    let machine_id = format!("m_{:02x?}", id);
    let hostname = std::env::var("HOSTNAME").ok();

    GlobalConfig {
        cred: CredMeta {
            version: "0.1.0".to_string(),
            config_version: 1,
        },
        machine: Some(Machine {
            id: Some(machine_id),
            hostname,
        }),
        preferences: Preferences {
            default_target: Some("github".to_string()),
            confirm_destructive: Some(true),
            color_output: Some(true),
        },
        targets: HashMap::new(),
    }
}

/// Persist a target token reference in config and store the token via keystore backend.
pub fn set_target_token(target: &str, token: &str) -> Result<()> {
    let mut config = load()?;
    let auth_ref = format!("cred:target:{}:default", target);
    config
        .targets
        .entry(target.to_string())
        .or_default()
        .auth_ref = Some(auth_ref.clone());

    let config_path = ensure_global_config_exists()?;
    let toml_string = toml::to_string_pretty(&config)?;
    fs::write(&config_path, toml_string)?;

    keystore::set(&auth_ref, token)?;
    Ok(())
}

/// Retrieve a target token from the configured keystore backend.
pub fn get_target_token(target: &str) -> Result<Option<String>> {
    let config = load()?;
    let auth_ref = match config.targets.get(target).and_then(|t| t.auth_ref.as_ref()) {
        Some(r) => r.clone(),
        None => return Ok(None),
    };
    keystore::get(&auth_ref)
}

/// Remove a target token reference and delete the stored secret if present.
pub fn remove_target_token(target: &str) -> Result<()> {
    let mut config = load()?;
    if let Some(tcfg) = config.targets.remove(target) {
        if let Some(auth_ref) = tcfg.auth_ref {
            keystore::remove(&auth_ref)?;
        }
        let config_path = ensure_global_config_exists()?;
        let toml_string = toml::to_string_pretty(&config)?;
        fs::write(&config_path, toml_string)?;
        println!("✓ Removed authentication for '{}'", target);
    } else {
        println!("Target '{}' was not configured.", target);
    }
    Ok(())
}

// ---------- Keystore backends ----------

mod keystore {
    use super::*;

    /// Pluggable secret storage backend for target tokens.
    enum KeystoreBackend {
        Memory,
        File { path: PathBuf, key: [u8; 32] },
        Keyring,
    }

    /// Select keystore backend via env vars (memory, file, or platform keyring).
    fn resolve_keystore() -> KeystoreBackend {
        match std::env::var("CRED_KEYSTORE").as_deref() {
            Ok("memory") => KeystoreBackend::Memory,
            Ok("file") => {
                let path = std::env::var("CRED_KEYSTORE_FILE")
                    .map(PathBuf::from)
                    .unwrap_or_else(|_| {
                        resolve_config_dir()
                            .unwrap_or_else(|_| PathBuf::from("."))
                            .join("keystore.enc")
                    });
                let key_b64 = std::env::var("CRED_KEYSTORE_FILE_KEY")
                    .expect("CRED_KEYSTORE_FILE_KEY (base64 32 bytes) required for file keystore");
                let key_raw = BASE64
                    .decode(key_b64)
                    .expect("Invalid base64 in CRED_KEYSTORE_FILE_KEY");
                assert!(
                    key_raw.len() == 32,
                    "CRED_KEYSTORE_FILE_KEY must be 32 bytes"
                );
                let mut key = [0u8; 32];
                key.copy_from_slice(&key_raw);
                KeystoreBackend::File { path, key }
            }
            _ => KeystoreBackend::Keyring,
        }
    }

    static MEMORY_KEYSTORE: OnceLock<std::sync::Mutex<HashMap<String, String>>> = OnceLock::new();

    /// Store a token in the active keystore backend.
    pub fn set(auth_ref: &str, token: &str) -> Result<()> {
        match resolve_keystore() {
            KeystoreBackend::Memory => {
                let store = MEMORY_KEYSTORE.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
                let mut guard = store.lock().unwrap();
                guard.insert(auth_ref.to_string(), token.to_string());
                Ok(())
            }
            KeystoreBackend::File { path, key } => {
                keystore_file_write(&path, &key, auth_ref, token)
            }
            KeystoreBackend::Keyring => {
                let entry = Entry::new("cred-target", auth_ref)?;
                entry.set_password(token)?;
                Ok(())
            }
        }
    }

    /// Fetch a token from the active keystore backend.
    pub fn get(auth_ref: &str) -> Result<Option<String>> {
        match resolve_keystore() {
            KeystoreBackend::Memory => {
                let store = MEMORY_KEYSTORE.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
                let guard = store.lock().unwrap();
                Ok(guard.get(auth_ref).cloned())
            }
            KeystoreBackend::File { path, key } => keystore_file_read(&path, &key, auth_ref),
            KeystoreBackend::Keyring => {
                let entry = Entry::new("cred-target", auth_ref)?;
                match entry.get_password() {
                    Ok(pw) => Ok(Some(pw)),
                    Err(_) => Ok(None),
                }
            }
        }
    }

    /// Remove a token from the active keystore backend.
    pub fn remove(auth_ref: &str) -> Result<()> {
        match resolve_keystore() {
            KeystoreBackend::Memory => {
                let store = MEMORY_KEYSTORE.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
                let mut guard = store.lock().unwrap();
                guard.remove(auth_ref);
                Ok(())
            }
            KeystoreBackend::File { path, key } => {
                keystore_file_delete(&path, &key, auth_ref)?;
                Ok(())
            }
            KeystoreBackend::Keyring => {
                let entry = Entry::new("cred-target", auth_ref)?;
                let _ = entry.set_password("");
                Ok(())
            }
        }
    }

    /// On-disk encrypted keystore blob.
    #[derive(Serialize, Deserialize)]
    struct EncKeystore {
        nonce: String,
        ciphertext: String,
    }

    /// Read a token from the file-based keystore.
    fn keystore_file_read(path: &Path, key: &[u8; 32], auth_ref: &str) -> Result<Option<String>> {
        if !path.exists() {
            return Ok(None);
        }
        let raw = fs::read(path)?;
        let enc: EncKeystore = serde_json::from_slice(&raw)?;
        let nonce_bytes = BASE64.decode(enc.nonce)?;
        let cipher_bytes = BASE64.decode(enc.ciphertext)?;
        let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
        if nonce_bytes.len() != 12 {
            anyhow::bail!("Invalid nonce length in keystore");
        }
        let nonce = Nonce::from_slice(&nonce_bytes);
        let plaintext = cipher
            .decrypt(nonce, cipher_bytes.as_ref())
            .map_err(|e| anyhow::anyhow!("Failed to decrypt keystore: {}", e))?;
        let mut map: HashMap<String, String> = serde_json::from_slice(&plaintext)?;
        Ok(map.remove(auth_ref))
    }

    /// Write/update a token in the file-based keystore.
    fn keystore_file_write(path: &Path, key: &[u8; 32], auth_ref: &str, token: &str) -> Result<()> {
        let mut map = if path.exists() {
            keystore_file_load_all(path, key)?
        } else {
            HashMap::new()
        };
        map.insert(auth_ref.to_string(), token.to_string());
        keystore_file_save_all(path, key, &map)
    }

    /// Delete a token from the file-based keystore.
    fn keystore_file_delete(path: &Path, key: &[u8; 32], auth_ref: &str) -> Result<()> {
        if !path.exists() {
            return Ok(());
        }
        let mut map = keystore_file_load_all(path, key)?;
        map.remove(auth_ref);
        keystore_file_save_all(path, key, &map)
    }

    /// Load the full file-based keystore into memory.
    fn keystore_file_load_all(path: &Path, key: &[u8; 32]) -> Result<HashMap<String, String>> {
        let raw = fs::read(path)?;
        let enc: EncKeystore = serde_json::from_slice(&raw)?;
        let nonce_bytes = BASE64.decode(enc.nonce)?;
        let cipher_bytes = BASE64.decode(enc.ciphertext)?;
        let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
        if nonce_bytes.len() != 12 {
            anyhow::bail!("Invalid nonce length in keystore");
        }
        let nonce = Nonce::from_slice(&nonce_bytes);
        let plaintext = cipher
            .decrypt(nonce, cipher_bytes.as_ref())
            .map_err(|e| anyhow::anyhow!("Failed to decrypt keystore: {}", e))?;
        let map: HashMap<String, String> = serde_json::from_slice(&plaintext)?;
        Ok(map)
    }

    /// Save the full file-based keystore map back to disk.
    fn keystore_file_save_all(
        path: &Path,
        key: &[u8; 32],
        map: &HashMap<String, String>,
    ) -> Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let plaintext = serde_json::to_vec(map)?;
        let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
        let mut nonce = [0u8; 12];
        rand::rng().fill(&mut nonce);
        let nonce_ga = Nonce::from_slice(&nonce);
        let ciphertext = cipher
            .encrypt(nonce_ga, plaintext.as_ref())
            .map_err(|e| anyhow::anyhow!("Failed to encrypt keystore: {}", e))?;
        let enc = EncKeystore {
            nonce: BASE64.encode(nonce),
            ciphertext: BASE64.encode(ciphertext),
        };
        let data = serde_json::to_vec_pretty(&enc)?;
        fs::write(path, data)?;
        Ok(())
    }
}

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

    #[test]
    fn test_parse_value_coercion() {
        assert_eq!(toml_path::parse_value("true"), Value::Boolean(true));
        assert_eq!(toml_path::parse_value("false"), Value::Boolean(false));
        assert_eq!(toml_path::parse_value("42"), Value::Integer(42));
        assert_eq!(toml_path::parse_value("3.14"), Value::Float(3.14));
        assert_eq!(
            toml_path::parse_value("text"),
            Value::String("text".to_string())
        );
    }

    #[test]
    fn test_set_get_unset_path() {
        let mut root = Value::Table(toml::map::Map::new());
        toml_path::set_path(
            &mut root,
            &["preferences", "default_target"],
            Value::String("github".into()),
        );
        let got = toml_path::get_path(&root, &["preferences", "default_target"]);
        assert_eq!(got, Some(&Value::String("github".into())));

        toml_path::unset_path(&mut root, &["preferences", "default_target"]);
        let got = toml_path::get_path(&root, &["preferences", "default_target"]);
        assert!(got.is_none());
    }

    #[test]
    fn test_default_config_shape() {
        let cfg = default_config();
        assert_eq!(cfg.cred.config_version, 1);
        assert_eq!(cfg.cred.version, "0.1.0");
        assert!(cfg.preferences.default_target.is_some());
    }
}