a3s-gateway 0.2.5

A3S Gateway - AI-native API gateway with reverse proxy, routing, and agent orchestration
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! ACME/Let's Encrypt — automatic TLS certificate management
//!
//! Provides automatic certificate issuance and renewal via the ACME protocol.
//! Supports HTTP-01 challenge validation for domain verification.

#![allow(dead_code)]
use crate::error::{GatewayError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime};

/// ACME directory URLs
pub const LETS_ENCRYPT_PRODUCTION: &str = "https://acme-v02.api.letsencrypt.org/directory";
pub const LETS_ENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory";

/// Default renewal threshold (30 days before expiry)
const DEFAULT_RENEWAL_DAYS: u64 = 30;

/// ACME challenge type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ChallengeType {
    /// HTTP-01: serve a token at /.well-known/acme-challenge/<token>
    #[serde(rename = "http-01")]
    #[default]
    Http01,
    /// DNS-01: create a TXT record at _acme-challenge.<domain>
    #[serde(rename = "dns-01")]
    Dns01,
}

/// ACME configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AcmeConfig {
    /// ACME directory URL
    #[serde(default = "default_directory")]
    pub directory_url: String,
    /// Contact email for the ACME account
    pub email: String,
    /// Domains to obtain certificates for
    pub domains: Vec<String>,
    /// Storage path for certificates and account keys
    #[serde(default = "default_storage_path")]
    pub storage_path: PathBuf,
    /// Use staging environment (for testing)
    #[serde(default)]
    pub staging: bool,
    /// Days before expiry to trigger renewal
    #[serde(default = "default_renewal_days")]
    pub renewal_days: u64,
    /// Challenge type (default: http-01)
    #[serde(default)]
    pub challenge_type: ChallengeType,
    /// DNS provider configuration (required when challenge_type is dns-01)
    #[serde(default)]
    pub dns_provider: Option<crate::proxy::acme_dns::DnsProviderConfig>,
}

fn default_directory() -> String {
    LETS_ENCRYPT_PRODUCTION.to_string()
}

fn default_storage_path() -> PathBuf {
    PathBuf::from("/etc/gateway/acme")
}

fn default_renewal_days() -> u64 {
    DEFAULT_RENEWAL_DAYS
}

impl Default for AcmeConfig {
    fn default() -> Self {
        Self {
            directory_url: default_directory(),
            email: String::new(),
            domains: Vec::new(),
            storage_path: default_storage_path(),
            staging: false,
            renewal_days: DEFAULT_RENEWAL_DAYS,
            challenge_type: ChallengeType::default(),
            dns_provider: None,
        }
    }
}

impl AcmeConfig {
    /// Get the effective directory URL (staging or production)
    pub fn effective_directory(&self) -> &str {
        if self.staging {
            LETS_ENCRYPT_STAGING
        } else {
            &self.directory_url
        }
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<()> {
        if self.email.is_empty() {
            return Err(GatewayError::Config("ACME email is required".to_string()));
        }
        if self.domains.is_empty() {
            return Err(GatewayError::Config(
                "ACME requires at least one domain".to_string(),
            ));
        }
        for domain in &self.domains {
            if domain.is_empty() || domain.contains(' ') {
                return Err(GatewayError::Config(format!(
                    "Invalid ACME domain: '{}'",
                    domain
                )));
            }
        }
        if self.challenge_type == ChallengeType::Dns01 {
            match &self.dns_provider {
                Some(dns_config) => dns_config.validate()?,
                None => {
                    return Err(GatewayError::Config(
                        "DNS provider configuration is required for DNS-01 challenge".to_string(),
                    ));
                }
            }
        }
        Ok(())
    }
}

/// Certificate status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CertStatus {
    /// No certificate exists
    Missing,
    /// Certificate is valid
    Valid,
    /// Certificate is expiring soon (within renewal window)
    ExpiringSoon,
    /// Certificate has expired
    Expired,
    /// Certificate issuance/renewal is in progress
    Pending,
    /// Certificate issuance/renewal failed
    Failed(String),
}

impl std::fmt::Display for CertStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Missing => write!(f, "missing"),
            Self::Valid => write!(f, "valid"),
            Self::ExpiringSoon => write!(f, "expiring-soon"),
            Self::Expired => write!(f, "expired"),
            Self::Pending => write!(f, "pending"),
            Self::Failed(msg) => write!(f, "failed: {}", msg),
        }
    }
}

