salvo-acme 0.92.2

Acme for Salvo web server framework. Powered by certon.
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
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::io::{Error as IoError, Result as IoResult};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use certon::acme_issuer::CertIssuer;
use certon::crypto::KeyType;
use certon::solvers::Solver;
use certon::storage::Storage;
use certon::{OcspConfig, OnDemandConfig};
use tokio::sync::RwLock;

use super::{ChallengeType, LETS_ENCRYPT_PRODUCTION};

/// ACME configuration.
#[allow(dead_code)]
pub struct AcmeConfig {
    pub(crate) directory_name: String,
    pub(crate) directory_url: String,
    pub(crate) domains: Vec<String>,
    pub(crate) contacts: Vec<String>,
    pub(crate) challenge_type: ChallengeType,
    pub(crate) cache_path: Option<PathBuf>,
    pub(crate) keys_for_http01: Option<Arc<RwLock<HashMap<String, String>>>>,
    pub(crate) before_expired: Duration,
    // --- New certon-powered fields ---
    pub(crate) key_type: KeyType,
    pub(crate) issuers: Option<Vec<Arc<dyn CertIssuer>>>,
    pub(crate) storage: Option<Arc<dyn Storage>>,
    pub(crate) http01_solver: Option<Arc<dyn Solver>>,
    pub(crate) tls_alpn01_solver: Option<Arc<dyn Solver>>,
    pub(crate) dns01_solver: Option<Arc<dyn Solver>>,
    pub(crate) ocsp: OcspConfig,
    pub(crate) on_demand: Option<Arc<OnDemandConfig>>,
    pub(crate) zerossl_api_key: Option<String>,
    pub(crate) agree_to_tos: bool,
}

impl AcmeConfig {
    /// Create an ACME configuration builder.
    #[inline]
    #[must_use]
    pub fn builder() -> AcmeConfigBuilder {
        AcmeConfigBuilder::new()
    }
}

impl Debug for AcmeConfig {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("AcmeConfig")
            .field("directory_name", &self.directory_name)
            .field("directory_url", &self.directory_url)
            .field("domains", &self.domains)
            .field("contacts", &self.contacts)
            .field("challenge_type", &self.challenge_type)
            .field("cache_path", &self.cache_path)
            .field("key_type", &self.key_type)
            .finish()
    }
}

/// ACME configuration builder.
///
/// Provides a fluent API for configuring ACME certificate management.
/// The builder now exposes advanced features from the `certon` crate:
///
/// - **Multiple issuers** via [`add_issuer`](AcmeConfigBuilder::add_issuer).
/// - **DNS-01 challenges** via [`dns01_challenge`](AcmeConfigBuilder::dns01_challenge).
/// - **On-demand TLS** via [`on_demand`](AcmeConfigBuilder::on_demand).
/// - **Key type selection** via [`key_type`](AcmeConfigBuilder::key_type).
/// - **OCSP stapling** via [`ocsp`](AcmeConfigBuilder::ocsp).
/// - **Custom storage** via [`storage`](AcmeConfigBuilder::storage).
/// - **ZeroSSL** via [`zerossl_api_key`](AcmeConfigBuilder::zerossl_api_key).
pub struct AcmeConfigBuilder {
    pub(crate) directory_name: String,
    pub(crate) directory_url: String,
    pub(crate) domains: Vec<String>,
    pub(crate) contacts: Vec<String>,
    pub(crate) challenge_type: ChallengeType,
    pub(crate) cache_path: Option<PathBuf>,
    pub(crate) keys_for_http01: Option<Arc<RwLock<HashMap<String, String>>>>,
    pub(crate) before_expired: Duration,
    pub(crate) key_type: KeyType,
    pub(crate) issuers: Option<Vec<Arc<dyn CertIssuer>>>,
    pub(crate) storage: Option<Arc<dyn Storage>>,
    pub(crate) http01_solver: Option<Arc<dyn Solver>>,
    pub(crate) tls_alpn01_solver: Option<Arc<dyn Solver>>,
    pub(crate) dns01_solver: Option<Arc<dyn Solver>>,
    pub(crate) ocsp: OcspConfig,
    pub(crate) on_demand: Option<Arc<OnDemandConfig>>,
    pub(crate) zerossl_api_key: Option<String>,
    pub(crate) agree_to_tos: bool,
}

