fortress-api-server 1.0.0

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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! GraphQL query handlers
//!
//! Implements all GraphQL queries for database operations, data retrieval,
//! and system information.

use crate::graphql::{
    context::from_context,
    types::*,
    context::AuthenticatedUser,
};
use async_graphql::{Result, Object, Context};
use chrono::Utc;
use std::collections::HashMap;
use fortress_core::{
    key::KeyManager,
    field_encryption::FieldIdentifier,
    secrets::SecretsEngine,
};
use serde_json;
use uuid::Uuid;

/// GraphQL query root
pub struct Query;

#[Object]
impl Query {
    // ==================== Database Queries ====================

    /// Get all databases
    async fn databases(&self, ctx: &Context<'_>) -> Result<Vec<Database>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // List all database metadata entries
        let db_keys = storage.list_prefix("db:").await
            .map_err(|e| async_graphql::Error::new(format!("Database listing failed: {}", e)))?;
        
        let mut databases = Vec::new();
        
        for key in db_keys {
            if let Some(data) = storage.get(&key).await
                .map_err(|e| async_graphql::Error::new(format!("Database data retrieval failed: {}", e)))? {
                
                if let Ok(db_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    if let (Some(name), Some(status), Some(algorithm), Some(created_at)) = (
                        db_info.get("name").and_then(|v| v.as_str()),
                        db_info.get("status").and_then(|v| v.as_str()),
                        db_info.get("encryption_algorithm").and_then(|v| v.as_str()),
                        db_info.get("created_at").and_then(|v| v.as_str())
                    ) {
                        let db = Database {
                            id: db_info.get("id").and_then(|v| v.as_str())
                                .map(|s| s.parse().unwrap_or_else(|_| Uuid::new_v4()))
                                .unwrap_or_else(|| Uuid::new_v4()).to_string(),
                            name: name.to_string(),
                            description: db_info.get("description").and_then(|v| v.as_str()).map(String::from),
                            status: match status {
                                "active" => DatabaseStatus::Active,
                                "creating" => DatabaseStatus::Creating,
                                "deleting" => DatabaseStatus::Deleting,
                                "maintenance" => DatabaseStatus::Maintenance,
                                "archived" => DatabaseStatus::Archived,
                                _ => DatabaseStatus::Active,
                            },
                            encryption_algorithm: match algorithm {
                                "AEGIS256" => EncryptionAlgorithm::Aegis256,
                                "AES256GCM" => EncryptionAlgorithm::Aes256Gcm,
                                "CHACHA20POLY1305" => EncryptionAlgorithm::ChaCha20Poly1305,
                                _ => EncryptionAlgorithm::Aegis256,
                            },
                            created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                            updated_at: db_info.get("updated_at").and_then(|v| v.as_str())
                                .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                            tags: db_info.get("tags")
                                .and_then(|v| v.as_array())
                                .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                                .unwrap_or_default(),
                            table_count: db_info.get("table_count").and_then(|v| v.as_u64()).unwrap_or(0) as i32,
                            storage_size_bytes: db_info.get("storage_size_bytes").and_then(|v| v.as_u64()).unwrap_or(0) as i64,
                        };
                        databases.push(db);
                    }
                }
            }
        }
        
        Ok(databases)
    }

