use crate::crypto::{Crypto, EncryptionResult};
use crate::error::{EncryptError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use chrono::{DateTime, Utc};
const VAULT_DIR: &str = ".encrypt";
const CONFIG_FILE: &str = "vault.lock";
const SECRETS_FILE: &str = "secrets.enc.json";
const GITIGNORE_FILE: &str = ".gitignore";
const LOCK_FILE: &str = "vault.unlocked";
#[derive(Debug, Serialize, Deserialize)]
pub struct VaultConfig {
pub password_hash: String,
pub salt: String,
pub hmac: String,
pub created_at: DateTime<Utc>,
pub version: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct VaultStatus {
pub is_locked: bool,
pub keys: Vec<String>,
pub last_modified: Option<DateTime<Utc>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LockFile {
pub unlocked: bool,
pub timestamp: DateTime<Utc>,
}
pub struct Vault {
project_root: PathBuf,
vault_path: PathBuf,
config_path: PathBuf,
secrets_path: PathBuf,
gitignore_path: PathBuf,
lock_file_path: PathBuf,
memory_cache: HashMap<String, String>,
}
impl Vault {
pub fn new() -> Self {
let project_root = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let vault_path = project_root.join(VAULT_DIR);
let config_path = vault_path.join(CONFIG_FILE);
let secrets_path = vault_path.join(SECRETS_FILE);
let gitignore_path = project_root.join(GITIGNORE_FILE);
let lock_file_path = vault_path.join(LOCK_FILE);
Self {
project_root,
vault_path,
config_path,
secrets_path,
gitignore_path,
lock_file_path,
memory_cache: HashMap::new(),
}
}
pub fn init(&self) -> Result<()> {
fs::create_dir_all(&self.vault_path)?;
if !self.secrets_path.exists() {
fs::write(&self.secrets_path, "{}")?;
}
if !self.config_path.exists() {
let initial_config = VaultConfig {
password_hash: String::new(),
salt: String::new(),
hmac: String::new(),
created_at: Utc::now(),
version: crate::get_version().to_string(),
};
fs::write(&self.config_path, serde_json::to_string_pretty(&initial_config)?)?;
}
self.update_gitignore()?;
Ok(())
}
fn update_gitignore(&self) -> Result<()> {
let gitignore_content = if self.gitignore_path.exists() {
fs::read_to_string(&self.gitignore_path)?
} else {
String::new()
};
if !gitignore_content.contains(".encrypt/") {
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&self.gitignore_path)?;
use std::io::Write;
writeln!(file, "\n# Encrypt vault\n.encrypt/")?;
}
Ok(())
}
pub fn exists(&self) -> bool {
self.vault_path.exists() && self.config_path.exists()
}
pub fn lockup(&mut self, password: &str) -> Result<()> {
if self.memory_cache.is_empty() {
self.load_secrets_from_file()?;
}
if self.memory_cache.is_empty() {
return Err(EncryptError::NoSecretsToLock);
}
let mut encrypted_secrets = HashMap::new();
for (key, value) in &self.memory_cache {
let result = Crypto::encrypt(value, password)?;
encrypted_secrets.insert(key.clone(), serde_json::to_string(&result)?);
}
let config_salt = Crypto::generate_salt();
let config = VaultConfig {
password_hash: Crypto::hash_password(password)?,
salt: config_salt.clone(),
hmac: Crypto::generate_hmac(
&serde_json::to_string(&encrypted_secrets)?,
&Crypto::derive_key(password, &config_salt)?,
),
created_at: Utc::now(),
version: "1.0.0".to_string(),
};
fs::write(&self.secrets_path, serde_json::to_string_pretty(&encrypted_secrets)?)?;
fs::write(&self.config_path, serde_json::to_string_pretty(&config)?)?;
self.memory_cache.clear();
if self.lock_file_path.exists() {
fs::remove_file(&self.lock_file_path)?;
}
Ok(())
}
pub fn setup(&mut self, password: &str) -> Result<()> {
if !self.exists() {
return Err(EncryptError::VaultNotFound);
}
let config: VaultConfig = serde_json::from_str(&fs::read_to_string(&self.config_path)?)?;
if config.password_hash.is_empty() {
self.memory_cache.clear();
self.create_lock_file()?;
return Ok(());
}
if !Crypto::verify_password(password, &config.password_hash)? {
return Err(EncryptError::InvalidPassword);
}
let encrypted_secrets: HashMap<String, String> =
serde_json::from_str(&fs::read_to_string(&self.secrets_path)?)?;
self.memory_cache.clear();
for (key, encrypted_data) in encrypted_secrets {
if encrypted_data.starts_with('{') {
let result: EncryptionResult = serde_json::from_str(&encrypted_data)?;
let decrypted = Crypto::decrypt(&result.encrypted, &result.salt, &result.hmac, password)?;
self.memory_cache.insert(key, decrypted);
} else {
self.memory_cache.insert(key, encrypted_data);
}
}
self.save_secrets_to_file()?;
self.create_lock_file()?;
Ok(())
}
pub fn set(&mut self, key: &str, value: &str) -> Result<()> {
if !self.unlocked()? {
return Err(EncryptError::VaultLocked);
}
if self.memory_cache.is_empty() {
self.load_secrets_from_file()?;
}
self.memory_cache.insert(key.to_string(), value.to_string());
self.save_secrets_to_file()?;
Ok(())
}
pub fn get(&mut self, key: &str) -> Result<String> {
if !self.unlocked()? {
return Err(EncryptError::VaultLocked);
}
if self.memory_cache.is_empty() {
self.load_secrets_from_file()?;
}
self.memory_cache
.get(key)
.ok_or_else(|| EncryptError::SecretNotFound(key.to_string()))
.map(|s| s.clone())
}
pub fn all(&mut self) -> Result<HashMap<String, String>> {
if !self.unlocked()? {
return Err(EncryptError::VaultLocked);
}
if self.memory_cache.is_empty() {
self.load_secrets_from_file()?;
}
Ok(self.memory_cache.clone())
}
pub fn status(&mut self) -> Result<VaultStatus> {
let is_unlocked = self.unlocked()?;
if is_unlocked && self.memory_cache.is_empty() {
self.load_secrets_from_file()?;
}
let keys: Vec<String> = self.memory_cache.keys().cloned().collect();
let last_modified = if self.exists() {
Some(DateTime::from(fs::metadata(&self.config_path)?.modified()?))
} else {
None
};
Ok(VaultStatus {
is_locked: !is_unlocked,
keys,
last_modified,
})
}
pub fn reset(&self) -> Result<()> {
if self.vault_path.exists() {
fs::remove_dir_all(&self.vault_path)?;
}
Ok(())
}
pub fn unlocked(&self) -> Result<bool> {
Ok(self.lock_file_path.exists())
}
fn create_lock_file(&self) -> Result<()> {
let lock_data = LockFile {
unlocked: true,
timestamp: Utc::now(),
};
fs::write(&self.lock_file_path, serde_json::to_string_pretty(&lock_data)?)?;
Ok(())
}
fn load_secrets_from_file(&mut self) -> Result<()> {
if !self.secrets_path.exists() {
return Ok(());
}
let secrets: HashMap<String, String> =
serde_json::from_str(&fs::read_to_string(&self.secrets_path)?)?;
self.memory_cache.clear();
for (key, value) in secrets {
if value.starts_with('{') {
return Err(EncryptError::Crypto(format!(
"Secret \"{}\" is encrypted but vault is unlocked. This should not happen.",
key
)));
} else {
self.memory_cache.insert(key, value);
}
}
Ok(())
}
fn save_secrets_to_file(&self) -> Result<()> {
fs::write(&self.secrets_path, serde_json::to_string_pretty(&self.memory_cache)?)?;
Ok(())
}
}