Skip to content

Commit e5fe428

Browse files
committed
fix: reload CLI config when getting credentials
When running in SDK server mode and using the file cred store, we must reload the credentials in the file each time. Typically, these credentials will be added via a tool call, which would invalidate the credentials held in memory. Signed-off-by: Donnie Adams <[email protected]>
1 parent 33c2995 commit e5fe428

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

pkg/config/cliconfig.go

+29-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/adrg/xdg"
1313
"github.com/docker/cli/cli/config/types"
14+
"github.com/gptscript-ai/gptscript/pkg/mvl"
1415
)
1516

1617
const (
@@ -24,6 +25,7 @@ const (
2425
var (
2526
// Helpers is a list of all supported credential helpers from github.com/gptscript-ai/gptscript-credential-helpers
2627
Helpers = []string{WincredCredHelper, OsxkeychainCredHelper, SecretserviceCredHelper, PassCredHelper}
28+
log = mvl.Package()
2729
)
2830

2931
type AuthConfig types.AuthConfig
@@ -85,9 +87,9 @@ func (c *CLIConfig) Save() error {
8587
}
8688

8789
if c.auths != nil {
88-
c.Auths = map[string]AuthConfig{}
90+
c.Auths = make(map[string]AuthConfig, len(c.auths))
8991
for k, v := range c.auths {
90-
c.Auths[k] = (AuthConfig)(v)
92+
c.Auths[k] = AuthConfig(v)
9193
}
9294
c.auths = nil
9395
}
@@ -116,13 +118,21 @@ func (c *CLIConfig) GetAuthConfigs() map[string]types.AuthConfig {
116118
defer c.authsLock.Unlock()
117119
}
118120

121+
if err := c.readFileIntoConfig(c.location); err != nil {
122+
// This is implementing an interface, so we can't return this error.
123+
log.Warnf("Failed to read config file: %v", err)
124+
}
125+
119126
if c.auths == nil {
120-
c.auths = map[string]types.AuthConfig{}
121-
for k, v := range c.Auths {
122-
authConfig := (types.AuthConfig)(v)
123-
c.auths[k] = authConfig
124-
}
127+
c.auths = make(map[string]types.AuthConfig, len(c.Auths))
128+
}
129+
130+
// Assume that whatever was pulled from the file is more recent.
131+
// The docker creds framework will save the file after creating or updating a credential.
132+
for k, v := range c.Auths {
133+
c.auths[k] = types.AuthConfig(v)
125134
}
135+
126136
return c.auths
127137
}
128138

@@ -142,17 +152,13 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
142152
}
143153
}
144154

145-
data, err := readFile(gptscriptConfigFile)
146-
if err != nil {
147-
return nil, err
148-
}
149155
result := &CLIConfig{
150156
authsLock: &sync.Mutex{},
151157
location: gptscriptConfigFile,
152-
raw: data,
153158
}
154-
if err := json.Unmarshal(data, result); err != nil {
155-
return nil, fmt.Errorf("failed to unmarshal %s: %v", gptscriptConfigFile, err)
159+
160+
if err := result.readFileIntoConfig(gptscriptConfigFile); err != nil {
161+
return nil, err
156162
}
157163

158164
if store := os.Getenv("GPTSCRIPT_CREDENTIAL_STORE"); store != "" {
@@ -180,13 +186,18 @@ func (c *CLIConfig) setDefaultCredentialsStore() error {
180186
return c.Save()
181187
}
182188

183-
func readFile(path string) ([]byte, error) {
189+
func (c *CLIConfig) readFileIntoConfig(path string) error {
184190
data, err := os.ReadFile(path)
185191
if os.IsNotExist(err) {
186-
return []byte("{}"), nil
192+
return nil
187193
} else if err != nil {
188-
return nil, fmt.Errorf("failed to read user config %s: %w", path, err)
194+
return fmt.Errorf("failed to read user config %s: %w", path, err)
189195
}
190196

191-
return data, nil
197+
c.raw = data
198+
if err := json.Unmarshal(data, c); err != nil {
199+
return fmt.Errorf("failed to unmarshal %s: %v", path, err)
200+
}
201+
202+
return nil
192203
}

0 commit comments

Comments
 (0)