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
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
//! ACME client wrapper around instant-acme
//!
//! Provides a high-level interface for ACME protocol operations including:
//! - Account creation and management
//! - Certificate ordering
//! - Challenge handling (HTTP-01 and DNS-01)
//! - Certificate finalization

use std::sync::Arc;
use std::time::Duration;

use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use chrono::{DateTime, Utc};
use instant_acme::{
    Account, AuthorizationStatus, ChallengeType, Identifier, LetsEncrypt, NewAccount, NewOrder,
    Order, OrderStatus, RetryPolicy,
};
use tokio::sync::RwLock;
use tracing::{debug, error, info, trace, warn};

use zentinel_config::server::AcmeConfig;

use super::dns::challenge::{create_challenge_info, Dns01ChallengeInfo};
use super::error::AcmeError;
use super::storage::{CertificateStorage, StoredAccountCredentials};

/// Let's Encrypt production directory URL
const LETSENCRYPT_PRODUCTION: &str = "https://acme-v02.api.letsencrypt.org/directory";
/// Let's Encrypt staging directory URL
const LETSENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory";

/// Default timeout for ACME operations
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
/// Timeout for challenge validation
const CHALLENGE_TIMEOUT: Duration = Duration::from_secs(120);

/// ACME client for automatic certificate management
///
/// Wraps the `instant-acme` library and provides Zentinel-specific functionality
/// for certificate ordering, challenge handling, and persistence.
pub struct AcmeClient {
    /// ACME account (lazy initialized)
    account: Arc<RwLock<Option<Account>>>,
    /// Configuration
    config: AcmeConfig,
    /// Certificate storage
    storage: Arc<CertificateStorage>,
}

impl AcmeClient {
    /// Create a new ACME client
    ///
    /// # Arguments
    ///
    /// * `config` - ACME configuration from the listener
    /// * `storage` - Certificate storage instance
    pub fn new(config: AcmeConfig, storage: Arc<CertificateStorage>) -> Self {
        Self {
            account: Arc::new(RwLock::new(None)),
            config,
            storage,
        }
    }

    /// Get the ACME configuration
    pub fn config(&self) -> &AcmeConfig {
        &self.config
    }

    /// Get the certificate storage
    pub fn storage(&self) -> &CertificateStorage {
        &self.storage
    }

    /// Get the ACME directory URL based on configuration
    fn directory_url(&self) -> &str {
        if let Some(ref url) = self.config.server_url {
            url
        } else if self.config.staging {
            LETSENCRYPT_STAGING
        } else {
            LETSENCRYPT_PRODUCTION
        }
    }

    /// Initialize or load the ACME account
    ///
    /// If account credentials exist in storage, loads them. Otherwise,
    /// creates a new account with Let's Encrypt.
    ///
    /// # Errors
    ///
    /// Returns an error if account creation or loading fails.
    pub async fn init_account(&self) -> Result<(), AcmeError> {
        // Check for existing account credentials (stored as JSON)
        if let Some(creds_json) = self.storage.load_credentials_json()? {
            info!("Loading existing ACME account from storage");

            // Deserialize credentials
            let credentials: instant_acme::AccountCredentials = serde_json::from_str(&creds_json)
                .map_err(|e| {
                AcmeError::AccountCreation(format!("Failed to deserialize credentials: {}", e))
            })?;

            // Reconstruct account from stored credentials
            let account = Account::builder()
                .map_err(|e| AcmeError::AccountCreation(e.to_string()))?
                .from_credentials(credentials)
                .await
                .map_err(|e| AcmeError::AccountCreation(e.to_string()))?;

            *self.account.write().await = Some(account);
            info!("ACME account loaded successfully");
            return Ok(());
        }

        // Create new account
        info!(
            email = %self.config.email,
            server_url = %self.directory_url(),
            key_type = ?self.config.key_type,
            "Creating new ACME account"
        );

        let eab = if let Some(ref eab_config) = self.config.eab {
            let hmac_key = URL_SAFE_NO_PAD.decode(&eab_config.hmac_key).map_err(|e| {
                AcmeError::AccountCreation(format!("Invalid EAB HMAC key (base64url): {}", e))
            })?;
            Some(instant_acme::ExternalAccountKey::new(
                eab_config.kid.clone(),
                &hmac_key,
            ))
        } else {
            None
        };

        let (account, credentials) = Account::builder()
            .map_err(|e| AcmeError::AccountCreation(e.to_string()))?
            .create(
                &NewAccount {
                    contact: &[&format!("mailto:{}", self.config.email)],
                    terms_of_service_agreed: true,
                    only_return_existing: false,
                },
                self.directory_url().to_owned(),
                eab.as_ref(),
            )
            .await
            .map_err(|e| AcmeError::AccountCreation(e.to_string()))?;

        // Store credentials as JSON (AccountCredentials is serializable)
        let creds_json = serde_json::to_string_pretty(&credentials).map_err(|e| {
            AcmeError::AccountCreation(format!("Failed to serialize credentials: {}", e))
        })?;
        self.storage.save_credentials_json(&creds_json)?;

        *self.account.write().await = Some(account);
        info!("ACME account created successfully");

        Ok(())
    }

