dotenvx-rs 0.4.31

Dotenvx is a Rust command-line/library to encrypt your .env files - limiting their attack vector while retaining their benefits
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
use crate::commands::crypt_util::{
    decrypt_env_item, encrypt_env_item, sign_message, verify_signature,
};
use crate::commands::{get_dotenvx_home, is_public_key_name};
use anyhow::anyhow;
use chrono::{DateTime, Local};
use dotenvx_rs::common::get_profile_name_from_file;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{Cursor, Read};
use std::path::Path;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DotenvxKeyStore {
    pub version: String,
    pub metadata: HashMap<String, String>,
    pub keys: HashMap<String, KeyPair>,
}

impl DotenvxKeyStore {
    pub fn new() -> Self {
        let mut metadata = HashMap::new();
        metadata.insert("uuid".to_owned(), uuid::Uuid::now_v7().to_string());
        DotenvxKeyStore {
            version: "0.1.0".to_string(),
            metadata,
            keys: HashMap::new(),
        }
    }
    pub fn load_global() -> anyhow::Result<DotenvxKeyStore> {
        let dotenvx_home = get_dotenvx_home();
        let env_keys_json_file = dotenvx_home.join(".env.keys.json");
        if env_keys_json_file.exists() {
            let file_content = fs::read_to_string(env_keys_json_file)?;
            return if file_content.contains("\"version\"") {
                Ok(serde_json::from_str(&file_content)?)
            } else {
                let keys: HashMap<String, KeyPair> = serde_json::from_str(&file_content)?;
                let mut metadata = HashMap::new();
                metadata.insert("uuid".to_owned(), uuid::Uuid::now_v7().to_string());
                Ok(DotenvxKeyStore {
                    version: "0.1.0".to_string(),
                    metadata,
                    keys,
                })
            };
        }
        Err(anyhow!("$HOME/.dotenvx/.env.keys.json not foud"))
    }

    pub fn find_private_key(&self, public_key: &str) -> Option<String> {
        if let Some(key_pair) = self.keys.get(public_key) {
            return Some(key_pair.private_key.clone());
        }
        None
    }

