github.com/tommi2day/pwcli@v0.0.0-20240317203041-4d1177a5ab91/cmd/encrypt.go (about)

     1  // Package cmd Commands
     2  package cmd
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  	"github.com/tommi2day/gomodules/common"
    11  )
    12  
    13  // encryptCmd represents the encrypt command
    14  var encryptCmd = &cobra.Command{
    15  	Use:   "encrypt",
    16  	Short: "Encrypt plaintext file",
    17  	Long: `Encrypt a plain file given in -p and saved as crypted fin given by -c flag
    18  default for paintext File is <app>.plain and for crypted file is <app.pw>`,
    19  	RunE:         encrypt,
    20  	SilenceUsage: true,
    21  }
    22  
    23  func encrypt(cmd *cobra.Command, _ []string) error {
    24  	log.Debug("encrypt called")
    25  	// check for plaintext file option
    26  	pfilename, _ := cmd.Flags().GetString("plaintext")
    27  	if pfilename != "" {
    28  		pc.PlainTextFile = pfilename
    29  	}
    30  	log.Debugf("encrypt plaintext file '%s' with method %s", pc.PlainTextFile, pc.Method)
    31  
    32  	// check for crypted file option
    33  	cfilename, _ := cmd.Flags().GetString("crypted")
    34  	if cfilename != "" {
    35  		pc.CryptedFile = cfilename
    36  	}
    37  	log.Debugf("create crypted file '%s'", pc.CryptedFile)
    38  
    39  	// check for keypass file option
    40  	kp, _ := cmd.Flags().GetString("keypass")
    41  	if kp != "" {
    42  		pc.KeyPass = kp
    43  		log.Debugf("use alternate key password '%s'", keypass)
    44  	}
    45  	if method == typeKMS {
    46  		if kmsKeyID == "" {
    47  			kmsKeyID = common.GetStringEnv("KMS_KEYID", "")
    48  			log.Debugf("KMS KeyID from environment: '%s'", kmsKeyID)
    49  		}
    50  		if kmsKeyID == "" {
    51  			return fmt.Errorf("need parameter kms_keyid to proceed")
    52  		}
    53  		if kmsEndpoint != "" {
    54  			log.Debugf("use KMS endpoint %s", kmsEndpoint)
    55  			_ = os.Setenv("KMS_ENDPOINT", kmsEndpoint)
    56  		}
    57  		log.Debugf("use KMS method with keyid %s", kmsKeyID)
    58  		pc.KMSKeyID = kmsKeyID
    59  	}
    60  	// do encrypt with default key
    61  	err := pc.EncryptFile()
    62  	if err == nil {
    63  		log.Infof("crypted file '%s' successfully created", pc.CryptedFile)
    64  		fmt.Println("DONE")
    65  	}
    66  	return err
    67  }
    68  func init() {
    69  	RootCmd.AddCommand(encryptCmd)
    70  	// don't have variables populated here
    71  	encryptCmd.PersistentFlags().StringP("plaintext", "t", "", "alternate plaintext file")
    72  	encryptCmd.PersistentFlags().StringP("crypted", "c", "", "alternate crypted file")
    73  	encryptCmd.Flags().StringP("keypass", "p", "", "dedicated password for the private key")
    74  	encryptCmd.Flags().StringVar(&kmsKeyID, "kms_keyid", kmsKeyID, "KMS KeyID")
    75  	encryptCmd.Flags().StringVar(&kmsEndpoint, "kms_endpoint", kmsEndpoint, "KMS Endpoint Url")
    76  }