sandbox_externalkey_unix.go 4.4 KB

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