Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: Add support in hash-password for reading from terminals/stdin (#3365) #3373

Merged
merged 5 commits into from
May 11, 2020
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions modules/caddyhttp/caddyauth/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,34 @@
package caddyauth

import (
"bufio"
"bytes"
"encoding/base64"
"flag"
"fmt"
"os"

"github.com/caddyserver/caddy/v2"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/scrypt"
"golang.org/x/crypto/ssh/terminal"
)

func init() {
caddycmd.RegisterCommand(caddycmd.Command{
Name: "hash-password",
Func: cmdHashPassword,
Usage: "--plaintext <password> [--salt <string>] [--algorithm <name>]",
Usage: "[--algorithm <name>] [--salt <string>] [--plaintext <password>]",
Short: "Hashes a password and writes base64",
Long: `
Convenient way to hash a plaintext password. The resulting
hash is written to stdout as a base64 string.

--plaintext, when omitted, will be read from stdin. If
Caddy is attached to a controlling tty, the plaintext will
not be echoed.

--algorithm may be bcrypt or scrypt. If script, the default
parameters are used.

Expand All @@ -52,16 +60,50 @@ be provided (scrypt).
}

func cmdHashPassword(fs caddycmd.Flags) (int, error) {
var err error

algorithm := fs.String("algorithm")
plaintext := []byte(fs.String("plaintext"))
salt := []byte(fs.String("salt"))

if len(plaintext) == 0 {
return caddy.ExitCodeFailedStartup, fmt.Errorf("password is required")
if terminal.IsTerminal(int(os.Stdin.Fd())) {
fmt.Print("Enter password: ")
plaintext, err = terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()

if err != nil {
return caddy.ExitCodeFailedStartup, err
}
mholt marked this conversation as resolved.
Show resolved Hide resolved

fmt.Print("Confirm password: ")
confirmation, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()

if err != nil {
return caddy.ExitCodeFailedStartup, err
}

if !bytes.Equal(plaintext, confirmation) {
return caddy.ExitCodeFailedStartup, fmt.Errorf("password does not match")
}
} else {
rd := bufio.NewReader(os.Stdin)
plaintext, err = rd.ReadBytes('\n')

if err != nil {
0az marked this conversation as resolved.
Show resolved Hide resolved
return caddy.ExitCodeFailedStartup, err
}

plaintext = plaintext[:len(plaintext)-1] // Trailing newline
}

if len(plaintext) == 0 {
return caddy.ExitCodeFailedStartup, fmt.Errorf("plaintext is required")
}
}

var hash []byte
var err error
switch algorithm {
case "bcrypt":
hash, err = bcrypt.GenerateFromPassword(plaintext, bcrypt.DefaultCost)
Expand Down