From 1ea9aedc68e34de6905615c71677f0571698fdce Mon Sep 17 00:00:00 2001 From: Mikael Hoegqvist Tabor Date: Wed, 13 Dec 2023 12:59:05 +0100 Subject: [PATCH] chore: handle cases which relied on api to be undefined on no token error --- core/src/cli/cli.ts | 8 ++++++-- core/src/cloud/api.ts | 2 +- core/src/commands/logout.ts | 13 ++++++------- core/src/server/instance-manager.ts | 13 +++++++++++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/src/cli/cli.ts b/core/src/cli/cli.ts index 4f006e9d241..7a411d3e357 100644 --- a/core/src/cli/cli.ts +++ b/core/src/cli/cli.ts @@ -51,7 +51,7 @@ import { generateBasicDebugInfoReport } from "../commands/get/get-debug-info.js" import type { AnalyticsHandler } from "../analytics/analytics.js" import type { GardenPluginReference } from "../plugin/plugin.js" import type { CloudApiFactory } from "../cloud/api.js" -import { CloudApi, CloudApiTokenRefreshError, getGardenCloudDomain } from "../cloud/api.js" +import { CloudApi, CloudApiNoTokenError, CloudApiTokenRefreshError, getGardenCloudDomain } from "../cloud/api.js" import { findProjectConfig } from "../config/base.js" import { pMemoizeDecorator } from "../lib/p-memoize.js" import { getCustomCommands } from "../commands/custom.js" @@ -269,7 +269,11 @@ ${renderCommands(commands)} requireLogin: config?.requireLogin, }) } catch (err) { - if (err instanceof CloudApiTokenRefreshError) { + if (err instanceof CloudApiNoTokenError) { + // this means the user has no token stored for the domain and we should just continue + // without a cloud api instance + gardenInitLog?.debug(`Cloud not configured since no token found for ${distroName} at ${cloudDomain}`) + } else if (err instanceof CloudApiTokenRefreshError) { log.warn(dedent` Unable to authenticate against ${distroName} with the current session token. Command results for this command run will not be available in ${distroName}. If this not a diff --git a/core/src/cloud/api.ts b/core/src/cloud/api.ts index a6f4412b0c1..1190c91f308 100644 --- a/core/src/cloud/api.ts +++ b/core/src/cloud/api.ts @@ -229,7 +229,7 @@ export class CloudApi { skipLogging = false, projectId = undefined, requireLogin = undefined, - }: CloudApiFactoryParams) { + }: CloudApiFactoryParams): Promise { const distroName = getCloudDistributionName(cloudDomain) const fixLevel = skipLogging ? LogLevel.silly : undefined const cloudFactoryLog = log.createLog({ fixLevel, name: getCloudLogSectionName(distroName), showDuration: true }) diff --git a/core/src/commands/logout.ts b/core/src/commands/logout.ts index 69d3050baa8..486708c144c 100644 --- a/core/src/commands/logout.ts +++ b/core/src/commands/logout.ts @@ -9,7 +9,7 @@ import type { CommandParams, CommandResult } from "./base.js" import { Command } from "./base.js" import { printHeader } from "../logger/util.js" -import { CloudApi, getGardenCloudDomain } from "../cloud/api.js" +import { CloudApi, CloudApiNoTokenError, getGardenCloudDomain } from "../cloud/api.js" import { getCloudDistributionName } from "../util/cloud.js" import { dedent, deline } from "../util/string.js" import { ConfigurationError } from "../exceptions.js" @@ -82,17 +82,16 @@ export class LogOutCommand extends Command<{}, Opts> { requireLogin: undefined, }) - if (!cloudApi) { - return {} - } - await cloudApi.post("token/logout", { headers: { Cookie: `rt=${token?.refreshToken}` } }) cloudApi.close() } catch (err) { - const msg = dedent` + // This is expected if the user never logged in + if (!(err instanceof CloudApiNoTokenError)) { + const msg = dedent` The following issue occurred while logging out from ${distroName} (your session will be cleared regardless): ${err}\n ` - log.warn(msg) + log.warn(msg) + } } finally { await CloudApi.clearAuthToken(log, garden.globalConfigStore, cloudDomain) log.success(`Successfully logged out from ${cloudDomain}.`) diff --git a/core/src/server/instance-manager.ts b/core/src/server/instance-manager.ts index dc0c7f9b4a2..14ce88580b8 100644 --- a/core/src/server/instance-manager.ts +++ b/core/src/server/instance-manager.ts @@ -12,7 +12,7 @@ import { Autocompleter } from "../cli/autocomplete.js" import { parseCliVarFlags } from "../cli/helpers.js" import type { ParameterObject, ParameterValues } from "../cli/params.js" import type { CloudApiFactory, CloudApiFactoryParams } from "../cloud/api.js" -import { CloudApi, getGardenCloudDomain } from "../cloud/api.js" +import { CloudApi, CloudApiNoTokenError, getGardenCloudDomain } from "../cloud/api.js" import type { Command } from "../commands/base.js" import { getBuiltinCommands, flattenCommands } from "../commands/commands.js" import { getCustomCommands } from "../commands/custom.js" @@ -202,7 +202,16 @@ export class GardenInstanceManager { let api = this.cloudApis.get(cloudDomain) if (!api) { - api = await this.cloudApiFactory(params) + try { + api = await this.cloudApiFactory(params) + } catch (err) { + // handles the case when the user should not be logged in + // otherwise we throw any error that can occure while + // authenticating + if (!(err instanceof CloudApiNoTokenError)) { + throw err + } + } api && this.cloudApis.set(cloudDomain, api) }