-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
377 lines (316 loc) · 10 KB
/
index.js
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
'use strict';
var async = require('async');
var crypto = require('crypto');
var fs = require('fs');
var GoogleAuth = require('google-auth-library').GoogleAuth;
var gcpMetadata = require('gcp-metadata');
var path = require('path');
var request = require('request');
class Auth {
constructor(config) {
this.authClientPromise = null;
this.authClient = null;
this.googleAuthClient = null;
this.config = config || {};
this.credentials = null;
this.environment = {};
this.jwtClient = null;
this.projectId = this.config.projectId;
this.token = this.config.token;
}
authorizeRequest (reqOpts, callback) {
this.getToken((err, token) => {
if (err) {
callback(err);
return;
}
var authorizedReqOpts = Object.assign({}, reqOpts, {
headers: Object.assign({}, reqOpts.headers, {
Authorization: `Bearer ${token}`
})
});
callback(null, authorizedReqOpts);
});
}
getAuthClient (callback) {
if (this.authClient) {
// This code works around an issue with context loss with async-listener.
// Strictly speaking, this should not be necessary as the call to
// authClientPromise.then(..) below would resolve to the same value.
// However, async-listener defaults to resuming the `then` callbacks with
// the context at the point of resolution rather than the context from the
// point where the `then` callback was added. In this case, the promise
// will be resolved on the very first incoming http request, and that
// context will become sticky (will be restored by async-listener) around
// the `then` callbacks for all subsequent requests.
//
// This breaks APM tools like Stackdriver Trace & others and tools like
// long stack traces (they will provide an incorrect stack trace).
//
// NOTE: this doesn't solve the problem generally. Any request concurrent
// to the first call to this function, before the promise resolves, will
// still lose context. We don't have a better solution at the moment :(.
return setImmediate(callback.bind(null, null, this.authClient));
}
var createAuthClientPromise = (resolve, reject) => {
var config = this.config;
var keyFile = config.keyFilename || config.keyFile;
this.googleAuthClient = new GoogleAuth();
var addScope = (err, authClient, projectId) => {
if (err) {
reject(err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
if (!config.scopes || config.scopes.length === 0) {
var scopeError = new Error('Scopes are required for this request.');
scopeError.code = 'MISSING_SCOPE';
reject(scopeError);
return;
}
}
authClient.scopes = config.scopes;
this.authClient = authClient;
this.projectId = config.projectId || projectId || authClient.projectId;
if (!this.projectId) {
this.googleAuthClient.getDefaultProjectId((err, projectId) => {
// Ignore error, since the user might not require a project ID.
if (projectId) {
this.projectId = projectId;
}
resolve(authClient);
});
return;
}
resolve(authClient);
};
if (config.credentials) {
try {
var client = this.googleAuthClient.fromJSON(config.credentials);
addScope(null, client);
} catch (e) {
addScope(e);
}
} else if (keyFile) {
keyFile = path.resolve(process.cwd(), keyFile);
fs.readFile(keyFile, (err, contents) => {
if (err) {
reject(err);
return;
}
try {
var client = this.googleAuthClient.fromJSON(JSON.parse(contents));
addScope(null, client);
} catch(e) {
// @TODO Find a better way to do this.
// Ref: https://github.com/googleapis/nodejs-storage/issues/147
// Ref: https://github.com/google/google-auth-library-nodejs/issues/313
var client = this.googleAuthClient.fromJSON({
type: 'jwt-pem-p12',
client_email: config.email,
private_key: keyFile
});
delete client.key;
client.keyFile = keyFile;
this.jwtClient = client;
addScope(null, client);
}
});
} else {
this.googleAuthClient.getApplicationDefault(addScope);
}
};
if (!this.authClientPromise) {
this.authClientPromise = new Promise(createAuthClientPromise);
}
this.authClientPromise.then((authClient) => {
callback(null, authClient);
// The return null is needed to avoid a spurious warning if the user is
// using bluebird.
// See: https://github.com/stephenplusplus/google-auto-auth/issues/28
return null;
}).catch(callback);
}
getCredentials (callback) {
if (this.credentials) {
setImmediate(() => {
callback(null, this.credentials);
});
return;
}
this.getAuthClient((err) => {
if (err) {
callback(err);
return;
}
this.googleAuthClient.getCredentials((err, credentials) => {
if (err) {
callback(err);
return;
}
this.credentials = credentials;
if (this.jwtClient) {
this.jwtClient.authorize(err => {
if (err) {
callback(err);
return;
}
this.credentials.private_key = this.jwtClient.key;
callback(null, this.credentials);
});
return;
}
callback(null, this.credentials);
});
});
}
getEnvironment (callback) {
async.parallel([
cb => this.isAppEngine(cb),
cb => this.isCloudFunction(cb),
cb => this.isComputeEngine(cb),
cb => this.isContainerEngine(cb)
], () => {
callback(null, this.environment);
});
}
getProjectId (callback) {
if (this.projectId) {
setImmediate(() => {
callback(null, this.projectId);
});
return;
}
this.getAuthClient(err => {
if (err) {
callback(err);
return;
}
callback(null, this.projectId);
});
}
getToken (callback) {
if (this.token) {
setImmediate(callback, null, this.token);
return;
}
this.getAuthClient((err, client) => {
if (err) {
callback(err);
return;
}
client.getAccessToken(callback);
});
}
isAppEngine (callback) {
setImmediate(() => {
var env = this.environment;
if (typeof env.IS_APP_ENGINE === 'undefined') {
env.IS_APP_ENGINE = !!(process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME);
}
callback(null, env.IS_APP_ENGINE);
});
}
isCloudFunction (callback) {
setImmediate(() => {
var env = this.environment;
if (typeof env.IS_CLOUD_FUNCTION === 'undefined') {
env.IS_CLOUD_FUNCTION = !!process.env.FUNCTION_NAME;
}
callback(null, env.IS_CLOUD_FUNCTION);
});
}
isComputeEngine (callback) {
var env = this.environment;
if (typeof env.IS_COMPUTE_ENGINE !== 'undefined') {
setImmediate(() => {
callback(null, env.IS_COMPUTE_ENGINE);
});
return;
}
request('http://metadata.google.internal', (err, res) => {
env.IS_COMPUTE_ENGINE = !err && res.headers['metadata-flavor'] === 'Google';
callback(null, env.IS_COMPUTE_ENGINE);
});
}
isContainerEngine (callback) {
var env = this.environment;
if (typeof env.IS_CONTAINER_ENGINE !== 'undefined') {
setImmediate(() => {
callback(null, env.IS_CONTAINER_ENGINE);
});
return;
}
gcpMetadata.instance('/attributes/cluster-name')
.then(() => {
env.IS_CONTAINER_ENGINE = true;
callback(null, env.IS_CONTAINER_ENGINE);
})
.catch(() => {
env.IS_CONTAINER_ENGINE = false
callback(null, env.IS_CONTAINER_ENGINE);
});
}
sign (data, callback) {
this.getCredentials((err, credentials) => {
if (err) {
callback(err);
return;
}
if (credentials.private_key) {
this._signWithPrivateKey(data, callback);
} else {
this._signWithApi(data, callback);
}
});
}
// `this.getCredentials()` will always have been run by this time
_signWithApi (data, callback) {
if (!this.projectId) {
callback(new Error('Cannot sign data without a project ID.'));
return;
}
var client_email = this.credentials.client_email;
if (!client_email) {
callback(new Error('Cannot sign data without `client_email`.'));
return;
}
var idString = `projects/${this.projectId}/serviceAccounts/${client_email}`;
var reqOpts = {
method: 'POST',
uri: `https://iam.googleapis.com/v1/${idString}:signBlob`,
json: {
bytesToSign: Buffer.from(data).toString('base64')
}
};
this.authorizeRequest(reqOpts, (err, authorizedReqOpts) => {
if (err) {
callback(err);
return;
}
request(authorizedReqOpts, function (err, resp, body) {
var response = resp.toJSON();
if (!err && response.statusCode < 200 || response.statusCode >= 400) {
if (typeof response.body === 'object') {
var apiError = response.body.error;
err = new Error(apiError.message);
Object.assign(err, apiError);
} else {
err = new Error(response.body);
err.code = response.statusCode;
}
}
callback(err, body && body.signature);
});
});
}
// `this.getCredentials()` will always have been run by this time
_signWithPrivateKey (data, callback) {
var sign = crypto.createSign('RSA-SHA256');
sign.update(data);
callback(null, sign.sign(this.credentials.private_key, 'base64'));
}
}
module.exports = config => {
return new Auth(config);
};