Skip to content

feat: support sqlite credential helper #857

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 6 commits into from
Sep 26, 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
43 changes: 31 additions & 12 deletions pkg/config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@ import (
"github.com/docker/cli/cli/config/types"
)

const (
WincredCredHelper = "wincred"
OsxkeychainCredHelper = "osxkeychain"
SecretserviceCredHelper = "secretservice"
PassCredHelper = "pass"
FileCredHelper = "file"
SqliteCredHelper = "sqlite"

GPTScriptHelperPrefix = "gptscript-credential-"
)

var (
darwinHelpers = []string{"osxkeychain", "file"}
windowsHelpers = []string{"wincred", "file"}
linuxHelpers = []string{"secretservice", "pass", "file"}
darwinHelpers = []string{OsxkeychainCredHelper, FileCredHelper, SqliteCredHelper}
windowsHelpers = []string{WincredCredHelper, FileCredHelper}
linuxHelpers = []string{SecretserviceCredHelper, PassCredHelper, FileCredHelper, SqliteCredHelper}
)

const GPTScriptHelperPrefix = "gptscript-credential-"
func listAsString(helpers []string) string {
if len(helpers) == 0 {
return ""
} else if len(helpers) == 1 {
return helpers[0]
}

return strings.Join(helpers[:len(helpers)-1], ", ") + " or " + helpers[len(helpers)-1]
}

type AuthConfig types.AuthConfig

Expand Down Expand Up @@ -150,13 +169,13 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
errMsg := fmt.Sprintf("invalid credential store '%s'", result.CredentialsStore)
switch runtime.GOOS {
case "darwin":
errMsg += " (use 'osxkeychain' or 'file')"
errMsg += fmt.Sprintf(" (use %s)", listAsString(darwinHelpers))
case "windows":
errMsg += " (use 'wincred' or 'file')"
errMsg += fmt.Sprintf(" (use %s)", listAsString(windowsHelpers))
case "linux":
errMsg += " (use 'secretservice', 'pass', or 'file')"
errMsg += fmt.Sprintf(" (use %s)", listAsString(linuxHelpers))
default:
errMsg += " (use 'file')"
errMsg += " (use file)"
}
errMsg += fmt.Sprintf("\nPlease edit your config file at %s to fix this.", result.location)

