feat: hide password on command

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
This commit is contained in:
이유비 2020-11-23 02:42:45 +09:00 committed by Manfred Touron
parent 3de9f58b5b
commit 2679646e30
4 changed files with 30 additions and 4 deletions

View File

@ -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
@ -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
@ -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)

1
go.mod generated
View File

@ -11,5 +11,6 @@ require (
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.5.1 // indirect
github.com/urfave/cli v1.22.5
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
)

4
go.sum generated
View File

@ -1,13 +1,11 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -37,9 +35,9 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -11,9 +11,9 @@ import (
"net/url"
"os"
"regexp"
"syscall"
"strings"
"sync"
"syscall"
"time"
"github.com/containerd/console"
@ -144,6 +144,8 @@ type Client struct {
V2 bool
message *gottyMessageType
WSOrigin string
User string
Password string
}
type querySingleType struct {
@ -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)
@ -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)
}