-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbucket.js
More file actions
372 lines (339 loc) · 12.9 KB
/
Copy pathbucket.js
File metadata and controls
372 lines (339 loc) · 12.9 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const Problem = require('api-problem');
const { UniqueViolationError } = require('objection');
const { NIL: SYSTEM_USER } = require('uuid');
const { DEFAULTREGION, Permissions } = require('../components/constants');
const utils = require('../db/models/utils');
const errorToProblem = require('../components/errorToProblem');
const log = require('../components/log')(module.filename);
const {
addDashesToUuid,
getCurrentIdentity,
isTruthy,
joinPath,
mixedQueryToArray,
stripDelimit
} = require('../components/utils');
const { redactSecrets } = require('../db/models/utils');
const { bucketService, storageService, userService } = require('../services');
const SERVICE = 'BucketService';
const secretFields = ['accessKeyId', 'secretAccessKey'];
/**
* The Bucket Controller
*/
const controller = {
/**
* @function _processS3Headers
* Accepts a typical S3 response object and inserts appropriate express response headers
* Returns an array of non-standard headers that need to be CORS exposed
* @param {object} s3Resp S3 response object
* @param {object} res Express response object
* @returns {string[]} An array of non-standard headers that need to be CORS exposed
*/
_processS3Headers(s3Resp, res) {
// TODO: Consider adding 'x-coms-public' and 'x-coms-path' headers into API spec?
const exposedHeaders = [];
if (s3Resp.ContentLength) res.set('Content-Length', s3Resp.ContentLength);
if (s3Resp.ContentType) res.set('Content-Type', s3Resp.ContentType);
if (s3Resp.ETag) {
const etag = 'ETag';
res.set(etag, s3Resp.ETag);
exposedHeaders.push(etag);
}
if (s3Resp.LastModified) res.set('Last-Modified', s3Resp.LastModified);
if (s3Resp.Metadata) {
Object.entries(s3Resp.Metadata).forEach(([key, value]) => {
const metadata = `x-amz-meta-${key}`;
res.set(metadata, value);
exposedHeaders.push(metadata);
});
if (s3Resp.Metadata.name) res.attachment(s3Resp.Metadata.name);
}
if (s3Resp.ServerSideEncryption) {
const sse = 'x-amz-server-side-encryption';
res.set(sse, s3Resp.ServerSideEncryption);
exposedHeaders.push(sse);
}
if (s3Resp.VersionId) {
const s3VersionId = 'x-amz-version-id';
res.set(s3VersionId, s3Resp.VersionId);
exposedHeaders.push(s3VersionId);
}
return exposedHeaders;
},
/**
* @function _validateCredentials
* Guard against creating or update a bucket with invalid creds
* @param {object} credentials The body of the request
* @throws A conflict error problem if the bucket is not reachable
*/
async _validateCredentials(credentials) {
try {
const bucketSettings = {
accessKeyId: credentials.accessKeyId,
bucket: credentials.bucket,
endpoint: credentials.endpoint,
key: credentials.key ? credentials.key : '/',
region: credentials.region || DEFAULTREGION,
secretAccessKey: credentials.secretAccessKey,
};
await storageService.headBucket(bucketSettings);
} catch (e) {
// If it's caught here it's unable to validate the supplied store/bucket and creds
log.warn(`Failure to validate bucket credentials: ${e.message}`, {
function: '_validateCredentials',
});
throw new Problem(403, {
detail: 'Unable to validate supplied credentials for the bucket',
});
}
},
/**
* @function createBucket
* Creates a bucket
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
* @throws The error encountered upon failure
*/
async createBucket(req, res, next) {
const data = {
...req.body,
endpoint: stripDelimit(req.body.endpoint),
key: req.body.key ? joinPath(stripDelimit(req.body.key)) : undefined
};
let response = undefined;
try {
// Check for credential accessibility/validity first
await controller._validateCredentials(data);
data.userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, SYSTEM_USER));
// if permCodes array (eg: ['READ', 'UPDATE'] or []) was provided use that (de-duped),
// otherwise assign all permissions
data.permCodes = req.body.permCodes ? Array.from(new Set(req.body.permCodes)) : Object.values(Permissions);
response = await bucketService.create(data);
} catch (e) {
// If bucket exists, check if credentials precisely match
if (e instanceof UniqueViolationError) {
// Grant permissions if credentials precisely match
response = await bucketService.checkGrantPermissions(data).catch(permErr => {
next(new Problem(409, { detail: permErr.message, instance: req.originalUrl }));
});
} else {
next(errorToProblem(SERVICE, e));
}
} finally {
if (response) res.status(201).json(redactSecrets(response, secretFields));
}
},
/**
* @function createBucketChild
* Creates a child bucket
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
* @throws The error encountered upon failure
*/
async createBucketChild(req, res, next) {
try {
// Get Parent bucket data
const parentBucketId = addDashesToUuid(req.params.bucketId);
const parentBucket = await bucketService.read(parentBucketId);
// Check new child key length
const childKey = joinPath(stripDelimit(parentBucket.key), stripDelimit(req.body.subKey));
if (childKey.length > 255) {
throw new Problem(422, {
detail: 'New derived key exceeds maximum length of 255',
instance: req.originalUrl,
key: childKey
});
}
// Future task: give user MANAGE permission on existing sub-folder (bucket) instead (see above)
// Check for existing bucket collision
const bucketCollision = await bucketService.readUnique({
bucket: parentBucket.bucket,
endpoint: parentBucket.endpoint,
key: childKey
}).catch(() => undefined);
if (bucketCollision) {
throw new Problem(409, {
bucketId: bucketCollision.bucketId,
detail: 'Requested bucket already exists',
instance: req.originalUrl,
key: childKey
});
}
// Check for credential accessibility/validity
const childBucket = {
bucketName: req.body.bucketName,
accessKeyId: parentBucket.accessKeyId,
bucket: parentBucket.bucket,
endpoint: parentBucket.endpoint,
key: childKey,
secretAccessKey: parentBucket.secretAccessKey,
region: parentBucket.region ?? undefined,
active: parentBucket.active
};
await controller._validateCredentials(childBucket);
childBucket.userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, SYSTEM_USER));
// assign all permissions
childBucket.permCodes = Object.values(Permissions);
// Create child bucket
const response = await bucketService.create(childBucket);
res.status(201).json(redactSecrets(response, secretFields));
} catch (e) {
next(errorToProblem(SERVICE, e));
}
},
/**
* @function deleteBucket
* Deletes a bucket
* Recursive option will delete all sub-folders (where current user has DELETE permission)
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
*/
async deleteBucket(req, res, next) {
try {
const bucketId = addDashesToUuid(req.params.bucketId);
const parentBucket = await bucketService.read(bucketId);
let buckets = [parentBucket];
// if doing recursive delete
if (isTruthy(req.query.recursive)) {
// if current user is OIDC
const userId = await userService.getCurrentUserId(
getCurrentIdentity(req.currentUser, SYSTEM_USER), SYSTEM_USER);
if (userId !== SYSTEM_USER) {
const dbChildBuckets = await bucketService.searchChildBuckets(parentBucket, true, userId);
// if there are sub-folders
if (dbChildBuckets.length > 0) {
const checkForDelete = obj => obj.permCode === 'DELETE';
const dbChildBucketsWithDelete = dbChildBuckets.filter(b =>
b.bucketPermission.some(checkForDelete));
// if user has DELETE on all subfolders
if (dbChildBucketsWithDelete.length === dbChildBuckets.length) {
buckets = buckets.concat(dbChildBucketsWithDelete);
} else {
throw new Problem(403, {
detail: 'User lacks DELETE permission for some sub-folders',
instance: req.originalUrl,
});
}
}
}
// else basic auth
else {
const dbChildBuckets = await bucketService.searchChildBuckets(parentBucket);
buckets = buckets.concat(dbChildBuckets);
}
}
// do delete
await utils.trxWrapper(async (trx) => {
return await Promise.all(
buckets.map(bucket => bucketService.delete(bucket.bucketId, trx))
);
});
res.status(204).end();
} catch (e) {
next(errorToProblem(SERVICE, e));
}
},
/**
* @function headBucket
* Returns bucket headers
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
*/
async headBucket(req, res, next) {
try {
const bucketId = addDashesToUuid(req.params.bucketId);
await storageService.headBucket({ bucketId });
res.status(204).end();
} catch (e) {
next(errorToProblem(SERVICE, e));
}
},
/**
* @function readBucket
* Returns a bucket
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
*/
async readBucket(req, res, next) {
try {
const bucketId = addDashesToUuid(req.params.bucketId);
const response = await bucketService.read(bucketId);
res.status(200).json(redactSecrets(response, secretFields));
} catch (e) {
next(errorToProblem(SERVICE, e));
}
},
/**
* @function searchBuckets
* Search and filter for specific buckets
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
*/
async searchBuckets(req, res, next) {
try {
const bucketIds = mixedQueryToArray(req.query.bucketId);
const params = {
bucketId: bucketIds ? bucketIds.map(id => addDashesToUuid(id)) : bucketIds,
bucketName: req.query.bucketName,
key: req.query.key,
active: isTruthy(req.query.active)
};
const response = await bucketService.searchBuckets(params);
res.status(200).json(response.map(bucket => redactSecrets(bucket, secretFields)));
} catch (error) {
next(error);
}
},
/**
* @function updateBucket
* Updates a bucket
* @param {object} req Express request object
* @param {object} res Express response object
* @param {function} next The next callback function
* @returns {function} Express middleware function
*/
async updateBucket(req, res, next) {
try {
const bucketId = addDashesToUuid(req.params.bucketId);
const currentBucket = await bucketService.read(bucketId);
// Check for credential accessibility/validity first
// Need to cross reference with existing data when partial patch data is provided
await controller._validateCredentials({
...currentBucket,
...req.body,
endpoint: req.body.endpoint ? stripDelimit(req.body.endpoint) : currentBucket.endpoint
});
let userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, SYSTEM_USER), SYSTEM_USER);
if ((userId === SYSTEM_USER || userId === undefined) && req.currentUser?.bucketSettings) {
userId = req.currentUser.bucketSettings.accessKeyId;
}
const response = await bucketService.update({
bucketId: bucketId,
bucketName: req.body.bucketName,
accessKeyId: req.body.accessKeyId,
bucket: req.body.bucket,
endpoint: req.body.endpoint ? stripDelimit(req.body.endpoint) : undefined,
secretAccessKey: req.body.secretAccessKey,
region: req.body.region,
active: isTruthy(req.body.active),
userId: userId
});
res.status(200).json(redactSecrets(response, secretFields));
} catch (e) {
next(errorToProblem(SERVICE, e));
}
}
};
module.exports = controller;