indicatrix-worker 0.3.3

Library server for a indicatrix-backed design catalogue (`serve`, default build): serves indicatrix-net's read-only design-library protocol over mutual TLS. The optional `worker` feature adds render capacity on top -- accepting RenderRequests and replying with traced radiance -- and `render` a one-shot scene-to-PNG CLI.
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
//! `cert init` / `cert issue-server` / `cert issue-client`: an in-process private CA
//! for `indicatrix-worker`'s mutual TLS.
//!
//! Certificate GENERATION only -- one-shot CLI operations that produce PEM files on
//! disk. Private-key writing (including Windows-ACL permissioning) and loading an
//! issued bundle back into `rustls` configs both live in [`indicatrix_net::tls`]
//! instead, since `apps/indicatrix-cut`'s token-redeem UI needs the same
//! restricted-permission write and both apps already depend on that crate.
//!
//! # Why a private CA rather than a public one
//!
//! The worker is addressed by IP on a LAN or private host, which no public CA will
//! issue for, and there is exactly one operator on both ends of the trust
//! relationship -- no third party's vouching is worth anything here.
//!
//! # Layout
//!
//! ```text
//! <pki-dir>/
//!   ca.pem          CA certificate (public -- ships to every worker and every viewer)
//!   ca.key          CA private key (sensitive -- ACL-restricted, never leaves this dir)
//!   server.pem       this worker's certificate (public)
//!   server.key       this worker's private key (sensitive -- ACL-restricted)
//!   allowlist.txt     trusted client-certificate fingerprints -- see `indicatrix_net::tls`
//!
//! <bundle-dir>/        (from `issue-client --out`, copied to the viewer machine)
//!   ca.pem
//!   client.pem
//!   client.key        (sensitive -- ACL-restricted)
//! ```
//!
//! `issue-server` and `issue-client` both re-derive the CA's signing identity from the
//! saved `ca.pem`/`ca.key` in `<pki-dir>` (via [`rcgen::Issuer::from_ca_cert_pem`]),
//! since each subcommand is its own process invocation with nothing else in memory.
//!
//! # Certificate lifetimes and why they're long
//!
//! CA: 10 years. Server/client leaf certs: 5 years. There is no CRL or OCSP --
//! revocation is the fingerprint [`Allowlist`](indicatrix_net::tls::Allowlist), edited
//! by hand -- so expiry is a backstop, not the primary revocation path. Long lifetimes
//! trade a weaker backstop for not re-enrolling every LAN machine every few months.
//!
//! Every `not_before` is backdated by [`NOT_BEFORE_SLACK_DAYS`] from issuance, so a
//! worker or viewer whose clock is a little behind doesn't reject a freshly issued
//! certificate as not-yet-valid.
//!
//! # Errors
//!
//! Every fallible function in this module returns [`PkiError`] rather than a bare
//! `String`: callers that only ever print the failure (`main.rs`) see the exact same
//! text as before via [`PkiError`]'s [`Display`](std::fmt::Display) impl, while a caller
//! that wants to distinguish failure kinds (rather than pattern-matching substrings out
//! of a message) can match on the enum instead -- `crate::enroll::registry::EnrollIssueError`
//! does exactly that, wrapping [`PkiError`] rather than flattening it to a `String`.

use indicatrix_net::tls;
use rcgen::{
    BasicConstraints, CertificateParams, DnType, ExtendedKeyUsagePurpose, IsCa, Issuer, KeyPair,
    KeyUsagePurpose,
};
use std::{
    fmt,
    net::IpAddr,
    path::{Path, PathBuf},
};
use time::{Duration, OffsetDateTime};

pub const CA_CERT_FILE: &str = "ca.pem";
pub const CA_KEY_FILE: &str = "ca.key";
pub const SERVER_CERT_FILE: &str = "server.pem";
pub const SERVER_KEY_FILE: &str = "server.key";
pub const CLIENT_CERT_FILE: &str = "client.pem";
pub const CLIENT_KEY_FILE: &str = "client.key";
/// Lives alongside the CA (`<pki-dir>/allowlist.txt`), never in a viewer bundle.
pub const ALLOWLIST_FILE: &str = "allowlist.txt";

