From 72807706057b51e3fb5e08d73ad61d072bcff574 Mon Sep 17 00:00:00 2001 From: Emanuele Libralato <10ko@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:35:00 +0200 Subject: [PATCH] fix: avoid throwing 401 when trying to login to cloud (#6447) * fix: avoid throwing 401 when trying to login to cloud * fix: revert package*.json edits * fix: tests * fix: tests * fix: tests * fix: linter * fix: tests --- core/src/cloud/api.ts | 2 ++ core/src/commands/login.ts | 14 ++-------- core/test/unit/src/commands/login.ts | 41 +--------------------------- 3 files changed, 5 insertions(+), 52 deletions(-) diff --git a/core/src/cloud/api.ts b/core/src/cloud/api.ts index b605f64cf5..877090b053 100644 --- a/core/src/cloud/api.ts +++ b/core/src/cloud/api.ts @@ -278,6 +278,7 @@ export class CloudApi { message: deline` The provided access token is expired or has been revoked for ${cloudDomain}, please create a new one from the ${distroName} UI.`, + responseStatusCode: 401, }) } } else { @@ -510,6 +511,7 @@ export class CloudApi { message: `An error occurred while verifying client auth token with ${getCloudDistributionName(this.domain)}: ${ err.message }. Response status code: ${err.response.statusCode}`, + responseStatusCode: err.response.statusCode, }) } } diff --git a/core/src/commands/login.ts b/core/src/commands/login.ts index 04bbc21710..0f06b69aa6 100644 --- a/core/src/commands/login.ts +++ b/core/src/commands/login.ts @@ -21,6 +21,7 @@ import { findProjectConfig } from "../config/base.js" import { BooleanParameter } from "../cli/params.js" import { getCloudDistributionName } from "../util/cloud.js" import { deline } from "../util/string.js" +import { gardenEnv } from "../constants.js" const loginTimeoutSec = 60 @@ -86,8 +87,6 @@ export class LoginCommand extends Command<{}, Opts> { // should use the default domain or not. The token lifecycle ends on logout. const cloudDomain: string = getGardenCloudDomain(projectConfig?.domain) - const distroName = getCloudDistributionName(cloudDomain) - try { const cloudApi = await CloudApi.factory({ log, cloudDomain, skipLogging: true, globalConfigStore }) @@ -97,18 +96,9 @@ export class LoginCommand extends Command<{}, Opts> { return {} } } catch (err) { - if (!(err instanceof CloudApiError)) { + if (!(err instanceof CloudApiError) || (err.responseStatusCode === 401 && gardenEnv.GARDEN_AUTH_TOKEN)) { throw err } - if (err.responseStatusCode === 401) { - const msg = dedent` - Looks like your session token is invalid. If you were previously logged into a different instance - of ${distroName}, log out first before logging in. - ` - log.warn(msg) - log.info("") - } - throw err } log.info({ msg: `Logging in to ${cloudDomain}...` }) diff --git a/core/test/unit/src/commands/login.ts b/core/test/unit/src/commands/login.ts index e34d56bdd4..c4ce3a7a03 100644 --- a/core/test/unit/src/commands/login.ts +++ b/core/test/unit/src/commands/login.ts @@ -13,11 +13,10 @@ import { expectError, getDataDir, makeTempDir, makeTestGarden, withDefaultGlobal import { AuthRedirectServer } from "../../../../src/cloud/auth.js" import { LoginCommand } from "../../../../src/commands/login.js" -import { dedent, randomString } from "../../../../src/util/string.js" +import { randomString } from "../../../../src/util/string.js" import { CloudApi } from "../../../../src/cloud/api.js" import { LogLevel } from "../../../../src/logger/logger.js" import { DEFAULT_GARDEN_CLOUD_DOMAIN, gardenEnv } from "../../../../src/constants.js" -import { CloudApiError } from "../../../../src/exceptions.js" import { getLogMessages } from "../../../../src/util/testing.js" import { GlobalConfigStore } from "../../../../src/config-store/global.js" import { makeDummyGarden } from "../../../../src/garden.js" @@ -226,44 +225,6 @@ describe("LoginCommand", () => { }) }) - it("should throw and print a helpful message on 401 errors", async () => { - const postfix = randomString() - const testToken = { - token: `dummy-token-${postfix}`, - refreshToken: `dummy-refresh-token-${postfix}`, - tokenValidity: 60, - } - - const command = new LoginCommand() - const garden = await makeTestGarden(getDataDir("test-projects", "login", "has-domain-and-id"), { - skipCloudConnect: false, - commandInfo: { name: "foo", args: {}, opts: {} }, - globalConfigStore, - }) - - await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!) - td.replace(CloudApi.prototype, "checkClientAuthToken", async () => false) - td.replace(CloudApi.prototype, "refreshToken", async () => { - throw new CloudApiError({ message: "bummer", responseStatusCode: 401 }) - }) - - const savedToken = await CloudApi.getStoredAuthToken(garden.log, garden.globalConfigStore, garden.cloudDomain!) - expect(savedToken).to.exist - expect(savedToken!.token).to.eql(testToken.token) - expect(savedToken!.refreshToken).to.eql(testToken.refreshToken) - - await expectError(async () => await command.action(loginCommandParams({ garden })), { - contains: "bummer", - }) - - const logOutput = getLogMessages(garden.log, (entry) => entry.level <= LogLevel.info).join("\n") - - expect(logOutput).to.include(dedent` - Looks like your session token is invalid. If you were previously logged into a different instance - of Garden Enterprise, log out first before logging in. - `) - }) - it("should not login if outside project root and disable-project-check flag is false", async () => { const postfix = randomString() const testToken = {