-
Notifications
You must be signed in to change notification settings - Fork 42
/
client.go
191 lines (159 loc) · 4.89 KB
/
client.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
// Copyright 2017-2023 Block, Inc.
// Package rce provides a gRPC-based Remote Code Execution client and server.
// The server (or "agent") runs on a remote host and executes a whitelist of
// shell commands specified in a config file. The client calls the server to
// execute whitelist commands. Commands from different clients run concurrently;
// there are no safeguards against conflicting or incompatible commands.
package rce
import (
"crypto/tls"
"io"
"time"
"github.com/square/rce-agent/pb"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
var (
// ConnectTimeout describes the total timeout for establishing a client
// connection to the rceagent server.
ConnectTimeout = time.Duration(10) * time.Second
// ConnectBackoffMaxDelay configures the dialer to use the
// provided maximum delay when backing off after
// failed connection attempts.
ConnectBackoffMaxDelay = time.Duration(2) * time.Second
// KeepaliveTime is the interval at which the client sends keepalive
// probes to the server.
KeepaliveTime = time.Duration(30) * time.Second
// KeepaliveTimeout is the amount of time the client waits to receive
// a response from the server after a keepalive probe.
KeepaliveTimeout = time.Duration(20) * time.Second
)
// A Client calls a remote agent (server) to execute commands.
type Client interface {
// Connect to a remote agent.
Open(host, port string) error
// Close connection to a remote agent.
Close() error
// Return hostname and port of remote agent, if connected.
AgentAddr() (string, string)
// Start a command on the remote agent. Must be connected first by calling
// Connect. This call is non-blocking. It returns the ID of the command or
// an error.
Start(cmdName string, args []string) (id string, err error)
// Wait for a command on the remote agent. This call blocks until the command
// completes. It returns the final statue of the command or an error.
Wait(id string) (*pb.Status, error)
// Get the status of a running command. This is safe to call by multiple
// goroutines. ErrNotFound is returned if Wait or Stop has already been
// called.
GetStatus(id string) (*pb.Status, error)
// Stop a running command. ErrNotFound is returne if Wait or Stop has already
// been called.
Stop(id string) error
// Return a list of all running command IDs.
Running() ([]string, error)
}
type client struct {
host string
port string
conn *grpc.ClientConn
agent pb.RCEAgentClient
tlsConfig *tls.Config
}
// NewClient makes a new Client.
func NewClient(tlsConfig *tls.Config) Client {
return &client{tlsConfig: tlsConfig}
}
func (c *client) Open(host, port string) error {
var opt grpc.DialOption
if c.tlsConfig == nil {
opt = grpc.WithInsecure()
} else {
creds := credentials.NewTLS(c.tlsConfig)
err := creds.OverrideServerName(host)
if err != nil {
return err
}
opt = grpc.WithTransportCredentials(creds)
}
conn, err := grpc.Dial(
host+":"+port,
opt, // insecure or with TLS
// Block = actually connect. Timeout = max time to retry on failure
// (no option to set retry count). Backoff delay = time between retries,
// up to Timeout.
grpc.WithBlock(),
grpc.WithTimeout(ConnectTimeout),
grpc.WithBackoffMaxDelay(ConnectBackoffMaxDelay),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: KeepaliveTime,
Timeout: KeepaliveTimeout,
}),
)
if err != nil {
return err
}
c.conn = conn
c.agent = pb.NewRCEAgentClient(conn)
c.host = host
c.port = port
return nil
}
func (c *client) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}
func (c *client) AgentAddr() (string, string) {
return c.host, c.port
}
func (c *client) Start(cmdName string, args []string) (string, error) {
cmd := &pb.Command{
Name: cmdName,
Arguments: args,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
id, err := c.agent.Start(ctx, cmd)
if err != nil {
return "", err
}
return id.ID, nil
}
func (c *client) Wait(id string) (*pb.Status, error) {
return c.agent.Wait(context.TODO(), &pb.ID{ID: id})
}
func (c *client) GetStatus(id string) (*pb.Status, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return c.agent.GetStatus(ctx, &pb.ID{ID: id})
}
func (c *client) Stop(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := c.agent.Stop(ctx, &pb.ID{ID: id})
return err
}
func (c *client) Running() ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
stream, err := c.agent.Running(ctx, &pb.Empty{})
if err != nil {
return nil, err
}
ids := []string{}
for {
id, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
ids = append(ids, id.ID)
}
return ids, nil
}