const CA_LIFETIME_DAYS: i64 = 365 * 10;
const LEAF_LIFETIME_DAYS: i64 = 365 * 5;
/// How far back to backdate every certificate's `not_before`, to absorb clock skew
/// between the issuing machine and whichever machine checks validity later.
const NOT_BEFORE_SLACK_DAYS: i64 = 1;

/// Everything that can go wrong generating or loading this module's certificates/keys.
///
/// Each variant carries what's needed to reproduce the exact message this module always
/// printed (see the `# Errors` sections on the functions below, and this module's own
/// doc comment on why the message text is what's preserved, not a particular variant
/// shape).
#[derive(Debug)]
pub enum PkiError {
    /// A plain filesystem operation on `path` failed. `verb` names what was attempted
    /// ("create" / "write" / "read"), reproducing this module's long-standing "could
    /// not {verb} {path}: {source}" phrasing.
    Io {
        verb: &'static str,
        path: PathBuf,
        source: std::io::Error,
    },
    /// [`init`] refuses to overwrite an existing CA at `dir`.
    CaAlreadyExists { dir: PathBuf },
    /// [`load_ca`] couldn't read `<dir>/ca.pem` or `<dir>/ca.key` back from disk --
    /// distinct from [`Self::Io`] because the message also points at `cert init`.
    CaFileUnreadable {
        path: PathBuf,
        dir: PathBuf,
        source: std::io::Error,
    },
    /// [`issue_server`] was given neither `--host` nor `--ip`.
    NoServerSan,
    /// [`sign_client_leaf`] was given an empty (or all-whitespace) `--name`.
    EmptyClientName,
    /// An [`rcgen`] operation failed (key generation, building a SAN list,
    /// self-signing, or leaf-signing). `context` names what was being built, matching
    /// this module's existing "<what> failed: <e>" messages.
    Rcgen {
        context: &'static str,
        source: rcgen::Error,
    },
    /// [`load_ca`] found `ca.key`/`ca.pem` on disk but [`rcgen`] couldn't parse it back
    /// into a signing identity.
    RcgenAtPath { path: PathBuf, source: rcgen::Error },
    /// A message already fully formatted by a helper outside this module -- currently
    /// only [`indicatrix_net::tls::write_private_key_pem`], which returns `String`
    /// because its own failure modes (directory creation, an external `icacls`
    /// process) aren't PKI-specific either. Printed verbatim, no extra wrapping.
    Message(String),
    /// [`indicatrix_net::tls::load_certs`] couldn't parse the certificate at `path`.
    Certificate {
        path: PathBuf,
        source: tls::TlsError,
    },
    /// [`issue_client`] issued the certificate and wrote the bundle to `out_dir`, but
    /// couldn't append its fingerprint to the allowlist at `path` -- the caller is
    /// told the exact line to add by hand.
    AllowlistUpdate {
        out_dir: PathBuf,
        path: PathBuf,
        fingerprint_hex: String,
        name: String,
        source: tls::TlsError,
    },
}

impl fmt::Display for PkiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io { verb, path, source } => {
                write!(f, "could not {verb} {}: {source}", path.display())
            }
            Self::CaAlreadyExists { dir } => write!(
                f,
                "{} already contains a CA ({CA_CERT_FILE} and/or {CA_KEY_FILE}) -- refusing to overwrite it, since \
                 that would invalidate every certificate already issued from it. Remove those files yourself first \
                 if you really mean to start over.",
                dir.display()
            ),
            Self::CaFileUnreadable { path, dir, source } => write!(
                f,
                "could not read {}: {source} (run `indicatrix-worker cert init --dir {}` first)",
                path.display(),
                dir.display()
            ),
            Self::NoServerSan => write!(
                f,
                "issue-server requires at least one --host or --ip: rustls ignores Common Name entirely, so a \
                 certificate with no Subject Alternative Name can never be validated by a mutual-TLS client -- see \
                 --help"
            ),
            Self::EmptyClientName => write!(f, "--name must not be empty"),
            Self::Rcgen { context, source } => write!(f, "{context}: {source}"),
            Self::RcgenAtPath { path, source } => write!(f, "{}: {source}", path.display()),
            Self::Message(msg) => f.write_str(msg),
            Self::Certificate { path, source } => write!(f, "{}: {source}", path.display()),
            Self::AllowlistUpdate {
                out_dir,
                path,
                fingerprint_hex,
                name,
                source,
            } => write!(
                f,
                "issued the certificate at {} but could not update {}: {source} -- add this line to it by hand: \
                 {fingerprint_hex}  # {name}",
                out_dir.display(),
                path.display()
            ),
        }
    }
}

