Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Remove creating instance on PersistentPreRun

Maidul Islam3b6b9d41d1a3
diff --git a/cli/packages/cmd/root.go b/cli/packages/cmd/root.go
index 17f78b5d5b..7cd9806fca 100644
--- a/cli/packages/cmd/root.go
+++ b/cli/packages/cmd/root.go
@@ -31,8 +31,6 @@ func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().BoolVarP(&debugLogging, "debug", "d", false, "Enable verbose logging")
rootCmd.PersistentFlags().StringVar(&util.INFISICAL_URL, "domain", "https://app.infisical.com/api", "Point the CLI to your own backend")
-
- rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
- util.InitKeyRingInstance()
- }
+ // rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
+ // }
}
diff --git a/cli/packages/util/credentials.go b/cli/packages/util/credentials.go
index b83227b714..80c98fa9a1 100644
--- a/cli/packages/util/credentials.go
+++ b/cli/packages/util/credentials.go
@@ -19,7 +19,13 @@ func StoreUserCredsInKeyRing(userCred *models.UserCredentials) error {
return fmt.Errorf("StoreUserCredsInKeyRing: something went wrong when marshalling user creds [err=%s]", err)
}
- err = keyringInstance.Set(keyring.Item{
+ // Get keyring
+ configuredKeyring, err := GetKeyRing()
+ if err != nil {
+ return fmt.Errorf("StoreUserCredsInKeyRing: unable to get keyring instance with [err=%s]", err)
+ }
+
+ err = configuredKeyring.Set(keyring.Item{
Key: userCred.Email,
Data: []byte(string(userCredMarshalled)),
})
@@ -32,20 +38,26 @@ func StoreUserCredsInKeyRing(userCred *models.UserCredentials) error {
}
func GetUserCredsFromKeyRing(userEmail string) (credentials models.UserCredentials, err error) {
- credentialsValue, err := keyringInstance.Get(userEmail)
+ // Get keyring
+ configuredKeyring, err := GetKeyRing()
+ if err != nil {
+ return models.UserCredentials{}, fmt.Errorf("GetUserCredsFromKeyRing: unable to get keyring instance with [err=%s]", err)
+ }
+
+ credentialsValue, err := configuredKeyring.Get(userEmail)
if err != nil {
- return models.UserCredentials{}, fmt.Errorf("Unable to get key from Keyring. could not find login credentials in your Keyring. This is common if you have switched vault backend recently. If so, please login in again and retry:", err)
+ return models.UserCredentials{}, fmt.Errorf("GetUserCredsFromKeyRing: unable to get key from Keyring. could not find login credentials in your Keyring. This is common if you have switched vault backend recently. If so, please login in again and retry [err=%s]", err)
}
var userCredentials models.UserCredentials
err = json.Unmarshal([]byte(credentialsValue.Data), &userCredentials)
if err != nil {
- return models.UserCredentials{}, fmt.Errorf("Something went wrong when unmarshalling user creds:", err)
+ return models.UserCredentials{}, fmt.Errorf("getUserCredsFromKeyRing: Something went wrong when unmarshalling user creds [err=%s]", err)
}
if err != nil {
- return models.UserCredentials{}, fmt.Errorf("Unable to store user credentials", err)
+ return models.UserCredentials{}, fmt.Errorf("GetUserCredsFromKeyRing: Unable to store user credentials [err=%s]", err)
}
return userCredentials, err
@@ -82,7 +94,7 @@ func IsUserLoggedIn() (hasUserLoggedIn bool, theUsersEmail string, err error) {
if response.StatusCode() > 299 {
log.Infoln("Login expired, please login again.")
- return false, "", fmt.Errorf("Login expired, please login again.")
+ return false, "", fmt.Errorf("GetUserCredsFromKeyRing: Login expired, please login again.")
}
return true, configFile.LoggedInUserEmail, nil
diff --git a/cli/packages/util/vault.go b/cli/packages/util/vault.go
index 95dba04d8c..939bd4c629 100644
--- a/cli/packages/util/vault.go
+++ b/cli/packages/util/vault.go
@@ -5,14 +5,9 @@ import (
"os"
"github.com/99designs/keyring"
- log "github.com/sirupsen/logrus"
"golang.org/x/term"
)
-// Keyring instance
-var keyringInstance keyring.Keyring
-var keyringInstanceConfig keyring.Config
-
func GetCurrentVaultBackend() (keyring.BackendType, error) {
configFile, err := GetConfigFile()
if err != nil {
@@ -28,13 +23,13 @@ func GetCurrentVaultBackend() (keyring.BackendType, error) {
return configFile.VaultBackendType, nil
}
-func InitKeyRingInstance() {
+func GetKeyRing() (keyring.Keyring, error) {
currentVaultBackend, err := GetCurrentVaultBackend()
if err != nil {
- log.Infof("InitKeyRingInstance: unable to get the current vault backend, [err=%s]", err)
+ return nil, fmt.Errorf("GetKeyRing: unable to get the current vault backend, [err=%s]", err)
}
- keyringInstanceConfig = keyring.Config{
+ keyringInstanceConfig := keyring.Config{
FilePasswordFunc: fileKeyringPassphrasePrompt,
ServiceName: SERVICE_NAME,
LibSecretCollectionName: SERVICE_NAME,
@@ -51,10 +46,12 @@ func InitKeyRingInstance() {
keyringInstanceConfig.AllowedBackends = []keyring.BackendType{keyring.BackendType(currentVaultBackend)}
}
- keyringInstance, err = keyring.Open(keyringInstanceConfig)
+ keyringInstance, err := keyring.Open(keyringInstanceConfig)
if err != nil {
- log.Errorf("InitKeyRingInstance: Unable to create instance of Keyring because of [err=%s]", err)
+ return nil, fmt.Errorf("GetKeyRing: Unable to create instance of Keyring because of [err=%s]", err)
}
+
+ return keyringInstance, nil
}
func fileKeyringPassphrasePrompt(prompt string) (string, error) {