| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"encoding/json" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"io/ioutil" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"os/signal" |
| 12 |
"strings" |
| 13 |
"syscall" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/cloudflare/redoctober/client" |
| 17 |
"github.com/cloudflare/redoctober/cmd/ro/gopass" |
| 18 |
"github.com/cloudflare/redoctober/core" |
| 19 |
"github.com/cloudflare/redoctober/cryptor" |
| 20 |
"github.com/cloudflare/redoctober/msp" |
| 21 |
"github.com/cloudflare/redoctober/order" |
| 22 |
) |
| 23 |
|
| 24 |
var action, user, pswd, userEnv, pswdEnv, server, caPath string |
| 25 |
|
| 26 |
var owners, lefters, righters, inPath, labels, outPath, outEnv string |
| 27 |
|
| 28 |
var uses, minUsers int |
| 29 |
|
| 30 |
var duration, users, userType, hcName string |
| 31 |
|
| 32 |
var pollInterval time.Duration |
| 33 |
|
| 34 |
type command struct { |
| 35 |
Run func() |
| 36 |
Desc string |
| 37 |
} |
| 38 |
|
| 39 |
var roServer *client.RemoteServer |
| 40 |
|
| 41 |
var commandSet = map[string]command{ |
| 42 |
"create": command{Run: runCreate, Desc: "create the disk vault and admin account"}, |
| 43 |
"create-user": command{Run: runCreateUser, Desc: "create a user account"}, |
| 44 |
"summary": command{Run: runSummary, Desc: "list the user and delegation summary"}, |
| 45 |
"delegate": command{Run: runDelegate, Desc: "do decryption delegation"}, |
| 46 |
"encrypt": command{Run: runEncrypt, Desc: "encrypt a file"}, |
| 47 |
"decrypt": command{Run: runDecrypt, Desc: "decrypt a file"}, |
| 48 |
"re-encrypt": command{Run: runReEncrypt, Desc: "re-encrypt a file"}, |
| 49 |
"order": command{Run: runOrder, Desc: "place an order for delegations"}, |
| 50 |
"owners": command{Run: runOwner, Desc: "show owners list"}, |
| 51 |
"status": command{Run: runStatus, Desc: "show Red October persistent delegation state"}, |
| 52 |
"restore": command{Run: runRestore, Desc: "perform a restore delegation"}, |
| 53 |
"reset-persisted": command{Run: runResetPersisted, Desc: "reset the persisted delegations"}, |
| 54 |
} |
| 55 |
|
| 56 |
func registerFlags() { |
| 57 |
flag.StringVar(&server, "server", "localhost:8080", "server address") |
| 58 |
flag.StringVar(&caPath, "ca", "", "ca file path") |
| 59 |
flag.StringVar(&owners, "owners", "", "comma separated owner list") |
| 60 |
flag.StringVar(&users, "users", "", "comma separated user list") |
| 61 |
flag.IntVar(&uses, "uses", 0, "number of delegated key uses") |
| 62 |
flag.IntVar(&minUsers, "minUsers", 2, "minimum number of delegations") |
| 63 |
flag.StringVar(&duration, "time", "0h", "duration of delegated key uses") |
| 64 |
flag.StringVar(&lefters, "left", "", "comma separated left owners") |
| 65 |
flag.StringVar(&righters, "right", "", "comma separated right owners") |
| 66 |
flag.StringVar(&labels, "labels", "", "comma separated labels") |
| 67 |
flag.StringVar(&inPath, "in", "", "input data file") |
| 68 |
flag.StringVar(&outPath, "out", "", "output data file") |
| 69 |
flag.StringVar(&outEnv, "outenv", "", "env variable for output data") |
| 70 |
flag.StringVar(&user, "user", "", "username") |
| 71 |
flag.StringVar(&pswd, "password", "", "password") |
| 72 |
flag.StringVar(&userType, "userType", "rsa", "user key type: ecc or rsa") |
| 73 |
flag.StringVar(&hcName, "hipchat-name", "", "hipchat name for user, used for notifications") |
| 74 |
flag.StringVar(&userEnv, "userenv", "RO_USER", "env variable for user name") |
| 75 |
flag.StringVar(&pswdEnv, "pswdenv", "RO_PASS", "env variable for user password") |
| 76 |
flag.DurationVar(&pollInterval, "poll-interval", time.Second, "interval for polling an outstanding order (set 0 to disable polling)") |
| 77 |
} |
| 78 |
|
| 79 |
func getUserCredentials() { |
| 80 |
user = os.Getenv(userEnv) |
| 81 |
pswd = os.Getenv(pswdEnv) |
| 82 |
if user == "" || pswd == "" { |
| 83 |
fmt.Print("Username:") |
| 84 |
fmt.Scan(&user) |
| 85 |
var err error |
| 86 |
pswd, err = gopass.GetPass("Password:") |
| 87 |
processError(err) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func processError(err error) { |
| 92 |
if err != nil { |
| 93 |
log.Fatal("error:", err) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func processCSL(s string) []string { |
| 98 |
if s == "" { |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
return strings.Split(s, ",") |
| 103 |
} |
| 104 |
|
| 105 |
func runCreate() { |
| 106 |
req := core.CreateRequest{ |
| 107 |
Name: user, |
| 108 |
Password: pswd, |
| 109 |
} |
| 110 |
resp, err := roServer.Create(req) |
| 111 |
processError(err) |
| 112 |
fmt.Println(resp.Status) |
| 113 |
} |
| 114 |
|
| 115 |
func runCreateUser() { |
| 116 |
req := core.CreateUserRequest{ |
| 117 |
Name: user, |
| 118 |
Password: pswd, |
| 119 |
UserType: userType, |
| 120 |
HipchatName: hcName, |
| 121 |
} |
| 122 |
resp, err := roServer.CreateUser(req) |
| 123 |
processError(err) |
| 124 |
fmt.Println(resp.Status) |
| 125 |
} |
| 126 |
|
| 127 |
func runDelegate() { |
| 128 |
req := core.DelegateRequest{ |
| 129 |
Name: user, |
| 130 |
Password: pswd, |
| 131 |
Uses: uses, |
| 132 |
Time: duration, |
| 133 |
Users: processCSL(users), |
| 134 |
Labels: processCSL(labels), |
| 135 |
} |
| 136 |
resp, err := roServer.Delegate(req) |
| 137 |
processError(err) |
| 138 |
fmt.Println(resp.Status) |
| 139 |
} |
| 140 |
|
| 141 |
func runRestore() { |
| 142 |
req := core.DelegateRequest{ |
| 143 |
Name: user, |
| 144 |
Password: pswd, |
| 145 |
Uses: uses, |
| 146 |
Time: duration, |
| 147 |
} |
| 148 |
|
| 149 |
resp, err := roServer.Restore(req) |
| 150 |
processError(err) |
| 151 |
|
| 152 |
if resp.Status != "ok" { |
| 153 |
fmt.Fprintf(os.Stderr, "failed: %s\n", resp.Status) |
| 154 |
os.Exit(1) |
| 155 |
} |
| 156 |
|
| 157 |
var st core.StatusData |
| 158 |
err = json.Unmarshal(resp.Response, &st) |
| 159 |
processError(err) |
| 160 |
|
| 161 |
fmt.Println("Restore delegation complete; persistence is now", st.Status) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
func runSummary() { |
| 166 |
req := core.SummaryRequest{ |
| 167 |
Name: user, |
| 168 |
Password: pswd, |
| 169 |
} |
| 170 |
resp, err := roServer.Summary(req) |
| 171 |
processError(err) |
| 172 |
fmt.Println(resp) |
| 173 |
} |
| 174 |
|
| 175 |
func runEncrypt() { |
| 176 |
inBytes, err := ioutil.ReadFile(inPath) |
| 177 |
processError(err) |
| 178 |
req := core.EncryptRequest{ |
| 179 |
Name: user, |
| 180 |
Password: pswd, |
| 181 |
Minimum: minUsers, |
| 182 |
Owners: processCSL(owners), |
| 183 |
LeftOwners: processCSL(lefters), |
| 184 |
RightOwners: processCSL(righters), |
| 185 |
Labels: processCSL(labels), |
| 186 |
Data: inBytes, |
| 187 |
} |
| 188 |
|
| 189 |
resp, err := roServer.Encrypt(req) |
| 190 |
processError(err) |
| 191 |
if resp.Status != "ok" { |
| 192 |
log.Fatal("response status error:", resp.Status) |
| 193 |
return |
| 194 |
} |
| 195 |
fmt.Println("Response Status:", resp.Status) |
| 196 |
outBytes := []byte(base64.StdEncoding.EncodeToString(resp.Response)) |
| 197 |
ioutil.WriteFile(outPath, outBytes, 0644) |
| 198 |
} |
| 199 |
|
| 200 |
func runReEncrypt() { |
| 201 |
inBytes, err := ioutil.ReadFile(inPath) |
| 202 |
processError(err) |
| 203 |
|
| 204 |
|
| 205 |
encBytes, err := base64.StdEncoding.DecodeString(string(inBytes)) |
| 206 |
if err != nil { |
| 207 |
log.Println("fail to base64 decode the data, proceed with raw data") |
| 208 |
encBytes = inBytes |
| 209 |
} |
| 210 |
|
| 211 |
req := core.ReEncryptRequest{ |
| 212 |
Name: user, |
| 213 |
Password: pswd, |
| 214 |
Owners: processCSL(owners), |
| 215 |
Minimum: minUsers, |
| 216 |
LeftOwners: processCSL(lefters), |
| 217 |
RightOwners: processCSL(righters), |
| 218 |
Labels: processCSL(labels), |
| 219 |
Data: encBytes, |
| 220 |
} |
| 221 |
|
| 222 |
resp, err := roServer.ReEncrypt(req) |
| 223 |
processError(err) |
| 224 |
if resp.Status != "ok" { |
| 225 |
log.Fatal("response status error:", resp.Status) |
| 226 |
return |
| 227 |
} |
| 228 |
fmt.Println("Response Status:", resp.Status) |
| 229 |
outBytes := []byte(base64.StdEncoding.EncodeToString(resp.Response)) |
| 230 |
ioutil.WriteFile(outPath, outBytes, 0644) |
| 231 |
} |
| 232 |
|
| 233 |
func runDecrypt() { |
| 234 |
inBytes, err := ioutil.ReadFile(inPath) |
| 235 |
processError(err) |
| 236 |
|
| 237 |
|
| 238 |
encBytes, err := base64.StdEncoding.DecodeString(string(inBytes)) |
| 239 |
if err != nil { |
| 240 |
log.Println("fail to base64 decode the data, proceed with raw data") |
| 241 |
encBytes = inBytes |
| 242 |
} |
| 243 |
|
| 244 |
req := core.DecryptRequest{ |
| 245 |
Name: user, |
| 246 |
Password: pswd, |
| 247 |
Data: encBytes, |
| 248 |
} |
| 249 |
|
| 250 |
resp, err := roServer.Decrypt(req) |
| 251 |
if err != nil { |
| 252 |
switch err.Error() { |
| 253 |
case cryptor.ErrNotEnoughDelegations.Error(), |
| 254 |
msp.ErrNotEnoughShares.Error(), |
| 255 |
"Need more delegated keys": |
| 256 |
|
| 257 |
sigChan := make(chan os.Signal, 1) |
| 258 |
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 259 |
|
| 260 |
for i := 0; i < 20; i++ { |
| 261 |
resp, err = roServer.Decrypt(req) |
| 262 |
if err == nil { |
| 263 |
break |
| 264 |
} |
| 265 |
log.Println("retry after 30 seconds due to error: ", err) |
| 266 |
|
| 267 |
select { |
| 268 |
case <-sigChan: |
| 269 |
log.Fatal("process is interrupted") |
| 270 |
return |
| 271 |
|
| 272 |
case <-time.After(30 * time.Second): |
| 273 |
} |
| 274 |
} |
| 275 |
default: |
| 276 |
processError(err) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if resp == nil { |
| 281 |
log.Fatal("response status error:", resp.Status) |
| 282 |
} |
| 283 |
|
| 284 |
fmt.Println("Response Status:", resp.Status) |
| 285 |
var msg core.DecryptWithDelegates |
| 286 |
err = json.Unmarshal(resp.Response, &msg) |
| 287 |
processError(err) |
| 288 |
fmt.Println("Secure:", msg.Secure) |
| 289 |
fmt.Println("Delegates:", msg.Delegates) |
| 290 |
ioutil.WriteFile(outPath, msg.Data, 0644) |
| 291 |
} |
| 292 |
|
| 293 |
func runOrder() { |
| 294 |
req := core.OrderRequest{ |
| 295 |
Name: user, |
| 296 |
Password: pswd, |
| 297 |
Uses: uses, |
| 298 |
Duration: duration, |
| 299 |
Labels: processCSL(labels), |
| 300 |
Users: processCSL(users), |
| 301 |
} |
| 302 |
resp, err := roServer.Order(req) |
| 303 |
processError(err) |
| 304 |
|
| 305 |
var o order.Order |
| 306 |
err = json.Unmarshal(resp.Response, &o) |
| 307 |
processError(err) |
| 308 |
|
| 309 |
if pollInterval > 0 { |
| 310 |
for o.Delegated < 2 { |
| 311 |
time.Sleep(pollInterval) |
| 312 |
resp, err = roServer.OrderInfo(core.OrderInfoRequest{Name: user, Password: pswd, OrderNum: o.Num}) |
| 313 |
processError(err) |
| 314 |
err = json.Unmarshal(resp.Response, &o) |
| 315 |
processError(err) |
| 316 |
} |
| 317 |
} |
| 318 |
fmt.Println(resp.Status) |
| 319 |
} |
| 320 |
|
| 321 |
func runOwner() { |
| 322 |
inBytes, err := ioutil.ReadFile(inPath) |
| 323 |
processError(err) |
| 324 |
|
| 325 |
req := core.OwnersRequest{ |
| 326 |
Data: inBytes, |
| 327 |
} |
| 328 |
|
| 329 |
resp, err := roServer.Owners(req) |
| 330 |
processError(err) |
| 331 |
|
| 332 |
fmt.Println(resp.Status) |
| 333 |
fmt.Println(resp) |
| 334 |
} |
| 335 |
|
| 336 |
func runStatus() { |
| 337 |
req := core.StatusRequest{ |
| 338 |
Name: user, |
| 339 |
Password: pswd, |
| 340 |
} |
| 341 |
|
| 342 |
resp, err := roServer.Status(req) |
| 343 |
processError(err) |
| 344 |
|
| 345 |
fmt.Println(resp.Status) |
| 346 |
fmt.Println(resp) |
| 347 |
} |
| 348 |
|
| 349 |
func runResetPersisted() { |
| 350 |
req := core.PurgeRequest{ |
| 351 |
Name: user, |
| 352 |
Password: pswd, |
| 353 |
} |
| 354 |
|
| 355 |
resp, err := roServer.ResetPersisted(req) |
| 356 |
processError(err) |
| 357 |
|
| 358 |
fmt.Println(resp.Status) |
| 359 |
fmt.Println(resp) |
| 360 |
} |
| 361 |
|
| 362 |
func main() { |
| 363 |
flag.Usage = func() { |
| 364 |
fmt.Println("Usage: ro [options] subcommand") |
| 365 |
fmt.Println("Currently supported subcommands are:") |
| 366 |
for key := range commandSet { |
| 367 |
fmt.Println("\t", key, ":", commandSet[key].Desc) |
| 368 |
} |
| 369 |
|
| 370 |
fmt.Println("Options:") |
| 371 |
flag.PrintDefaults() |
| 372 |
} |
| 373 |
|
| 374 |
registerFlags() |
| 375 |
flag.Parse() |
| 376 |
|
| 377 |
if flag.NArg() != 1 { |
| 378 |
flag.Usage() |
| 379 |
os.Exit(1) |
| 380 |
} |
| 381 |
|
| 382 |
action := flag.Arg(0) |
| 383 |
|
| 384 |
cmd, found := commandSet[action] |
| 385 |
if !found { |
| 386 |
fmt.Println("Unsupported subcommand:", action) |
| 387 |
flag.Usage() |
| 388 |
os.Exit(1) |
| 389 |
} else { |
| 390 |
var err error |
| 391 |
roServer, err = client.NewRemoteServer(server, caPath) |
| 392 |
processError(err) |
| 393 |
|
| 394 |
getUserCredentials() |
| 395 |
cmd.Run() |
| 396 |
} |
| 397 |
} |