reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Zero-Downtime Key Rotation
//!
//! Implements key rotation with overlap periods for zero service interruption:
//! - Overlap period where both old and new keys are valid
//! - Grace period after rotation before old key invalidation
//! - Automatic rotation based on key age
//! - Emergency rotation for compromised keys

use std::sync::Arc;
use std::time::Duration;
use uuid::Uuid;

use crate::security::api_keys::{
    ApiKeyManager, ApiKeyRecord, GeneratedKey, KeyRotationInfo, KeyStatus, RotationReason,
};
use crate::security::audit::{AuditActor, AuditEvent, AuditEventType, AuditLogger, AuditOutcome};
use chrono::{DateTime, Utc};
use secrecy::SecretString;

/// Key rotation configuration
#[derive(Debug, Clone, serde::Deserialize)]
pub struct RotationConfig {
    /// Overlap period where both old and new keys are valid
    pub overlap_period: Duration,
    /// Notification period before forced rotation
    pub notification_period: Duration,
    /// Grace period after rotation before old key invalidation
    pub grace_period: Duration,
    /// Maximum key age before forced rotation
    pub max_key_age: Duration,
    /// Enable automatic rotation
    pub auto_rotate: bool,
}

impl Default for RotationConfig {
    fn default() -> Self {
        Self {
            overlap_period: Duration::from_secs(3600),       // 1 hour
            notification_period: Duration::from_secs(604800), // 7 days
            grace_period: Duration::from_secs(86400),        // 24 hours
            max_key_age: Duration::from_secs(7776000),       // 90 days
            auto_rotate: true,
        }
    }
}

/// Key rotation states
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RotationState {
    /// No rotation in progress
    Stable,
    /// Rotation initiated, new key generated
    Initiated { new_key_id: Uuid },
    /// Both keys active (overlap period)
    Overlapping {
        old_key_id: Uuid,
        new_key_id: Uuid,
        overlap_ends: DateTime<Utc>,
    },
    /// Transition to new key, old key in grace period
    Transitioning {
        old_key_id: Uuid,
        new_key_id: Uuid,
        grace_ends: DateTime<Utc>,
    },
    /// Rotation complete, old key invalidated
    Completed { new_key_id: Uuid },
}

/// Rotation error
#[derive(Debug, thiserror::Error)]
pub enum RotationError {
    #[error("Key is already being rotated")]
    AlreadyRotating,
    #[error("Key is not in rotation state")]
    NotRotating,
    #[error("Overlap period still active")]
    OverlapPeriodActive,
    #[error("Key not found: {0}")]
    KeyNotFound(Uuid),
    #[error("Key error: {0}")]
    KeyError(String),
    #[error("Audit error: {0}")]
    AuditError(String),
}

/// Rotation notification event
#[derive(Debug, Clone)]
pub enum RotationNotificationEvent {
    RotationInitiated,
    OverlapEnding,
    RotationCompleted,
    EmergencyRotation,
}

/// Rotation notification
#[derive(Debug, Clone)]
pub struct RotationNotification {
    pub event: RotationNotificationEvent,
    pub old_key_prefix: String,
    pub new_key_prefix: String,
    pub overlap_ends: DateTime<Utc>,
    pub action_required: bool,
}

/// Rotation notifier trait
#[async_trait::async_trait]
pub trait RotationNotifier: Send + Sync {
    async fn send_rotation_notification(
        &self,
        tenant_id: Uuid,
        notification: RotationNotification,
    ) -> Result<(), String>;

    async fn send_urgent_notification(
        &self,
        tenant_id: Uuid,
        title: &str,
        message: &str,
    ) -> Result<(), String>;
}

/// Rotation handle returned when initiating rotation
#[derive(Debug)]
pub struct RotationHandle {
    pub old_key_id: Uuid,
    pub new_key_id: Uuid,
    /// The new API key (only returned once, must be stored by caller)
    pub new_api_key: SecretString,
    pub state: RotationState,
}

