2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-07 19:40:49 +00:00
restic/cmd/restic/cmd_key.go

146 lines
2.8 KiB
Go
Raw Normal View History

2014-11-25 21:52:53 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
2014-11-25 21:52:53 +00:00
)
2014-12-07 15:30:52 +00:00
type CmdKey struct{}
2014-11-30 21:39:58 +00:00
func init() {
2014-12-07 15:30:52 +00:00
_, err := parser.AddCommand("key",
"manage keys",
"The key command manages keys (passwords) of a repository",
&CmdKey{})
if err != nil {
panic(err)
}
2014-11-30 21:39:58 +00:00
}
2014-12-21 17:10:19 +00:00
func list_keys(s restic.Server) error {
2014-11-25 21:52:53 +00:00
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
2014-12-21 17:10:19 +00:00
plen, err := s.PrefixLength(backend.Key)
2014-11-25 21:52:53 +00:00
if err != nil {
return err
}
2014-12-21 17:10:19 +00:00
s.Each(backend.Key, func(id backend.ID, data []byte, err error) {
2014-12-05 20:45:49 +00:00
k := restic.Key{}
2014-11-25 21:52:53 +00:00
err = json.Unmarshal(data, &k)
if err != nil {
return
}
2014-11-27 22:26:19 +00:00
var current string
2014-12-21 17:10:19 +00:00
if id.Equal(s.Key().ID()) {
2014-11-27 22:26:19 +00:00
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)})
})
2014-12-22 10:21:14 +00:00
tab.Write(os.Stdout)
2014-11-25 21:52:53 +00:00
return nil
}
2014-12-21 17:10:19 +00:00
func add_key(s restic.Server) error {
2014-12-05 20:45:49 +00:00
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
2014-11-25 22:07:00 +00:00
if pw != pw2 {
return errors.New("passwords do not match")
2014-11-25 22:07:00 +00:00
}
id, err := restic.AddKey(s, pw, s.Key())
2014-11-25 22:07:00 +00:00
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-12-21 17:10:19 +00:00
func delete_key(s restic.Server, id backend.ID) error {
if id.Equal(s.Key().ID()) {
2014-11-25 22:18:02 +00:00
return errors.New("refusing to remove key currently used to access repository")
}
2014-12-21 17:10:19 +00:00
err := s.Remove(backend.Key, id)
2014-11-25 22:18:02 +00:00
if err != nil {
return err
}
fmt.Printf("removed key %v\n", id)
return nil
}
2014-12-21 17:10:19 +00:00
func change_password(s restic.Server) error {
2014-12-05 20:45:49 +00:00
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
if pw != pw2 {
return errors.New("passwords do not match")
}
// add new key
id, err := restic.AddKey(s, pw, s.Key())
if err != nil {
return fmt.Errorf("creating new key failed: %v\n", err)
}
// remove old key
2014-12-21 17:10:19 +00:00
err = s.Remove(backend.Key, s.Key().ID())
if err != nil {
return err
}
fmt.Printf("saved new key as %s\n", id)
return nil
}
2014-12-07 15:30:52 +00:00
func (cmd CmdKey) Usage() string {
return "[list|add|rm|change] [ID]"
}
func (cmd CmdKey) Execute(args []string) error {
2014-11-25 22:18:02 +00:00
if len(args) < 1 || (args[0] == "rm" && len(args) != 2) {
2014-12-07 15:30:52 +00:00
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
}
2014-12-21 17:10:19 +00:00
s, err := OpenRepo()
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-11-25 21:52:53 +00:00
}
switch args[0] {
case "list":
2014-12-21 17:10:19 +00:00
return list_keys(s)
2014-11-25 22:07:00 +00:00
case "add":
2014-12-21 17:10:19 +00:00
return add_key(s)
2014-11-25 22:18:02 +00:00
case "rm":
2014-12-21 17:10:19 +00:00
id, err := backend.Find(s, backend.Key, args[1])
2014-11-25 22:18:02 +00:00
if err != nil {
return err
}
2014-12-21 17:10:19 +00:00
return delete_key(s, id)
case "change":
2014-12-21 17:10:19 +00:00
return change_password(s)
2014-11-25 21:52:53 +00:00
}
return nil
}