github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/bcrypt.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/spf13/cobra"
     8  	"golang.org/x/crypto/bcrypt"
     9  
    10  	"github.com/argoproj/argo-cd/v3/util/cli"
    11  )
    12  
    13  // NewBcryptCmd represents the bcrypt command
    14  func NewBcryptCmd() *cobra.Command {
    15  	var password string
    16  	bcryptCmd := &cobra.Command{
    17  		Use:   "bcrypt",
    18  		Short: "Generate bcrypt hash for any password",
    19  		Example: `# Generate bcrypt hash for any password 
    20  argocd account bcrypt --password YOUR_PASSWORD
    21  
    22  # Prompt for password input
    23  argocd account bcrypt
    24  
    25  # Read password from stdin
    26  echo -e "password" | argocd account bcrypt`,
    27  		Run: func(cmd *cobra.Command, _ []string) {
    28  			password = cli.PromptPassword(password)
    29  			bytePassword := []byte(password)
    30  			// Hashing the password
    31  			hash, err := bcrypt.GenerateFromPassword(bytePassword, bcrypt.DefaultCost)
    32  			if err != nil {
    33  				log.Fatalf("Failed to generate bcrypt hash: %v", err)
    34  			}
    35  			fmt.Fprint(cmd.OutOrStdout(), string(hash))
    36  		},
    37  	}
    38  
    39  	bcryptCmd.Flags().StringVar(&password, "password", "", "Password for which bcrypt hash is generated")
    40  	return bcryptCmd
    41  }