github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/registry/login.go (about)

     1  package registry
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"strings"
     8  
     9  	"github.com/docker/cli/cli"
    10  	"github.com/docker/cli/cli/command"
    11  	configtypes "github.com/docker/cli/cli/config/types"
    12  	"github.com/docker/docker/api/types"
    13  	registrytypes "github.com/docker/docker/api/types/registry"
    14  	"github.com/docker/docker/client"
    15  	"github.com/docker/docker/registry"
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  const unencryptedWarning = `WARNING! Your password will be stored unencrypted in %s.
    21  Configure a credential helper to remove this warning. See
    22  https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    23  `
    24  
    25  type loginOptions struct {
    26  	serverAddress string
    27  	user          string
    28  	password      string
    29  	passwordStdin bool
    30  }
    31  
    32  // NewLoginCommand creates a new `docker login` command
    33  func NewLoginCommand(dockerCli command.Cli) *cobra.Command {
    34  	var opts loginOptions
    35  
    36  	cmd := &cobra.Command{
    37  		Use:   "login [OPTIONS] [SERVER]",
    38  		Short: "Log in to a Docker registry",
    39  		Long:  "Log in to a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
    40  		Args:  cli.RequiresMaxArgs(1),
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			if len(args) > 0 {
    43  				opts.serverAddress = args[0]
    44  			}
    45  			return runLogin(dockerCli, opts)
    46  		},
    47  	}
    48  
    49  	flags := cmd.Flags()
    50  
    51  	flags.StringVarP(&opts.user, "username", "u", "", "Username")
    52  	flags.StringVarP(&opts.password, "password", "p", "", "Password")
    53  	flags.BoolVarP(&opts.passwordStdin, "password-stdin", "", false, "Take the password from stdin")
    54  
    55  	return cmd
    56  }
    57  
    58  // displayUnencryptedWarning warns the user when using an insecure credential storage.
    59  // After a deprecation period, user will get prompted if stdin and stderr are a terminal.
    60  // Otherwise, we'll assume they want it (sadly), because people may have been scripting
    61  // insecure logins and we don't want to break them. Maybe they'll see the warning in their
    62  // logs and fix things.
    63  func displayUnencryptedWarning(dockerCli command.Streams, filename string) error {
    64  	_, err := fmt.Fprintln(dockerCli.Err(), fmt.Sprintf(unencryptedWarning, filename))
    65  
    66  	return err
    67  }
    68  
    69  type isFileStore interface {
    70  	IsFileStore() bool
    71  	GetFilename() string
    72  }
    73  
    74  func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
    75  	if opts.password != "" {
    76  		fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
    77  		if opts.passwordStdin {
    78  			return errors.New("--password and --password-stdin are mutually exclusive")
    79  		}
    80  	}
    81  
    82  	if opts.passwordStdin {
    83  		if opts.user == "" {
    84  			return errors.New("Must provide --username with --password-stdin")
    85  		}
    86  
    87  		contents, err := ioutil.ReadAll(dockerCli.In())
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		opts.password = strings.TrimSuffix(string(contents), "\n")
    93  		opts.password = strings.TrimSuffix(opts.password, "\r")
    94  	}
    95  	return nil
    96  }
    97  
    98  func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint: gocyclo
    99  	ctx := context.Background()
   100  	clnt := dockerCli.Client()
   101  	if err := verifyloginOptions(dockerCli, &opts); err != nil {
   102  		return err
   103  	}
   104  	var (
   105  		serverAddress string
   106  		authServer    = command.ElectAuthServer(ctx, dockerCli)
   107  	)
   108  	if opts.serverAddress != "" && opts.serverAddress != registry.DefaultNamespace {
   109  		serverAddress = opts.serverAddress
   110  	} else {
   111  		serverAddress = authServer
   112  	}
   113  
   114  	var response registrytypes.AuthenticateOKBody
   115  	isDefaultRegistry := serverAddress == authServer
   116  	authConfig, err := command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry)
   117  	if err == nil && authConfig.Username != "" && authConfig.Password != "" {
   118  		response, err = loginWithCredStoreCreds(ctx, dockerCli, &authConfig)
   119  	}
   120  	if err != nil || authConfig.Username == "" || authConfig.Password == "" {
   121  		err = command.ConfigureAuth(dockerCli, opts.user, opts.password, &authConfig, isDefaultRegistry)
   122  		if err != nil {
   123  			return err
   124  		}
   125  
   126  		response, err = clnt.RegistryLogin(ctx, authConfig)
   127  		if err != nil && client.IsErrConnectionFailed(err) {
   128  			// If the server isn't responding (yet) attempt to login purely client side
   129  			response, err = loginClientSide(ctx, authConfig)
   130  		}
   131  		// If we (still) have an error, give up
   132  		if err != nil {
   133  			return err
   134  		}
   135  	}
   136  	if response.IdentityToken != "" {
   137  		authConfig.Password = ""
   138  		authConfig.IdentityToken = response.IdentityToken
   139  	}
   140  
   141  	creds := dockerCli.ConfigFile().GetCredentialsStore(serverAddress)
   142  
   143  	store, isDefault := creds.(isFileStore)
   144  	// Display a warning if we're storing the users password (not a token)
   145  	if isDefault && authConfig.Password != "" {
   146  		err = displayUnencryptedWarning(dockerCli, store.GetFilename())
   147  		if err != nil {
   148  			return err
   149  		}
   150  	}
   151  
   152  	if err := creds.Store(configtypes.AuthConfig(authConfig)); err != nil {
   153  		return errors.Errorf("Error saving credentials: %v", err)
   154  	}
   155  
   156  	if response.Status != "" {
   157  		fmt.Fprintln(dockerCli.Out(), response.Status)
   158  	}
   159  	return nil
   160  }
   161  
   162  func loginWithCredStoreCreds(ctx context.Context, dockerCli command.Cli, authConfig *types.AuthConfig) (registrytypes.AuthenticateOKBody, error) {
   163  	fmt.Fprintf(dockerCli.Out(), "Authenticating with existing credentials...\n")
   164  	cliClient := dockerCli.Client()
   165  	response, err := cliClient.RegistryLogin(ctx, *authConfig)
   166  	if err != nil {
   167  		if client.IsErrUnauthorized(err) {
   168  			fmt.Fprintf(dockerCli.Err(), "Stored credentials invalid or expired\n")
   169  		} else {
   170  			fmt.Fprintf(dockerCli.Err(), "Login did not succeed, error: %s\n", err)
   171  		}
   172  	}
   173  	return response, err
   174  }
   175  
   176  func loginClientSide(ctx context.Context, auth types.AuthConfig) (registrytypes.AuthenticateOKBody, error) {
   177  	svc, err := registry.NewService(registry.ServiceOptions{})
   178  	if err != nil {
   179  		return registrytypes.AuthenticateOKBody{}, err
   180  	}
   181  
   182  	status, token, err := svc.Auth(ctx, &auth, command.UserAgent())
   183  
   184  	return registrytypes.AuthenticateOKBody{
   185  		Status:        status,
   186  		IdentityToken: token,
   187  	}, err
   188  }