2023-03-22 19:41:10 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
var ErrVaultNotInitiated = errors.New("vault is not initiated set either vault-passphrase or vault-passphrase-file")
|
2023-03-22 19:41:10 +00:00
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
func VaultCommand(cmder VaultCommander) *cobra.Command {
|
|
|
|
var initCfg VaultInitConfig
|
|
|
|
|
|
|
|
vaultCmd := &cobra.Command{
|
2023-03-22 19:41:10 +00:00
|
|
|
Use: "vault",
|
|
|
|
Short: "Interact with the buildr vault",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
}
|
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
initVaultCmd := &cobra.Command{
|
2023-04-25 14:14:28 +00:00
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize vault - create an empty vault and a key file",
|
|
|
|
Long: `Creates an empty vault file and bootstraps a random passphrase
|
|
|
|
which will be written either to the configured --vault-passphrase-file or to the default .buildr/.vaultpw file`,
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
Args: cobra.NoArgs,
|
2023-05-01 08:15:53 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
2023-06-22 16:06:56 +00:00
|
|
|
return cmder.Init(cmd.Context(), initCfg)
|
2023-05-01 08:15:53 +00:00
|
|
|
},
|
2023-04-25 14:14:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
getVaultCmd := &cobra.Command{
|
2023-03-22 19:41:10 +00:00
|
|
|
Use: "get",
|
|
|
|
Short: "Get value from vault",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
Args: cobra.ExactArgs(1),
|
2023-06-22 16:06:56 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cmder.Get(cmd.Context(), args[0], os.Stdout)
|
2023-05-01 08:15:53 +00:00
|
|
|
},
|
2023-03-22 19:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
listVaultCmd := &cobra.Command{
|
2023-03-22 19:41:10 +00:00
|
|
|
Use: "list",
|
|
|
|
Short: "List all vault entries - no decrypted values",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
Args: cobra.NoArgs,
|
2023-06-22 16:06:56 +00:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return cmder.List(cmd.Context(), os.Stdout)
|
2023-05-01 08:15:53 +00:00
|
|
|
},
|
2023-03-22 19:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:06:56 +00:00
|
|
|
const argsWithVaultValue = 2
|
2023-05-01 08:15:53 +00:00
|
|
|
setVaultCmd := &cobra.Command{
|
2023-03-22 19:41:10 +00:00
|
|
|
Use: "set",
|
|
|
|
Short: "Set a vault value",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
2023-06-22 16:06:56 +00:00
|
|
|
Args: cobra.RangeArgs(1, argsWithVaultValue),
|
2023-05-01 08:15:53 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 1 {
|
|
|
|
inBuf := bytes.NewBuffer(nil)
|
|
|
|
if _, err := io.Copy(inBuf, os.Stdin); err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:06:56 +00:00
|
|
|
return cmder.Set(cmd.Context(), args[0], inBuf.Bytes())
|
2023-05-01 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:06:56 +00:00
|
|
|
return cmder.Set(cmd.Context(), args[0], []byte(args[1]))
|
2023-05-01 08:15:53 +00:00
|
|
|
},
|
2023-03-22 19:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
rmVaultCmd := &cobra.Command{
|
2023-03-22 19:41:10 +00:00
|
|
|
Use: "rm",
|
|
|
|
Short: "Remove value from vault",
|
|
|
|
Aliases: []string{"del"},
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
Args: cobra.ExactArgs(1),
|
2023-06-22 16:06:56 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cmder.Remove(cmd.Context(), args[0])
|
2023-05-01 08:15:53 +00:00
|
|
|
},
|
2023-04-25 14:14:28 +00:00
|
|
|
}
|
2023-05-01 08:15:53 +00:00
|
|
|
initVaultCmd.Flags().AddGoFlagSet(initCfg.Flags())
|
2023-04-25 14:14:28 +00:00
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
vaultCmd.AddCommand(initVaultCmd, listVaultCmd, getVaultCmd, setVaultCmd, rmVaultCmd)
|
2023-04-25 16:26:48 +00:00
|
|
|
|
2023-05-01 08:15:53 +00:00
|
|
|
return vaultCmd
|
2023-04-25 16:26:48 +00:00
|
|
|
}
|