acton-service 0.29.0

Production-ready Rust backend framework with type-enforced API versioning
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Audit-emitting decorators for auth storage backends.
//!
//! The framework declares audit event kinds for token refresh, API-key
//! lifecycle, and OAuth callbacks, but the flows they describe run through
//! storage traits ([`RefreshTokenStorage`], [`ApiKeyStorage`]) and the
//! [`OAuthProvider`] trait — implemented once per backend and constructed by
//! the application, not by `ServiceBuilder`. Emitting inside each backend
//! would duplicate the same lines four times per family, so emission lives in
//! these wrappers instead: wrap whatever you construct, hand it the
//! [`AuditLogger`] from `AppState::audit_logger()`, and every backend emits
//! the same events (issue #16).
//!
//! ```rust,ignore
//! let storage = RedisRefreshStorage::new(pool);
//! let storage = AuditedRefreshStorage::new(storage, logger.clone());
//! ```
//!
//! All emissions honor `[audit] audit_auth_events` like the built-in auth
//! middleware events.

use async_trait::async_trait;
use chrono::{DateTime, Utc};

use crate::auth::api_keys::{ApiKey, ApiKeyStorage};
#[cfg(feature = "oauth")]
use crate::auth::oauth::provider::{OAuthProvider, OAuthTokens, OAuthUserInfo};
use crate::auth::tokens::refresh::{
    RefreshTokenData, RefreshTokenMetadata, RefreshTokenStorage,
};
use crate::error::Error;

use super::event::{AuditEvent, AuditEventKind, AuditSeverity, AuditSource};
use super::logger::AuditLogger;

/// Server-side operations have no request context; the source carries only
/// the subject (and, for refresh rotation, whatever the stored token metadata
/// captured at issuance).
fn subject_source(subject: Option<String>) -> AuditSource {
    AuditSource {
        ip: None,
        user_agent: None,
        subject,
        request_id: None,
    }
}

async fn emit(
    logger: &AuditLogger,
    kind: AuditEventKind,
    severity: AuditSeverity,
    source: AuditSource,
    metadata: serde_json::Value,
) {
    if !logger.config().audit_auth_events {
        return;
    }
    let event = AuditEvent::new(kind, severity, logger.service_name().to_string())
        .with_source(source)
        .with_metadata(metadata);
    logger.log(event).await;
}

/// [`RefreshTokenStorage`] wrapper that emits `AuthTokenRefresh` on every
/// successful rotation.
///
/// Only `rotate` emits: `store` is initial issuance (part of login, already
/// audited by the auth middleware) and revocations have their own event kinds.
pub struct AuditedRefreshStorage<S> {
    inner: S,
    logger: AuditLogger,
}

impl<S: RefreshTokenStorage> AuditedRefreshStorage<S> {
    /// Wrap a refresh-token storage backend.
    pub fn new(inner: S, logger: AuditLogger) -> Self {
        Self { inner, logger }
    }
}

#[async_trait]
impl<S: RefreshTokenStorage> RefreshTokenStorage for AuditedRefreshStorage<S> {
    async fn store(
        &self,
        token_id: &str,
        user_id: &str,
        family_id: &str,
        expires_at: DateTime<Utc>,
        metadata: &RefreshTokenMetadata,
    ) -> Result<(), Error> {
        self.inner
            .store(token_id, user_id, family_id, expires_at, metadata)
            .await
    }

    async fn get(&self, token_id: &str) -> Result<Option<RefreshTokenData>, Error> {
        self.inner.get(token_id).await
    }

    async fn revoke(&self, token_id: &str) -> Result<(), Error> {
        self.inner.revoke(token_id).await
    }

    async fn revoke_family(&self, family_id: &str) -> Result<u64, Error> {
        self.inner.revoke_family(family_id).await
    }

    async fn revoke_all_for_user(&self, user_id: &str) -> Result<u64, Error> {
        self.inner.revoke_all_for_user(user_id).await
    }