impl std::error::Error for PkiError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } | Self::CaFileUnreadable { source, .. } => Some(source),
            Self::Rcgen { source, .. } | Self::RcgenAtPath { source, .. } => Some(source),
            Self::Certificate { source, .. } | Self::AllowlistUpdate { source, .. } => Some(source),
            Self::CaAlreadyExists { .. }
            | Self::NoServerSan
            | Self::EmptyClientName
            | Self::Message(_) => None,
        }
    }
}

/// [`indicatrix_net::tls::write_private_key_pem`] returns `Result<(), String>`; this
/// lets every `tls::write_private_key_pem(..)?` call site in this module keep working
/// against this module's own [`PkiError`]-returning functions.
impl From<String> for PkiError {
    fn from(msg: String) -> Self {
        Self::Message(msg)
    }
}

fn not_before_with_skew_slack() -> OffsetDateTime {
    OffsetDateTime::now_utc() - Duration::days(NOT_BEFORE_SLACK_DAYS)
}

fn not_after(lifetime_days: i64) -> OffsetDateTime {
    OffsetDateTime::now_utc() + Duration::days(lifetime_days)
}

/// `indicatrix-worker cert init --dir <pki-dir>`: generates a new private CA keypair and
/// self-signed certificate.
///
/// Refuses to run if `<pki-dir>` already contains a CA -- overwriting one would
/// invalidate every certificate already issued from it. No `--force`: removing the old
/// files yourself is the point at which you're supposed to notice the blast radius.
///
/// # Errors
///
/// A human-readable message if `dir` can't be created, already has a CA, key
/// generation/self-signing fails, or the CA files can't be written (including a
/// Windows-ACL failure restricting `ca.key` -- see [`indicatrix_net::tls::write_private_key_pem`]).
pub fn init(dir: &Path) -> Result<(), PkiError> {
    std::fs::create_dir_all(dir).map_err(|source| PkiError::Io {
        verb: "create",
        path: dir.to_path_buf(),
        source,
    })?;

    let ca_cert_path = dir.join(CA_CERT_FILE);
    let ca_key_path = dir.join(CA_KEY_FILE);
    if ca_cert_path.exists() || ca_key_path.exists() {
        return Err(PkiError::CaAlreadyExists {
            dir: dir.to_path_buf(),
        });
    }

    let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
        context: "CA key generation failed",
        source,
    })?;
    let mut params =
        CertificateParams::new(Vec::<String>::new()).map_err(|source| PkiError::Rcgen {
            context: "unexpected error building an empty SAN list",
            source,
        })?;
    params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
    params.key_usages = vec![
        KeyUsagePurpose::KeyCertSign,
        KeyUsagePurpose::CrlSign,
        KeyUsagePurpose::DigitalSignature,
    ];
    params
        .distinguished_name
        .push(DnType::CommonName, "indicatrix-worker private CA");
    params.not_before = not_before_with_skew_slack();
    params.not_after = not_after(CA_LIFETIME_DAYS);

    let cert = params
        .self_signed(&key_pair)
        .map_err(|source| PkiError::Rcgen {
            context: "CA self-signing failed",
            source,
        })?;

    std::fs::write(&ca_cert_path, cert.pem()).map_err(|source| PkiError::Io {
        verb: "write",
        path: ca_cert_path.clone(),
        source,
    })?;
    tls::write_private_key_pem(&ca_key_path, &key_pair.serialize_pem())?;

    tracing::info!(
        "indicatrix-worker cert init: wrote {} and {} -- CA expires {} (UTC)",
        ca_cert_path.display(),
        ca_key_path.display(),
        params.not_after.date()
    );
    Ok(())
}

