Skip to content

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

removeed vault use command and auto generated passphrase

Maidul Islam281d703cc3a0
diff --git a/cli/packages/cmd/vault.go b/cli/packages/cmd/vault.go
index 4720e094e7..6a92ef960e 100644
--- a/cli/packages/cmd/vault.go
+++ b/cli/packages/cmd/vault.go
@@ -31,39 +31,54 @@ var AvailableVaults = []VaultBackendType{
}
var vaultSetCmd = &cobra.Command{
- Example: `infisical vault set file --passphrase <your-passphrase>`,
- Use: "set [file|auto] [flags]",
+ Example: `infisical vault set file`,
+ Use: "set [file|auto]",
Short: "Used to configure the vault backends",
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
-
- vaultType := args[0]
-
- passphrase, err := cmd.Flags().GetString("passphrase")
+ wantedVaultTypeName := args[0]
+ currentVaultBackend, err := util.GetCurrentVaultBackend()
if err != nil {
- util.HandleError(err, "Unable to get passphrase flag")
+ log.Error().Msgf("Unable to set vault to [%s] because of [err=%s]", wantedVaultTypeName, err)
+ return
}
- if vaultType == util.VAULT_BACKEND_FILE_MODE && passphrase != "" {
- setFileVaultPassphrase(passphrase)
+ if wantedVaultTypeName == string(currentVaultBackend) {
+ log.Error().Msgf("You are already on vault backend [%s]", currentVaultBackend)
return
}
- util.PrintWarning("This command has been deprecated. Please use 'infisical vault use [file|auto]' to select which vault to use.\n")
- selectVaultTypeCmd(cmd, args)
+ if wantedVaultTypeName == util.VAULT_BACKEND_AUTO_MODE || wantedVaultTypeName == util.VAULT_BACKEND_FILE_MODE {
+ configFile, err := util.GetConfigFile()
+ if err != nil {
+ log.Error().Msgf("Unable to set vault to [%s] because of [err=%s]", wantedVaultTypeName, err)
+ return
+ }
+
+ configFile.VaultBackendType = wantedVaultTypeName
+ configFile.LoggedInUserEmail = ""
+ configFile.VaultBackendPassphrase = base64.StdEncoding.EncodeToString([]byte(util.GenerateRandomString(10)))
+
+ err = util.WriteConfigFile(&configFile)
+ if err != nil {
+ log.Error().Msgf("Unable to set vault to [%s] because an error occurred when saving the config file [err=%s]", wantedVaultTypeName, err)
+ return
+ }
+
+ fmt.Printf("\nSuccessfully, switched vault backend from [%s] to [%s]. Please login in again to store your login details in the new vault with [infisical login]\n", currentVaultBackend, wantedVaultTypeName)
+
+ Telemetry.CaptureEvent("cli-command:vault set", posthog.NewProperties().Set("currentVault", currentVaultBackend).Set("wantedVault", wantedVaultTypeName).Set("version", util.CLI_VERSION))
+ } else {
+ var availableVaultsNames []string
+ for _, vault := range AvailableVaults {
+ availableVaultsNames = append(availableVaultsNames, vault.Name)
+ }
+ log.Error().Msgf("The requested vault type [%s] is not available on this system. Only the following vault backends are available for you system: %s", wantedVaultTypeName, strings.Join(availableVaultsNames, ", "))
+ }
},
}
-var vaultUseCmd = &cobra.Command{
- Example: `infisical vault use [file|auto]`,
- Use: "use [file|auto]",
- Short: "Used to select the the type of vault backend to store sensitive data securely at rest",
- DisableFlagsInUseLine: true,
- Args: cobra.MinimumNArgs(1),
- Run: selectVaultTypeCmd,
-}
-
// runCmd represents the run command
var vaultCmd = &cobra.Command{
Use: "vault",
@@ -75,26 +90,6 @@ var vaultCmd = &cobra.Command{
},
}
-func setFileVaultPassphrase(passphrase string) {
- configFile, err := util.GetConfigFile()
- if err != nil {
- log.Error().Msgf("Unable to set passphrase for file vault because of [err=%s]", err)
- return
- }
-
- // encode with base64
- encodedPassphrase := base64.StdEncoding.EncodeToString([]byte(passphrase))
- configFile.VaultBackendPassphrase = encodedPassphrase
-
- err = util.WriteConfigFile(&configFile)
- if err != nil {
- log.Error().Msgf("Unable to set passphrase for file vault because of [err=%s]", err)
- return
- }
-
- util.PrintSuccessMessage("\nSuccessfully, set passphrase for file vault.\n")
-}
-
func printAvailableVaultBackends() {
fmt.Printf("Vaults are used to securely store your login details locally. Available vaults:")
for _, vaultType := range AvailableVaults {
@@ -111,53 +106,8 @@ func printAvailableVaultBackends() {
fmt.Printf("\n\nYou are currently using [%s] vault to store your login credentials\n", string(currentVaultBackend))
}
-func selectVaultTypeCmd(cmd *cobra.Command, args []string) {
- wantedVaultTypeName := args[0]
- currentVaultBackend, err := util.GetCurrentVaultBackend()
- if err != nil {
- log.Error().Msgf("Unable to set vault to [%s] because of [err=%s]", wantedVaultTypeName, err)
- return
- }
-
- if wantedVaultTypeName == string(currentVaultBackend) {
- log.Error().Msgf("You are already on vault backend [%s]", currentVaultBackend)
- return
- }
-
- if wantedVaultTypeName == util.VAULT_BACKEND_AUTO_MODE || wantedVaultTypeName == util.VAULT_BACKEND_FILE_MODE {
- configFile, err := util.GetConfigFile()
- if err != nil {
- log.Error().Msgf("Unable to set vault to [%s] because of [err=%s]", wantedVaultTypeName, err)
- return
- }
-
- configFile.VaultBackendType = wantedVaultTypeName // save selected vault
- configFile.LoggedInUserEmail = "" // reset the logged in user to prompt them to re login
-
- err = util.WriteConfigFile(&configFile)
- if err != nil {
- log.Error().Msgf("Unable to set vault to [%s] because an error occurred when saving the config file [err=%s]", wantedVaultTypeName, err)
- return
- }
-
- fmt.Printf("\nSuccessfully, switched vault backend from [%s] to [%s]. Please login in again to store your login details in the new vault with [infisical login]\n", currentVaultBackend, wantedVaultTypeName)
-
- Telemetry.CaptureEvent("cli-command:vault set", posthog.NewProperties().Set("currentVault", currentVaultBackend).Set("wantedVault", wantedVaultTypeName).Set("version", util.CLI_VERSION))
- } else {
- var availableVaultsNames []string
- for _, vault := range AvailableVaults {
- availableVaultsNames = append(availableVaultsNames, vault.Name)
- }
- log.Error().Msgf("The requested vault type [%s] is not available on this system. Only the following vault backends are available for you system: %s", wantedVaultTypeName, strings.Join(availableVaultsNames, ", "))
- }
-}
-
func init() {
-
- vaultSetCmd.Flags().StringP("passphrase", "p", "", "Set the passphrase for the file vault")
-
vaultCmd.AddCommand(vaultSetCmd)
- vaultCmd.AddCommand(vaultUseCmd)
rootCmd.AddCommand(vaultCmd)
}