Skip to content

Fix server creds #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/cli/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

cmd2 "github.com/gptscript-ai/cmd"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/gptscript-ai/gptscript/pkg/gptscript"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -44,7 +45,12 @@ func (c *Credential) Run(cmd *cobra.Command, _ []string) error {
}
defer gptScript.Close(true)

store, err := gptScript.CredentialStoreFactory.NewStore(gptScript.DefaultCredentialContexts)
credCtxs := gptScript.DefaultCredentialContexts
if c.AllContexts {
credCtxs = []string{credentials.AllCredentialContexts}
}

store, err := gptScript.CredentialStoreFactory.NewStore(credCtxs)
if err != nil {
return err
}
Expand Down
47 changes: 29 additions & 18 deletions pkg/config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/adrg/xdg"
"github.com/docker/cli/cli/config/types"
"github.com/gptscript-ai/gptscript/pkg/mvl"
)

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

type AuthConfig types.AuthConfig
Expand Down Expand Up @@ -85,9 +87,9 @@ func (c *CLIConfig) Save() error {
}

if c.auths != nil {
c.Auths = map[string]AuthConfig{}
c.Auths = make(map[string]AuthConfig, len(c.auths))
for k, v := range c.auths {
c.Auths[k] = (AuthConfig)(v)
c.Auths[k] = AuthConfig(v)
}
c.auths = nil
}
Expand Down Expand Up @@ -116,13 +118,21 @@ func (c *CLIConfig) GetAuthConfigs() map[string]types.AuthConfig {
defer c.authsLock.Unlock()
}

if err := c.readFileIntoConfig(c.location); err != nil {
// This is implementing an interface, so we can't return this error.
log.Warnf("Failed to read config file: %v", err)
}

if c.auths == nil {
c.auths = map[string]types.AuthConfig{}
for k, v := range c.Auths {
authConfig := (types.AuthConfig)(v)
c.auths[k] = authConfig
}
c.auths = make(map[string]types.AuthConfig, len(c.Auths))
}

// Assume that whatever was pulled from the file is more recent.
// The docker creds framework will save the file after creating or updating a credential.
for k, v := range c.Auths {
c.auths[k] = types.AuthConfig(v)
}

return c.auths
}

Expand All @@ -142,17 +152,13 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
}
}

data, err := readFile(gptscriptConfigFile)
if err != nil {
return nil, err
}
result := &CLIConfig{
authsLock: &sync.Mutex{},
location: gptscriptConfigFile,
raw: data,
}
if err := json.Unmarshal(data, result); err != nil {
return nil, fmt.Errorf("failed to unmarshal %s: %v", gptscriptConfigFile, err)

if err := result.readFileIntoConfig(gptscriptConfigFile); err != nil {
return nil, err
}

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

func readFile(path string) ([]byte, error) {
func (c *CLIConfig) readFileIntoConfig(path string) error {
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return []byte("{}"), nil
return nil
} else if err != nil {
return nil, fmt.Errorf("failed to read user config %s: %w", path, err)
return fmt.Errorf("failed to read user config %s: %w", path, err)
}

return data, nil
c.raw = data
if err := json.Unmarshal(data, c); err != nil {
return fmt.Errorf("failed to unmarshal %s: %v", path, err)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s Store) List(_ context.Context) ([]Credential, error) {
}

if len(s.credCtxs) > 0 && s.credCtxs[0] == AllCredentialContexts {
allCreds := make([]Credential, len(list))
allCreds := make([]Credential, 0, len(list))
for serverAddress := range list {
ac, err := store.Get(serverAddress)
if err != nil {
Expand Down