engenho-runtime 0.53.131

engenho-runtime — the single-node assembly layer. Boots StoreMesh + ApiServer + controllers + scheduler + kubelet over ONE durable store in ONE process, so a POSTed apps/v1 Deployment converges to a running container. The engenho binary is a thin launcher over Runtime::start. Spec: theory/ENGENHO.md §IX; M0.1 item 7.
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
//! The confirmation handshake every destructive operation passes (P7).
//!
//! 1. **Prepare** (`createConfirmation`, `engenho ctl reinit prepare`): the
//!    daemon binds a challenge to what the operation would act on — the
//!    cluster and node names, the CA's fingerprint, the stop epoch for an
//!    operation that touches the store, the operation and its parameters, and
//!    who asked — and hands back a single-use id and the phrase to type (the
//!    cluster's name). It stands for [`TTL`].
//! 2. **Execute**: the operation carries the id (`Engenho-Confirmation`) and
//!    the phrase. The daemon takes the challenge out of the book — it is used
//!    up whatever happens next — and checks it ([`Binding::check`]): the same
//!    operation and parameters, the same caller, the phrase, and, computed
//!    afresh, the same cluster, node, CA and epoch.
//!
//! What this proves, tier-honestly: the operation is aimed at the cluster
//! the operator named, as it was when they looked, by whoever looked, once.
//! It does not prove a human typed it — which is why no destructive
//! operation is ever an MCP tool.
//!
//! No MAC. The plan named `Selo` (a keyed-hash capability token) for the
//! challenge. The client never carries the binding, only a 128-bit random id
//! the daemon keeps beside it, so there is nothing for a MAC to protect; and
//! single use needs the daemon's state whatever the token looks like.

use std::collections::HashMap;
use std::sync::{Mutex, MutexGuard, PoisonError};

use chrono::{DateTime, TimeDelta, Utc};
use engenho_apiserver::pki_inventory::CaFact;
use engenho_control_types::types;

use crate::entropy;

/// How long a challenge stands.
pub const TTL: TimeDelta = TimeDelta::seconds(120);

/// The most challenges the daemon holds at once. Only a destructive-tier
/// caller can issue one, but a bound on what it can make the daemon hold
/// costs nothing.
pub const PENDING_MAX: usize = 64;

/// What the CA was, when a challenge was bound.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CaBound {
    /// A CA certificate with this SHA-256 (lowercase hex).
    Present(String),
    /// No CA.
    Absent,
    /// Something there that does not read as a CA.
    Unreadable,
}

impl CaBound {
    /// What the PKI inventory says about the CA.
    #[must_use]
    pub fn of(fact: &CaFact) -> Self {
        match fact {
            CaFact::Present { sha256, .. } => Self::Present(sha256.clone()),
            CaFact::Absent => Self::Absent,
            CaFact::Unreadable(_) => Self::Unreadable,
        }
    }

    /// The control API's view of it. A fingerprint the API's pattern refuses
    /// (not expected: the inventory writes lowercase SHA-256 hex) is not a
    /// CA anything can bind to, and reads as unreadable.
    #[must_use]
    pub fn to_wire(&self) -> types::CaBinding {
        match self {
            Self::Present(sha256) => types::Sha256Hex::try_from(sha256.as_str())
                .map_or(types::CaBinding::Unreadable, types::CaBinding::Present),
            Self::Absent => types::CaBinding::Absent,
            Self::Unreadable => types::CaBinding::Unreadable,
        }
    }
}

/// Whether a challenge is bound to the runtime staying stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EpochBound {
    /// It touches the store: it runs only while the runtime rests in the
    /// stop epoch it was prepared in.
    Stopped(u64),
    /// It does not.
    NotRequired,
}

/// Everything a challenge is bound to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Binding {
    /// The cluster's name: the phrase the operator types.
    pub cluster_name: String,
    /// This node's name.
    pub node_name: String,
    /// The CA.
    pub ca: CaBound,
    /// The epoch, for an operation that touches the store.
    pub epoch: EpochBound,
    /// The operation.
    pub operation: types::ReinitOp,
    /// BLAKE3 of the operation's request, parameters included.
    pub params: blake3::Hash,
    /// BLAKE3 of what the transport attested about the caller.
    pub principal: blake3::Hash,
}

