/
agent.go
424 lines (365 loc) · 9.86 KB
/
agent.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package agent
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/guseggert/clustertest/agent/process"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"nhooyr.io/websocket"
)
// NodeAgent is an HTTP agent that runs on each node.
// The agent requires mTLS for both traffic encryption and authz.
type NodeAgent struct {
logger *zap.SugaredLogger
caCertPEM []byte
certPEM []byte
keyPEM []byte
heartbeatFailureHandler func()
heartbeatTimeout time.Duration
listenAddr string
httpServer *http.Server
commandServer *process.Server
closed chan struct{}
heartbeatMut sync.Mutex
lastHeartbeat time.Time
}
type Option func(n *NodeAgent)
func WithHeartbeatTimeout(d time.Duration) Option {
return func(n *NodeAgent) {
n.heartbeatTimeout = d
}
}
func WithHeartbeatFailureHandler(f func()) Option {
return func(n *NodeAgent) {
n.heartbeatFailureHandler = f
}
}
func WithListenAddr(s string) Option {
return func(n *NodeAgent) {
n.listenAddr = s
}
}
func WithLogger(l *zap.Logger) Option {
return func(n *NodeAgent) {
n.logger = l.Sugar()
}
}
func WithLogLevel(l zapcore.Level) Option {
return func(n *NodeAgent) {
n.logger = n.logger.WithOptions(zap.IncreaseLevel(l))
}
}
func HeartbeatFailureShutdown() {
fmt.Println("heartbeat failed, shutting down")
cmd := exec.Command("shutdown", "now")
err := cmd.Run()
if err != nil {
fmt.Printf("unable to shutdown host: %s", err)
}
}
func HeartbeatFailureExit() {
fmt.Println("heartbeat failed, exiting")
os.Exit(1)
}
// NewNodeAgent constructs a new host agent.
func NewNodeAgent(caCertPEM, certPEM, keyPEM []byte, opts ...Option) (*NodeAgent, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, fmt.Errorf("building logger: %w", err)
}
n := &NodeAgent{
logger: logger.Named("nodeagent").Sugar(),
commandServer: &process.Server{Log: logger.Named("command_server").Sugar()},
caCertPEM: caCertPEM,
certPEM: certPEM,
keyPEM: keyPEM,
heartbeatTimeout: 1 * time.Minute,
listenAddr: "0.0.0.0:8080",
}
for _, o := range opts {
o(n)
}
return n, nil
}
// startHeartbeatCheck starts a goroutine that checks for a heartbeat timeout and shuts down the node when a timeout occurs.
func (a *NodeAgent) startHeartbeatCheck() {
go func() {
a.heartbeatMut.Lock()
a.lastHeartbeat = time.Now()
a.heartbeatMut.Unlock()
for {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
select {
case <-a.closed:
return
case <-ticker.C:
}
a.heartbeatMut.Lock()
lastHeartbeat := a.lastHeartbeat
a.heartbeatMut.Unlock()
if lastHeartbeat.Add(a.heartbeatTimeout).Before(time.Now()) {
if a.heartbeatFailureHandler != nil {
a.heartbeatFailureHandler()
}
}
}
}()
}
func (a *NodeAgent) runHTTPServer() error {
tcpListener, err := net.Listen("tcp", a.listenAddr)
if err != nil {
return fmt.Errorf("listening TCP: %w", err)
}
tlsConfig, err := ServerTLSConfig(a.caCertPEM, a.certPEM, a.keyPEM)
if err != nil {
return fmt.Errorf("building server TLS config: %w", err)
}
tlsListener := tls.NewListener(tcpListener, tlsConfig)
router := httprouter.New()
router.GET("/heartbeat", a.heartbeat)
router.GET("/command", a.commandWS)
router.POST("/command", a.command)
router.POST("/file/*path", a.postFile)
router.GET("/file/*path", a.readFile)
router.GET("/connect/:network/:addr", a.connect)
router.POST("/fetch", a.fetch)
handler := a.logHandler(router)
server := http.Server{Handler: handler}
a.httpServer = &server
err = server.Serve(tlsListener)
if errors.Is(err, http.ErrServerClosed) {
a.logger.Info("server closed gracefully")
return nil
}
a.logger.Infof("server closed abnormally: %s", err)
return err
}
func (a *NodeAgent) logHandler(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
a.logger.Debugw(
"received request",
"Time", time.Now(),
"URL", r.URL,
"Method", r.Method,
)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// Run runs the node agent and returns once the node agent has stopped.
func (a *NodeAgent) Run() error {
a.startHeartbeatCheck()
return a.runHTTPServer()
}
type ConnectRequest struct {
Addr string
Network string
}
type FetchRequest struct {
URL string
Dest string
}
func (a *NodeAgent) fetch(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
var req FetchRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
f, err := os.Create(req.Dest)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
httpReq, err := http.NewRequestWithContext(r.Context(), http.MethodGet, req.URL, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// connect proxies traffic to a destination through the agent, via a WebSocket connection
func (a *NodeAgent) connect(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
network := params.ByName("network")
addr := params.ByName("addr")
wsConn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
CompressionMode: websocket.CompressionContextTakeover,
})
if err != nil {
a.logger.Debugf("connect WebSocket accept error: %s", err)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
remoteConn := websocket.NetConn(r.Context(), wsConn, websocket.MessageBinary)
localConn, err := net.DialTimeout(network, addr, 5*time.Second)
if err != nil {
a.logger.Debugf("connect dial error: %s", err)
remoteConn.Close()
return
}
go func() {
defer remoteConn.Close()
defer localConn.Close()
_, err := io.Copy(localConn, remoteConn)
if err != nil {
a.logger.Debugf("connect copy to local error: %s", err)
}
}()
_, err = io.Copy(remoteConn, localConn)
if err != nil {
a.logger.Debugf("connect copy to remote error: %s", err)
}
}
func (a *NodeAgent) postFile(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
path := params.ByName("path")
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0777)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
f, err := os.Create(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
_, err = io.Copy(f, r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (a *NodeAgent) readFile(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
path := params.ByName("path")
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "no such file or directory", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = io.Copy(w, f)
if err != nil {
a.logger.Debugf("error sending file response: %s", err)
}
}
func (a *NodeAgent) heartbeat(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
a.heartbeatMut.Lock()
lastHeartbeat := a.lastHeartbeat
a.lastHeartbeat = time.Now()
a.heartbeatMut.Unlock()
response := struct {
LastHeartbeat string
}{
LastHeartbeat: lastHeartbeat.UTC().Format(time.RFC3339),
}
b, err := json.Marshal(response)
if err != nil {
a.logger.Debugf("error marshaling heartbeat response: %s", err)
}
w.Header().Add("Content-Type", "application/json")
w.Write(b)
}
type PostCommandRequest struct {
Command string
Args []string
Stdin string
Env []string
WorkingDir string
}
type PostCommandResponse struct {
ExitCode int
TimeMS int64
Stdout string
Stderr string
}
func (a *NodeAgent) commandWS(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
a.commandServer.ServeHTTP(w, r)
}
// command is a simple command runner which takes a stdin buffer and sends all of stdout and stderr in the response.
// This is much easier to curl and write simple clients against, but doesn't support streaming input & output.
func (a *NodeAgent) command(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
var req PostCommandRequest
dec := json.NewDecoder(r.Body)
err := dec.Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if req.Command == "" {
http.Error(w, "request contained no command", http.StatusBadRequest)
return
}
cmd := exec.Command(req.Command, req.Args...)
if req.WorkingDir != "" {
cmd.Dir = req.WorkingDir
}
cmd.Env = append(cmd.Env, req.Env...)
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
cmd.Stderr = stderr
cmd.Stdout = stdout
if req.Stdin != "" {
cmd.Stdin = strings.NewReader(req.Stdin)
}
start := time.Now()
err = cmd.Start()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// If the request is aborted, kill the process.
// In the normal case, this is a no-op as the process will already be finished when the context is done.
go func() {
<-r.Context().Done()
cmd.Process.Kill()
}()
cmd.Wait()
exeTime := time.Since(start)
resp := PostCommandResponse{
ExitCode: cmd.ProcessState.ExitCode(),
TimeMS: exeTime.Milliseconds(),
Stdout: stdout.String(),
Stderr: stderr.String(),
}
b, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(b)
}
func (a *NodeAgent) Stop() error {
return a.httpServer.Close()
}