Expand All @@ -169,11 +188,11 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
func (c *CLIConfig) setDefaultCredentialsStore() error {
switch runtime.GOOS {
case "darwin":
c.CredentialsStore = "osxkeychain"
c.CredentialsStore = OsxkeychainCredHelper
case "windows":
c.CredentialsStore = "wincred"
c.CredentialsStore = WincredCredHelper
default:
c.CredentialsStore = "file"
c.CredentialsStore = FileCredHelper
}
return c.Save()
}
Expand All @@ -187,7 +206,7 @@ func isValidCredentialHelper(helper string) bool {
case "linux":
return slices.Contains(linuxHelpers, helper)
default:
return helper == "file"
return helper == FileCredHelper
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewStore(cfg *config.CLIConfig, credentialBuilder CredentialBuilder, credCt
return Store{
credCtxs: credCtxs,
credBuilder: credentialBuilder,
credHelperDirs: GetCredentialHelperDirs(cacheDir),
credHelperDirs: GetCredentialHelperDirs(cacheDir, cfg.CredentialsStore),
cfg: cfg,
}, nil
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (s *Store) getStore(ctx context.Context) (credentials.Store, error) {
}

func (s *Store) getStoreByHelper(ctx context.Context, helper string) (credentials.Store, error) {
if helper == "" || helper == config.GPTScriptHelperPrefix+"file" {
if helper == "" || helper == config.GPTScriptHelperPrefix+config.FileCredHelper {
return credentials.NewFileStore(s.cfg), nil
}

Expand Down
33 changes: 29 additions & 4 deletions pkg/credentials/util.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
package credentials

import (
"fmt"
"path/filepath"

"github.com/gptscript-ai/gptscript/pkg/config"
runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
)

type CredentialHelperDirs struct {
RevisionFile, LastCheckedFile, BinDir string
}

func GetCredentialHelperDirs(cacheDir string) CredentialHelperDirs {
func RepoNameForCredentialStore(store string) string {
switch store {
case config.SqliteCredHelper:
return "gptscript-credential-sqlite"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: gptscript-credential-sqlite and gptscript-credential-helpers shows up multiple and might be a good idea to define those as const.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might change this at a later point. Don't want to go through the process of getting approvals on this one again just for that change

default:
return "gptscript-credential-helpers"
}
}

func GitURLForRepoName(repoName string) (string, error) {
switch repoName {
case "gptscript-credential-sqlite":
return runtimeEnv.VarOrDefault("GPTSCRIPT_CRED_SQLITE_ROOT", "https://github.com/gptscript-ai/gptscript-credential-sqlite.git"), nil
case "gptscript-credential-helpers":
return runtimeEnv.VarOrDefault("GPTSCRIPT_CRED_HELPERS_ROOT", "https://github.com/gptscript-ai/gptscript-credential-helpers.git"), nil
default:
return "", fmt.Errorf("unknown repo name: %s", repoName)
}
}

func GetCredentialHelperDirs(cacheDir, store string) CredentialHelperDirs {
repoName := RepoNameForCredentialStore(store)
return CredentialHelperDirs{
RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"),
LastCheckedFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "last-checked"),
BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"),
RevisionFile: filepath.Join(cacheDir, "repos", repoName, "revision"),
LastCheckedFile: filepath.Join(cacheDir, "repos", repoName, "last-checked"),
BinDir: filepath.Join(cacheDir, "repos", repoName, "bin"),
}
}

Expand Down
50 changes: 29 additions & 21 deletions pkg/repos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/BurntSushi/locker"
"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/credentials"
runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
"github.com/gptscript-ai/gptscript/pkg/hash"
"github.com/gptscript-ai/gptscript/pkg/repos/git"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
Expand Down Expand Up @@ -55,10 +54,10 @@ func (n noopRuntime) Setup(_ context.Context, _ types.Tool, _, _ string, _ []str
}

type Manager struct {
cacheDir string
storageDir string
gitDir string
runtimeDir string
credHelperDirs credentials.CredentialHelperDirs
runtimes []Runtime
credHelperConfig *credHelperConfig
}
Expand All @@ -72,11 +71,11 @@ type credHelperConfig struct {
func New(cacheDir string, runtimes ...Runtime) *Manager {
root := filepath.Join(cacheDir, "repos")
return &Manager{
storageDir: root,
gitDir: filepath.Join(root, "git"),
runtimeDir: filepath.Join(root, "runtimes"),
credHelperDirs: credentials.GetCredentialHelperDirs(cacheDir),
runtimes: runtimes,
cacheDir: cacheDir,
storageDir: root,
gitDir: filepath.Join(root, "git"),
runtimeDir: filepath.Join(root, "runtimes"),
runtimes: runtimes,
}
}

Expand Down Expand Up @@ -110,50 +109,59 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
distInfo, suffix string
)
// The file helper is built-in and does not need to be downloaded.
if helperName == "file" {
if helperName == config.FileCredHelper {
return nil
}
switch helperName {
case "wincred":
case config.WincredCredHelper:
suffix = ".exe"
default:
distInfo = fmt.Sprintf("-%s-%s", runtime.GOOS, runtime.GOARCH)
}

locker.Lock("gptscript-credential-helpers")
defer locker.Unlock("gptscript-credential-helpers")
repoName := credentials.RepoNameForCredentialStore(helperName)

locker.Lock(repoName)
defer locker.Unlock(repoName)

credHelperDirs := credentials.GetCredentialHelperDirs(m.cacheDir, helperName)

// Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
now := time.Now()
lastChecked, err := os.ReadFile(m.credHelperDirs.LastCheckedFile)
lastChecked, err := os.ReadFile(credHelperDirs.LastCheckedFile)
if err == nil {
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && now.Sub(t) < 24*time.Hour {
// Make sure the binary still exists, and if it does, return.
if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
if _, err := os.Stat(filepath.Join(credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, t.Add(24*time.Hour))
return nil
}
}
}

if err := os.MkdirAll(filepath.Dir(m.credHelperDirs.LastCheckedFile), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(credHelperDirs.LastCheckedFile), 0755); err != nil {
return err
}

// Update the last-checked file.
if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil {
if err := os.WriteFile(credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil {
return err
}

gitURL, err := credentials.GitURLForRepoName(repoName)
if err != nil {
return err
}

tool := types.Tool{
ToolDef: types.ToolDef{
Parameters: types.Parameters{
Name: "gptscript-credential-helpers",
Name: repoName,
},
},
Source: types.ToolSource{
Repo: &types.Repo{
Root: runtimeEnv.VarOrDefault("GPTSCRIPT_CRED_HELPERS_ROOT", "https://github.com/gptscript-ai/gptscript-credential-helpers.git"),
Root: gitURL,
},
},
}
Expand All @@ -164,12 +172,12 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co

var needsDownloaded bool
// Check the last revision shasum and see if it is different from the current one.
lastRevision, err := os.ReadFile(m.credHelperDirs.RevisionFile)
lastRevision, err := os.ReadFile(credHelperDirs.RevisionFile)
if (err == nil && strings.TrimSpace(string(lastRevision)) != tool.Source.Repo.Root+tag) || errors.Is(err, fs.ErrNotExist) {
// Need to pull the latest version.
needsDownloaded = true
// Update the revision file to the new revision.
if err = os.WriteFile(m.credHelperDirs.RevisionFile, []byte(tool.Source.Repo.Root+tag), 0644); err != nil {
if err = os.WriteFile(credHelperDirs.RevisionFile, []byte(tool.Source.Repo.Root+tag), 0644); err != nil {
return err
}
} else if err != nil {
Expand All @@ -179,15 +187,15 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
if !needsDownloaded {
// Check for the existence of the credential helper binary.
// If it's there, we have no need to download it and can just return.
if _, err = os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
if _, err = os.Stat(filepath.Join(credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
return nil
}
}

// Find the Go runtime and use it to build the credential helper.
for _, rt := range m.runtimes {
if strings.HasPrefix(rt.ID(), "go") {
return rt.(*golang.Runtime).DownloadCredentialHelper(ctx, tool, helperName, distInfo, suffix, m.credHelperDirs.BinDir)
return rt.(*golang.Runtime).DownloadCredentialHelper(ctx, tool, helperName, distInfo, suffix, credHelperDirs.BinDir)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/repos/runtimes/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"runtime"
"strings"

"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/debugcmd"
runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
"github.com/gptscript-ai/gptscript/pkg/hash"
Expand Down Expand Up @@ -286,7 +287,7 @@ func (r *Runtime) Setup(ctx context.Context, _ types.Tool, dataRoot, toolSource
}

func (r *Runtime) DownloadCredentialHelper(ctx context.Context, tool types.Tool, helperName, distInfo, suffix string, binDir string) error {
if helperName == "file" {
if helperName == config.FileCredHelper {
return nil
}

Expand Down