added shamir keys
This commit is contained in:
parent
13780f1c74
commit
be0f532628
14 changed files with 238 additions and 43 deletions
|
|
@ -5,9 +5,11 @@ package cli
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
|
||||
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
|
||||
clientv1 "git.giftfish.de/ston1th/keyctl/pkg/api/v1/client"
|
||||
|
|
@ -27,33 +29,64 @@ func Run(arg string) {
|
|||
ctx := context.Background()
|
||||
switch arg {
|
||||
case "new":
|
||||
name, encoding, size := newKeyFlags()
|
||||
name, encoding, t, size, err := newKeyFlags()
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
newKey := schema.NewKey{
|
||||
Name: name,
|
||||
Size: size,
|
||||
Encoding: core.EncodingFromString(encoding),
|
||||
Type: core.TypeFromString(t),
|
||||
}
|
||||
c := client()
|
||||
err = c.CreateKey(ctx, newKey)
|
||||
k, err := c.CreateKey(ctx, newKey)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
switch k.Type {
|
||||
case core.Plain:
|
||||
fmt.Println("Key ID:", k.ID)
|
||||
case core.Shamir:
|
||||
fmt.Println("Key ID:", k.ID)
|
||||
fmt.Println("Key Share:", k.Key)
|
||||
}
|
||||
case "del":
|
||||
id := idFlags("key id")
|
||||
id, err := idFlags("key id")
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
c := client()
|
||||
err = c.DeleteKey(ctx, id)
|
||||
case "approve":
|
||||
id := idFlags("approval id")
|
||||
id, err := idFlags("approval id")
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
c := client()
|
||||
err = c.AcceptApproval(ctx, id)
|
||||
case "reject":
|
||||
id := idFlags("approval id")
|
||||
id, err := idFlags("approval id")
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
c := client()
|
||||
err = c.RejectApproval(ctx, id)
|
||||
case "get":
|
||||
id, json := getFlags()
|
||||
id, share, json, err := getFlags()
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
c := client()
|
||||
key, err := c.GetKey(ctx, id)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
out, err := key.Print(share)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
key.Key = out
|
||||
if json {
|
||||
jsonOutput(key) // does not return
|
||||
}
|
||||
|
|
@ -68,15 +101,7 @@ func Run(arg string) {
|
|||
if json {
|
||||
jsonOutput(keys) // does not return
|
||||
}
|
||||
for _, k := range keys {
|
||||
fmt.Printf("%s %s %d %s %d\n",
|
||||
k.ID,
|
||||
k.Name,
|
||||
k.Size,
|
||||
k.Encoding,
|
||||
k.Created,
|
||||
)
|
||||
}
|
||||
print(keys)
|
||||
case "req":
|
||||
json := outputFlags()
|
||||
c := client()
|
||||
|
|
@ -87,16 +112,7 @@ func Run(arg string) {
|
|||
if json {
|
||||
jsonOutput(approvals) // does not return
|
||||
}
|
||||
for _, a := range approvals {
|
||||
fmt.Printf("%s %s %s %s %d %s\n",
|
||||
a.ID,
|
||||
a.AID,
|
||||
a.Name,
|
||||
a.Status,
|
||||
a.Created,
|
||||
a.IP,
|
||||
)
|
||||
}
|
||||
print(approvals)
|
||||
}
|
||||
if err != nil {
|
||||
exit(err)
|
||||
|
|
@ -126,16 +142,23 @@ func jsonOutput(v any) {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
func idFlags(desc string) (id string) {
|
||||
func idFlags(desc string) (id string, err error) {
|
||||
fs.StringVar(&id, "id", "", desc)
|
||||
fs.Parse(os.Args[2:])
|
||||
if id == "" {
|
||||
err = errors.New("missing flag: id")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getFlags() (id string, json bool) {
|
||||
func getFlags() (id, share string, json bool, err error) {
|
||||
fs.StringVar(&id, "id", "", "key id")
|
||||
fs.StringVar(&share, "share", "", "shamir key share")
|
||||
fs.BoolVar(&json, "json", false, "json output")
|
||||
fs.Parse(os.Args[2:])
|
||||
if id == "" {
|
||||
err = errors.New("missing flag: id")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -145,10 +168,26 @@ func outputFlags() (json bool) {
|
|||
return
|
||||
}
|
||||
|
||||
func newKeyFlags() (name, encoding string, size int) {
|
||||
func newKeyFlags() (name, encoding, t string, size int, err error) {
|
||||
fs.StringVar(&name, "name", "", "key name")
|
||||
fs.StringVar(&encoding, "enc", "hex", "key encoding scheme [hex|base32|base64|base64url]")
|
||||
fs.StringVar(&t, "type", "plain", "key type [plain|shamir]")
|
||||
fs.IntVar(&size, "size", 16, "key size in bytes")
|
||||
fs.Parse(os.Args[2:])
|
||||
if name == "" {
|
||||
err = errors.New("missing flag: name")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func print(tw core.TableWriter) {
|
||||
if tw.Len() == 0 {
|
||||
return
|
||||
}
|
||||
w := tabwriter.NewWriter(os.Stdout, 1, 0, 1, ' ', tabwriter.Debug)
|
||||
fmt.Fprintln(w, tw.Fields())
|
||||
for _, v := range tw.Values() {
|
||||
fmt.Fprintln(w, v)
|
||||
}
|
||||
w.Flush()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue