Skip to content

Commit

Permalink
feat: hide password on command
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
이유비 authored and moul committed Mar 29, 2021
1 parent 3de9f58 commit 2679646
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
17 changes: 17 additions & 0 deletions cmd/gotty-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
gottyclient "github.com/moul/gotty-client"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)

var VERSION string
Expand Down Expand Up @@ -67,6 +68,11 @@ func main() {
Usage: "WebSocket Origin URL",
EnvVar: "GOTTY_CLIENT_WS_ORIGIN",
},
cli.StringFlag{
Name: "user, u",
Usage: "User for Basic Authentication",
EnvVar: "GOTTY_CLIENT_USER",
},
}

app.Action = action
Expand Down Expand Up @@ -121,6 +127,17 @@ func action(c *cli.Context) error {
}
}

if user := c.String("user"); user != "" {
client.User = user
fmt.Print("Password: ")
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
return cli.NewExitError("Failed to get password from stdin", 2)
}
client.Password = string(password)
}

// loop
if err = client.Loop(); err != nil {
logrus.Fatalf("Communication error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion gotty-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"net/url"
"os"
"regexp"
"syscall"
"strings"
"sync"
"syscall"
"time"

"github.com/containerd/console"
Expand Down Expand Up @@ -144,6 +144,8 @@ type Client struct {
V2 bool
message *gottyMessageType
WSOrigin string
User string
Password string
}

type querySingleType struct {
Expand All @@ -163,6 +165,10 @@ func (c *Client) GetAuthToken() (string, error) {
if err != nil {
return "", err
}
if c.User != "" {
basicAuth := c.User + ":" + c.Password
header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(basicAuth)))
}

logrus.Debugf("Fetching auth token auth-token: %q", target.String())
req, err := http.NewRequest("GET", target.String(), nil)
Expand Down Expand Up @@ -220,6 +226,10 @@ func (c *Client) Connect() error {
if err != nil {
return err
}
if c.User != "" {
basicAuth := c.User + ":" + c.Password
header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(basicAuth)))
}
if c.WSOrigin != "" {
header.Add("Origin", c.WSOrigin)
}
Expand Down

0 comments on commit 2679646

Please # to comment.