auto_encryption.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2020 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <string>
  16. #include <bsoncxx/document/view_or_value.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <mongocxx/stdx.hpp>
  19. #include <mongocxx/config/prelude.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. class client;
  23. class pool;
  24. namespace options {
  25. ///
  26. /// Class representing options for automatic client-side encryption.
  27. ///
  28. class MONGOCXX_API auto_encryption {
  29. public:
  30. ///
  31. /// Default constructs a new auto_encryption object.
  32. ///
  33. auto_encryption() noexcept;
  34. ///
  35. /// When the key vault collection is on a separate MongoDB cluster,
  36. /// sets the optional client to use to route data key queries to
  37. /// that cluster.
  38. ///
  39. /// The given key vault client MUST outlive any client that has
  40. /// been enabled to use it through these options.
  41. ///
  42. /// @param
  43. /// A client to use for routing queries to the key vault collection.
  44. ///
  45. /// @return
  46. /// A reference to this object to facilitate method chaining.
  47. ///
  48. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  49. ///
  50. auto_encryption& key_vault_client(client* client);
  51. ///
  52. /// Gets the key vault client.
  53. ///
  54. /// @return
  55. /// An optional pointer to the key vault client.
  56. ///
  57. const stdx::optional<client*>& key_vault_client() const;
  58. ///
  59. /// When the key vault collection is on a separate MongoDB cluster,
  60. /// sets the optional client pool to use to route data key queries to
  61. /// that cluster.
  62. ///
  63. /// This option may not be used if a key_vault_client is set.
  64. ///
  65. /// The given key vault pool MUST outlive any pool that has
  66. /// been enabled to use it through these options.
  67. ///
  68. /// May only be set when enabling automatic encryption on a pool.
  69. ///
  70. /// @param
  71. /// A pool to use for routing queries to the key vault collection.
  72. ///
  73. /// @return
  74. /// A reference to this object to facilitate method chaining.
  75. ///
  76. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  77. ///
  78. auto_encryption& key_vault_pool(pool* pool);
  79. ///
  80. /// Gets the key vault pool.
  81. ///
  82. /// @return
  83. /// An optional pointer to the key vault pool.
  84. ///
  85. const stdx::optional<pool*>& key_vault_pool() const;
  86. ///
  87. /// Sets the namespace to use to access the key vault collection, which
  88. /// contains all data keys used for encryption and decryption. This
  89. /// option must be set:
  90. ///
  91. /// auto_encryption.key_vault_namespace({ "db", "coll" });
  92. ///
  93. /// @param ns
  94. /// A std::pair of strings representing the db and collection to use
  95. /// to access the key vault.
  96. ///
  97. /// @return
  98. /// A reference to this object to facilitate method chaining.
  99. ///
  100. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  101. ///
  102. using ns_pair = std::pair<std::string, std::string>;
  103. auto_encryption& key_vault_namespace(ns_pair ns);
  104. ///
  105. /// Gets the key vault namespace.
  106. ///
  107. /// @return
  108. /// An optional pair of strings representing the namespace of the
  109. /// key vault collection.
  110. ///
  111. const stdx::optional<ns_pair>& key_vault_namespace() const;
  112. ///
  113. /// Sets the KMS providers to use for client side encryption.
  114. ///
  115. /// Multiple KMS providers may be specified. Two KMS providers are
  116. /// supported: "aws" and "local". The kmsProviders map values differ
  117. /// by provider:
  118. ///
  119. /// aws: {
  120. /// accessKeyId: String,
  121. /// secretAccessKey: String
  122. /// }
  123. ///
  124. /// local: {
  125. /// key: byte[96] // The master key used to encrypt/decrypt data keys.
  126. /// }
  127. ///
  128. /// @param kms_providers
  129. /// A document containing the KMS providers.
  130. ///
  131. /// @return
  132. /// A reference to this object to facilitate method chaining.
  133. ///
  134. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  135. ///
  136. auto_encryption& kms_providers(bsoncxx::document::view_or_value kms_providers);
  137. ///
  138. /// Gets the KMS providers.
  139. ///
  140. /// @return
  141. /// An optional document containing the KMS providers.
  142. ///
  143. const stdx::optional<bsoncxx::document::view_or_value>& kms_providers() const;
  144. ///
  145. /// Sets a local JSON schema.
  146. ///
  147. /// Supplying a schemaMap provides more security than relying on
  148. /// JSON schemas obtained from the server. It protects against a
  149. /// malicious server advertising a false JSON Schema, which could
  150. /// trick the client into sending unencrypted data that should be
  151. /// encrypted.
  152. ///
  153. /// Schemas supplied in the schemaMap only apply to configuring
  154. /// automatic encryption for client side encryption. Other validation
  155. /// rules in the JSON schema will not be enforced by the driver and
  156. /// will result in an error.
  157. ///
  158. /// @param schema_map
  159. /// The JSON schema to use.
  160. ///
  161. /// @return
  162. /// A reference to this object to facilitate method chaining.
  163. ///
  164. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  165. ///
  166. auto_encryption& schema_map(bsoncxx::document::view_or_value schema_map);
  167. ///
  168. /// Gets the schema map.
  169. ///
  170. /// @return
  171. /// An optional document containing the schema map.
  172. ///
  173. const stdx::optional<bsoncxx::document::view_or_value>& schema_map() const;
  174. ///
  175. /// Automatic encryption is disabled when the 'bypassAutoEncryption'
  176. /// option is true. Default is 'false,' so auto encryption is enabled.
  177. ///
  178. /// @param should_bypass
  179. /// Whether or not to bypass automatic encryption.
  180. ///
  181. /// @return
  182. /// A reference to this object to facilitate method chaining.
  183. ///
  184. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  185. ///
  186. auto_encryption& bypass_auto_encryption(bool should_bypass);
  187. ///
  188. /// Gets a boolean specifying whether or not auto encryption is bypassed.
  189. ///
  190. /// @return
  191. /// A boolean specifying whether auto encryption is bypassed.
  192. ///
  193. bool bypass_auto_encryption() const;
  194. ///
  195. /// Set extra options related to the mongocryptd process. This options
  196. /// document may include the following fields:
  197. ///
  198. /// - mongocryptdURI: string, defaults to "mongodb://localhost:27020".
  199. ///
  200. /// - mongocryptdBypassSpawn: bool, defaults to false.
  201. ///
  202. /// - mongocryptdSpawnPath: string, defaults to "" and spawns mongocryptd
  203. /// from the system path.
  204. ///
  205. /// - mongocryptdSpawnArgs: array[strings], options passed to mongocryptd
  206. /// when spawing. Defaults to ["--idleShutdownTimeoutSecs=60"].
  207. ///
  208. /// @param extra
  209. /// The extra options to set.
  210. ///
  211. /// @return
  212. /// A reference to this object to facilitate method chaining.
  213. ///
  214. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  215. ///
  216. auto_encryption& extra_options(bsoncxx::document::view_or_value extra);
  217. ///
  218. /// Gets extra options related to the mongocryptd process.
  219. ///
  220. /// @return
  221. /// An optional document containing the extra options.
  222. ///
  223. const stdx::optional<bsoncxx::document::view_or_value>& extra_options() const;
  224. private:
  225. friend class mongocxx::client;
  226. friend class mongocxx::pool;
  227. MONGOCXX_PRIVATE void* convert() const;
  228. bool _bypass;
  229. stdx::optional<mongocxx::client*> _key_vault_client;
  230. stdx::optional<mongocxx::pool*> _key_vault_pool;
  231. stdx::optional<ns_pair> _key_vault_namespace;
  232. stdx::optional<bsoncxx::document::view_or_value> _kms_providers;
  233. stdx::optional<bsoncxx::document::view_or_value> _schema_map;
  234. stdx::optional<bsoncxx::document::view_or_value> _extra_options;
  235. };
  236. } // namespace options
  237. MONGOCXX_INLINE_NAMESPACE_END
  238. } // namespace mongocxx