/// Stored certificate information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertInfo {
    /// Domain name
    pub domain: String,
    /// Certificate PEM data
    pub cert_pem: String,
    /// Private key PEM data
    pub key_pem: String,
    /// Expiry timestamp (seconds since epoch)
    pub expires_at: u64,
    /// Issuance timestamp (seconds since epoch)
    pub issued_at: u64,
}

impl CertInfo {
    /// Check if the certificate has expired
    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now >= self.expires_at
    }

    /// Check if the certificate is expiring within the given number of days
    pub fn is_expiring_within(&self, days: u64) -> bool {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        let threshold = self.expires_at.saturating_sub(days * 86400);
        now >= threshold
    }

    /// Get the remaining validity duration
    pub fn remaining(&self) -> Duration {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        if now >= self.expires_at {
            Duration::ZERO
        } else {
            Duration::from_secs(self.expires_at - now)
        }
    }

    /// Get the status based on renewal threshold
    pub fn status(&self, renewal_days: u64) -> CertStatus {
        if self.is_expired() {
            CertStatus::Expired
        } else if self.is_expiring_within(renewal_days) {
            CertStatus::ExpiringSoon
        } else {
            CertStatus::Valid
        }
    }
}

/// HTTP-01 challenge token store
///
/// When the ACME server sends an HTTP-01 challenge, the gateway must serve
/// the challenge response at `/.well-known/acme-challenge/<token>`.
pub struct ChallengeStore {
    /// token → key_authorization mapping
    challenges: Arc<RwLock<HashMap<String, String>>>,
}

