pb-mapper-auth 0.14.0

Credential lifecycle and persistence for pb-mapper
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
//! Serialized owner of mutable authentication lifecycle state.
//!
//! ```text
//! authenticated admin command
//!           |
//!           v
//!   validate current admin lease
//!           |
//!           v
//! append encrypted WAL -> mutate slots / leases / timing wheel
//!           |
//!           +-> periodic snapshot + bounded replay/audit retention
//! ```
//!
//! Keeping authorization revalidation and mutations in one actor prevents a request
//! authenticated before root rotation from executing against the new administrator
//! state. The actor is also the sole strong owner of temporary-key leases.

use std::ops::ControlFlow;

use super::*;

mod command;
mod epoch;
mod lifecycle;
mod replay;
pub(super) use command::AuthCommand;
use epoch::*;
use lifecycle::*;
pub(super) use replay::AdminReplays;

/// Everything the actor owns. The runtime builds it and spawns [`Actor::run`].
///
/// Fields drop in declaration order, and `inner` must come last: dropping the
/// schedule runs each live key's retire callback, which upgrades the state to
/// cancel the key's lease. So an actor that ends without `stop()` (aborted,
/// or unwinding from a panic) still cancels every live
/// temporary-key session and logs `temporary_key_expired` for it.
pub(super) struct Actor {
    config: AuthConfig,
    leases: Leases,
    /// The current administrator lease. The actor holds its only strong
    /// reference, so replacing it is what ends the previous one.
    admin_lease: Arc<AuthLease>,
    replays: AdminReplays,
    /// The wall-clock second of the last compaction, whether or not it had
    /// anything to write, or of the start.
    last_snapshot_at: u64,
    /// Keeps the state directory locked for as long as the actor runs.
    _state_lock: Arc<File>,
    /// The only strong reference to the state; see above for why it is last.
    inner: Arc<AuthStateInner>,
}

/// What the actor does next, off the runtime's workers.
enum Step {
    Command(AuthCommand),
    /// Compact, at this wall-clock second.
    Compact(u64),
    /// Every handle is gone.
    Stop,
}

impl Actor {
    pub(super) fn new(
        inner: Arc<AuthStateInner>,
        admin_lease: Arc<AuthLease>,
        config: AuthConfig,
        leases: Leases,
        replays: AdminReplays,
        state_lock: Arc<File>,
        now: u64,
    ) -> Self {
        Self {
            inner,
            config,
            leases,
            admin_lease,
            replays,
            last_snapshot_at: now,
            _state_lock: state_lock,
        }
    }

