zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
//! Certificate and account storage for ACME
//!
//! Provides persistent storage for ACME account credentials and issued certificates.
//!
//! # Directory Structure
//!
//! ```text
//! storage/
//! ├── account.json          # ACME account credentials (opaque, serialized)
//! └── domains/
//!     └── example.com/
//!         ├── cert.pem      # Certificate chain
//!         ├── key.pem       # Private key
//!         └── meta.json     # Certificate metadata (expiry, issued date)
//! ```

use std::fs;
use std::path::{Path, PathBuf};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, trace, warn};

use super::error::StorageError;

/// Certificate metadata stored alongside the certificate
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertificateMeta {
    /// When the certificate expires
    pub expires: DateTime<Utc>,
    /// When the certificate was issued
    pub issued: DateTime<Utc>,
    /// Domains covered by this certificate
    pub domains: Vec<String>,
    /// Issuer (e.g., "Let's Encrypt")
    #[serde(default)]
    pub issuer: Option<String>,
}

/// A stored certificate with its metadata
#[derive(Debug, Clone)]
pub struct StoredCertificate {
    /// PEM-encoded certificate chain
    pub cert_pem: String,
    /// PEM-encoded private key
    pub key_pem: String,
    /// Certificate metadata
    pub meta: CertificateMeta,
}

/// ACME account metadata for storage
///
/// Stores metadata about the ACME account alongside the credentials JSON.
/// The actual `instant_acme::AccountCredentials` is stored separately as JSON.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoredAccountCredentials {
    /// Contact email (for reference)
    #[serde(default)]
    pub contact_email: Option<String>,
    /// When the account was created
    pub created: DateTime<Utc>,
}

/// Certificate storage manager
///
/// Handles persistent storage of ACME account credentials and certificates.
/// Uses a simple filesystem-based storage with restrictive permissions.
#[derive(Debug)]
pub struct CertificateStorage {
    /// Base storage directory
    base_path: PathBuf,
}

impl CertificateStorage {
    /// Create a new certificate storage at the given path
    ///
    /// Creates the directory structure if it doesn't exist and sets
    /// restrictive permissions (0700 on Unix).
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be created or permissions
    /// cannot be set.
    pub fn new(base_path: &Path) -> Result<Self, StorageError> {
        // Create base directory
        fs::create_dir_all(base_path)?;

        // Create domains subdirectory
        let domains_path = base_path.join("domains");
        fs::create_dir_all(&domains_path)?;

        // Set restrictive permissions on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = fs::Permissions::from_mode(0o700);
            fs::set_permissions(base_path, perms.clone())?;
            fs::set_permissions(&domains_path, perms)?;
        }

        info!(
            storage_path = %base_path.display(),
            "Initialized ACME certificate storage"
        );