    /// Get a specific database by name
    async fn database(&self, ctx: &Context<'_>, name: String) -> Result<Option<Database>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Look up database by name
        let db_key = format!("db:{}", name);
        
        if let Some(data) = storage.get(&db_key).await
            .map_err(|e| async_graphql::Error::new(format!("Database retrieval failed: {}", e)))? {
            
            if let Ok(db_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                if let (Some(status), Some(algorithm), Some(created_at)) = (
                    db_info.get("status").and_then(|v| v.as_str()),
                    db_info.get("encryption_algorithm").and_then(|v| v.as_str()),
                    db_info.get("created_at").and_then(|v| v.as_str())
                ) {
                    let default_id = Uuid::new_v4().to_string();
                    let db = Database {
                        id: db_info.get("id").and_then(|v| v.as_str())
                            .unwrap_or(&default_id)
                            .parse().unwrap_or_else(|_| Uuid::new_v4()).to_string(),
                        name: name.clone(),
                        description: db_info.get("description").and_then(|v| v.as_str()).map(String::from),
                        status: match status {
                            "active" => DatabaseStatus::Active,
                            "creating" => DatabaseStatus::Creating,
                            "deleting" => DatabaseStatus::Deleting,
                            "maintenance" => DatabaseStatus::Maintenance,
                            "archived" => DatabaseStatus::Archived,
                            _ => DatabaseStatus::Active,
                        },
                        encryption_algorithm: match algorithm {
                            "AEGIS256" => EncryptionAlgorithm::Aegis256,
                            "AES256GCM" => EncryptionAlgorithm::Aes256Gcm,
                            "CHACHA20POLY1305" => EncryptionAlgorithm::ChaCha20Poly1305,
                            _ => EncryptionAlgorithm::Aegis256,
                        },
                        created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                        updated_at: db_info.get("updated_at").and_then(|v| v.as_str())
                            .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                        tags: db_info.get("tags")
                            .and_then(|v| v.as_array())
                            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                            .unwrap_or_default(),
                        table_count: db_info.get("table_count").and_then(|v| v.as_u64()).unwrap_or(0) as i32,
                        storage_size_bytes: db_info.get("storage_size_bytes").and_then(|v| v.as_u64()).unwrap_or(0) as i64,
                    };
                    return Ok(Some(db));
                }
            }
        }
        
        Ok(None)
    }

    // ==================== Table Queries ====================

    /// Get all tables in a database
    async fn tables(&self, ctx: &Context<'_>, database: String) -> Result<Vec<Table>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // List all table metadata entries for this database
        let table_prefix = format!("db:{}:table:", database);
        let table_keys = storage.list_prefix(&table_prefix).await
            .map_err(|e| async_graphql::Error::new(format!("Table listing failed: {}", e)))?;
        
        let mut tables = Vec::new();
        
        for key in table_keys {
            if let Some(data) = storage.get(&key).await
                .map_err(|e| async_graphql::Error::new(format!("Table data retrieval failed: {}", e)))? {
                
                if let Ok(table_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    if let (Some(name), Some(created_at)) = (
                        table_info.get("name").and_then(|v| v.as_str()),
                        table_info.get("created_at").and_then(|v| v.as_str())
                    ) {
                        let table = Table {
                            id: table_info.get("id").and_then(|v| v.as_str())
                                .map(|s| s.to_string())
                                .map(|s| s.parse().unwrap_or_else(|_| Uuid::new_v4()))
                                .unwrap_or_else(|| Uuid::new_v4()).to_string(),
                            name: name.to_string(),
                            database: database.clone(),
                            description: table_info.get("description").and_then(|v| v.as_str()).map(String::from),
                            // Parse fields from table_info
                            fields: if let Some(fields_array) = table_info.get("fields").and_then(|v| v.as_array()) {
                                fields_array.iter().filter_map(|field| {
                                    if let (Some(name), Some(field_type_str)) = (
                                        field.get("name").and_then(|v| v.as_str()),
                                        field.get("field_type").and_then(|v| v.as_str())
                                    ) {
                                        let field_type = match field_type_str {
                                            "Text" => crate::graphql::types::FieldType::Text,
                                            "Integer" => crate::graphql::types::FieldType::Integer,
                                            "Float" => crate::graphql::types::FieldType::Float,
                                            "Boolean" => crate::graphql::types::FieldType::Boolean,
                                            "DateTime" => crate::graphql::types::FieldType::DateTime,
                                            "Date" => crate::graphql::types::FieldType::DateTime,
                                            "Json" => crate::graphql::types::FieldType::Json,
                                            "Binary" => crate::graphql::types::FieldType::Binary,
                                            _ => crate::graphql::types::FieldType::Text,
                                        };
                                        Some(crate::graphql::types::Field {
                                            name: name.to_string(),
                                            field_type,
                                            required: field.get("required").and_then(|v| v.as_bool()).unwrap_or(false),
                                            description: field.get("description").and_then(|v| v.as_str()).map(String::from),
                                            default_value: field.get("default_value").and_then(|v| v.as_str()).map(String::from),
                                            encryption_algorithm: field.get("encryption_algorithm").and_then(|v| v.as_str()).and_then(|alg| match alg {
                                                "Aegis256" => Some(crate::graphql::types::EncryptionAlgorithm::Aegis256),
                                                "ChaCha20Poly1305" => Some(crate::graphql::types::EncryptionAlgorithm::ChaCha20Poly1305),
                                                "Aes256Gcm" => Some(crate::graphql::types::EncryptionAlgorithm::Aes256Gcm),
                                                "Rsa2048" => Some(crate::graphql::types::EncryptionAlgorithm::Rsa2048),
                                                "Rsa4096" => Some(crate::graphql::types::EncryptionAlgorithm::Rsa4096),
                                                "EcdsaP256" => Some(crate::graphql::types::EncryptionAlgorithm::EcdsaP256),
                                                "EcdsaP384" => Some(crate::graphql::types::EncryptionAlgorithm::EcdsaP384),
                                                _ => None,
                                            }),
                                            encrypted: field.get("encrypted").and_then(|v| v.as_bool()).unwrap_or(false),
                                        })
                                    } else {
                                        None
                                    }
                                }).collect::<Vec<_>>()
                            } else {
                                Vec::new()
                            },
                            // Parse primary_key from table_info
                            primary_key: if let Some(pk_array) = table_info.get("primary_key").and_then(|v| v.as_array()) {
                                pk_array.iter().filter_map(|pk| pk.as_str().map(String::from)).collect::<Vec<_>>()
                            } else {
                                Vec::new()
                            },
                            created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                            updated_at: table_info.get("updated_at").and_then(|v| v.as_str())
                                .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                            record_count: table_info.get("record_count").and_then(|v| v.as_u64()).unwrap_or(0) as i32,
                            encryption_enabled: table_info.get("encryption_enabled").and_then(|v| v.as_bool()).unwrap_or(true),
                        };
                        tables.push(table);
                    }
                }
            }
        }
        
        Ok(tables)
    }

