Skip to content

Commit bcbd734

Browse files
committed
fix: clean up auth when deleting named contexts
1 parent ee58a76 commit bcbd734

3 files changed

Lines changed: 63 additions & 8 deletions

File tree

cmd/context.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ func deleteContext(context, configPath string) error {
7272
if localCfg == nil {
7373
return fmt.Errorf("Nothing to logout from")
7474
}
75-
serverName, ok := localCfg.RemoveContext(context)
75+
contextRef, ok := localCfg.RemoveContext(context)
7676
if !ok {
7777
return fmt.Errorf("Context %s does not exist", context)
7878
}
79-
_ = localCfg.RemoveUser(context)
80-
_ = localCfg.RemoveServer(serverName)
79+
_ = localCfg.RemoveUser(contextRef.User)
80+
_ = localCfg.RemoveServer(contextRef.Server)
81+
_ = localCfg.RemoveAuth(contextRef.Server)
8182

8283
if localCfg.IsEmpty() {
8384
err := localCfg.DeleteLocalConfig(configPath)

cmd/context_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,57 @@ func TestDeleteContext(t *testing.T) {
6464
_, err = config.ReadLocalConfig(testConfigFilePath)
6565
require.NoError(t, err)
6666
}
67+
68+
func TestDeleteNamedContextRemovesReferencedUserAndAuth(t *testing.T) {
69+
configPath := t.TempDir() + "/config"
70+
localConfig := `current-context: staging
71+
contexts:
72+
- name: dev
73+
server: http://localhost:8080
74+
user: http://localhost:8080
75+
instance: ""
76+
- name: staging
77+
server: http://localhost:8083
78+
user: http://localhost:8083
79+
instance: ""
80+
servers:
81+
- name: ""
82+
server: http://localhost:8080
83+
insecureTLS: true
84+
keycloakEnable: true
85+
- name: ""
86+
server: http://localhost:8083
87+
insecureTLS: true
88+
keycloakEnable: true
89+
users:
90+
- name: http://localhost:8080
91+
auth-token: stale-token
92+
refresh-token: stale-refresh-token
93+
- name: http://localhost:8083
94+
auth-token: ""
95+
refresh-token: ""
96+
auths:
97+
- server: http://localhost:8080
98+
clientid: my-client
99+
clientsecret: my-secret
100+
`
101+
102+
err := os.WriteFile(configPath, []byte(localConfig), os.ModePerm)
103+
require.NoError(t, err)
104+
err = os.Chmod(configPath, 0o600)
105+
require.NoError(t, err)
106+
107+
err = deleteContext("dev", configPath)
108+
require.NoError(t, err)
109+
110+
localCfg, err := config.ReadLocalConfig(configPath)
111+
require.NoError(t, err)
112+
require.NotNil(t, localCfg)
113+
114+
assert.Equal(t, "staging", localCfg.CurrentContext)
115+
assert.NotContains(t, localCfg.Contexts, config.ContextRef{Name: "dev", Server: "http://localhost:8080", User: "http://localhost:8080", Instance: ""})
116+
assert.NotContains(t, localCfg.Servers, config.Server{Server: "http://localhost:8080", InsecureTLS: true, KeycloakEnable: true})
117+
assert.NotContains(t, localCfg.Users, config.User{Name: "http://localhost:8080", AuthToken: "stale-token", RefreshToken: "stale-refresh-token"})
118+
assert.NotContains(t, localCfg.Auths, config.Auth{Server: "http://localhost:8080", ClientId: "my-client", ClientSecret: "my-secret"})
119+
assert.Contains(t, localCfg.Contexts, config.ContextRef{Name: "staging", Server: "http://localhost:8083", User: "http://localhost:8083", Instance: ""})
120+
}

pkg/config/localconfig.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ func (l *LocalConfig) UpsertContext(context ContextRef) {
215215
l.Contexts = append(l.Contexts, context)
216216
}
217217

218-
// RemoveContext and returns server name and true if context was removed successfully
219-
func (l *LocalConfig) RemoveContext(serverName string) (string, bool) {
218+
// RemoveContext removes a context reference and returns it if context was removed successfully.
219+
func (l *LocalConfig) RemoveContext(name string) (ContextRef, bool) {
220220
for i, c := range l.Contexts {
221-
if c.Name == serverName {
221+
if c.Name == name {
222222
l.Contexts = append(l.Contexts[:i], l.Contexts[i+1:]...)
223-
return c.Server, true
223+
return c, true
224224
}
225225
}
226-
return "", false
226+
return ContextRef{}, false
227227
}
228228

229229
// RemoveToken and returns true if user was removed successfully

0 commit comments

Comments
 (0)