sandbox_externalkey_unix.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //go:build linux || freebsd
  2. // +build linux freebsd
  3. package libnetwork
  4. import (
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "net"
  10. "os"
  11. "path/filepath"
  12. "github.com/docker/docker/libnetwork/types"
  13. "github.com/docker/docker/pkg/reexec"
  14. "github.com/docker/docker/pkg/stringid"
  15. "github.com/opencontainers/runtime-spec/specs-go"
  16. "github.com/sirupsen/logrus"
  17. )
  18. const (
  19. execSubdir = "libnetwork"
  20. defaultExecRoot = "/run/docker"
  21. success = "success"
  22. )
  23. func init() {
  24. // TODO(thaJeztah): should this actually be registered on FreeBSD, or only on Linux?
  25. reexec.Register("libnetwork-setkey", processSetKeyReexec)
  26. }
  27. type setKeyData struct {
  28. ContainerID string
  29. Key string
  30. }
  31. // processSetKeyReexec is a private function that must be called only on an reexec path
  32. // It expects 3 args { [0] = "libnetwork-setkey", [1] = <container-id>, [2] = <short-controller-id> }
  33. // It also expects specs.State as a json string in <stdin>
  34. // Refer to https://github.com/opencontainers/runc/pull/160/ for more information
  35. // The docker exec-root can be specified as "-exec-root" flag. The default value is "/run/docker".
  36. func processSetKeyReexec() {
  37. if err := setKey(); err != nil {
  38. _, _ = fmt.Fprintln(os.Stderr, err)
  39. os.Exit(1)
  40. }
  41. }
  42. func setKey() error {
  43. execRoot := flag.String("exec-root", defaultExecRoot, "docker exec root")
  44. flag.Parse()
  45. // expecting 3 os.Args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<short-controller-id> }
  46. // (i.e. expecting 2 flag.Args())
  47. args := flag.Args()
  48. if len(args) < 2 {
  49. return fmt.Errorf("re-exec expects 2 args (after parsing flags), received : %d", len(args))
  50. }
  51. containerID, shortCtlrID := args[0], args[1]
  52. // We expect specs.State as a json string in <stdin>
  53. stateBuf, err := io.ReadAll(os.Stdin)
  54. if err != nil {
  55. return err
  56. }
  57. var state specs.State
  58. if err = json.Unmarshal(stateBuf, &state); err != nil {
  59. return err
  60. }
  61. return SetExternalKey(shortCtlrID, containerID, fmt.Sprintf("/proc/%d/ns/net", state.Pid), *execRoot)
  62. }
  63. // SetExternalKey provides a convenient way to set an External key to a sandbox
  64. func SetExternalKey(shortCtlrID string, containerID string, key string, execRoot string) error {
  65. keyData := setKeyData{
  66. ContainerID: containerID,
  67. Key: key,
  68. }
  69. uds := filepath.Join(execRoot, execSubdir, shortCtlrID+".sock")
  70. c, err := net.Dial("unix", uds)
  71. if err != nil {
  72. return err
  73. }
  74. defer c.Close()
  75. if err = sendKey(c, keyData); err != nil {
  76. return fmt.Errorf("sendKey failed with : %v", err)
  77. }
  78. return processReturn(c)
  79. }
  80. func sendKey(c net.Conn, data setKeyData) error {
  81. var err error
  82. defer func() {
  83. if err != nil {
  84. c.Close()
  85. }
  86. }()
  87. var b []byte
  88. if b, err = json.Marshal(data); err != nil {
  89. return err
  90. }
  91. _, err = c.Write(b)
  92. return err
  93. }
  94. func processReturn(r io.Reader) error {
  95. buf := make([]byte, 1024)
  96. n, err := r.Read(buf[:])
  97. if err != nil {
  98. return fmt.Errorf("failed to read buf in processReturn : %v", err)
  99. }
  100. if string(buf[0:n]) != success {
  101. return fmt.Errorf(string(buf[0:n]))
  102. }
  103. return nil
  104. }
  105. func (c *Controller) startExternalKeyListener() error {
  106. execRoot := defaultExecRoot
  107. if v := c.Config().ExecRoot; v != "" {
  108. execRoot = v
  109. }
  110. udsBase := filepath.Join(execRoot, execSubdir)
  111. if err := os.MkdirAll(udsBase, 0600); err != nil {
  112. return err
  113. }
  114. shortCtlrID := stringid.TruncateID(c.id)
  115. uds := filepath.Join(udsBase, shortCtlrID+".sock")
  116. l, err := net.Listen("unix", uds)
  117. if err != nil {
  118. return err
  119. }
  120. if err := os.Chmod(uds, 0600); err != nil {
  121. l.Close()
  122. return err
  123. }
  124. c.mu.Lock()
  125. c.extKeyListener = l
  126. c.mu.Unlock()
  127. go c.acceptClientConnections(uds, l)
  128. return nil
  129. }
  130. func (c *Controller) acceptClientConnections(sock string, l net.Listener) {
  131. for {
  132. conn, err := l.Accept()
  133. if err != nil {
  134. if _, err1 := os.Stat(sock); os.IsNotExist(err1) {
  135. logrus.Debugf("Unix socket %s doesn't exist. cannot accept client connections", sock)
  136. return
  137. }
  138. logrus.Errorf("Error accepting connection %v", err)
  139. continue
  140. }
  141. go func() {
  142. defer conn.Close()
  143. err := c.processExternalKey(conn)
  144. ret := success
  145. if err != nil {
  146. ret = err.Error()
  147. }
  148. _, err = conn.Write([]byte(ret))
  149. if err != nil {
  150. logrus.Errorf("Error returning to the client %v", err)
  151. }
  152. }()
  153. }
  154. }
  155. func (c *Controller) processExternalKey(conn net.Conn) error {
  156. buf := make([]byte, 1280)
  157. nr, err := conn.Read(buf)
  158. if err != nil {
  159. return err
  160. }
  161. var s setKeyData
  162. if err = json.Unmarshal(buf[0:nr], &s); err != nil {
  163. return err
  164. }
  165. var sandbox *Sandbox
  166. search := SandboxContainerWalker(&sandbox, s.ContainerID)
  167. c.WalkSandboxes(search)
  168. if sandbox == nil {
  169. return types.BadRequestErrorf("no sandbox present for %s", s.ContainerID)
  170. }
  171. return sandbox.SetKey(s.Key)
  172. }
  173. func (c *Controller) stopExternalKeyListener() {
  174. c.extKeyListener.Close()
  175. }