yacme 5.0.1

Yet another ACME client.
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
//! # Utilities for testing against the pebble ACME server
//!

use std::{
    collections::BTreeMap,
    io::{Read, Seek, Write},
    net::Ipv4Addr,
    path::{Path, PathBuf},
};

use fd_lock::{RwLock, RwLockWriteGuard};
use serde::Serialize;

/// Parse an HTTP response formatted like an RFC 8555 example.
///
/// The response body is not parsed, and instead passed literally in the
/// returned response object.
pub fn parse_http_response_example(data: &str) -> http::Response<String> {
    let mut lines = data.lines();

    let status = {
        let status_line = lines.next().unwrap().trim();
        let (version, status) = status_line.split_once(' ').unwrap();

        if version != "HTTP/1.1" {
            panic!("Expected HTTP/1.1, got {version}");
        }

        let (code, _reason) = status.split_once(' ').unwrap();
        http::StatusCode::from_u16(code.parse().expect("status code is u16"))
            .expect("known status code")
    };

    let mut headers = http::HeaderMap::new();

    for line in lines.by_ref() {
        if line.is_empty() {
            break;
        } else {
            let (name, value) = line
                .trim()
                .split_once(": ")
                .expect("Header delimiter is ':'");
            headers.append(
                http::header::HeaderName::from_bytes(name.as_bytes()).unwrap(),
                value.parse().expect("valid header value"),
            );
        }
    }

    let body: String = lines.collect();
    let mut response = http::Response::new(body);
    *response.headers_mut() = headers;
    *response.status_mut() = status;
    *response.version_mut() = http::Version::HTTP_11;
    response
}

const PEBBLE_DIRECTORY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/pebble/");

#[derive(Debug)]
struct PidRc {
    lock: RwLock<std::fs::File>,
    counts: BTreeMap<u32, usize>,
}

struct LockedPidRc<'l> {
    guard: RwLockWriteGuard<'l, std::fs::File>,
    counts: &'l mut BTreeMap<u32, usize>,
}

impl PidRc {
    fn new<P: AsRef<Path>>(path: &P) -> Self {
        let lock = RwLock::new(
            std::fs::File::options()
                .read(true)
                .append(true)
                .create(true)
                .open(path.as_ref())
                .unwrap(),
        );

        Self {
            lock,
            counts: Default::default(),
        }
    }

    fn lock(&mut self) -> LockedPidRc<'_> {
        let guard = self.lock.write().unwrap();
        LockedPidRc {
            guard,
            counts: &mut self.counts,
        }
    }
}

impl LockedPidRc<'_> {
    fn write(&mut self) {
        self.guard.set_len(0).expect("truncate lock file");

        for (pid, c) in self.counts.iter() {
            if *c > 0 {
                for _ in 0..*c {
                    writeln!(self.guard, "{pid}").unwrap();
                }
            }
        }
    }

    fn read(&mut self) {
        self.guard.rewind().expect("rewind to start of lockfile");
        let mut buf = String::new();
        let _ = self.guard.read_to_string(&mut buf);

        self.counts.clear();
        for pid in buf.lines() {
            let count = self.counts.entry(pid.parse().unwrap()).or_insert(0);
            *count += 1;
        }
    }

    fn increment(&mut self) {
        self.read();

        let pid = std::process::id();
        let count = self.counts.entry(pid).or_insert(0);
        *count += 1;

        tracing::debug!(%pid, "increment: {}", count);

        self.write();
    }

    fn decrement(&mut self) -> bool {
        self.read();

        let pid = std::process::id();
        match self.counts.entry(pid) {
            std::collections::btree_map::Entry::Vacant(_) => {
                panic!("pid not found in lock file when decrementing");
            }
            std::collections::btree_map::Entry::Occupied(mut value) => {
                assert_ne!(value.get(), &0, "pid count is 0 when decrementing");
                tracing::debug!(%pid, "decrement: {}", value.get());

                let new_value = value.get().saturating_sub(1);
                if new_value > 0 {
                    value.insert(new_value);
                } else {
                    value.remove();
                }
            }
        }

        self.write();
        !self.counts.is_empty()
    }

    fn clear(&mut self) {
        self.counts.clear();
        self.write();
    }

    fn reset(&mut self) {
        self.counts.clear();
        self.counts.insert(std::process::id(), 1);
        self.write();
    }

    fn is_empty(&mut self) -> bool {
        self.read();
        self.counts.is_empty()
    }
}

/// RAII wrapper around a pebble service.
///
/// The pebble service will be started and managed via docker compose.
#[derive(Debug)]
pub struct Pebble {
    directory: PathBuf,
    lock: PidRc,
}

impl Pebble {
    #[allow(clippy::new_without_default)]
    /// Create a new pebble manager instance.
    ///
    /// This effectively acts as a signleton, in that only one pebble
    /// docker container will be started at any given time, but creating
    /// multiple `Pebble` instances will all refer to the same container.
    pub fn new() -> Self {
        let directory: PathBuf = PEBBLE_DIRECTORY.into();
        let lock = PidRc::new(&directory.join("lock"));
        let mut pebble = Self { directory, lock };

        pebble.start();

        pebble
    }