    /// Get a specific table by name
    async fn table(&self, ctx: &Context<'_>, database: String, name: String) -> Result<Option<Table>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Look up table by database and name
        let table_key = format!("db:{}:table:{}", database, name);
        
        if let Some(data) = storage.get(&table_key).await
            .map_err(|e| async_graphql::Error::new(format!("Table retrieval failed: {}", e)))? {
            
            if let Ok(table_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                if let Some(created_at) = table_info.get("created_at").and_then(|v| v.as_str()) {
                    let default_id = Uuid::new_v4().to_string();
                    let table = Table {
                        id: table_info.get("id").and_then(|v| v.as_str())
                            .unwrap_or(&default_id)
                            .parse().unwrap_or_else(|_| Uuid::new_v4()).to_string(),
                        name: name.clone(),
                        database: database.clone(),
                        description: table_info.get("description").and_then(|v| v.as_str()).map(String::from),
                        // Parse fields from table_info
                        fields: if let Some(fields_array) = table_info.get("fields").and_then(|v| v.as_array()) {
                            fields_array.iter().filter_map(|field| {
                                if let (Some(name), Some(field_type_str)) = (
                                    field.get("name").and_then(|v| v.as_str()),
                                    field.get("field_type").and_then(|v| v.as_str())
                                ) {
                                    let field_type = match field_type_str {
                                        "Text" => crate::graphql::types::FieldType::Text,
                                        "Integer" => crate::graphql::types::FieldType::Integer,
                                        "Float" => crate::graphql::types::FieldType::Float,
                                        "Boolean" => crate::graphql::types::FieldType::Boolean,
                                        "DateTime" => crate::graphql::types::FieldType::DateTime,
                                        "Date" => crate::graphql::types::FieldType::DateTime,
                                        "Json" => crate::graphql::types::FieldType::Json,
                                        "Binary" => crate::graphql::types::FieldType::Binary,
                                        _ => crate::graphql::types::FieldType::Text,
                                    };
                                    Some(crate::graphql::types::Field {
                                        name: name.to_string(),
                                        field_type,
                                        required: field.get("required").and_then(|v| v.as_bool()).unwrap_or(false),
                                        description: field.get("description").and_then(|v| v.as_str()).map(String::from),
                                        default_value: field.get("default_value").and_then(|v| v.as_str()).map(String::from),
                                        encryption_algorithm: field.get("encryption_algorithm").and_then(|v| v.as_str()).and_then(|alg| match alg {
                                            "Aegis256" => Some(crate::graphql::types::EncryptionAlgorithm::Aegis256),
                                            "ChaCha20Poly1305" => Some(crate::graphql::types::EncryptionAlgorithm::ChaCha20Poly1305),
                                            "Aes256Gcm" => Some(crate::graphql::types::EncryptionAlgorithm::Aes256Gcm),
                                            "Rsa2048" => Some(crate::graphql::types::EncryptionAlgorithm::Rsa2048),
                                            "Rsa4096" => Some(crate::graphql::types::EncryptionAlgorithm::Rsa4096),
                                            "EcdsaP256" => Some(crate::graphql::types::EncryptionAlgorithm::EcdsaP256),
                                            "EcdsaP384" => Some(crate::graphql::types::EncryptionAlgorithm::EcdsaP384),
                                            _ => None,
                                        }),
                                        encrypted: field.get("encrypted").and_then(|v| v.as_bool()).unwrap_or(false),
                                    })
                                } else {
                                    None
                                }
                            }).collect::<Vec<_>>()
                        } else {
                            Vec::new()
                        },
                        // Parse primary_key from table_info
                        primary_key: if let Some(pk_array) = table_info.get("primary_key").and_then(|v| v.as_array()) {
                            pk_array.iter().filter_map(|pk| pk.as_str().map(String::from)).collect::<Vec<_>>()
                        } else {
                            Vec::new()
                        },
                        created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                        updated_at: table_info.get("updated_at").and_then(|v| v.as_str())
                            .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                        record_count: table_info.get("record_count").and_then(|v| v.as_u64()).unwrap_or(0) as i32,
                        encryption_enabled: table_info.get("encryption_enabled").and_then(|v| v.as_bool()).unwrap_or(true),
                    };
                    return Ok(Some(table));
                }
            }
        }
        
        Ok(None)
    }