/// Reloads the CA's signing identity from `<dir>/ca.pem` and `<dir>/ca.key`, as saved
/// by [`init`]. Each `issue-*` subcommand is a fresh process, so there's nothing else
/// in memory to sign with.
///
/// # Errors
///
/// A human-readable message (naming `dir` and suggesting `cert init`) if either file is
/// missing or unreadable, or can't be parsed back into a CA signing identity.
pub(crate) fn load_ca(dir: &Path) -> Result<Issuer<'static, KeyPair>, PkiError> {
    let ca_cert_path = dir.join(CA_CERT_FILE);
    let ca_key_path = dir.join(CA_KEY_FILE);

    let ca_pem =
        std::fs::read_to_string(&ca_cert_path).map_err(|source| PkiError::CaFileUnreadable {
            path: ca_cert_path.clone(),
            dir: dir.to_path_buf(),
            source,
        })?;
    let ca_key_pem =
        std::fs::read_to_string(&ca_key_path).map_err(|source| PkiError::CaFileUnreadable {
            path: ca_key_path.clone(),
            dir: dir.to_path_buf(),
            source,
        })?;

    let ca_key = KeyPair::from_pem(&ca_key_pem).map_err(|source| PkiError::RcgenAtPath {
        path: ca_key_path.clone(),
        source,
    })?;
    // rcgen 0.14: `Issuer` pairs the CA's stored subject/extensions with its private
    // key; issuing a leaf only reads the stored CA cert, never mints a second one.
    let issuer =
        Issuer::from_ca_cert_pem(&ca_pem, ca_key).map_err(|source| PkiError::RcgenAtPath {
            path: ca_cert_path.clone(),
            source,
        })?;

    Ok(issuer)
}

/// `indicatrix-worker cert issue-server --dir <pki-dir> --host <name> --ip <addr>`: issues
/// this worker's server certificate, signed by the CA in `<pki-dir>`.
///
/// `hosts` and `ips` become the certificate's DNS and IP Subject Alternative Names --
/// at least one is required. `rustls` ignores Common Name entirely for hostname/IP
/// verification, so a server certificate with no SAN can never be validated.
///
/// # Errors
///
/// A human-readable message if both `hosts` and `ips` are empty, the CA can't be loaded
/// (see [`load_ca`]), signing fails, or the output files can't be written.
pub fn issue_server(dir: &Path, hosts: &[String], ips: &[IpAddr]) -> Result<(), PkiError> {
    if hosts.is_empty() && ips.is_empty() {
        return Err(PkiError::NoServerSan);
    }

    let ca_issuer = load_ca(dir)?;

    let mut sans: Vec<String> = hosts.to_vec();
    sans.extend(ips.iter().map(IpAddr::to_string));
    // `CertificateParams::new` classifies each string as IP or DNS by parsing it as an
    // `IpAddr` first, so passing them combined is equivalent to building SANs by hand.
    let mut params = CertificateParams::new(sans).map_err(|source| PkiError::Rcgen {
        context: "invalid --host/--ip value",
        source,
    })?;

    let common_name = hosts.first().cloned().unwrap_or_else(|| ips[0].to_string());
    params
        .distinguished_name
        .push(DnType::CommonName, common_name);
    params.not_before = not_before_with_skew_slack();
    params.not_after = not_after(LEAF_LIFETIME_DAYS);
    params.use_authority_key_identifier_extension = true;
    params.key_usages = vec![
        KeyUsagePurpose::DigitalSignature,
        KeyUsagePurpose::KeyEncipherment,
    ];
    params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ServerAuth];

    let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
        context: "server key generation failed",
        source,
    })?;
    let cert = params
        .signed_by(&key_pair, &ca_issuer)
        .map_err(|source| PkiError::Rcgen {
            context: "failed to sign the server certificate",
            source,
        })?;

    let cert_path = dir.join(SERVER_CERT_FILE);
    let key_path = dir.join(SERVER_KEY_FILE);
    std::fs::write(&cert_path, cert.pem()).map_err(|source| PkiError::Io {
        verb: "write",
        path: cert_path.clone(),
        source,
    })?;
    tls::write_private_key_pem(&key_path, &key_pair.serialize_pem())?;

    tracing::info!(
        "indicatrix-worker cert issue-server: wrote {} and {} (SANs: {}{}{}) -- expires {} (UTC)",
        cert_path.display(),
        key_path.display(),
        hosts.join(", "),
        if hosts.is_empty() || ips.is_empty() {
            ""
        } else {
            ", "
        },
        ips.iter()
            .map(IpAddr::to_string)
            .collect::<Vec<_>>()
            .join(", "),
        params.not_after.date()
    );
    Ok(())
}