/// How an executed operation differs from the challenge it names.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Mismatch {
    /// The challenge was prepared for another operation.
    #[error("the challenge was prepared for {bound:?}, not {presented:?}")]
    Operation {
        /// What it was prepared for.
        bound: types::ReinitOp,
        /// What was executed.
        presented: types::ReinitOp,
    },
    /// The same operation, other parameters.
    #[error("the challenge was prepared with other parameters")]
    Params,
    /// Someone else prepared it.
    #[error("the challenge was prepared by another caller")]
    Principal,
    /// The phrase is not the cluster's name.
    #[error("the confirmation phrase is not the cluster's name")]
    Phrase,
    /// The cluster is not the one it was prepared against.
    #[error("the cluster is now {now:?}, not {bound:?}")]
    Cluster {
        /// Then.
        bound: String,
        /// Now.
        now: String,
    },
    /// The node is not the one it was prepared on.
    #[error("the node is now {now:?}, not {bound:?}")]
    Node {
        /// Then.
        bound: String,
        /// Now.
        now: String,
    },
    /// The CA changed since.
    #[error("the CA changed since the challenge was prepared")]
    Ca,
    /// The runtime ran since, or is not stopped.
    #[error("the runtime is not resting in the stop epoch the challenge was prepared in")]
    Epoch,
}

impl Binding {
    /// Check `now` — the operation as executed, its facts read afresh —
    /// against this challenge, and `phrase` against the cluster's name.
    ///
    /// # Errors
    ///
    /// The first [`Mismatch`], what was asked before what was found.
    pub fn check(&self, now: &Self, phrase: &str) -> Result<(), Mismatch> {
        if self.operation != now.operation {
            return Err(Mismatch::Operation {
                bound: self.operation,
                presented: now.operation,
            });
        }
        if self.params != now.params {
            return Err(Mismatch::Params);
        }
        if self.principal != now.principal {
            return Err(Mismatch::Principal);
        }
        if phrase != self.cluster_name {
            return Err(Mismatch::Phrase);
        }
        if self.cluster_name != now.cluster_name {
            return Err(Mismatch::Cluster {
                bound: self.cluster_name.clone(),
                now: now.cluster_name.clone(),
            });
        }
        if self.node_name != now.node_name {
            return Err(Mismatch::Node {
                bound: self.node_name.clone(),
                now: now.node_name.clone(),
            });
        }
        if self.ca != now.ca {
            return Err(Mismatch::Ca);
        }
        if self.epoch != now.epoch {
            return Err(Mismatch::Epoch);
        }
        Ok(())
    }

    /// The control API's view of it.
    ///
    /// # Errors
    ///
    /// A hash or fingerprint the API's pattern refuses (not expected: both
    /// are lowercase hex of the right length).
    pub fn to_wire(&self) -> Result<types::ChallengeBinding, String> {
        let hex = |h: &blake3::Hash| {
            types::Blake3Hex::try_from(h.to_hex().as_str()).map_err(|e| e.to_string())
        };
        Ok(types::ChallengeBinding {
            cluster_name: self.cluster_name.clone(),
            node_name: self.node_name.clone(),
            ca: self.ca.to_wire(),
            epoch: match self.epoch {
                EpochBound::Stopped(epoch) => types::EpochBinding::StopEpoch(epoch),
                EpochBound::NotRequired => types::EpochBinding::NotRequired,
            },
            operation: self.operation,
            params_blake3: hex(&self.params)?,
            principal_fingerprint: hex(&self.principal)?,
        })
    }
}

/// BLAKE3 of a value's JSON: the digest a challenge binds parameters by.
/// JSON of the same value is the same bytes (field order is declaration
/// order).
#[must_use]
pub fn digest<T: serde::Serialize>(value: &T) -> blake3::Hash {
    blake3::hash(&serde_json::to_vec(value).unwrap_or_default())
}

/// The digest a challenge binds its caller by: WHO, as the transport
/// attested it — the uid on the local socket, the pinned key (and the name
/// it is authorized under) remotely. Not the process: preparing and
/// executing are two `engenho ctl` runs, with two pids, by one operator.
#[must_use]
pub fn caller(attested: &types::AttestedView) -> blake3::Hash {
    match attested {
        types::AttestedView::LocalUid { uid, .. } => digest(&("local_uid", uid)),
        types::AttestedView::RemotePin { client, spki } => {
            digest(&("remote_pin", client, spki.as_str()))
        }
    }
}

/// A challenge the book issued.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Issued {
    /// Its single-use id: 32 lowercase hex characters.
    pub id: String,
    /// When it stops standing.
    pub expires_at: DateTime<Utc>,
}

/// Why a challenge was not issued.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum IssueError {
    /// [`PENDING_MAX`] stand already.
    #[error("{PENDING_MAX} challenges are already pending; cancel one or let them expire")]
    Full,
    /// No entropy for an id.
    #[error("no entropy for a challenge id: {0}")]
    Entropy(String),
}

