allsource-core 0.19.1

High-performance event store core built in Rust
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
use crate::{
    domain::{
        entities::{Actor, AuditAction, AuditEvent, AuditOutcome},
        repositories::AuditEventRepository,
        value_objects::TenantId,
    },
    error::AllSourceError,
};
use serde_json::Value as JsonValue;
use std::sync::Arc;
use tracing::error;

/// Request context extracted from HTTP requests
#[derive(Debug, Clone)]
pub struct RequestContext {
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub request_id: Option<String>,
}

impl RequestContext {
    pub fn new() -> Self {
        Self {
            ip_address: None,
            user_agent: None,
            request_id: None,
        }
    }

    pub fn with_ip(mut self, ip: String) -> Self {
        self.ip_address = Some(ip);
        self
    }

    pub fn with_user_agent(mut self, user_agent: String) -> Self {
        self.user_agent = Some(user_agent);
        self
    }

    pub fn with_request_id(mut self, request_id: String) -> Self {
        self.request_id = Some(request_id);
        self
    }
}

impl Default for RequestContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for creating audit log entries
pub struct AuditLogBuilder {
    tenant_id: TenantId,
    action: AuditAction,
    actor: Actor,
    outcome: AuditOutcome,
    resource_type: Option<String>,
    resource_id: Option<String>,
    ip_address: Option<String>,
    user_agent: Option<String>,
    request_id: Option<String>,
    error_message: Option<String>,
    metadata: Option<JsonValue>,
}

impl AuditLogBuilder {
    fn new(tenant_id: TenantId, action: AuditAction, actor: Actor) -> Self {
        Self {
            tenant_id,
            action,
            actor,
            outcome: AuditOutcome::Success,
            resource_type: None,
            resource_id: None,
            ip_address: None,
            user_agent: None,
            request_id: None,
            error_message: None,
            metadata: None,
        }
    }

    pub fn with_outcome(mut self, outcome: AuditOutcome) -> Self {
        self.outcome = outcome;
        self
    }

    pub fn with_resource(mut self, resource_type: String, resource_id: String) -> Self {
        self.resource_type = Some(resource_type);
        self.resource_id = Some(resource_id);
        self
    }

    pub fn with_context(mut self, context: RequestContext) -> Self {
        self.ip_address = context.ip_address;
        self.user_agent = context.user_agent;
        self.request_id = context.request_id;
        self
    }

    pub fn with_ip_address(mut self, ip: String) -> Self {
        self.ip_address = Some(ip);
        self
    }

    pub fn with_user_agent(mut self, user_agent: String) -> Self {
        self.user_agent = Some(user_agent);
        self
    }

    pub fn with_request_id(mut self, request_id: String) -> Self {
        self.request_id = Some(request_id);
        self
    }

    pub fn with_error(mut self, error_message: String) -> Self {
        self.error_message = Some(error_message);
        self.outcome = AuditOutcome::Failure;
        self
    }

    pub fn with_metadata(mut self, metadata: JsonValue) -> Self {
        self.metadata = Some(metadata);
        self
    }

    fn build(self) -> AuditEvent {
        let mut event = AuditEvent::new(self.tenant_id, self.action, self.actor, self.outcome);

        if let (Some(resource_type), Some(resource_id)) = (self.resource_type, self.resource_id) {
            event = event.with_resource(resource_type, resource_id);
        }

        if let Some(ip) = self.ip_address {
            event = event.with_ip_address(ip);
        }

        if let Some(ua) = self.user_agent {
            event = event.with_user_agent(ua);
        }

        if let Some(req_id) = self.request_id {
            event = event.with_request_id(req_id);
        }

        if let Some(err) = self.error_message {
            event = event.with_error(err);
        }

        if let Some(meta) = self.metadata {
            event = event.with_metadata(meta);
        }

        event
    }

    pub async fn record<R: AuditEventRepository>(self, repo: &R) -> Result<(), AllSourceError> {
        let event = self.build();
        repo.append(event).await
    }
}

/// AuditLogger service for simplified audit event recording
///
/// This service provides a convenient API for recording audit events
/// from application code and middleware. It handles:
/// - Automatic context extraction from HTTP requests
/// - Actor detection from authentication context
/// - Async, non-blocking logging
/// - Error handling (audit failures are logged but don't break requests)
///
/// # Example
/// ```ignore
/// let audit_logger = AuditLogger::new(audit_repo);
///
/// audit_logger.log(
///     tenant_id,
///     AuditAction::EventIngested,
///     Actor::api_key("key-123".to_string(), "prod-api-key".to_string()),
/// )
/// .with_resource("event_stream".to_string(), "stream-456".to_string())
/// .with_context(request_context)
/// .record_async()
/// .await;
/// ```
pub struct AuditLogger<R: AuditEventRepository> {
    repository: Arc<R>,
}

