2016-09-08 13:11:39 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-29 11:10:56 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-02-15 14:43:01 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loginOptions struct {
|
|
|
|
serverAddress string
|
|
|
|
user string
|
|
|
|
password string
|
2017-06-29 11:10:56 -04:00
|
|
|
passwordStdin bool
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoginCommand creates a new `docker login` command
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewLoginCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts loginOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "login [OPTIONS] [SERVER]",
|
2016-10-31 23:07:31 -04:00
|
|
|
Short: "Log in to a Docker registry",
|
2016-09-08 13:11:39 -04:00
|
|
|
Long: "Log in to a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
opts.serverAddress = args[0]
|
|
|
|
}
|
|
|
|
return runLogin(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-03-15 13:25:36 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.StringVarP(&opts.user, "username", "u", "", "Username")
|
|
|
|
flags.StringVarP(&opts.password, "password", "p", "", "Password")
|
2017-06-29 11:10:56 -04:00
|
|
|
flags.BoolVarP(&opts.passwordStdin, "password-stdin", "", false, "Take the password from stdin")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func runLogin(dockerCli command.Cli, opts loginOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
2017-06-28 17:22:04 -04:00
|
|
|
if opts.password != "" {
|
2017-06-29 11:10:56 -04:00
|
|
|
fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
|
|
|
|
if opts.passwordStdin {
|
|
|
|
return errors.New("--password and --password-stdin are mutually exclusive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.passwordStdin {
|
|
|
|
if opts.user == "" {
|
|
|
|
return errors.New("Must provide --username with --password-stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(dockerCli.In())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.password = strings.TrimSuffix(string(contents), "\n")
|
|
|
|
opts.password = strings.TrimSuffix(opts.password, "\r")
|
2017-06-28 17:22:04 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
var (
|
|
|
|
serverAddress string
|
2016-09-09 15:38:00 -04:00
|
|
|
authServer = command.ElectAuthServer(ctx, dockerCli)
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
2017-02-15 14:43:01 -05:00
|
|
|
if opts.serverAddress != "" && opts.serverAddress != registry.DefaultNamespace {
|
2016-09-08 13:11:39 -04:00
|
|
|
serverAddress = opts.serverAddress
|
|
|
|
} else {
|
|
|
|
serverAddress = authServer
|
|
|
|
}
|
|
|
|
|
|
|
|
isDefaultRegistry := serverAddress == authServer
|
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig, err := command.ConfigureAuth(dockerCli, opts.user, opts.password, serverAddress, isDefaultRegistry)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response, err := clnt.RegistryLogin(ctx, authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if response.IdentityToken != "" {
|
|
|
|
authConfig.Password = ""
|
|
|
|
authConfig.IdentityToken = response.IdentityToken
|
|
|
|
}
|
2017-06-21 17:20:49 -04:00
|
|
|
if err := dockerCli.ConfigFile().GetCredentialsStore(serverAddress).Store(authConfig); err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Error saving credentials: %v", err)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.Status != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), response.Status)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|