docker.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "runtime"
  9. "strings"
  10. "github.com/Sirupsen/logrus"
  11. "github.com/docker/docker/api/client"
  12. "github.com/docker/docker/autogen/dockerversion"
  13. "github.com/docker/docker/opts"
  14. flag "github.com/docker/docker/pkg/mflag"
  15. "github.com/docker/docker/pkg/reexec"
  16. "github.com/docker/docker/pkg/term"
  17. )
  18. const (
  19. defaultTrustKeyFile = "key.json"
  20. defaultCaFile = "ca.pem"
  21. defaultKeyFile = "key.pem"
  22. defaultCertFile = "cert.pem"
  23. )
  24. func main() {
  25. if reexec.Init() {
  26. return
  27. }
  28. // Set terminal emulation based on platform as required.
  29. stdin, stdout, stderr := term.StdStreams()
  30. initLogging(stderr)
  31. flag.Parse()
  32. // FIXME: validate daemon flags here
  33. if *flVersion {
  34. showVersion()
  35. return
  36. }
  37. if *flLogLevel != "" {
  38. lvl, err := logrus.ParseLevel(*flLogLevel)
  39. if err != nil {
  40. logrus.Fatalf("Unable to parse logging level: %s", *flLogLevel)
  41. }
  42. setLogLevel(lvl)
  43. } else {
  44. setLogLevel(logrus.InfoLevel)
  45. }
  46. if *flDebug {
  47. os.Setenv("DEBUG", "1")
  48. }
  49. if len(flHosts) == 0 {
  50. defaultHost := os.Getenv("DOCKER_HOST")
  51. if defaultHost == "" || *flDaemon {
  52. if runtime.GOOS != "windows" {
  53. // If we do not have a host, default to unix socket
  54. defaultHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket)
  55. } else {
  56. // If we do not have a host, default to TCP socket on Windows
  57. defaultHost = fmt.Sprintf("tcp://%s:%d", opts.DefaultHTTPHost, opts.DefaultHTTPPort)
  58. }
  59. }
  60. defaultHost, err := opts.ValidateHost(defaultHost)
  61. if err != nil {
  62. logrus.Fatal(err)
  63. }
  64. flHosts = append(flHosts, defaultHost)
  65. }
  66. setDefaultConfFlag(flTrustKey, defaultTrustKeyFile)
  67. if *flDaemon {
  68. if *flHelp {
  69. flag.Usage()
  70. return
  71. }
  72. mainDaemon()
  73. return
  74. }
  75. if len(flHosts) > 1 {
  76. logrus.Fatal("Please specify only one -H")
  77. }
  78. protoAddrParts := strings.SplitN(flHosts[0], "://", 2)
  79. var (
  80. cli *client.DockerCli
  81. tlsConfig tls.Config
  82. )
  83. tlsConfig.InsecureSkipVerify = true
  84. // Regardless of whether the user sets it to true or false, if they
  85. // specify --tlsverify at all then we need to turn on tls
  86. if flag.IsSet("-tlsverify") {
  87. *flTls = true
  88. }
  89. // If we should verify the server, we need to load a trusted ca
  90. if *flTlsVerify {
  91. certPool := x509.NewCertPool()
  92. file, err := ioutil.ReadFile(*flCa)
  93. if err != nil {
  94. logrus.Fatalf("Couldn't read ca cert %s: %s", *flCa, err)
  95. }
  96. certPool.AppendCertsFromPEM(file)
  97. tlsConfig.RootCAs = certPool
  98. tlsConfig.InsecureSkipVerify = false
  99. }
  100. // If tls is enabled, try to load and send client certificates
  101. if *flTls || *flTlsVerify {
  102. _, errCert := os.Stat(*flCert)
  103. _, errKey := os.Stat(*flKey)
  104. if errCert == nil && errKey == nil {
  105. *flTls = true
  106. cert, err := tls.LoadX509KeyPair(*flCert, *flKey)
  107. if err != nil {
  108. logrus.Fatalf("Couldn't load X509 key pair: %q. Make sure the key is encrypted", err)
  109. }
  110. tlsConfig.Certificates = []tls.Certificate{cert}
  111. }
  112. // Avoid fallback to SSL protocols < TLS1.0
  113. tlsConfig.MinVersion = tls.VersionTLS10
  114. }
  115. if *flTls || *flTlsVerify {
  116. cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], &tlsConfig)
  117. } else {
  118. cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], nil)
  119. }
  120. if err := cli.Cmd(flag.Args()...); err != nil {
  121. if sterr, ok := err.(client.StatusError); ok {
  122. if sterr.Status != "" {
  123. logrus.Println(sterr.Status)
  124. }
  125. os.Exit(sterr.StatusCode)
  126. }
  127. logrus.Fatal(err)
  128. }
  129. }
  130. func showVersion() {
  131. fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
  132. }