start_windows.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. gwHNSID := ""
  57. if container.NetworkSettings != nil {
  58. for n := range container.NetworkSettings.Networks {
  59. sn, err := daemon.FindNetwork(n)
  60. if err != nil {
  61. continue
  62. }
  63. ep, err := container.GetEndpointInNetwork(sn)
  64. if err != nil {
  65. continue
  66. }
  67. data, err := ep.DriverInfo()
  68. if err != nil {
  69. continue
  70. }
  71. if data["GW_INFO"] != nil {
  72. gwInfo := data["GW_INFO"].(map[string]interface{})
  73. if gwInfo["hnsid"] != nil {
  74. gwHNSID = gwInfo["hnsid"].(string)
  75. }
  76. }
  77. if data["hnsid"] != nil {
  78. epList = append(epList, data["hnsid"].(string))
  79. }
  80. if data["AllowUnqualifiedDNSQuery"] != nil {
  81. AllowUnqualifiedDNSQuery = true
  82. }
  83. }
  84. }
  85. if gwHNSID != "" {
  86. epList = append(epList, gwHNSID)
  87. }
  88. // Read and add credentials from the security options if a credential spec has been provided.
  89. if container.HostConfig.SecurityOpt != nil {
  90. for _, sOpt := range container.HostConfig.SecurityOpt {
  91. sOpt = strings.ToLower(sOpt)
  92. if !strings.Contains(sOpt, "=") {
  93. return nil, fmt.Errorf("invalid security option: no equals sign in supplied value %s", sOpt)
  94. }
  95. var splitsOpt []string
  96. splitsOpt = strings.SplitN(sOpt, "=", 2)
  97. if len(splitsOpt) != 2 {
  98. return nil, fmt.Errorf("invalid security option: %s", sOpt)
  99. }
  100. if splitsOpt[0] != "credentialspec" {
  101. return nil, fmt.Errorf("security option not supported: %s", splitsOpt[0])
  102. }
  103. credentialsOpts := &libcontainerd.CredentialsOption{}
  104. var (
  105. match bool
  106. csValue string
  107. err error
  108. )
  109. if match, csValue = getCredentialSpec("file://", splitsOpt[1]); match {
  110. if csValue == "" {
  111. return nil, fmt.Errorf("no value supplied for file:// credential spec security option")
  112. }
  113. if credentialsOpts.Credentials, err = readCredentialSpecFile(container.ID, daemon.root, filepath.Clean(csValue)); err != nil {
  114. return nil, err
  115. }
  116. } else if match, csValue = getCredentialSpec("registry://", splitsOpt[1]); match {
  117. if csValue == "" {
  118. return nil, fmt.Errorf("no value supplied for registry:// credential spec security option")
  119. }
  120. if credentialsOpts.Credentials, err = readCredentialSpecRegistry(container.ID, csValue); err != nil {
  121. return nil, err
  122. }
  123. } else {
  124. return nil, fmt.Errorf("invalid credential spec security option - value must be prefixed file:// or registry:// followed by a value")
  125. }
  126. createOptions = append(createOptions, credentialsOpts)
  127. }
  128. }
  129. // Now add the remaining options.
  130. createOptions = append(createOptions, &libcontainerd.FlushOption{IgnoreFlushesDuringBoot: !container.HasBeenStartedBefore})
  131. createOptions = append(createOptions, hvOpts)
  132. createOptions = append(createOptions, layerOpts)
  133. if epList != nil {
  134. createOptions = append(createOptions, &libcontainerd.NetworkEndpointsOption{Endpoints: epList, AllowUnqualifiedDNSQuery: AllowUnqualifiedDNSQuery})
  135. }
  136. return createOptions, nil
  137. }
  138. // getCredentialSpec is a helper function to get the value of a credential spec supplied
  139. // on the CLI, stripping the prefix
  140. func getCredentialSpec(prefix, value string) (bool, string) {
  141. if strings.HasPrefix(value, prefix) {
  142. return true, strings.TrimPrefix(value, prefix)
  143. }
  144. return false, ""
  145. }
  146. // readCredentialSpecRegistry is a helper function to read a credential spec from
  147. // the registry. If not found, we return an empty string and warn in the log.
  148. // This allows for staging on machines which do not have the necessary components.
  149. func readCredentialSpecRegistry(id, name string) (string, error) {
  150. var (
  151. k registry.Key
  152. err error
  153. val string
  154. )
  155. if k, err = registry.OpenKey(registry.LOCAL_MACHINE, credentialSpecRegistryLocation, registry.QUERY_VALUE); err != nil {
  156. return "", fmt.Errorf("failed handling spec %q for container %s - %s could not be opened", name, id, credentialSpecRegistryLocation)
  157. }
  158. if val, _, err = k.GetStringValue(name); err != nil {
  159. if err == registry.ErrNotExist {
  160. return "", fmt.Errorf("credential spec %q for container %s as it was not found", name, id)
  161. }
  162. return "", fmt.Errorf("error %v reading credential spec %q from registry for container %s", err, name, id)
  163. }
  164. return val, nil
  165. }
  166. // readCredentialSpecFile is a helper function to read a credential spec from
  167. // a file. If not found, we return an empty string and warn in the log.
  168. // This allows for staging on machines which do not have the necessary components.
  169. func readCredentialSpecFile(id, root, location string) (string, error) {
  170. if filepath.IsAbs(location) {
  171. return "", fmt.Errorf("invalid credential spec - file:// path cannot be absolute")
  172. }
  173. base := filepath.Join(root, credentialSpecFileLocation)
  174. full := filepath.Join(base, location)
  175. if !strings.HasPrefix(full, base) {
  176. return "", fmt.Errorf("invalid credential spec - file:// path must be under %s", base)
  177. }
  178. bcontents, err := ioutil.ReadFile(full)
  179. if err != nil {
  180. return "", fmt.Errorf("credential spec '%s' for container %s as the file could not be read: %q", full, id, err)
  181. }
  182. return string(bcontents[:]), nil
  183. }