-
Notifications
You must be signed in to change notification settings - Fork 25
/
dockerRemote.go
343 lines (321 loc) · 9.33 KB
/
dockerRemote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package collector
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"encoding/json"
"errors"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
config "github.com/banyanops/collector/config"
blog "github.com/ccpaging/log4go"
)
var (
// DockerTransport points to the http transport used to connect to the docker unix socket
DockerTransport *http.Transport
DockerTLSVerify = true
DockerProto = "unix"
DockerAddr = dummydomain
)
const (
// dummydomain is a fake domain name needed to perform HTTP requests to the Docker UNIX socket.
dummydomain = "example.com"
// HTTPTIMEOUT is the time to wait for an HTTP request to complete before giving up.
HTTPTIMEOUT = 32 * time.Second
// TARGETCONTAINERDIR is the path in the target container where the exported binaries and scripts are located.
TARGETCONTAINERDIR = "/banyancollector"
)
type HostConfig struct {
Binds []string
Links []string
Privileged bool
VolumesFrom []string
}
type Container struct {
User string
AttachStdin bool
AttachStdout bool
AttachStderr bool
Tty bool
Env []string
Cmd []string
Entrypoint []string
Image string
WorkingDir string
HostConfig HostConfig
}
func NewTLSTransport(hostpath string, certfile, cafile, keyfile string) (transport *http.Transport, err error) {
cert, err := tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
return
}
caCert, err := ioutil.ReadFile(cafile)
if err != nil {
return
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport = &http.Transport{TLSClientConfig: tlsConfig}
return
}
// NewDockerTransport creates an HTTP transport to the Docker unix/tcp socket.
func NewDockerTransport(proto, addr string) (tr *http.Transport, e error) {
// check Docker environment variables
dockerHost := os.Getenv("DOCKER_HOST")
if os.Getenv("DOCKER_TLS_VERIFY") == "0" {
DockerTLSVerify = false
}
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
if dockerHost == "" {
DockerProto = proto
DockerAddr = addr
} else {
blog.Info("$DOCKER_HOST env var = %s", dockerHost)
switch {
case strings.HasPrefix(dockerHost, "tcp://"):
blog.Info("Using protocol tcp")
DockerProto = "tcp"
DockerAddr = dockerHost[6:]
case strings.HasPrefix(dockerHost, "unix://"):
blog.Info("Using protocol unix")
DockerProto = "unix"
DockerAddr = dockerHost[6:]
default:
blog.Exit("Unexpected value in $DOCKER_HOST:", dockerHost)
}
}
// create transport for unix socket
if DockerProto != "unix" && DockerProto != "tcp" {
e = errors.New("Protocol " + DockerProto + " is not yet supported")
return
}
if DockerProto == "unix" {
tr = &http.Transport{}
tr.DisableCompression = true
tr.Dial = func(_, _ string) (net.Conn, error) {
return net.DialTimeout(DockerProto, DockerAddr, HTTPTIMEOUT)
}
return
}
if DockerTLSVerify {
certfile := dockerCertPath + "/cert.pem"
cafile := dockerCertPath + "/ca.pem"
keyfile := dockerCertPath + "/key.pem"
tr, e = NewTLSTransport(DockerAddr, certfile, cafile, keyfile)
if e != nil {
blog.Exit(e, "NewTLSTransport")
}
return
}
tr = &http.Transport{}
return
}
// doDockerAPI performs an HTTP GET,POST,DELETE operation to the Docker daemon.
func doDockerAPI(tr *http.Transport, operation, apipath string, jsonString []byte,
XRegistryAuth string) (resp []byte, e error) {
switch operation {
case "GET", "POST", "DELETE":
break
default:
e = errors.New("Operation " + operation + " not supported")
return
}
// for unix socket, URL (host.domain) is needed but can be anything
var host string
HTTP := "http://"
if DockerProto == "unix" {
host = dummydomain
} else {
host = DockerAddr
if DockerTLSVerify {
HTTP = "https://"
}
}
URL := HTTP + host + apipath
blog.Debug("doDockerAPI %s", URL)
req, e := http.NewRequest(operation, URL, bytes.NewBuffer(jsonString))
if e != nil {
blog.Error(e, ":doDockerAPI failed to create http request")
return
}
req.Header.Add("Content-Type", "application/json")
if XRegistryAuth != "" {
req.Header.Add("X-Registry-Auth", XRegistryAuth)
}
//req.Header.Set("Authorization", "Bearer "+authToken)
client := &http.Client{Transport: tr}
r, e := client.Do(req)
if e != nil {
blog.Error(e, ":doDockerAPI URL", URL, "client request failed")
return
}
defer r.Body.Close()
resp, e = ioutil.ReadAll(r.Body)
if e != nil {
blog.Error(e, ":doDockerAPI URL", URL, "invalid response body")
return
}
if r.StatusCode < 200 || r.StatusCode > 299 {
e = errors.New("doDockerAPI URL: " + URL + " status code: " + strconv.Itoa(r.StatusCode) +
"error: " + string(resp))
return
}
return
}
func dockerVersion() (major, minor, revision int, err error) {
apipath := "/version"
resp, err := doDockerAPI(DockerTransport, "GET", apipath, []byte{}, "")
if err != nil {
blog.Error(err, ": Error in Remote Docker API call: ", apipath)
return
}
var msg struct {
Version string
}
err = json.Unmarshal(resp, &msg)
if err != nil {
blog.Error(err, "unmarshal", string(resp))
return
}
version := msg.Version
blog.Info("Docker version %s", version)
arr := strings.Split(version, ".")
if len(arr) >= 1 {
major, err = strconv.Atoi(arr[0])
if err != nil {
blog.Error(err)
return
}
}
if len(arr) >= 2 {
minor, err = strconv.Atoi(arr[1])
if err != nil {
blog.Error(err)
return
}
}
if len(arr) >= 3 {
revision, err = strconv.Atoi(arr[2])
if err != nil {
blog.Error(err)
return
}
}
return
}
// createCmd returns a json byte slice desribing the container we want to create
func createCmd(imageID ImageIDType, scriptName, staticBinary, dirPath string) (jsonString []byte, err error) {
var container Container
container.User = "0"
container.AttachStdout = true
container.AttachStderr = true
container.HostConfig.Binds = []string{config.BANYANHOSTDIR() + "/hosttarget" + ":" + TARGETCONTAINERDIR + ":ro"}
container.Image = string(imageID)
container.Entrypoint = []string{TARGETCONTAINERDIR + "/bin/bash-static", "-c"}
container.Cmd = []string{"PATH=" + TARGETCONTAINERDIR + "/bin" + ":$PATH " + staticBinary + " " + dirPath + "/" + scriptName}
blog.Info("Executing command: docker %v", container.Cmd)
return json.Marshal(container)
}
// createContainer makes a docker remote API call to create a container
func createContainer(containerSpec []byte) (containerID string, err error) {
apipath := "/containers/create"
resp, err := doDockerAPI(DockerTransport, "POST", apipath, containerSpec, "")
if err != nil {
blog.Error(err, ": Error in Remote Docker API call: ", apipath, string(containerSpec))
return
}
blog.Debug("Response from docker remote API call for create: " + string(resp))
var msg struct {
Id string
Warnings string
}
err = json.Unmarshal(resp, &msg)
if err != nil {
blog.Error(err, "createContainer resp", string(resp))
return
}
blog.Info("Got ID %s Warnings %s\n", msg.Id, msg.Warnings)
containerID = msg.Id
return
}
// startContainer makes a docker remote API call to create a container
func startContainer(containerID string) (jsonOut []byte, err error) {
apipath := "/containers/" + containerID + "/start"
resp, err := doDockerAPI(DockerTransport, "POST", apipath, []byte{}, "")
if err != nil {
blog.Error(err, ": Error in Remote Docker API call: ", apipath)
return
}
blog.Debug("Response from docker remote API call for start: " + string(resp))
return
}
// waitContainer makes a docker remote API call to wait for a container to finish running
func waitContainer(containerID string) (statusCode int, err error) {
apipath := "/containers/" + containerID + "/wait"
resp, err := doDockerAPI(DockerTransport, "POST", apipath, []byte{}, "")
if err != nil {
blog.Error(err, ": Error in Remote Docker API call: ", apipath)
return
}
blog.Debug("Response from docker remote API call for wait: " + string(resp))
var msg struct {
StatusCode int
}
err = json.Unmarshal(resp, &msg)
if err != nil {
blog.Error(err, "waitContainer resp", string(resp))
return
}
blog.Info("Got StatusCode %d\n", msg.StatusCode)
statusCode = msg.StatusCode
return
}
// logsContainer makes a docker remote API call to get logs from a container
func logsContainer(containerID string) (output []byte, err error) {
apipath := "/containers/" + containerID + "/logs?stdout=1"
resp, err := doDockerAPI(DockerTransport, "GET", apipath, []byte{}, "")
if err != nil {
blog.Error(err, ": Error in Remote Docker API call: ", apipath)
return
}
blog.Debug("Response from docker remote API call for logs: " + string(resp))
for {
if len(resp) < 8 {
break
}
header := resp[0:8]
var size int32
buf := bytes.NewBuffer(header[4:8])
binary.Read(buf, binary.BigEndian, &size)
payload := resp[8:(8 + size)]
// blog.Info(string(frame))
resp = resp[(8 + size):]
if header[0] == uint8(1) {
// 1=stdout: return only the stdout log
output = append(output, payload...)
}
}
return
}
// removeContainer makes a docker remote API call to remove a container
func removeContainer(containerID string) (resp []byte, err error) {
apipath := "/containers/" + containerID
resp, err = doDockerAPI(DockerTransport, "DELETE", apipath, []byte{}, "")
if err != nil {
blog.Error(err)
return
}
blog.Debug("Response from docker remote API call for remove: " + string(resp))
return
}