From 46a68bade1c575da7d082d634e22680b926b29f8 Mon Sep 17 00:00:00 2001 From: cotishq Date: Mon, 25 May 2026 12:04:48 +0530 Subject: [PATCH] fix: clean up auth when deleting named contexts Signed-off-by: cotishq --- cmd/context.go | 7 ++--- cmd/context_test.go | 54 +++++++++++++++++++++++++++++++++++++++ pkg/config/localconfig.go | 10 ++++---- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/cmd/context.go b/cmd/context.go index 8b7f418b..aafca74b 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -72,12 +72,13 @@ func deleteContext(context, configPath string) error { if localCfg == nil { return fmt.Errorf("Nothing to logout from") } - serverName, ok := localCfg.RemoveContext(context) + contextRef, ok := localCfg.RemoveContext(context) if !ok { return fmt.Errorf("Context %s does not exist", context) } - _ = localCfg.RemoveUser(context) - _ = localCfg.RemoveServer(serverName) + _ = localCfg.RemoveUser(contextRef.User) + _ = localCfg.RemoveServer(contextRef.Server) + _ = localCfg.RemoveAuth(contextRef.Server) if localCfg.IsEmpty() { err := localCfg.DeleteLocalConfig(configPath) diff --git a/cmd/context_test.go b/cmd/context_test.go index 8de7935a..1763655f 100644 --- a/cmd/context_test.go +++ b/cmd/context_test.go @@ -64,3 +64,57 @@ func TestDeleteContext(t *testing.T) { _, err = config.ReadLocalConfig(testConfigFilePath) require.NoError(t, err) } + +func TestDeleteNamedContextRemovesReferencedUserAndAuth(t *testing.T) { + configPath := t.TempDir() + "/config" + localConfig := `current-context: staging +contexts: +- name: dev + server: http://localhost:8080 + user: http://localhost:8080 + instance: "" +- name: staging + server: http://localhost:8083 + user: http://localhost:8083 + instance: "" +servers: +- name: "" + server: http://localhost:8080 + insecureTLS: true + keycloakEnable: true +- name: "" + server: http://localhost:8083 + insecureTLS: true + keycloakEnable: true +users: +- name: http://localhost:8080 + auth-token: stale-token + refresh-token: stale-refresh-token +- name: http://localhost:8083 + auth-token: "" + refresh-token: "" +auths: +- server: http://localhost:8080 + clientid: my-client + clientsecret: my-secret +` + + err := os.WriteFile(configPath, []byte(localConfig), os.ModePerm) + require.NoError(t, err) + err = os.Chmod(configPath, 0o600) + require.NoError(t, err) + + err = deleteContext("dev", configPath) + require.NoError(t, err) + + localCfg, err := config.ReadLocalConfig(configPath) + require.NoError(t, err) + require.NotNil(t, localCfg) + + assert.Equal(t, "staging", localCfg.CurrentContext) + assert.NotContains(t, localCfg.Contexts, config.ContextRef{Name: "dev", Server: "http://localhost:8080", User: "http://localhost:8080", Instance: ""}) + assert.NotContains(t, localCfg.Servers, config.Server{Server: "http://localhost:8080", InsecureTLS: true, KeycloakEnable: true}) + assert.NotContains(t, localCfg.Users, config.User{Name: "http://localhost:8080", AuthToken: "stale-token", RefreshToken: "stale-refresh-token"}) + assert.NotContains(t, localCfg.Auths, config.Auth{Server: "http://localhost:8080", ClientId: "my-client", ClientSecret: "my-secret"}) + assert.Contains(t, localCfg.Contexts, config.ContextRef{Name: "staging", Server: "http://localhost:8083", User: "http://localhost:8083", Instance: ""}) +} diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 1e5eeda6..a7c11132 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -215,15 +215,15 @@ func (l *LocalConfig) UpsertContext(context ContextRef) { l.Contexts = append(l.Contexts, context) } -// RemoveContext and returns server name and true if context was removed successfully -func (l *LocalConfig) RemoveContext(serverName string) (string, bool) { +// RemoveContext removes a context reference and returns it if context was removed successfully. +func (l *LocalConfig) RemoveContext(name string) (ContextRef, bool) { for i, c := range l.Contexts { - if c.Name == serverName { + if c.Name == name { l.Contexts = append(l.Contexts[:i], l.Contexts[i+1:]...) - return c.Server, true + return c, true } } - return "", false + return ContextRef{}, false } // RemoveToken and returns true if user was removed successfully