/// `indicatrix-worker cert issue-client --dir <pki-dir> --name <name> --out <bundle-dir>`.
///
/// Issues a viewer's client certificate signed by the CA in `<pki-dir>`, writes a
/// self-contained bundle (`ca.pem`, `client.pem`, `client.key`) to `<bundle-dir>`, and
/// appends the new certificate's fingerprint to `<pki-dir>/allowlist.txt` (labeled
/// `name`) so the worker trusts it immediately -- no separate enrollment step.
///
/// `name` becomes the certificate's Subject Common Name, purely a human label; unlike
/// `issue-server`, no SAN is set since a client cert is never validated by hostname.
///
/// # Errors
///
/// A human-readable message if `name` is empty, the CA can't be loaded, signing fails,
/// `bundle-dir` or its files can't be written, or the fingerprint can't be appended to
/// the allowlist (in which case the printed message includes the line to add by hand).
pub fn issue_client(dir: &Path, name: &str, out: &Path) -> Result<(), PkiError> {
    let ca_issuer = load_ca(dir)?;
    let (cert, key_pair, not_after) = sign_client_leaf(&ca_issuer, name)?;

    std::fs::create_dir_all(out).map_err(|source| PkiError::Io {
        verb: "create",
        path: out.to_path_buf(),
        source,
    })?;

    let ca_source_path = dir.join(CA_CERT_FILE);
    let ca_pem = std::fs::read_to_string(&ca_source_path).map_err(|source| PkiError::Io {
        verb: "read",
        path: ca_source_path.clone(),
        source,
    })?;
    let out_ca_path = out.join(CA_CERT_FILE);
    let out_cert_path = out.join(CLIENT_CERT_FILE);
    let out_key_path = out.join(CLIENT_KEY_FILE);
    std::fs::write(&out_ca_path, &ca_pem).map_err(|source| PkiError::Io {
        verb: "write",
        path: out_ca_path.clone(),
        source,
    })?;
    std::fs::write(&out_cert_path, cert.pem()).map_err(|source| PkiError::Io {
        verb: "write",
        path: out_cert_path.clone(),
        source,
    })?;
    tls::write_private_key_pem(&out_key_path, &key_pair.serialize_pem())?;

    let fingerprint = tls::fingerprint(cert.der());
    let fingerprint_hex = tls::fingerprint_to_hex(&fingerprint);
    let allowlist_path = dir.join(ALLOWLIST_FILE);
    tls::append_to_allowlist(&allowlist_path, &fingerprint, name).map_err(|source| {
        PkiError::AllowlistUpdate {
            out_dir: out.to_path_buf(),
            path: allowlist_path.clone(),
            fingerprint_hex: fingerprint_hex.clone(),
            name: name.to_string(),
            source,
        }
    })?;

    tracing::info!(
        "indicatrix-worker cert issue-client: wrote bundle to {} ({CA_CERT_FILE}, {CLIENT_CERT_FILE}, {CLIENT_KEY_FILE}) -- \
         fingerprint {fingerprint_hex} added to {} -- expires {} (UTC)",
        out.display(),
        allowlist_path.display(),
        not_after.date()
    );
    Ok(())
}

