start_windows.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package daemon
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/container"
  8. "github.com/docker/docker/layer"
  9. "github.com/docker/docker/libcontainerd"
  10. "golang.org/x/sys/windows/registry"
  11. )
  12. const (
  13. credentialSpecRegistryLocation = `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\Containers\CredentialSpecs`
  14. credentialSpecFileLocation = "CredentialSpecs"
  15. )
  16. func (daemon *Daemon) getLibcontainerdCreateOptions(container *container.Container) (*[]libcontainerd.CreateOption, error) {
  17. createOptions := []libcontainerd.CreateOption{}
  18. // Are we going to run as a Hyper-V container?
  19. hvOpts := &libcontainerd.HyperVIsolationOption{}
  20. if container.HostConfig.Isolation.IsDefault() {
  21. // Container is set to use the default, so take the default from the daemon configuration
  22. hvOpts.IsHyperV = daemon.defaultIsolation.IsHyperV()
  23. } else {
  24. // Container is requesting an isolation mode. Honour it.
  25. hvOpts.IsHyperV = container.HostConfig.Isolation.IsHyperV()
  26. }
  27. // Generate the layer folder of the layer options
  28. layerOpts := &libcontainerd.LayerOption{}
  29. m, err := container.RWLayer.Metadata()
  30. if err != nil {
  31. return nil, fmt.Errorf("failed to get layer metadata - %s", err)
  32. }
  33. if hvOpts.IsHyperV {
  34. hvOpts.SandboxPath = filepath.Dir(m["dir"])
  35. }
  36. layerOpts.LayerFolderPath = m["dir"]
  37. // Generate the layer paths of the layer options
  38. img, err := daemon.imageStore.Get(container.ImageID)
  39. if err != nil {
  40. return nil, fmt.Errorf("failed to graph.Get on ImageID %s - %s", container.ImageID, err)
  41. }
  42. // Get the layer path for each layer.
  43. max := len(img.RootFS.DiffIDs)
  44. for i := 1; i <= max; i++ {
  45. img.RootFS.DiffIDs = img.RootFS.DiffIDs[:i]
  46. layerPath, err := layer.GetLayerPath(daemon.layerStore, img.RootFS.ChainID())
  47. if err != nil {
  48. return nil, fmt.Errorf("failed to get layer path from graphdriver %s for ImageID %s - %s", daemon.layerStore, img.RootFS.ChainID(), err)
  49. }
  50. // Reverse order, expecting parent most first
  51. layerOpts.LayerPaths = append([]string{layerPath}, layerOpts.LayerPaths...)
  52. }
  53. // Get endpoints for the libnetwork allocated networks to the container
  54. var epList []string
  55. AllowUnqualifiedDNSQuery := false
  56. if container.NetworkSettings != nil {
  57. for n := range container.NetworkSettings.Networks {
  58. sn, err := daemon.FindNetwork(n)
  59. if err != nil {
  60. continue
  61. }
  62. ep, err := container.GetEndpointInNetwork(sn)
  63. if err != nil {
  64. continue
  65. }
  66. data, err := ep.DriverInfo()
  67. if err != nil {
  68. continue
  69. }
  70. if data["hnsid"] != nil {
  71. epList = append(epList, data["hnsid"].(string))
  72. }
  73. if data["AllowUnqualifiedDNSQuery"] != nil {
  74. AllowUnqualifiedDNSQuery = true
  75. }
  76. }
  77. }
  78. // Read and add credentials from the security options if a credential spec has been provided.
  79. if container.HostConfig.SecurityOpt != nil {
  80. for _, sOpt := range container.HostConfig.SecurityOpt {
  81. sOpt = strings.ToLower(sOpt)
  82. if !strings.Contains(sOpt, "=") {
  83. return nil, fmt.Errorf("invalid security option: no equals sign in supplied value %s", sOpt)
  84. }
  85. var splitsOpt []string
  86. splitsOpt = strings.SplitN(sOpt, "=", 2)
  87. if len(splitsOpt) != 2 {
  88. return nil, fmt.Errorf("invalid security option: %s", sOpt)
  89. }
  90. if splitsOpt[0] != "credentialspec" {
  91. return nil, fmt.Errorf("security option not supported: %s", splitsOpt[0])
  92. }
  93. credentialsOpts := &libcontainerd.CredentialsOption{}
  94. var (
  95. match bool
  96. csValue string
  97. err error
  98. )
  99. if match, csValue = getCredentialSpec("file://", splitsOpt[1]); match {
  100. if csValue == "" {
  101. return nil, fmt.Errorf("no value supplied for file:// credential spec security option")
  102. }
  103. if credentialsOpts.Credentials, err = readCredentialSpecFile(container.ID, daemon.root, filepath.Clean(csValue)); err != nil {
  104. return nil, err
  105. }
  106. } else if match, csValue = getCredentialSpec("registry://", splitsOpt[1]); match {
  107. if csValue == "" {
  108. return nil, fmt.Errorf("no value supplied for registry:// credential spec security option")
  109. }
  110. if credentialsOpts.Credentials, err = readCredentialSpecRegistry(container.ID, csValue); err != nil {
  111. return nil, err
  112. }
  113. } else {
  114. return nil, fmt.Errorf("invalid credential spec security option - value must be prefixed file:// or registry:// followed by a value")
  115. }
  116. createOptions = append(createOptions, credentialsOpts)
  117. }
  118. }
  119. // Now add the remaining options.
  120. createOptions = append(createOptions, &libcontainerd.FlushOption{IgnoreFlushesDuringBoot: !container.HasBeenStartedBefore})
  121. createOptions = append(createOptions, hvOpts)
  122. createOptions = append(createOptions, layerOpts)
  123. if epList != nil {
  124. createOptions = append(createOptions, &libcontainerd.NetworkEndpointsOption{Endpoints: epList, AllowUnqualifiedDNSQuery: AllowUnqualifiedDNSQuery})
  125. }
  126. return &createOptions, nil
  127. }
  128. // getCredentialSpec is a helper function to get the value of a credential spec supplied
  129. // on the CLI, stripping the prefix
  130. func getCredentialSpec(prefix, value string) (bool, string) {
  131. if strings.HasPrefix(value, prefix) {
  132. return true, strings.TrimPrefix(value, prefix)
  133. }
  134. return false, ""
  135. }
  136. // readCredentialSpecRegistry is a helper function to read a credential spec from
  137. // the registry. If not found, we return an empty string and warn in the log.
  138. // This allows for staging on machines which do not have the necessary components.
  139. func readCredentialSpecRegistry(id, name string) (string, error) {
  140. var (
  141. k registry.Key
  142. err error
  143. val string
  144. )
  145. if k, err = registry.OpenKey(registry.LOCAL_MACHINE, credentialSpecRegistryLocation, registry.QUERY_VALUE); err != nil {
  146. return "", fmt.Errorf("failed handling spec %q for container %s - %s could not be opened", name, id, credentialSpecRegistryLocation)
  147. }
  148. if val, _, err = k.GetStringValue(name); err != nil {
  149. if err == registry.ErrNotExist {
  150. return "", fmt.Errorf("credential spec %q for container %s as it was not found", name, id)
  151. }
  152. return "", fmt.Errorf("error %v reading credential spec %q from registry for container %s", err, name, id)
  153. }
  154. return val, nil
  155. }
  156. // readCredentialSpecFile is a helper function to read a credential spec from
  157. // a file. If not found, we return an empty string and warn in the log.
  158. // This allows for staging on machines which do not have the necessary components.
  159. func readCredentialSpecFile(id, root, location string) (string, error) {
  160. if filepath.IsAbs(location) {
  161. return "", fmt.Errorf("invalid credential spec - file:// path cannot be absolute")
  162. }
  163. base := filepath.Join(root, credentialSpecFileLocation)
  164. full := filepath.Join(base, location)
  165. if !strings.HasPrefix(full, base) {
  166. return "", fmt.Errorf("invalid credential spec - file:// path must be under %s", base)
  167. }
  168. bcontents, err := ioutil.ReadFile(full)
  169. if err != nil {
  170. return "", fmt.Errorf("credential spec '%s' for container %s as the file could not be read: %q", full, id, err)
  171. }
  172. return string(bcontents[:]), nil
  173. }