Skip to content

Commit

Permalink
chore: handle cases which relied on api to be undefined on no token e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
mkhq committed Dec 13, 2023
1 parent aa47f40 commit 1ea9aed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
8 changes: 6 additions & 2 deletions core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class CloudApi {
skipLogging = false,
projectId = undefined,
requireLogin = undefined,
}: CloudApiFactoryParams) {
}: CloudApiFactoryParams): Promise<CloudApi> {
const distroName = getCloudDistributionName(cloudDomain)
const fixLevel = skipLogging ? LogLevel.silly : undefined
const cloudFactoryLog = log.createLog({ fixLevel, name: getCloudLogSectionName(distroName), showDuration: true })
Expand Down
13 changes: 6 additions & 7 deletions core/src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}.`)
Expand Down
13 changes: 11 additions & 2 deletions core/src/server/instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 1ea9aed

Please sign in to comment.