cli.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package command
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "github.com/docker/docker/api"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/versions"
  13. cliconfig "github.com/docker/docker/cli/config"
  14. "github.com/docker/docker/cli/config/configfile"
  15. "github.com/docker/docker/cli/config/credentials"
  16. cliflags "github.com/docker/docker/cli/flags"
  17. "github.com/docker/docker/client"
  18. "github.com/docker/docker/dockerversion"
  19. dopts "github.com/docker/docker/opts"
  20. "github.com/docker/go-connections/sockets"
  21. "github.com/docker/go-connections/tlsconfig"
  22. "github.com/spf13/cobra"
  23. "golang.org/x/net/context"
  24. )
  25. // Streams is an interface which exposes the standard input and output streams
  26. type Streams interface {
  27. In() *InStream
  28. Out() *OutStream
  29. Err() io.Writer
  30. }
  31. // Cli represents the docker command line client.
  32. type Cli interface {
  33. Client() client.APIClient
  34. Out() *OutStream
  35. Err() io.Writer
  36. In() *InStream
  37. ConfigFile() *configfile.ConfigFile
  38. }
  39. // DockerCli is an instance the docker command line client.
  40. // Instances of the client can be returned from NewDockerCli.
  41. type DockerCli struct {
  42. configFile *configfile.ConfigFile
  43. in *InStream
  44. out *OutStream
  45. err io.Writer
  46. keyFile string
  47. client client.APIClient
  48. defaultVersion string
  49. server ServerInfo
  50. }
  51. // DefaultVersion returns api.defaultVersion or DOCKER_API_VERSION if specified.
  52. func (cli *DockerCli) DefaultVersion() string {
  53. return cli.defaultVersion
  54. }
  55. // Client returns the APIClient
  56. func (cli *DockerCli) Client() client.APIClient {
  57. return cli.client
  58. }
  59. // Out returns the writer used for stdout
  60. func (cli *DockerCli) Out() *OutStream {
  61. return cli.out
  62. }
  63. // Err returns the writer used for stderr
  64. func (cli *DockerCli) Err() io.Writer {
  65. return cli.err
  66. }
  67. // In returns the reader used for stdin
  68. func (cli *DockerCli) In() *InStream {
  69. return cli.in
  70. }
  71. // ShowHelp shows the command help.
  72. func (cli *DockerCli) ShowHelp(cmd *cobra.Command, args []string) error {
  73. cmd.SetOutput(cli.err)
  74. cmd.HelpFunc()(cmd, args)
  75. return nil
  76. }
  77. // ConfigFile returns the ConfigFile
  78. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  79. return cli.configFile
  80. }
  81. // ServerInfo returns the server version details for the host this client is
  82. // connected to
  83. func (cli *DockerCli) ServerInfo() ServerInfo {
  84. return cli.server
  85. }
  86. // GetAllCredentials returns all of the credentials stored in all of the
  87. // configured credential stores.
  88. func (cli *DockerCli) GetAllCredentials() (map[string]types.AuthConfig, error) {
  89. auths := make(map[string]types.AuthConfig)
  90. for registry := range cli.configFile.CredentialHelpers {
  91. helper := cli.CredentialsStore(registry)
  92. newAuths, err := helper.GetAll()
  93. if err != nil {
  94. return nil, err
  95. }
  96. addAll(auths, newAuths)
  97. }
  98. defaultStore := cli.CredentialsStore("")
  99. newAuths, err := defaultStore.GetAll()
  100. if err != nil {
  101. return nil, err
  102. }
  103. addAll(auths, newAuths)
  104. return auths, nil
  105. }
  106. func addAll(to, from map[string]types.AuthConfig) {
  107. for reg, ac := range from {
  108. to[reg] = ac
  109. }
  110. }
  111. // CredentialsStore returns a new credentials store based
  112. // on the settings provided in the configuration file. Empty string returns
  113. // the default credential store.
  114. func (cli *DockerCli) CredentialsStore(serverAddress string) credentials.Store {
  115. if helper := getConfiguredCredentialStore(cli.configFile, serverAddress); helper != "" {
  116. return credentials.NewNativeStore(cli.configFile, helper)
  117. }
  118. return credentials.NewFileStore(cli.configFile)
  119. }
  120. // getConfiguredCredentialStore returns the credential helper configured for the
  121. // given registry, the default credsStore, or the empty string if neither are
  122. // configured.
  123. func getConfiguredCredentialStore(c *configfile.ConfigFile, serverAddress string) string {
  124. if c.CredentialHelpers != nil && serverAddress != "" {
  125. if helper, exists := c.CredentialHelpers[serverAddress]; exists {
  126. return helper
  127. }
  128. }
  129. return c.CredentialsStore
  130. }
  131. // Initialize the dockerCli runs initialization that must happen after command
  132. // line flags are parsed.
  133. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
  134. cli.configFile = LoadDefaultConfigFile(cli.err)
  135. var err error
  136. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  137. if err != nil {
  138. return err
  139. }
  140. cli.defaultVersion = cli.client.ClientVersion()
  141. if opts.Common.TrustKey == "" {
  142. cli.keyFile = filepath.Join(cliconfig.Dir(), cliflags.DefaultTrustKeyFile)
  143. } else {
  144. cli.keyFile = opts.Common.TrustKey
  145. }
  146. if ping, err := cli.client.Ping(context.Background()); err == nil {
  147. cli.server = ServerInfo{
  148. HasExperimental: ping.Experimental,
  149. OSType: ping.OSType,
  150. }
  151. // since the new header was added in 1.25, assume server is 1.24 if header is not present.
  152. if ping.APIVersion == "" {
  153. ping.APIVersion = "1.24"
  154. }
  155. // if server version is lower than the current cli, downgrade
  156. if versions.LessThan(ping.APIVersion, cli.client.ClientVersion()) {
  157. cli.client.UpdateClientVersion(ping.APIVersion)
  158. }
  159. }
  160. return nil
  161. }
  162. // ServerInfo stores details about the supported features and platform of the
  163. // server
  164. type ServerInfo struct {
  165. HasExperimental bool
  166. OSType string
  167. }
  168. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  169. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  170. return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
  171. }
  172. // LoadDefaultConfigFile attempts to load the default config file and returns
  173. // an initialized ConfigFile struct if none is found.
  174. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  175. configFile, e := cliconfig.Load(cliconfig.Dir())
  176. if e != nil {
  177. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  178. }
  179. if !configFile.ContainsAuth() {
  180. credentials.DetectDefaultStore(configFile)
  181. }
  182. return configFile
  183. }
  184. // NewAPIClientFromFlags creates a new APIClient from command line flags
  185. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  186. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  187. if err != nil {
  188. return &client.Client{}, err
  189. }
  190. customHeaders := configFile.HTTPHeaders
  191. if customHeaders == nil {
  192. customHeaders = map[string]string{}
  193. }
  194. customHeaders["User-Agent"] = UserAgent()
  195. verStr := api.DefaultVersion
  196. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  197. verStr = tmpStr
  198. }
  199. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  200. if err != nil {
  201. return &client.Client{}, err
  202. }
  203. return client.NewClient(host, verStr, httpClient, customHeaders)
  204. }
  205. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  206. switch len(hosts) {
  207. case 0:
  208. host = os.Getenv("DOCKER_HOST")
  209. case 1:
  210. host = hosts[0]
  211. default:
  212. return "", errors.New("Please specify only one -H")
  213. }
  214. host, err = dopts.ParseHost(tlsOptions != nil, host)
  215. return
  216. }
  217. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  218. if tlsOptions == nil {
  219. // let the api client configure the default transport.
  220. return nil, nil
  221. }
  222. config, err := tlsconfig.Client(*tlsOptions)
  223. if err != nil {
  224. return nil, err
  225. }
  226. tr := &http.Transport{
  227. TLSClientConfig: config,
  228. }
  229. proto, addr, _, err := client.ParseHost(host)
  230. if err != nil {
  231. return nil, err
  232. }
  233. sockets.ConfigureTransport(tr, proto, addr)
  234. return &http.Client{
  235. Transport: tr,
  236. }, nil
  237. }
  238. // UserAgent returns the user agent string used for making API requests
  239. func UserAgent() string {
  240. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  241. }