proksi 0.10.0

A batteries-included reverse proxy with automatic HTTPS using Cloudflare Pingora and Let's Encrypt.
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
use std::{sync::Arc, time::Duration};

use acme_v2::{order::NewOrder, Account, DirectoryUrl};
use anyhow::anyhow;
use async_trait::async_trait;

use openssl::{pkey::PKey, x509::X509};
use pingora::{
    server::{ListenFds, ShutdownWatch},
    services::Service,
};
use tokio::time;
use tracing::info;

use crate::{
    config::Config,
    stores::{self, certificates::Certificate},
};

use super::{
    lease::{with_lease, Outcome, LEASE_TTL},
    storage::PersistType,
};

/// Default interval in days to attempt renewal of certificates
const DEFAULT_RENEW_INTERVAL_DAYS: i64 = 30;

/// Lease taken while creating (or reading) the Let's Encrypt account
const ACCOUNT_LEASE: &str = "acme:account";

/// A service that handles the creation of certificates using the Let's Encrypt API
pub struct LetsencryptService {
    pub(crate) config: Arc<Config>,
    // pub(crate) route_store: RouteStore,
    // pub(crate) cert_store: CertificateStore,
}

impl LetsencryptService {
    pub fn new(config: Arc<Config>) -> Self {
        Self { config }
    }

    /// Parse a PEM-encoded private key from a string slice
    fn parse_private_key(key_pem: &str) -> Result<PKey<openssl::pkey::Private>, anyhow::Error> {
        Ok(PKey::private_key_from_pem(key_pem.as_bytes())?)
    }

    /// Update global certificate store with new `X509` and `PKey` for the
    /// given domain also considering that the certificate could be a bundle file.
    async fn insert_certificate(
        domain: &str,
        bundle: &str,
        key_pem: &str,
    ) -> Result<(), anyhow::Error> {
        // The leaf, followed by its chain
        let mut certificates = X509::stack_from_pem(bundle.as_bytes())?.into_iter();
        let Some(leaf) = certificates.next() else {
            return Err(anyhow::anyhow!("Certificate is empty"));
        };
        let chain = certificates.collect();

        let key = Self::parse_private_key(key_pem)?;

        stores::global::get_store()
            .set_certificate(domain, Certificate { key, leaf, chain })
            .await
            .map_err(|o_err| anyhow!("failed to save certificate in the store: {o_err}"))?;

        Ok(())
    }

    /// Start an HTTP-01 challenge for a given order
    async fn handle_http_01_challenge(
        order: &mut NewOrder<PersistType>,
    ) -> Result<(), anyhow::Error> {
        for auth in order.authorizations()? {
            let challenge = auth.http_challenge();

            info!("HTTP-01 challenge for domain: {}", auth.domain_name());

            // Use the global store to set the challenge
            // This allows multiple replicas of Proksi to handle challenges
            if let Err(err) = stores::global::get_store()
                .set_challenge(
                    auth.domain_name(),
                    challenge.http_token().to_string(),
                    challenge.http_proof().clone(),
                )
                .await
            {
                tracing::error!("Failed to set challenge in store: {}", err);
                return Err(anyhow!("Failed to set challenge in store: {err}"));
            }

            // Let's Encrypt will check the domain's URL to validate the challenge
            tracing::info!("HTTP-01 validating (retry: 5s)...");
            challenge.validate(5000)?; // Retry every 5000 ms
        }
        Ok(())
    }

    /// Creates an in-memory self-signed certificate for a domain if let's encrypt
    /// cannot be used.
    /// Note this is only useful for local development or testing purposes
    /// and should be used sparingly
    async fn create_self_signed_certificate(
        domain: &str,
        enabled: bool,
    ) -> Result<(), anyhow::Error> {
        // Generate self-signed certificate only if self_signed_on_failure is set to true
        // If not provided, default to true
        if !enabled {
            // Nothing to do
            return Ok(());
        }

        tracing::info!("creating an in-memory self-signed certificate for {domain}");

        let rsa = openssl::rsa::Rsa::generate(2048)?;
        let mut openssl_cert = openssl::x509::X509Builder::new()?;
        let mut x509_name = openssl::x509::X509NameBuilder::new()?;

        x509_name.append_entry_by_text("CN", domain)?;
        x509_name.append_entry_by_text("ST", "TX")?;
        x509_name.append_entry_by_text("O", "Proksi")?;
        // x509_name.append_entry_by_text("CN", "Test")?;
        let x509_name = x509_name.build();

        let hash = pingora::tls::hash::MessageDigest::sha256();
        let key = pingora::tls::pkey::PKey::from_rsa(rsa)?;
        let one_year = openssl::asn1::Asn1Time::days_from_now(365)?;
        let today = openssl::asn1::Asn1Time::days_from_now(0)?;
        openssl_cert.set_version(2)?;
        openssl_cert.set_subject_name(&x509_name)?;
        openssl_cert.set_issuer_name(&x509_name)?;
        openssl_cert.set_pubkey(&key)?;
        openssl_cert.set_not_before(&today)?;
        openssl_cert.set_not_after(&one_year)?;
        openssl_cert.sign(&key, hash)?;

        let openssl_cert = openssl_cert.build();

        stores::global::get_store()
            .set_certificate(
                domain,
                Certificate {
                    key,
                    leaf: openssl_cert,
                    chain: vec![],
                },
            )
            .await
            .map_err(|o_err| anyhow!("failed to save self-signed certificate {o_err}"))?;

        Ok(())
    }

