added existing key option

This commit is contained in:
ston1th 2023-04-01 13:10:04 +02:00
commit 13e4e4de07
6 changed files with 112 additions and 14 deletions

View file

@ -3,12 +3,15 @@
package cli
import (
"bufio"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
@ -28,12 +31,13 @@ func Run(arg string) {
ctx := context.Background()
switch arg {
case "new":
name, encoding, t, size, err := newKeyFlags()
name, existing, encoding, t, size, err := newKeyFlags()
if err != nil {
exit(err)
}
newKey := schema.NewKey{
Name: name,
Existing: existing,
Size: size,
Encoding: core.EncodingFromString(encoding),
Type: core.TypeFromString(t),
@ -49,10 +53,13 @@ func Run(arg string) {
fmt.Println("Key Share:", k.Key)
}
case "del":
id, err := idFlags("key id")
id, force, err := delFlags("key id")
if err != nil {
exit(err)
}
if !force && !confirm(os.Stdin) {
exit(errors.New("confirmation failed: secret not deleted"))
}
c := client()
err = c.DeleteKey(ctx, id)
if err != nil {
@ -79,7 +86,7 @@ func Run(arg string) {
exit(err)
}
case "get":
id, share, loop, json, err := getFlags()
id, share, loop, json, newline, err := getFlags()
if err != nil {
exit(err)
}
@ -104,7 +111,11 @@ func Run(arg string) {
key.Key = out
jsonOutput(key) // does not return
}
fmt.Println(out)
if newline {
fmt.Println(out)
} else {
fmt.Print(out)
}
case "ls":
json := outputFlags()
c := client()
@ -162,11 +173,22 @@ func idFlags(desc string) (id string, err error) {
return
}
func getFlags() (id, share string, loop, json bool, err error) {
func delFlags(desc string) (id string, force bool, err error) {
fs.StringVar(&id, "id", "", desc)
fs.BoolVar(&force, "force", false, "do not ask for confirmation")
fs.Parse(os.Args[2:])
if id == "" {
err = errors.New("missing flag: id")
}
return
}
func getFlags() (id, share string, loop, json, newline bool, err error) {
fs.StringVar(&id, "id", "", "key id")
fs.StringVar(&share, "share", "", "shamir key share")
fs.BoolVar(&loop, "loop", false, "ignore network errors and loop until key is received")
fs.BoolVar(&json, "json", false, "json output")
fs.BoolVar(&newline, "newline", true, "output newline after secret")
fs.Parse(os.Args[2:])
if id == "" {
err = errors.New("missing flag: id")
@ -183,8 +205,9 @@ func outputFlags() (json bool) {
return
}
func newKeyFlags() (name, encoding, t string, size int, err error) {
func newKeyFlags() (name, existing, encoding, t string, size int, err error) {
fs.StringVar(&name, "name", "", "key name")
fs.StringVar(&existing, "key", "", "use existing secret key (optional; input-format: hex; types: plain, shamir)")
fs.StringVar(&encoding, "enc", "hex", "key encoding scheme [hex|base32|base64|base64url]")
fs.StringVar(&t, "type", "plain", "key type [plain|shamir|xor]")
fs.IntVar(&size, "size", 16, "key size in bytes")
@ -206,3 +229,16 @@ func print(tw core.TableWriter) {
}
w.Flush()
}
func confirm(r io.Reader) (ok bool) {
br := bufio.NewReader(r)
fmt.Printf("confirm delete [y]: ")
res, err := br.ReadString('\n')
if err != nil {
return
}
if len(res) < 2 {
return
}
return strings.TrimSpace(res)[0] == 'y'
}