github.com/tommi2day/pwcli@v0.0.0-20240317203041-4d1177a5ab91/cmd/get.go (about) 1 // Package cmd commands 2 package cmd 3 4 import ( 5 "fmt" 6 "os" 7 8 "github.com/tommi2day/gomodules/common" 9 10 "github.com/tommi2day/gomodules/pwlib" 11 12 log "github.com/sirupsen/logrus" 13 14 "github.com/spf13/cobra" 15 ) 16 17 // getCmd represents the get command 18 var getCmd = &cobra.Command{ 19 Use: "get", 20 Short: "Get encrypted password", 21 Long: `Return a password for a an Account on a system/database`, 22 RunE: getpass, 23 SilenceUsage: true, 24 } 25 26 func handleVault(cmd *cobra.Command, account *string, system *string) (err error) { 27 *account, _ = cmd.Flags().GetString("entry") 28 *system, _ = cmd.Flags().GetString("path") 29 log.Debugf("use vault method with path %s and key %s", *system, *account) 30 if *account == "" || *system == "" { 31 err = fmt.Errorf("method vault needs parameter path and entry set") 32 return err 33 } 34 if vaultAddr != "" { 35 _ = os.Setenv("VAULT_ADDR", vaultAddr) 36 } 37 if vaultToken != "" { 38 _ = os.Setenv("VAULT_TOKEN", vaultToken) 39 } 40 return 41 } 42 43 func handleKMS() (err error) { 44 if kmsKeyID == "" { 45 kmsKeyID = common.GetStringEnv("KMS_KEYID", "") 46 log.Debugf("KMS KeyID from environment: '%s'", kmsKeyID) 47 } 48 if kmsKeyID == "" { 49 return fmt.Errorf("need parameter kms_keyid to proceed") 50 } 51 if kmsEndpoint != "" { 52 log.Debugf("use KMS endpoint %s", kmsEndpoint) 53 _ = os.Setenv("KMS_ENDPOINT", kmsEndpoint) 54 } 55 log.Debugf("use KMS method with keyid %s", kmsKeyID) 56 pc.KMSKeyID = kmsKeyID 57 return nil 58 } 59 func getpass(cmd *cobra.Command, _ []string) error { 60 var system string 61 var account string 62 var password string 63 var err error 64 log.Debugf("Get password called, method %s", method) 65 system, _ = cmd.Flags().GetString("system") 66 if system == "" { 67 system, _ = cmd.Flags().GetString("db") 68 } 69 account, _ = cmd.Flags().GetString("user") 70 switch method { 71 case typeVault: 72 err = handleVault(cmd, &account, &system) 73 case typeKMS: 74 err = handleKMS() 75 } 76 if err != nil { 77 return err 78 } 79 if account == "" { 80 err = fmt.Errorf("need parameter user to proceed") 81 return err 82 } 83 kp, _ := cmd.Flags().GetString("keypass") 84 if kp != "" { 85 pc.KeyPass = kp 86 log.Debugf("use alternate password: %s", kp) 87 } 88 pwlib.SilentCheck = false 89 password, err = pc.GetPassword(system, account) 90 if err == nil { 91 fmt.Println(password) 92 log.Infof("Found matching entry: '%s'", password) 93 } 94 return err 95 } 96 97 func init() { 98 RootCmd.AddCommand(getCmd) 99 getCmd.Flags().StringP("system", "s", "", "name of the system/database") 100 getCmd.Flags().StringP("db", "d", "", "name of the system/database") 101 getCmd.Flags().StringP("user", "u", "", "account/user name") 102 getCmd.Flags().StringP("keypass", "p", "", "password for the private key") 103 getCmd.Flags().StringP("path", "P", "", "vault path to the secret, eg /secret/data/... within method vault, use together with path") 104 getCmd.Flags().StringP("entry", "E", "", "vault secret entry key within method vault, use together with path") 105 getCmd.Flags().StringVar(&vaultAddr, "vault_addr", vaultAddr, "VAULT_ADDR Url") 106 getCmd.Flags().StringVar(&vaultToken, "vault_token", vaultToken, "VAULT_TOKEN") 107 getCmd.Flags().StringVar(&kmsKeyID, "kms_keyid", kmsKeyID, "KMS KeyID") 108 getCmd.Flags().StringVar(&kmsEndpoint, "kms_endpoint", kmsEndpoint, "KMS Endpoint Url") 109 }