/
command.go
244 lines (205 loc) · 6.41 KB
/
command.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
package agent
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log/syslog"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/jhunt/go-log"
"golang.org/x/crypto/ssh"
)
type Command struct {
Op string `json:"operation"`
TargetPlugin string `json:"target_plugin,omitempty"`
TargetEndpoint string `json:"target_endpoint,omitempty"`
StorePlugin string `json:"store_plugin,omitempty"`
StoreEndpoint string `json:"store_endpoint,omitempty"`
RestoreKey string `json:"restore_key,omitempty"`
EncryptType string `json:"encrypt_type,omitempty"`
EncryptKey string `json:"encrypt_key,omitempty"`
EncryptIV string `json:"encrypt_iv,omitempty"`
Compression string `json:"compression,omitempty"`
TaskUUID string `json:"task_uuid,omitempty"`
}
func ParseCommand(b []byte) (*Command, error) {
cmd := &Command{}
if err := json.Unmarshal(b, &cmd); err != nil {
return nil, fmt.Errorf("malformed agent command: %s\n", err)
}
switch cmd.Op {
case "":
return nil, fmt.Errorf("missing required 'operation' value in payload")
case "backup":
if cmd.TargetPlugin == "" {
return nil, fmt.Errorf("missing required 'target_plugin' value in payload")
}
if cmd.TargetEndpoint == "" {
return nil, fmt.Errorf("missing required 'target_endpoint' value in payload")
}
if cmd.StorePlugin == "" {
return nil, fmt.Errorf("missing required 'store_plugin' value in payload")
}
if cmd.StoreEndpoint == "" {
return nil, fmt.Errorf("missing required 'store_endpoint' value in payload")
}
case "restore", "shield-restore":
if cmd.TargetPlugin == "" {
return nil, fmt.Errorf("missing required 'target_plugin' value in payload")
}
if cmd.TargetEndpoint == "" {
return nil, fmt.Errorf("missing required 'target_endpoint' value in payload")
}
if cmd.StorePlugin == "" {
return nil, fmt.Errorf("missing required 'store_plugin' value in payload")
}
if cmd.StoreEndpoint == "" {
return nil, fmt.Errorf("missing required 'store_endpoint' value in payload")
}
if cmd.RestoreKey == "" {
return nil, fmt.Errorf("missing required 'restore_key' value in payload (for restore operation)")
}
case "purge":
if cmd.StorePlugin == "" {
return nil, fmt.Errorf("missing required 'store_plugin' value in payload")
}
if cmd.StoreEndpoint == "" {
return nil, fmt.Errorf("missing required 'store_endpoint' value in payload")
}
if cmd.RestoreKey == "" {
return nil, fmt.Errorf("missing required 'restore_key' value in payload (for purge operation)")
}
case "test-store":
if cmd.StorePlugin == "" {
return nil, fmt.Errorf("missing required 'store_plugin' value in payload")
}
if cmd.StoreEndpoint == "" {
return nil, fmt.Errorf("missing required 'store_endpoint' value in payload")
}
case "status":
/* nothing to validate */
default:
return nil, fmt.Errorf("unsupported operation: '%s'", cmd.Op)
}
return cmd, nil
}
func ParseCommandFromSSHRequest(r *ssh.Request) (*Command, error) {
var raw struct{ Value []byte }
if err := ssh.Unmarshal(r.Payload, &raw); err != nil {
return nil, err
}
return ParseCommand(raw.Value)
}
func (c *Command) Details() string {
switch c.Op {
case "backup":
return fmt.Sprintf("backup of target '%s' to store '%s' with task_uuid '%s'",
c.TargetPlugin, c.StorePlugin, c.TaskUUID)
case "restore":
return fmt.Sprintf("restore of [%s] from store '%s' to target '%s'",
c.RestoreKey, c.StorePlugin, c.TargetPlugin)
case "purge":
return fmt.Sprintf("purge of [%s] from store '%s'",
c.RestoreKey, c.StorePlugin)
default:
return fmt.Sprintf("%s op", c.Op)
}
}
func appendEndpointVariables(env []string, prefix, raw string) []string {
cooked := make(map[string]interface{})
err := json.Unmarshal([]byte(raw), &cooked)
if err != nil {
return env
}
re := regexp.MustCompile(`[^A-Z0-9]+`)
for k, v := range cooked {
k = re.ReplaceAllString(strings.ToUpper(k), "_")
env = append(env, fmt.Sprintf("%s%s=%v", prefix, k, v))
}
return env
}
func (agent *Agent) Execute(c *Command, out chan string) error {
cmd := exec.Command("shield-pipe")
log.Infof("Executing %s via shield-pipe", c.Details())
cmd.Env = []string{
fmt.Sprintf("HOME=%s", os.Getenv("HOME")),
fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
fmt.Sprintf("USER=%s", os.Getenv("USER")),
fmt.Sprintf("LANG=%s", os.Getenv("LANG")),
fmt.Sprintf("http_proxy=%s", os.Getenv("http_proxy")),
fmt.Sprintf("https_proxy=%s", os.Getenv("https_proxy")),
fmt.Sprintf("no_proxy=%s", os.Getenv("no_proxy")),
fmt.Sprintf("SHIELD_OP=%s", c.Op),
fmt.Sprintf("SHIELD_STORE_PLUGIN=%s", c.StorePlugin),
fmt.Sprintf("SHIELD_STORE_ENDPOINT=%s", c.StoreEndpoint),
fmt.Sprintf("SHIELD_TARGET_PLUGIN=%s", c.TargetPlugin),
fmt.Sprintf("SHIELD_TARGET_ENDPOINT=%s", c.TargetEndpoint),
fmt.Sprintf("SHIELD_TASK_UUID=%s", c.TaskUUID),
fmt.Sprintf("SHIELD_RESTORE_KEY=%s", c.RestoreKey),
fmt.Sprintf("SHIELD_PLUGINS_PATH=%s", strings.Join(agent.PluginPaths, ":")),
fmt.Sprintf("SHIELD_AGENT_NAME=%s", agent.Name),
fmt.Sprintf("SHIELD_AGENT_VERSION=%s", agent.Version),
fmt.Sprintf("SHIELD_ENCRYPT_TYPE=%s", c.EncryptType),
fmt.Sprintf("SHIELD_ENCRYPT_KEY=%s", c.EncryptKey),
fmt.Sprintf("SHIELD_ENCRYPT_IV=%s", c.EncryptIV),
fmt.Sprintf("SHIELD_COMPRESSION=%s", c.Compression),
}
cmd.Env = appendEndpointVariables(cmd.Env, "SHIELD_TARGET_PARAM_", c.TargetEndpoint)
cmd.Env = appendEndpointVariables(cmd.Env, "SHIELD_STORE_PARAM_", c.StoreEndpoint)
if log.LogLevel() == syslog.LOG_DEBUG {
cmd.Env = append(cmd.Env, "DEBUG=true")
}
log.Debugf("ENV: %s", strings.Join(cmd.Env, ","))
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
var wg sync.WaitGroup
drain := func(prefix string, out chan string, in io.Reader) {
defer wg.Done()
s := bufio.NewScanner(in)
for s.Scan() {
out <- fmt.Sprintf("%s:%s\n", prefix, s.Text())
}
}
wg.Add(2)
go drain("E", out, stderr)
go drain("O", out, stdout)
err = cmd.Start()
if err != nil {
close(out)
return err
}
wg.Wait()
close(out)
err = cmd.Wait()
if err != nil {
return err
}
return nil
}
func (agent *Agent) ResolvePathsIn(c *Command) error {
if c.TargetPlugin != "" {
bin, err := agent.ResolveBinary(c.TargetPlugin)
if err != nil {
return err
}
c.TargetPlugin = bin
}
if c.StorePlugin != "" {
bin, err := agent.ResolveBinary(c.StorePlugin)
if err != nil {
return err
}
c.StorePlugin = bin
}
return nil
}