    // Creates or retrieves the existing LetsEncrypt account
    fn get_account(&self) -> Result<Account<PersistType>, anyhow::Error> {
        let persist =
            crate::services::letsencrypt::storage::CertificatePersist::new(self.config.clone());

        let dir = acme_v2::Directory::from_url(persist.get_persist()?, self.get_lets_encrypt_url())
            .map_err(|err| anyhow!("failed to create LetsEncrypt directory: {err}"))?;

        dir.account(&self.config.lets_encrypt.email)
            .map_err(|err| anyhow!("failed to create or retrieve existing account: {err}"))
    }

    /// Creates or retrieves the account, holding a lease so replicas starting together don't
    /// each create one. If another replica holds it, waits for it (up to a minute) and then
    /// reads the account it created.
    async fn get_account_with_lease(&self) -> Result<Account<PersistType>, anyhow::Error> {
        let store = stores::global::get_store();
        let holder = stores::replica_id();
        for _ in 0..12 {
            match store.try_lock(ACCOUNT_LEASE, holder, LEASE_TTL).await {
                Ok(false) => time::sleep(Duration::from_secs(5)).await,
                // Taken, or the store is unavailable (as without a lease)
                Ok(true) | Err(_) => break,
            }
        }

        let account = self.get_account();
        if let Err(err) = store.unlock(ACCOUNT_LEASE, holder).await {
            tracing::warn!("failed to release the lease {ACCOUNT_LEASE}: {err}");
        }
        account
    }

    // Based on the letsencrypt configuration, return the appropriate URL
    fn get_lets_encrypt_url(&self) -> DirectoryUrl<'_> {
        match self.config.lets_encrypt.staging {
            Some(false) => DirectoryUrl::LetsEncrypt,
            _ => DirectoryUrl::LetsEncryptStaging,
        }
    }

    /// Create a new order for a domain (HTTP-01 challenge)
    async fn create_order_for_domain(
        domain: &str,
        account: &Account<PersistType>,
    ) -> Result<(), anyhow::Error> {
        let mut order = account.new_order(domain, &[])?;

        let order_csr = loop {
            // Break if we are done confirming validations
            if let Some(csr) = order.confirm_validations() {
                break csr;
            }

            // Get the possible authorizations (for a single domain
            // this will only be one element).
            Self::handle_http_01_challenge(&mut order)
                .await
                .map_err(|err| anyhow!("Failed to handle HTTP-01 challenge: {err}"))?;

            order.refresh().unwrap_or_default();
        };

        // Order OK
        let pkey = acme_v2::create_p384_key();
        let order_cert = order_csr.finalize_pkey(pkey, 5000)?;

        info!("certificate created for order {:?}", order_cert.api_order());

        let cert = order_cert.download_and_save_cert()?;

        Self::insert_certificate(domain, cert.certificate(), cert.private_key()).await?;

        Ok(())
    }

    /// Watch for route changes and create or update certificates for new routes
    async fn watch_for_route_changes(&self, account: &Account<PersistType>) {
        let mut interval = time::interval(Duration::from_secs(20));

        loop {
            interval.tick().await;
            tracing::debug!("checking for new routes to create certificates for");
            let store = stores::global::get_store();
            let existing = store
                .get_certificates()
                .await
                .keys()
                .cloned()
                .collect::<std::collections::HashSet<_>>();

            // Hosts without TLS (and `*`) don't get certificates
            for (host, self_signed_on_failure) in stores::lookup::tls_hosts() {
                if existing.contains(&host) {
                    continue;
                }

                // Another replica may have just created it
                let outcome = with_lease(
                    store.as_ref(),
                    stores::replica_id(),
                    &format!("acme:{host}"),
                    LEASE_TTL,
                    async { store.get_certificate(&host).await.is_none() },
                    Self::handle_certificate_for_domain(&host, account, self_signed_on_failure),
                )
                .await;
                log_outcome(&host, "creating", &outcome);
            }
        }
    }

    /// Check for certificates expiration and renew them if needed
    async fn check_for_certificates_expiration(&self, account: &Account<PersistType>) {
        let mut interval = time::interval(Duration::from_secs(
            self.config
                .lets_encrypt
                .renew_interval_secs
                .unwrap_or(84_600),
        ));

        loop {
            tracing::debug!("checking for certificates to renew");
            for (domain, _) in stores::lookup::tls_hosts() {
                let domain = domain.as_str();
                let Ok(Some(cert)) = account.certificate(domain) else {
                    continue;
                };

                let valid_days_left = cert.valid_days_left();
                tracing::info!("certificate for domain {domain} expires in {valid_days_left} days",);

                // Nothing to do before the renewal interval
                if valid_days_left > DEFAULT_RENEW_INTERVAL_DAYS {
                    continue;
                }

                // Another replica may have just renewed it
                let still_expiring = async {
                    account
                        .certificate(domain)
                        .ok()
                        .flatten()
                        .is_some_and(|cert| cert.valid_days_left() <= DEFAULT_RENEW_INTERVAL_DAYS)
                };
                let outcome = with_lease(
                    stores::global::get_store().as_ref(),
                    stores::replica_id(),
                    &format!("acme:{domain}"),
                    LEASE_TTL,
                    still_expiring,
                    Self::create_order_for_domain(domain, account),
                )
                .await;
                log_outcome(domain, "renewing", &outcome);
            }

            interval.tick().await;
        }
    }

    /// Saves the domain's certificate in the store: the one Let's Encrypt already issued, a
    /// new one, or a self-signed one if ordering fails (and `self_signed_on_failure`)
    async fn handle_certificate_for_domain(
        domain: &str,
        account: &Account<PersistType>,
        self_signed_on_failure: bool,
    ) -> Result<(), anyhow::Error> {
        if let Some(cert) = account.certificate(domain)? {
            return Self::insert_certificate(domain, cert.certificate(), cert.private_key()).await;
        }

        let ordered = Self::create_order_for_domain(domain, account).await;
        if ordered.is_err() {
            Self::create_self_signed_certificate(domain, self_signed_on_failure).await?;
        }
        ordered
    }
}