    fn start(&mut self) {
        let mut guard = self.lock.lock();

        if !guard.is_empty() {
            let output = std::process::Command::new("docker")
                .arg("compose")
                .args(["ps"])
                .current_dir(&self.directory)
                .output()
                .expect("able to spawn docker compose command");

            // must get more than 2 lines of output.
            if output.status.success()
                && output
                    .stdout
                    .iter()
                    .map(|&b| (b == b'\n') as u32)
                    .sum::<u32>()
                    > 2
            {
                tracing::debug!("Pebble server already running");
                guard.increment();
                return;
            }
        }

        tracing::debug!("Starting pebble server");

        let output = std::process::Command::new("docker")
            .arg("compose")
            .args([
                "up",
                "--detach",
                "--remove-orphans",
                "--renew-anon-volumes",
                "--wait",
            ])
            .current_dir(&self.directory)
            .output()
            .expect("able to spawn docker compose command");

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            guard.clear(); // nothing is running, so clear the lock file
            panic!("Failed to start a pebble server: {stderr}");
        } else {
            tracing::debug!("Pebble server started");
            guard.reset();
        }
    }

    /// Wait for the pebble server to be ready.
    pub async fn ready(&self) -> Result<(), reqwest::Error> {
        let client = reqwest::Client::builder()
            .add_root_certificate(self.certificate())
            .build()
            .unwrap();

        loop {
            match client.get("https://localhost:14000/dir").send().await {
                Ok(resp) => {
                    if resp.status().is_success() {
                        break;
                    }
                    tracing::trace!(
                        "Error response from pebble:\n{}",
                        resp.text().await.unwrap_or("".to_owned())
                    );
                    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
                }
                Err(error) => {
                    tracing::trace!("Error connecting to pebble: {}", error);
                }
            };
        }

        Ok(())
    }

    /// Get the pebble root CA certificate.
    pub fn certificate(&self) -> reqwest::Certificate {
        let cert = self.directory.join("pebble.minica.pem");

        reqwest::Certificate::from_pem(&std::fs::read(cert).expect("read pebble root CA"))
            .expect("valid pebble root CA")
    }

    /// Set a DNS A record for a given host on the pebble challenge responder.
    #[tracing::instrument(skip(self, addresses))]
    pub async fn dns_a(&self, host: &str, addresses: &[Ipv4Addr]) {
        #[derive(Debug, Serialize)]
        struct PebbleDNSRecord {
            host: String,
            addresses: Vec<String>,
        }

        let chall_setup = PebbleDNSRecord {
            host: host.to_owned(),
            addresses: addresses.iter().map(|ip| ip.to_string()).collect(),
        };

        tracing::trace!(
            "Challenge Setup:\n{}",
            serde_json::to_string(&chall_setup).unwrap()
        );

        let resp = reqwest::Client::new()
            .post("http://localhost:8055/add-a")
            .json(&chall_setup)
            .send()
            .await
            .expect("connect to pebble");
        match resp.error_for_status_ref() {
            Ok(_) => {}
            Err(_) => {
                eprint_response_error(&chall_setup, resp).await;
                panic!("Failed to update challenge server");
            }
        }
    }

    /// Set a DNS01 TXT record for a given host on the pebble challenge responder.
    #[tracing::instrument(skip(self, value))]
    pub async fn dns01(&self, host: &str, value: &str) {
        #[derive(Debug, Serialize)]
        struct Dns01TXT {
            host: String,
            value: String,
        }

        let chall_setup = Dns01TXT {
            host: host.to_owned(),
            value: value.to_owned(),
        };

        tracing::trace!(
            "Challenge Setup:\n{}",
            serde_json::to_string(&chall_setup).unwrap()
        );

        let resp = reqwest::Client::new()
            .post("http://localhost:8055/set-txt")
            .json(&chall_setup)
            .send()
            .await
            .expect("connect to pebble");
        match resp.error_for_status_ref() {
            Ok(_) => {}
            Err(_) => {
                eprint_response_error(&chall_setup, resp).await;
                panic!("Failed to update challenge server");
            }
        }
    }

    /// Configure the pebble challenge responder to serve a HTTP01 challenge.
    #[tracing::instrument(skip_all)]
    pub async fn http01(&self, token: &str, content: &str) {
        #[derive(Debug, Serialize)]
        struct Http01ChallengeSetup {
            token: String,
            content: String,
        }

        let chall_setup = Http01ChallengeSetup {
            token: token.to_owned(),
            content: content.to_owned(),
        };

        tracing::trace!(
            "Challenge Setup:\n{}",
            serde_json::to_string(&chall_setup).unwrap()
        );

        let resp = reqwest::Client::new()
            .post("http://localhost:8055/add-http01")
            .json(&chall_setup)
            .send()
            .await
            .expect("connect to pebble");
        match resp.error_for_status_ref() {
            Ok(_) => {}
            Err(_) => {
                eprint_response_error(&chall_setup, resp).await;
                panic!("Failed to update challenge server");
            }
        }
    }

    fn down_internal(&mut self) {
        let mut guard = self.lock.lock();

        if guard.decrement() {
            return;
        }

        tracing::debug!("Stopping pebble server");
        let output = std::process::Command::new("docker")
            .arg("compose")
            .args(["down", "--remove-orphans", "--volumes", "--timeout", "10"])
            .current_dir(&self.directory)
            .output()
            .expect("able to spawn docker compose command");

        if !output.status.success() {
            panic!("Failed to stop a pebble server");
        }
        tracing::debug!("Pebble server stopped");

        guard.clear();
    }
}

impl Drop for Pebble {
    fn drop(&mut self) {
        self.down_internal();
    }
}

async fn eprint_response_error<R: Serialize>(request: &R, response: reqwest::Response) {
    eprintln!("Request:");
    eprintln!("{}", serde_json::to_string(&request).unwrap());
    eprintln!("ERROR:");
    eprintln!("Status: {}", response.status());
    eprintln!(
        "{}",
        response
            .text()
            .await
            .expect("get response body from pebble")
    );
}