-
Notifications
You must be signed in to change notification settings - Fork 267
/
api.ts
1064 lines (913 loc) · 33.4 KB
/
api.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2024 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import type { IncomingHttpHeaders } from "http"
import type { GotHeaders, GotJsonOptions, GotResponse } from "../util/http.js"
import { got, GotHttpError } from "../util/http.js"
import { CloudApiError, GardenError, InternalError } from "../exceptions.js"
import type { Log } from "../logger/log-entry.js"
import { DEFAULT_GARDEN_CLOUD_DOMAIN, gardenEnv } from "../constants.js"
import { Cookie } from "tough-cookie"
import { cloneDeep, isObject, omit } from "lodash-es"
import { dedent, deline } from "../util/string.js"
import type {
BaseResponse,
CreateEphemeralClusterResponse,
CreateProjectsForRepoResponse,
CreateSecretRequest,
CreateSecretResponse,
EphemeralClusterWithRegistry,
GetKubeconfigResponse,
GetProfileResponse,
GetProjectResponse,
ListProjectsResponse,
ListSecretsResponse,
SecretResult as CloudApiSecretResult,
SecretResult,
UpdateSecretRequest,
UpdateSecretResponse,
} from "@garden-io/platform-api-types"
import { getCloudDistributionName, getCloudLogSectionName } from "../util/cloud.js"
import { getPackageVersion } from "../util/util.js"
import type { CommandInfo } from "../plugin-context.js"
import type { ClientAuthToken, GlobalConfigStore } from "../config-store/global.js"
import { add } from "date-fns"
import { LogLevel } from "../logger/logger.js"
import { makeAuthHeader } from "./auth.js"
import type { StringMap } from "../config/common.js"
import { styles } from "../logger/styles.js"
import { HTTPError, RequestError } from "got"
import type { Garden } from "../garden.js"
import type { ApiCommandError } from "../commands/cloud/helpers.js"
import { enumerate } from "../util/enumerate.js"
import queryString from "query-string"
const gardenClientName = "garden-core"
const gardenClientVersion = getPackageVersion()
export class CloudApiDuplicateProjectsError extends CloudApiError {}
export class CloudApiTokenRefreshError extends CloudApiError {}
function extractErrorMessageBodyFromGotError(error: any): error is GotHttpError {
return error?.response?.body?.message
}
function stripLeadingSlash(str: string) {
return str.replace(/^\/+/, "")
}
// This is to prevent Unhandled Promise Rejections in got
// See: https://github.com/sindresorhus/got/issues/1489#issuecomment-805485731
function isGotResponseOk(response: GotResponse) {
const { statusCode } = response
const limitStatusCode = response.request.options.followRedirect ? 299 : 399
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304
}
const refreshThreshold = 10 // Threshold (in seconds) subtracted to jwt validity when checking if a refresh is needed
const secretsPageLimit = 100
export interface ApiFetchParams {
headers: GotHeaders
method: "GET" | "POST" | "PUT" | "PATCH" | "HEAD" | "DELETE"
retry: boolean
retryDescription?: string
maxRetries?: number
body?: any
}
interface BulkOperationResult {
results: SecretResult[]
errors: ApiCommandError[]
}
export interface Secret {
name: string
value: string
}
export interface BulkCreateSecretRequest extends Omit<CreateSecretRequest, "name" | "value"> {
secrets: Secret[]
}
export interface SingleUpdateSecretRequest extends UpdateSecretRequest {
id: string
}
export interface BulkUpdateSecretRequest {
secrets: SingleUpdateSecretRequest[]
}
export interface ApiFetchOptions {
headers?: GotHeaders
/**
* True by default except for api.post (where retry = true must explicitly be passed, since retries aren't always
* safe / desirable for such requests).
*/
retry?: boolean
maxRetries?: number
/**
* An optional prefix to use for retry error messages.
*/
retryDescription?: string
}
export interface AuthTokenResponse {
token: string
refreshToken: string
tokenValidity: number
}
export type ApiFetchResponse<T> = T & {
headers: IncomingHttpHeaders
}
// TODO: Read this from the `api-types` package once the session registration logic has been released in Cloud.
export interface CloudSessionResponse {
environmentId: string
namespaceId: string
shortId: string
}
export interface CloudSession extends CloudSessionResponse {
api: CloudApi
id: string
projectId: string
}
// Represents a cloud environment
export interface CloudEnvironment {
id: string
name: string
}
// Represents a cloud project
export interface CloudProject {
id: string
name: string
repositoryUrl: string
environments: CloudEnvironment[]
}
export interface GetSecretsParams {
log: Log
projectId: string
environmentName: string
}
function toCloudProject(
project: GetProjectResponse["data"] | ListProjectsResponse["data"][0] | CreateProjectsForRepoResponse["data"][0]
): CloudProject {
const environments: CloudEnvironment[] = []
for (const environment of project.environments) {
environments.push({ id: environment.id, name: environment.name })
}
return {
id: project.id,
name: project.name,
repositoryUrl: project.repositoryUrl,
environments,
}
}
/**
* A helper function to get the cloud domain from a project config. Uses the env var
* GARDEN_CLOUD_DOMAIN to override a configured domain.
*/
export function getGardenCloudDomain(configuredDomain: string | undefined): string {
let cloudDomain: string | undefined
if (gardenEnv.GARDEN_CLOUD_DOMAIN) {
cloudDomain = new URL(gardenEnv.GARDEN_CLOUD_DOMAIN).origin
} else if (configuredDomain) {
cloudDomain = new URL(configuredDomain).origin
}
return cloudDomain || DEFAULT_GARDEN_CLOUD_DOMAIN
}
export interface CloudApiFactoryParams {
log: Log
cloudDomain: string
globalConfigStore: GlobalConfigStore
skipLogging?: boolean
}
export type CloudApiFactory = (params: CloudApiFactoryParams) => Promise<CloudApi | undefined>
/**
* The Enterprise API client.
*
* Can only be initialized if the user is actually logged in. Includes a handful of static helper methods
* for cases where the user is not logged in (e.g. the login method itself).
*/
export class CloudApi {
private intervalId: NodeJS.Timeout | null = null
private intervalMsec = 4500 // Refresh interval in ms, it needs to be less than refreshThreshold/2
private apiPrefix = "api"
private _profile?: GetProfileResponse["data"]
private projects: Map<string, CloudProject> // keyed by project ID
private registeredSessions: Map<string, CloudSession> // keyed by session ID
private log: Log
public readonly domain: string
public readonly distroName: string
private globalConfigStore: GlobalConfigStore
constructor({ log, domain, globalConfigStore }: { log: Log; domain: string; globalConfigStore: GlobalConfigStore }) {
this.log = log
this.domain = domain
this.distroName = getCloudDistributionName(domain)
this.globalConfigStore = globalConfigStore
this.projects = new Map()
this.registeredSessions = new Map()
}
/**
* Initialize the Cloud API.
*
* Returns null if the user is not logged in.
*
* Throws if the user is logged in but the token is invalid and can't be refreshed.
*
* Optionally skip logging during initialization. Useful for noProject commands that need to use the class
* without all the "flair".
*/
static async factory({ log, cloudDomain, globalConfigStore, skipLogging = false }: CloudApiFactoryParams) {
const distroName = getCloudDistributionName(cloudDomain)
const fixLevel = skipLogging ? LogLevel.silly : undefined
const cloudFactoryLog = log.createLog({ fixLevel, name: getCloudLogSectionName(distroName), showDuration: true })
cloudFactoryLog.debug("Initializing Garden Cloud API client.")
const token = await CloudApi.getStoredAuthToken(log, globalConfigStore, cloudDomain)
if (!token && !gardenEnv.GARDEN_AUTH_TOKEN) {
log.debug(
`No auth token found, proceeding without access to ${distroName}. Command results for this command run will not be available in ${distroName}.`
)
return
}
const api = new CloudApi({ log, domain: cloudDomain, globalConfigStore })
const tokenIsValid = await api.checkClientAuthToken()
cloudFactoryLog.debug("Authorizing...")
if (gardenEnv.GARDEN_AUTH_TOKEN) {
// Throw if using an invalid "CI" access token
if (!tokenIsValid) {
throw new CloudApiError({
message: deline`
The provided access token is expired or has been revoked for ${cloudDomain}, please create a new
one from the ${distroName} UI.`,
})
}
} else {
// Refresh the token if it's invalid.
if (!tokenIsValid) {
cloudFactoryLog.debug({ msg: `Current auth token is invalid, refreshing` })
// We can assert the token exists since we're not using GARDEN_AUTH_TOKEN
await api.refreshToken(token!)
}
// Start refresh interval if using JWT
cloudFactoryLog.debug({ msg: `Starting refresh interval.` })
api.startInterval()
}
return api
}
static async saveAuthToken(
log: Log,
globalConfigStore: GlobalConfigStore,
tokenResponse: AuthTokenResponse,
domain: string
) {
const distroName = getCloudDistributionName(domain)
if (!tokenResponse.token) {
const errMsg = deline`
Received a null/empty client auth token while logging in. This indicates that either your user account hasn't
yet been created in ${distroName}, or that there's a problem with your account's VCS username / login
credentials.
`
throw new CloudApiError({ message: errMsg })
}
try {
const validityMs = tokenResponse.tokenValidity || 604800000
await globalConfigStore.set("clientAuthTokens", domain, {
token: tokenResponse.token,
refreshToken: tokenResponse.refreshToken,
validity: add(new Date(), { seconds: validityMs / 1000 }),
})
log.debug("Saved client auth token to config store")
} catch (error) {
const redactedResponse = cloneDeep(tokenResponse)
if (redactedResponse.refreshToken) {
redactedResponse.refreshToken = "<Redacted>"
}
if (redactedResponse.token) {
redactedResponse.token = "<Redacted>"
}
// If we get here, this is a bug.
throw InternalError.wrapError(
error,
dedent`
An error occurred while saving client auth token to local config db.
Token response: ${JSON.stringify(redactedResponse)}`
)
}
}
/**
* Returns the full client auth token from the local DB.
*
* In the inconsistent/erroneous case of more than one auth token existing in the local store, picks the first auth
* token and deletes all others.
*/
static async getStoredAuthToken(log: Log, globalConfigStore: GlobalConfigStore, domain: string) {
log.silly(() => `Retrieving client auth token from config store`)
return globalConfigStore.get("clientAuthTokens", domain)
}
/**
* If a persisted client auth token was found, or if the GARDEN_AUTH_TOKEN environment variable is present,
* returns it. Returns null otherwise.
*
* Note that the GARDEN_AUTH_TOKEN environment variable takes precedence over a persisted auth token if both are
* present.
*/
static async getAuthToken(
log: Log,
globalConfigStore: GlobalConfigStore,
domain: string
): Promise<string | undefined> {
const tokenFromEnv = gardenEnv.GARDEN_AUTH_TOKEN
if (tokenFromEnv) {
log.silly(() => "Read client auth token from env")
return tokenFromEnv
}
return (await CloudApi.getStoredAuthToken(log, globalConfigStore, domain))?.token
}
/**
* If a persisted client auth token exists, deletes it.
*/
static async clearAuthToken(log: Log, globalConfigStore: GlobalConfigStore, domain: string) {
await globalConfigStore.delete("clientAuthTokens", domain)
log.debug("Cleared persisted auth token (if any)")
}
private startInterval() {
this.log.debug({ msg: `Will run refresh function every ${this.intervalMsec} ms.` })
this.intervalId = setInterval(() => {
this.refreshTokenIfExpired().catch((err) => {
this.log.debug({ msg: "Something went wrong while trying to refresh the authentication token." })
this.log.debug({ msg: err.message })
})
}, this.intervalMsec)
}
close() {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = null
}
}
sessionRegistered(id: string) {
return this.registeredSessions.has(id)
}
async getProjectByName(projectName: string): Promise<CloudProject | undefined> {
let response: ListProjectsResponse
try {
response = await this.get<ListProjectsResponse>(
`/projects?name=${encodeURIComponent(projectName)}&exactMatch=true`
)
} catch (err) {
throw new CloudApiError({
message: `Failed to find Garden Cloud project by name: ${err}`,
})
}
const projectList = response.data
// Expect a single project, otherwise we fail with an error
if (projectList.length > 1) {
throw new CloudApiDuplicateProjectsError({
message: deline`Found an unexpected state with multiple projects using the same name, ${projectName}.
Please make sure there is only one project with the given name.
Projects can be deleted through the Garden Cloud UI at ${this.domain}`,
})
}
if (projectList.length === 0) {
return undefined
}
const project = toCloudProject(projectList[0])
// Cache the entry by ID
this.projects.set(project.id, project)
return project
}
async createProject(projectName: string): Promise<CloudProject> {
let response: CreateProjectsForRepoResponse
try {
const createRequest = {
name: projectName,
repositoryUrl: "",
relativeProjectRootPath: "",
importFromVcsProvider: false,
}
response = await this.post<CreateProjectsForRepoResponse>(`/projects/`, {
body: createRequest,
})
} catch (err) {
this.log.debug(`Create project request failed with error, ${err}`)
throw err
}
const project: CreateProjectsForRepoResponse["data"][0] = response.data[0]
return toCloudProject(project)
}
async getOrCreateProjectByName(projectName: string): Promise<CloudProject> {
let project: CloudProject | undefined = await this.getProjectByName(projectName)
if (!project) {
project = await this.createProject(projectName)
}
return project
}
private async refreshTokenIfExpired() {
const token = await this.globalConfigStore.get("clientAuthTokens", this.domain)
if (!token || gardenEnv.GARDEN_AUTH_TOKEN) {
this.log.debug({ msg: "Nothing to refresh, returning." })
return
}
// Note: lazy-loading for startup performance
const { sub, isAfter } = await import("date-fns")
if (isAfter(new Date(), sub(token.validity, { seconds: refreshThreshold }))) {
await this.refreshToken(token)
}
}
private async refreshToken(token: ClientAuthToken) {
try {
const res = await this.get<any>("token/refresh", { headers: { Cookie: `rt=${token?.refreshToken}` } })
let cookies: any
if (res.headers["set-cookie"] instanceof Array) {
cookies = res.headers["set-cookie"].map((cookieStr) => {
return Cookie.parse(cookieStr)
})
} else {
cookies = [Cookie.parse(res.headers["set-cookie"] || "")]
}
const rt = cookies.find((cookie: any) => cookie?.key === "rt")
const tokenObj = {
token: res.data.jwt,
refreshToken: rt.value || "",
tokenValidity: res.data.jwtValidity,
}
await CloudApi.saveAuthToken(this.log, this.globalConfigStore, tokenObj, this.domain)
} catch (err) {
if (!(err instanceof GotHttpError)) {
throw err
}
this.log.debug({ msg: `Failed to refresh the token.` })
throw new CloudApiTokenRefreshError({
message: `An error occurred while verifying client auth token with ${getCloudDistributionName(this.domain)}: ${
err.message
}. Response status code: ${err.response.statusCode}`,
})
}
}
private async apiFetch<T>(path: string, params: ApiFetchParams): Promise<ApiFetchResponse<T>> {
const { method, headers, retry, retryDescription } = params
this.log.silly(() => `Calling Cloud API with ${method} ${path}`)
const token = await CloudApi.getAuthToken(this.log, this.globalConfigStore, this.domain)
// TODO add more logging details
const requestObj = {
method,
headers: {
"x-garden-client-version": gardenClientVersion,
"x-garden-client-name": gardenClientName,
...headers,
...makeAuthHeader(token || ""),
},
json: params.body,
}
const requestOptions: GotJsonOptions = {
...requestObj,
responseType: "json",
}
const url = new URL(`/${this.apiPrefix}/${stripLeadingSlash(path)}`, this.domain)
if (retry) {
let retryLog: Log | undefined = undefined
const retryLimit = params.maxRetries || 3
requestOptions.retry = {
methods: ["GET", "POST", "PUT", "DELETE"], // We explicitly include the POST method if `retry = true`.
statusCodes: [
408, // Request Timeout
// 413, // Payload Too Large: No use in retrying.
429, // Too Many Requests
// 500, // Internal Server Error: Generally not safe to retry without potentially creating duplicate data.
502, // Bad Gateway
503, // Service Unavailable
504, // Gateway Timeout
// Cloudflare-specific status codes
521, // Web Server Is Down
522, // Connection Timed Out
524, // A Timeout Occurred
],
limit: retryLimit,
}
requestOptions.hooks = {
beforeRetry: [
(error, retryCount) => {
if (error) {
// Intentionally skipping search params in case they contain tokens or sensitive data.
const href = url.origin + url.pathname
const description = retryDescription || `Request`
retryLog = retryLog || this.log.createLog({ fixLevel: LogLevel.debug })
const statusCodeDescription = error.code ? ` (status code ${error.code})` : ``
retryLog.info(deline`
${description} failed with error ${error.message}${statusCodeDescription},
retrying (${retryCount}/${retryLimit}) (url=${href})
`)
}
},
],
// See: https://github.com/sindresorhus/got/issues/1489#issuecomment-805485731
afterResponse: [
(response) => {
if (isGotResponseOk(response)) {
response.request.destroy()
}
return response
},
],
}
} else {
requestOptions.retry = undefined // Disables retry
}
try {
const res = await got<T>(url.href, requestOptions)
if (!isObject(res.body)) {
throw new CloudApiError({
message: dedent`
Unexpected response from Garden Cloud: Expected object.
Request ID: ${res.headers["x-request-id"]}
Request url: ${url}
Response code: ${res?.statusCode}
Response body: ${JSON.stringify(res?.body)}
`,
responseStatusCode: res?.statusCode,
})
}
return {
...res.body,
headers: res.headers,
}
} catch (e: unknown) {
if (!(e instanceof RequestError)) {
throw e
}
// The assumption here is that Garden Enterprise is self-hosted.
// This error should only be thrown if the Garden Enterprise instance is not hosted by us (i.e. Garden Inc.)
if (e.code === "DEPTH_ZERO_SELF_SIGNED_CERT" && getCloudDistributionName(this.domain) === "Garden Enterprise") {
throw new CloudApiError({
message: dedent`
SSL error when communicating to Garden Cloud: ${e}
If your Garden Cloud instance is self-hosted and you are using a self-signed certificate, Garden will not trust your system's CA certificates.
In case if you need to trust extra certificate authorities, consider exporting the environment variable NODE_EXTRA_CA_CERTS. See https://nodejs.org/api/cli.html#node_extra_ca_certsfile
Request url: ${url}
Error code: ${e.code}
`,
})
}
throw e
}
}
async get<T>(path: string, opts: ApiFetchOptions = {}) {
const { headers, retry, retryDescription, maxRetries } = opts
return this.apiFetch<T>(path, {
method: "GET",
headers: headers || {},
retry: retry !== false, // defaults to true unless false is explicitly passed
retryDescription,
maxRetries,
})
}
async delete<T>(path: string, opts: ApiFetchOptions = {}) {
const { headers, retry, retryDescription, maxRetries } = opts
return await this.apiFetch<T>(path, {
method: "DELETE",
headers: headers || {},
retry: retry !== false, // defaults to true unless false is explicitly passed
retryDescription,
maxRetries,
})
}
async post<T>(path: string, opts: ApiFetchOptions & { body?: any } = {}) {
const { body, headers, retry, retryDescription, maxRetries } = opts
return this.apiFetch<T>(path, {
method: "POST",
body: body || {},
headers: headers || {},
retry: !!retry, // defaults to false unless true is explicitly passed
retryDescription,
maxRetries,
})
}
async put<T>(path: string, opts: ApiFetchOptions & { body?: any } = {}) {
const { body, headers, retry, retryDescription, maxRetries } = opts
return this.apiFetch<T>(path, {
method: "PUT",
body: body || {},
headers: headers || {},
retry: !!retry, // defaults to false unless true is explicitly passed
retryDescription,
maxRetries,
})
}
async registerSession({
parentSessionId,
sessionId,
projectId,
commandInfo,
localServerPort,
environment,
namespace,
isDevCommand,
}: {
parentSessionId: string | undefined
sessionId: string
projectId: string
commandInfo: CommandInfo
localServerPort: number | undefined
environment: string
namespace: string
isDevCommand: boolean
}): Promise<CloudSession | undefined> {
let session = this.registeredSessions.get(sessionId)
if (session) {
return session
}
try {
const body = {
sessionId,
parentSessionId,
commandInfo,
localServerPort,
projectUid: projectId,
environment,
namespace,
isDevCommand,
}
this.log.debug(`Registering session with ${this.distroName} for ${projectId} in ${environment}/${namespace}.`)
const res: CloudSessionResponse = await this.post("sessions", {
body,
retry: true,
retryDescription: "Registering session",
})
this.log.debug(`Successfully registered session with ${this.distroName}.`)
session = { api: this, id: sessionId, projectId, ...res }
this.registeredSessions.set(sessionId, session)
return session
} catch (err) {
if (!(err instanceof GotHttpError)) {
throw err
}
// We don't want the command to fail when an error occurs in the backend during session registration.
if (err.response.statusCode === 422) {
const errMsg = deline`
Session registration skipped due to mismatch between CLI and API versions. Please make sure your Garden CLI
version is compatible with your version of ${this.distroName}.
`
this.log.debug(errMsg)
} else {
this.log.warn(`An error occurred while registering the session: ${err.message}`)
}
return
}
}
async getProjectById(projectId: string) {
const existing = this.projects.get(projectId)
if (existing) {
return existing
}
const res = await this.get<GetProjectResponse>(`/projects/uid/${projectId}`)
const projectData: GetProjectResponse["data"] = res.data
const project = toCloudProject(projectData)
this.projects.set(projectId, project)
return project
}
async getProjectByIdOrThrow({
projectId,
projectName,
}: Pick<Garden, "projectId" | "projectName">): Promise<CloudProject> {
let project: CloudProject | undefined
if (projectId) {
project = await this.getProjectById(projectId)
}
if (!project) {
throw new CloudApiError({
message: `Project ${projectName} is not a ${getCloudDistributionName(this.domain)} project`,
})
}
return project
}
async getProfile() {
if (this._profile) {
return this._profile
}
const res = await this.get<GetProfileResponse>(`/profile`)
this._profile = res.data
return this._profile
}
/**
* Checks with the backend whether the provided client auth token is valid.
*/
async checkClientAuthToken(): Promise<boolean> {
let valid = false
try {
const url = new URL("/token/verify", this.domain)
this.log.debug(`Checking client auth token with ${getCloudDistributionName(this.domain)}: ${url.href}`)
await this.get("token/verify")
valid = true
} catch (err) {
if (!(err instanceof GotHttpError)) {
throw err
}
if (err.response.statusCode !== 401) {
throw new CloudApiError({
message: `An error occurred while verifying client auth token with ${getCloudDistributionName(
this.domain
)}: ${err.message}`,
responseStatusCode: err.response.statusCode,
})
}
}
this.log.debug(`Checked client auth token with ${getCloudDistributionName(this.domain)} - valid: ${valid}`)
return valid
}
getProjectUrl(projectId: string) {
return new URL(`/projects/${projectId}`, this.domain)
}
getCommandResultUrl({ projectId, sessionId, shortId }: { projectId: string; sessionId: string; shortId: string }) {
// fallback to full url if shortid is missing
const path = shortId ? `/go/command/${shortId}` : `/projects/${projectId}/commands/${sessionId}`
return new URL(path, this.domain)
}
getLivePageUrl({ shortId }: { shortId: string }) {
const path = `/go/${shortId}`
return new URL(path, this.domain)
}
getRegisteredSession(sessionId: string) {
return this.registeredSessions.get(sessionId)
}
async getSecrets({ log, projectId, environmentName }: GetSecretsParams): Promise<StringMap> {
let secrets: StringMap = {}
const distroName = getCloudDistributionName(this.domain)
try {
const res = await this.get<BaseResponse>(`/secrets/projectUid/${projectId}/env/${environmentName}`)
secrets = res.data
} catch (err) {
if (!(err instanceof GotHttpError)) {
throw err
}
// This happens if an environment or project does not exist
if (err.response.statusCode === 404) {
const errorHeaderMsg = styles.error(`Unable to read secrets from ${distroName}.`)
const errorDetailMsg = styles.accent(dedent`
Either the environment ${styles.accent.bold(environmentName)} does not exist in ${distroName},
or no project matches the project ID ${styles.accent.bold(projectId)} in your project level garden.yml file.
💡Suggestion:
Visit ${styles.link(this.domain)} to review existing environments and projects.
First check whether an environment with name ${environmentName} exists for this project. You
can view the list of environments and the project ID on the project's Settings page.
${styles.accent.bold(
"If the environment does not exist"
)}, you can either create one from the Settings page or update
the environments in your project level garden.yml config to match one that already exists.
${styles.accent.bold(
"If a project with this ID does not exist"
)}, it's likely because the ID has been changed in the
project level garden.yml config file or the project has been deleted from ${distroName}.
Either update the ID in the project level garden.yml config file to match one of an
existing project or import a new project from the Projects page and replace the ID in your
project configuration with the ID of the new project.
`)
log.error(dedent`
${errorHeaderMsg}
${errorDetailMsg}\n
`)
} else {
throw err
}
}
const emptyKeys = Object.keys(secrets).filter((key) => !secrets[key])
if (emptyKeys.length > 0) {
const prefix =
emptyKeys.length === 1
? "The following secret key has an empty value"
: "The following secret keys have empty values"
log.error(`${prefix}: ${emptyKeys.sort().join(", ")}`)
}
return secrets
}
async fetchAllSecrets(projectId: string, log: Log): Promise<CloudApiSecretResult[]> {
let page = 0
const secrets: CloudApiSecretResult[] = []
let hasMore = true
while (hasMore) {
log.debug(`Fetching page ${page}`)
const q = queryString.stringify({ projectId, offset: page * secretsPageLimit, limit: secretsPageLimit })
const res = await this.get<ListSecretsResponse>(`/secrets?${q}`)
if (res.data.length === 0) {
hasMore = false
} else {
secrets.push(...res.data)
page++
}
}
return secrets
}
async createSecret(request: CreateSecretRequest): Promise<CreateSecretResponse> {
return await this.post<CreateSecretResponse>(`/secrets`, { body: request })
}
async createSecrets({ request, log }: { request: BulkCreateSecretRequest; log: Log }): Promise<BulkOperationResult> {
const { secrets, environmentId, userId, projectId } = request
const errors: ApiCommandError[] = []
const results: SecretResult[] = []
for (const [counter, { name, value }] of enumerate(secrets, 1)) {
log.info({ msg: `Creating secrets... → ${counter}/${secrets.length}` })
try {
const body = { environmentId, userId, projectId, name, value }
const res = await this.createSecret(body)
results.push(res.data)
} catch (err) {
if (!(err instanceof HTTPError)) {
throw err
}
// skip already existing secret and continue the loop
if (err.response.statusCode === 409) {
errors.push({
identifier: name,
message: "Secret already exists",
})
} else {
throw err
}
}
}
return { results, errors }
}
async updateSecret(secretId: string, request: UpdateSecretRequest): Promise<UpdateSecretResponse> {
return await this.put<UpdateSecretResponse>(`/secrets/${secretId}`, { body: request })
}
async updateSecrets({ request, log }: { request: BulkUpdateSecretRequest; log: Log }): Promise<BulkOperationResult> {
const { secrets } = request
const errors: ApiCommandError[] = []
const results: SecretResult[] = []
for (const [counter, secret] of enumerate(secrets, 1)) {
log.info({ msg: `Updating secrets... → ${counter}/${secrets.length}` })
try {
const body = omit(secret, "id")
const res = await this.updateSecret(secret.id, body)
results.push(res.data)
} catch (err) {
if (!(err instanceof GardenError)) {
throw err
}
errors.push({
identifier: secret.name,
message: err.message,
})
}
}
return { results, errors }
}
async registerCloudBuilderBuild(body: {
actionName: string
actionUid: string
coreSessionId: string
}): Promise<RegisterCloudBuilderBuildResponse> {
try {
return await this.post<RegisterCloudBuilderBuildResponse>(`/cloudbuilder/builds/`, {
body,
})
} catch (err) {