auto_encrypter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. "use strict";
  2. var _a;
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.AutoEncrypter = exports.AutoEncryptionLoggerLevel = void 0;
  5. const net = require("net");
  6. const bson_1 = require("../bson");
  7. const constants_1 = require("../constants");
  8. const deps_1 = require("../deps");
  9. const error_1 = require("../error");
  10. const mongo_client_1 = require("../mongo_client");
  11. const utils_1 = require("../utils");
  12. const client_encryption_1 = require("./client_encryption");
  13. const errors_1 = require("./errors");
  14. const mongocryptd_manager_1 = require("./mongocryptd_manager");
  15. const providers_1 = require("./providers");
  16. const state_machine_1 = require("./state_machine");
  17. /** @public */
  18. exports.AutoEncryptionLoggerLevel = Object.freeze({
  19. FatalError: 0,
  20. Error: 1,
  21. Warning: 2,
  22. Info: 3,
  23. Trace: 4
  24. });
  25. /**
  26. * @internal An internal class to be used by the driver for auto encryption
  27. * **NOTE**: Not meant to be instantiated directly, this is for internal use only.
  28. */
  29. class AutoEncrypter {
  30. static { _a = constants_1.kDecorateResult; }
  31. /** @internal */
  32. static getMongoCrypt() {
  33. const encryption = (0, deps_1.getMongoDBClientEncryption)();
  34. if ('kModuleError' in encryption) {
  35. throw encryption.kModuleError;
  36. }
  37. return encryption.MongoCrypt;
  38. }
  39. /**
  40. * Create an AutoEncrypter
  41. *
  42. * **Note**: Do not instantiate this class directly. Rather, supply the relevant options to a MongoClient
  43. *
  44. * **Note**: Supplying `options.schemaMap` provides more security than relying on JSON Schemas obtained from the server.
  45. * It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted data that should be encrypted.
  46. * Schemas supplied in the schemaMap only apply to configuring automatic encryption for Client-Side Field Level Encryption.
  47. * Other validation rules in the JSON schema will not be enforced by the driver and will result in an error.
  48. *
  49. * @example <caption>Create an AutoEncrypter that makes use of mongocryptd</caption>
  50. * ```ts
  51. * // Enabling autoEncryption via a MongoClient using mongocryptd
  52. * const { MongoClient } = require('mongodb');
  53. * const client = new MongoClient(URL, {
  54. * autoEncryption: {
  55. * kmsProviders: {
  56. * aws: {
  57. * accessKeyId: AWS_ACCESS_KEY,
  58. * secretAccessKey: AWS_SECRET_KEY
  59. * }
  60. * }
  61. * }
  62. * });
  63. * ```
  64. *
  65. * await client.connect();
  66. * // From here on, the client will be encrypting / decrypting automatically
  67. * @example <caption>Create an AutoEncrypter that makes use of libmongocrypt's CSFLE shared library</caption>
  68. * ```ts
  69. * // Enabling autoEncryption via a MongoClient using CSFLE shared library
  70. * const { MongoClient } = require('mongodb');
  71. * const client = new MongoClient(URL, {
  72. * autoEncryption: {
  73. * kmsProviders: {
  74. * aws: {}
  75. * },
  76. * extraOptions: {
  77. * cryptSharedLibPath: '/path/to/local/crypt/shared/lib',
  78. * cryptSharedLibRequired: true
  79. * }
  80. * }
  81. * });
  82. * ```
  83. *
  84. * await client.connect();
  85. * // From here on, the client will be encrypting / decrypting automatically
  86. */
  87. constructor(client, options) {
  88. /**
  89. * Used by devtools to enable decorating decryption results.
  90. *
  91. * When set and enabled, `decrypt` will automatically recursively
  92. * traverse a decrypted document and if a field has been decrypted,
  93. * it will mark it as decrypted. Compass uses this to determine which
  94. * fields were decrypted.
  95. */
  96. this[_a] = false;
  97. this._client = client;
  98. this._bypassEncryption = options.bypassAutoEncryption === true;
  99. this._keyVaultNamespace = options.keyVaultNamespace || 'admin.datakeys';
  100. this._keyVaultClient = options.keyVaultClient || client;
  101. this._metaDataClient = options.metadataClient || client;
  102. this._proxyOptions = options.proxyOptions || {};
  103. this._tlsOptions = options.tlsOptions || {};
  104. this._kmsProviders = options.kmsProviders || {};
  105. this._credentialProviders = options.credentialProviders;
  106. if (options.credentialProviders?.aws && !(0, providers_1.isEmptyCredentials)('aws', this._kmsProviders)) {
  107. throw new errors_1.MongoCryptInvalidArgumentError('Can only provide a custom AWS credential provider when the state machine is configured for automatic AWS credential fetching');
  108. }
  109. const mongoCryptOptions = {
  110. errorWrapper: errors_1.defaultErrorWrapper
  111. };
  112. if (options.schemaMap) {
  113. mongoCryptOptions.schemaMap = Buffer.isBuffer(options.schemaMap)
  114. ? options.schemaMap
  115. : (0, bson_1.serialize)(options.schemaMap);
  116. }
  117. if (options.encryptedFieldsMap) {
  118. mongoCryptOptions.encryptedFieldsMap = Buffer.isBuffer(options.encryptedFieldsMap)
  119. ? options.encryptedFieldsMap
  120. : (0, bson_1.serialize)(options.encryptedFieldsMap);
  121. }
  122. mongoCryptOptions.kmsProviders = !Buffer.isBuffer(this._kmsProviders)
  123. ? (0, bson_1.serialize)(this._kmsProviders)
  124. : this._kmsProviders;
  125. if (options.options?.logger) {
  126. mongoCryptOptions.logger = options.options.logger;
  127. }
  128. if (options.extraOptions && options.extraOptions.cryptSharedLibPath) {
  129. mongoCryptOptions.cryptSharedLibPath = options.extraOptions.cryptSharedLibPath;
  130. }
  131. if (options.bypassQueryAnalysis) {
  132. mongoCryptOptions.bypassQueryAnalysis = options.bypassQueryAnalysis;
  133. }
  134. if (options.keyExpirationMS != null) {
  135. mongoCryptOptions.keyExpirationMS = options.keyExpirationMS;
  136. }
  137. this._bypassMongocryptdAndCryptShared = this._bypassEncryption || !!options.bypassQueryAnalysis;
  138. if (options.extraOptions && options.extraOptions.cryptSharedLibSearchPaths) {
  139. // Only for driver testing
  140. mongoCryptOptions.cryptSharedLibSearchPaths = options.extraOptions.cryptSharedLibSearchPaths;
  141. }
  142. else if (!this._bypassMongocryptdAndCryptShared) {
  143. mongoCryptOptions.cryptSharedLibSearchPaths = ['$SYSTEM'];
  144. }
  145. const MongoCrypt = AutoEncrypter.getMongoCrypt();
  146. this._mongocrypt = new MongoCrypt(mongoCryptOptions);
  147. this._contextCounter = 0;
  148. if (options.extraOptions &&
  149. options.extraOptions.cryptSharedLibRequired &&
  150. !this.cryptSharedLibVersionInfo) {
  151. throw new errors_1.MongoCryptInvalidArgumentError('`cryptSharedLibRequired` set but no crypt_shared library loaded');
  152. }
  153. // Only instantiate mongocryptd manager/client once we know for sure
  154. // that we are not using the CSFLE shared library.
  155. if (!this._bypassMongocryptdAndCryptShared && !this.cryptSharedLibVersionInfo) {
  156. this._mongocryptdManager = new mongocryptd_manager_1.MongocryptdManager(options.extraOptions);
  157. const clientOptions = {
  158. serverSelectionTimeoutMS: 10000
  159. };
  160. if ((options.extraOptions == null || typeof options.extraOptions.mongocryptdURI !== 'string') &&
  161. !net.getDefaultAutoSelectFamily) {
  162. // Only set family if autoSelectFamily options are not supported.
  163. clientOptions.family = 4;
  164. }
  165. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  166. // @ts-ignore: TS complains as this always returns true on versions where it is present.
  167. if (net.getDefaultAutoSelectFamily) {
  168. // AutoEncrypter is made inside of MongoClient constructor while options are being parsed,
  169. // we do not have access to the options that are in progress.
  170. // TODO(NODE-6449): AutoEncrypter does not use client options for autoSelectFamily
  171. Object.assign(clientOptions, (0, client_encryption_1.autoSelectSocketOptions)(this._client.s?.options ?? {}));
  172. }
  173. this._mongocryptdClient = new mongo_client_1.MongoClient(this._mongocryptdManager.uri, clientOptions);
  174. }
  175. }
  176. /**
  177. * Initializes the auto encrypter by spawning a mongocryptd and connecting to it.
  178. *
  179. * This function is a no-op when bypassSpawn is set or the crypt shared library is used.
  180. */
  181. async init() {
  182. if (this._bypassMongocryptdAndCryptShared || this.cryptSharedLibVersionInfo) {
  183. return;
  184. }
  185. if (!this._mongocryptdManager) {
  186. throw new error_1.MongoRuntimeError('Reached impossible state: mongocryptdManager is undefined when neither bypassSpawn nor the shared lib are specified.');
  187. }
  188. if (!this._mongocryptdClient) {
  189. throw new error_1.MongoRuntimeError('Reached impossible state: mongocryptdClient is undefined when neither bypassSpawn nor the shared lib are specified.');
  190. }
  191. if (!this._mongocryptdManager.bypassSpawn) {
  192. await this._mongocryptdManager.spawn();
  193. }
  194. try {
  195. const client = await this._mongocryptdClient.connect();
  196. return client;
  197. }
  198. catch (error) {
  199. throw new error_1.MongoRuntimeError('Unable to connect to `mongocryptd`, please make sure it is running or in your PATH for auto-spawn', { cause: error });
  200. }
  201. }
  202. /**
  203. * Cleans up the `_mongocryptdClient`, if present.
  204. */
  205. async close() {
  206. await this._mongocryptdClient?.close();
  207. }
  208. /**
  209. * Encrypt a command for a given namespace.
  210. */
  211. async encrypt(ns, cmd, options = {}) {
  212. options.signal?.throwIfAborted();
  213. if (this._bypassEncryption) {
  214. // If `bypassAutoEncryption` has been specified, don't encrypt
  215. return cmd;
  216. }
  217. const commandBuffer = Buffer.isBuffer(cmd) ? cmd : (0, bson_1.serialize)(cmd, options);
  218. const context = this._mongocrypt.makeEncryptionContext(utils_1.MongoDBCollectionNamespace.fromString(ns).db, commandBuffer);
  219. context.id = this._contextCounter++;
  220. context.ns = ns;
  221. context.document = cmd;
  222. const stateMachine = new state_machine_1.StateMachine({
  223. promoteValues: false,
  224. promoteLongs: false,
  225. proxyOptions: this._proxyOptions,
  226. tlsOptions: this._tlsOptions,
  227. socketOptions: (0, client_encryption_1.autoSelectSocketOptions)(this._client.s.options)
  228. });
  229. return (0, bson_1.deserialize)(await stateMachine.execute(this, context, options), {
  230. promoteValues: false,
  231. promoteLongs: false
  232. });
  233. }
  234. /**
  235. * Decrypt a command response
  236. */
  237. async decrypt(response, options = {}) {
  238. options.signal?.throwIfAborted();
  239. const context = this._mongocrypt.makeDecryptionContext(response);
  240. context.id = this._contextCounter++;
  241. const stateMachine = new state_machine_1.StateMachine({
  242. ...options,
  243. proxyOptions: this._proxyOptions,
  244. tlsOptions: this._tlsOptions,
  245. socketOptions: (0, client_encryption_1.autoSelectSocketOptions)(this._client.s.options)
  246. });
  247. return await stateMachine.execute(this, context, options);
  248. }
  249. /**
  250. * Ask the user for KMS credentials.
  251. *
  252. * This returns anything that looks like the kmsProviders original input
  253. * option. It can be empty, and any provider specified here will override
  254. * the original ones.
  255. */
  256. async askForKMSCredentials() {
  257. return await (0, providers_1.refreshKMSCredentials)(this._kmsProviders, this._credentialProviders);
  258. }
  259. /**
  260. * Return the current libmongocrypt's CSFLE shared library version
  261. * as `{ version: bigint, versionStr: string }`, or `null` if no CSFLE
  262. * shared library was loaded.
  263. */
  264. get cryptSharedLibVersionInfo() {
  265. return this._mongocrypt.cryptSharedLibVersionInfo;
  266. }
  267. static get libmongocryptVersion() {
  268. return AutoEncrypter.getMongoCrypt().libmongocryptVersion;
  269. }
  270. }
  271. exports.AutoEncrypter = AutoEncrypter;
  272. //# sourceMappingURL=auto_encrypter.js.map