Skip to content

Commit bbf7b13

Browse files
authored
fix(cli): remove defaulting to keyring when --global-config set (#20943)
This fixes a regression that caused the VS code extension to be unable to authenticate after making keyring usage on by default. This is because the VS code extension assumes the CLI will always use the session token stored on disk, specifically in the directory specified by --global-config. This fix makes keyring usage enabled when the --global-config directory is not set. This is a bit wonky but necessary to allow the extension to continue working without modification and without backwards compat concerns. In the future we should modify these extensions to either access the credential in the keyring (like Coder Desktop) or some other approach that doesn't rely on the session token being stored on disk. Tests: `coder login dev.coder.com` -> token stored in keyring `coder login --global-config=/tmp/ dev.coder.com` -> token stored in `/tmp/session`
1 parent c87c33f commit bbf7b13

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

cli/clitest/clitest.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func NewWithCommand(
6161
t testing.TB, cmd *serpent.Command, args ...string,
6262
) (*serpent.Invocation, config.Root) {
6363
configDir := config.Root(t.TempDir())
64-
// Keyring usage is disabled here because many existing tests expect the session token
65-
// to be stored on disk and is not properly instrumented for parallel testing against
66-
// the actual operating system keyring.
67-
invArgs := append([]string{"--global-config", string(configDir), "--use-keyring=false"}, args...)
64+
// Keyring usage is disabled here when --global-config is set because many existing
65+
// tests expect the session token to be stored on disk and is not properly instrumented
66+
// for parallel testing against the actual operating system keyring.
67+
invArgs := append([]string{"--global-config", string(configDir)}, args...)
6868
return setupInvocation(t, cmd, invArgs...), configDir
6969
}
7070

cli/keyring_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func setupKeyringTestEnv(t *testing.T, clientURL string, args ...string) keyring
5454

5555
serviceName := keyringTestServiceName(t)
5656
root.WithKeyringServiceName(serviceName)
57+
root.UseKeyringWithGlobalConfig()
5758

5859
inv, cfg := clitest.NewWithDefaultKeyringCommand(t, cmd, args...)
5960

@@ -169,6 +170,7 @@ func TestUseKeyring(t *testing.T) {
169170
logoutCmd, err := logoutRoot.Command(logoutRoot.AGPL())
170171
require.NoError(t, err)
171172
logoutRoot.WithKeyringServiceName(env.serviceName)
173+
logoutRoot.UseKeyringWithGlobalConfig()
172174

173175
logoutInv, _ := clitest.NewWithDefaultKeyringCommand(t, logoutCmd,
174176
"logout",

cli/root.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
483483
Flag: varUseKeyring,
484484
Env: envUseKeyring,
485485
Description: "Store and retrieve session tokens using the operating system " +
486-
"keyring. Enabled by default. If the keyring is not supported on the " +
487-
"current platform, file-based storage is used automatically. Set to " +
488-
"false to force file-based storage.",
486+
"keyring. This flag is ignored and file-based storage is used when " +
487+
"--global-config is set or keyring usage is not supported on the current " +
488+
"platform. Set to false to force file-based storage on supported platforms.",
489489
Default: "true",
490490
Value: serpent.BoolOf(&r.useKeyring),
491491
Group: globalGroup,
@@ -536,11 +536,12 @@ type RootCmd struct {
536536
disableDirect bool
537537
debugHTTP bool
538538

539-
disableNetworkTelemetry bool
540-
noVersionCheck bool
541-
noFeatureWarning bool
542-
useKeyring bool
543-
keyringServiceName string
539+
disableNetworkTelemetry bool
540+
noVersionCheck bool
541+
noFeatureWarning bool
542+
useKeyring bool
543+
keyringServiceName string
544+
useKeyringWithGlobalConfig bool
544545
}
545546

546547
// InitClient creates and configures a new client with authentication, telemetry,
@@ -721,8 +722,14 @@ func (r *RootCmd) createUnauthenticatedClient(ctx context.Context, serverURL *ur
721722
// flag.
722723
func (r *RootCmd) ensureTokenBackend() sessionstore.Backend {
723724
if r.tokenBackend == nil {
725+
// Checking for the --global-config directory being set is a bit wonky but necessary
726+
// to allow extensions that invoke the CLI with this flag (e.g. VS code) to continue
727+
// working without modification. In the future we should modify these extensions to
728+
// either access the credential in the keyring (like Coder Desktop) or some other
729+
// approach that doesn't rely on the session token being stored on disk.
730+
assumeExtensionInUse := r.globalConfig != config.DefaultDir() && !r.useKeyringWithGlobalConfig
724731
keyringSupported := runtime.GOOS == "windows" || runtime.GOOS == "darwin"
725-
if r.useKeyring && keyringSupported {
732+
if r.useKeyring && !assumeExtensionInUse && keyringSupported {
726733
serviceName := sessionstore.DefaultServiceName
727734
if r.keyringServiceName != "" {
728735
serviceName = r.keyringServiceName
@@ -742,6 +749,13 @@ func (r *RootCmd) WithKeyringServiceName(serviceName string) {
742749
r.keyringServiceName = serviceName
743750
}
744751

752+
// UseKeyringWithGlobalConfig enables the use of the keyring storage backend
753+
// when the --global-config directory is set. This is only intended as an override
754+
// for tests, which require specifying the global config directory for test isolation.
755+
func (r *RootCmd) UseKeyringWithGlobalConfig() {
756+
r.useKeyringWithGlobalConfig = true
757+
}
758+
745759
type AgentAuth struct {
746760
// Agent Client config
747761
agentToken string

cli/testdata/coder_--help.golden

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ variables or flags.
111111

112112
--use-keyring bool, $CODER_USE_KEYRING (default: true)
113113
Store and retrieve session tokens using the operating system keyring.
114-
Enabled by default. If the keyring is not supported on the current
115-
platform, file-based storage is used automatically. Set to false to
116-
force file-based storage.
114+
This flag is ignored and file-based storage is used when
115+
--global-config is set or keyring usage is not supported on the
116+
current platform. Set to false to force file-based storage on
117+
supported platforms.
117118

118119
-v, --verbose bool, $CODER_VERBOSE
119120
Enable verbose output.

docs/reference/cli/index.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/testdata/coder_--help.golden

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ variables or flags.
7070

7171
--use-keyring bool, $CODER_USE_KEYRING (default: true)
7272
Store and retrieve session tokens using the operating system keyring.
73-
Enabled by default. If the keyring is not supported on the current
74-
platform, file-based storage is used automatically. Set to false to
75-
force file-based storage.
73+
This flag is ignored and file-based storage is used when
74+
--global-config is set or keyring usage is not supported on the
75+
current platform. Set to false to force file-based storage on
76+
supported platforms.
7677

7778
-v, --verbose bool, $CODER_VERBOSE
7879
Enable verbose output.

0 commit comments

Comments
 (0)