-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathclient.ts
597 lines (516 loc) · 16.4 KB
/
client.ts
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Copyright 2021 Google LLC
*
* 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.
*/
import { randomBytes } from 'crypto';
import fs from 'fs';
import * as path from 'path';
import { tmpdir } from 'os';
import { HttpClient } from '@actions/http-client';
import { GoogleAuth } from 'google-auth-library';
import { errorMessage, forceRemove } from '@google-github-actions/actions-utils';
import { zipDir, ZipOptions } from './util';
// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: appVersion } = require('../package.json');
// userAgent is the default user agent.
const userAgent = `google-github-actions:deploy-cloud-functions/${appVersion}`;
// defaultBaseURL is the URL for Cloud Functions.
const defaultBaseURL = 'https://cloudfunctions.googleapis.com/v1';
// defaultTimeout is the default timeout in seconds.
const defaultTimeout = 300;
// cloudFunctionResourceNamePattern is the regular expression to use to match
// resource names.
const cloudFunctionResourceNamePattern = new RegExp(
/^projects\/.+\/locations\/.+\/functions\/.+$/gi,
);
export type CloudFunctionClientOptions = {
projectID?: string;
location?: string;
baseURL?: string;
};
export type PollOperationOptions = {
retries: number;
interval: number;
onPoll?: OnFunction;
};
export type Operation = {
name: string;
metadata: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
done: boolean;
error: OperationStatus;
response?: CloudFunctionResponse;
};
export type OperationStatus = {
code: number;
message: string;
};
export type KVPair = Record<string, string>;
export type CloudFunction = {
name: string;
runtime: string;
description?: string;
availableMemoryMb?: number;
buildEnvironmentVariables?: KVPair;
buildWorkerPool?: string;
dockerRegistry?: string;
dockerRepository?: string;
entryPoint?: string;
environmentVariables?: KVPair;
ingressSettings?: string;
kmsKeyName?: string;
labels?: KVPair;
maxInstances?: number;
minInstances?: number;
network?: string;
secretEnvironmentVariables?: SecretEnvVar[];
secretVolumes?: SecretVolume[];
serviceAccountEmail?: string;
sourceToken?: string;
timeout?: string;
vpcConnector?: string;
vpcConnectorEgressSettings?: string;
// oneof
sourceArchiveUrl?: string;
sourceRepository?: SourceRepository;
sourceUploadUrl?: string;
// oneof
httpsTrigger?: HTTPSTrigger;
eventTrigger?: EventTrigger;
};
export type SecretEnvVar = {
key: string;
projectId: string;
secret: string;
version: string;
};
export type SecretVolume = {
mountPath: string;
projectId: string;
secret: string;
versions: {
path: string;
version: string;
}[];
};
export type CloudFunctionResponse = CloudFunction & {
status: string;
updateTime: string;
versionId: string;
buildName?: string;
buildId?: string;
};
export type SourceRepository = {
url: string;
deployedUrl: string;
};
export type HTTPSTrigger = {
url?: string;
securityLevel?: string;
};
export type EventTrigger = {
eventType: string;
resource: string;
service?: string;
failurePolicy?: FailurePolicy;
};
export type FailurePolicy = {
retry: Record<string, string>;
};
export type CreateOptions = {
timeout?: number;
onPoll?: OnFunction;
};
export type DeleteOptions = {
timeout?: number;
onPoll?: OnFunction;
};
export type PatchOptions = {
timeout?: number;
onPoll?: OnFunction;
};
export type DeployOptions = {
timeout?: number;
onPoll?: OnFunction;
onZip?: OnZipFunction;
onNew?: OnFunction;
onExisting?: OnFunction;
} & ZipOptions;
export type OnFunction = () => void;
export type OnZipFunction = (sourceDir: string, zipPath: string) => void;
export class CloudFunctionsClient {
/**
* baseURL is the cloud functions endpoint. By default, this is the public
* cloud functions endpoint, but it can be overridden for testing.
*/
readonly #baseURL: string;
/**
* auth is the authentication client.
*/
readonly #auth: GoogleAuth;
/**
* projectID and location are hints to the client if a Cloud Function resource
* name does not include the full resource name. If a full resource name is
* given (e.g. `projects/p/locations/l/functions/f`), then that is used.
* However, if just a name is given (e.g. `f`), these values will be used to
* construct the full resource name.
*/
readonly #projectID?: string;
readonly #location?: string;
/**
* client is the HTTP client.
*/
readonly #client: HttpClient;
constructor(opts?: CloudFunctionClientOptions) {
this.#baseURL = opts?.baseURL || defaultBaseURL;
this.#auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
projectId: opts?.projectID,
});
this.#projectID = opts?.projectID;
this.#location = opts?.location;
this.#client = new HttpClient(userAgent);
}
async #request(
method: string,
url: string,
data?: any, // eslint-disable-line @typescript-eslint/no-explicit-any
) {
const authToken = await this.#auth.getAccessToken();
if (!authToken) {
throw new Error(`Failed to get auth token for ${method} ${url}`);
}
const headers = {
'Authorization': `Bearer ${authToken}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
};
try {
const response = await this.#client.request(method, url, data, headers);
const body = await response.readBody();
const statusCode = response.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`(${statusCode}) ${body}`);
}
return JSON.parse(body);
} catch (err) {
const msg = errorMessage(err);
throw new Error(`Failed to ${method} ${url}: ${msg}`);
}
}
/**
* pollOperation polls the operation, calling pollFn on each attempt.
*
* @param name Name of the operation, of the format `operations/{name}`.
* @param opts Options for polling
*/
async #pollOperation(
name: string,
opts: PollOperationOptions = { interval: 5, retries: 60 },
): Promise<Operation> {
const intervalMs: number = +opts.interval * 1000;
for (let i = 0; i < opts.retries; i++) {
// If a poll function was given, call it.
if (opts.onPoll) opts.onPoll();
const resp = await this.getOperation(name);
if (resp.error) {
throw new Error(`Operation failed: ${resp.error.message}`);
}
if (resp.done) {
return resp;
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
throw new Error(`Operation timed out`);
}
/**
* getOperation fetches the operation by name.
*
* @param name Name of the operation, of the format `operations/{name}`.
*/
async getOperation(name: string): Promise<Operation> {
if (!name.startsWith('operations/')) {
name = `operations/${name}`;
}
const u = `${this.#baseURL}/${name}`;
const resp: Operation = await this.#request('GET', u);
return resp;
}
/**
* create creates a new Cloud Function.
*
* @param cf Cloud Function to deploy.
*/
async create(cf: CloudFunction, opts?: CreateOptions): Promise<CloudFunctionResponse> {
const timeout = opts?.timeout || defaultTimeout;
const resourceName = this.fullResourceName(cf.name);
cf.name = resourceName;
const parent = this.parentFromName(resourceName);
const u = `${this.#baseURL}/${parent}/functions`;
const body = JSON.stringify(cf);
const resp: Operation = await this.#request('POST', u, body);
const op = await this.#pollOperation(resp.name, {
interval: 5,
retries: timeout / 5,
onPoll: opts?.onPoll,
});
if (!op.response) {
throw new Error(`create operation result did not include function`);
}
return op.response;
}
/**
* delete removes a function with the given name.
*
* @param name Full resource name of the Cloud Function.
*/
async delete(name: string, opts?: DeleteOptions): Promise<Operation> {
const timeout = opts?.timeout || defaultTimeout;
const resourceName = this.fullResourceName(name);
const u = `${this.#baseURL}/${resourceName}`;
const resp: Operation = await this.#request('DELETE', u);
return await this.#pollOperation(resp.name, {
interval: 5,
retries: timeout / 5,
onPoll: opts?.onPoll,
});
}
/**
* generateUploadURL generates a signed URL for which to upload the blob.
*
* @param parent Name of the location in which to deploy the function, of the
* format `projects/p/locations/l`.
*/
async generateUploadURL(parent: string): Promise<string> {
const u = `${this.#baseURL}/${parent}/functions:generateUploadUrl`;
const resp = await this.#request('POST', u);
return resp.uploadUrl;
}
/**
* get returns a function with the given name.
*
* @param name Name of the function to get, of the format
* `projects/p/locations/l/functions/f`.
*/
async get(name: string): Promise<CloudFunctionResponse> {
const resourceName = this.fullResourceName(name);
const u = `${this.#baseURL}/${resourceName}`;
const resp: CloudFunctionResponse = await this.#request('GET', u);
return resp;
}
/**
* getSafe attempts to get the existing function by resource name.
* If the function exists, it returns the function. If the function does not
* exist, it returns null. If there are any errors besides a 404 returned, it
* throws that error.
*/
async getSafe(name: string): Promise<CloudFunction | null> {
try {
return await this.get(name);
} catch (err) {
const msg = errorMessage(err);
if (!msg.includes('404') && !msg.includes('NOT_FOUND')) {
throw new Error(
`Failed to lookup existing function - does the caller have ` +
`cloudfunctions.functions.get permissions? ${err}`,
);
}
return null;
}
}
/**
* patch updates fields on the function.
*
* @param cf Cloud Function to patch
*/
async patch(cf: CloudFunction, opts?: PatchOptions): Promise<CloudFunctionResponse> {
const timeout = opts?.timeout || defaultTimeout;
// fieldMasks are used if we are overwriting only specific fields of the
// resource in the case we assume we will always need to replace.
//
// https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#CloudFunction
const updateMasks = [
'availableMemoryMb',
'buildEnvironmentVariables',
'buildWorkerPool',
'description',
'dockerRegistry',
'dockerRepository',
'entryPoint',
'environmentVariables',
'eventTrigger',
'httpsTrigger',
'ingressSettings',
'kmsKeyName',
'labels',
'maxInstances',
'minInstances',
'name',
'network',
'runtime',
'secretEnvironmentVariables',
'secretVolumes',
'serviceAccountEmail',
'timeout',
'vpcConnector',
'vpcConnectorEgressSettings',
];
// These values cannot be set on the updateMask unless they are also given
// in the request.
if (cf.sourceArchiveUrl?.length) {
updateMasks.push('sourceArchiveUrl');
}
if (cf.sourceRepository?.url?.length) {
updateMasks.push('sourceRepository');
}
if (cf.sourceToken?.length) {
updateMasks.push('sourceToken');
}
if (cf.sourceUploadUrl?.length) {
updateMasks.push('sourceUploadUrl');
}
const resourceName = this.fullResourceName(cf.name);
cf.name = resourceName;
const u = `${this.#baseURL}/${resourceName}?updateMask=${updateMasks.join(',')}`;
const body = JSON.stringify(cf);
const resp: Operation = await this.#request('PATCH', u, body);
const op = await this.#pollOperation(resp.name, {
interval: 5,
retries: timeout / 5,
onPoll: opts?.onPoll,
});
if (!op.response) {
throw new Error(`patch operation result did not include function`);
}
return op.response;
}
/**
* deployFromLocalSource deploys a function. If the function already exists, it deploys a new
* version. If the function does not already exist, it creates a new one. This is not an API method, but rather a helper around a collection of API methods.
*
* @param cf Cloud Function.
* @param sourceDir Path on local disk to the source to deploy.
*/
async deployFromLocalSource(
cf: CloudFunction,
sourceDir: string,
opts?: DeployOptions,
): Promise<CloudFunctionResponse> {
const timeout = opts?.timeout || defaultTimeout;
const randomName = randomBytes(12).toString('hex');
const zipPath = path.join(tmpdir(), `cfsrc-${randomName}.zip`);
try {
await zipDir(sourceDir, zipPath, opts);
if (opts?.onZip) opts.onZip(sourceDir, zipPath);
} catch (err) {
throw new Error(`Zip file ${zipPath} creation failed: ${err}`);
}
const resourceName = this.fullResourceName(cf.name);
cf.name = resourceName;
// Extract the parent from the name attribute.
const parent = this.parentFromName(resourceName);
// Upload source code to the upload URL.
let uploadURL;
try {
uploadURL = await this.generateUploadURL(parent);
await this.uploadSource(uploadURL, zipPath);
} catch (err) {
throw new Error(`Failed to upload zip file: ${err}`);
}
// Delete temp zip file after upload
forceRemove(zipPath);
cf.sourceUploadUrl = uploadURL;
// Get the existing function data.
const existingFunction = await this.getSafe(resourceName);
// If the function already exists, create a new version
if (existingFunction) {
if (opts?.onExisting) opts.onExisting();
const resp: CloudFunctionResponse = await this.patch(cf, {
timeout: timeout,
onPoll: opts?.onPoll,
});
return resp;
} else {
if (opts?.onNew) opts.onNew();
const resp: CloudFunctionResponse = await this.create(cf, {
timeout: timeout,
onPoll: opts?.onPoll,
});
return resp;
}
}
/**
* Upload a file to a Signed URL.
*
* @param uploadURL Signed URL.
* @param zipPath File to upload.
* @returns uploaded URL.
*/
async uploadSource(uploadURL: string, zipPath: string): Promise<void> {
const zipFile = fs.createReadStream(zipPath);
try {
// This is different logic than the primary request function, and it does
// not return JSON.
const response = await this.#client.request('PUT', uploadURL, zipFile, {
'content-type': 'application/zip',
'x-goog-content-length-range': '0,104857600',
});
const body = await response.readBody();
const statusCode = response.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`(${statusCode}) ${body}`);
}
} catch (err) {
const msg = errorMessage(err);
throw new Error(`Failed to upload source: ${msg}`);
}
}
fullResourceName(name: string): string {
if (!name) {
name = '';
}
name = name.trim();
if (!name) {
throw new Error(`Failed to parse resource name: name cannot be empty`);
}
if (name.includes('/')) {
if (name.match(cloudFunctionResourceNamePattern)) {
return name;
} else {
throw new Error(`Invalid resource name '${name}'`);
}
}
const projectID = this.#projectID;
if (!projectID) {
throw new Error(`Failed to get project ID to build resource name. Try setting 'project_id'.`);
}
const location = this.#location;
if (!location) {
throw new Error(
`Failed to get location (region) to build resource name. Try setting 'region'.`,
);
}
return `projects/${projectID}/locations/${location}/functions/${name}`;
}
parentFromName(name: string): string {
const parts = name.split('/');
if (parts.length < 3) {
throw new Error(
`Invalid or missing name '${name}' (expected 'projects/p/locations/l/functions/f')`,
);
}
const parent = parts.slice(0, parts.length - 2).join('/');
return parent;
}
}