registry.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/api/types"
  13. registrytypes "github.com/docker/docker/api/types/registry"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/docker/docker/reference"
  16. "github.com/docker/docker/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. isDefaultRegistry := indexServer == cli.ElectAuthServer(context.Background())
  47. authConfig, err := cli.ConfigureAuth("", "", indexServer, isDefaultRegistry)
  48. if err != nil {
  49. return "", err
  50. }
  51. return EncodeAuthToBase64(authConfig)
  52. }
  53. }
  54. func (cli *DockerCli) promptWithDefault(prompt string, configDefault string) {
  55. if configDefault == "" {
  56. fmt.Fprintf(cli.out, "%s: ", prompt)
  57. } else {
  58. fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
  59. }
  60. }
  61. // ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
  62. // default index, it uses the default index name for the daemon's platform,
  63. // not the client's platform.
  64. func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
  65. configKey := index.Name
  66. if index.Official {
  67. configKey = cli.ElectAuthServer(ctx)
  68. }
  69. a, _ := GetCredentials(cli.configFile, configKey)
  70. return a
  71. }
  72. // RetrieveAuthConfigs return all credentials.
  73. func (cli *DockerCli) RetrieveAuthConfigs() map[string]types.AuthConfig {
  74. acs, _ := GetAllCredentials(cli.configFile)
  75. return acs
  76. }
  77. // ConfigureAuth returns an AuthConfig from the specified user, password and server.
  78. func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
  79. // On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
  80. if runtime.GOOS == "windows" {
  81. cli.in = NewInStream(os.Stdin)
  82. }
  83. if !isDefaultRegistry {
  84. serverAddress = registry.ConvertToHostname(serverAddress)
  85. }
  86. authconfig, err := GetCredentials(cli.configFile, serverAddress)
  87. if err != nil {
  88. return authconfig, err
  89. }
  90. // Some links documenting this:
  91. // - https://code.google.com/archive/p/mintty/issues/56
  92. // - https://github.com/docker/docker/issues/15272
  93. // - https://mintty.github.io/ (compatibility)
  94. // Linux will hit this if you attempt `cat | docker login`, and Windows
  95. // will hit this if you attempt docker login from mintty where stdin
  96. // is a pipe, not a character based console.
  97. if flPassword == "" && !cli.In().IsTerminal() {
  98. return authconfig, fmt.Errorf("Error: Cannot perform an interactive login from a non TTY device")
  99. }
  100. authconfig.Username = strings.TrimSpace(authconfig.Username)
  101. if flUser = strings.TrimSpace(flUser); flUser == "" {
  102. if isDefaultRegistry {
  103. // if this is a default registry (docker hub), then display the following message.
  104. 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.")
  105. }
  106. cli.promptWithDefault("Username", authconfig.Username)
  107. flUser = readInput(cli.in, cli.out)
  108. flUser = strings.TrimSpace(flUser)
  109. if flUser == "" {
  110. flUser = authconfig.Username
  111. }
  112. }
  113. if flUser == "" {
  114. return authconfig, fmt.Errorf("Error: Non-null Username Required")
  115. }
  116. if flPassword == "" {
  117. oldState, err := term.SaveState(cli.In().FD())
  118. if err != nil {
  119. return authconfig, err
  120. }
  121. fmt.Fprintf(cli.out, "Password: ")
  122. term.DisableEcho(cli.In().FD(), oldState)
  123. flPassword = readInput(cli.in, cli.out)
  124. fmt.Fprint(cli.out, "\n")
  125. term.RestoreTerminal(cli.In().FD(), oldState)
  126. if flPassword == "" {
  127. return authconfig, fmt.Errorf("Error: Password Required")
  128. }
  129. }
  130. authconfig.Username = flUser
  131. authconfig.Password = flPassword
  132. authconfig.ServerAddress = serverAddress
  133. authconfig.IdentityToken = ""
  134. return authconfig, nil
  135. }
  136. // resolveAuthConfigFromImage retrieves that AuthConfig using the image string
  137. func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image string) (types.AuthConfig, error) {
  138. registryRef, err := reference.ParseNamed(image)
  139. if err != nil {
  140. return types.AuthConfig{}, err
  141. }
  142. repoInfo, err := registry.ParseRepositoryInfo(registryRef)
  143. if err != nil {
  144. return types.AuthConfig{}, err
  145. }
  146. authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
  147. return authConfig, nil
  148. }
  149. // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
  150. func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image string) (string, error) {
  151. // Retrieve encoded auth token from the image reference
  152. authConfig, err := cli.resolveAuthConfigFromImage(ctx, image)
  153. if err != nil {
  154. return "", err
  155. }
  156. encodedAuth, err := EncodeAuthToBase64(authConfig)
  157. if err != nil {
  158. return "", err
  159. }
  160. return encodedAuth, nil
  161. }
  162. func readInput(in io.Reader, out io.Writer) string {
  163. reader := bufio.NewReader(in)
  164. line, _, err := reader.ReadLine()
  165. if err != nil {
  166. fmt.Fprintln(out, err.Error())
  167. os.Exit(1)
  168. }
  169. return string(line)
  170. }