    async fn rotate(
        &self,
        old_token_id: &str,
        new_token_id: &str,
        user_id: &str,
        family_id: &str,
        expires_at: DateTime<Utc>,
        metadata: &RefreshTokenMetadata,
    ) -> Result<(), Error> {
        self.inner
            .rotate(
                old_token_id,
                new_token_id,
                user_id,
                family_id,
                expires_at,
                metadata,
            )
            .await?;

        let source = AuditSource {
            ip: metadata.ip_address.clone(),
            user_agent: metadata.user_agent.clone(),
            subject: Some(user_id.to_string()),
            request_id: None,
        };
        emit(
            &self.logger,
            AuditEventKind::AuthTokenRefresh,
            AuditSeverity::Notice,
            source,
            serde_json::json!({
                "family_id": family_id,
                "old_token_id": old_token_id,
                "new_token_id": new_token_id,
            }),
        )
        .await;
        Ok(())
    }

    async fn cleanup_expired(&self) -> Result<u64, Error> {
        self.inner.cleanup_expired().await
    }
}

/// [`ApiKeyStorage`] wrapper that emits `AuthApiKeyCreated` and
/// `AuthApiKeyRevoked` on the respective successful operations.
///
/// Event metadata never includes the key material or its hash — only the ID,
/// display name, prefix, and scopes.
pub struct AuditedApiKeyStorage<S> {
    inner: S,
    logger: AuditLogger,
}

impl<S: ApiKeyStorage> AuditedApiKeyStorage<S> {
    /// Wrap an API-key storage backend.
    pub fn new(inner: S, logger: AuditLogger) -> Self {
        Self { inner, logger }
    }
}

#[async_trait]
impl<S: ApiKeyStorage> ApiKeyStorage for AuditedApiKeyStorage<S> {
    async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error> {
        self.inner.get_by_key(key).await
    }

    async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error> {
        self.inner.get_by_prefix(prefix).await
    }

    async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error> {
        self.inner.get_by_id(id).await
    }

    async fn create(&self, key: &ApiKey) -> Result<(), Error> {
        self.inner.create(key).await?;

        emit(
            &self.logger,
            AuditEventKind::AuthApiKeyCreated,
            AuditSeverity::Notice,
            subject_source(Some(key.user_id.clone())),
            serde_json::json!({
                "key_id": key.id,
                "name": key.name,
                "prefix": key.prefix,
                "scopes": key.scopes,
            }),
        )
        .await;
        Ok(())
    }

    async fn update_last_used(&self, id: &str) -> Result<(), Error> {
        self.inner.update_last_used(id).await
    }

    async fn revoke(&self, id: &str) -> Result<(), Error> {
        self.inner.revoke(id).await?;

        emit(
            &self.logger,
            AuditEventKind::AuthApiKeyRevoked,
            AuditSeverity::Notice,
            subject_source(None),
            serde_json::json!({ "key_id": id }),
        )
        .await;
        Ok(())
    }

    async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error> {
        self.inner.list_by_user(user_id).await
    }

    async fn delete(&self, id: &str) -> Result<(), Error> {
        self.inner.delete(id).await
    }
}

/// [`OAuthProvider`] wrapper that emits `AuthOAuthCallback` when an
/// authorization code is exchanged — the framework-visible moment of the
/// OAuth callback — on both success and failure.
#[cfg(feature = "oauth")]
pub struct AuditedOAuthProvider<P> {
    inner: P,
    logger: AuditLogger,
}

#[cfg(feature = "oauth")]
impl<P: OAuthProvider> AuditedOAuthProvider<P> {
    /// Wrap an OAuth provider.
    pub fn new(inner: P, logger: AuditLogger) -> Self {
        Self { inner, logger }
    }
}

#[cfg(feature = "oauth")]
#[async_trait]
impl<P: OAuthProvider> OAuthProvider for AuditedOAuthProvider<P> {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn authorization_url(&self, state: &str, scopes: &[String]) -> String {
        self.inner.authorization_url(state, scopes)
    }

