-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathwrapper.js
More file actions
346 lines (329 loc) · 13 KB
/
wrapper.js
File metadata and controls
346 lines (329 loc) · 13 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const async = require('async');
const { errors } = require('arsenal');
const { config } = require('../Config');
const logger = require('../utilities/logger');
const inMemory = require('./in_memory/backend').backend;
const file = require('./file/backend');
const KMIPClient = require('arsenal').network.kmipClient;
const AWSClient = require('arsenal').network.awsClient;
const Common = require('./common');
let scalityKMS;
let scalityKMSImpl;
try {
// eslint-disable-next-line import/no-unresolved
const ScalityKMS = require('scality-kms');
scalityKMS = new ScalityKMS(config.kms);
scalityKMSImpl = 'scalityKms';
} catch (error) {
logger.warn('scality kms unavailable. ' +
'Using file kms backend unless mem specified.',
{ error });
scalityKMS = file;
scalityKMSImpl = 'fileKms';
}
let client;
let implName;
if (config.backends.kms === 'mem') {
client = inMemory;
implName = 'memoryKms';
} else if (config.backends.kms === 'file' || config.backends.kms === 'cdmi') {
client = file;
implName = 'fileKms';
} else if (config.backends.kms === 'scality') {
client = scalityKMS;
implName = scalityKMSImpl;
} else if (config.backends.kms === 'kmip') {
const kmipConfig = { kmip: config.kmip };
if (!kmipConfig.kmip) {
throw new Error('KMIP KMS driver configuration is missing.');
}
client = new KMIPClient(kmipConfig);
implName = 'kmip';
} else if (config.backends.kms === 'aws') {
const awsConfig = { kmsAWS: config.kmsAWS };
client = new AWSClient(awsConfig);
implName = 'aws';
} else {
throw new Error('KMS backend is not configured');
}
class KMS {
/**
*
* @param {string} bucketName - bucket name
* @param {object} log - logger object
* @param {function} cb - callback
* @returns {undefined}
* @callback called with (err, masterKeyId: string)
*/
static createBucketKey(bucketName, log, cb) {
log.debug('creating a new bucket key');
client.createBucketKey(bucketName, log, (err, masterKeyId) => {
if (err) {
log.debug('error from kms', { implName, error: err });
return cb(err);
}
log.trace('bucket key created in kms');
return cb(null, masterKeyId);
});
}
/**
*
* @param {string} bucketName - bucket name
* @param {object} sseConfig - SSE configuration
* @param {object} log - logger object
* @param {function} cb - callback
* @returns {undefined}
* @callback called with (err, serverSideEncryptionInfo: object)
*/
static bucketLevelEncryption(bucketName, sseConfig, log, cb) {
/*
The purpose of bucket level encryption is so that the client does not
have to send appropriate headers to trigger encryption on each object
put in an "encrypted bucket". Customer provided keys are not
feasible in this system because we do not want to store this key
in the bucket metadata.
*/
const { algorithm, configuredMasterKeyId, mandatory } = sseConfig;
const _mandatory = mandatory === true;
if (algorithm === 'AES256' || algorithm === 'aws:kms') {
return this.createBucketKey(bucketName, log, (err, masterKeyId) => {
if (err) {
return cb(err);
}
const serverSideEncryptionInfo = {
cryptoScheme: 1,
algorithm,
masterKeyId,
mandatory: _mandatory,
};
if (algorithm === 'aws:kms' && configuredMasterKeyId) {
serverSideEncryptionInfo.configuredMasterKeyId = configuredMasterKeyId;
}
return cb(null, serverSideEncryptionInfo);
});
}
/*
* no encryption
*/
return cb(null, null);
}
/**
*
* @param {string} bucketKeyId - the Id of the bucket key
* @param {object} log - logger object
* @param {function} cb - callback
* @returns {undefined}
* @callback called with (err)
*/
static destroyBucketKey(bucketKeyId, log, cb) {
log.debug('deleting bucket key', { bucketKeyId });
client.destroyBucketKey(bucketKeyId, log, err => {
if (err) {
log.debug('error from kms', { implName, error: err });
return cb(err);
}
log.trace('bucket key destroyed in kms');
return cb(null);
});
}
/**
* createCipherBundle
* @param {object} serverSideEncryptionInfo - info for encryption
* @param {number} serverSideEncryptionInfo.cryptoScheme -
* cryptoScheme used
* @param {string} serverSideEncryptionInfo.algorithm -
* algorithm to use
* @param {string} serverSideEncryptionInfo.masterKeyId -
* key to get master key
* @param {boolean} serverSideEncryptionInfo.mandatory -
* true for mandatory encryption
* @param {object} log - logger object
* @param {function} cb - cb from external call
* @returns {undefined}
* @callback called with (err, cipherBundle)
*/
static createCipherBundle(serverSideEncryptionInfo,
log, cb) {
const { algorithm, configuredMasterKeyId, masterKeyId: bucketMasterKeyId } = serverSideEncryptionInfo;
let masterKeyId = bucketMasterKeyId;
if (configuredMasterKeyId) {
log.debug('using user configured kms master key id');
masterKeyId = configuredMasterKeyId;
}
const cipherBundle = {
algorithm,
masterKeyId,
cryptoScheme: 1,
cipheredDataKey: null,
cipher: null,
};
async.waterfall([
function generateDataKey(next) {
/* There are 2 ways of generating a datakey :
- using the generateDataKey of the KMS backend if it exists
(currently only implemented for the AWS KMS backend). This is
the prefered solution since a dedicated KMS should offer a better
entropy for generating random content.
- using local random number generation, and then use the KMS to
encrypt the datakey. This method is used when the KMS backend doesn't
provide the generateDataKey method.
*/
let res;
if (client.generateDataKey) {
log.debug('creating a data key using the KMS');
res = client.generateDataKey(cipherBundle.cryptoScheme,
cipherBundle.masterKeyId,
log, (err, plainTextDataKey, cipheredDataKey) => {
if (err) {
log.debug('error from kms',
{ implName, error: err });
return next(err);
}
log.trace('data key generated by the kms');
return next(null, plainTextDataKey, cipheredDataKey);
});
} else {
log.debug('creating a new data key');
const dataKey = Common.createDataKey();
log.debug('ciphering the data key');
res = client.cipherDataKey(cipherBundle.cryptoScheme,
cipherBundle.masterKeyId,
dataKey, log, (err, cipheredDataKey) => {
if (err) {
log.debug('error from kms',
{ implName, error: err });
return next(err);
}
log.trace('data key ciphered by the kms');
return next(null, dataKey, cipheredDataKey);
});
}
return res;
},
function createCipher(plainTextDataKey, cipheredDataKey, next) {
log.debug('creating a cipher');
cipherBundle.cipheredDataKey =
cipheredDataKey.toString('base64');
return Common.createCipher(cipherBundle.cryptoScheme,
plainTextDataKey, 0, log, (err, cipher) => {
plainTextDataKey.fill(0);
if (err) {
log.debug('error from kms',
{ implName, error: err });
return next(err);
}
log.trace('cipher created by the kms');
return next(null, cipher);
});
},
function finishCipherBundle(cipher, next) {
cipherBundle.cipher = cipher;
return next(null, cipherBundle);
},
], (err, cipherBundle) => {
if (err) {
log.error('error processing cipher bundle',
{ implName, error: err });
}
return cb(err, cipherBundle);
});
}
/**
* createDecipherBundle
* @param {object} serverSideEncryptionInfo - info for decryption
* @param {number} serverSideEncryptionInfo.cryptoScheme -
* cryptoScheme used
* @param {string} serverSideEncryptionInfo.algorithm -
* algorithm to use
* @param {string} serverSideEncryptionInfo.masterKeyId -
* key to get master key
* @param {boolean} serverSideEncryptionInfo.mandatory -
* true for mandatory encryption
* @param {buffer} serverSideEncryptionInfo.cipheredDataKey -
* ciphered data key
* @param {number} offset - offset for decryption
* @param {object} log - logger object
* @param {function} cb - cb from external call
* @returns {undefined}
* @callback called with (err, decipherBundle)
*/
static createDecipherBundle(serverSideEncryptionInfo, offset,
log, cb) {
if (!serverSideEncryptionInfo.masterKeyId ||
!serverSideEncryptionInfo.cipheredDataKey ||
!serverSideEncryptionInfo.cryptoScheme) {
log.error('Invalid cryptographic information', { implName });
return cb(errors.InternalError);
}
const decipherBundle = {
cryptoScheme: serverSideEncryptionInfo.cryptoScheme,
decipher: null,
};
return async.waterfall([
function decipherDataKey(next) {
return client.decipherDataKey(
decipherBundle.cryptoScheme,
serverSideEncryptionInfo.masterKeyId,
serverSideEncryptionInfo.cipheredDataKey,
log, (err, plainTextDataKey) => {
log.debug('deciphering a data key');
if (err) {
log.debug('error from kms',
{ implName, error: err });
return next(err);
}
log.trace('data key deciphered by the kms');
return next(null, plainTextDataKey);
});
},
function createDecipher(plainTextDataKey, next) {
log.debug('creating a decipher');
return Common.createDecipher(decipherBundle.cryptoScheme,
plainTextDataKey, offset, log, (err, decipher) => {
plainTextDataKey.fill(0);
if (err) {
log.debug('error from kms',
{ implName, error: err });
return next(err);
}
log.trace('decipher created by the kms');
return next(null, decipher);
});
},
function finishDecipherBundle(decipher, next) {
decipherBundle.decipher = decipher;
return next(null, decipherBundle);
},
], (err, decipherBundle) => {
if (err) {
log.error('error processing decipher bundle',
{ implName, error: err });
return cb(err);
}
return cb(err, decipherBundle);
});
}
static checkHealth(log, cb) {
if (!client.healthcheck) {
return cb(null, {
[implName]: { code: 200, message: 'OK' },
});
}
return client.healthcheck(log, err => {
const respBody = {};
if (err) {
respBody[implName] = {
error: err.description,
code: err.code,
};
} else {
respBody[implName] = {
code: 200,
message: 'OK',
};
}
return cb(null, respBody);
});
}
}
module.exports = KMS;