fortress-api-server 1.0.3

REST API server for Fortress secure database system
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
//! Integration tests for the Fortress REST API
//!
//! This module tests the complete API functionality including
//! authentication, data storage, retrieval, and key management.

use fortress_api_server::prelude::*;
use serde_json::json;
use axum::body::Body;
use http::{Request, StatusCode};
use tower::ServiceExt;
use uuid::Uuid;

fn default_test_config() -> ServerConfig {
    let mut config = ServerConfig::default();
    config.core.storage.backend = "in_memory".to_string();
    config.features.bootstrap_default_admin = true;
    config
}

async fn login_access_token(app: &axum::Router) -> String {
    let body = serde_json::json!({
        "username": "admin",
        "password": "admin123"
    });
    let (status, json) = request_json(
        app.clone(),
        Request::builder()
            .method("POST")
            .uri("/api/v1/auth/login")
            .header("content-type", "application/json")
            .body(Body::from(body.to_string()))
            .unwrap(),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "login response: {json}");
    json["data"]["access_token"]
        .as_str()
        .expect("access_token in login response")
        .to_string()
}

fn auth_request(builder: http::request::Builder, token: &str) -> Request<Body> {
    builder
        .header("authorization", format!("Bearer {token}"))
        .body(Body::empty())
        .unwrap()
}

struct AuthedRouter {
    router: axum::Router,
    token: String,
}

impl AuthedRouter {
    async fn new() -> Self {
        let config = default_test_config();
        let server = FortressServer::new(config).await.unwrap();
        let router = server.router().await.unwrap();
        let token = login_access_token(&router).await;
        Self { router, token }
    }

    fn get(&self, uri: impl AsRef<str>) -> Request<Body> {
        auth_request(
            Request::builder().method("GET").uri(uri.as_ref()),
            &self.token,
        )
    }

    fn post_json(&self, uri: impl AsRef<str>, body: String) -> Request<Body> {
        Request::builder()
            .method("POST")
            .uri(uri.as_ref())
            .header("content-type", "application/json")
            .header("authorization", format!("Bearer {}", self.token))
            .body(Body::from(body))
            .unwrap()
    }

    fn delete_json(&self, uri: impl AsRef<str>, body: String) -> Request<Body> {
        Request::builder()
            .method("DELETE")
            .uri(uri.as_ref())
            .header("content-type", "application/json")
            .header("authorization", format!("Bearer {}", self.token))
            .body(Body::from(body))
            .unwrap()
    }
}

async fn request_json(app: axum::Router, req: Request<Body>) -> (StatusCode, serde_json::Value) {
    let resp = app.oneshot(req).await.unwrap();
    let status = resp.status();
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
    let json: serde_json::Value = match serde_json::from_slice(&bytes) {
        Ok(v) => v,
        Err(e) => {
            let body_preview = String::from_utf8_lossy(&bytes);
            panic!(
                "Expected JSON response but failed to parse. status={status} err={e} body={body_preview}"
            );
        }
    };
    (status, json)
}

async fn request_text(app: axum::Router, req: Request<Body>) -> (StatusCode, String) {
    let resp = app.oneshot(req).await.unwrap();
    let status = resp.status();
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
    (status, String::from_utf8(bytes.to_vec()).unwrap())
}

