Skip to content

Commit 806157b

Browse files
committed
sandbox_externalkey.go: split for cross compilation
runc/libcontainer split the `State` struct into platform specific structs in opencontainers/runc@fe1cce6. As a result, `NamespacePaths` isn't anymore in a global struct and libnetwork is not cross-compiling in Docker (specifically on Windows) because `sandbox_externalkey.go` is using `NamespacePaths`. This patch splits `sandbox_externalkey.go` into platform specific files and moves common things to a generic `sandbox_externalkey.go`. Signed-off-by: Antonio Murdaca <runcom@redhat.com>
1 parent 54948e2 commit 806157b

File tree

3 files changed

+223
-174
lines changed

3 files changed

+223
-174
lines changed

sandbox_externalkey.go

Lines changed: 1 addition & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
package libnetwork
22

3-
import (
4-
"encoding/json"
5-
"fmt"
6-
"io"
7-
"io/ioutil"
8-
"net"
9-
"os"
10-
11-
"github.com/Sirupsen/logrus"
12-
"github.com/docker/docker/pkg/reexec"
13-
"github.com/docker/libnetwork/types"
14-
"github.com/opencontainers/runc/libcontainer"
15-
"github.com/opencontainers/runc/libcontainer/configs"
16-
)
3+
import "github.com/docker/docker/pkg/reexec"
174

