Repository navigation
Expand file tree
/
Copy pathacl.js
More file actions
220 lines (199 loc) · 8.21 KB
/
Copy pathacl.js
File metadata and controls
220 lines (199 loc) · 8.21 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
// Copyright 2015-2016, Google, Inc.
// 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.
'use strict';
// [START all]
// [START setup]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
var Storage = require('@google-cloud/storage');
// Instantiate a storage client
var storage = Storage();
// [END setup]
// [START add_access_control]
/**
* Add access controls to a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket for the new access controls.
* @param {string} options.entity The entity for the new access controls.
* @param {string} options.role The role for the new access controls.
* @param {string} [options.file] Optional. The file for the new access controls.
* @param {boolean} [options.default] Optional. Whether to set default access controls.
* @param {function} callback The callback function.
*/
function addAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
var acl = bucket.acl;
if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally add "default" access controls to the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
acl = acl.default;
}
// Specify the entity and role for the new access control object
var config = {
entity: options.entity,
role: options.role
};
acl.add(config, function (err, aclObject) {
if (err) {
return callback(err);
}
console.log('Added access controls to: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null, aclObject);
});
}
// [END add_access_control]
// [START get_access_control]
/**
* Get access controls for a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket to target.
* @param {string} [options.entity] Optional. The entity to filter by.
* @param {string} [options.file] Optional. The file to target.
* @param {boolean} [options.default] Optional. Whether to get default access controls.
* @param {function} callback The callback function.
*/
function getAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
var acl = bucket.acl;
if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally get "default" access controls for the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
acl = acl.default;
}
if (options.entity) {
// Get a list of access controls for a specific entity
acl.get({ entity: options.entity }, handleResponse);
} else {
// Get a list of all access controls
acl.get(handleResponse);
}
function handleResponse (err, aclObject) {
if (err) {
return callback(err);
}
console.log('Got access controls for: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null, aclObject);
}
}
// [END get_access_control]
// [START delete_access_control]
/**
* Delete access controls from a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket to target.
* @param {string} options.entity The entity whose access is to be revoked.
* @param {string} [options.file] Optional. The file to target.
* @param {boolean} [options.default] Optional. Whether to delete default access controls.
* @param {function} callback The callback function.
*/
function deleteAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
var acl = bucket.acl;
if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/file
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally delete "default" access controls from the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/storage/latest/storage/bucket
acl = acl.default;
}
// Delete access controls for a specific entity
acl.delete({ entity: options.entity }, function (err) {
if (err) {
return callback(err);
}
console.log('Deleted access controls from: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null);
});
}
// [END delete_access_control]
// [END all]
// The command-line program
var cli = require('yargs');
var utils = require('../utils');
var program = module.exports = {
addAccessControl: addAccessControl,
getAccessControl: getAccessControl,
deleteAccessControl: deleteAccessControl,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};
cli
.demand(1)
.command('add <entity> <role>', 'Add access controls on a bucket or file.', {}, function (options) {
program.addAccessControl(utils.pick(options, ['entity', 'role', 'bucket', 'default', 'file']), utils.makeHandler());
})
.command('get [entity]', 'Get access controls on a bucket or file.', {}, function (options) {
program.getAccessControl(utils.pick(options, ['entity', 'bucket', 'default', 'file']), utils.makeHandler());
})
.command('delete <entity>', 'Delete access controls from a bucket or file.', {}, function (options) {
program.deleteAccessControl(utils.pick(options, ['entity', 'bucket', 'default', 'file']), utils.makeHandler());
})
.example('node $0 add user-bob@domain.com OWNER -b mybucket', 'Add OWNER access controls for "user-bob@domain.com" to "mybucket".')
.example('node $0 add viewers-2256 WRITER -b mybucket -d', 'Add default WRITER access controls to "mybucket" for "viewers-2256".')
.example('node $0 get editors-1234 -b mybucket', 'Get access controls for "editors-1234" in "mybucket".')
.example('node $0 delete -b mybucket -f file.txt', 'Delete all access controls for all entities from "file.txt" in "mybucket".')
.options({
bucket: {
alias: 'b',
global: true,
demand: true,
requiresArg: true,
type: 'string',
description: 'The target storage bucket.'
},
default: {
alias: 'd',
global: true,
type: 'boolean',
description: 'Whether to set default access controls. Only valid when setting access controls on a bucket.'
},
file: {
alias: 'f',
global: true,
requiresArg: true,
type: 'string',
description: 'The target file.'
}
})
.wrap(100)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists');
if (module === require.main) {
program.main(process.argv.slice(2));
}