/// The signing logic shared by [`issue_client`] (writes the bundle to disk and
/// allowlists it immediately) and [`issue_client_in_memory`] (returns PEM strings,
/// touches neither disk nor the allowlist).
///
/// # Errors
///
/// A human-readable message if `name` is empty, or signing fails.
fn sign_client_leaf(
    ca_issuer: &Issuer<'static, KeyPair>,
    name: &str,
) -> Result<(rcgen::Certificate, KeyPair, OffsetDateTime), PkiError> {
    if name.trim().is_empty() {
        return Err(PkiError::EmptyClientName);
    }

    let mut params =
        CertificateParams::new(Vec::<String>::new()).map_err(|source| PkiError::Rcgen {
            context: "unexpected error building an empty SAN list",
            source,
        })?;
    params.distinguished_name.push(DnType::CommonName, name);
    params.not_before = not_before_with_skew_slack();
    params.not_after = not_after(LEAF_LIFETIME_DAYS);
    params.use_authority_key_identifier_extension = true;
    params.key_usages = vec![KeyUsagePurpose::DigitalSignature];
    params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ClientAuth];

    let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
        context: "client key generation failed",
        source,
    })?;
    let cert = params
        .signed_by(&key_pair, ca_issuer)
        .map_err(|source| PkiError::Rcgen {
            context: "failed to sign the client certificate",
            source,
        })?;

    Ok((cert, key_pair, params.not_after))
}

/// A freshly issued client certificate bundle, held entirely in memory.
///
/// The counterpart to the three files [`issue_client`] writes to `<bundle-dir>`, for
/// callers (`crate::enroll`) that must not write secret key material to disk while an
/// enrollment token is unclaimed.
pub struct InMemoryClientBundle {
    /// PEM text of the CA certificate at `<pki-dir>/ca.pem` -- included so the claiming
    /// side gets the same self-contained three-file bundle `issue_client` would write.
    pub ca_pem: String,
    /// The CA certificate's SHA-256 fingerprint -- what an enrollment token commits to,
    /// so a claiming client can verify this worker's identity before sending its bearer
    /// secret. See `indicatrix_net::token`'s doc comment.
    pub ca_fingerprint: tls::Fingerprint,
    pub client_cert_pem: String,
    pub client_key_pem: String,
    /// The issued client certificate's SHA-256 fingerprint, appended to
    /// `allowlist.txt` only once the enrollment token is actually claimed (never at
    /// issue time -- see `crate::enroll`).
    pub client_fingerprint: tls::Fingerprint,
}

/// Mints a client certificate exactly like [`issue_client`] does, but in memory only.
///
/// Same CA, lifetime, and `ClientAuth` leaf, returned as PEM strings instead of written
/// to `<bundle-dir>`, and never touches `<pki-dir>/allowlist.txt`. This is what makes
/// token-based enrollment possible -- `crate::enroll` holds the bundle in an
/// [`crate::enroll::EnrollRegistry`] entry until claimed or expired.
///
/// # Errors
///
/// Same as [`issue_client`]: a human-readable message if `name` is empty, the CA can't be
/// loaded, or signing fails.
pub fn issue_client_in_memory(dir: &Path, name: &str) -> Result<InMemoryClientBundle, PkiError> {
    let ca_issuer = load_ca(dir)?;
    let (cert, key_pair, _not_after) = sign_client_leaf(&ca_issuer, name)?;

    let ca_cert_path = dir.join(CA_CERT_FILE);
    let ca_pem = std::fs::read_to_string(&ca_cert_path).map_err(|source| PkiError::Io {
        verb: "read",
        path: ca_cert_path.clone(),
        source,
    })?;
    let ca_der = tls::load_certs(&ca_cert_path).map_err(|source| PkiError::Certificate {
        path: ca_cert_path.clone(),
        source,
    })?;
    let ca_fingerprint = tls::fingerprint(&ca_der[0]);
    let client_fingerprint = tls::fingerprint(cert.der());

    Ok(InMemoryClientBundle {
        ca_pem,
        ca_fingerprint,
        client_cert_pem: cert.pem(),
        client_key_pem: key_pair.serialize_pem(),
        client_fingerprint,
    })
}

