-
Notifications
You must be signed in to change notification settings - Fork 142
/
main.go
510 lines (430 loc) · 12.8 KB
/
main.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/cloudflare/redoctober/client"
"github.com/cloudflare/redoctober/cmd/ro/gopass"
"github.com/cloudflare/redoctober/cmd/ro/roagent"
"github.com/cloudflare/redoctober/core"
"github.com/cloudflare/redoctober/cryptor"
"github.com/cloudflare/redoctober/msp"
"github.com/cloudflare/redoctober/order"
"golang.org/x/crypto/ssh"
)
var action, user, pswd, userEnv, pswdEnv, server, caPath, pubKeyPath string
var owners, lefters, righters, inPath, labels, usages, outPath, outEnv string
var uses, minUsers int
var duration, users, userType, hcName string
var pollInterval time.Duration
type command struct {
Run func()
Desc string
}
var roServer *client.RemoteServer
var commandSet = map[string]command{
"create": command{Run: runCreate, Desc: "create the disk vault and admin account"},
"create-user": command{Run: runCreateUser, Desc: "create a user account"},
"summary": command{Run: runSummary, Desc: "list the user and delegation summary"},
"delegate": command{Run: runDelegate, Desc: "do decryption delegation"},
"encrypt": command{Run: runEncrypt, Desc: "encrypt a file"},
"decrypt": command{Run: runDecrypt, Desc: "decrypt a file"},
"ssh-agent": command{Run: runSSHAgent, Desc: "act as an SSH agent"},
"ssh-add": command{Run: runSSHAdd, Desc: "act as ssh-add"},
"re-encrypt": command{Run: runReEncrypt, Desc: "re-encrypt a file"},
"order": command{Run: runOrder, Desc: "place an order for delegations"},
"owners": command{Run: runOwner, Desc: "show owners list"},
"status": command{Run: runStatus, Desc: "show Red October persistent delegation state"},
"restore": command{Run: runRestore, Desc: "perform a restore delegation"},
"reset-persisted": command{Run: runResetPersisted, Desc: "reset the persisted delegations"},
}
func registerFlags() {
flag.StringVar(&server, "server", "localhost:8080", "server address")
flag.StringVar(&caPath, "ca", "", "ca file path")
flag.StringVar(&owners, "owners", "", "comma separated owner list")
flag.StringVar(&users, "users", "", "comma separated user list")
flag.IntVar(&uses, "uses", 0, "number of delegated key uses")
flag.IntVar(&minUsers, "minUsers", 2, "minimum number of delegations")
flag.StringVar(&duration, "time", "0h", "duration of delegated key uses")
flag.StringVar(&lefters, "left", "", "comma separated left owners")
flag.StringVar(&righters, "right", "", "comma separated right owners")
flag.StringVar(&labels, "labels", "", "comma separated labels")
flag.StringVar(&usages, "usages", "", "comma separated usages")
flag.StringVar(&inPath, "in", "", "input data file")
flag.StringVar(&outPath, "out", "", "output data file")
flag.StringVar(&outEnv, "outenv", "", "env variable for output data")
flag.StringVar(&user, "user", "", "username")
flag.StringVar(&pswd, "password", "", "password")
flag.StringVar(&userType, "userType", "rsa", "user key type: ecc or rsa")
flag.StringVar(&hcName, "hipchat-name", "", "hipchat name for user, used for notifications")
flag.StringVar(&userEnv, "userenv", "RO_USER", "env variable for user name")
flag.StringVar(&pswdEnv, "pswdenv", "RO_PASS", "env variable for user password")
flag.DurationVar(&pollInterval, "poll-interval", time.Second, "interval for polling an outstanding order (set 0 to disable polling)")
flag.StringVar(&pubKeyPath, "pubkey", "id_rsa.pub", "path to SSH public key")
}
func readLine(prompt string) (line string, err error) {
fmt.Fprintf(os.Stderr, prompt)
rd := bufio.NewReader(os.Stdin)
line, err = rd.ReadString('\n')
if err != nil {
return
}
line = strings.TrimSpace(line)
return
}
func getUserCredentials() {
var err error
if user == "" {
user = os.Getenv(userEnv)
}
if pswd == "" {
pswd = os.Getenv(pswdEnv)
}
if user == "" {
user, err = readLine("Username: ")
processError("error", err)
}
if pswd == "" {
pswd, err = gopass.GetPass("Password: ")
processError("error", err)
}
}
func processError(msg string, err error) {
if err != nil {
log.Fatalln(msg, ":", err)
}
}
func processCSL(s string) []string {
if s == "" {
return nil
}
return strings.Split(s, ",")
}
func runCreate() {
req := core.CreateRequest{
Name: user,
Password: pswd,
}
resp, err := roServer.Create(req)
processError("error", err)
fmt.Println(resp.Status)
}
func runCreateUser() {
req := core.CreateUserRequest{
Name: user,
Password: pswd,
UserType: userType,
HipchatName: hcName,
}
resp, err := roServer.CreateUser(req)
processError("error", err)
fmt.Println(resp.Status)
}
func runDelegate() {
req := core.DelegateRequest{
Name: user,
Password: pswd,
Uses: uses,
Time: duration,
Users: processCSL(users),
Labels: processCSL(labels),
}
resp, err := roServer.Delegate(req)
processError("error", err)
fmt.Println(resp.Status)
}
func runRestore() {
req := core.DelegateRequest{
Name: user,
Password: pswd,
Uses: uses,
Time: duration,
}
resp, err := roServer.Restore(req)
processError("error", err)
if resp.Status != "ok" {
fmt.Fprintf(os.Stderr, "failed: %s\n", resp.Status)
os.Exit(1)
}
var st core.StatusData
err = json.Unmarshal(resp.Response, &st)
processError("error", err)
fmt.Println("Restore delegation complete; persistence is now", st.Status)
}
// TODO: summary response needs better formatting
func runSummary() {
req := core.SummaryRequest{
Name: user,
Password: pswd,
}
resp, err := roServer.Summary(req)
processError("error", err)
fmt.Println(resp)
}
func runEncrypt() {
inBytes, err := ioutil.ReadFile(inPath)
processError("error", err)
req := core.EncryptRequest{
Name: user,
Password: pswd,
Minimum: minUsers,
Owners: processCSL(owners),
Usages: processCSL(usages),
LeftOwners: processCSL(lefters),
RightOwners: processCSL(righters),
Labels: processCSL(labels),
Data: inBytes,
}
resp, err := roServer.Encrypt(req)
processError("error", err)
if resp.Status != "ok" {
log.Fatalln("response status error", resp.Status)
return
}
fmt.Println("Response Status:", resp.Status)
outBytes := []byte(base64.StdEncoding.EncodeToString(resp.Response))
ioutil.WriteFile(outPath, outBytes, 0644)
}
func runReEncrypt() {
inBytes, err := ioutil.ReadFile(inPath)
processError("error", err)
// base64 decode the input
encBytes, err := base64.StdEncoding.DecodeString(string(inBytes))
if err != nil {
log.Println("fail to base64 decode the data, proceed with raw data")
encBytes = inBytes
}
req := core.ReEncryptRequest{
Name: user,
Password: pswd,
Owners: processCSL(owners),
Minimum: minUsers,
LeftOwners: processCSL(lefters),
RightOwners: processCSL(righters),
Labels: processCSL(labels),
Data: encBytes,
}
resp, err := roServer.ReEncrypt(req)
processError("error", err)
if resp.Status != "ok" {
log.Fatalln("response status error", resp.Status)
return
}
fmt.Println("Response Status:", resp.Status)
outBytes := []byte(base64.StdEncoding.EncodeToString(resp.Response))
ioutil.WriteFile(outPath, outBytes, 0644)
}
func runDecrypt() {
inBytes, err := ioutil.ReadFile(inPath)
processError("error", err)
// base64 decode the input
encBytes, err := base64.StdEncoding.DecodeString(string(inBytes))
if err != nil {
log.Println("fail to base64 decode the data, proceed with raw data")
encBytes = inBytes
}
req := core.DecryptRequest{
Name: user,
Password: pswd,
Data: encBytes,
}
resp, err := roServer.Decrypt(req)
if err != nil {
switch err.Error() {
case cryptor.ErrNotEnoughDelegations.Error(),
msp.ErrNotEnoughShares.Error(),
"need more delegated keys":
// retry forever unless keyboard interrupt
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
for i := 0; i < 20; i++ {
resp, err = roServer.Decrypt(req)
if err == nil {
break
}
log.Println("retry after 30 seconds due to error: ", err)
select {
case <-sigChan:
log.Fatalln("process is interrupted")
return
case <-time.After(30 * time.Second):
}
}
default:
processError("error", err)
}
}
if resp == nil {
log.Fatalln("response status error", resp.Status)
}
fmt.Println("Response Status:", resp.Status)
var msg core.DecryptWithDelegates
err = json.Unmarshal(resp.Response, &msg)
processError("error", err)
fmt.Println("Secure:", msg.Secure)
fmt.Println("Delegates:", msg.Delegates)
ioutil.WriteFile(outPath, msg.Data, 0644)
}
func runOrder() {
req := core.OrderRequest{
Name: user,
Password: pswd,
Uses: uses,
Duration: duration,
Labels: processCSL(labels),
Users: processCSL(users),
}
resp, err := roServer.Order(req)
processError("error", err)
var o order.Order
err = json.Unmarshal(resp.Response, &o)
processError("error", err)
if pollInterval > 0 {
for o.Delegated < 2 {
time.Sleep(pollInterval)
resp, err = roServer.OrderInfo(core.OrderInfoRequest{Name: user, Password: pswd, OrderNum: o.Num})
processError("error", err)
err = json.Unmarshal(resp.Response, &o)
processError("error", err)
}
}
fmt.Println(resp.Status)
}
func runOwner() {
inBytes, err := ioutil.ReadFile(inPath)
processError("error", err)
// attempt to base64 decode the input file
base64decoded, err := base64.StdEncoding.DecodeString(string(inBytes))
if err == nil {
inBytes = base64decoded
}
req := core.OwnersRequest{
Data: inBytes,
}
resp, err := roServer.Owners(req)
processError("error", err)
fmt.Println(resp.Status)
fmt.Println(resp)
}
func runStatus() {
req := core.StatusRequest{
Name: user,
Password: pswd,
}
resp, err := roServer.Status(req)
processError("error", err)
fmt.Println(resp.Status)
fmt.Println(resp)
}
func runResetPersisted() {
req := core.PurgeRequest{
Name: user,
Password: pswd,
}
resp, err := roServer.ResetPersisted(req)
processError("error", err)
fmt.Println(resp.Status)
fmt.Println(resp)
}
func runSSHAgent() {
log.Println("Starting Red October Secret Shell Agent")
// Prepare a socket
dir, err := ioutil.TempDir("", "ro_ssh_")
processError("error making a temporary directory", err)
authSockPath := path.Join(dir, "roagent.sock")
os.Setenv("SSH_AUTH_SOCK", authSockPath)
fmt.Printf("export SSH_AUTH_SOCK=%s\n", authSockPath)
socket := net.UnixAddr{Net: "unix", Name: authSockPath}
ear, err := net.ListenUnix("unix", &socket)
processError("error making a unix socket", err)
// Make an agent
roAgent := roagent.NewROAgent(roServer, user, pswd)
// Process the arguments
if inPath != "" && pubKeyPath != "" {
go runSSHAdd()
}
// Prepare for signal interception
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func(ear net.Listener, c chan os.Signal) {
sig := <-c
log.Printf("Caught signal %s: shutting down.", sig)
os.RemoveAll(path.Dir(authSockPath))
ear.Close()
os.Exit(0)
}(ear, sigChan)
// Serve the agent
for {
conn, err := ear.AcceptUnix()
processError("error accepting socket connection", err)
go roagent.ServeAgent(roAgent, conn)
}
}
func runSSHAdd() {
// Process the arguments
inBytes, err := ioutil.ReadFile(inPath)
processError("error reading encrypted data", err)
encBytes, err := base64.StdEncoding.DecodeString(string(inBytes))
if err != nil {
log.Println("error base64-decoding the data, proceeding with raw data")
encBytes = inBytes
}
inBytes, err = ioutil.ReadFile(pubKeyPath)
processError("error reading public key", err)
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(inBytes)
processError("error parsing public key", err)
// Prepare a socket
authSockPath := os.Getenv("SSH_AUTH_SOCK")
socket := net.UnixAddr{Net: "unix", Name: authSockPath}
mouth, err := net.DialUnix("unix", nil, &socket)
processError("error connecting to unix socket", err)
// Contact the agent
newROAgent := roagent.NewClient(mouth)
rosigner := roagent.NewROSigner(pubKey, encBytes)
err = newROAgent.Add(roagent.AddedKey{
PrivateKey: rosigner,
})
processError("failed to add new signer to the ROAgent", err)
fmt.Println("New signer was added to the ROAgent")
}
func main() {
flag.Usage = func() {
fmt.Println("Usage: ro [options] subcommand")
fmt.Println("Currently supported subcommands are:")
for key := range commandSet {
fmt.Println("\t", key, ":", commandSet[key].Desc)
}
fmt.Println("Options:")
flag.PrintDefaults()
}
registerFlags()
flag.Parse()
action := flag.Arg(0)
if flag.NArg() != 1 && action != "ssh" {
flag.Usage()
os.Exit(1)
}
cmd, found := commandSet[action]
if !found {
fmt.Println("Unsupported subcommand:", action)
flag.Usage()
os.Exit(1)
} else {
var err error
roServer, err = client.NewRemoteServer(server, caPath)
processError("error", err)
getUserCredentials()
cmd.Run()
}
}