|
| 1 | +.. _cpp-client-side-encryption: |
| 2 | + |
| 3 | +=================================== |
| 4 | +Client-Side Field Level Encryption |
| 5 | +=================================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +Client-Side Field Level Encryption |
| 18 | +---------------------------------- |
| 19 | + |
| 20 | +New in MongoDB 4.2 `Client-Side Field Level Encryption (CSFLE) <https://www.mongodb.com/docs/drivers/use-cases/client-side-field-level-encryption-guide>`__ allows administrators and developers to encrypt specific data fields in addition to other MongoDB encryption features. |
| 21 | + |
| 22 | +With CSFLE, developers can encrypt fields client side without any server-side configuration or directives. Client-Side Field Level Encryption supports workloads where applications must guarantee that unauthorized parties, including server administrators, cannot read the encrypted data. |
| 23 | + |
| 24 | +For an overview of CSFLE, please read :manual:`the official MongoDB documentation in the manual </core/security-client-side-encryption/>`. |
| 25 | + |
| 26 | +Installation |
| 27 | +------------ |
| 28 | + |
| 29 | +``libmongocrypt`` |
| 30 | +~~~~~~~~~~~~~~~~~ |
| 31 | + |
| 32 | +Client-Side Field Level Encryption relies on a C library called ``libmongocrypt`` to do the heavy lifting encryption work. This dependency is managed by the C driver. As long as the C driver installation is 1.16.0 or higher, and has been compiled with Client-Side Field Level Encryption support, this dependency should be managed internally. See the C driver's `Using Client-Side Field Level Encryption <https://mongoc.org/libmongoc/current/client-side-field-level-encryption.html>`__ for more information. |
| 33 | + |
| 34 | +``mongocryptd`` |
| 35 | +~~~~~~~~~~~~~~~ |
| 36 | + |
| 37 | +Automatic CSFLE relies upon a new binary called ``mongocryptd`` running as a daemon while the driver operates. This binary is only available with MongoDB Enterprise. |
| 38 | + |
| 39 | +``mongocryptd`` can either be started separately from the driver, or left to spawn automatically when encryption is used. |
| 40 | + |
| 41 | +To run mongocryptd separately, pass the ``mongocryptdBypassSpawn`` flag to the client's auto encryption options: |
| 42 | + |
| 43 | +.. code-block:: cpp |
| 44 | + |
| 45 | + auto mongocryptd_options = make_document(kvp("mongocryptdBypassSpawn", true)); |
| 46 | + |
| 47 | + options::auto_encryption auto_encrypt_opts{}; |
| 48 | + auto_encrypt_opts.extra_options({mongocryptd_options.view()}); |
| 49 | + |
| 50 | +If the mongocryptd binary is on the current path, the driver will be able to spawn it without any custom flags. However, if the mongocryptd binary is on a different path, set the path with the ``mongocryptdSpawnPath`` option: |
| 51 | + |
| 52 | +.. code-block:: cpp |
| 53 | + |
| 54 | + auto mongocryptd_options = make_document(kvp("mongocryptdSpawnPath", "path/to/mongocryptd")); |
| 55 | + |
| 56 | + options::auto_encryption auto_encrypt_opts{}; |
| 57 | + auto_encrypt_opts.extra_options({mongocryptd_options.view()}); |
| 58 | + |
| 59 | +Examples |
| 60 | +-------- |
| 61 | + |
| 62 | +Automatic Client-Side Field Level Encryption |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +Automatic Client-Side Field Level Encryption is enabled by creating a ``mongocxx::client`` with the ``auto_encryption_opts`` option set to an instance of ``mongocxx::options::auto_encryption``. The following examples show how to set up automatic client-side field level encryption using the ``mongocxx::client_encryption`` class to create a new encryption data key. |
| 66 | + |
| 67 | +.. note:: |
| 68 | + |
| 69 | + Automatic client-side field level encryption requires MongoDB 4.2 enterprise or a MongoDB 4.2 Atlas cluster. The community version of the server supports automatic decryption as well as explicit client-side field level encryption. |
| 70 | + |
| 71 | +Providing Local Automatic Encryption Rules |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +The following example shows how to specify automatic encryption rules via the |
| 75 | +schema_map option. The automatic encryption rules are expressed using a `strict |
| 76 | +subset of the JSON Schema syntax |
| 77 | +<https://www.mongodb.com/docs/manual/reference/security-client-side-automatic-json-schema/>`__. |
| 78 | + |
| 79 | +Supplying a ``schema_map`` provides more security than relying on JSON Schemas obtained from the server. It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted data that should be encrypted. |
| 80 | + |
| 81 | +JSON Schemas supplied in the ``schema_map`` only apply to configuring automatic client-side field level encryption. Other validation rules in the JSON schema will not be enforced by the driver and will result in an error. |
| 82 | + |
| 83 | +.. code-block:: cpp |
| 84 | + |
| 85 | + // |
| 86 | + // The schema map has the following form: |
| 87 | + // |
| 88 | + // { |
| 89 | + // "test.coll" : { |
| 90 | + // "bsonType" : "object", |
| 91 | + // "properties" : { |
| 92 | + // "encryptedFieldName" : { |
| 93 | + // "encrypt" : { |
| 94 | + // "keyId" : [ <datakey as UUID> ], |
| 95 | + // "bsonType" : "string", |
| 96 | + // "algorithm" : <algorithm> |
| 97 | + // } |
| 98 | + // } |
| 99 | + // } |
| 100 | + // } |
| 101 | + // } |
| 102 | + // |
| 103 | + |
| 104 | +Please see `examples/mongocxx/automatic_client_side_field_level_encryption.cpp <https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/automatic_client_side_field_level_encryption.cpp>`__ for a full example of how to set a json schema for automatic encryption. |
| 105 | + |
| 106 | +Server-Side Field Level Encryption Enforcement |
| 107 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 | + |
| 109 | +The MongoDB 4.2 server supports using schema validation to enforce encryption of specific fields in a collection. This schema validation will prevent an application from inserting unencrypted values for any fields marked with the ``"encrypt"`` JSON schema keyword. |
| 110 | + |
| 111 | +It is possible to set up automatic client-side field level encryption using the ``mongocxx::client_encryption`` to create a new encryption data key and create a collection with the :manual:`Automatic Encryption JSON Schema Syntax </reference/security-client-side-automatic-json-schema/>`. |
| 112 | + |
| 113 | +.. code-block:: cpp |
| 114 | + |
| 115 | + // Please see the linked example below for full json_schema construction. |
| 116 | + bsoncxx::document::value json_schema{}; |
| 117 | + |
| 118 | + // Create the collection with the encryption JSON Schema. |
| 119 | + auto cmd = document{} << "create" |
| 120 | + << "coll" |
| 121 | + << "validator" << open_document |
| 122 | + << "$jsonSchema" << json_schema.view() |
| 123 | + << close_document << finalize; |
| 124 | + |
| 125 | + db.run_command(cmd.view()); |
| 126 | + |
| 127 | +Please see `examples/mongocxx/server_side_field_level_encryption_enforcement.cpp <https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/server_side_field_level_encryption_enforcement.cpp>`__ for a full example of setting encryption enforcement on the server. |
| 128 | + |
| 129 | +Explicit Encryption |
| 130 | +~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +Explicit encryption is a MongoDB community feature and does not use the ``mongocryptd`` process. Explicit encryption is provided by the ``mongocxx::client_encryption`` class. |
| 133 | + |
| 134 | +.. code-block:: cpp |
| 135 | + |
| 136 | + // Explicitly encrypt a BSON value. |
| 137 | + auto to_encrypt = bsoncxx::types::bson_value::make_value("secret message"); |
| 138 | + auto encrypted_message = client_encryption.encrypt(to_encrypt, encrypt_opts); |
| 139 | + |
| 140 | + // Explicitly decrypt a BSON value. |
| 141 | + auto decrypted_message = client_encryption.decrypt(encrypted_message); |
| 142 | + |
| 143 | +Please see `examples/mongocxx/explicit_encryption.cpp <https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/explicit_encryption.cpp>`__ for a full example of using explicit encryption and decryption. |
| 144 | + |
| 145 | +Explicit Encryption with Automatic Decryption |
| 146 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 147 | + |
| 148 | +Although automatic encryption requires MongoDB 4.2 enterprise or a MongoDB 4.2 Atlas cluster, automatic decryption is supported for all users. To configure automatic decryption without automatic encryption, set ``bypass_auto_encryption=True`` in the ``options::auto_encryption`` class. |
| 149 | + |
| 150 | +.. code-block:: cpp |
| 151 | + |
| 152 | + options::auto_encryption auto_encrypt_opts{}; |
| 153 | + auto_encrypt_opts.bypass_auto_encryption(true); |
| 154 | + // Please see full example for complete options construction. |
| 155 | + |
| 156 | + // Create a client with automatic decryption enabled, but automatic encryption bypassed. |
| 157 | + options::client client_opts{}; |
| 158 | + client_opts.auto_encryption_opts(std::move(auto_encrypt_opts)); |
| 159 | + class client client_encrypted {uri{}, std::move(client_opts)}; |
| 160 | + |
| 161 | +Please see `examples/mongocxx/explicit_encryption_auto_decryption.cpp <https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/explicit_encryption_auto_decryption.cpp>`__ for an example of using explicit encryption with automatic decryption. |
| 162 | + |
0 commit comments