impl fmt::Debug for AcmeConfigBuilder {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("AcmeConfigBuilder")
            .field("directory_name", &self.directory_name)
            .field("directory_url", &self.directory_url)
            .field("domains", &self.domains)
            .field("contacts", &self.contacts)
            .field("challenge_type", &self.challenge_type)
            .field("cache_path", &self.cache_path)
            .field("key_type", &self.key_type)
            .finish()
    }
}

impl AcmeConfigBuilder {
    #[inline]
    #[must_use]
    pub(crate) fn new() -> Self {
        Self {
            directory_name: "lets_encrypt".to_owned(),
            directory_url: LETS_ENCRYPT_PRODUCTION.to_owned(),
            domains: Vec::new(),
            contacts: Default::default(),
            challenge_type: ChallengeType::TlsAlpn01,
            cache_path: None,
            keys_for_http01: None,
            before_expired: Duration::from_secs(12 * 60 * 60),
            key_type: KeyType::EcdsaP256,
            issuers: None,
            storage: None,
            http01_solver: None,
            tls_alpn01_solver: None,
            dns01_solver: None,
            ocsp: OcspConfig::default(),
            on_demand: None,
            zerossl_api_key: None,
            agree_to_tos: true,
        }
    }

    /// Sets the directory url.
    ///
    /// Defaults to Let's Encrypt production.
    #[inline]
    #[must_use]
    pub fn directory(self, name: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            directory_name: name.into(),
            directory_url: url.into(),
            ..self
        }
    }

    /// Sets domains.
    #[inline]
    #[must_use]
    pub fn domains(mut self, domains: impl Into<Vec<String>>) -> Self {
        self.domains = domains.into();
        self
    }
    /// Add a domain.
    #[inline]
    #[must_use]
    pub fn add_domain(mut self, domain: impl Into<String>) -> Self {
        self.domains.push(domain.into());
        self
    }

    /// Sets contact emails for the ACME account.
    #[inline]
    #[must_use]
    pub fn contacts(mut self, contacts: impl Into<Vec<String>>) -> Self {
        self.contacts = contacts.into();
        self
    }
    /// Add a contact email for the ACME account.
    #[inline]
    #[must_use]
    pub fn add_contact(mut self, contact: impl Into<String>) -> Self {
        self.contacts.push(contact.into());
        self
    }

    /// Sets the challenge type to HTTP-01.
    #[inline]
    #[must_use]
    pub fn http01_challenge(self) -> Self {
        Self {
            challenge_type: ChallengeType::Http01,
            keys_for_http01: Some(Default::default()),
            ..self
        }
    }

    /// Sets the challenge type to TLS-ALPN-01.
    #[inline]
    #[must_use]
    pub fn tls_alpn01_challenge(self) -> Self {
        Self {
            challenge_type: ChallengeType::TlsAlpn01,
            keys_for_http01: None,
            ..self
        }
    }

    /// Sets the challenge type to DNS-01 with a custom DNS provider.
    #[inline]
    #[must_use]
    pub fn dns01_challenge(mut self, solver: Arc<dyn Solver>) -> Self {
        self.challenge_type = ChallengeType::Dns01;
        self.dns01_solver = Some(solver);
        self.keys_for_http01 = None;
        self
    }

    /// Sets the cache path for caching certificates.
    ///
    /// This is not a necessary option. If you do not configure the cache path,
    /// the obtained certificate will be stored in memory and will need to be
    /// obtained again when the server is restarted next time.
    #[inline]
    #[must_use]
    pub fn cache_path(self, path: impl Into<PathBuf>) -> Self {
        Self {
            cache_path: Some(path.into()),
            ..self
        }
    }

    /// Sets the duration before expiry to start certificate renewal.
    #[inline]
    #[must_use]
    pub fn before_expired(self, before_expired: Duration) -> Self {
        Self {
            before_expired,
            ..self
        }
    }

    // ----- New certon-powered options -----

    /// Sets the key type for certificate private keys.
    ///
    /// Defaults to [`KeyType::EcdsaP256`].
    /// Available types: `EcdsaP256`, `EcdsaP384`, `EcdsaP521`, `Rsa2048`,
    /// `Rsa4096`, `Rsa8192`, `Ed25519`.
    #[inline]
    #[must_use]
    pub fn key_type(mut self, key_type: KeyType) -> Self {
        self.key_type = key_type;
        self
    }

    /// Adds a custom certificate issuer.
    ///
    /// Multiple issuers can be added. They will be tried in order until one
    /// succeeds.
    #[must_use]
    pub fn add_issuer(mut self, issuer: Arc<dyn CertIssuer>) -> Self {
        self.issuers.get_or_insert_with(Vec::new).push(issuer);
        self
    }

    /// Sets a custom persistent storage backend.
    ///
    /// By default, a [`FileStorage`] will be created from the `cache_path`
    /// if provided.
    #[inline]
    #[must_use]
    pub fn storage(mut self, storage: Arc<dyn Storage>) -> Self {
        self.storage = Some(storage);
        self
    }

    /// Sets the OCSP stapling configuration.
    #[inline]
    #[must_use]
    pub fn ocsp(mut self, ocsp: OcspConfig) -> Self {
        self.ocsp = ocsp;
        self
    }

    /// Sets the on-demand TLS configuration.
    ///
    /// When enabled, certificates for unknown domains can be obtained at
    /// TLS handshake time.
    #[inline]
    #[must_use]
    pub fn on_demand(mut self, on_demand: Arc<OnDemandConfig>) -> Self {
        self.on_demand = Some(on_demand);
        self
    }

    /// Configures ZeroSSL as an additional issuer via its API key.
    ///
    /// This automatically adds a [`ZeroSslIssuer`](certon::ZeroSslIssuer)
    /// to the issuer list.
    #[inline]
    #[must_use]
    pub fn zerossl_api_key(mut self, api_key: impl Into<String>) -> Self {
        self.zerossl_api_key = Some(api_key.into());
        self
    }

    /// Sets a custom HTTP-01 solver (overrides the default).
    #[inline]
    #[must_use]
    pub fn http01_solver(mut self, solver: Arc<dyn Solver>) -> Self {
        self.http01_solver = Some(solver);
        self
    }

    /// Sets a custom TLS-ALPN-01 solver (overrides the default).
    #[inline]
    #[must_use]
    pub fn tls_alpn01_solver(mut self, solver: Arc<dyn Solver>) -> Self {
        self.tls_alpn01_solver = Some(solver);
        self
    }

    /// Whether to automatically agree to the CA's terms of service.
    /// Defaults to `true`.
    #[inline]
    #[must_use]
    pub fn agree_to_tos(mut self, agree: bool) -> Self {
        self.agree_to_tos = agree;
        self
    }

    /// Consumes this builder and returns a [`AcmeConfig`] object.
    pub fn build(self) -> IoResult<AcmeConfig> {
        if self.domains.is_empty() {
            return Err(IoError::other("at least one domain name is expected"));
        }
        let Self {
            directory_name,
            directory_url,
            domains,
            contacts,
            challenge_type,
            cache_path,
            keys_for_http01,
            before_expired,
            key_type,
            issuers,
            storage,
            http01_solver,
            tls_alpn01_solver,
            dns01_solver,
            ocsp,
            on_demand,
            zerossl_api_key,
            agree_to_tos,
        } = self;

        Ok(AcmeConfig {
            directory_name,
            directory_url,
            domains,
            contacts,
            challenge_type,
            cache_path,
            keys_for_http01,
            before_expired,
            key_type,
            issuers,
            storage,
            http01_solver,
            tls_alpn01_solver,
            dns01_solver,
            ocsp,
            on_demand,
            zerossl_api_key,
            agree_to_tos,
        })
    }
}

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

    #[test]
    fn test_acme_config_builder_new_defaults() {
        let builder = AcmeConfigBuilder::new();
        assert_eq!(builder.directory_name, "lets_encrypt");
        assert_eq!(builder.directory_url, LETS_ENCRYPT_PRODUCTION);
        assert!(builder.domains.is_empty());
        assert!(builder.contacts.is_empty());
        assert_eq!(builder.challenge_type, ChallengeType::TlsAlpn01);
        assert!(builder.cache_path.is_none());
        assert!(builder.keys_for_http01.is_none());
        assert_eq!(builder.before_expired, Duration::from_secs(12 * 60 * 60));
    }

    #[test]
    fn test_acme_config_builder() {
        let domains = vec!["example.com".to_owned(), "example.org".to_owned()];
        let contacts = vec!["mailto:admin@example.com".to_owned()];

        let acme_config = AcmeConfig::builder()
            .directory("test_directory", "https://test-directory-url.com")
            .domains(domains.clone())
            .contacts(contacts.clone())
            .http01_challenge()
            .cache_path("test_cache_path")
            .before_expired(Duration::from_secs(24 * 60 * 60))
            .build()
            .unwrap();

        assert_eq!(acme_config.directory_name, "test_directory");
        assert_eq!(acme_config.directory_url, "https://test-directory-url.com");
        assert_eq!(acme_config.domains, domains);
        assert_eq!(acme_config.contacts, contacts);
        assert_eq!(acme_config.challenge_type, ChallengeType::Http01);
        assert_eq!(
            acme_config.cache_path,
            Some(PathBuf::from("test_cache_path"))
        );
        assert_eq!(
            acme_config.before_expired,
            Duration::from_secs(24 * 60 * 60)
        );
    }

    #[test]
    fn test_acme_config_builder_add_domain() {
        let config = AcmeConfig::builder()
            .add_domain("example.com")
            .add_domain("www.example.com")
            .build()
            .unwrap();

        assert_eq!(config.domains.len(), 2);
        assert_eq!(config.domains[0], "example.com");
        assert_eq!(config.domains[1], "www.example.com");
    }

    #[test]
    fn test_acme_config_builder_add_contact() {
        let config = AcmeConfig::builder()
            .add_domain("example.com")
            .add_contact("mailto:admin@example.com")
            .add_contact("mailto:webmaster@example.com")
            .build()
            .unwrap();

        assert_eq!(config.contacts.len(), 2);
    }

    #[test]
    fn test_acme_config_builder_no_domains_error() {
        let result = AcmeConfig::builder().build();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("at least one domain"));
    }

    #[test]
    fn test_acme_config_builder_key_type() {
        let config = AcmeConfig::builder()
            .add_domain("example.com")
            .key_type(KeyType::Rsa4096)
            .build()
            .unwrap();

        assert!(matches!(config.key_type, KeyType::Rsa4096));
    }

    #[test]
    fn test_acme_config_builder_dns01() {
        // DNS-01 requires a solver; just test the challenge type is set.
        let builder = AcmeConfigBuilder::new();
        assert_eq!(builder.challenge_type, ChallengeType::TlsAlpn01);
    }

    #[test]
    fn test_acme_config_debug() {
        let config = AcmeConfig::builder()
            .add_domain("example.com")
            .build()
            .unwrap();

        let debug_str = format!("{config:?}");
        assert!(debug_str.contains("AcmeConfig"));
        assert!(debug_str.contains("directory_name"));
    }
}