mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-02-20 01:28:45 +01:00
feat(cli): support password prompt input when --password is not provided in bcrypt cli (#23906)
Signed-off-by: gyu-young-park <gyoue200125@gmail.com>
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/argoproj/argo-cd/v3/util/cli"
|
||||
)
|
||||
|
||||
// NewBcryptCmd represents the bcrypt command
|
||||
@@ -15,8 +17,15 @@ func NewBcryptCmd() *cobra.Command {
|
||||
Use: "bcrypt",
|
||||
Short: "Generate bcrypt hash for any password",
|
||||
Example: `# Generate bcrypt hash for any password
|
||||
argocd account bcrypt --password YOUR_PASSWORD`,
|
||||
argocd account bcrypt --password YOUR_PASSWORD
|
||||
|
||||
# Prompt for password input
|
||||
argocd account bcrypt
|
||||
|
||||
# Read password from stdin
|
||||
echo -e "password" | argocd account bcrypt`,
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
password = cli.PromptPassword(password)
|
||||
bytePassword := []byte(password)
|
||||
// Hashing the password
|
||||
hash, err := bcrypt.GenerateFromPassword(bytePassword, bcrypt.DefaultCost)
|
||||
@@ -28,9 +37,5 @@ argocd account bcrypt --password YOUR_PASSWORD`,
|
||||
}
|
||||
|
||||
bcryptCmd.Flags().StringVar(&password, "password", "", "Password for which bcrypt hash is generated")
|
||||
err := bcryptCmd.MarkFlagRequired("password")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return bcryptCmd
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -20,3 +22,27 @@ func TestGeneratePassword(t *testing.T) {
|
||||
err = bcrypt.CompareHashAndPassword(output.Bytes(), []byte("abc"))
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGeneratePasswordWithStdin(t *testing.T) {
|
||||
oldStdin := os.Stdin
|
||||
defer func() {
|
||||
os.Stdin = oldStdin
|
||||
}()
|
||||
|
||||
input := bytes.NewBufferString("abc\n")
|
||||
r, w, _ := os.Pipe()
|
||||
_, _ = w.Write(input.Bytes())
|
||||
w.Close()
|
||||
os.Stdin = r
|
||||
|
||||
bcryptCmd := NewBcryptCmd()
|
||||
bcryptCmd.SetArgs([]string{})
|
||||
output := new(bytes.Buffer)
|
||||
bcryptCmd.SetOut(output)
|
||||
|
||||
err := bcryptCmd.Execute()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = bcrypt.CompareHashAndPassword(output.Bytes(), []byte("abc"))
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,12 @@ argocd account bcrypt [flags]
|
||||
```
|
||||
# Generate bcrypt hash for any password
|
||||
argocd account bcrypt --password YOUR_PASSWORD
|
||||
|
||||
# Prompt for password input
|
||||
argocd account bcrypt
|
||||
|
||||
# Read password from stdin
|
||||
echo -e "password" | argocd account bcrypt
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
Reference in New Issue
Block a user