    pub fn write(&self) -> anyhow::Result<()> {
        let dotenvx_home = get_dotenvx_home();
        if !dotenvx_home.exists() {
            fs::create_dir_all(&dotenvx_home)?;
        }
        let env_keys_json_file = dotenvx_home.join(".env.keys.json");
        let file_content = serde_json::to_string_pretty(self)?;
        fs::write(env_keys_json_file, file_content)?;
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyPair {
    pub public_key: String,
    pub private_key: String,
    pub path: Option<String>,
    pub group: Option<String>,
    pub name: Option<String>,
    pub profile: Option<String>,
    pub comment: Option<String>,
    pub timestamp: Option<DateTime<Local>>,
}

impl KeyPair {
    pub fn new(public_key: &str, private_key: &str, profile: &Option<String>) -> Self {
        KeyPair {
            public_key: public_key.to_string(),
            private_key: private_key.to_string(),
            path: None,
            group: None,
            name: None,
            profile: profile.clone(),
            comment: None,
            timestamp: Some(Local::now()),
        }
    }
    pub fn from(
        public_key: &str,
        private_key: &str,
        group: &Option<String>,
        name: &Option<String>,
        profile: &Option<String>,
    ) -> Self {
        KeyPair {
            public_key: public_key.to_string(),
            private_key: private_key.to_string(),
            path: None,
            group: group.clone(),
            name: name.clone(),
            profile: profile.clone(),
            comment: None,
            timestamp: Some(Local::now()),
        }
    }
}

#[allow(dead_code)]
pub struct EnvKeys {
    pub metadata: Option<HashMap<String, String>>,
    pub keys: Option<Vec<String>>,
    pub source: Option<String>,
}

impl EnvKeys {
    pub fn from_file<P: AsRef<Path>>(env_keys_file_path: P) -> anyhow::Result<EnvKeys> {
        let content = fs::read_to_string(&env_keys_file_path)?;
        let metadata = extract_front_matter(&content);
        let keys: Vec<String> = content
            .lines()
            .filter(|line| !line.starts_with('#') && !line.trim().is_empty())
            .map(|line| line.trim().to_string())
            .collect();
        Ok(EnvKeys {
            metadata: Some(metadata),
            keys: Some(keys),
            source: Some(env_keys_file_path.as_ref().to_string_lossy().to_string()),
        })
    }

    pub fn new<P: AsRef<Path>>(env_keys_file_path: P) -> Self {
        let keys_uuid = uuid::Uuid::now_v7().to_string();
        let mut metadata = HashMap::new();
        metadata.insert("uuid".to_string(), keys_uuid);
        EnvKeys {
            metadata: Some(metadata),
            keys: Some(Vec::new()),
            source: Some(env_keys_file_path.as_ref().to_string_lossy().to_string()),
        }
    }

    pub fn write(&self) -> anyhow::Result<()> {
        let mut content = String::new();
        if let Some(metadata) = &self.metadata {
            content.push_str("# ---\n");
            if !metadata.contains_key("uuid") {
                let keys_uuid = uuid::Uuid::now_v7().to_string();
                content.push_str(&format!("# uuid: {keys_uuid}\n"));
            }
            for (key, value) in metadata {
                content.push_str(&format!("# {key}: {value}\n"));
            }
            content.push_str("# ---\n\n");
        }
        if let Some(keys) = &self.keys {
            for key in keys {
                content.push_str(&format!("{key}\n"));
            }
        }
        let file_path = self
            .source
            .as_ref()
            .ok_or_else(|| anyhow!("Source path is not set"))?;
        fs::write(file_path, content)?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct EnvFile {
    pub name: String,
    pub source: Option<String>,
    pub content: String,
    pub profile: Option<String>,
    pub metadata: HashMap<String, String>,
    pub entries: HashMap<String, String>,
}

impl EnvFile {
    pub fn from<P: AsRef<Path>>(env_file_path: P) -> Result<Self, std::io::Error> {
        let file_name = env_file_path
            .as_ref()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap();
        let mut path: Option<String> = None;
        if let Ok(path_buf) = &env_file_path.as_ref().canonicalize() {
            path = Some(path_buf.to_str().unwrap().to_string());
        }
        let file = File::open(&env_file_path)?;
        Self::from_read(file_name, path, file)
    }

    pub fn from_read<R: Read>(
        name: &str,
        source: Option<String>,
        mut reader: R,
    ) -> Result<Self, std::io::Error> {
        let mut content = String::new();
        reader.read_to_string(&mut content)?;
        let profile = if name.starts_with(".env.") {
            Some(name.replace(".env.", ""))
        } else {
            None
        };
        let metadata = extract_front_matter(&content);
        if let Ok(entries) = read_dotenv_entries(&content) {
            Ok(EnvFile {
                name: name.to_string(),
                source,
                content,
                profile,
                metadata,
                entries,
            })
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Failed to read dotenv entries",
            ))
        }
    }
}

impl EnvFile {
    pub fn is_signed(&self) -> bool {
        self.content.contains("# sign:") || self.content.contains("#sign:")
    }

    pub fn get_uuid(&self) -> Option<&String> {
        self.metadata.get("uuid")
    }

    pub fn get_public_key(&self) -> Option<String> {
        for (key, value) in &self.entries {
            if is_public_key_name(key) {
                return Some(value.clone());
            }
        }
        None
    }

    pub fn is_verified(&self) -> bool {
        if let Some(signature) = get_signature(&self.content) {
            let message = remove_signature(&self.content);
            let public_key = self.get_public_key();
            if let Some(public_key) = public_key {
                verify_signature(&public_key, &message, &signature).unwrap_or(false)
            } else {
                false
            }
        } else {
            false
        }
    }
}

#[allow(dead_code)]
struct ApplicationProperties {
    pub profile: Option<String>,
    pub metadata: HashMap<String, String>,
    pub entries: HashMap<String, String>,
    pub content: String,
    pub source: Option<String>,
}

impl ApplicationProperties {
    pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, std::io::Error> {
        let content = fs::read_to_string(&file_path)?;
        let metadata = extract_front_matter(&content);
        let entries = dotenvy::from_read_iter(Cursor::new(content.as_bytes()))
            .flatten()
            .collect();
        let file_name = file_path
            .as_ref()
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("application.properties")
            .to_string();
        let profile = get_profile_name_from_file(&file_name);
        Ok(ApplicationProperties {
            profile,
            metadata,
            entries,
            content,
            source: Some(file_path.as_ref().to_string_lossy().to_string()),
        })
    }
}

pub fn read_dotenv_entries(
    content: &str,
) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
    let mut entries: HashMap<String, String> = HashMap::new();
    let reader = Cursor::new(content.as_bytes());
    for (key, value) in dotenvy::from_read_iter(reader).flatten() {
        entries.insert(key.clone(), value.clone());
    }
    Ok(entries)
}

fn extract_front_matter(content: &str) -> HashMap<String, String> {
    let mut metadata = HashMap::new();
    if content.starts_with("# ---") || content.starts_with("#---") {
        let mut lines = content.lines();
        // Skip the first line
        lines.next();
        // Read until we find the end marker
        for line in lines {
            if line.starts_with("# ---") || line.starts_with("#---") {
                break;
            }
            if let Some((key, value)) = line.trim_start_matches("#").trim().split_once(':') {
                metadata.insert(key.trim().to_string(), value.trim().to_string());
            }
        }
    }
    metadata
}

#[allow(dead_code)]
pub fn sign_available(env_file_content: &str) -> bool {
    env_file_content
        .lines()
        .any(|line| line.starts_with("# sign:") || line.starts_with("#sign:"))
}

pub fn get_signature(env_file_content: &str) -> Option<String> {
    // Find the signature line
    for line in env_file_content.lines() {
        if line.starts_with("# sign:") || line.starts_with("#sign:") {
            return Some(line.trim_start_matches("# sign:").trim().to_string());
        }
    }
    None
}

#[allow(dead_code)]
pub fn is_sign_legal(env_file_content: &str, public_key: &str) -> anyhow::Result<bool> {
    if let Some(signature) = get_signature(env_file_content) {
        let message = remove_signature(env_file_content);
        verify_signature(public_key, &message, &signature)
    } else {
        Err(anyhow::anyhow!(
            "The .env file does not contain a valid signature."
        ))
    }
}

pub fn remove_signature(env_file_content: &str) -> String {
    // Remove lines starting with "#  --"
    env_file_content
        .lines() // Split into lines
        .filter(|line| !line.starts_with("# sign:"))
        .filter(|line| !line.starts_with("#sign:"))
        .collect::<Vec<_>>() // Collect remaining lines into a Vec
        .join("\n")
}

pub fn construct_front_matter() -> String {
    let env_file_uuid = uuid::Uuid::now_v7().to_string();
    format!("# ---\n# uuid: {env_file_uuid}\n# ---\n\n")
}

pub fn sign_and_update_env_file_content(
    private_key: &str,
    env_file_content: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    let new_content =
        if !(env_file_content.starts_with("# ---") || env_file_content.starts_with("#---")) {
            // append front matter for signature
            let front_matter = construct_front_matter();
            format!("{front_matter}{env_file_content}")
        } else {
            env_file_content.to_string()
        };
    let message = remove_signature(&new_content);
    let signature = sign_message(private_key, &message)?;
    Ok(update_signature(&new_content, &signature))
}

pub fn update_signature(env_file_content: &str, signature: &str) -> String {
    // remove existing signature line
    let new_content = remove_signature(env_file_content);
    let new_signature = format!("# sign: {signature}");
    // check front matter or not
    if new_content.starts_with("# ---") || new_content.starts_with("#---") {
        let mut lines = new_content.lines().collect::<Vec<_>>();
        // Find index of "# ---" or "#---" from lines
        let index = lines[1..]
            .iter()
            .position(|&line| line.starts_with("# ---") || line.starts_with("#---"));
        if let Some(idx) = index {
            // Insert the signature line before the end marker
            lines.insert(idx + 1, &new_signature);
        } else {
            // If no end marker found, append the signature as second line
            lines.insert(1, &new_signature);
        }
        lines.join("\n")
    } else {
        env_file_content.to_string()
    }
}

impl EnvFile {
    #[allow(dead_code)]
    pub fn encrypt(
        &self,
        public_key: &str,
    ) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
        let mut encrypted_entries: HashMap<String, String> = HashMap::new();
        for (key, value) in &self.entries {
            if !value.starts_with("encrypted:") {
                let encrypted_value = encrypt_env_item(public_key, value)?;
                encrypted_entries.insert(key.clone(), encrypted_value);
            } else {
                encrypted_entries.insert(key.clone(), value.clone());
            }
        }
        Ok(encrypted_entries)
    }

