sandbox_externalkey_unix.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //go:build linux || freebsd
  2. package libnetwork
  3. import (
  4. "context"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "net"
  10. "os"
  11. "path/filepath"
  12. "github.com/containerd/log"
  13. "github.com/docker/docker/libnetwork/types"
  14. "github.com/docker/docker/pkg/reexec"
  15. "github.com/docker/docker/pkg/stringid"
  16. "github.com/opencontainers/runtime-spec/specs-go"
  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. var state specs.State
  54. if err := json.NewDecoder(os.Stdin).Decode(&state); err != nil {
  55. return err
  56. }
  57. return setExternalKey(shortCtlrID, containerID, fmt.Sprintf("/proc/%d/ns/net", state.Pid), *execRoot)
  58. }
  59. // setExternalKey provides a convenient way to set an External key to a sandbox
  60. func setExternalKey(shortCtlrID string, containerID string, key string, execRoot string) error {
  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. err = json.NewEncoder(c).Encode(setKeyData{
  68. ContainerID: containerID,
  69. Key: key,
  70. })
  71. if err != nil {
  72. return fmt.Errorf("sendKey failed with : %v", err)
  73. }
  74. return processReturn(c)
  75. }
  76. func processReturn(r io.Reader) error {
  77. buf := make([]byte, 1024)
  78. n, err := r.Read(buf[:])
  79. if err != nil {
  80. return fmt.Errorf("failed to read buf in processReturn : %v", err)
  81. }
  82. if string(buf[0:n]) != success {
  83. return fmt.Errorf(string(buf[0:n]))
  84. }
  85. return nil
  86. }
  87. func (c *Controller) startExternalKeyListener() error {
  88. execRoot := defaultExecRoot
  89. if v := c.Config().ExecRoot; v != "" {
  90. execRoot = v
  91. }
  92. udsBase := filepath.Join(execRoot, execSubdir)
  93. if err := os.MkdirAll(udsBase, 0o600); err != nil {
  94. return err
  95. }
  96. shortCtlrID := stringid.TruncateID(c.id)
  97. uds := filepath.Join(udsBase, shortCtlrID+".sock")
  98. l, err := net.Listen("unix", uds)
  99. if err != nil {
  100. return err
  101. }
  102. if err := os.Chmod(uds, 0o600); err != nil {
  103. l.Close()
  104. return err
  105. }
  106. c.mu.Lock()
  107. c.extKeyListener = l
  108. c.mu.Unlock()
  109. go c.acceptClientConnections(uds, l)
  110. return nil
  111. }
  112. func (c *Controller) acceptClientConnections(sock string, l net.Listener) {
  113. for {
  114. conn, err := l.Accept()
  115. if err != nil {
  116. if _, err1 := os.Stat(sock); os.IsNotExist(err1) {
  117. log.G(context.TODO()).Debugf("Unix socket %s doesn't exist. cannot accept client connections", sock)
  118. return
  119. }
  120. log.G(context.TODO()).Errorf("Error accepting connection %v", err)
  121. continue
  122. }
  123. go func() {
  124. defer conn.Close()
  125. err := c.processExternalKey(conn)
  126. ret := success
  127. if err != nil {
  128. ret = err.Error()
  129. }
  130. _, err = conn.Write([]byte(ret))
  131. if err != nil {
  132. log.G(context.TODO()).Errorf("Error returning to the client %v", err)
  133. }
  134. }()
  135. }
  136. }
  137. func (c *Controller) processExternalKey(conn net.Conn) error {
  138. buf := make([]byte, 1280)
  139. nr, err := conn.Read(buf)
  140. if err != nil {
  141. return err
  142. }
  143. var s setKeyData
  144. if err = json.Unmarshal(buf[0:nr], &s); err != nil {
  145. return err
  146. }
  147. sb, err := c.GetSandbox(s.ContainerID)
  148. if err != nil {
  149. return types.InvalidParameterErrorf("failed to get sandbox for %s", s.ContainerID)
  150. }
  151. return sb.SetKey(s.Key)
  152. }
  153. func (c *Controller) stopExternalKeyListener() {
  154. c.extKeyListener.Close()
  155. }