Skip to content

Commit

Permalink
fix: avoid throwing 401 when trying to login to cloud (#6447)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
10ko authored Sep 18, 2024
1 parent af2a0f2 commit 7280770
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 52 deletions.
2 changes: 2 additions & 0 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
})
}
}
Expand Down
14 changes: 2 additions & 12 deletions core/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 })

Expand All @@ -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}...` })
Expand Down
41 changes: 1 addition & 40 deletions core/test/unit/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 7280770

Please sign in to comment.