/// The default path `serve --allowlist` uses when not given explicitly.
///
/// Sits alongside the `--ca` file, named [`ALLOWLIST_FILE`] -- matching where
/// [`issue_client`] writes by default, so the common case needs no separate
/// `--allowlist` flag.
#[must_use]
pub fn default_allowlist_path(ca_path: &Path) -> PathBuf {
    ca_path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."))
        .join(ALLOWLIST_FILE)
}

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

    fn temp_dir(label: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "indicatrix-worker-pki-test-{label}-{}-{}",
            std::process::id(),
            fastrand_seed()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    // No rand dependency: a nanosecond timestamp keeps parallel test temp dirs unique.
    fn fastrand_seed() -> u128 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    }

    #[test]
    fn init_writes_a_ca_and_refuses_to_overwrite_it() {
        let dir = temp_dir("init");
        init(&dir).unwrap();
        assert!(dir.join(CA_CERT_FILE).exists());
        assert!(dir.join(CA_KEY_FILE).exists());

        let err = init(&dir).unwrap_err().to_string();
        assert!(err.contains("already contains a CA"), "{err}");

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn issue_server_requires_at_least_one_san() {
        let dir = temp_dir("issue-server-no-san");
        init(&dir).unwrap();

        let err = issue_server(&dir, &[], &[]).unwrap_err().to_string();
        assert!(err.contains("--host or --ip"), "{err}");

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn issue_server_writes_a_cert_with_the_requested_sans() {
        let dir = temp_dir("issue-server");
        init(&dir).unwrap();
        issue_server(
            &dir,
            &["worker.lan".to_string()],
            &["10.0.0.5".parse().unwrap()],
        )
        .unwrap();

        let cert_pem = std::fs::read_to_string(dir.join(SERVER_CERT_FILE)).unwrap();
        assert!(cert_pem.contains("BEGIN CERTIFICATE"));
        assert!(dir.join(SERVER_KEY_FILE).exists());

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn issue_client_writes_a_bundle_and_updates_the_allowlist() {
        let dir = temp_dir("issue-client-dir");
        let out = temp_dir("issue-client-out");
        init(&dir).unwrap();
        issue_client(&dir, "laptop", &out).unwrap();

        assert!(out.join(CA_CERT_FILE).exists());
        assert!(out.join(CLIENT_CERT_FILE).exists());
        assert!(out.join(CLIENT_KEY_FILE).exists());

        let allowlist_text = std::fs::read_to_string(dir.join(ALLOWLIST_FILE)).unwrap();
        assert!(allowlist_text.contains("# laptop"), "{allowlist_text}");

        let client_der = tls::load_certs(&out.join(CLIENT_CERT_FILE)).unwrap();
        let fp = tls::fingerprint(&client_der[0]);
        let allowlist = tls::Allowlist::load(&dir.join(ALLOWLIST_FILE)).unwrap();
        assert!(allowlist.contains(&fp));

        std::fs::remove_dir_all(&dir).ok();
        std::fs::remove_dir_all(&out).ok();
    }

    #[test]
    fn issue_client_rejects_an_empty_name() {
        let dir = temp_dir("issue-client-empty-name");
        init(&dir).unwrap();
        let err = issue_client(&dir, "  ", &dir.join("bundle"))
            .unwrap_err()
            .to_string();
        assert!(err.contains("--name"), "{err}");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn issue_server_and_issue_client_fail_clearly_without_an_existing_ca() {
        let dir = temp_dir("no-ca");
        let err = issue_server(&dir, &["worker.lan".to_string()], &[])
            .unwrap_err()
            .to_string();
        assert!(err.contains("cert init"), "{err}");
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn default_allowlist_path_sits_beside_the_ca_file() {
        let ca = Path::new("C:/pki/ca.pem");
        assert_eq!(
            default_allowlist_path(ca),
            PathBuf::from("C:/pki").join(ALLOWLIST_FILE)
        );
    }
}