    #[allow(dead_code)]
    pub fn decrypt(
        &self,
        private_key: &str,
    ) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
        let mut decrypted_entries: HashMap<String, String> = HashMap::new();
        for (key, value) in &self.entries {
            if value.starts_with("encrypted:") {
                let decrypted_value = decrypt_env_item(private_key, value)?;
                decrypted_entries.insert(key.clone(), decrypted_value);
            } else {
                decrypted_entries.insert(key.clone(), value.clone());
            }
        }
        Ok(decrypted_entries)
    }
}

#[cfg(test)]
mod tests {
    use crate::commands::crypt_util::{sign_message, verify_signature};
    use crate::commands::model::DotenvxKeyStore;

    #[test]
    fn test_from_file() {
        let env_file = super::EnvFile::from(".env.example").unwrap();
        println!("{env_file:?}");
    }

    #[test]
    fn test_global() {
        let store = DotenvxKeyStore::load_global().unwrap();
        println!("{store:?}");
    }

    #[test]
    fn test_generate_signature() {
        let private_key = "9e70188d351c25d0714929205df9b8f4564b6b859966bdae7aef7f752a749d8b";
        let env_file_content = std::fs::read_to_string(".env").unwrap();
        let message = super::remove_signature(&env_file_content);
        let signature = sign_message(private_key, &message).unwrap();
        println!("{signature}");
    }

