encrypter.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Encrypter = void 0;
  4. const util_1 = require("util");
  5. const auto_encrypter_1 = require("./client-side-encryption/auto_encrypter");
  6. const constants_1 = require("./constants");
  7. const deps_1 = require("./deps");
  8. const error_1 = require("./error");
  9. const mongo_client_1 = require("./mongo_client");
  10. /** @internal */
  11. const kInternalClient = Symbol('internalClient');
  12. /** @internal */
  13. class Encrypter {
  14. constructor(client, uri, options) {
  15. if (typeof options.autoEncryption !== 'object') {
  16. throw new error_1.MongoInvalidArgumentError('Option "autoEncryption" must be specified');
  17. }
  18. // initialize to null, if we call getInternalClient, we may set this it is important to not overwrite those function calls.
  19. this[kInternalClient] = null;
  20. this.bypassAutoEncryption = !!options.autoEncryption.bypassAutoEncryption;
  21. this.needsConnecting = false;
  22. if (options.maxPoolSize === 0 && options.autoEncryption.keyVaultClient == null) {
  23. options.autoEncryption.keyVaultClient = client;
  24. }
  25. else if (options.autoEncryption.keyVaultClient == null) {
  26. options.autoEncryption.keyVaultClient = this.getInternalClient(client, uri, options);
  27. }
  28. if (this.bypassAutoEncryption) {
  29. options.autoEncryption.metadataClient = undefined;
  30. }
  31. else if (options.maxPoolSize === 0) {
  32. options.autoEncryption.metadataClient = client;
  33. }
  34. else {
  35. options.autoEncryption.metadataClient = this.getInternalClient(client, uri, options);
  36. }
  37. if (options.proxyHost) {
  38. options.autoEncryption.proxyOptions = {
  39. proxyHost: options.proxyHost,
  40. proxyPort: options.proxyPort,
  41. proxyUsername: options.proxyUsername,
  42. proxyPassword: options.proxyPassword
  43. };
  44. }
  45. this.autoEncrypter = new auto_encrypter_1.AutoEncrypter(client, options.autoEncryption);
  46. }
  47. getInternalClient(client, uri, options) {
  48. // TODO(NODE-4144): Remove new variable for type narrowing
  49. let internalClient = this[kInternalClient];
  50. if (internalClient == null) {
  51. const clonedOptions = {};
  52. for (const key of [
  53. ...Object.getOwnPropertyNames(options),
  54. ...Object.getOwnPropertySymbols(options)
  55. ]) {
  56. if (['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].includes(key))
  57. continue;
  58. Reflect.set(clonedOptions, key, Reflect.get(options, key));
  59. }
  60. clonedOptions.minPoolSize = 0;
  61. internalClient = new mongo_client_1.MongoClient(uri, clonedOptions);
  62. this[kInternalClient] = internalClient;
  63. for (const eventName of constants_1.MONGO_CLIENT_EVENTS) {
  64. for (const listener of client.listeners(eventName)) {
  65. internalClient.on(eventName, listener);
  66. }
  67. }
  68. client.on('newListener', (eventName, listener) => {
  69. internalClient?.on(eventName, listener);
  70. });
  71. this.needsConnecting = true;
  72. }
  73. return internalClient;
  74. }
  75. async connectInternalClient() {
  76. // TODO(NODE-4144): Remove new variable for type narrowing
  77. const internalClient = this[kInternalClient];
  78. if (this.needsConnecting && internalClient != null) {
  79. this.needsConnecting = false;
  80. await internalClient.connect();
  81. }
  82. }
  83. closeCallback(client, force, callback) {
  84. (0, util_1.callbackify)(this.close.bind(this))(client, force, callback);
  85. }
  86. async close(client, force) {
  87. const maybeError = await this.autoEncrypter.teardown(!!force).catch(e => e);
  88. const internalClient = this[kInternalClient];
  89. if (internalClient != null && client !== internalClient) {
  90. return internalClient.close(force);
  91. }
  92. if (maybeError) {
  93. throw maybeError;
  94. }
  95. }
  96. static checkForMongoCrypt() {
  97. const mongodbClientEncryption = (0, deps_1.getMongoDBClientEncryption)();
  98. if ('kModuleError' in mongodbClientEncryption) {
  99. throw new error_1.MongoMissingDependencyError('Auto-encryption requested, but the module is not installed. ' +
  100. 'Please add `mongodb-client-encryption` as a dependency of your project');
  101. }
  102. }
  103. }
  104. exports.Encrypter = Encrypter;
  105. //# sourceMappingURL=encrypter.js.map