2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux || freebsd
|
2016-02-17 14:24:43 -05:00
|
|
|
// +build linux freebsd
|
2015-11-23 17:02:42 -05:00
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
"flag"
|
2015-11-23 17:02:42 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
"path/filepath"
|
2015-11-23 17:02:42 -05:00
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2018-12-06 20:47:05 -05:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-23 17:02:42 -05:00
|
|
|
)
|
|
|
|
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
const (
|
|
|
|
execSubdir = "libnetwork"
|
|
|
|
defaultExecRoot = "/run/docker"
|
|
|
|
success = "success"
|
|
|
|
)
|
2015-11-23 17:02:42 -05:00
|
|
|
|
|
|
|
// processSetKeyReexec is a private function that must be called only on an reexec path
|
2019-08-28 13:59:49 -04:00
|
|
|
// It expects 3 args { [0] = "libnetwork-setkey", [1] = <container-id>, [2] = <short-controller-id> }
|
2018-12-06 20:47:05 -05:00
|
|
|
// It also expects specs.State as a json string in <stdin>
|
2015-11-23 17:02:42 -05:00
|
|
|
// Refer to https://github.com/opencontainers/runc/pull/160/ for more information
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
// The docker exec-root can be specified as "-exec-root" flag. The default value is "/run/docker".
|
2015-11-23 17:02:42 -05:00
|
|
|
func processSetKeyReexec() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Return a failure to the calling process via ExitCode
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
execRoot := flag.String("exec-root", defaultExecRoot, "docker exec root")
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-08-28 13:59:49 -04:00
|
|
|
// expecting 3 os.Args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<short-controller-id> }
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
// (i.e. expecting 2 flag.Args())
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) < 2 {
|
|
|
|
err = fmt.Errorf("Re-exec expects 2 args (after parsing flags), received : %d", len(args))
|
2015-11-23 17:02:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-08-28 13:59:49 -04:00
|
|
|
containerID, shortCtlrID := args[0], args[1]
|
2015-11-23 17:02:42 -05:00
|
|
|
|
2018-12-06 20:47:05 -05:00
|
|
|
// We expect specs.State as a json string in <stdin>
|
2021-08-24 06:10:50 -04:00
|
|
|
stateBuf, err := io.ReadAll(os.Stdin)
|
2015-11-23 17:02:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-12-06 20:47:05 -05:00
|
|
|
var state specs.State
|
2015-11-23 17:02:42 -05:00
|
|
|
if err = json.Unmarshal(stateBuf, &state); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-28 13:59:49 -04:00
|
|
|
err = SetExternalKey(shortCtlrID, containerID, fmt.Sprintf("/proc/%d/ns/net", state.Pid), *execRoot)
|
2015-11-23 17:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetExternalKey provides a convenient way to set an External key to a sandbox
|
2019-08-28 13:59:49 -04:00
|
|
|
func SetExternalKey(shortCtlrID string, containerID string, key string, execRoot string) error {
|
2015-11-23 17:02:42 -05:00
|
|
|
keyData := setKeyData{
|
|
|
|
ContainerID: containerID,
|
|
|
|
Key: key}
|
|
|
|
|
2019-08-28 13:59:49 -04:00
|
|
|
uds := filepath.Join(execRoot, execSubdir, shortCtlrID+".sock")
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
c, err := net.Dial("unix", uds)
|
2015-11-23 17:02:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
if err = sendKey(c, keyData); err != nil {
|
|
|
|
return fmt.Errorf("sendKey failed with : %v", err)
|
|
|
|
}
|
|
|
|
return processReturn(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendKey(c net.Conn, data setKeyData) error {
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var b []byte
|
|
|
|
if b, err = json.Marshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func processReturn(r io.Reader) error {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, err := r.Read(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read buf in processReturn : %v", err)
|
|
|
|
}
|
|
|
|
if string(buf[0:n]) != success {
|
|
|
|
return fmt.Errorf(string(buf[0:n]))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) startExternalKeyListener() error {
|
allow propagating custom exec-root (e.g. "/run/docker") to libnetwork-setkey
The docker daemon needs to be modified as follows:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 00ace320df..ea7daa72df 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -809,7 +809,7 @@ func (daemon *Daemon) createSpec(c *container.Container) (retSpec *specs.Spec, e
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: target,
- Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID()},
+ Args: []string{"libnetwork-setkey", c.ID, daemon.netController.ID(), "-exec-root="+daemon.configStore.GetExecRoot()},
}},
}
}
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-08-11 08:26:40 -04:00
|
|
|
execRoot := defaultExecRoot
|
|
|
|
if v := c.Config().Daemon.ExecRoot; v != "" {
|
|
|
|
execRoot = v
|
|
|
|
}
|
|
|
|
udsBase := filepath.Join(execRoot, execSubdir)
|
2015-11-23 17:02:42 -05:00
|
|
|
if err := os.MkdirAll(udsBase, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-28 13:59:49 -04:00
|
|
|
shortCtlrID := stringid.TruncateID(c.id)
|
|
|
|
uds := filepath.Join(udsBase, shortCtlrID+".sock")
|
2015-11-23 17:02:42 -05:00
|
|
|
l, err := net.Listen("unix", uds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chmod(uds, 0600); err != nil {
|
|
|
|
l.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.extKeyListener = l
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
go c.acceptClientConnections(uds, l)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) acceptClientConnections(sock string, l net.Listener) {
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if _, err1 := os.Stat(sock); os.IsNotExist(err1) {
|
2016-02-23 16:44:38 -05:00
|
|
|
logrus.Debugf("Unix socket %s doesn't exist. cannot accept client connections", sock)
|
2015-11-23 17:02:42 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Errorf("Error accepting connection %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go func() {
|
2016-07-01 19:29:51 -04:00
|
|
|
defer conn.Close()
|
|
|
|
|
2015-11-23 17:02:42 -05:00
|
|
|
err := c.processExternalKey(conn)
|
|
|
|
ret := success
|
|
|
|
if err != nil {
|
|
|
|
ret = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte(ret))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error returning to the client %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) processExternalKey(conn net.Conn) error {
|
|
|
|
buf := make([]byte, 1280)
|
|
|
|
nr, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var s setKeyData
|
|
|
|
if err = json.Unmarshal(buf[0:nr], &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var sandbox Sandbox
|
|
|
|
search := SandboxContainerWalker(&sandbox, s.ContainerID)
|
|
|
|
c.WalkSandboxes(search)
|
|
|
|
if sandbox == nil {
|
|
|
|
return types.BadRequestErrorf("no sandbox present for %s", s.ContainerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sandbox.SetKey(s.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) stopExternalKeyListener() {
|
|
|
|
c.extKeyListener.Close()
|
|
|
|
}
|