-
-
Notifications
You must be signed in to change notification settings - Fork 134
feat(credentials): add secure credential storage for API keys #263
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "slices" | ||
| "sort" | ||
|
|
||
| "github.com/appleboy/CodeGPT/util" | ||
|
|
||
| "github.com/fatih/color" | ||
| "github.com/rodaine/table" | ||
| "github.com/spf13/cobra" | ||
|
|
@@ -73,9 +76,24 @@ var configListCmd = &cobra.Command{ | |
|
|
||
| // Add the key and value to the table | ||
| for _, v := range keys { | ||
| // Hide the api key | ||
| if v == "openai.api_key" || v == "gemini.api_key" { | ||
| tbl.AddRow(v, "****************") | ||
| if slices.Contains(sensitiveConfigKeys, v) { | ||
| cred, err := util.GetCredential(v) | ||
| if err != nil { | ||
| tbl.AddRow(v, "(error reading secure store)") | ||
| continue | ||
| } | ||
| switch { | ||
| case cred != "": | ||
| if util.CredStoreIsKeyring() { | ||
| tbl.AddRow(v, "(stored in keyring)") | ||
| } else { | ||
|
Comment on lines
+79
to
+89
|
||
| tbl.AddRow(v, "(stored in secure file)") | ||
| } | ||
| case viper.InConfig(v): | ||
| tbl.AddRow(v, "**************** (YAML — run config set to migrate)") | ||
| default: | ||
| tbl.AddRow(v, "(not set)") | ||
| } | ||
| continue | ||
| } | ||
| tbl.AddRow(v, viper.Get(v)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/go-authgate/sdk-go/credstore" | ||
| ) | ||
|
|
||
| const credServiceName = "codegpt" | ||
|
|
||
| // credStore is the singleton SecureStore[string] instance. | ||
| // Initialized once; uses OS keyring with file-based fallback. | ||
| var credStore *credstore.SecureStore[string] | ||
|
|
||
| func init() { | ||
| home, err := os.UserHomeDir() | ||
| var fallbackPath string | ||
| if err != nil || home == "" { | ||
| fallbackPath = filepath.Join(os.TempDir(), "codegpt", "credentials.json") | ||
| } else { | ||
| fallbackPath = filepath.Join(home, ".config", "codegpt", ".cache", "credentials.json") | ||
| } | ||
|
|
||
| // Ensure the directory for the fallback credential file exists. | ||
| dir := filepath.Dir(fallbackPath) | ||
| _ = os.MkdirAll(dir, 0o700) | ||
|
|
||
| keyring := credstore.NewStringKeyringStore(credServiceName) | ||
| file := credstore.NewStringFileStore(fallbackPath) | ||
| credStore = credstore.NewSecureStore(keyring, file) | ||
| } | ||
|
|
||
| // GetCredential retrieves a stored credential by key. | ||
| // Returns ("", nil) if not found. | ||
| func GetCredential(key string) (string, error) { | ||
| val, err := credStore.Load(key) | ||
| if err == credstore.ErrNotFound { | ||
| return "", nil | ||
| } | ||
| return val, err | ||
| } | ||
|
|
||
| // SetCredential stores a credential by key. | ||
| func SetCredential(key, value string) error { | ||
| return credStore.Save(key, value) | ||
| } | ||
|
|
||
| // DeleteCredential removes a credential by key. | ||
| func DeleteCredential(key string) error { | ||
| return credStore.Delete(key) | ||
| } | ||
|
|
||
| // CredStoreIsKeyring reports whether the active backend is the OS keyring. | ||
| func CredStoreIsKeyring() bool { | ||
| return credStore.UseKeyring() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
migrateCredentialsToStore() calls viper.GetString(key), which includes values from env vars due to viper.AutomaticEnv(). This means running any command with OPENAI_API_KEY/GEMINI_API_KEY set can silently persist that secret into the credential store and also write to the config file (side-effect in CI/headless too). Migration should only occur for values actually present in the config file (e.g., gate on viper.InConfig(key) or read the YAML directly) and avoid writing when the value source is env/flags.