/// Candidate for rotation
#[derive(Debug, Clone)]
pub struct RotationCandidate {
    pub key_id: Uuid,
    pub tenant_id: Uuid,
    pub reason: RotationReason,
    pub priority: RotationPriority,
    pub age_days: u32,
}

/// Rotation priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RotationPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// Key Rotation Manager
pub struct KeyRotationManager {
    key_manager: Arc<ApiKeyManager>,
    config: RotationConfig,
    audit: Arc<AuditLogger>,
    notifier: Option<Arc<dyn RotationNotifier>>,
}

impl KeyRotationManager {
    /// Create a new key rotation manager
    pub fn new(
        key_manager: Arc<ApiKeyManager>,
        config: RotationConfig,
        audit: Arc<AuditLogger>,
    ) -> Self {
        Self {
            key_manager,
            config,
            audit,
            notifier: None,
        }
    }

    /// Set rotation notifier
    pub fn with_notifier(mut self, notifier: Arc<dyn RotationNotifier>) -> Self {
        self.notifier = Some(notifier);
        self
    }

    /// Initiate key rotation for a tenant
    pub async fn initiate_rotation(
        &self,
        tenant_id: Uuid,
        old_key_id: Uuid,
        reason: RotationReason,
    ) -> Result<RotationHandle, RotationError> {
        // Get existing key details
        let old_key = self
            .key_manager
            .get_key(old_key_id)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        if old_key.status == KeyStatus::Rotating {
            return Err(RotationError::AlreadyRotating);
        }

        // Generate new key with same scopes
        let expires_in = old_key.expires_at.map(|e| {
            (e - old_key.created_at)
                .to_std()
                .unwrap_or(Duration::from_secs(86400 * 365))
        });

        let new_key = self
            .key_manager
            .generate_key(tenant_id, old_key.scopes.clone(), expires_in)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Mark old key as rotating
        self.key_manager
            .update_status(old_key_id, KeyStatus::Rotating)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Set rotation metadata
        self.key_manager
            .set_rotation_info(
                old_key_id,
                KeyRotationInfo {
                    previous_key_id: None,
                    rotation_started: Utc::now(),
                    rotation_completed: None,
                    reason: reason.clone(),
                },
            )
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        let overlap_ends = Utc::now()
            + chrono::Duration::from_std(self.config.overlap_period)
                .unwrap_or(chrono::Duration::hours(1));

        // Audit log
        self.audit
            .log(
                AuditEvent::new(AuditEventType::KeyRotationStarted, AuditActor::System)
                    .with_tenant(tenant_id)
                    .with_key(old_key_id)
                    .with_details(serde_json::json!({
                        "old_key_id": old_key_id,
                        "new_key_id": new_key.key_id,
                        "reason": format!("{:?}", reason),
                        "overlap_ends": overlap_ends,
                    }))
                    .with_outcome(AuditOutcome::Success),
            )
            .await
            .map_err(|e| RotationError::AuditError(e.to_string()))?;

        // Notify customer
        if let Some(ref notifier) = self.notifier {
            let _ = notifier
                .send_rotation_notification(
                    tenant_id,
                    RotationNotification {
                        event: RotationNotificationEvent::RotationInitiated,
                        old_key_prefix: old_key.key_prefix.clone(),
                        new_key_prefix: new_key.key_id.to_string()[..8].to_string(),
                        overlap_ends,
                        action_required: true,
                    },
                )
                .await;
        }

        Ok(RotationHandle {
            old_key_id,
            new_key_id: new_key.key_id,
            new_api_key: new_key.api_key,
            state: RotationState::Overlapping {
                old_key_id,
                new_key_id: new_key.key_id,
                overlap_ends,
            },
        })
    }

