Add command "key rm"

This commit is contained in:
Alexander Neumann 2014-11-25 23:18:02 +01:00
parent f7fcd7c04c
commit 43ccee3b08
2 changed files with 33 additions and 3 deletions

View file

@ -54,9 +54,23 @@ func add_key(be backend.Server, key *khepri.Key) error {
return nil 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 { func commandKey(be backend.Server, key *khepri.Key, args []string) error {
if len(args) < 1 { if len(args) < 1 || (args[0] == "rm" && len(args) != 2) {
return errors.New("usage: key [list|add]") return errors.New("usage: key [list|add|rm] [ID]")
} }
switch args[0] { switch args[0] {
@ -64,6 +78,13 @@ func commandKey(be backend.Server, key *khepri.Key, args []string) error {
return list_keys(be, key) return list_keys(be, key)
case "add": case "add":
return add_key(be, key) 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 return nil

11
key.go
View file

@ -63,6 +63,8 @@ type Key struct {
user *keys user *keys
master *keys master *keys
id backend.ID
} }
// keys is a JSON structure that holds signing and encryption keys. // 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 // store in repository and return
_, err = be.Create(backend.Key, buf) id, err := be.Create(backend.Key, buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
k.id = id
FreeChunkBuf("key", k.Data) FreeChunkBuf("key", k.Data)
@ -177,6 +180,7 @@ func OpenKey(be backend.Server, id backend.ID, password string) (*Key, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
k.id = id
return k, nil return k, nil
} }
@ -262,6 +266,7 @@ func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
newkey.id = id
FreeChunkBuf("key", newkey.Data) FreeChunkBuf("key", newkey.Data)
@ -446,3 +451,7 @@ func (k *Key) String() string {
} }
return fmt.Sprintf("<Key of %s@%s, created on %s>", k.Username, k.Hostname, k.Created) return fmt.Sprintf("<Key of %s@%s, created on %s>", k.Username, k.Hostname, k.Created)
} }
func (k Key) ID() backend.ID {
return k.id
}