    /// Order a certificate for the configured domains
    ///
    /// Creates a new certificate order and returns it along with the
    /// authorization challenges that need to be completed.
    ///
    /// # Returns
    ///
    /// A tuple of (Order, Vec<`ChallengeInfo`>) containing the order and
    /// HTTP-01 challenge information for each domain.
    pub async fn create_order(&self) -> Result<(Order, Vec<ChallengeInfo>), AcmeError> {
        let account_guard = self.account.read().await;
        let account = account_guard.as_ref().ok_or(AcmeError::NoAccount)?;

        // Create identifiers for all domains
        let identifiers: Vec<Identifier> = self
            .config
            .domains
            .iter()
            .map(|d: &String| Identifier::Dns(d.clone()))
            .collect();

        info!(domains = ?self.config.domains, "Creating certificate order");

        // Create the order
        let mut order = account
            .new_order(&NewOrder::new(&identifiers))
            .await
            .map_err(|e| AcmeError::OrderCreation(e.to_string()))?;

        // Get authorizations and extract HTTP-01 challenges
        let mut authorizations = order.authorizations();
        let mut challenges = Vec::new();

        while let Some(result) = authorizations.next().await {
            let mut authz = result.map_err(|e| {
                AcmeError::OrderCreation(format!("Failed to get authorization: {}", e))
            })?;

            let identifier = authz.identifier();
            let domain = match &identifier.identifier {
                Identifier::Dns(domain) => domain.clone(),
                _ => continue,
            };

            debug!(domain = %domain, status = ?authz.status, "Processing authorization");

            // Skip if already valid
            if authz.status == AuthorizationStatus::Valid {
                debug!(domain = %domain, "Authorization already valid");
                continue;
            }

            // Find HTTP-01 challenge
            let http01_challenge = authz
                .challenge(ChallengeType::Http01)
                .ok_or_else(|| AcmeError::NoHttp01Challenge(domain.clone()))?;

            let key_authorization = http01_challenge.key_authorization();

            challenges.push(ChallengeInfo {
                domain,
                token: http01_challenge.token.clone(),
                key_authorization: key_authorization.as_str().to_string(),
                url: http01_challenge.url.clone(),
            });
        }

        Ok((order, challenges))
    }

    /// Order a certificate using DNS-01 challenges
    ///
    /// Creates a new certificate order and returns it along with the
    /// DNS-01 challenge information for each domain.
    ///
    /// # Returns
    ///
    /// A tuple of (Order, Vec<`Dns01ChallengeInfo`>) containing the order and
    /// DNS-01 challenge information for each domain.
    pub async fn create_order_dns01(&self) -> Result<(Order, Vec<Dns01ChallengeInfo>), AcmeError> {
        let account_guard = self.account.read().await;
        let account = account_guard.as_ref().ok_or(AcmeError::NoAccount)?;

        // Create identifiers for all domains
        let identifiers: Vec<Identifier> = self
            .config
            .domains
            .iter()
            .map(|d: &String| Identifier::Dns(d.clone()))
            .collect();

        info!(domains = ?self.config.domains, "Creating certificate order with DNS-01 challenges");

        // Create the order
        let mut order = account
            .new_order(&NewOrder::new(&identifiers))
            .await
            .map_err(|e| AcmeError::OrderCreation(e.to_string()))?;

        // Get authorizations and extract DNS-01 challenges
        let mut authorizations = order.authorizations();
        let mut challenges = Vec::new();

        while let Some(result) = authorizations.next().await {
            let mut authz = result.map_err(|e| {
                AcmeError::OrderCreation(format!("Failed to get authorization: {}", e))
            })?;

            let identifier = authz.identifier();
            let domain = match &identifier.identifier {
                Identifier::Dns(domain) => domain.clone(),
                _ => continue,
            };

            debug!(domain = %domain, status = ?authz.status, "Processing DNS-01 authorization");

            // Skip if already valid
            if authz.status == AuthorizationStatus::Valid {
                debug!(domain = %domain, "Authorization already valid");
                continue;
            }

            // Find DNS-01 challenge
            let dns01_challenge = authz
                .challenge(ChallengeType::Dns01)
                .ok_or_else(|| AcmeError::NoDns01Challenge(domain.clone()))?;

            let key_authorization = dns01_challenge.key_authorization();

            // Create DNS-01 challenge info with computed value
            let challenge_info =
                create_challenge_info(&domain, key_authorization.as_str(), &dns01_challenge.url);

            challenges.push(challenge_info);
        }

        Ok((order, challenges))
    }

