sandbox_externalkey_unix.go 4.5 KB

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