mirror of
https://github.com/cloudflare/redoctober.git
synced 2026-05-28 10:40:49 +00:00
Add order support to ro client
This commit is contained in:
@@ -283,3 +283,63 @@ func (c *RemoteServer) Password(req []byte) (*core.ResponseData, error) {
|
||||
|
||||
return unmarshalResponseData(respBytes)
|
||||
}
|
||||
|
||||
// Order issues an order request to the remote server
|
||||
func (c *RemoteServer) Order(req core.OrderRequest) (*core.ResponseData, error) {
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBytes, err := c.doAction("order", reqBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unmarshalResponseData(respBytes)
|
||||
}
|
||||
|
||||
// OrderOutstanding issues an order outstanding request to the remote server
|
||||
func (c *RemoteServer) OrderOutstanding(req core.OrderOutstandingRequest) (*core.ResponseData, error) {
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBytes, err := c.doAction("orderout", reqBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unmarshalResponseData(respBytes)
|
||||
}
|
||||
|
||||
// OrderInfo issues an order info request to the remote server
|
||||
func (c *RemoteServer) OrderInfo(req core.OrderInfoRequest) (*core.ResponseData, error) {
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBytes, err := c.doAction("orderinfo", reqBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unmarshalResponseData(respBytes)
|
||||
}
|
||||
|
||||
// OrderCancel issues an order cancel request to the remote server
|
||||
func (c *RemoteServer) OrderCancel(req core.OrderInfoRequest) (*core.ResponseData, error) {
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBytes, err := c.doAction("ordercancel", reqBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unmarshalResponseData(respBytes)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/cloudflare/redoctober/client"
|
||||
"github.com/cloudflare/redoctober/cmd/ro/gopass"
|
||||
"github.com/cloudflare/redoctober/core"
|
||||
"github.com/cloudflare/redoctober/order"
|
||||
)
|
||||
|
||||
var action, user, pswd, userEnv, pswdEnv, server, caPath string
|
||||
@@ -23,6 +24,8 @@ var uses int
|
||||
|
||||
var time, users string
|
||||
|
||||
var pollInterval time.Duration
|
||||
|
||||
type command struct {
|
||||
Run func()
|
||||
Desc string
|
||||
@@ -37,6 +40,7 @@ var commandSet = map[string]command{
|
||||
"encrypt": command{Run: runEncrypt, Desc: "encrypt a file"},
|
||||
"decrypt": command{Run: runDecrypt, Desc: "decrypt a file"},
|
||||
"re-encrypt": command{Run: runReEncrypt, Desc: "re-encrypt a file"},
|
||||
"order": command{Run: runOrder, Desc: "place an order for delegations"},
|
||||
}
|
||||
|
||||
func registerFlags() {
|
||||
@@ -56,6 +60,7 @@ func registerFlags() {
|
||||
flag.StringVar(&pswd, "password", "", "password")
|
||||
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)")
|
||||
}
|
||||
|
||||
func getUserCredentials() {
|
||||
@@ -207,6 +212,33 @@ func runDecrypt() {
|
||||
ioutil.WriteFile(outPath, msg.Data, 0644)
|
||||
}
|
||||
|
||||
func runOrder() {
|
||||
req := core.OrderRequest{
|
||||
Name: user,
|
||||
Password: pswd,
|
||||
Uses: uses,
|
||||
Duration: time,
|
||||
Labels: processCSL(labels),
|
||||
}
|
||||
resp, err := roServer.Order(req)
|
||||
processError(err)
|
||||
|
||||
var o order.Order
|
||||
err = json.Unmarshal(resp.Response, &o)
|
||||
processError(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(err)
|
||||
err = json.Unmarshal(resp.Response, &o)
|
||||
processError(err)
|
||||
}
|
||||
}
|
||||
fmt.Println(resp.Status)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Println("Usage: ro [options] subcommand")
|
||||
|
||||
Reference in New Issue
Block a user