From 43ccee3b08b3644fa2cd3977033beb02365a6bfa Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 25 Nov 2014 23:18:02 +0100 Subject: [PATCH] Add command "key rm" --- cmd/khepri/cmd_key.go | 25 +++++++++++++++++++++++-- key.go | 11 ++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cmd/khepri/cmd_key.go b/cmd/khepri/cmd_key.go index c728d03e3..6e8552f99 100644 --- a/cmd/khepri/cmd_key.go +++ b/cmd/khepri/cmd_key.go @@ -54,9 +54,23 @@ func add_key(be backend.Server, key *khepri.Key) error { return nil } +func delete_key(be backend.Server, key *khepri.Key, id backend.ID) error { + if id.Equal(key.ID()) { + return errors.New("refusing to remove key currently used to access repository") + } + + err := be.Remove(backend.Key, id) + if err != nil { + return err + } + + fmt.Printf("removed key %v\n", id) + return nil +} + func commandKey(be backend.Server, key *khepri.Key, args []string) error { - if len(args) < 1 { - return errors.New("usage: key [list|add]") + if len(args) < 1 || (args[0] == "rm" && len(args) != 2) { + return errors.New("usage: key [list|add|rm] [ID]") } switch args[0] { @@ -64,6 +78,13 @@ func commandKey(be backend.Server, key *khepri.Key, args []string) error { return list_keys(be, key) case "add": return add_key(be, key) + case "rm": + id, err := backend.Find(be, backend.Key, args[1]) + if err != nil { + return err + } + + return delete_key(be, key, id) } return nil diff --git a/key.go b/key.go index 86e0d77ee..bb2abb49d 100644 --- a/key.go +++ b/key.go @@ -63,6 +63,8 @@ type Key struct { user *keys master *keys + + id backend.ID } // keys is a JSON structure that holds signing and encryption keys. @@ -129,10 +131,11 @@ func CreateKey(be backend.Server, password string) (*Key, error) { } // store in repository and return - _, err = be.Create(backend.Key, buf) + id, err := be.Create(backend.Key, buf) if err != nil { return nil, err } + k.id = id FreeChunkBuf("key", k.Data) @@ -177,6 +180,7 @@ func OpenKey(be backend.Server, id backend.ID, password string) (*Key, error) { if err != nil { return nil, err } + k.id = id return k, nil } @@ -262,6 +266,7 @@ func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error if err != nil { return nil, err } + newkey.id = id FreeChunkBuf("key", newkey.Data) @@ -446,3 +451,7 @@ func (k *Key) String() string { } return fmt.Sprintf("", k.Username, k.Hostname, k.Created) } + +func (k Key) ID() backend.ID { + return k.id +}