sandbox_externalkey_unix.go 4.5 KB

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