#[tokio::test]
async fn test_health_check() {
    let config = default_test_config();
    let server = FortressServer::new(config).await.unwrap();
    let router = server.router().await.unwrap();

    let (status, body) = request_json(
        router,
        Request::builder().method("GET").uri("/health").body(Body::empty()).unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(body["success"].as_bool().unwrap());
    assert!(body["data"].is_object());
}

#[tokio::test]
async fn test_store_and_retrieve_data() {
    let app = AuthedRouter::new().await;
    
    // Test data to store
    let test_data = json!({
        "name": "John Doe",
        "email": "john@example.com",
        "age": 30,
        "address": {
            "street": "123 Main St",
            "city": "Anytown",
            "country": "USA"
        }
    });
    
    // Store data
    let store_request = json!({
        "data": test_data,
        "metadata": {
            "source": "test",
            "version": "1.0"
        },
        "algorithm": "aegis256"
    });
    

    let (status, store_body) = request_json(
        app.router.clone(),
        app.post_json("/api/v1/data", store_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "store response: {store_body}");
    assert!(store_body["success"].as_bool().unwrap());
    
    let data_id = store_body["data"]["id"].as_str().unwrap();
    let key_id = store_body["data"]["key_id"].as_str().unwrap();
    
    // Retrieve data

    let (status, retrieve_body) = request_json(
        app.router,
        app.get(format!("/api/v1/data/{data_id}")),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "retrieve response: {retrieve_body}");
    assert!(retrieve_body["success"].as_bool().unwrap());
    
    let retrieved_data = &retrieve_body["data"]["data"];
    assert_eq!(retrieved_data["name"], "John Doe");
    assert_eq!(retrieved_data["email"], "john@example.com");
    assert_eq!(retrieved_data["age"], 30);
    
    // Verify the stored algorithm and key_id match
    assert_eq!(retrieve_body["data"]["algorithm"], "aegis256");
    assert_eq!(retrieve_body["data"]["key_id"], key_id);
}

#[tokio::test]
async fn test_list_data() {
    let app = AuthedRouter::new().await;
    
    // Store multiple items
    for i in 1..=3 {
        let test_data = json!({
            "name": format!("User {}", i),
            "index": i
        });
        
        let store_request = json!({
            "data": test_data,
            "algorithm": "aegis256"
        });
        
        let (status, _body) = request_json(
            app.router.clone(),
            app.post_json("/api/v1/data", store_request.to_string()),
        )
        .await;

        assert_eq!(status, StatusCode::OK);
    }
    
    // List data
    let (status, list_body) = request_json(app.router, app.get("/api/v1/data")).await;

    assert_eq!(status, StatusCode::OK);
    assert!(list_body["success"].as_bool().unwrap());
    
    let items = list_body["data"]["items"].as_array().unwrap();
    assert_eq!(items.len(), 3);
    assert_eq!(list_body["data"]["total_count"], 3);
    
    // Verify items are sorted by creation time (descending)
    for (_i, item) in items.iter().enumerate() {
        assert!(item["id"].is_string());
        assert!(item["key_id"].is_string());
        assert_eq!(item["algorithm"], "aegis256");
        assert!(item["size_bytes"].is_number());
    }
}

#[tokio::test]
async fn test_delete_data() {
    let app = AuthedRouter::new().await;
    
    // Store data first
    let test_data = json!({
        "name": "To Be Deleted",
        "temp": true
    });
    
    let store_request = json!({
        "data": test_data,
        "algorithm": "aegis256"
    });
    

    let (status, store_body) = request_json(
        app.router.clone(),
        app.post_json("/api/v1/data", store_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "store response: {store_body}");
    let data_id = store_body["data"]["id"].as_str().unwrap();
    
    // Delete data
    let delete_request = json!({
        "id": data_id,
        "soft_delete": false
    });
    

    let (status, delete_body) = request_json(
        app.router.clone(),
        app.delete_json(format!("/api/v1/data/{data_id}"), delete_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "delete response: {delete_body}");
    assert!(delete_body["success"].as_bool().unwrap());
    assert_eq!(delete_body["data"]["id"], data_id);
    assert_eq!(delete_body["data"]["soft_delete"], false);
    
    // Verify data is gone

    let (status, _body) = request_json(
        app.router,
        app.get(format!("/api/v1/data/{data_id}")),
    )
    .await;

    assert_eq!(status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn test_generate_key() {
    let app = AuthedRouter::new().await;
    
    // Generate key request
    let key_request = json!({
        "algorithm": "aegis256",
        "key_size": 256,
        "metadata": {
            "purpose": "test",
            "created_by": "integration_test"
        }
    });
    

    let (status, body) = request_json(
        app.router,
        app.post_json("/api/v1/keys", key_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(body["success"].as_bool().unwrap());
    
    let key_data = &body["data"];
    assert!(key_data["id"].is_string());
    assert_eq!(key_data["algorithm"], "aegis256");
    assert_eq!(key_data["key_size"], 256);
    assert!(key_data["fingerprint"].is_string());
    assert!(key_data["created_at"].is_string());
    
    // Verify fingerprint is 16 characters long (SHA256 truncated)
    let fingerprint = key_data["fingerprint"].as_str().unwrap();
    assert_eq!(fingerprint.len(), 16);
}

#[tokio::test]
async fn test_field_level_encryption() {
    let mut config = default_test_config();
    config.features.field_encryption = true;
    let server = FortressServer::new(config).await.unwrap();
    let router = server.router().await.unwrap();
    let token = login_access_token(&router).await;
    
    // Test data with sensitive fields
    let test_data = json!({
        "name": "John Doe",
        "ssn": "123-45-6789",
        "credit_card": "4111-1111-1111-1111",
        "email": "john@example.com"
    });
    
    // Field encryption configuration
    let field_config = json!({
        "fields": {
            "ssn": {
                "algorithm": "aegis256",
                "sensitivity": "high"
            },
            "credit_card": {
                "algorithm": "aegis256",
                "sensitivity": "high"
            }
        }
    });
    
    let store_request = json!({
        "data": test_data,
        "field_encryption": field_config,
        "algorithm": "aegis256"
    });

    let (status, store_body) = request_json(
        router.clone(),
        Request::builder()
            .method("POST")
            .uri("/api/v1/data")
            .header("content-type", "application/json")
            .header("authorization", format!("Bearer {token}"))
            .body(Body::from(store_request.to_string()))
            .unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(store_body["success"].as_bool().unwrap());
    
    // Verify field metadata is present
    let field_metadata = &store_body["data"]["field_metadata"];
    if field_metadata.is_object() {
        // Field encryption metadata may be empty depending on key availability / implementation
        assert!(field_metadata.as_object().is_some());
    }
    
    let data_id = store_body["data"]["id"].as_str().unwrap();
    
    // Retrieve and verify data integrity

    let (status, retrieve_body) = request_json(
        router,
        auth_request(
            Request::builder()
                .method("GET")
                .uri(format!("/api/v1/data/{data_id}")),
            &token,
        ),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(retrieve_body["success"].as_bool().unwrap());
    
    let retrieved_data = &retrieve_body["data"]["data"];
    assert_eq!(retrieved_data["name"], "John Doe");
    assert_eq!(retrieved_data["email"], "john@example.com");
}

#[tokio::test]
async fn test_authentication_flow() {
    let mut config = default_test_config();
    config.features.auth_enabled = true;
    
    let server = FortressServer::new(config).await.unwrap();
    let router = server.router().await.unwrap();
    
    // Test login with bootstrapped admin credentials
    let auth_request = json!({
        "username": "admin",
        "password": "admin123"
    });
    

    let (status, body) = request_json(
        router,
        Request::builder()
            .method("POST")
            .uri("/api/v1/auth/login")
            .header("content-type", "application/json")
            .body(Body::from(auth_request.to_string()))
            .unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(body["success"].as_bool().unwrap_or(false));
    assert!(body["data"]["access_token"].is_string());
    assert!(body["data"]["token_type"].is_string());
    assert!(body["data"]["expires_in"].is_number());
}

#[tokio::test]
async fn test_metrics_endpoint() {
    let config = default_test_config();
    let server = FortressServer::new(config).await.unwrap();
    let router = server.router().await.unwrap();

    // JSON metrics are exposed at /api/v1/metrics
    let (status, body) = request_json(
        router.clone(),
        Request::builder().method("GET").uri("/api/v1/metrics").body(Body::empty()).unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(body["metrics"].is_object());
    assert!(body["timestamp"].is_string());

    // Prometheus exposition is served as plain text on /metrics
    let (status, prometheus_body) = request_text(
        router,
        Request::builder().method("GET").uri("/metrics").body(Body::empty()).unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    assert!(!prometheus_body.is_empty());
    assert!(prometheus_body.contains('#') || prometheus_body.contains('_'));
}

#[tokio::test]
async fn test_storage_backend_integration() {
    let app = AuthedRouter::new().await;
    
    // Store and retrieve data
    let test_data = json!({
        "test": "storage_backend_integration",
        "backend": "memory"
    });
    
    let store_request = json!({
        "data": test_data
    });

    let (status, store_body) = request_json(
        app.router.clone(),
        app.post_json("/api/v1/data", store_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    let data_id = store_body["data"]["id"].as_str().unwrap();
    
    // Retrieve the data

    let (status, retrieve_body) = request_json(
        app.router,
        app.get(format!("/api/v1/data/{data_id}")),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "retrieve response: {retrieve_body}");
    assert_eq!(retrieve_body["data"]["data"]["test"], "storage_backend_integration");
}

#[tokio::test]
async fn test_error_handling() {
    let app = AuthedRouter::new().await;

    let non_existent_id = Uuid::new_v4().to_string();

    let (status, body) = request_json(
        app.router,
        app.get(format!("/api/v1/data/{non_existent_id}")),
    )
    .await;

    assert_eq!(status, StatusCode::NOT_FOUND, "error response: {body}");
    assert!(body["error"].is_object());
    assert!(body["error"]["code"].is_string());
    assert!(body["error"]["message"].is_string());
}

#[tokio::test]
async fn test_concurrent_requests() {
    let app = AuthedRouter::new().await;
    
    // Create multiple concurrent requests
    let mut handles = Vec::new();
    
    for i in 0..10 {
        let router = app.router.clone();
        let token = app.token.clone();
        let handle = tokio::spawn(async move {
            let test_data = json!({
                "id": i,
                "message": format!("Concurrent test {}", i)
            });
            
            let store_request = json!({
                "data": test_data
            });
            
            request_json(
                router,
                Request::builder()
                    .method("POST")
                    .uri("/api/v1/data")
                    .header("content-type", "application/json")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::from(store_request.to_string()))
                    .unwrap(),
            )
            .await
        });
        handles.push(handle);
    }
    
    // Wait for all requests to complete
    let results = futures::future::join_all(handles).await;
    
    // All requests should succeed
    for result in results {
        let (status, _body) = result.unwrap();
        assert_eq!(status, StatusCode::OK);
    }
}

#[tokio::test]
async fn test_large_data_handling() {
    let app = AuthedRouter::new().await;
    
    // Create large test data (within the API limit)
    // The API currently validates JSON payloads with a 100KB max size.
    let large_data = json!({
        "data": "x".repeat(80 * 1024),
        "size": 80 * 1024
    });
    
    let store_request = json!({
        "data": large_data
    });

    let (status, store_body) = request_json(
        app.router.clone(),
        app.post_json("/api/v1/data", store_request.to_string()),
    )
    .await;

    assert_eq!(status, StatusCode::OK);
    let data_id = store_body["data"]["id"].as_str().unwrap();
    
    // Retrieve the large data
    let (status, retrieve_body) = request_json(
        app.router,
        app.get(format!("/api/v1/data/{data_id}")),
    )
    .await;

    assert_eq!(status, StatusCode::OK, "retrieve response: {retrieve_body}");
    let retrieved_size = retrieve_body["data"]["data"]["size"].as_u64().unwrap();
    assert_eq!(retrieved_size, 80 * 1024);
}

#[tokio::test]
async fn test_unauthenticated_data_access_rejected() {
    let config = default_test_config();
    let server = FortressServer::new(config).await.unwrap();
    let router = server.router().await.unwrap();

    let (status, _) = request_json(
        router,
        Request::builder()
            .method("GET")
            .uri("/api/v1/data")
            .body(Body::empty())
            .unwrap(),
    )
    .await;

    assert_eq!(status, StatusCode::UNAUTHORIZED);
}