This is a sample application that demonstrates how to use the Mongoid Client-Side Field Level Encryption feature with the Mongoid ODM.
To use CSFLE with Mongoid, you must have the following:
- A MongoDB Atlas cluster running MongoDB 4.2 or later.
- MongoDB Ruby driver 2.13.0 or later.
- Mongoid 9.0.0 or later. This version is not released yet, so you must use the master branch of the Mongoid repository.
- The crypt_shared library. It can be downloaded from the MongoDB Download Center. Store the library file in vendor/crypt_shared directory.
-
Clone this repository.
-
Install the dependencies:
bundle install
Please note that
libmongocrypt-helper
gem builds the library from sources, so you need to havecmake
installed. If you want to avoid building the library, you can download a pre-built version following these instructions. If you decide to do so, you will need to set theLIBMONGOCRYPT_PATH
environment variable to the path where you downloaded the library:export LIBMONGOCRYPT_PATH=<path to libmongocrypt>
In this case also remove gem
libmongocrypt-helper
from theGemfile
. -
Export the following environment variables :
export ATLAS_URI=<your MongoDB Atlas connection string> export LOCAL_MASTER_KEY=<Random 96-byte string>
-
Create your first data key:
rails db:mongoid:encryption:create_data_key
-
Export the id of the data key as an environment variable:
export USER_KEY_ID=<your data key id>
-
Seed the database:
rails db:seed
-
Run the application:
rails server
The seeds includes two users: 'jane@doe.com' and 'john@doe.com', both with password '111111'. You can log in with either of them.
Copyright (c) 2023-Present MongoDB Inc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.