encrypter.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Encrypter = void 0;
  4. /* eslint-disable @typescript-eslint/no-var-requires */
  5. const bson_1 = require("./bson");
  6. const constants_1 = require("./constants");
  7. const error_1 = require("./error");
  8. const mongo_client_1 = require("./mongo_client");
  9. let AutoEncrypterClass;
  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. options.autoEncryption.bson = Object.create(null);
  46. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  47. options.autoEncryption.bson.serialize = bson_1.serialize;
  48. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  49. options.autoEncryption.bson.deserialize = bson_1.deserialize;
  50. this.autoEncrypter = new AutoEncrypterClass(client, options.autoEncryption);
  51. }
  52. getInternalClient(client, uri, options) {
  53. // TODO(NODE-4144): Remove new variable for type narrowing
  54. let internalClient = this[kInternalClient];
  55. if (internalClient == null) {
  56. const clonedOptions = {};
  57. for (const key of [
  58. ...Object.getOwnPropertyNames(options),
  59. ...Object.getOwnPropertySymbols(options)
  60. ]) {
  61. if (['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].includes(key))
  62. continue;
  63. Reflect.set(clonedOptions, key, Reflect.get(options, key));
  64. }
  65. clonedOptions.minPoolSize = 0;
  66. internalClient = new mongo_client_1.MongoClient(uri, clonedOptions);
  67. this[kInternalClient] = internalClient;
  68. for (const eventName of constants_1.MONGO_CLIENT_EVENTS) {
  69. for (const listener of client.listeners(eventName)) {
  70. internalClient.on(eventName, listener);
  71. }
  72. }
  73. client.on('newListener', (eventName, listener) => {
  74. internalClient === null || internalClient === void 0 ? void 0 : internalClient.on(eventName, listener);
  75. });
  76. this.needsConnecting = true;
  77. }
  78. return internalClient;
  79. }
  80. connectInternalClient(callback) {
  81. // TODO(NODE-4144): Remove new variable for type narrowing
  82. const internalClient = this[kInternalClient];
  83. if (this.needsConnecting && internalClient != null) {
  84. this.needsConnecting = false;
  85. return internalClient.connect(callback);
  86. }
  87. return callback();
  88. }
  89. close(client, force, callback) {
  90. this.autoEncrypter.teardown(!!force, e => {
  91. const internalClient = this[kInternalClient];
  92. if (internalClient != null && client !== internalClient) {
  93. return internalClient.close(force, callback);
  94. }
  95. callback(e);
  96. });
  97. }
  98. static checkForMongoCrypt() {
  99. let mongodbClientEncryption = undefined;
  100. // Ensure you always wrap an optional require in the try block NODE-3199
  101. try {
  102. // Note (NODE-4254): This is to get around the circular dependency between
  103. // mongodb-client-encryption and the driver in the test scenarios.
  104. if (process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE) {
  105. mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
  106. }
  107. else {
  108. mongodbClientEncryption = require('mongodb-client-encryption');
  109. }
  110. }
  111. catch (err) {
  112. throw new error_1.MongoMissingDependencyError('Auto-encryption requested, but the module is not installed. ' +
  113. 'Please add `mongodb-client-encryption` as a dependency of your project');
  114. }
  115. AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
  116. }
  117. }
  118. exports.Encrypter = Encrypter;
  119. //# sourceMappingURL=encrypter.js.map