forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl.js
More file actions
540 lines (489 loc) · 14.4 KB
/
acl.js
File metadata and controls
540 lines (489 loc) · 14.4 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*!
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module storage/acl
*/
'use strict';
var nodeutil = require('util');
/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');
/**
* Google Cloud Storage uses access control lists (ACLs) to manage object and
* bucket access. ACLs are the mechanism you use to share objects with other
* users and allow other users to access your buckets and objects.
*
* An ACL consists of one or more entries, where each entry grants permissions
* to an entity. Permissions define the actions that can be performed against an
* object or bucket (for example, `READ` or `WRITE`); the entity defines who the
* permission applies to (for example, a specific user or group of users).
*
* Where an `entity` value is accepted, we follow the format the Cloud Storage
* API expects.
*
* Refer to
* https://cloud.google.com/storage/docs/json_api/v1/defaultObjectAccessControls
* for the most up-to-date values.
*
* - `user-userId`
* - `user-email`
* - `group-groupId`
* - `group-email`
* - `domain-domain`
* - `project-team-projectId`
* - `allUsers`
* - `allAuthenticatedUsers`
*
* Examples:
*
* - The user "liz@example.com" would be `user-liz@example.com`.
* - The group "example@googlegroups.com" would be
* `group-example@googlegroups.com`.
* - To refer to all members of the Google Apps for Business domain
* "example.com", the entity would be `domain-example.com`.
*
* For more detailed information, see
* [About Access Control Lists](http://goo.gl/6qBBPO).
*
* @constructor
* @alias module:storage/acl
*/
function Acl(options) {
AclRoleAccessorMethods.call(this);
this.makeReq = options.makeReq;
this.pathPrefix = options.pathPrefix;
}
/**
* An object of convenience methods to add or delete owner ACL permissions for a
* given entity.
*
* The supported methods include:
*
* - `myFile.acl.owners.addAllAuthenticatedUsers`
* - `myFile.acl.owners.deleteAllAuthenticatedUsers`
* - `myFile.acl.owners.addAllUsers`
* - `myFile.acl.owners.deleteAllUsers`
* - `myFile.acl.owners.addDomain`
* - `myFile.acl.owners.deleteDomain`
* - `myFile.acl.owners.addGroup`
* - `myFile.acl.owners.deleteGroup`
* - `myFile.acl.owners.addProject`
* - `myFile.acl.owners.deleteProject`
* - `myFile.acl.owners.addUser`
* - `myFile.acl.owners.deleteUser`
*
* @alias acl.owners
*
* @return {object}
*
* @example
* //-
* // Add a user as an owner of a file.
* //-
* myFile.acl.owners.addUser('email@example.com', function(err, aclObject) {});
*
* //-
* // For reference, the above command is the same as running the following.
* //-
* myFile.acl.add({
* entity: 'user-email@example.com',
* role: storage.acl.OWNER_ROLE
* }, function(err, aclObject) {});
*/
Acl.prototype.owners = {};
/**
* An object of convenience methods to add or delete reader ACL permissions for
* a given entity.
*
* The supported methods include:
*
* - `myFile.acl.readers.addAllAuthenticatedUsers`
* - `myFile.acl.readers.deleteAllAuthenticatedUsers`
* - `myFile.acl.readers.addAllUsers`
* - `myFile.acl.readers.deleteAllUsers`
* - `myFile.acl.readers.addDomain`
* - `myFile.acl.readers.deleteDomain`
* - `myFile.acl.readers.addGroup`
* - `myFile.acl.readers.deleteGroup`
* - `myFile.acl.readers.addProject`
* - `myFile.acl.readers.deleteProject`
* - `myFile.acl.readers.addUser`
* - `myFile.acl.readers.deleteUser`
*
* @alias acl.readers
*
* @return {object}
*
* @example
* //-
* // Add a user as a reader of a file.
* //-
* myFile.acl.readers.addUser('email@example.com', function(err, aclObject) {});
*
* //-
* // For reference, the above command is the same as running the following.
* //-
* myFile.acl.add({
* entity: 'user-email@example.com',
* role: storage.acl.READER_ROLE
* }, function(err, aclObject) {});
*/
Acl.prototype.readers = {};
/**
* An object of convenience methods to add or delete writer ACL permissions for
* a given entity.
*
* The supported methods include:
*
* - `myFile.acl.writers.addAllAuthenticatedUsers`
* - `myFile.acl.writers.deleteAllAuthenticatedUsers`
* - `myFile.acl.writers.addAllUsers`
* - `myFile.acl.writers.deleteAllUsers`
* - `myFile.acl.writers.addDomain`
* - `myFile.acl.writers.deleteDomain`
* - `myFile.acl.writers.addGroup`
* - `myFile.acl.writers.deleteGroup`
* - `myFile.acl.writers.addProject`
* - `myFile.acl.writers.deleteProject`
* - `myFile.acl.writers.addUser`
* - `myFile.acl.writers.deleteUser`
*
* @alias acl.writers
*
* @return {object}
*
* @example
* //-
* // Add a user as a writer of a file.
* //-
* myFile.acl.writers.addUser('email@example.com', function(err, aclObject) {});
*
* //-
* // For reference, the above command is the same as running the following.
* //-
* myFile.acl.add({
* entity: 'user-email@example.com',
* role: storage.acl.WRITER_ROLE
* }, function(err, aclObject) {});
*/
Acl.prototype.writers = {};
nodeutil.inherits(Acl, AclRoleAccessorMethods);
/**
* Add access controls on a {module:storage/bucket} or {module:storage/file}.
*
* @param {object} options - Configuration object.
* @param {string} options.entity - Whose permissions will be added.
* @param {string} options.role - Permissions allowed for the defined entity.
* See {module:storage#acl}.
* @param {int=} options.generation - **File Objects Only** Select a specific
* revision of this file (as opposed to the latest version, the default).
* @param {function} callback - The callback function.
*
* @alias acl.add
*
* @example
* myBucket.acl.add({
* entity: 'user-useremail@example.com',
* role: storage.acl.OWNER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
* // Here is how you would grant ownership permissions to a user on a specific
* // revision of a file.
* //-
* myFile.acl.add({
* entity: 'user-useremail@example.com',
* role: storage.acl.OWNER_ROLE,
* generation: 1
* }, function(err, aclObject) {});
*/
Acl.prototype.add = function(options, callback) {
var that = this;
var body = {
entity: options.entity,
role: options.role.toUpperCase()
};
var query = null;
if (options.generation) {
query = {
generation: options.generation
};
}
this.makeReq_('POST', '', query, body, function(err, resp) {
if (err) {
callback(err);
return;
}
callback(null, that.makeAclObject_(resp));
});
};
/**
* Delete access controls on a {module:storage/bucket} or {module:storage/file}.
*
* @param {object=} options - Configuration object.
* @param {string} options.entity - Whose permissions will be revoked.
* @param {int=} options.generation - **File Objects Only** Select a specific
* revision of this file (as opposed to the latest version, the default).
* @param {function} callback - The callback function.
*
* @alias acl.delete
*
* @example
* myBucket.acl.delete({
* entity: 'user-useremail@example.com'
* }, function(err) {});
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
* //-
* myFile.acl.delete({
* entity: 'user-useremail@example.com',
* generation: 1
* }, function(err) {});
*/
Acl.prototype.delete = function(options, callback) {
var path = '/' + encodeURIComponent(options.entity);
var query = null;
if (options.generation) {
query = {
generation: options.generation
};
}
this.makeReq_('DELETE', path, query, null, callback);
};
/**
* Get access controls on a {module:storage/bucket} or {module:storage/file}. If
* an entity is omitted, you will receive an array of all applicable access
* controls.
*
* @param {object|function} options - Configuration object. If you want to
* receive a list of all access controls, pass the callback function as the
* only argument.
* @param {string=} options.entity - Whose permissions will be fetched.
* @param {int=} options.generation - **File Objects Only** Select a specific
* revision of this file (as opposed to the latest version, the default).
*
* @alias acl.get
*
* @example
* myBucket.acl.get({
* entity: 'user-useremail@example.com'
* }, function(err, aclObject) {});
*
* //-
* // Get all access controls.
* //-
* myBucket.acl.get(function(err, aclObjects) {
* // aclObjects = [
* // {
* // entity: 'user-useremail@example.com',
* // role: 'owner'
* // }
* // ]
* });
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
* //-
* myFile.acl.get({
* entity: 'user-useremail@example.com',
* generation: 1
* } function(err, aclObject) {});
*/
Acl.prototype.get = function(options, callback) {
var that = this;
var path = '';
var query = null;
if (util.is(options, 'function')) {
callback = options;
options = null;
} else {
path = '/' + encodeURIComponent(options.entity);
if (options.generation) {
query = {
generation: options.generation
};
}
}
this.makeReq_('GET', path, query, null, function(err, resp) {
if (err) {
callback(err);
return;
}
var results = resp;
if (resp.items) {
results = (resp.items || []).map(that.makeAclObject_);
} else {
results = that.makeAclObject_(results);
}
callback(null, results);
});
};
/**
* Update access controls on a {module:storage/bucket} or {module:storage/file}.
*
* @param {object=} options - Configuration object.
* @param {string} options.entity - Whose permissions will be updated.
* @param {string} options.role - Permissions allowed for the defined entity.
* See {module:storage#acl}.
* @param {int=} options.generation - **File Objects Only** Select a specific
* revision of this file (as opposed to the latest version, the default).
* @param {function} callback - The callback function.
*
* @alias acl.update
*
* @example
* var storage = gcloud.storage();
*
* myBucket.acl.update({
* entity: 'user-useremail@example.com',
* role: storage.acl.WRITER_ROLE
* }, function(err) {});
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
* //-
* myFile.acl.update({
* entity: 'user-useremail@example.com',
* role: storage.acl.WRITER_ROLE,
* generation: 1
* }, function(err) {});
*/
Acl.prototype.update = function(options, callback) {
var that = this;
var path = '/' + encodeURIComponent(options.entity);
var query = null;
if (options.generation) {
query = {
generation: options.generation
};
}
var body = {
role: options.role.toUpperCase()
};
this.makeReq_('PUT', path, query, body, function(err, resp) {
if (err) {
callback(err);
return;
}
callback(null, that.makeAclObject_(resp));
});
};
/**
* Transform API responses to a consistent object format.
*
* @private
*/
Acl.prototype.makeAclObject_ = function(accessControlObject) {
var obj = {
entity: accessControlObject.entity,
role: accessControlObject.role
};
if (accessControlObject.projectTeam) {
obj.projectTeam = accessControlObject.projectTeam;
}
return obj;
};
/**
* Patch requests up to the bucket's request object.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Acl.prototype.makeReq_ = function(method, path, query, body, callback) {
this.makeReq(method, this.pathPrefix + path, query, body, callback);
};
module.exports = Acl;
/**
* Attach functionality to a {module:storage/acl} instance. This will add an
* object for each role group (owners, readers, and writers), with each object
* containing methods to add or delete a type of entity.
*
* As an example, here are a few methods that are created.
*
* myBucket.acl.readers.deleteGroup('groupId', function(err) {});
*
* myBucket.acl.owners.addUser('email@example.com', function(err, acl) {});
*
* myBucket.acl.writers.addDomain('example.com', function(err, acl) {});
*
* @private
*/
function AclRoleAccessorMethods() {
AclRoleAccessorMethods.roles.forEach(this._assignAccessMethods.bind(this));
}
AclRoleAccessorMethods.accessMethods = [
'add',
'delete'
];
AclRoleAccessorMethods.entities = [
// Special entity groups that do not require further specification.
'allAuthenticatedUsers',
'allUsers',
// Entity groups that require specification, e.g. `user-email@example.com`.
'domain-',
'group-',
'project-',
'user-'
];
AclRoleAccessorMethods.roles = [
'OWNER',
'READER',
'WRITER'
];
AclRoleAccessorMethods.prototype._assignAccessMethods = function(role) {
var that = this;
var accessMethods = AclRoleAccessorMethods.accessMethods;
var entities = AclRoleAccessorMethods.entities;
var roleGroup = role.toLowerCase() + 's';
this[roleGroup] = entities.reduce(function(acc, entity) {
var isPrefix = entity.charAt(entity.length - 1) === '-';
accessMethods.forEach(function(accessMethod) {
var method = accessMethod + entity[0].toUpperCase() + entity.substr(1);
if (isPrefix) {
method = method.replace('-', '');
}
// Wrap the parent accessor method (e.g. `add` or `delete`) to avoid the
// more complex API of specifying an `entity` and `role`.
acc[method] = function(entityId, callback) {
var apiEntity;
if (isPrefix) {
apiEntity = entity + entityId;
} else {
// If the entity is not a prefix, it is a special entity group that
// does not require further details. The accessor methods only accept
// a callback.
apiEntity = entity;
callback = entityId;
}
that[accessMethod]({
entity: apiEntity,
role: role
}, callback);
};
});
return acc;
}, {});
};
module.exports.AclRoleAccessorMethods = AclRoleAccessorMethods;