/// Why a challenge could not be taken.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum RedeemError {
    /// No such challenge: never issued, already used, or cancelled.
    #[error("no pending challenge {0}: it was never issued, or it has been used or cancelled")]
    Unknown(String),
    /// It stood until then.
    #[error("the challenge expired at {0}")]
    Expired(DateTime<Utc>),
}

#[derive(Debug)]
struct Pending {
    binding: Binding,
    expires_at: DateTime<Utc>,
}

/// The daemon's pending challenges. Held in memory: a challenge does not
/// outlive the process that issued it.
#[derive(Debug, Default)]
pub struct ConfirmationBook {
    pending: Mutex<HashMap<String, Pending>>,
}

impl ConfirmationBook {
    fn lock(&self) -> MutexGuard<'_, HashMap<String, Pending>> {
        self.pending.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Issue a challenge bound to `binding`, standing until [`TTL`] after
    /// `now`. Expired challenges are dropped first.
    ///
    /// # Errors
    ///
    /// [`IssueError`].
    pub fn issue(&self, binding: Binding, now: DateTime<Utc>) -> Result<Issued, IssueError> {
        let mut pending = self.lock();
        pending.retain(|_, p| p.expires_at > now);
        if pending.len() >= PENDING_MAX {
            return Err(IssueError::Full);
        }
        let id = entropy::random::<16>()
            .map(|bytes| entropy::lower_hex(&bytes))
            .map_err(|e| IssueError::Entropy(e.to_string()))?;
        let expires_at = now + TTL;
        pending.insert(
            id.clone(),
            Pending {
                binding,
                expires_at,
            },
        );
        Ok(Issued { id, expires_at })
    }

    /// Take a challenge out of the book: it cannot be used again, whatever
    /// the caller does with it.
    ///
    /// # Errors
    ///
    /// [`RedeemError::Unknown`], or [`RedeemError::Expired`] (taken out all
    /// the same).
    pub fn redeem(&self, id: &str, now: DateTime<Utc>) -> Result<Binding, RedeemError> {
        let taken = self
            .lock()
            .remove(id)
            .ok_or_else(|| RedeemError::Unknown(id.to_owned()))?;
        if taken.expires_at <= now {
            return Err(RedeemError::Expired(taken.expires_at));
        }
        Ok(taken.binding)
    }