        Ok(Self {
            base_path: base_path.to_path_buf(),
        })
    }

    /// Get the storage base path
    pub fn base_path(&self) -> &Path {
        &self.base_path
    }

    // =========================================================================
    // Account Operations
    // =========================================================================

    /// Load stored account credentials
    pub fn load_account(&self) -> Result<Option<StoredAccountCredentials>, StorageError> {
        let account_path = self.base_path.join("account.json");

        if !account_path.exists() {
            trace!("No stored ACME account found");
            return Ok(None);
        }

        let content = fs::read_to_string(&account_path)?;
        let creds: StoredAccountCredentials = serde_json::from_str(&content)?;

        debug!(
            contact = ?creds.contact_email,
            created = %creds.created,
            "Loaded ACME account credentials"
        );
        Ok(Some(creds))
    }

    /// Save account credentials
    pub fn save_account(&self, creds: &StoredAccountCredentials) -> Result<(), StorageError> {
        let account_path = self.base_path.join("account.json");
        let content = serde_json::to_string_pretty(creds)?;
        fs::write(&account_path, content)?;

        // Set restrictive permissions on the account file
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&account_path, fs::Permissions::from_mode(0o600))?;
        }

        info!(contact = ?creds.contact_email, "Saved ACME account credentials");
        Ok(())
    }

    /// Load raw credentials JSON (for instant_acme::AccountCredentials)
    pub fn load_credentials_json(&self) -> Result<Option<String>, StorageError> {
        let creds_path = self.base_path.join("credentials.json");

        if !creds_path.exists() {
            trace!("No stored ACME credentials found");
            return Ok(None);
        }

        let content = fs::read_to_string(&creds_path)?;
        debug!("Loaded ACME credentials JSON");
        Ok(Some(content))
    }

    /// Save raw credentials JSON (for instant_acme::AccountCredentials)
    pub fn save_credentials_json(&self, json: &str) -> Result<(), StorageError> {
        let creds_path = self.base_path.join("credentials.json");
        fs::write(&creds_path, json)?;

        // Set restrictive permissions on the credentials file
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&creds_path, fs::Permissions::from_mode(0o600))?;
        }

        info!("Saved ACME credentials JSON");
        Ok(())
    }

    // =========================================================================
    // Certificate Operations
    // =========================================================================

    /// Get the path to a domain's certificate directory
    fn domain_path(&self, domain: &str) -> PathBuf {
        self.base_path.join("domains").join(domain)
    }

    /// Load a stored certificate for a domain
    pub fn load_certificate(
        &self,
        domain: &str,
    ) -> Result<Option<StoredCertificate>, StorageError> {
        let domain_path = self.domain_path(domain);
        let cert_path = domain_path.join("cert.pem");
        let key_path = domain_path.join("key.pem");
        let meta_path = domain_path.join("meta.json");

        if !cert_path.exists() {
            trace!(domain = %domain, "No stored certificate found");
            return Ok(None);
        }

        let cert_pem = fs::read_to_string(&cert_path)?;
        let key_pem = fs::read_to_string(&key_path)?;
        let meta_content = fs::read_to_string(&meta_path)?;
        let meta: CertificateMeta = serde_json::from_str(&meta_content)?;

        debug!(
            domain = %domain,
            expires = %meta.expires,
            "Loaded stored certificate"
        );

        Ok(Some(StoredCertificate {
            cert_pem,
            key_pem,
            meta,
        }))
    }

    /// Save a certificate for a domain
    pub fn save_certificate(
        &self,
        domain: &str,
        cert_pem: &str,
        key_pem: &str,
        expires: DateTime<Utc>,
        all_domains: &[String],
    ) -> Result<(), StorageError> {
        let domain_path = self.domain_path(domain);
        fs::create_dir_all(&domain_path)?;

        let cert_path = domain_path.join("cert.pem");
        let key_path = domain_path.join("key.pem");
        let meta_path = domain_path.join("meta.json");

        // Write certificate
        fs::write(&cert_path, cert_pem)?;

        // Write private key with restrictive permissions
        fs::write(&key_path, key_pem)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&key_path, fs::Permissions::from_mode(0o600))?;
        }

        // Write metadata
        let meta = CertificateMeta {
            expires,
            issued: Utc::now(),
            domains: all_domains.to_vec(),
            issuer: Some("Let's Encrypt".to_string()),
        };
        let meta_content = serde_json::to_string_pretty(&meta)?;
        fs::write(&meta_path, meta_content)?;

        info!(
            domain = %domain,
            expires = %expires,
            "Saved certificate to storage"
        );

        Ok(())
    }

    /// Check if a certificate needs renewal
    ///
    /// Returns `true` if:
    /// - No certificate exists for the domain
    /// - Certificate expires within `renew_before_days` days
    pub fn needs_renewal(
        &self,
        domain: &str,
        renew_before_days: u32,
    ) -> Result<bool, StorageError> {
        let Some(cert) = self.load_certificate(domain)? else {
            debug!(domain = %domain, "No certificate exists, needs issuance");
            return Ok(true);
        };

        let renew_threshold = Utc::now() + chrono::Duration::days(i64::from(renew_before_days));
        let needs_renewal = cert.meta.expires <= renew_threshold;

        if needs_renewal {
            debug!(
                domain = %domain,
                expires = %cert.meta.expires,
                threshold = %renew_threshold,
                "Certificate needs renewal"
            );
        } else {
            trace!(
                domain = %domain,
                expires = %cert.meta.expires,
                "Certificate is still valid"
            );
        }

        Ok(needs_renewal)
    }

    /// Get certificate paths for a domain
    ///
    /// Returns the paths to cert.pem and key.pem if they exist.
    pub fn certificate_paths(&self, domain: &str) -> Option<(PathBuf, PathBuf)> {
        let domain_path = self.domain_path(domain);
        let cert_path = domain_path.join("cert.pem");
        let key_path = domain_path.join("key.pem");

        if cert_path.exists() && key_path.exists() {
            Some((cert_path, key_path))
        } else {
            None
        }
    }

    /// List all stored domains
    pub fn list_domains(&self) -> Result<Vec<String>, StorageError> {
        let domains_path = self.base_path.join("domains");

        if !domains_path.exists() {
            return Ok(Vec::new());
        }

        let mut domains = Vec::new();
        for entry in fs::read_dir(&domains_path)? {
            let entry = entry?;
            if entry.file_type()?.is_dir() {
                if let Some(name) = entry.file_name().to_str() {
                    domains.push(name.to_string());
                }
            }
        }

        Ok(domains)
    }

    /// Delete stored certificate for a domain
    pub fn delete_certificate(&self, domain: &str) -> Result<(), StorageError> {
        let domain_path = self.domain_path(domain);

        if domain_path.exists() {
            fs::remove_dir_all(&domain_path)?;
            info!(domain = %domain, "Deleted stored certificate");
        } else {
            warn!(domain = %domain, "Certificate to delete not found");
        }

        Ok(())
    }
}

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

    fn setup_storage() -> (TempDir, CertificateStorage) {
        let temp_dir = TempDir::new().unwrap();
        let storage = CertificateStorage::new(temp_dir.path()).unwrap();
        (temp_dir, storage)
    }

    #[test]
    fn test_storage_creation() {
        let (_temp_dir, storage) = setup_storage();
        assert!(storage.base_path().exists());
        assert!(storage.base_path().join("domains").exists());
    }

    #[test]
    fn test_credentials_json_save_load() {
        let (_temp_dir, storage) = setup_storage();

        let test_json = r#"{"test": "credentials"}"#;
        storage.save_credentials_json(test_json).unwrap();

        let loaded = storage.load_credentials_json().unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap(), test_json);
    }

    #[test]
    fn test_certificate_save_load() {
        let (_temp_dir, storage) = setup_storage();

        let cert_pem = "-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----";
        let key_pem = "-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----";
        let expires = Utc::now() + chrono::Duration::days(90);

        storage
            .save_certificate(
                "example.com",
                cert_pem,
                key_pem,
                expires,
                &["example.com".to_string()],
            )
            .unwrap();

        let loaded = storage.load_certificate("example.com").unwrap();
        assert!(loaded.is_some());
        let loaded = loaded.unwrap();
        assert_eq!(loaded.cert_pem, cert_pem);
        assert_eq!(loaded.key_pem, key_pem);
    }

    #[test]
    fn test_needs_renewal_no_cert() {
        let (_temp_dir, storage) = setup_storage();
        assert!(storage.needs_renewal("nonexistent.com", 30).unwrap());
    }

    #[test]
    fn test_needs_renewal_expiring_soon() {
        let (_temp_dir, storage) = setup_storage();

        // Save a certificate expiring in 15 days
        let expires = Utc::now() + chrono::Duration::days(15);
        storage
            .save_certificate(
                "expiring.com",
                "cert",
                "key",
                expires,
                &["expiring.com".to_string()],
            )
            .unwrap();

        // Should need renewal if we renew 30 days before expiry
        assert!(storage.needs_renewal("expiring.com", 30).unwrap());
    }

    #[test]
    fn test_needs_renewal_still_valid() {
        let (_temp_dir, storage) = setup_storage();

        // Save a certificate expiring in 60 days
        let expires = Utc::now() + chrono::Duration::days(60);
        storage
            .save_certificate(
                "valid.com",
                "cert",
                "key",
                expires,
                &["valid.com".to_string()],
            )
            .unwrap();

        // Should NOT need renewal if we renew 30 days before expiry
        assert!(!storage.needs_renewal("valid.com", 30).unwrap());
    }

    #[test]
    fn test_list_domains() {
        let (_temp_dir, storage) = setup_storage();

        storage
            .save_certificate(
                "a.com",
                "cert",
                "key",
                Utc::now() + chrono::Duration::days(90),
                &["a.com".to_string()],
            )
            .unwrap();
        storage
            .save_certificate(
                "b.com",
                "cert",
                "key",
                Utc::now() + chrono::Duration::days(90),
                &["b.com".to_string()],
            )
            .unwrap();

        let domains = storage.list_domains().unwrap();
        assert_eq!(domains.len(), 2);
        assert!(domains.contains(&"a.com".to_string()));
        assert!(domains.contains(&"b.com".to_string()));
    }

    #[test]
    fn test_delete_certificate() {
        let (_temp_dir, storage) = setup_storage();

        storage
            .save_certificate(
                "delete.com",
                "cert",
                "key",
                Utc::now() + chrono::Duration::days(90),
                &["delete.com".to_string()],
            )
            .unwrap();

        assert!(storage.load_certificate("delete.com").unwrap().is_some());

        storage.delete_certificate("delete.com").unwrap();

        assert!(storage.load_certificate("delete.com").unwrap().is_none());
    }
}