impl ChallengeStore {
    /// Create a new challenge store
    pub fn new() -> Self {
        Self {
            challenges: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Add a challenge token and its key authorization
    pub fn add(&self, token: String, key_authorization: String) {
        let mut challenges = self.challenges.write().unwrap();
        challenges.insert(token, key_authorization);
    }

    /// Get the key authorization for a token
    pub fn get(&self, token: &str) -> Option<String> {
        let challenges = self.challenges.read().unwrap();
        challenges.get(token).cloned()
    }

    /// Remove a challenge token
    pub fn remove(&self, token: &str) {
        let mut challenges = self.challenges.write().unwrap();
        challenges.remove(token);
    }

    /// Clear all challenges
    pub fn clear(&self) {
        let mut challenges = self.challenges.write().unwrap();
        challenges.clear();
    }

    /// Number of active challenges
    pub fn len(&self) -> usize {
        let challenges = self.challenges.read().unwrap();
        challenges.len()
    }

    /// Check if there are no active challenges
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Check if a request path is an ACME challenge path
    pub fn is_challenge_path(path: &str) -> bool {
        path.starts_with("/.well-known/acme-challenge/")
    }

    /// Extract the token from an ACME challenge path
    pub fn extract_token(path: &str) -> Option<&str> {
        path.strip_prefix("/.well-known/acme-challenge/")
            .filter(|t| !t.is_empty())
    }
}

impl Default for ChallengeStore {
    fn default() -> Self {
        Self::new()
    }
}

/// Certificate storage — persists certificates to disk
pub struct CertStorage {
    base_path: PathBuf,
}

impl CertStorage {
    /// Create a new certificate storage
    pub fn new(base_path: impl AsRef<Path>) -> Self {
        Self {
            base_path: base_path.as_ref().to_path_buf(),
        }
    }

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

    /// Get the certificate file path for a domain
    pub fn cert_path(&self, domain: &str) -> PathBuf {
        self.base_path
            .join(format!("{}.crt", sanitize_domain(domain)))
    }

    /// Get the key file path for a domain
    pub fn key_path(&self, domain: &str) -> PathBuf {
        self.base_path
            .join(format!("{}.key", sanitize_domain(domain)))
    }

    /// Get the metadata file path for a domain
    pub fn meta_path(&self, domain: &str) -> PathBuf {
        self.base_path
            .join(format!("{}.json", sanitize_domain(domain)))
    }

    /// Save certificate info to disk
    pub fn save(&self, info: &CertInfo) -> Result<()> {
        std::fs::create_dir_all(&self.base_path).map_err(|e| {
            GatewayError::Other(format!(
                "Failed to create ACME storage directory {}: {}",
                self.base_path.display(),
                e
            ))
        })?;

        // Write cert PEM
        std::fs::write(self.cert_path(&info.domain), &info.cert_pem)
            .map_err(|e| GatewayError::Other(format!("Failed to write certificate: {}", e)))?;

        // Write key PEM
        std::fs::write(self.key_path(&info.domain), &info.key_pem)
            .map_err(|e| GatewayError::Other(format!("Failed to write private key: {}", e)))?;

        // Write metadata
        let meta = serde_json::to_string_pretty(info).map_err(|e| {
            GatewayError::Other(format!("Failed to serialize cert metadata: {}", e))
        })?;
        std::fs::write(self.meta_path(&info.domain), meta)
            .map_err(|e| GatewayError::Other(format!("Failed to write cert metadata: {}", e)))?;

        Ok(())
    }

    /// Load certificate info from disk
    pub fn load(&self, domain: &str) -> Result<CertInfo> {
        let meta_path = self.meta_path(domain);
        let content = std::fs::read_to_string(&meta_path).map_err(|e| {
            GatewayError::Other(format!(
                "Failed to read cert metadata {}: {}",
                meta_path.display(),
                e
            ))
        })?;
        let info: CertInfo = serde_json::from_str(&content)
            .map_err(|e| GatewayError::Other(format!("Failed to parse cert metadata: {}", e)))?;
        Ok(info)
    }

    /// Check if a certificate exists on disk
    pub fn exists(&self, domain: &str) -> bool {
        self.cert_path(domain).exists() && self.key_path(domain).exists()
    }
}

/// Sanitize a domain name for use as a filename
fn sanitize_domain(domain: &str) -> String {
    domain
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '.' || c == '-' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

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

    // --- AcmeConfig ---

    #[test]
    fn test_config_default() {
        let config = AcmeConfig::default();
        assert_eq!(config.directory_url, LETS_ENCRYPT_PRODUCTION);
        assert!(config.email.is_empty());
        assert!(config.domains.is_empty());
        assert!(!config.staging);
        assert_eq!(config.renewal_days, 30);
    }

    #[test]
    fn test_config_effective_directory_production() {
        let config = AcmeConfig {
            staging: false,
            ..Default::default()
        };
        assert_eq!(config.effective_directory(), LETS_ENCRYPT_PRODUCTION);
    }

    #[test]
    fn test_config_effective_directory_staging() {
        let config = AcmeConfig {
            staging: true,
            ..Default::default()
        };
        assert_eq!(config.effective_directory(), LETS_ENCRYPT_STAGING);
    }

    #[test]
    fn test_config_validate_ok() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["example.com".to_string()],
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validate_missing_email() {
        let config = AcmeConfig {
            domains: vec!["example.com".to_string()],
            ..Default::default()
        };
        let err = config.validate().unwrap_err();
        assert!(err.to_string().contains("email"));
    }

    #[test]
    fn test_config_validate_missing_domains() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            ..Default::default()
        };
        let err = config.validate().unwrap_err();
        assert!(err.to_string().contains("domain"));
    }