    // ==================== Data Queries ====================

    /// Query data from a table
    async fn query_data(&self, ctx: &Context<'_>, input: QueryDataInput) -> Result<QueryResult> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Query data records for the table
        let data_prefix = format!("db:{}:table:{}:record:", input.database, input.table);
        let record_keys = storage.list_prefix(&data_prefix).await
            .map_err(|e| async_graphql::Error::new(format!("Data query failed: {}", e)))?;
        
        let mut records = Vec::new();
        
        for key in record_keys {
            if let Some(data) = storage.get(&key).await
                .map_err(|e| async_graphql::Error::new(format!("Record retrieval failed: {}", e)))? {
                
                if let Ok(record_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    if let (Some(id), Some(record_data), Some(created_at)) = (
                        record_info.get("id").and_then(|v| v.as_str()),
                        record_info.get("data"),
                        record_info.get("created_at").and_then(|v| v.as_str())
                    ) {
                        let record = DataRecord {
                            id: id.to_string(),
                            data: async_graphql::Json(record_data.clone()),
                            created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                            updated_at: record_info.get("updated_at").and_then(|v| v.as_str())
                                .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                            encryption_metadata: None,
                        };
                        records.push(record);
                    }
                }
            }
        }
        
        // Apply pagination
        let page = input.pagination.as_ref().and_then(|p| p.page).unwrap_or(0) as usize;
        let page_size = input.pagination.as_ref().and_then(|p| p.page_size).unwrap_or(10) as usize;
        let start_idx = page * page_size;
        let end_idx = std::cmp::min(start_idx + page_size, records.len());
        
        let paginated_records = if start_idx < records.len() {
            records[start_idx..end_idx].to_vec()
        } else {
            Vec::new()
        };
        
        let pagination_info = PaginationInfo {
            page: page as i32,
            page_size: page_size as i32,
            total_pages: ((records.len() as i32 + page_size as i32 - 1) / page_size as i32) as i32,
            total_records: records.len() as i32,
            has_next: end_idx < records.len(),
            has_previous: page > 0,
        };
        
        Ok(QueryResult {
            records: paginated_records,
            total_count: records.len() as i32,
            has_more: end_idx < records.len(),
            pagination: Some(pagination_info),
        })
    }

