pb-mapper-auth 0.6.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
//! 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 super::*;

mod epoch;
mod lifecycle;
use epoch::*;
use lifecycle::*;

pub(super) struct AuthActorState {
    leases: Leases,
    admin_replays: HashSet<[u8; 32]>,
    admin_replay_order: VecDeque<AdminReplayRecord>,
}

impl AuthActorState {
    pub(super) fn new(
        leases: Leases,
        admin_replays: HashSet<[u8; 32]>,
        admin_replay_order: VecDeque<AdminReplayRecord>,
    ) -> Self {
        Self {
            leases,
            admin_replays,
            admin_replay_order,
        }
    }
}

pub(super) async fn run_auth_actor(
    inner: Arc<AuthStateInner>,
    mut admin_lease: Arc<AuthLease>,
    mut command_rx: mpsc::Receiver<AuthCommand>,
    config: AuthConfig,
    state: AuthActorState,
    _state_lock: Arc<File>,
) {
    let AuthActorState {
        mut leases,
        mut admin_replays,
        mut admin_replay_order,
    } = state;
    let mut last_snapshot_at = unix_seconds();
    let mut tick = tokio::time::interval(Duration::from_secs(1));
    tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
    loop {
        tokio::select! {
            _ = tick.tick() => {
                let now = unix_seconds();
                leases.tick(now);
                prune_expired_admin_replays(
                    now,
                    &mut admin_replays,
                    &mut admin_replay_order,
                );
                // 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 compaction_is_allowed(inner.safe_mode.load(Ordering::Acquire))
                    && now.saturating_sub(last_snapshot_at)
                        >= SNAPSHOT_COMPACTION_INTERVAL.as_secs()
                {
                    let snapshot = build_snapshot(&inner, &admin_replay_order);
                    if let Err(error) = write_snapshot_and_truncate_wal(
                        &config,
                        &inner.admin_key(),
                        &snapshot,
                    ) {
                        inner.safe_mode.store(true, Ordering::Release);
                        cancel_all_temporary_leases(&inner);
                        tracing::error!(
                            event = "auth_state_safe_mode",
                            auth_stage = "snapshot_compaction",
                            reason = %error.code,
                            error = %error,
                            "authentication state compaction failed closed"
                        );
                    } else {
                        last_snapshot_at = now;
                    }
                }
            }
            command = command_rx.recv() => {
                let Some(command) = command else {
                    admin_lease.cancel_rotated();
                    cancel_all_temporary_leases(&inner);
                    break;
                };
                match command {
                    AuthCommand::ClaimAdminMutation {
                        authority,
                        fingerprint,
                        client_timestamp,
                        response,
                    } => {
                        let result = validate_admin_authority(&inner, &authority).and_then(|()| {
                            actor_claim_admin_mutation(
                                &inner,
                                &config,
                                &mut admin_replays,
                                &mut admin_replay_order,
                                fingerprint,
                                client_timestamp,
                            )
                        });
                        let _ = response.send(result);
                    }
                    AuthCommand::Issue { authority, ttl, label, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_issue(&inner, &config, &mut leases, ttl, label));
                        let _ = response.send(result);
                    }
                    AuthCommand::List { authority, page, page_size, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_list(&inner, page, page_size));
                        let _ = response.send(result);
                    }
                    AuthCommand::Show { authority, key_id, reveal, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_show(&inner, &config, key_id, reveal));
                        let _ = response.send(result);
                    }
                    AuthCommand::Renew { authority, key_id, ttl, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_renew(&inner, &config, &mut leases, key_id, ttl));
                        let _ = response.send(result);
                    }
                    AuthCommand::Revoke { authority, key_id, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_revoke(&inner, &config, &mut leases, key_id));
                        let _ = response.send(result);
                    }
                    AuthCommand::Gc { authority, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_gc(&inner, &config, &mut leases, &admin_replay_order));
                        let _ = response.send(result);
                    }
                    AuthCommand::Reset { authority, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_reset(&inner, &config, &mut leases, &admin_replay_order, "auth_state_reset"));
                        let _ = response.send(result);
                    }
                    AuthCommand::RotateRoot { authority, new_key, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_rotate_root(&inner, &config, &mut leases, &mut admin_lease, new_key));
                        if result.is_ok() {
                            admin_replays.clear();
                            admin_replay_order.clear();
                        }
                        let _ = response.send(result);
                    }
                    AuthCommand::SetLegacyProtocol { authority, policy, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .and_then(|()| actor_set_legacy_protocol(&inner, &config, policy));
                        let _ = response.send(result);
                    }
                    AuthCommand::Status { authority, response } => {
                        let result = validate_admin_authority(&inner, &authority)
                            .map(|()| actor_status(&inner));
                        let _ = response.send(result);
                    }
                    AuthCommand::Audit { authority, action, key_id, detail, response } => {
                        let result = validate_admin_authority(&inner, &authority).and_then(|()| {
                            append_audit(
                                &config,
                                &inner,
                                audit(&action, key_id, detail),
                            )
                        });
                        let _ = response.send(result);
                    }
                    AuthCommand::Shutdown { response } => {
                        admin_lease.cancel_rotated();
                        cancel_all_temporary_leases(&inner);
                        let _ = response.send(());
                        break;
                    }
                }
            }
        }
    }
}

