forked from googleapis/nodejs-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam.js
More file actions
337 lines (317 loc) · 10.1 KB
/
Copy pathiam.js
File metadata and controls
337 lines (317 loc) · 10.1 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
/*!
* 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 pubsub/iam
*/
'use strict';
var arrify = require('arrify');
var common = require('@google-cloud/common');
var is = require('is');
/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
* allows you to set permissions on invidual resources and offers a wider range
* of roles: editor, owner, publisher, subscriber, and viewer. This gives you
* greater flexibility and allows you to set more fine-grained access control.
*
* For example:
* * Grant access on a per-topic or per-subscription basis, rather than for
* the whole Cloud project.
* * Grant access with limited capabilities, such as to only publish messages
* to a topic, or to only to consume messages from a subscription, but not
* to delete the topic or subscription.
*
*
* *The IAM access control features described in this document are Beta,
* including the API methods to get and set IAM policies, and to test IAM
* permissions. Cloud Pub/Sub's use of IAM features is not covered by any
* SLA or deprecation policy, and may be subject to backward-incompatible
* changes.*
*
* @constructor Iam
* @mixin
* @param {PubSub} pubsub PubSub Object.
* @param {string} id The name of the topic or subscription.
*
* @see [Access Control Overview]{@link https://cloud.google.com/pubsub/access_control}
* @see [What is Cloud IAM?]{@link https://cloud.google.com/iam/}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* // topic.iam
*
* const subscription = pubsub.subscription('my-subscription');
* // subscription.iam
*/
function IAM(pubsub, id) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
}
this.pubsub = pubsub;
this.request = pubsub.request.bind(pubsub);
this.id = id;
}
/**
* @typedef {array} GetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
/**
* @callback GetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/
/**
* Get the IAM policy
*
* @param {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {GetPolicyCallback} [callback] Callback function.
* @returns {Promise<GetPolicyResponse>}
*
* @see [Topics: getIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/getIamPolicy}
* @see [Subscriptions: getIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/getIamPolicy}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const subscription = topic.subscription('my-subscription');
*
* topic.iam.getPolicy(function(err, policy, apiResponse) {});
*
* subscription.iam.getPolicy(function(err, policy, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.iam.getPolicy().then(function(data) {
* var policy = data[0];
* var apiResponse = data[1];
* });
*/
IAM.prototype.getPolicy = function(gaxOpts, callback) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}
var reqOpts = {
resource: this.id,
};
this.request(
{
client: 'SubscriberClient',
method: 'getIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
callback
);
};
/**
* @typedef {array} SetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
/**
* @callback SetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/
/**
* Set the IAM policy
*
* @throws {Error} If no policy is provided.
*
* @param {object} policy The [policy](https://cloud.google.com/pubsub/docs/reference/rest/Shared.Types/Policy).
* @param {array} [policy.bindings] Bindings associate members with roles.
* @param {Array<object>} [policy.rules] Rules to be applied to the policy.
* @param {string} [policy.etag] Etags are used to perform a read-modify-write.
* @param {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {SetPolicyCallback} callback Callback function.
* @returns {Promise<SetPolicyResponse>}
*
* @see [Topics: setIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/setIamPolicy}
* @see [Subscriptions: setIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/setIamPolicy}
* @see [Policy]{@link https://cloud.google.com/pubsub/docs/reference/rest/Shared.Types/Policy}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const subscription = topic.subscription('my-subscription');
*
* var myPolicy = {
* bindings: [
* {
* role: 'roles/pubsub.subscriber',
* members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com']
* }
* ]
* };
*
* topic.iam.setPolicy(myPolicy, function(err, policy, apiResponse) {});
*
* subscription.iam.setPolicy(myPolicy, function(err, policy, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.iam.setPolicy(myPolicy).then(function(data) {
* var policy = data[0];
* var apiResponse = data[1];
* });
*/
IAM.prototype.setPolicy = function(policy, gaxOpts, callback) {
if (!is.object(policy)) {
throw new Error('A policy object is required.');
}
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}
var reqOpts = {
resource: this.id,
policy,
};
this.request(
{
client: 'SubscriberClient',
method: 'setIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
callback
);
};
/**
* @typedef {array} TestIamPermissionsResponse
* @property {object[]} 0 A subset of permissions that the caller is allowed.
* @property {object} 1 The full API response.
*/
/**
* @callback TestIamPermissionsCallback
* @param {?Error} err Request error, if any.
* @param {object[]} permissions A subset of permissions that the caller is allowed.
* @param {object} apiResponse The full API response.
*/
/**
* Test a set of permissions for a resource.
*
* Permissions with wildcards such as `*` or `storage.*` are not allowed.
*
* @throws {Error} If permissions are not provided.
*
* @param {string|string[]} permissions The permission(s) to test for.
* @param {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {TestIamPermissionsCallback} [callback] Callback function.
* @returns {Promise<TestIamPermissionsResponse>}
*
* @see [Topics: testIamPermissions API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/testIamPermissions}
* @see [Subscriptions: testIamPermissions API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/testIamPermissions}
* @see [Permissions Reference]{@link https://cloud.google.com/pubsub/access_control#permissions}
*
* @example
* const PubSub = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const subscription = topic.subscription('my-subscription');
*
* //-
* // Test a single permission.
* //-
* const test = 'pubsub.topics.update';
*
* topic.iam.testPermissions(test, function(err, permissions, apiResponse) {
* console.log(permissions);
* // {
* // "pubsub.topics.update": true
* // }
* });
*
* //-
* // Test several permissions at once.
* //-
* const tests = [
* 'pubsub.subscriptions.consume',
* 'pubsub.subscriptions.update'
* ];
*
* subscription.iam.testPermissions(tests, function(err, permissions) {
* console.log(permissions);
* // {
* // "pubsub.subscriptions.consume": true,
* // "pubsub.subscriptions.update": false
* // }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* topic.iam.testPermissions(test).then(function(data) {
* const permissions = data[0];
* const apiResponse = data[1];
* });
*/
IAM.prototype.testPermissions = function(permissions, gaxOpts, callback) {
if (!is.array(permissions) && !is.string(permissions)) {
throw new Error('Permissions are required.');
}
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}
var reqOpts = {
resource: this.id,
permissions: arrify(permissions),
};
this.request(
{
client: 'SubscriberClient',
method: 'testIamPermissions',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}
var availablePermissions = arrify(resp.permissions);
var permissionHash = permissions.reduce(function(acc, permission) {
acc[permission] = availablePermissions.indexOf(permission) > -1;
return acc;
}, {});
callback(null, permissionHash, resp);
}
);
};
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(IAM);
module.exports = IAM;