    /// Get a specific record by ID
    async fn get_record(&self, ctx: &Context<'_>, database: String, table: String, id: String) -> Result<Option<DataRecord>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Look up specific record
        let record_key = format!("db:{}:table:{}:record:{}", database, table, id);
        
        if let Some(data) = storage.get(&record_key).await
            .map_err(|e| async_graphql::Error::new(format!("Record details retrieval failed: {}", e)))? {
            
            if let Ok(record_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                if let (Some(record_id), Some(record_data), Some(created_at)) = (
                    record_info.get("id").and_then(|v| v.as_str()),
                    record_info.get("data"),
                    record_info.get("created_at").and_then(|v| v.as_str())
                ) {
                    let record = DataRecord {
                        id: record_id.to_string(),
                        data: async_graphql::Json(record_data.clone()),
                        created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                        updated_at: record_info.get("updated_at").and_then(|v| v.as_str())
                            .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                        encryption_metadata: None,
                    };
                    return Ok(Some(record));
                }
            }
        }
        
        Ok(None)
    }

    // ==================== Encryption Queries ====================

    /// Get encryption metadata for a table
    async fn encryption_metadata(&self, ctx: &Context<'_>, database: String, table: String) -> Result<Vec<EncryptionMetadata>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Look up table encryption metadata
        let metadata_key = format!("db:{}:table:{}:encryption", database, table);
        
        if let Some(data) = storage.get(&metadata_key).await
            .map_err(|e| async_graphql::Error::new(format!("Encryption metadata retrieval failed: {}", e)))? {
            
            if let Ok(metadata_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                if let Some(fields) = metadata_info.get("fields").and_then(|v| v.as_array()) {
                    let mut encryption_metadata = Vec::new();
                    
                    for field in fields {
                        if let Some(field_name) = field.get("field_name").and_then(|v| v.as_str()) {
                            let metadata = EncryptionMetadata {
                                field_name: field_name.to_string(),
                                algorithm: field.get("algorithm").and_then(|v| v.as_str())
                                    .map(|s| match s {
                                        "AEGIS256" => EncryptionAlgorithm::Aegis256,
                                        "AES256GCM" => EncryptionAlgorithm::Aes256Gcm,
                                        "CHACHA20POLY1305" => EncryptionAlgorithm::ChaCha20Poly1305,
                                        _ => EncryptionAlgorithm::Aegis256,
                                    }).unwrap_or(EncryptionAlgorithm::Aegis256),
                                key_id: field.get("key_id").and_then(|v| v.as_str()).map(String::from).unwrap_or_default(),
                                key_version: field.get("key_version").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
                                encrypted_at: field.get("encrypted_at").and_then(|v| v.as_str())
                                    .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                            };
                            encryption_metadata.push(metadata);
                        }
                    }
                    
                    return Ok(encryption_metadata);
                }
            }
        }
        
        Ok(Vec::new())
    }

    /// Get key rotation status
    async fn key_rotation_status(&self, ctx: &Context<'_>, database: String, table: String, rotation_id: String) -> Result<Option<KeyRotationStatus>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Look up key rotation status
        let rotation_key = format!("db:{}:table:{}:rotation:{}", database, table, rotation_id);
        
        if let Some(data) = storage.get(&rotation_key).await
            .map_err(|e| async_graphql::Error::new(format!("Rotation status retrieval failed: {}", e)))? {
            
            if let Ok(rotation_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                if let Some(status) = rotation_info.get("status").and_then(|v| v.as_str()) {
                    let rotation_status = KeyRotationStatus {
                        id: rotation_id,
                        status: status.to_string(),
                        progress_percentage: rotation_info.get("progress_percentage").and_then(|v| v.as_f64()).unwrap_or(0.0),
                        started_at: rotation_info.get("started_at").and_then(|v| v.as_str())
                            .and_then(|s| s.parse().ok()),
                        completed_at: rotation_info.get("completed_at").and_then(|v| v.as_str())
                            .and_then(|s| s.parse().ok()),
                        error_message: rotation_info.get("error_message").and_then(|v| v.as_str()).map(String::from),
                        records_processed: rotation_info.get("records_processed").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
                        total_records: rotation_info.get("total_records").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
                    };
                    return Ok(Some(rotation_status));
                }
            }
        }
        
        Ok(None)
    }

    // ==================== System Queries ====================