fn actor_claim_admin_mutation(
    inner: &AuthStateInner,
    config: &AuthConfig,
    admin_replays: &mut HashSet<[u8; 32]>,
    admin_replay_order: &mut VecDeque<AdminReplayRecord>,
    fingerprint: [u8; 32],
    client_timestamp: u64,
) -> Result<(), AuthFailure> {
    let now = unix_seconds();
    prune_expired_admin_replays(now, admin_replays, admin_replay_order);
    if admin_replays.contains(&fingerprint) {
        return Err(AuthFailure::new(
            "admin_request_replayed",
            "administrator mutation was already admitted",
            false,
        ));
    }
    if admin_replays.len() >= ADMIN_REPLAY_CAPACITY {
        return Err(AuthFailure::new(
            "admin_replay_capacity_exhausted",
            "administrator mutation replay window is full; retry after older claims expire",
            true,
        ));
    }
    if now.abs_diff(client_timestamp) > ADMIN_REPLAY_RETENTION.as_secs() / 2 {
        return Err(AuthFailure::new(
            "admin_request_timestamp_invalid",
            "administrator mutation timestamp is outside the accepted window",
            false,
        ));
    }
    let record = AdminReplayRecord {
        fingerprint,
        client_timestamp,
        accepted_at: now,
    };
    fail_closed_on_uncertain_wal(
        inner,
        append_wal(
            config,
            &inner.admin_key(),
            &WalRecord::AdminReplay(record.clone()),
        ),
    )?;
    admin_replays.insert(fingerprint);
    admin_replay_order.push_back(record);
    Ok(())
}

pub(super) fn prune_expired_admin_replays(
    now: u64,
    admin_replays: &mut HashSet<[u8; 32]>,
    admin_replay_order: &mut VecDeque<AdminReplayRecord>,
) {
    admin_replay_order.retain(|record| {
        let keep = record.within_retention(now);
        if !keep {
            admin_replays.remove(&record.fingerprint);
        }
        keep
    });
}

fn actor_status(inner: &Arc<AuthStateInner>) -> AuthStatus {
    let slots = inner.slots();
    let high = inner.high();
    let active_keys = slots
        .iter()
        .filter(|slot| slot.state == SlotState::Active)
        .count()
        + high
            .iter()
            .filter(|entry| entry.state == SlotState::Active)
            .count();
    let expired_keys = slots
        .iter()
        .filter(|slot| slot.state == SlotState::Expired)
        .count()
        + high
            .iter()
            .filter(|entry| entry.state == SlotState::Expired)
            .count();
    let revoked_keys = slots
        .iter()
        .filter(|slot| slot.state == SlotState::Revoked)
        .count()
        + high
            .iter()
            .filter(|entry| entry.state == SlotState::Revoked)
            .count();
    let last_legacy_connection_at = inner.last_legacy_connection_at.load(Ordering::Acquire);
    AuthStatus {
        schema_version: 1,
        safe_mode: inner.safe_mode.load(Ordering::Acquire),
        capacity: slots.len(),
        active_keys,
        expired_keys,
        revoked_keys,
        legacy_protocol: if inner.legacy_protocol_allowed.load(Ordering::Acquire) {
            LegacyProtocolPolicy::Allow
        } else {
            LegacyProtocolPolicy::Deny
        },
        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()),
    }
}

fn validate_admin_authority(
    inner: &AuthStateInner,
    authority: &Weak<AuthLease>,
) -> Result<(), AuthFailure> {
    let presented = authority.upgrade().ok_or_else(|| {
        AuthFailure::new(
            "administrator_key_rotated",
            "administrator credential lease is no longer active",
            false,
        )
    })?;
    if presented.cancellation.is_cancelled() {
        return Err(AuthFailure::new(
            "administrator_key_rotated",
            "administrator credential lease has been cancelled",
            false,
        ));
    }
    let current = inner.admin.read().lease.upgrade().ok_or_else(|| {
        AuthFailure::new(
            "administrator_key_rotated",
            "active administrator credential lease is unavailable",
            false,
        )
    })?;
    if !Arc::ptr_eq(&presented, &current) {
        return Err(AuthFailure::new(
            "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(
            "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(
        "temporary_key_not_found",
        format!("temporary key {key_id} does not exist"),
        false,
    )
}

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

fn key_not_active() -> AuthFailure {
    AuthFailure::new(
        "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",
    }
}

fn audit(action: &str, key_id: Option<KeyId>, label: Option<String>) -> AuditRecord {
    AuditRecord {
        at: unix_seconds(),
        action: action.to_string(),
        key_id,
        label,
    }
}