    /// Notify the ACME server that a challenge is ready for validation
    ///
    /// Iterates through the order's authorizations to find the challenge
    /// matching the given URL and marks it as ready.
    ///
    /// # Arguments
    ///
    /// * `order` - The certificate order
    /// * `challenge_url` - The URL of the challenge to validate
    pub async fn validate_challenge(
        &self,
        order: &mut Order,
        challenge_url: &str,
    ) -> Result<(), AcmeError> {
        debug!(challenge_url = %challenge_url, "Setting challenge ready");

        // Iterate authorizations to find the matching challenge by URL
        let mut authorizations = order.authorizations();
        while let Some(result) = authorizations.next().await {
            let mut authz = result.map_err(|e| AcmeError::ChallengeValidation {
                domain: "unknown".to_string(),
                message: format!("Failed to get authorization: {}", e),
            })?;

            // Determine which challenge type matches the URL
            let matching_type = authz
                .challenges
                .iter()
                .find(|c| c.url == challenge_url)
                .map(|c| c.r#type.clone());

            if let Some(challenge_type) = matching_type {
                if let Some(mut challenge) = authz.challenge(challenge_type) {
                    challenge
                        .set_ready()
                        .await
                        .map_err(|e| AcmeError::ChallengeValidation {
                            domain: "unknown".to_string(),
                            message: e.to_string(),
                        })?;
                    return Ok(());
                }
            }
        }

        Err(AcmeError::ChallengeValidation {
            domain: "unknown".to_string(),
            message: format!("Challenge not found for URL: {}", challenge_url),
        })
    }

    /// Wait for the order to become ready (all challenges validated)
    ///
    /// Polls the order status until it becomes ready or times out.
    pub async fn wait_for_order_ready(&self, order: &mut Order) -> Result<(), AcmeError> {
        let deadline = tokio::time::Instant::now() + CHALLENGE_TIMEOUT;

        loop {
            let state = order
                .refresh()
                .await
                .map_err(|e| AcmeError::OrderCreation(format!("Failed to refresh order: {}", e)))?;

            match state.status {
                OrderStatus::Ready => {
                    info!("Order is ready for finalization");
                    return Ok(());
                }
                OrderStatus::Invalid => {
                    error!("Order became invalid");
                    return Err(AcmeError::OrderCreation("Order became invalid".to_string()));
                }
                OrderStatus::Valid => {
                    info!("Order is already valid (certificate issued)");
                    return Ok(());
                }
                OrderStatus::Pending | OrderStatus::Processing => {
                    if tokio::time::Instant::now() > deadline {
                        return Err(AcmeError::Timeout(
                            "Timed out waiting for order to become ready".to_string(),
                        ));
                    }
                    trace!(status = ?state.status, "Order not ready yet, waiting...");
                    tokio::time::sleep(Duration::from_secs(2)).await;
                }
            }
        }
    }

    /// Finalize the order and retrieve the certificate
    ///
    /// Generates a CSR, submits it to the ACME server, and retrieves
    /// the issued certificate.
    ///
    /// # Returns
    ///
    /// A tuple of (certificate_pem, private_key_pem, expiry_date)
    pub async fn finalize_order(
        &self,
        order: &mut Order,
    ) -> Result<(String, String, DateTime<Utc>), AcmeError> {
        info!("Finalizing certificate order");

        // Map config key type to rcgen signature algorithm
        use zentinel_config::server::AcmeKeyType;
        let algo = match self.config.key_type {
            AcmeKeyType::EcdsaP256 => &rcgen::PKCS_ECDSA_P256_SHA256,
            AcmeKeyType::EcdsaP384 => &rcgen::PKCS_ECDSA_P384_SHA384,
        };

        // Generate a new private key for the certificate
        let cert_key = rcgen::KeyPair::generate_for(algo)
            .map_err(|e| AcmeError::Finalization(format!("Failed to generate key: {}", e)))?;

        // Create CSR with all domains
        let mut params = rcgen::CertificateParams::new(self.config.domains.clone())
            .map_err(|e| AcmeError::Finalization(format!("Failed to create CSR params: {}", e)))?;

        // Set the Common Name to the first domain — rcgen defaults to "rcgen self signed cert"
        // which ACME CAs reject as an invalid domain name
        let mut dn = rcgen::DistinguishedName::new();
        dn.push(rcgen::DnType::CommonName, self.config.domains[0].clone());
        params.distinguished_name = dn;

        // Serialize CSR with the key pair (rcgen 0.14 API)
        let csr_request = params
            .serialize_request(&cert_key)
            .map_err(|e| AcmeError::Finalization(format!("Failed to serialize CSR: {}", e)))?;
        let csr = csr_request.der().to_vec();

        // Submit CSR and finalize
        order
            .finalize_csr(&csr)
            .await
            .map_err(|e| AcmeError::Finalization(format!("Failed to finalize order: {}", e)))?;

        // Wait for certificate to be issued
        let deadline = tokio::time::Instant::now() + DEFAULT_TIMEOUT;
        let cert_chain = loop {
            let state = order
                .refresh()
                .await
                .map_err(|e| AcmeError::Finalization(format!("Failed to refresh order: {}", e)))?;

            match state.status {
                OrderStatus::Valid => {
                    let cert_chain = order.certificate().await.map_err(|e| {
                        AcmeError::Finalization(format!("Failed to get certificate: {}", e))
                    })?;
                    break cert_chain.ok_or_else(|| {
                        AcmeError::Finalization("No certificate in response".to_string())
                    })?;
                }
                OrderStatus::Invalid => {
                    return Err(AcmeError::Finalization("Order became invalid".to_string()));
                }
                _ => {
                    if tokio::time::Instant::now() > deadline {
                        return Err(AcmeError::Timeout(
                            "Timed out waiting for certificate".to_string(),
                        ));
                    }
                    tokio::time::sleep(Duration::from_secs(1)).await;
                }
            }
        };

        // Get the private key PEM
        let key_pem = cert_key.serialize_pem();

        // Parse certificate to get expiry date
        let expiry = parse_certificate_expiry(&cert_chain)?;

        info!(
            domains = ?self.config.domains,
            expires = %expiry,
            "Certificate issued successfully"
        );

        Ok((cert_chain, key_pem, expiry))
    }

    /// Check if a certificate exists and needs renewal
    pub fn needs_renewal(&self, domain: &str) -> Result<bool, AcmeError> {
        Ok(self
            .storage
            .needs_renewal(domain, self.config.renew_before_days)?)
    }
}

/// Information about an HTTP-01 challenge
#[derive(Debug, Clone)]
pub struct ChallengeInfo {
    /// Domain this challenge is for
    pub domain: String,
    /// Challenge token (appears in URL path)
    pub token: String,
    /// Key authorization (the response content)
    pub key_authorization: String,
    /// Challenge URL for validation notification
    pub url: String,
}

/// Parse certificate PEM to extract expiry date
fn parse_certificate_expiry(cert_pem: &str) -> Result<DateTime<Utc>, AcmeError> {
    use x509_parser::prelude::*;

    // Parse PEM
    let (_, pem) = pem::parse_x509_pem(cert_pem.as_bytes())
        .map_err(|e| AcmeError::CertificateParse(format!("Failed to parse PEM: {}", e)))?;

    // Parse X.509 certificate
    let (_, cert) = X509Certificate::from_der(&pem.contents)
        .map_err(|e| AcmeError::CertificateParse(format!("Failed to parse certificate: {}", e)))?;

    // Get expiry time
    let not_after = cert.validity().not_after;
    let timestamp = not_after.timestamp();

    DateTime::from_timestamp(timestamp, 0)
        .ok_or_else(|| AcmeError::CertificateParse("Invalid expiry timestamp".to_string()))
}

impl std::fmt::Debug for AcmeClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AcmeClient")
            .field("config", &self.config)
            .field(
                "has_account",
                &self
                    .account
                    .try_read()
                    .map(|a| a.is_some())
                    .unwrap_or(false),
            )
            .finish()
    }
}