11package com .ayedata .simault .config ;
22
33import com .mongodb .client .MongoClient ;
4- import com .mongodb .client .MongoClients ;
54import com .mongodb .client .MongoCollection ;
5+ import com .mongodb .client .MongoDatabase ;
6+ import com .mongodb .client .model .Filters ;
67import com .mongodb .client .model .IndexOptions ;
78import com .mongodb .client .model .Indexes ;
89import jakarta .annotation .PostConstruct ;
910import org .bson .Document ;
1011import org .springframework .beans .factory .annotation .Value ;
11- import org .springframework .context .annotation .Bean ;
1212import org .springframework .context .annotation .Configuration ;
1313
1414import java .util .concurrent .TimeUnit ;
1515
1616@ Configuration
1717public class MongoConfig {
1818
19- @ Value ("${vault.mongodb.uri}" ) private String uri ;
20- @ Value ("${vault.mongodb.database}" ) private String dbName ;
21- @ Value ("${vault.mongodb.collection}" ) private String collName ;
19+ private final MongoClient mongoClient ;
2220
23- @ Bean
24- public MongoClient mongoClient () {
25- return MongoClients .create (uri );
21+ @ Value ("${vault.mongodb.database}" )
22+ private String dbName ;
23+
24+ @ Value ("${vault.mongodb.collection}" )
25+ private String secretsCollName ;
26+
27+ public MongoConfig (MongoClient mongoClient ) {
28+ this .mongoClient = mongoClient ;
2629 }
2730
2831 @ PostConstruct
29- public void initIndexes () {
30- try (MongoClient client = MongoClients .create (uri )) {
31- // 1. Ensure TTL Index for Secrets (1 Hour)
32- MongoCollection <Document > secretColl = client .getDatabase (dbName ).getCollection (collName );
33- secretColl .createIndex (Indexes .ascending ("createdAt" ),
34- new IndexOptions ().expireAfter (3600L , TimeUnit .SECONDS ));
35-
36- System .out .println ("✅ Mongo Indexes Configured." );
37- }
32+ public void configureIndexes () {
33+ System .out .println ("⚙️ Configuring MongoDB Indexes..." );
34+
35+ MongoDatabase db = mongoClient .getDatabase (dbName );
36+ MongoDatabase encryptionDb = mongoClient .getDatabase ("encryption" );
37+
38+ // =================================================================
39+ // 1. KEY VAULT COLLECTION (__keyVault)
40+ // =================================================================
41+ // Critical for CSFLE: Ensures unique key names
42+ MongoCollection <Document > keyVaultColl = encryptionDb .getCollection ("__keyVault" );
43+ keyVaultColl .createIndex (
44+ Indexes .ascending ("keyAltNames" ),
45+ new IndexOptions ().unique (true ).partialFilterExpression (Filters .exists ("keyAltNames" ))
46+ );
47+
48+ // =================================================================
49+ // 2. SECRETS COLLECTION
50+ // =================================================================
51+ MongoCollection <Document > secretsColl = db .getCollection (secretsCollName );
52+
53+ // [TTL Index] Auto-expire secrets after 1 hour (3600 seconds)
54+ secretsColl .createIndex (
55+ Indexes .ascending ("createdAt" ),
56+ new IndexOptions ().expireAfter (3600L , TimeUnit .SECONDS )
57+ );
58+
59+ // [Performance Index] Fast lookup by App ID (Critical for getSecret)
60+ secretsColl .createIndex (
61+ Indexes .ascending ("appId" ),
62+ new IndexOptions ().background (true )
63+ );
64+
65+ // =================================================================
66+ // 3. ALLOWED APPS COLLECTION (Registry)
67+ // =================================================================
68+ MongoCollection <Document > appsColl = db .getCollection ("allowed_apps" );
69+
70+ // [Unique Index] Prevent duplicate registrations
71+ appsColl .createIndex (
72+ Indexes .ascending ("appId" ),
73+ new IndexOptions ().unique (true )
74+ );
75+
76+ // [Sort Index] Efficiently list apps by registration date (Admin Dashboard)
77+ appsColl .createIndex (
78+ Indexes .descending ("registeredAt" )
79+ );
80+
81+ // [Text Index] Enable fuzzy search by description (for AI/MCP Search)
82+ appsColl .createIndex (
83+ Indexes .text ("description" )
84+ );
85+
86+ System .out .println ("✅ All MongoDB Indexes Configured Successfully." );
3887 }
3988}
0 commit comments