    /// Complete rotation (invalidate old key)
    pub async fn complete_rotation(
        &self,
        old_key_id: Uuid,
        new_key_id: Uuid,
    ) -> Result<(), RotationError> {
        // Verify rotation is in progress
        let old_key = self
            .key_manager
            .get_key(old_key_id)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        if old_key.status != KeyStatus::Rotating {
            return Err(RotationError::NotRotating);
        }

        // Check overlap period has passed
        if let Some(rotation) = &old_key.rotation {
            let overlap_end = rotation.rotation_started
                + chrono::Duration::from_std(self.config.overlap_period)
                    .unwrap_or(chrono::Duration::hours(1));
            if Utc::now() < overlap_end {
                return Err(RotationError::OverlapPeriodActive);
            }
        }

        // Revoke old key
        self.key_manager
            .update_status(old_key_id, KeyStatus::Revoked)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Update new key to active (remove rotation flag)
        self.key_manager
            .update_status(new_key_id, KeyStatus::Active)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        let rotation_duration = old_key
            .rotation
            .as_ref()
            .map(|r| (Utc::now() - r.rotation_started).num_seconds())
            .unwrap_or(0);

        // Audit log
        self.audit
            .log(
                AuditEvent::new(AuditEventType::KeyRotationCompleted, AuditActor::System)
                    .with_tenant(old_key.tenant_id)
                    .with_key(new_key_id)
                    .with_details(serde_json::json!({
                        "old_key_id": old_key_id,
                        "new_key_id": new_key_id,
                        "rotation_duration_secs": rotation_duration,
                    }))
                    .with_outcome(AuditOutcome::Success),
            )
            .await
            .map_err(|e| RotationError::AuditError(e.to_string()))?;

        Ok(())
    }

    /// Check for keys requiring rotation
    pub async fn check_rotation_required(&self) -> Vec<RotationCandidate> {
        let mut candidates = Vec::new();

        let keys = match self.key_manager.list_active_keys().await {
            Ok(keys) => keys,
            Err(_) => return candidates,
        };

        for key in keys {
            let age = Utc::now() - key.created_at;

            // Check if key exceeds max age
            let max_age_duration =
                chrono::Duration::from_std(self.config.max_key_age).unwrap_or(chrono::Duration::days(90));
            if age > max_age_duration {
                candidates.push(RotationCandidate {
                    key_id: key.key_id,
                    tenant_id: key.tenant_id,
                    reason: RotationReason::Scheduled,
                    priority: RotationPriority::High,
                    age_days: age.num_days() as u32,
                });
                continue;
            }

            // Check if approaching max age (notification period)
            let notification_threshold = self.config.max_key_age - self.config.notification_period;
            let notification_duration =
                chrono::Duration::from_std(notification_threshold).unwrap_or(chrono::Duration::days(83));
            if age > notification_duration {
                candidates.push(RotationCandidate {
                    key_id: key.key_id,
                    tenant_id: key.tenant_id,
                    reason: RotationReason::Scheduled,
                    priority: RotationPriority::Medium,
                    age_days: age.num_days() as u32,
                });
            }
        }

        candidates
    }

