mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-05 11:26:33 +00:00
removeed vault use command and auto generated passphrase
This commit is contained in:
+37
-87
@@ -31,37 +31,52 @@ var AvailableVaults = []VaultBackendType{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var vaultSetCmd = &cobra.Command{
|
var vaultSetCmd = &cobra.Command{
|
||||||
Example: `infisical vault set file --passphrase <your-passphrase>`,
|
Example: `infisical vault set file`,
|
||||||
Use: "set [file|auto] [flags]",
|
Use: "set [file|auto]",
|
||||||
Short: "Used to configure the vault backends",
|
Short: "Used to configure the vault backends",
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
wantedVaultTypeName := args[0]
|
||||||
vaultType := args[0]
|
currentVaultBackend, err := util.GetCurrentVaultBackend()
|
||||||
|
|
||||||
passphrase, err := cmd.Flags().GetString("passphrase")
|
|
||||||
if err != nil {
|
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)
|
||||||
}
|
|
||||||
|
|
||||||
if vaultType == util.VAULT_BACKEND_FILE_MODE && passphrase != "" {
|
|
||||||
setFileVaultPassphrase(passphrase)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
util.PrintWarning("This command has been deprecated. Please use 'infisical vault use [file|auto]' to select which vault to use.\n")
|
if wantedVaultTypeName == string(currentVaultBackend) {
|
||||||
selectVaultTypeCmd(cmd, args)
|
log.Error().Msgf("You are already on vault backend [%s]", currentVaultBackend)
|
||||||
},
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var vaultUseCmd = &cobra.Command{
|
if wantedVaultTypeName == util.VAULT_BACKEND_AUTO_MODE || wantedVaultTypeName == util.VAULT_BACKEND_FILE_MODE {
|
||||||
Example: `infisical vault use [file|auto]`,
|
configFile, err := util.GetConfigFile()
|
||||||
Use: "use [file|auto]",
|
if err != nil {
|
||||||
Short: "Used to select the the type of vault backend to store sensitive data securely at rest",
|
log.Error().Msgf("Unable to set vault to [%s] because of [err=%s]", wantedVaultTypeName, err)
|
||||||
DisableFlagsInUseLine: true,
|
return
|
||||||
Args: cobra.MinimumNArgs(1),
|
}
|
||||||
Run: selectVaultTypeCmd,
|
|
||||||
|
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, ", "))
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// runCmd represents the run command
|
// runCmd represents the run command
|
||||||
@@ -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() {
|
func printAvailableVaultBackends() {
|
||||||
fmt.Printf("Vaults are used to securely store your login details locally. Available vaults:")
|
fmt.Printf("Vaults are used to securely store your login details locally. Available vaults:")
|
||||||
for _, vaultType := range AvailableVaults {
|
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))
|
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() {
|
func init() {
|
||||||
|
|
||||||
vaultSetCmd.Flags().StringP("passphrase", "p", "", "Set the passphrase for the file vault")
|
|
||||||
|
|
||||||
vaultCmd.AddCommand(vaultSetCmd)
|
vaultCmd.AddCommand(vaultSetCmd)
|
||||||
vaultCmd.AddCommand(vaultUseCmd)
|
|
||||||
|
|
||||||
rootCmd.AddCommand(vaultCmd)
|
rootCmd.AddCommand(vaultCmd)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user