Skip to content

Commit

Permalink
--password-stdin flag in podman login
Browse files Browse the repository at this point in the history
Support --password-stdin flag, reads a password from STDIN and pass it to `podman login`.

Signed-off-by: Qi Wang <qiwan@redhat.com>
  • Loading branch information
QiWang19 committed Feb 12, 2019
1 parent 112a5ab commit 7ea2362
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
13 changes: 7 additions & 6 deletions cmd/podman/cliconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ type LoadValues struct {

type LoginValues struct {
PodmanCommand
Password string
Username string
Authfile string
CertDir string
GetLogin bool
TlsVerify bool
Password string
StdinPassword bool
Username string
Authfile string
CertDir string
GetLogin bool
TlsVerify bool
}

type LogoutValues struct {
Expand Down
19 changes: 18 additions & 1 deletion cmd/podman/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
flags.StringVarP(&loginCommand.Password, "password", "p", "", "Password for registry")
flags.BoolVar(&loginCommand.TlsVerify, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries (default: true)")
flags.StringVarP(&loginCommand.Username, "username", "u", "", "Username for registry")
flags.BoolVar(&loginCommand.StdinPassword, "password-stdin", false, "Take the password from stdin")

rootCmd.AddCommand(loginCommand.Command)
}
Expand Down Expand Up @@ -90,6 +91,19 @@ func loginCmd(c *cliconfig.LoginValues) error {
}

ctx := getContext()

var password string
var stdinPasswordStrBuilder strings.Builder
if c.Flag("password-stdin").Changed {
if c.Username == "" {
return errors.Errorf("Must provide --username with --password-stdin")
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Fprint(&stdinPasswordStrBuilder, scanner.Text())
}
password = stdinPasswordStrBuilder.String()
}
// If no username and no password is specified, try to use existing ones.
if c.Username == "" && c.Password == "" {
fmt.Println("Authenticating with existing credentials...")
Expand All @@ -100,7 +114,10 @@ func loginCmd(c *cliconfig.LoginValues) error {
fmt.Println("Existing credentials are invalid, please enter valid username and password")
}

username, password, err := getUserAndPass(c.Username, c.Password, userFromAuthFile)
if password == "" {
password = c.Password
}
username, password, err := getUserAndPass(c.Username, password, userFromAuthFile)
if err != nil {
return errors.Wrapf(err, "error getting username and password")
}
Expand Down
1 change: 1 addition & 0 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,7 @@ _podman_login() {
local boolean_options="
--help
-h
--password-stdin
"
_complete_ "$options_with_args" "$boolean_options"
}
Expand Down
9 changes: 9 additions & 0 deletions docs/podman-login.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ flag. The default path used is **${XDG\_RUNTIME_DIR}/containers/auth.json**.

Password for registry

**--password-stdin**

Take the password from stdin

**--username, -u**

Username for registry
Expand Down Expand Up @@ -86,6 +90,11 @@ $ podman login --cert-dir /etc/containers/certs.d/ -u foo -p bar localhost:5000
Login Succeeded!
```

```
$ podman login -u testuser --password-stdin < testpassword.txt docker.io
Login Succeeded!
```

## SEE ALSO
podman(1), podman-logout(1), crio(8)

Expand Down

0 comments on commit 7ea2362

Please sign in to comment.