cli.go 8.5 KB

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