Skip to content

Commit 82f65b4

Browse files
committed
fix: remove old credentials from when removing tools
When removing the last tool that uses a credential, this change will remove the old credential. The effect is that the user will have to authenticate the tool again if a tool requiring that credential is added. Signed-off-by: Donnie Adams <[email protected]>
1 parent d995334 commit 82f65b4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/controller/handlers/toolinfo/toolinfo.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package toolinfo
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/gptscript-ai/go-gptscript"
@@ -87,6 +88,34 @@ func (h *Handler) SetToolInfoStatus(req router.Request, resp router.Response) (e
8788
return nil
8889
}
8990

91+
func (h *Handler) RemoveUnneededCredentials(req router.Request, _ router.Response) error {
92+
creds, err := h.gptscript.ListCredentials(req.Ctx, gptscript.ListCredentialsOptions{
93+
CredentialContexts: []string{req.Object.GetName()},
94+
})
95+
if err != nil || len(creds) == 0 {
96+
return err
97+
}
98+
99+
toolInfos := req.Object.(v1.ToolUser).GetToolInfos()
100+
credentialNames := make(map[string]struct{}, len(toolInfos))
101+
102+
for _, cred := range toolInfos {
103+
for _, name := range cred.CredentialNames {
104+
credentialNames[name] = struct{}{}
105+
}
106+
}
107+
108+
for _, cred := range creds {
109+
if _, ok := credentialNames[cred.ToolName]; !ok {
110+
if err := h.gptscript.DeleteCredential(req.Ctx, req.Object.GetName(), cred.ToolName); err != nil && !errors.As(err, &gptscript.ErrNotFound{}) {
111+
return err
112+
}
113+
}
114+
}
115+
116+
return nil
117+
}
118+
90119
func (h *Handler) credentialNamesForNonToolReferences(ctx context.Context, name string) ([]string, error) {
91120
prg, err := h.gptscript.LoadFile(ctx, name)
92121
if err != nil {

pkg/controller/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (c *Controller) setupRoutes() error {
7474
root.Type(&v1.Workflow{}).HandlerFunc(cleanup.Cleanup)
7575
root.Type(&v1.Workflow{}).HandlerFunc(alias.AssignAlias)
7676
root.Type(&v1.Workflow{}).HandlerFunc(toolInfo.SetToolInfoStatus)
77+
root.Type(&v1.Workflow{}).HandlerFunc(toolInfo.RemoveUnneededCredentials)
7778
root.Type(&v1.Workflow{}).HandlerFunc(generationed.UpdateObservedGeneration)
7879
root.Type(&v1.Workflow{}).FinalizeFunc(v1.WorkflowFinalizer, credentialCleanup.Remove)
7980

@@ -87,6 +88,7 @@ func (c *Controller) setupRoutes() error {
8788
root.Type(&v1.Agent{}).HandlerFunc(agents.BackPopulateAuthStatus)
8889
root.Type(&v1.Agent{}).HandlerFunc(alias.AssignAlias)
8990
root.Type(&v1.Agent{}).HandlerFunc(toolInfo.SetToolInfoStatus)
91+
root.Type(&v1.Agent{}).HandlerFunc(toolInfo.RemoveUnneededCredentials)
9092
root.Type(&v1.Agent{}).HandlerFunc(generationed.UpdateObservedGeneration)
9193
root.Type(&v1.Agent{}).FinalizeFunc(v1.AgentFinalizer, credentialCleanup.Remove)
9294

0 commit comments

Comments
 (0)