    /// Serves commands until a shutdown or until every runtime handle is gone,
    /// advancing the schedule once a second in between.
    ///
    /// The schedule advances here. Every command and every compaction, which
    /// write and sync the state's files, runs on the blocking pool instead,
    /// with the actor moved there and back: one step at a time, in the order
    /// they came, each finished before the next begins. So their file work
    /// holds up no other task on the runtime, and each stays whole as if it
    /// ran here: an abort or a runtime shutdown lets a step under way finish
    /// and drops the actor after it, never inside one.
    pub(super) async fn run(self, mut command_rx: mpsc::Receiver<AuthCommand>) {
        let mut actor = self;
        let mut tick = tokio::time::interval(Duration::from_secs(1));
        tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            let step = tokio::select! {
                _ = tick.tick() => {
                    let now = unix_seconds();
                    actor.advance(now);
                    if !actor.compaction_due(now) {
                        continue;
                    }
                    Step::Compact(now)
                }
                command = command_rx.recv() => match command {
                    Some(command) => Step::Command(command),
                    None => Step::Stop,
                },
            };
            match off_runtime(move || actor.step(step)).await {
                Some(Some(next)) => actor = next,
                // Stopped, or the runtime is shutting down: either way the
                // actor has dropped.
                Some(None) | None => break,
            }
        }
    }

    /// Takes one step. Returns the actor while it serves on; once it stops,
    /// it drops here.
    fn step(mut self, step: Step) -> Option<Self> {
        match step {
            Step::Command(command) => {
                if self.handle(command).is_break() {
                    return None;
                }
            }
            Step::Compact(now) => self.compact(now),
            Step::Stop => {
                self.stop();
                return None;
            }
        }
        Some(self)
    }

    /// Runs the schedule and the replay window up to wall-clock second `now`.
    pub(super) fn advance(&mut self, now: u64) {
        self.leases.tick(now);
        if self.replays.prune(now) {
            self.inner.mark_snapshot_stale();
        }
    }

    /// Whether to compact at wall-clock second `now`: once an interval, if
    /// anything has changed since the last compaction. An interval with
    /// nothing to write counts as compacted.
    pub(super) fn compaction_due(&mut self, now: u64) -> bool {
        // WHY: A failed load starts safe mode with empty in-memory
        // generations. Compacting that reconstruction would replace the
        // damaged snapshot, truncate the WAL, and let the next start
        // exit safe mode without rotating the instance id.
        if self.inner.safe_mode.load(Ordering::Acquire)
            || now.saturating_sub(self.last_snapshot_at) < SNAPSHOT_COMPACTION_INTERVAL.as_secs()
        {
            return false;
        }
        if !self.inner.snapshot_stale.load(Ordering::Acquire) {
            self.last_snapshot_at = now;
            return false;
        }
        true
    }

    /// Replaces the snapshot with the state as of wall-clock second `now` and
    /// empties the WAL, or fails the store closed.
    pub(super) fn compact(&mut self, now: u64) {
        let snapshot = build_snapshot(&self.inner, self.replays.records());
        if let Err(error) =
            write_snapshot_and_truncate_wal(&self.config, &self.inner.admin_key(), &snapshot)
        {
            self.inner.safe_mode.store(true, Ordering::Release);
            cancel_all_temporary_leases(&self.inner);
            tracing::error!(
                event = "auth_state_safe_mode",
                auth_stage = "snapshot_compaction",
                reason = %error.code,
                error = %error,
                "authentication state compaction failed closed"
            );
        } else {
            // Nothing else changes the state while this step runs.
            self.inner.snapshot_stale.store(false, Ordering::Release);
            self.last_snapshot_at = now;
        }
    }

    /// Runs one command. Breaks once the actor should stop.
    pub(super) fn handle(&mut self, command: AuthCommand) -> ControlFlow<()> {
        match command {
            AuthCommand::ClaimAdminMutation {
                authority,
                fingerprint,
                client_timestamp,
                response,
            } => self.admin(&authority, response, |actor| {
                actor
                    .replays
                    .claim(&actor.inner, &actor.config, fingerprint, client_timestamp)
            }),
            AuthCommand::AllocateNamespace {
                authority,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_allocate_namespace(&actor.inner, &actor.config)
            }),
            AuthCommand::Issue {
                authority,
                namespace,
                ttl,
                label,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_issue(
                    &actor.inner,
                    &actor.config,
                    &mut actor.leases,
                    namespace,
                    ttl,
                    label,
                )
            }),
            AuthCommand::List {
                authority,
                page,
                page_size,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_list(&actor.inner, page, page_size)
            }),
            AuthCommand::Show {
                authority,
                key_id,
                reveal,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_show(&actor.inner, &actor.config, key_id, reveal)
            }),
            AuthCommand::Renew {
                authority,
                key_id,
                ttl,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_renew(&actor.inner, &actor.config, &mut actor.leases, key_id, ttl)
            }),
            AuthCommand::Revoke {
                authority,
                key_id,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_revoke(&actor.inner, &actor.config, &mut actor.leases, key_id)
            }),
            AuthCommand::Gc {
                authority,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_gc(
                    &actor.inner,
                    &actor.config,
                    &mut actor.leases,
                    actor.replays.records(),
                )
            }),
            AuthCommand::Reset {
                authority,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_reset(
                    &actor.inner,
                    &actor.config,
                    &mut actor.leases,
                    actor.replays.records(),
                )
            }),
            AuthCommand::RotateRoot {
                authority,
                new_key,
                response,
            } => self.admin(&authority, response, |actor| {
                let result = actor_rotate_root(
                    &actor.inner,
                    &actor.config,
                    &mut actor.leases,
                    &mut actor.admin_lease,
                    new_key,
                );
                if result.is_ok() {
                    actor.replays.clear();
                }
                result
            }),
            AuthCommand::SetLegacyProtocol {
                authority,
                policy,
                response,
            } => self.admin(&authority, response, |actor| {
                actor_set_legacy_protocol(&actor.inner, &actor.config, policy)
            }),
            AuthCommand::Status {
                authority,
                response,
            } => self.admin(&authority, response, |actor| Ok(actor_status(&actor.inner))),
            AuthCommand::Audit {
                authority,
                action,
                key_id,
                detail,
                response,
            } => self.admin(&authority, response, |actor| {
                append_audit(&actor.config, &actor.inner, audit(&action, key_id, detail))
            }),
            AuthCommand::Shutdown { response } => {
                self.stop();
                let _ = response.send(());
                return ControlFlow::Break(());
            }
        }
        ControlFlow::Continue(())
    }

    /// Runs an administrator command, checking the authority it was sent with
    /// against the current administrator lease immediately beforehand, and
    /// sends the outcome back.
    fn admin<T>(
        &mut self,
        authority: &Weak<AuthLease>,
        response: oneshot::Sender<Result<T, AuthFailure>>,
        command: impl FnOnce(&mut Self) -> Result<T, AuthFailure>,
    ) {
        let result = validate_admin_authority(&self.inner, authority).and_then(|()| command(self));
        let _ = response.send(result);
    }

    /// Ends the administrator lease and every temporary one, for either way
    /// the actor stops.
    fn stop(&mut self) {
        self.admin_lease.cancel_rotated();
        cancel_all_temporary_leases(&self.inner);
    }
}