    /// Withdraw a challenge without executing anything.
    ///
    /// # Errors
    ///
    /// [`RedeemError::Unknown`].
    pub fn cancel(&self, id: &str) -> Result<(), RedeemError> {
        self.lock()
            .remove(id)
            .map(drop)
            .ok_or_else(|| RedeemError::Unknown(id.to_owned()))
    }
}

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

    fn at(seconds: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(1_800_000_000 + seconds, 0).expect("a moment")
    }

    fn binding() -> Binding {
        Binding {
            cluster_name: "ryn".into(),
            node_name: "ryn".into(),
            ca: CaBound::Present("ab".repeat(32)),
            epoch: EpochBound::Stopped(3),
            operation: types::ReinitOp::WipeStore,
            params: digest(&"store_only"),
            principal: digest(&501_u32),
        }
    }

    #[test]
    fn a_challenge_is_used_once() {
        let book = ConfirmationBook::default();
        let issued = book.issue(binding(), at(0)).expect("issued");
        assert_eq!(issued.id.len(), 32);
        assert!(issued.id.chars().all(|c| c.is_ascii_hexdigit()));
        assert_eq!(issued.expires_at, at(120));

        assert_eq!(book.redeem(&issued.id, at(1)), Ok(binding()));
        assert_eq!(
            book.redeem(&issued.id, at(2)),
            Err(RedeemError::Unknown(issued.id.clone())),
            "a used challenge was accepted again"
        );
    }

    #[test]
    fn an_expired_challenge_is_refused_and_used_up() {
        let book = ConfirmationBook::default();
        let issued = book.issue(binding(), at(0)).expect("issued");
        assert_eq!(
            book.redeem(&issued.id, at(120)),
            Err(RedeemError::Expired(at(120))),
            "it stands until, not through, its expiry"
        );
        assert!(matches!(
            book.redeem(&issued.id, at(0)),
            Err(RedeemError::Unknown(_))
        ));
        let fresh = book.issue(binding(), at(0)).expect("issued");
        assert!(book.redeem(&fresh.id, at(119)).is_ok());
    }

    #[test]
    fn a_cancelled_challenge_cannot_be_used() {
        let book = ConfirmationBook::default();
        let issued = book.issue(binding(), at(0)).expect("issued");
        assert_eq!(book.cancel(&issued.id), Ok(()));
        assert!(book.cancel(&issued.id).is_err());
        assert!(matches!(
            book.redeem(&issued.id, at(1)),
            Err(RedeemError::Unknown(_))
        ));
    }

    #[test]
    fn the_book_is_bounded_and_forgets_what_expired() {
        let book = ConfirmationBook::default();
        let ids: Vec<String> = (0..PENDING_MAX)
            .map(|_| book.issue(binding(), at(0)).expect("issued").id)
            .collect();
        assert_eq!(book.issue(binding(), at(1)), Err(IssueError::Full));
        assert_eq!(
            ids.iter().collect::<std::collections::BTreeSet<_>>().len(),
            PENDING_MAX,
            "two challenges share an id"
        );
        assert!(
            book.issue(binding(), at(120)).is_ok(),
            "the expired ones were not dropped"
        );
    }

    /// Every field of the binding is checked, each with its own reason, and
    /// what was asked is checked before what was found.
    #[test]
    fn every_bound_fact_is_checked() {
        type Change = fn(&mut Binding);
        let bound = binding();
        assert_eq!(bound.check(&bound, "ryn"), Ok(()));
        let cases: [(Change, Mismatch); 8] = [
            (
                |b| b.operation = types::ReinitOp::ReseedPki,
                Mismatch::Operation {
                    bound: types::ReinitOp::WipeStore,
                    presented: types::ReinitOp::ReseedPki,
                },
            ),
            (
                |b| b.params = digest(&"store_and_node_local"),
                Mismatch::Params,
            ),
            (|b| b.principal = digest(&0_u32), Mismatch::Principal),
            (
                |b| b.cluster_name = "plo".into(),
                Mismatch::Cluster {
                    bound: "ryn".into(),
                    now: "plo".into(),
                },
            ),
            (
                |b| b.node_name = "plo".into(),
                Mismatch::Node {
                    bound: "ryn".into(),
                    now: "plo".into(),
                },
            ),
            (|b| b.ca = CaBound::Absent, Mismatch::Ca),
            (|b| b.ca = CaBound::Unreadable, Mismatch::Ca),
            (|b| b.epoch = EpochBound::Stopped(4), Mismatch::Epoch),
        ];
        for (change, expected) in cases {
            let mut now = bound.clone();
            change(&mut now);
            assert_eq!(bound.check(&now, "ryn"), Err(expected));
        }
        assert_eq!(bound.check(&bound, "RYN"), Err(Mismatch::Phrase));
        assert_eq!(bound.check(&bound, ""), Err(Mismatch::Phrase));
        let mut retargeted = bound.clone();
        retargeted.operation = types::ReinitOp::RotateAdminToken;
        assert!(
            matches!(
                bound.check(&retargeted, "wrong"),
                Err(Mismatch::Operation { .. })
            ),
            "the operation is checked before the phrase"
        );
    }

    /// Preparing and executing are two processes of one operator: the
    /// caller is the uid, not the pid; another uid, or a remote key, is
    /// someone else.
    #[test]
    fn the_caller_is_who_not_which_process() {
        let local = |uid, pid| types::AttestedView::LocalUid { uid, gid: 20, pid };
        assert_eq!(caller(&local(501, 100)), caller(&local(501, 200)));
        assert_ne!(caller(&local(501, 100)), caller(&local(0, 100)));
        let remote = types::AttestedView::RemotePin {
            client: "ryn".into(),
            spki: types::SpkiSha256::try_from(["sha256:", &"ab".repeat(32)].concat())
                .expect("a pin"),
        };
        assert_ne!(caller(&remote), caller(&local(501, 100)));
    }

    #[test]
    fn the_binding_crosses_to_the_api() {
        let wire = binding().to_wire().expect("crosses");
        assert_eq!(wire.cluster_name, "ryn");
        assert_eq!(wire.epoch, types::EpochBinding::StopEpoch(3));
        assert_eq!(
            wire.params_blake3.as_str(),
            digest(&"store_only").to_hex().as_str()
        );
        let mut absent = binding();
        absent.ca = CaBound::Unreadable;
        absent.epoch = EpochBound::NotRequired;
        let wire = absent.to_wire().expect("crosses");
        assert_eq!(wire.ca, types::CaBinding::Unreadable);
        assert_eq!(wire.epoch, types::EpochBinding::NotRequired);
    }
}