impl<R: AuditEventRepository> AuditLogger<R> {
    /// Create a new AuditLogger with the given repository
    pub fn new(repository: Arc<R>) -> Self {
        Self { repository }
    }

    /// Start building an audit log entry
    pub fn log(
        &self,
        tenant_id: TenantId,
        action: AuditAction,
        actor: Actor,
    ) -> AuditLogEntry<'_, R> {
        AuditLogEntry {
            logger: self,
            builder: AuditLogBuilder::new(tenant_id, action, actor),
        }
    }

    /// Log a successful event (convenience method)
    pub async fn log_success(
        &self,
        tenant_id: TenantId,
        action: AuditAction,
        actor: Actor,
    ) -> Result<(), AllSourceError> {
        let event = AuditEvent::new(tenant_id, action, actor, AuditOutcome::Success);
        self.repository.append(event).await
    }

    /// Log a failed event (convenience method)
    pub async fn log_failure(
        &self,
        tenant_id: TenantId,
        action: AuditAction,
        actor: Actor,
        error_message: String,
    ) -> Result<(), AllSourceError> {
        let event = AuditEvent::new(tenant_id, action, actor, AuditOutcome::Failure)
            .with_error(error_message);
        self.repository.append(event).await
    }

    /// Log an event with resource information (convenience method)
    pub async fn log_resource_action(
        &self,
        tenant_id: TenantId,
        action: AuditAction,
        actor: Actor,
        resource_type: String,
        resource_id: String,
        outcome: AuditOutcome,
    ) -> Result<(), AllSourceError> {
        let event = AuditEvent::new(tenant_id, action, actor, outcome)
            .with_resource(resource_type, resource_id);
        self.repository.append(event).await
    }

    /// Record an event without returning an error (logs errors internally)
    /// This is useful for middleware where audit failures shouldn't break requests
    pub async fn record_silently(&self, event: AuditEvent) {
        if let Err(e) = self.repository.append(event).await {
            error!("Failed to record audit event: {}", e);
        }
    }

    /// Batch log multiple events
    pub async fn log_batch(&self, events: Vec<AuditEvent>) -> Result<(), AllSourceError> {
        self.repository.append_batch(events).await
    }

    /// Batch log multiple events without returning an error
    pub async fn log_batch_silently(&self, events: Vec<AuditEvent>) {
        if let Err(e) = self.repository.append_batch(events).await {
            error!("Failed to record audit event batch: {}", e);
        }
    }
}

/// Builder for a single audit log entry
pub struct AuditLogEntry<'a, R: AuditEventRepository> {
    logger: &'a AuditLogger<R>,
    builder: AuditLogBuilder,
}

