common.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package api
  2. import (
  3. "encoding/json"
  4. "encoding/pem"
  5. "fmt"
  6. "mime"
  7. "os"
  8. "path/filepath"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/pkg/ioutils"
  14. "github.com/docker/docker/pkg/system"
  15. "github.com/docker/engine-api/types"
  16. "github.com/docker/libtrust"
  17. )
  18. // Common constants for daemon and client.
  19. const (
  20. // Version of Current REST API
  21. DefaultVersion string = "1.25"
  22. // MinVersion represents Minimum REST API version supported
  23. MinVersion string = "1.12"
  24. // NoBaseImageSpecifier is the symbol used by the FROM
  25. // command to specify that no base image is to be used.
  26. NoBaseImageSpecifier string = "scratch"
  27. )
  28. // byPortInfo is a temporary type used to sort types.Port by its fields
  29. type byPortInfo []types.Port
  30. func (r byPortInfo) Len() int { return len(r) }
  31. func (r byPortInfo) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  32. func (r byPortInfo) Less(i, j int) bool {
  33. if r[i].PrivatePort != r[j].PrivatePort {
  34. return r[i].PrivatePort < r[j].PrivatePort
  35. }
  36. if r[i].IP != r[j].IP {
  37. return r[i].IP < r[j].IP
  38. }
  39. if r[i].PublicPort != r[j].PublicPort {
  40. return r[i].PublicPort < r[j].PublicPort
  41. }
  42. return r[i].Type < r[j].Type
  43. }
  44. // DisplayablePorts returns formatted string representing open ports of container
  45. // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
  46. // it's used by command 'docker ps'
  47. func DisplayablePorts(ports []types.Port) string {
  48. type portGroup struct {
  49. first int
  50. last int
  51. }
  52. groupMap := make(map[string]*portGroup)
  53. var result []string
  54. var hostMappings []string
  55. var groupMapKeys []string
  56. sort.Sort(byPortInfo(ports))
  57. for _, port := range ports {
  58. current := port.PrivatePort
  59. portKey := port.Type
  60. if port.IP != "" {
  61. if port.PublicPort != current {
  62. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
  63. continue
  64. }
  65. portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
  66. }
  67. group := groupMap[portKey]
  68. if group == nil {
  69. groupMap[portKey] = &portGroup{first: current, last: current}
  70. // record order that groupMap keys are created
  71. groupMapKeys = append(groupMapKeys, portKey)
  72. continue
  73. }
  74. if current == (group.last + 1) {
  75. group.last = current
  76. continue
  77. }
  78. result = append(result, formGroup(portKey, group.first, group.last))
  79. groupMap[portKey] = &portGroup{first: current, last: current}
  80. }
  81. for _, portKey := range groupMapKeys {
  82. g := groupMap[portKey]
  83. result = append(result, formGroup(portKey, g.first, g.last))
  84. }
  85. result = append(result, hostMappings...)
  86. return strings.Join(result, ", ")
  87. }
  88. func formGroup(key string, start, last int) string {
  89. parts := strings.Split(key, "/")
  90. groupType := parts[0]
  91. var ip string
  92. if len(parts) > 1 {
  93. ip = parts[0]
  94. groupType = parts[1]
  95. }
  96. group := strconv.Itoa(start)
  97. if start != last {
  98. group = fmt.Sprintf("%s-%d", group, last)
  99. }
  100. if ip != "" {
  101. group = fmt.Sprintf("%s:%s->%s", ip, group, group)
  102. }
  103. return fmt.Sprintf("%s/%s", group, groupType)
  104. }
  105. // MatchesContentType validates the content type against the expected one
  106. func MatchesContentType(contentType, expectedType string) bool {
  107. mimetype, _, err := mime.ParseMediaType(contentType)
  108. if err != nil {
  109. logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
  110. }
  111. return err == nil && mimetype == expectedType
  112. }
  113. // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
  114. // otherwise generates a new one
  115. func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
  116. err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700)
  117. if err != nil {
  118. return nil, err
  119. }
  120. trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
  121. if err == libtrust.ErrKeyFileDoesNotExist {
  122. trustKey, err = libtrust.GenerateECP256PrivateKey()
  123. if err != nil {
  124. return nil, fmt.Errorf("Error generating key: %s", err)
  125. }
  126. encodedKey, err := serializePrivateKey(trustKey, filepath.Ext(trustKeyPath))
  127. if err != nil {
  128. return nil, fmt.Errorf("Error serializing key: %s", err)
  129. }
  130. if err := ioutils.AtomicWriteFile(trustKeyPath, encodedKey, os.FileMode(0600)); err != nil {
  131. return nil, fmt.Errorf("Error saving key file: %s", err)
  132. }
  133. } else if err != nil {
  134. return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
  135. }
  136. return trustKey, nil
  137. }
  138. func serializePrivateKey(key libtrust.PrivateKey, ext string) (encoded []byte, err error) {
  139. if ext == ".json" || ext == ".jwk" {
  140. encoded, err = json.Marshal(key)
  141. if err != nil {
  142. return nil, fmt.Errorf("unable to encode private key JWK: %s", err)
  143. }
  144. } else {
  145. pemBlock, err := key.PEMBlock()
  146. if err != nil {
  147. return nil, fmt.Errorf("unable to encode private key PEM: %s", err)
  148. }
  149. encoded = pem.EncodeToMemory(pemBlock)
  150. }
  151. return
  152. }