    #[test]
    fn test_config_validate_invalid_domain() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["invalid domain".to_string()],
            ..Default::default()
        };
        let err = config.validate().unwrap_err();
        assert!(err.to_string().contains("Invalid ACME domain"));
    }

    #[test]
    fn test_config_validate_empty_domain() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["".to_string()],
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_serialization() {
        let config = AcmeConfig {
            email: "test@example.com".to_string(),
            domains: vec!["example.com".to_string(), "www.example.com".to_string()],
            staging: true,
            ..Default::default()
        };
        let json = serde_json::to_string(&config).unwrap();
        let parsed: AcmeConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.email, "test@example.com");
        assert_eq!(parsed.domains.len(), 2);
        assert!(parsed.staging);
    }

    // --- CertStatus ---

    #[test]
    fn test_cert_status_display() {
        assert_eq!(CertStatus::Missing.to_string(), "missing");
        assert_eq!(CertStatus::Valid.to_string(), "valid");
        assert_eq!(CertStatus::ExpiringSoon.to_string(), "expiring-soon");
        assert_eq!(CertStatus::Expired.to_string(), "expired");
        assert_eq!(CertStatus::Pending.to_string(), "pending");
        assert_eq!(
            CertStatus::Failed("timeout".to_string()).to_string(),
            "failed: timeout"
        );
    }

    #[test]
    fn test_cert_status_equality() {
        assert_eq!(CertStatus::Valid, CertStatus::Valid);
        assert_ne!(CertStatus::Valid, CertStatus::Expired);
    }

    // --- CertInfo ---

    #[test]
    fn test_cert_info_expired() {
        let info = CertInfo {
            domain: "example.com".to_string(),
            cert_pem: String::new(),
            key_pem: String::new(),
            expires_at: 1000, // Long expired
            issued_at: 0,
        };
        assert!(info.is_expired());
        assert_eq!(info.remaining(), Duration::ZERO);
        assert_eq!(info.status(30), CertStatus::Expired);
    }

    #[test]
    fn test_cert_info_valid() {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let info = CertInfo {
            domain: "example.com".to_string(),
            cert_pem: String::new(),
            key_pem: String::new(),
            expires_at: now + 90 * 86400, // 90 days from now
            issued_at: now,
        };
        assert!(!info.is_expired());
        assert!(info.remaining() > Duration::ZERO);
        assert_eq!(info.status(30), CertStatus::Valid);
    }

    #[test]
    fn test_cert_info_expiring_soon() {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let info = CertInfo {
            domain: "example.com".to_string(),
            cert_pem: String::new(),
            key_pem: String::new(),
            expires_at: now + 15 * 86400, // 15 days from now
            issued_at: now,
        };
        assert!(!info.is_expired());
        assert!(info.is_expiring_within(30));
        assert_eq!(info.status(30), CertStatus::ExpiringSoon);
    }

    #[test]
    fn test_cert_info_serialization() {
        let info = CertInfo {
            domain: "example.com".to_string(),
            cert_pem: "-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----".to_string(),
            key_pem: "-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----".to_string(),
            expires_at: 1700000000,
            issued_at: 1690000000,
        };
        let json = serde_json::to_string(&info).unwrap();
        let parsed: CertInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.domain, "example.com");
        assert_eq!(parsed.expires_at, 1700000000);
    }

    // --- ChallengeStore ---

    #[test]
    fn test_challenge_store_new() {
        let store = ChallengeStore::new();
        assert!(store.is_empty());
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_challenge_store_add_get() {
        let store = ChallengeStore::new();
        store.add("token123".to_string(), "auth456".to_string());
        assert_eq!(store.get("token123"), Some("auth456".to_string()));
        assert_eq!(store.len(), 1);
    }

    #[test]
    fn test_challenge_store_get_missing() {
        let store = ChallengeStore::new();
        assert_eq!(store.get("nonexistent"), None);
    }

    #[test]
    fn test_challenge_store_remove() {
        let store = ChallengeStore::new();
        store.add("token123".to_string(), "auth456".to_string());
        store.remove("token123");
        assert!(store.is_empty());
        assert_eq!(store.get("token123"), None);
    }

    #[test]
    fn test_challenge_store_clear() {
        let store = ChallengeStore::new();
        store.add("t1".to_string(), "a1".to_string());
        store.add("t2".to_string(), "a2".to_string());
        assert_eq!(store.len(), 2);
        store.clear();
        assert!(store.is_empty());
    }

    #[test]
    fn test_is_challenge_path() {
        assert!(ChallengeStore::is_challenge_path(
            "/.well-known/acme-challenge/abc123"
        ));
        assert!(!ChallengeStore::is_challenge_path("/api/data"));
        assert!(!ChallengeStore::is_challenge_path("/.well-known/other"));
    }

    #[test]
    fn test_extract_token() {
        assert_eq!(
            ChallengeStore::extract_token("/.well-known/acme-challenge/abc123"),
            Some("abc123")
        );
        assert_eq!(
            ChallengeStore::extract_token("/.well-known/acme-challenge/"),
            None
        );
        assert_eq!(ChallengeStore::extract_token("/other/path"), None);
    }

    // --- CertStorage ---

    #[test]
    fn test_cert_storage_paths() {
        let storage = CertStorage::new("/etc/acme");
        assert_eq!(storage.base_path(), Path::new("/etc/acme"));
        assert_eq!(
            storage.cert_path("example.com"),
            PathBuf::from("/etc/acme/example.com.crt")
        );
        assert_eq!(
            storage.key_path("example.com"),
            PathBuf::from("/etc/acme/example.com.key")
        );
        assert_eq!(
            storage.meta_path("example.com"),
            PathBuf::from("/etc/acme/example.com.json")
        );
    }

    #[test]
    fn test_cert_storage_save_load() {
        let dir = tempfile::tempdir().unwrap();
        let storage = CertStorage::new(dir.path());

        let info = CertInfo {
            domain: "test.example.com".to_string(),
            cert_pem: "cert-data".to_string(),
            key_pem: "key-data".to_string(),
            expires_at: 1700000000,
            issued_at: 1690000000,
        };

        storage.save(&info).unwrap();
        assert!(storage.exists("test.example.com"));

        let loaded = storage.load("test.example.com").unwrap();
        assert_eq!(loaded.domain, "test.example.com");
        assert_eq!(loaded.cert_pem, "cert-data");
        assert_eq!(loaded.key_pem, "key-data");
    }

    #[test]
    fn test_cert_storage_not_exists() {
        let dir = tempfile::tempdir().unwrap();
        let storage = CertStorage::new(dir.path());
        assert!(!storage.exists("nonexistent.com"));
    }

    #[test]
    fn test_cert_storage_load_missing() {
        let dir = tempfile::tempdir().unwrap();
        let storage = CertStorage::new(dir.path());
        let result = storage.load("nonexistent.com");
        assert!(result.is_err());
    }

    // --- sanitize_domain ---

    #[test]
    fn test_sanitize_domain() {
        assert_eq!(sanitize_domain("example.com"), "example.com");
        assert_eq!(sanitize_domain("sub.example.com"), "sub.example.com");
        assert_eq!(sanitize_domain("my-domain.com"), "my-domain.com");
        assert_eq!(sanitize_domain("*.example.com"), "_.example.com");
    }

    // --- Constants ---

    #[test]
    fn test_lets_encrypt_urls() {
        assert!(LETS_ENCRYPT_PRODUCTION.contains("acme-v02"));
        assert!(LETS_ENCRYPT_STAGING.contains("staging"));
    }

    // --- ChallengeType ---

    #[test]
    fn test_challenge_type_default() {
        assert_eq!(ChallengeType::default(), ChallengeType::Http01);
    }

    #[test]
    fn test_challenge_type_serialization() {
        let json = serde_json::to_string(&ChallengeType::Http01).unwrap();
        assert_eq!(json, "\"http-01\"");
        let json = serde_json::to_string(&ChallengeType::Dns01).unwrap();
        assert_eq!(json, "\"dns-01\"");
    }

    #[test]
    fn test_challenge_type_deserialization() {
        let parsed: ChallengeType = serde_json::from_str("\"http-01\"").unwrap();
        assert_eq!(parsed, ChallengeType::Http01);
        let parsed: ChallengeType = serde_json::from_str("\"dns-01\"").unwrap();
        assert_eq!(parsed, ChallengeType::Dns01);
    }

    // --- DNS-01 validation ---

    #[test]
    fn test_config_validate_dns01_missing_provider() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["*.example.com".to_string()],
            challenge_type: ChallengeType::Dns01,
            dns_provider: None,
            ..Default::default()
        };
        let err = config.validate().unwrap_err();
        assert!(err.to_string().contains("DNS provider"));
    }

    #[test]
    fn test_config_validate_dns01_with_provider() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["*.example.com".to_string()],
            challenge_type: ChallengeType::Dns01,
            dns_provider: Some(crate::proxy::acme_dns::DnsProviderConfig {
                provider: crate::proxy::acme_dns::DnsProvider::Cloudflare,
                api_token: "tok".to_string(),
                zone_id: "z1".to_string(),
                propagation_wait_secs: 60,
            }),
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validate_dns01_invalid_provider() {
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["*.example.com".to_string()],
            challenge_type: ChallengeType::Dns01,
            dns_provider: Some(crate::proxy::acme_dns::DnsProviderConfig {
                provider: crate::proxy::acme_dns::DnsProvider::Cloudflare,
                api_token: String::new(), // invalid
                zone_id: "z1".to_string(),
                propagation_wait_secs: 60,
            }),
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_http01_ignores_dns_provider() {
        // HTTP-01 should not require dns_provider
        let config = AcmeConfig {
            email: "admin@example.com".to_string(),
            domains: vec!["example.com".to_string()],
            challenge_type: ChallengeType::Http01,
            dns_provider: None,
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }
}