2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00
restic/cmd/khepri/cmd_key.go

129 lines
2.6 KiB
Go
Raw Normal View History

2014-11-25 21:52:53 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
2014-11-30 21:39:58 +00:00
func init() {
commands["key"] = commandKey
}
2014-11-25 21:52:53 +00:00
func list_keys(be backend.Server, key *khepri.Key) error {
tab := NewTable()
2014-11-27 22:26:19 +00:00
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
tab.RowFormat = "%s%-10s %-10s %-10s %s"
2014-11-25 21:52:53 +00:00
plen, err := backend.PrefixLength(be, backend.Key)
if err != nil {
return err
}
backend.Each(be, backend.Key, func(id backend.ID, data []byte, err error) {
k := khepri.Key{}
err = json.Unmarshal(data, &k)
if err != nil {
return
}
2014-11-27 22:26:19 +00:00
var current string
if id.Equal(key.ID()) {
current = "*"
} else {
current = " "
}
tab.Rows = append(tab.Rows, []interface{}{current, id[:plen],
2014-11-25 21:52:53 +00:00
k.Username, k.Hostname, k.Created.Format(TimeFormat)})
})
tab.Print(os.Stdout)
return nil
}
2014-11-25 22:07:00 +00:00
func add_key(be backend.Server, key *khepri.Key) error {
pw := readPassword("KHEPRI_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("KHEPRI_NEWPASSWORD", "enter password again: ")
if pw != pw2 {
return errors.New("passwords do not match")
2014-11-25 22:07:00 +00:00
}
id, err := key.AddKey(be, pw)
if err != nil {
return fmt.Errorf("creating new key failed: %v\n", err)
}
fmt.Printf("saved new key as %s\n", id)
return nil
}
2014-11-25 22:18:02 +00:00
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 change_password(be backend.Server, key *khepri.Key) error {
pw := readPassword("KHEPRI_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("KHEPRI_NEWPASSWORD", "enter password again: ")
if pw != pw2 {
return errors.New("passwords do not match")
}
// add new key
id, err := key.AddKey(be, pw)
if err != nil {
return fmt.Errorf("creating new key failed: %v\n", err)
}
// remove old key
err = be.Remove(backend.Key, key.ID())
if err != nil {
return err
}
fmt.Printf("saved new key as %s\n", id)
return nil
}
2014-11-25 21:52:53 +00:00
func commandKey(be backend.Server, key *khepri.Key, args []string) error {
2014-11-25 22:18:02 +00:00
if len(args) < 1 || (args[0] == "rm" && len(args) != 2) {
return errors.New("usage: key [list|add|rm|change] [ID]")
2014-11-25 21:52:53 +00:00
}
switch args[0] {
case "list":
return list_keys(be, key)
2014-11-25 22:07:00 +00:00
case "add":
return add_key(be, key)
2014-11-25 22:18:02 +00:00
case "rm":
id, err := backend.Find(be, backend.Key, args[1])
if err != nil {
return err
}
return delete_key(be, key, id)
case "change":
return change_password(be, key)
2014-11-25 21:52:53 +00:00
}
return nil
}