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

@ -22,15 +22,18 @@ const (
type NewKey struct { type NewKey struct {
Name string `json:"name"` Name string `json:"name"`
Existing string `json:"existing,omitempty"`
Size int `json:"size"` Size int `json:"size"`
Encoding core.Encoding `json:"encoding"` Encoding core.Encoding `json:"encoding"`
Type core.Type `json:"type"` Type core.Type `json:"type"`
} }
var ( var (
ErrEmptyKeyName = errors.New("empty key name") ErrEmptyKeyName = errors.New("empty key name")
ErrInvalidKeyName = errors.New("invalid key name") ErrExistingKeyType = errors.New("invalid key type for existing key")
ErrInvalidKeySize = errors.New("invalid key size") ErrExistingKeyEncoding = errors.New("invalid key encoding for existing key")
ErrInvalidKeyName = errors.New("invalid key name")
ErrInvalidKeySize = errors.New("invalid key size")
) )
const ( const (
@ -42,6 +45,15 @@ func (nk NewKey) Validate() error {
if nk.Name == "" { if nk.Name == "" {
return ErrEmptyKeyName return ErrEmptyKeyName
} }
if nk.Existing != "" {
if nk.Type == core.Xor {
return ErrExistingKeyType
}
_, err := hex.DecodeString(nk.Existing)
if err != nil {
return ErrExistingKeyEncoding
}
}
if core.NameRe.MatchString(nk.Name) { if core.NameRe.MatchString(nk.Name) {
return ErrInvalidKeyName return ErrInvalidKeyName
} }

View file

@ -43,7 +43,7 @@ func newHandler(ctx *types.Context) {
ctx.Err(types.ErrInvalid) ctx.Err(types.ErrInvalid)
return return
} }
key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Size, newKey.Encoding, newKey.Type) key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Existing, newKey.Size, newKey.Encoding, newKey.Type)
if err != nil { if err != nil {
ctx.Log.Error(err, "error writing key", "name", newKey.Name) ctx.Log.Error(err, "error writing key", "name", newKey.Name)
ctx.Err(types.ErrISE) ctx.Err(types.ErrISE)

View file

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

47
pkg/cli/cli_test.go Normal file
View file

@ -0,0 +1,47 @@
// Copyright (C) 2023 Marius Schellenberger
package cli
import (
"bytes"
"testing"
)
func TestConfirm(t *testing.T) {
b := new(bytes.Buffer)
b.WriteString("\n")
if confirm(b) {
t.Error("invalid confirm")
}
b.Reset()
b.WriteString(" n\n")
if confirm(b) {
t.Error("invalid confirm")
}
b.Reset()
b.WriteString(" foo\n")
if confirm(b) {
t.Error("invalid confirm")
}
b.Reset()
b.WriteString("Y\n")
if confirm(b) {
t.Error("invalid confirm")
}
b.Reset()
b.WriteString(" y\n")
if !confirm(b) {
t.Error("valid confirm")
}
b.Reset()
b.WriteString("y\n")
if !confirm(b) {
t.Error("valid confirm")
}
}

View file

@ -31,8 +31,8 @@ func (db *DB) GetKeyWithSecret(id string) (k core.Key, err error) {
return return
} }
func (db *DB) CreateKey(name string, size int, enc core.Encoding, t core.Type) (k core.Key, err error) { func (db *DB) CreateKey(name, existing string, size int, enc core.Encoding, t core.Type) (k core.Key, err error) {
kstore, k, err := key.Generate(name, size, enc, t) kstore, k, err := key.Generate(name, existing, size, enc, t)
if err != nil { if err != nil {
return return
} }

View file

@ -26,13 +26,16 @@ func GenerateID() (string, error) {
return hex.EncodeToString(id), nil return hex.EncodeToString(id), nil
} }
func Generate(name string, size int, enc core.Encoding, t core.Type) (kstore, k core.Key, err error) { func Generate(name, existing string, size int, enc core.Encoding, t core.Type) (kstore, k core.Key, err error) {
id, err := GenerateID() id, err := GenerateID()
if err != nil { if err != nil {
return return
} }
var key []byte var key []byte
if t == core.Xor { if existing != "" {
key, err = hex.DecodeString(existing)
size = len(key)
} else if t == core.Xor {
key, err = generate(size * 2) key, err = generate(size * 2)
} else { } else {
key, err = generate(size) key, err = generate(size)