registry.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package client
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "os"
  9. "runtime"
  10. "strings"
  11. "golang.org/x/net/context"
  12. "github.com/docker/docker/pkg/term"
  13. "github.com/docker/docker/reference"
  14. "github.com/docker/docker/registry"
  15. "github.com/docker/engine-api/types"
  16. registrytypes "github.com/docker/engine-api/types/registry"
  17. )
  18. // ElectAuthServer returns the default registry to use (by asking the daemon)
  19. func (cli *DockerCli) ElectAuthServer(ctx context.Context) string {
  20. // The daemon `/info` endpoint informs us of the default registry being
  21. // used. This is essential in cross-platforms environment, where for
  22. // example a Linux client might be interacting with a Windows daemon, hence
  23. // the default registry URL might be Windows specific.
  24. serverAddress := registry.IndexServer
  25. if info, err := cli.client.Info(ctx); err != nil {
  26. fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
  27. } else {
  28. serverAddress = info.IndexServerAddress
  29. }
  30. return serverAddress
  31. }
  32. // EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload
  33. func EncodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
  34. buf, err := json.Marshal(authConfig)
  35. if err != nil {
  36. return "", err
  37. }
  38. return base64.URLEncoding.EncodeToString(buf), nil
  39. }
  40. // RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
  41. // for the given command.
  42. func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
  43. return func() (string, error) {
  44. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  45. indexServer := registry.GetAuthConfigKey(index)
  46. authConfig, err := cli.ConfigureAuth("", "", indexServer, false)
  47. if err != nil {
  48. return "", err
  49. }
  50. return EncodeAuthToBase64(authConfig)
  51. }
  52. }
  53. func (cli *DockerCli) promptWithDefault(prompt string, configDefault string) {
  54. if configDefault == "" {
  55. fmt.Fprintf(cli.out, "%s: ", prompt)
  56. } else {
  57. fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
  58. }
  59. }
  60. // ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
  61. // default index, it uses the default index name for the daemon's platform,
  62. // not the client's platform.
  63. func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
  64. configKey := index.Name
  65. if index.Official {
  66. configKey = cli.ElectAuthServer(ctx)
  67. }
  68. a, _ := GetCredentials(cli.configFile, configKey)
  69. return a
  70. }
  71. // RetrieveAuthConfigs return all credentials.
  72. func (cli *DockerCli) RetrieveAuthConfigs() map[string]types.AuthConfig {
  73. acs, _ := GetAllCredentials(cli.configFile)
  74. return acs
  75. }
  76. // ConfigureAuth returns an AuthConfig from the specified user, password and server.
  77. func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
  78. // On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
  79. if runtime.GOOS == "windows" {
  80. cli.in = os.Stdin
  81. }
  82. authconfig, err := GetCredentials(cli.configFile, serverAddress)
  83. if err != nil {
  84. return authconfig, err
  85. }
  86. // Some links documenting this:
  87. // - https://code.google.com/archive/p/mintty/issues/56
  88. // - https://github.com/docker/docker/issues/15272
  89. // - https://mintty.github.io/ (compatibility)
  90. // Linux will hit this if you attempt `cat | docker login`, and Windows
  91. // will hit this if you attempt docker login from mintty where stdin
  92. // is a pipe, not a character based console.
  93. if flPassword == "" && !cli.isTerminalIn {
  94. return authconfig, fmt.Errorf("Error: Cannot perform an interactive login from a non TTY device")
  95. }
  96. authconfig.Username = strings.TrimSpace(authconfig.Username)
  97. if flUser = strings.TrimSpace(flUser); flUser == "" {
  98. if isDefaultRegistry {
  99. // if this is a default registry (docker hub), then display the following message.
  100. fmt.Fprintln(cli.out, "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
  101. }
  102. cli.promptWithDefault("Username", authconfig.Username)
  103. flUser = readInput(cli.in, cli.out)
  104. flUser = strings.TrimSpace(flUser)
  105. if flUser == "" {
  106. flUser = authconfig.Username
  107. }
  108. }
  109. if flUser == "" {
  110. return authconfig, fmt.Errorf("Error: Non-null Username Required")
  111. }
  112. if flPassword == "" {
  113. oldState, err := term.SaveState(cli.inFd)
  114. if err != nil {
  115. return authconfig, err
  116. }
  117. fmt.Fprintf(cli.out, "Password: ")
  118. term.DisableEcho(cli.inFd, oldState)
  119. flPassword = readInput(cli.in, cli.out)
  120. fmt.Fprint(cli.out, "\n")
  121. term.RestoreTerminal(cli.inFd, oldState)
  122. if flPassword == "" {
  123. return authconfig, fmt.Errorf("Error: Password Required")
  124. }
  125. }
  126. authconfig.Username = flUser
  127. authconfig.Password = flPassword
  128. authconfig.ServerAddress = serverAddress
  129. authconfig.IdentityToken = ""
  130. return authconfig, nil
  131. }
  132. // resolveAuthConfigFromImage retrieves that AuthConfig using the image string
  133. func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image string) (types.AuthConfig, error) {
  134. registryRef, err := reference.ParseNamed(image)
  135. if err != nil {
  136. return types.AuthConfig{}, err
  137. }
  138. repoInfo, err := registry.ParseRepositoryInfo(registryRef)
  139. if err != nil {
  140. return types.AuthConfig{}, err
  141. }
  142. authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
  143. return authConfig, nil
  144. }
  145. // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
  146. func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image string) (string, error) {
  147. // Retrieve encoded auth token from the image reference
  148. authConfig, err := cli.resolveAuthConfigFromImage(ctx, image)
  149. if err != nil {
  150. return "", err
  151. }
  152. encodedAuth, err := EncodeAuthToBase64(authConfig)
  153. if err != nil {
  154. return "", err
  155. }
  156. return encodedAuth, nil
  157. }
  158. func readInput(in io.Reader, out io.Writer) string {
  159. reader := bufio.NewReader(in)
  160. line, _, err := reader.ReadLine()
  161. if err != nil {
  162. fmt.Fprintln(out, err.Error())
  163. os.Exit(1)
  164. }
  165. return string(line)
  166. }