/// Logs what happened to a domain's certificate
fn log_outcome(domain: &str, action: &str, outcome: &Outcome) {
    match outcome {
        Outcome::Done => info!("{action} certificate for {domain}: done"),
        Outcome::Held => tracing::debug!(
            "{action} certificate for {domain}: skipped, another replica is ordering it"
        ),
        Outcome::NotNeeded => {
            tracing::debug!("{action} certificate for {domain}: another replica just did it");
        }
        Outcome::Failed(err) => {
            tracing::error!("{action} certificate for {domain} failed: {err}");
        }
        Outcome::Unavailable(err) => tracing::warn!(
            "{action} certificate for {domain}: the store is unavailable, retrying later: {err}"
        ),
    }
}

#[async_trait]
impl Service for LetsencryptService {
    async fn start_service(
        &mut self,
        _fds: Option<ListenFds>,
        mut _shutdown: ShutdownWatch,
        _listeners_per_fd: usize,
    ) {
        if self.config.lets_encrypt.enabled.is_some_and(|v| !v) {
            // Nothing to do, lets encrypt is disabled
            return;
        }
        info!("started LetsEncrypt service");

        // Retry until the account is available (e.g. network not ready yet)
        let account = loop {
            match self.get_account_with_lease().await {
                Ok(account) => break account,
                Err(err) => {
                    tracing::error!("failed to setup LetsEncrypt, retrying in 60s: {err}");
                    time::sleep(Duration::from_secs(60)).await;
                }
            }
        };

        let _ = tokio::join!(
            self.watch_for_route_changes(&account),
            self.check_for_certificates_expiration(&account)
        );
    }

    fn name(&self) -> &'static str {
        "lets_encrypt_service"
    }

    fn threads(&self) -> Option<usize> {
        Some(1)
    }
}

#[cfg(test)]
mod tests {
    use crate::stores::certificates::tests::signed;

    #[tokio::test]
    async fn test_ordered_certificates_keep_their_whole_chain() {
        let first = signed("intermediate 1", None);
        let second = signed("intermediate 2", Some(&first));
        let (leaf, key) = signed("acme-chain.test", Some(&second));
        let bundle = [&leaf, &second.0, &first.0]
            .map(|c| String::from_utf8(c.to_pem().unwrap()).unwrap())
            .join("\n");
        let key = String::from_utf8(key.private_key_to_pem_pkcs8().unwrap()).unwrap();

        super::LetsencryptService::insert_certificate("acme-chain.test", &bundle, &key)
            .await
            .unwrap();

        let stored = crate::stores::global::get_store()
            .get_certificate("acme-chain.test")
            .await
            .unwrap();
        assert_eq!(stored.leaf.to_pem().unwrap(), leaf.to_pem().unwrap());
        assert_eq!(
            stored
                .chain
                .iter()
                .map(|c| c.to_pem().unwrap())
                .collect::<Vec<_>>(),
            vec![second.0.to_pem().unwrap(), first.0.to_pem().unwrap()]
        );
    }
}