185
type setKeyData struct {
196
ContainerID string
@@ -23,163 +10,3 @@ type setKeyData struct {
2310
func init() {
2411
reexec.Register("libnetwork-setkey", processSetKeyReexec)
2512
}
26-
27-
const udsBase = "/var/lib/docker/network/files/"
28-
const success = "success"
29-
30-
// processSetKeyReexec is a private function that must be called only on an reexec path
31-
// It expects 3 args { [0] = "libnetwork-setkey", [1] = <container-id>, [2] = <controller-id> }
32-
// It also expects libcontainer.State as a json string in <stdin>
33-
// Refer to https://github.com/opencontainers/runc/pull/160/ for more information
34-
func processSetKeyReexec() {
35-
var err error
36-
37-
// Return a failure to the calling process via ExitCode
38-
defer func() {
39-
if err != nil {
40-
logrus.Fatalf("%v", err)
41-
}
42-
}()
43-
44-
// expecting 3 args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<controller-id> }
45-
if len(os.Args) < 3 {
46-
err = fmt.Errorf("Re-exec expects 3 args, received : %d", len(os.Args))
47-
return
48-
}
49-
containerID := os.Args[1]
50-
51-
// We expect libcontainer.State as a json string in <stdin>
52-
stateBuf, err := ioutil.ReadAll(os.Stdin)
53-
if err != nil {
54-
return
55-
}
56-
var state libcontainer.State
57-
if err = json.Unmarshal(stateBuf, &state); err != nil {
58-
return
59-
}
60-
61-
controllerID := os.Args[2]
62-
key := state.NamespacePaths[configs.NamespaceType("NEWNET")]
63-
64-
err = SetExternalKey(controllerID, containerID, key)
65-
return
66-
}
67-
68-
// SetExternalKey provides a convenient way to set an External key to a sandbox
69-
func SetExternalKey(controllerID string, containerID string, key string) error {
70-
keyData := setKeyData{
71-
ContainerID: containerID,
72-
Key: key}
73-
74-
c, err := net.Dial("unix", udsBase+controllerID+".sock")
75-
if err != nil {
76-
return err
77-
}
78-
defer c.Close()
79-
80-
if err = sendKey(c, keyData); err != nil {
81-
return fmt.Errorf("sendKey failed with : %v", err)
82-
}
83-
return processReturn(c)
84-
}
85-
86-
func sendKey(c net.Conn, data setKeyData) error {
87-
var err error
88-
defer func() {
89-
if err != nil {
90-
c.Close()
91-
}
92-
}()
93-
94-
var b []byte
95-
if b, err = json.Marshal(data); err != nil {
96-
return err
97-
}
98-
99-
_, err = c.Write(b)
100-
return err
101-
}
102-
103-
func processReturn(r io.Reader) error {
104-
buf := make([]byte, 1024)
105-
n, err := r.Read(buf[:])
106-
if err != nil {
107-
return fmt.Errorf("failed to read buf in processReturn : %v", err)
108-
}
109-
if string(buf[0:n]) != success {
110-
return fmt.Errorf(string(buf[0:n]))
111-
}
112-
return nil
113-
}
114-
115-
func (c *controller) startExternalKeyListener() error {
116-
if err := os.MkdirAll(udsBase, 0600); err != nil {
117-
return err
118-
}
119-
uds := udsBase + c.id + ".sock"
120-
l, err := net.Listen("unix", uds)
121-
if err != nil {
122-
return err
123-
}
124-
if err := os.Chmod(uds, 0600); err != nil {
125-
l.Close()
126-
return err
127-
}
128-
c.Lock()
129-
c.extKeyListener = l
130-
c.Unlock()
131-
132-
go c.acceptClientConnections(uds, l)
133-
return nil
134-
}
135-
136-
func (c *controller) acceptClientConnections(sock string, l net.Listener) {
137-
for {
138-
conn, err := l.Accept()
139-
if err != nil {
140-
if _, err1 := os.Stat(sock); os.IsNotExist(err1) {
141-
logrus.Debugf("Unix socket %s doesnt exist. cannot accept client connections", sock)
142-
return
143-
}
144-
logrus.Errorf("Error accepting connection %v", err)
145-
continue
146-
}
147-
go func() {
148-
err := c.processExternalKey(conn)
149-
ret := success
150-
if err != nil {
151-
ret = err.Error()
152-
}
153-
154-
_, err = conn.Write([]byte(ret))
155-
if err != nil {
156-
logrus.Errorf("Error returning to the client %v", err)
157-
}
158-
}()
159-
}
160-
}
161-
162-
func (c *controller) processExternalKey(conn net.Conn) error {
163-
buf := make([]byte, 1280)
164-
nr, err := conn.Read(buf)
165-
if err != nil {
166-
return err
167-
}
168-
var s setKeyData
169-
if err = json.Unmarshal(buf[0:nr], &s); err != nil {
170-
return err
171-
}
172-
173-
var sandbox Sandbox
174-
search := SandboxContainerWalker(&sandbox, s.ContainerID)
175-
c.WalkSandboxes(search)
176-
if sandbox == nil {
177-
return types.BadRequestErrorf("no sandbox present for %s", s.ContainerID)
178-
}
179-
180-
return sandbox.SetKey(s.Key)
181-
}
182-
183-
func (c *controller) stopExternalKeyListener() {
184-
c.extKeyListener.Close()
185-
}

sandbox_externalkey_unix.go

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// +build !windows
2+
3+
package libnetwork
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"io/ioutil"
10+
"net"
11+
"os"
12+
13+
"github.com/Sirupsen/logrus"
14+
"github.com/docker/libnetwork/types"
15+
"github.com/opencontainers/runc/libcontainer"
16+
"github.com/opencontainers/runc/libcontainer/configs"
17+
)
18+
19+
const udsBase = "/var/lib/docker/network/files/"
20+
const 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] = <controller-id> }
24+
// It also expects libcontainer.State as a json string in <stdin>
25+
// Refer to https://github.com/opencontainers/runc/pull/160/ for more information
26+
func processSetKeyReexec() {
27+
var err error
28+
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+
36+
// expecting 3 args {[0]="libnetwork-setkey", [1]=<container-id>, [2]=<controller-id> }
37+
if len(os.Args) < 3 {
38+
err = fmt.Errorf("Re-exec expects 3 args, received : %d", len(os.Args))
39+
return
40+
}
41+
containerID := os.Args[1]
42+
43+
// We expect libcontainer.State as a json string in <stdin>
44+
stateBuf, err := ioutil.ReadAll(os.Stdin)
45+
if err != nil {
46+
return
47+
}
48+
var state libcontainer.State
49+
if err = json.Unmarshal(stateBuf, &state); err != nil {
50+
return
51+
}
52+
53+
controllerID := os.Args[2]
54+
key := state.NamespacePaths[configs.NamespaceType("NEWNET")]
55+
56+
err = SetExternalKey(controllerID, containerID, key)
57+
return
58+
}
59+
60+
// SetExternalKey provides a convenient way to set an External key to a sandbox
61+
func SetExternalKey(controllerID string, containerID string, key string) error {
62+
keyData := setKeyData{
63+
ContainerID: containerID,
64+
Key: key}
65+
66+
c, err := net.Dial("unix", udsBase+controllerID+".sock")
67+
if err != nil {
68+
return err
69+
}
70+
defer c.Close()
71+
72+
if err = sendKey(c, keyData); err != nil {
73+
return fmt.Errorf("sendKey failed with : %v", err)
74+
}
75+
return processReturn(c)
76+
}
77+
78+
func sendKey(c net.Conn, data setKeyData) error {
79+
var err error
80+
defer func() {
81+
if err != nil {
82+
c.Close()
83+
}
84+
}()
85+
86+
var b []byte
87+
if b, err = json.Marshal(data); err != nil {
88+
return err
89+
}
90+
91+
_, err = c.Write(b)
92+
return err
93+
}
94+
95+
func processReturn(r io.Reader) error {
96+
buf := make([]byte, 1024)
97+
n, err := r.Read(buf[:])
98+
if err != nil {
99+
return fmt.Errorf("failed to read buf in processReturn : %v", err)
100+
}
101+
if string(buf[0:n]) != success {
102+
return fmt.Errorf(string(buf[0:n]))
103+
}
104+
return nil
105+
}
106+
107+
func (c *controller) startExternalKeyListener() error {
108+
if err := os.MkdirAll(udsBase, 0600); err != nil {
109+
return err
110+
}
111+
uds := udsBase + c.id + ".sock"
112+
l, err := net.Listen("unix", uds)
113+
if err != nil {
114+
return err
115+
}
116+
if err := os.Chmod(uds, 0600); err != nil {
117+
l.Close()
118+
return err
119+
}
120+
c.Lock()
121+
c.extKeyListener = l
122+
c.Unlock()
123+
124+
go c.acceptClientConnections(uds, l)
125+
return nil
126+
}
127+
128+
func (c *controller) acceptClientConnections(sock string, l net.Listener) {
129+
for {
130+
conn, err := l.Accept()
131+
if err != nil {
132+
if _, err1 := os.Stat(sock); os.IsNotExist(err1) {
133+
logrus.Debugf("Unix socket %s doesnt exist. cannot accept client connections", sock)
134+
return
135+
}
136+
logrus.Errorf("Error accepting connection %v", err)
137+
continue
138+
}
139+
go func() {
140+
err := c.processExternalKey(conn)
141+
ret := success
142+
if err != nil {
143+
ret = err.Error()
144+
}
145+
146+
_, err = conn.Write([]byte(ret))
147+
if err != nil {
148+
logrus.Errorf("Error returning to the client %v", err)
149+
}
150+
}()
151+
}
152+
}
153+
154+
func (c *controller) processExternalKey(conn net.Conn) error {
155+
buf := make([]byte, 1280)
156+
nr, err := conn.Read(buf)
157+
if err != nil {
158+
return err
159+
}
160+
var s setKeyData
161+
if err = json.Unmarshal(buf[0:nr], &s); err != nil {
162+
return err
163+
}
164+
165+
var sandbox Sandbox
166+
search := SandboxContainerWalker(&sandbox, s.ContainerID)
167+
c.WalkSandboxes(search)
168+
if sandbox == nil {
169+
return types.BadRequestErrorf("no sandbox present for %s", s.ContainerID)
170+
}
171+
172+
return sandbox.SetKey(s.Key)
173+
}
174+
175+
func (c *controller) stopExternalKeyListener() {
176+
c.extKeyListener.Close()
177+
}

0 commit comments

Comments
 (0)