    /// Emergency rotation (immediate, no overlap)
    pub async fn emergency_rotation(
        &self,
        tenant_id: Uuid,
        key_id: Uuid,
        reason: &str,
    ) -> Result<RotationHandle, RotationError> {
        // Immediately revoke compromised key
        self.key_manager
            .update_status(key_id, KeyStatus::Revoked)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Get old key for scope replication
        let old_key = self
            .key_manager
            .get_key(key_id)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Calculate remaining validity if expiration was set
        let expires_in = old_key.expires_at.and_then(|e| {
            let remaining = e - Utc::now();
            if remaining > chrono::Duration::zero() {
                remaining.to_std().ok()
            } else {
                Some(Duration::from_secs(86400)) // 1 day minimum
            }
        });

        // Generate new key
        let new_key = self
            .key_manager
            .generate_key(tenant_id, old_key.scopes.clone(), expires_in)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        // Audit log with security incident
        self.audit
            .log(
                AuditEvent::new(AuditEventType::SecurityIncident, AuditActor::System)
                    .with_tenant(tenant_id)
                    .with_key(key_id)
                    .with_details(serde_json::json!({
                        "incident_type": "emergency_key_rotation",
                        "reason": reason,
                        "old_key_id": key_id,
                        "new_key_id": new_key.key_id,
                    }))
                    .with_outcome(AuditOutcome::Success),
            )
            .await
            .map_err(|e| RotationError::AuditError(e.to_string()))?;

        // Urgent notification
        if let Some(ref notifier) = self.notifier {
            let _ = notifier
                .send_urgent_notification(
                    tenant_id,
                    "Emergency Key Rotation",
                    &format!(
                        "Your API key has been rotated due to: {}. Please update immediately.",
                        reason
                    ),
                )
                .await;
        }

        Ok(RotationHandle {
            old_key_id: key_id,
            new_key_id: new_key.key_id,
            new_api_key: new_key.api_key,
            state: RotationState::Completed {
                new_key_id: new_key.key_id,
            },
        })
    }

    /// Get rotation status for a key
    pub async fn get_rotation_status(&self, key_id: Uuid) -> Result<RotationState, RotationError> {
        let key = self
            .key_manager
            .get_key(key_id)
            .await
            .map_err(|e| RotationError::KeyError(e.to_string()))?;

        match key.status {
            KeyStatus::Rotating => {
                if let Some(rotation) = &key.rotation {
                    let overlap_end = rotation.rotation_started
                        + chrono::Duration::from_std(self.config.overlap_period)
                            .unwrap_or(chrono::Duration::hours(1));

                    if Utc::now() < overlap_end {
                        // Still in overlap
                        Ok(RotationState::Overlapping {
                            old_key_id: key_id,
                            new_key_id: Uuid::nil(), // Would need to track this
                            overlap_ends: overlap_end,
                        })
                    } else {
                        // In grace period
                        let grace_end = overlap_end
                            + chrono::Duration::from_std(self.config.grace_period)
                                .unwrap_or(chrono::Duration::days(1));
                        Ok(RotationState::Transitioning {
                            old_key_id: key_id,
                            new_key_id: Uuid::nil(),
                            grace_ends: grace_end,
                        })
                    }
                } else {
                    Ok(RotationState::Stable)
                }
            }
            KeyStatus::Revoked => {
                if key.rotation.is_some() {
                    Ok(RotationState::Completed {
                        new_key_id: Uuid::nil(),
                    })
                } else {
                    Ok(RotationState::Stable)
                }
            }
            _ => Ok(RotationState::Stable),
        }
    }
}

/// No-op rotation notifier for testing
pub struct NoOpRotationNotifier;

#[async_trait::async_trait]
impl RotationNotifier for NoOpRotationNotifier {
    async fn send_rotation_notification(
        &self,
        _tenant_id: Uuid,
        _notification: RotationNotification,
    ) -> Result<(), String> {
        Ok(())
    }

    async fn send_urgent_notification(
        &self,
        _tenant_id: Uuid,
        _title: &str,
        _message: &str,
    ) -> Result<(), String> {
        Ok(())
    }
}

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

    #[test]
    fn test_default_rotation_config() {
        let config = RotationConfig::default();

        assert_eq!(config.overlap_period, Duration::from_secs(3600));
        assert_eq!(config.notification_period, Duration::from_secs(604800));
        assert_eq!(config.grace_period, Duration::from_secs(86400));
        assert_eq!(config.max_key_age, Duration::from_secs(7776000));
        assert!(config.auto_rotate);
    }

    #[test]
    fn test_rotation_priority_ordering() {
        assert!(RotationPriority::Critical != RotationPriority::High);
        assert!(RotationPriority::High != RotationPriority::Medium);
        assert!(RotationPriority::Medium != RotationPriority::Low);
    }
}