    #[test]
    fn test_verify_file_signature() {
        let public_key = "02b4972559803fa3c2464e93858f80c3a4c86f046f725329f8975e007b393dc4f0";
        let env_file_content = std::fs::read_to_string(".env").unwrap();
        let signature = super::get_signature(&env_file_content).unwrap();
        let message = super::remove_signature(&env_file_content);
        let result = verify_signature(public_key, &message, &signature).unwrap();
        assert!(result, "Signature verification failed");
    }

    #[test]
    fn test_get_signature() {
        let public_key = "039dd52f537a84a560fd18600ff40856f3bfcc103e70f329acc21327622042b195";
        let private_key = "a3d15e4b69c4a942c3813ba6085542ff6db1189378596d2f8a8652c550b7dea6";
        let content = std::fs::read_to_string(".env.example").unwrap().to_string();
        let signature = if let Some(signature) = super::get_signature(&content) {
            signature
        } else {
            sign_message(private_key, &content).unwrap()
        };
        // update the content with the signature
        let updated_content = super::update_signature(&content, &signature);
        let signature_2 = super::get_signature(&updated_content).unwrap();
        assert_eq!(signature, signature_2, "Signature mismatch");
        let content_2 = super::remove_signature(&updated_content);
        let result = verify_signature(public_key, &content_2, &signature_2).unwrap();
        assert!(result, "Signature verification failed");
    }

    #[test]
    fn test_update_signature() {
        let content = std::fs::read_to_string(".env.example").unwrap();
        let updated_content = super::update_signature(&content, "your_signature_here");
        println!("{updated_content}");
    }
}