    /// Get system health status
    async fn health(&self, ctx: &Context<'_>) -> Result<HealthStatus> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Check storage health
        let storage_healthy = storage.health_check().await.is_ok();
        
        let mut services = HashMap::new();
        services.insert("database".to_string(), ServiceHealth {
            name: "database".to_string(),
            healthy: storage_healthy,
            response_time_ms: if storage_healthy { 5 } else { 1000 },
            details: HashMap::new(),
        });
        services.insert("encryption".to_string(), ServiceHealth {
            name: "encryption".to_string(),
            healthy: true, // Key manager is always healthy if initialized
            response_time_ms: 1,
            details: HashMap::new(),
        });
        services.insert("api".to_string(), ServiceHealth {
            name: "api".to_string(),
            healthy: true,
            response_time_ms: 2,
            details: HashMap::new(),
        });
        
        Ok(HealthStatus {
            healthy: storage_healthy,
            services,
            last_check: Utc::now(),
        })
    }

    /// Get API version information
    async fn version(&self, _ctx: &Context<'_>) -> Result<String> {
        Ok(crate::VERSION.to_string())
    }

    /// Get authenticated user information
    async fn me(&self, ctx: &Context<'_>) -> Result<Option<AuthenticatedUser>> {
        let graphql_ctx = from_context(ctx)?;
        Ok(graphql_ctx.user.clone())
    }

    // ==================== Dynamic Secrets Queries ====================

    /// Get dynamic secrets engine status
    async fn dynamic_secrets_status(&self, ctx: &Context<'_>) -> Result<DynamicSecretsStatus> {
        let graphql_ctx = from_context(ctx)?;
        
        // Check permissions - require at least user role
        graphql_ctx.require_role("user")?;

        let dynamic_secrets = &graphql_ctx.app_state.dynamic_secrets;
        let engine_status = dynamic_secrets.status().await
            .map_err(|e| async_graphql::Error::new(format!("Failed to get dynamic secrets status: {}", e)))?;

        let supported_databases = vec![
            crate::graphql::types::DynamicDatabaseType::Postgresql,
            crate::graphql::types::DynamicDatabaseType::Mysql,
            crate::graphql::types::DynamicDatabaseType::Sqlserver,
        ];

        Ok(DynamicSecretsStatus {
            name: engine_status.name,
            initialized: engine_status.initialized,
            total_secrets: engine_status.stats.total_secrets,
            active_leases: engine_status.stats.active_leases,
            aws_configured: engine_status.config.get("aws").is_some(),
            supported_databases,
            default_ttl: engine_status.config.get("default_ttl")
                .and_then(|v| v.as_u64())
                .unwrap_or(3600),
            max_ttl: engine_status.config.get("max_ttl")
                .and_then(|v| v.as_u64())
                .unwrap_or(86400),
            auto_cleanup: engine_status.config.get("auto_cleanup")
                .and_then(|v| v.as_bool())
                .unwrap_or(true),
        })
    }

    /// List dynamic credentials
    async fn list_dynamic_credentials(&self, ctx: &Context<'_>, path: Option<String>) -> Result<Vec<String>> {
        let graphql_ctx = from_context(ctx)?;
        
        // Check permissions - require at least user role
        graphql_ctx.require_role("user")?;

        let dynamic_secrets = &graphql_ctx.app_state.dynamic_secrets;
        let search_path = path.unwrap_or_else(|| "".to_string());
        
        dynamic_secrets.list(&search_path).await
            .map_err(|e| async_graphql::Error::new(format!("Failed to list dynamic credentials: {}", e)))
    }

    /// Get a specific dynamic credential
    async fn get_dynamic_credential(&self, ctx: &Context<'_>, lease_id: String) -> Result<Option<SecretData>> {
        let graphql_ctx = from_context(ctx)?;
        
        // Check permissions - require at least user role
        graphql_ctx.require_role("user")?;

        let dynamic_secrets = &graphql_ctx.app_state.dynamic_secrets;
        let secret = dynamic_secrets.read(&lease_id).await
            .map_err(|e| async_graphql::Error::new(format!("Failed to get dynamic credential: {}", e)))?;

        match secret {
            Some(secret) => {
                let secret_data = SecretData {
                    data: secret.data,
                    created_at: secret.metadata.created_at,
                    updated_at: secret.metadata.updated_at,
                    version: secret.metadata.version as i32, // Cast u64 to i32
                    lease: secret.metadata.lease.map(|lease| crate::graphql::types::LeaseInfo {
                        lease_id: lease.lease_id,
                        ttl: lease.ttl,
                        created_at: lease.created_at,
                        renewable: lease.renewable,
                        max_ttl: lease.max_ttl,
                    }),
                };
                Ok(Some(secret_data))
            },
            None => Ok(None),
        }
    }
}