impl<R: AuditEventRepository> AuditLogEntry<'_, R> {
    pub fn with_outcome(mut self, outcome: AuditOutcome) -> Self {
        self.builder = self.builder.with_outcome(outcome);
        self
    }

    pub fn with_resource(mut self, resource_type: String, resource_id: String) -> Self {
        self.builder = self.builder.with_resource(resource_type, resource_id);
        self
    }

    pub fn with_context(mut self, context: RequestContext) -> Self {
        self.builder = self.builder.with_context(context);
        self
    }

    pub fn with_ip_address(mut self, ip: String) -> Self {
        self.builder = self.builder.with_ip_address(ip);
        self
    }

    pub fn with_user_agent(mut self, user_agent: String) -> Self {
        self.builder = self.builder.with_user_agent(user_agent);
        self
    }

    pub fn with_request_id(mut self, request_id: String) -> Self {
        self.builder = self.builder.with_request_id(request_id);
        self
    }

    pub fn with_error(mut self, error_message: String) -> Self {
        self.builder = self.builder.with_error(error_message);
        self
    }

    pub fn with_metadata(mut self, metadata: JsonValue) -> Self {
        self.builder = self.builder.with_metadata(metadata);
        self
    }

    /// Record the audit event
    pub async fn record(self) -> Result<(), AllSourceError> {
        let event = self.builder.build();
        self.logger.repository.append(event).await
    }

    /// Record the audit event silently (logs errors instead of returning them)
    pub async fn record_silently(self) {
        let event = self.builder.build();
        self.logger.record_silently(event).await;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        domain::entities::AuditAction, infrastructure::repositories::InMemoryAuditRepository,
    };

    fn setup_logger() -> AuditLogger<InMemoryAuditRepository> {
        let repo = Arc::new(InMemoryAuditRepository::new());
        AuditLogger::new(repo)
    }

    fn test_tenant_id() -> TenantId {
        TenantId::new("test-tenant".to_string()).unwrap()
    }

    fn test_actor() -> Actor {
        Actor::user("user-123".to_string(), "john-doe".to_string())
    }

    #[tokio::test]
    async fn test_audit_logger_creation() {
        let logger = setup_logger();
        // Logger should be created successfully - verify we can log
        let result = logger
            .log_success(test_tenant_id(), AuditAction::Login, test_actor())
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_log_success() {
        let logger = setup_logger();
        let result = logger
            .log_success(test_tenant_id(), AuditAction::Login, test_actor())
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_log_failure() {
        let logger = setup_logger();
        let result = logger
            .log_failure(
                test_tenant_id(),
                AuditAction::LoginFailed,
                test_actor(),
                "Invalid credentials".to_string(),
            )
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_log_with_resource() {
        let logger = setup_logger();
        let result = logger
            .log_resource_action(
                test_tenant_id(),
                AuditAction::EventIngested,
                Actor::api_key("key-123".to_string(), "prod-api-key".to_string()),
                "event_stream".to_string(),
                "stream-456".to_string(),
                AuditOutcome::Success,
            )
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_builder_api() {
        let logger = setup_logger();

        let result = logger
            .log(
                test_tenant_id(),
                AuditAction::EventIngested,
                Actor::api_key("key-123".to_string(), "prod-api-key".to_string()),
            )
            .with_resource("event_stream".to_string(), "stream-456".to_string())
            .with_ip_address("192.168.1.1".to_string())
            .with_request_id("req-789".to_string())
            .record()
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_builder_with_context() {
        let logger = setup_logger();

        let context = RequestContext::new()
            .with_ip("10.0.0.1".to_string())
            .with_user_agent("Mozilla/5.0".to_string())
            .with_request_id("req-abc".to_string());

        let result = logger
            .log(test_tenant_id(), AuditAction::Login, test_actor())
            .with_context(context)
            .record()
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_builder_with_error() {
        let logger = setup_logger();

        let result = logger
            .log(
                test_tenant_id(),
                AuditAction::PermissionDenied,
                test_actor(),
            )
            .with_error("Insufficient permissions".to_string())
            .record()
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_builder_with_metadata() {
        let logger = setup_logger();

        let metadata = serde_json::json!({
            "reason": "rate_limit",
            "limit": 100,
            "current": 150
        });

        let result = logger
            .log(
                test_tenant_id(),
                AuditAction::RateLimitExceeded,
                test_actor(),
            )
            .with_metadata(metadata)
            .record()
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_record_silently() {
        let logger = setup_logger();

        let event = AuditEvent::new(
            test_tenant_id(),
            AuditAction::Login,
            test_actor(),
            AuditOutcome::Success,
        );

        // This should never panic, even if there's an error
        logger.record_silently(event).await;
    }

    #[tokio::test]
    async fn test_batch_logging() {
        let logger = setup_logger();

        let events = vec![
            AuditEvent::new(
                test_tenant_id(),
                AuditAction::Login,
                test_actor(),
                AuditOutcome::Success,
            ),
            AuditEvent::new(
                test_tenant_id(),
                AuditAction::EventIngested,
                Actor::api_key("key-123".to_string(), "prod-api-key".to_string()),
                AuditOutcome::Success,
            ),
        ];

        let result = logger.log_batch(events).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_batch_logging_silently() {
        let logger = setup_logger();

        let events = vec![AuditEvent::new(
            test_tenant_id(),
            AuditAction::Login,
            test_actor(),
            AuditOutcome::Success,
        )];

        // This should never panic
        logger.log_batch_silently(events).await;
    }

    #[tokio::test]
    async fn test_request_context_builder() {
        let context = RequestContext::new()
            .with_ip("192.168.1.1".to_string())
            .with_user_agent("curl/7.64.1".to_string())
            .with_request_id("req-123".to_string());

        assert_eq!(context.ip_address, Some("192.168.1.1".to_string()));
        assert_eq!(context.user_agent, Some("curl/7.64.1".to_string()));
        assert_eq!(context.request_id, Some("req-123".to_string()));
    }
}