    async fn exchange_code(&self, code: &str) -> Result<OAuthTokens, Error> {
        match self.inner.exchange_code(code).await {
            Ok(tokens) => {
                emit(
                    &self.logger,
                    AuditEventKind::AuthOAuthCallback,
                    AuditSeverity::Notice,
                    subject_source(None),
                    serde_json::json!({
                        "provider": self.inner.name(),
                        "outcome": "success",
                    }),
                )
                .await;
                Ok(tokens)
            }
            Err(e) => {
                emit(
                    &self.logger,
                    AuditEventKind::AuthOAuthCallback,
                    AuditSeverity::Warning,
                    subject_source(None),
                    serde_json::json!({
                        "provider": self.inner.name(),
                        "outcome": "failure",
                        "error": e.to_string(),
                    }),
                )
                .await;
                Err(e)
            }
        }
    }

    async fn get_user_info(&self, access_token: &str) -> Result<OAuthUserInfo, Error> {
        self.inner.get_user_info(access_token).await
    }

    async fn refresh_token(&self, refresh_token: &str) -> Result<OAuthTokens, Error> {
        self.inner.refresh_token(refresh_token).await
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    use acton_reactive::prelude::*;
    use async_trait::async_trait;
    use chrono::Utc;

    use super::*;
    use crate::audit::agent::AuditAgent;
    use crate::audit::config::{AuditConfig, SyslogConfig};
    use crate::audit::storage::AuditStorage;

    /// AuditStorage that records every appended event for assertions.
    #[derive(Default)]
    struct CapturingAuditStorage {
        events: Mutex<Vec<AuditEvent>>,
    }

    #[async_trait]
    impl AuditStorage for CapturingAuditStorage {
        async fn append(&self, event: &AuditEvent) -> Result<(), Error> {
            self.events.lock().expect("events lock").push(event.clone());
            Ok(())
        }

        async fn latest(&self) -> Result<Option<AuditEvent>, Error> {
            Ok(None)
        }

        async fn query_range(
            &self,
            _from: chrono::DateTime<Utc>,
            _to: chrono::DateTime<Utc>,
            _limit: usize,
        ) -> Result<Vec<AuditEvent>, Error> {
            Ok(Vec::new())
        }

        async fn verify_chain(&self, _from_sequence: u64) -> Result<Option<u64>, Error> {
            Ok(None)
        }
    }

    /// Spawn a real audit agent backed by the capturing storage. The runtime
    /// must stay alive for the agent to process events, so it is returned
    /// alongside the logger.
    async fn capturing_logger() -> (ActorRuntime, Arc<CapturingAuditStorage>, AuditLogger) {
        let mut runtime = ActonApp::launch_async().await;
        let storage = Arc::new(CapturingAuditStorage::default());
        let config = AuditConfig {
            syslog: SyslogConfig {
                transport: "none".to_string(),
                ..SyslogConfig::default()
            },
            ..AuditConfig::default()
        };
        let handle = AuditAgent::spawn(
            &mut runtime,
            config.clone(),
            Some(storage.clone() as Arc<dyn AuditStorage>),
            "test-svc".to_string(),
        )
        .await
        .expect("spawn audit agent");
        let logger = AuditLogger::new(handle, "test-svc".to_string(), config);

        // No chain-readiness probe needed: the agent buffers events emitted
        // before `ChainLoaded` and drains them in order once the chain exists.
        (runtime, storage, logger)
    }

    /// Fire-and-forget emission: poll until the expected kind shows up.
    async fn wait_for_event(
        storage: &CapturingAuditStorage,
        kind: &AuditEventKind,
    ) -> Option<AuditEvent> {
        for _ in 0..50 {
            if let Some(event) = storage
                .events
                .lock()
                .expect("events lock")
                .iter()
                .find(|e| &e.kind == kind)
                .cloned()
            {
                return Some(event);
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
        None
    }

    #[derive(Default)]
    struct NoopRefreshStorage;

    #[async_trait]
    impl RefreshTokenStorage for NoopRefreshStorage {
        async fn store(
            &self,
            _token_id: &str,
            _user_id: &str,
            _family_id: &str,
            _expires_at: chrono::DateTime<Utc>,
            _metadata: &RefreshTokenMetadata,
        ) -> Result<(), Error> {
            Ok(())
        }

        async fn get(&self, _token_id: &str) -> Result<Option<RefreshTokenData>, Error> {
            Ok(None)
        }

        async fn revoke(&self, _token_id: &str) -> Result<(), Error> {
            Ok(())
        }

        async fn revoke_family(&self, _family_id: &str) -> Result<u64, Error> {
            Ok(0)
        }

        async fn revoke_all_for_user(&self, _user_id: &str) -> Result<u64, Error> {
            Ok(0)
        }

        async fn rotate(
            &self,
            _old_token_id: &str,
            _new_token_id: &str,
            _user_id: &str,
            _family_id: &str,
            _expires_at: chrono::DateTime<Utc>,
            _metadata: &RefreshTokenMetadata,
        ) -> Result<(), Error> {
            Ok(())
        }

        async fn cleanup_expired(&self) -> Result<u64, Error> {
            Ok(0)
        }
    }

    /// Refresh storage whose rotate always fails, to prove no event is
    /// emitted for failed rotations.
    #[derive(Default)]
    struct FailingRefreshStorage;

    #[async_trait]
    impl RefreshTokenStorage for FailingRefreshStorage {
        async fn store(
            &self,
            _token_id: &str,
            _user_id: &str,
            _family_id: &str,
            _expires_at: chrono::DateTime<Utc>,
            _metadata: &RefreshTokenMetadata,
        ) -> Result<(), Error> {
            Ok(())
        }

        async fn get(&self, _token_id: &str) -> Result<Option<RefreshTokenData>, Error> {
            Ok(None)
        }

        async fn revoke(&self, _token_id: &str) -> Result<(), Error> {
            Ok(())
        }

        async fn revoke_family(&self, _family_id: &str) -> Result<u64, Error> {
            Ok(0)
        }

        async fn revoke_all_for_user(&self, _user_id: &str) -> Result<u64, Error> {
            Ok(0)
        }

        async fn rotate(
            &self,
            _old_token_id: &str,
            _new_token_id: &str,
            _user_id: &str,
            _family_id: &str,
            _expires_at: chrono::DateTime<Utc>,
            _metadata: &RefreshTokenMetadata,
        ) -> Result<(), Error> {
            Err(Error::Internal("rotation failed".to_string()))
        }

        async fn cleanup_expired(&self) -> Result<u64, Error> {
            Ok(0)
        }
    }

    #[derive(Default)]
    struct NoopApiKeyStorage;

    #[async_trait]
    impl ApiKeyStorage for NoopApiKeyStorage {
        async fn get_by_key(&self, _key: &str) -> Result<Option<ApiKey>, Error> {
            Ok(None)
        }

        async fn get_by_prefix(&self, _prefix: &str) -> Result<Option<ApiKey>, Error> {
            Ok(None)
        }

        async fn get_by_id(&self, _id: &str) -> Result<Option<ApiKey>, Error> {
            Ok(None)
        }

        async fn create(&self, _key: &ApiKey) -> Result<(), Error> {
            Ok(())
        }

        async fn update_last_used(&self, _id: &str) -> Result<(), Error> {
            Ok(())
        }

        async fn revoke(&self, _id: &str) -> Result<(), Error> {
            Ok(())
        }

        async fn list_by_user(&self, _user_id: &str) -> Result<Vec<ApiKey>, Error> {
            Ok(Vec::new())
        }

        async fn delete(&self, _id: &str) -> Result<(), Error> {
            Ok(())
        }
    }

    fn test_api_key() -> ApiKey {
        ApiKey {
            id: "key_123".to_string(),
            user_id: "user_9".to_string(),
            name: "ci deploy key".to_string(),
            prefix: "sk_test".to_string(),
            key_hash: "not-logged".to_string(),
            scopes: vec!["deploy".to_string()],
            rate_limit: None,
            is_revoked: false,
            last_used_at: None,
            expires_at: None,
            created_at: Utc::now(),
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn rotate_emits_auth_token_refresh_with_stored_metadata() {
        let (_runtime, storage, logger) = capturing_logger().await;
        let wrapped = AuditedRefreshStorage::new(NoopRefreshStorage, logger);

        let metadata = RefreshTokenMetadata {
            ip_address: Some("203.0.113.5".to_string()),
            user_agent: Some("acton-test/1.0".to_string()),
            ..RefreshTokenMetadata::default()
        };
        wrapped
            .rotate("old_jti", "new_jti", "user_9", "fam_1", Utc::now(), &metadata)
            .await
            .expect("rotate");

        let event = wait_for_event(&storage, &AuditEventKind::AuthTokenRefresh)
            .await
            .expect("AuthTokenRefresh emitted");
        assert_eq!(event.source.subject.as_deref(), Some("user_9"));
        assert_eq!(event.source.ip.as_deref(), Some("203.0.113.5"));
        let metadata = event.metadata.expect("metadata attached");
        assert_eq!(metadata["family_id"], "fam_1");
        assert_eq!(metadata["old_token_id"], "old_jti");
        assert_eq!(metadata["new_token_id"], "new_jti");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn failed_rotate_emits_nothing() {
        let (_runtime, storage, logger) = capturing_logger().await;
        let wrapped = AuditedRefreshStorage::new(FailingRefreshStorage, logger);

        let result = wrapped
            .rotate(
                "old_jti",
                "new_jti",
                "user_9",
                "fam_1",
                Utc::now(),
                &RefreshTokenMetadata::default(),
            )
            .await;
        assert!(result.is_err(), "inner failure must propagate");

        // Give the fire-and-forget path a moment to (incorrectly) deliver.
        tokio::time::sleep(Duration::from_millis(300)).await;
        assert!(
            storage.events.lock().expect("events lock").is_empty(),
            "failed rotation must not be audited as a refresh"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn api_key_create_and_revoke_emit_without_key_material() {
        let (_runtime, storage, logger) = capturing_logger().await;
        let wrapped = AuditedApiKeyStorage::new(NoopApiKeyStorage, logger);

        wrapped.create(&test_api_key()).await.expect("create");
        let created = wait_for_event(&storage, &AuditEventKind::AuthApiKeyCreated)
            .await
            .expect("AuthApiKeyCreated emitted");
        let metadata = created.metadata.expect("metadata attached");
        assert_eq!(metadata["key_id"], "key_123");
        assert!(
            !metadata.to_string().contains("not-logged"),
            "key hash must never reach the audit log"
        );
        assert_eq!(created.source.subject.as_deref(), Some("user_9"));

        wrapped.revoke("key_123").await.expect("revoke");
        let revoked = wait_for_event(&storage, &AuditEventKind::AuthApiKeyRevoked)
            .await
            .expect("AuthApiKeyRevoked emitted");
        assert_eq!(revoked.metadata.expect("metadata")["key_id"], "key_123");
    }

    #[cfg(feature = "oauth")]
    mod oauth_tests {
        use super::*;
        use crate::auth::oauth::provider::{OAuthProvider, OAuthTokens, OAuthUserInfo};

        struct FailingProvider;

        #[async_trait]
        impl OAuthProvider for FailingProvider {
            fn name(&self) -> &str {
                "test-provider"
            }

            fn authorization_url(&self, _state: &str, _scopes: &[String]) -> String {
                "https://example.com/auth".to_string()
            }

            async fn exchange_code(&self, _code: &str) -> Result<OAuthTokens, Error> {
                Err(Error::Internal("exchange rejected".to_string()))
            }

            async fn get_user_info(&self, _access_token: &str) -> Result<OAuthUserInfo, Error> {
                Err(Error::Internal("unused".to_string()))
            }

            async fn refresh_token(&self, _refresh_token: &str) -> Result<OAuthTokens, Error> {
                Err(Error::Internal("unused".to_string()))
            }
        }

        #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
        async fn failed_code_exchange_emits_oauth_callback_warning() {
            let (_runtime, storage, logger) = capturing_logger().await;
            let wrapped = AuditedOAuthProvider::new(FailingProvider, logger);

            let result = wrapped.exchange_code("bad-code").await;
            assert!(result.is_err(), "inner failure must propagate");

            let event = wait_for_event(&storage, &AuditEventKind::AuthOAuthCallback)
                .await
                .expect("AuthOAuthCallback emitted on failure");
            assert_eq!(event.severity, AuditSeverity::Warning);
            let metadata = event.metadata.expect("metadata attached");
            assert_eq!(metadata["provider"], "test-provider");
            assert_eq!(metadata["outcome"], "failure");
        }
    }
}