fn actor_status(inner: &Arc<AuthStateInner>) -> AuthStatus {
    let slots = inner.slots();
    let high = inner.high();
    let (mut active_keys, mut expired_keys, mut revoked_keys) = (0, 0, 0);
    let states = slots.iter().map(|slot| slot.state);
    for state in states.chain(high.iter().map(|entry| entry.state)) {
        match state {
            SlotState::Free => {}
            SlotState::Active => active_keys += 1,
            SlotState::Expired => expired_keys += 1,
            SlotState::Revoked => revoked_keys += 1,
        }
    }
    let last_legacy_connection_at = inner.last_legacy_connection_at.load(Ordering::Acquire);
    AuthStatus {
        schema_version: ADMIN_SCHEMA_VERSION,
        safe_mode: inner.safe_mode.load(Ordering::Acquire),
        capacity: slots.len(),
        active_keys,
        expired_keys,
        revoked_keys,
        legacy_protocol: inner.legacy_policy(),
        active_legacy_connections: inner.active_legacy_connections.load(Ordering::Acquire),
        last_legacy_connection_at: (last_legacy_connection_at != 0)
            .then_some(last_legacy_connection_at),
        auth_successes: inner.auth_successes.load(Ordering::Relaxed),
        auth_failures: inner.auth_failures.load(Ordering::Relaxed),
        server_instance_id: hex(&inner.instance_id()),
        capabilities: vec![CAPABILITY_KEY_NAMESPACES.to_string()],
    }
}

fn validate_admin_authority(
    inner: &AuthStateInner,
    authority: &Weak<AuthLease>,
) -> Result<(), AuthFailure> {
    let presented = authority.upgrade().ok_or_else(|| {
        AuthFailure::new(
            codes::ADMINISTRATOR_KEY_ROTATED,
            "administrator credential lease is no longer active",
            false,
        )
    })?;
    if presented.is_cancelled() {
        return Err(AuthFailure::new(
            codes::ADMINISTRATOR_KEY_ROTATED,
            "administrator credential lease has been cancelled",
            false,
        ));
    }
    let current = inner.admin.read().lease.upgrade().ok_or_else(|| {
        AuthFailure::new(
            codes::ADMINISTRATOR_KEY_ROTATED,
            "active administrator credential lease is unavailable",
            false,
        )
    })?;
    if !Arc::ptr_eq(&presented, &current) {
        return Err(AuthFailure::new(
            codes::ADMINISTRATOR_KEY_ROTATED,
            "administrator request was authenticated before the latest root-key rotation",
            false,
        ));
    }
    Ok(())
}

fn ensure_store_available(inner: &AuthStateInner) -> Result<(), AuthFailure> {
    if inner.safe_mode.load(Ordering::Acquire) {
        Err(AuthFailure::new(
            codes::TEMPORARY_KEY_STORE_UNAVAILABLE,
            "temporary key store is in administrator safe mode",
            false,
        ))
    } else {
        Ok(())
    }
}

fn validate_slot_identity(slot: &SlotHot, key_id: KeyId) -> Result<(), AuthFailure> {
    if slot.generation != key_id.generation() || slot.state == SlotState::Free {
        Err(key_not_found(key_id))
    } else {
        Ok(())
    }
}

fn key_not_found(key_id: KeyId) -> AuthFailure {
    AuthFailure::new(
        codes::TEMPORARY_KEY_NOT_FOUND,
        format!("temporary key {key_id} does not exist"),
        false,
    )
}

fn key_not_renewable() -> AuthFailure {
    AuthFailure::new(
        codes::TEMPORARY_KEY_NOT_RENEWABLE,
        "only an active, unexpired temporary key can be renewed",
        false,
    )
}

fn key_not_active() -> AuthFailure {
    AuthFailure::new(
        codes::TEMPORARY_KEY_NOT_ACTIVE,
        "temporary key is not active",
        false,
    )
}

fn slot_state_name(state: SlotState) -> &'static str {
    match state {
        SlotState::Free => "free",
        SlotState::Active => "active",
        SlotState::Expired => "expired",
        SlotState::Revoked => "revoked",
    }
}

/// An audit record of `action`, stamped now. See [`AuditRecord::label`] for what
/// the last argument holds.
fn audit(action: &str, key_id: Option<KeyId>, label_or_detail: Option<String>) -> AuditRecord {
    AuditRecord {
        at: unix_seconds(),
        action: action.to_string(),
        key_id,
        label: label_or_detail,
        namespace: None,
    }
}