Aperto Chiuso

Override DB Client to use MongoDB CSFLE #7157


User avatar
0
aaguiar creato

Hi, I'm trying to implement MongoDB CSFLE, I need a way to override the MongoDB settings and the MongoClient.

I created a class implementing AbpMongoDbContext could you please let me know how can I achieve that? One of the settings that I have to change is: mongoDBSettings.AutoEncryptionOptions

Database and Client are private set, so I can't override it.

  • ABP Framework version: 8.0.3
  • UI Type: Angular
  • Database System: MongoDB

7 risposte
  • User Avatar
    0
    liangshiwei creato
    Team di supporto Fullstack Developer

    Hi,

    You can configure the AbpMongoDbContextOptions

    Configure<AbpMongoDbContextOptions>(options =>
    {
        options.MongoClientSettingsConfigurer = settings =>
        {
            settings.AutoEncryptionOptions = ....
        };
    });
    
  • User Avatar
    0
    aaguiar creato

    To be able to build the AutoEncryptionOptions I need to fetch the collection to grab the encryption key, how can I achieve that in the DBModule if I don't have the MongoClient just yet?

  • User Avatar
    0
    liangshiwei creato
    Team di supporto Fullstack Developer

    You can check this: https://stackoverflow.com/questions/68659436/how-to-encrypt-a-class-property-using-mongodb-csfle-in-dot-net-core

  • User Avatar
    0
    aaguiar creato

    I'm not talking about the CSFLE implementation, that part is done, my question is how to override the MongoClient in the AbpMongoDbContext, because I need to fetch the keys from the DB, create the AutoEncryptionOptions and then override the client config

  • User Avatar
    0
    liangshiwei creato
    Team di supporto Fullstack Developer

    There are two ways:

    1:

    https://support.abp.io/QA/Questions/7157/Override-DB-Client-to-use-MongoDB-CSFLE#answer-3a126877-4681-cdaa-d7a7-13856681c892

    2:

    Override the CreateMongoClient method of the UnitOfWorkMongoDbContextProvider

  • User Avatar
    0
    aaguiar creato

    Found a solution which worked:

    public override void InitializeDatabase(IMongoDatabase database, IMongoClient client, IClientSessionHandle sessionHandle)
    { 
        var clientSettings = client.Settings.Clone();
    
        clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
    
        client = new MongoClient(clientSettings);
        database = client.GetDatabase(database.DatabaseNamespace.DatabaseName);
        base.InitializeDatabase(database, client, sessionHandle);
    }
    
  • User Avatar
    0
    liangshiwei creato
    Team di supporto Fullstack Developer

    Good to see you find a way

Made with ❤️ on ABP v8.2.0-preview Updated on marzo 25, 2024, 15:11