common.go 4.6 KB

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