encrypter.js
4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const MongoClient = require('./mongo_client');
const BSON = require('./core/connection/utils').retrieveBSON();
const MongoError = require('./core/error').MongoError;
let mongodbClientEncryption = undefined;
try {
// Ensure you always wrap an optional require in the try block NODE-3199
mongodbClientEncryption = require('mongodb-client-encryption');
} catch (err) {
throw new MongoError(
'Auto-encryption requested, but the module is not installed. ' +
'Please add `mongodb-client-encryption` as a dependency of your project'
);
}
if (
mongodbClientEncryption === undefined ||
typeof mongodbClientEncryption.extension !== 'function'
) {
throw new MongoError(
'loaded version of `mongodb-client-encryption` does not have property `extension`. ' +
'Please make sure you are loading the correct version of `mongodb-client-encryption`'
);
}
const AutoEncrypter = mongodbClientEncryption.extension(require('../index')).AutoEncrypter;
const kInternalClient = Symbol('internalClient');
class Encrypter {
/**
* @param {MongoClient} client
* @param {{autoEncryption: import('./mongo_client').AutoEncryptionOptions, bson: object}} options
*/
constructor(client, options) {
this.bypassAutoEncryption = !!options.autoEncryption.bypassAutoEncryption;
this.needsConnecting = false;
if (options.maxPoolSize === 0 && options.autoEncryption.keyVaultClient == null) {
options.autoEncryption.keyVaultClient = client;
} else if (options.autoEncryption.keyVaultClient == null) {
options.autoEncryption.keyVaultClient = this.getInternalClient(client);
}
if (this.bypassAutoEncryption) {
options.autoEncryption.metadataClient = undefined;
} else if (options.maxPoolSize === 0) {
options.autoEncryption.metadataClient = client;
} else {
options.autoEncryption.metadataClient = this.getInternalClient(client);
}
options.autoEncryption.bson = Encrypter.makeBSON(options);
this.autoEncrypter = new AutoEncrypter(client, options.autoEncryption);
}
getInternalClient(client) {
if (!this[kInternalClient]) {
const clonedOptions = {};
for (const key of Object.keys(client.s.options)) {
if (
['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].indexOf(key) !==
-1
)
continue;
clonedOptions[key] = client.s.options[key];
}
clonedOptions.minPoolSize = 0;
const allEvents = [
// APM
'commandStarted',
'commandSucceeded',
'commandFailed',
// SDAM
'serverOpening',
'serverClosed',
'serverDescriptionChanged',
'serverHeartbeatStarted',
'serverHeartbeatSucceeded',
'serverHeartbeatFailed',
'topologyOpening',
'topologyClosed',
'topologyDescriptionChanged',
// Legacy
'joined',
'left',
'ping',
'ha',
// CMAP
'connectionPoolCreated',
'connectionPoolClosed',
'connectionCreated',
'connectionReady',
'connectionClosed',
'connectionCheckOutStarted',
'connectionCheckOutFailed',
'connectionCheckedOut',
'connectionCheckedIn',
'connectionPoolCleared'
];
this[kInternalClient] = new MongoClient(client.s.url, clonedOptions);
for (const eventName of allEvents) {
for (const listener of client.listeners(eventName)) {
this[kInternalClient].on(eventName, listener);
}
}
client.on('newListener', (eventName, listener) => {
this[kInternalClient].on(eventName, listener);
});
this.needsConnecting = true;
}
return this[kInternalClient];
}
connectInternalClient(callback) {
if (this.needsConnecting) {
this.needsConnecting = false;
return this[kInternalClient].connect(callback);
}
return callback();
}
close(client, force, callback) {
this.autoEncrypter.teardown(e => {
if (this[kInternalClient] && client !== this[kInternalClient]) {
return this[kInternalClient].close(force, callback);
}
callback(e);
});
}
static makeBSON(options) {
return (
(options || {}).bson ||
new BSON([
BSON.Binary,
BSON.Code,
BSON.DBRef,
BSON.Decimal128,
BSON.Double,
BSON.Int32,
BSON.Long,
BSON.Map,
BSON.MaxKey,
BSON.MinKey,
BSON.ObjectId,
BSON.BSONRegExp,
BSON.Symbol,
BSON.Timestamp
])
);
}
}
module.exports = { Encrypter };