restic/cmd/restic/cmd_key.go

181 lines
3.9 KiB
Go
Raw Normal View History

2014-11-25 21:52:53 +00:00
package main
import (
2017-03-08 19:17:30 +00:00
"context"
2014-11-25 21:52:53 +00:00
"fmt"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-03-08 19:17:30 +00:00
"github.com/spf13/cobra"
2014-11-25 21:52:53 +00:00
)
2016-09-17 10:36:05 +00:00
var cmdKey = &cobra.Command{
Use: "key [list|add|remove|passwd] [ID]",
2016-09-17 10:36:05 +00:00
Short: "manage keys (passwords)",
Long: `
The "key" command manages keys (passwords) for accessing the repository.
2016-09-17 10:36:05 +00:00
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return runKey(globalOptions, args)
},
}
2014-12-07 15:30:52 +00:00
2014-11-30 21:39:58 +00:00
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdKey)
2014-11-30 21:39:58 +00:00
}
2017-03-08 19:17:30 +00:00
func listKeys(ctx context.Context, s *repository.Repository) 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
2017-06-04 09:16:55 +00:00
for id := range s.List(ctx, restic.KeyFile) {
k, err := repository.LoadKey(ctx, s, id.String())
2014-11-25 21:52:53 +00:00
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("LoadKey() failed: %v\n", err)
2015-03-28 10:50:23 +00:00
continue
2014-11-25 21:52:53 +00:00
}
2014-11-27 22:26:19 +00:00
var current string
if id.String() == s.KeyName() {
2014-11-27 22:26:19 +00:00
current = "*"
} else {
current = " "
}
2016-09-17 10:36:05 +00:00
tab.Rows = append(tab.Rows, []interface{}{current, id.Str(),
2014-11-25 21:52:53 +00:00
k.Username, k.Hostname, k.Created.Format(TimeFormat)})
2015-03-28 10:50:23 +00:00
}
2014-11-25 21:52:53 +00:00
2016-09-17 10:36:05 +00:00
return tab.Write(globalOptions.stdout)
2014-11-25 21:52:53 +00:00
}
2016-09-17 10:36:05 +00:00
// testKeyNewPassword is used to set a new password during integration testing.
var testKeyNewPassword string
func getNewPassword(gopts GlobalOptions) (string, error) {
if testKeyNewPassword != "" {
return testKeyNewPassword, nil
2014-11-25 22:07:00 +00:00
}
// Since we already have an open repository, temporary remove the password
// to prompt the user for the passwd.
newopts := gopts
newopts.password = ""
return ReadPasswordTwice(newopts,
2015-06-21 13:01:52 +00:00
"enter password for new key: ",
"enter password again: ")
}
2016-09-17 10:36:05 +00:00
func addKey(gopts GlobalOptions, repo *repository.Repository) error {
pw, err := getNewPassword(gopts)
if err != nil {
return err
}
2017-06-04 09:16:55 +00:00
id, err := repository.AddKey(context.TODO(), repo, pw, repo.Key())
2014-11-25 22:07:00 +00:00
if err != nil {
2016-09-01 20:17:37 +00:00
return errors.Fatalf("creating new key failed: %v\n", err)
2014-11-25 22:07:00 +00:00
}
2016-09-17 10:36:05 +00:00
Verbosef("saved new key as %s\n", id)
2014-11-25 22:07:00 +00:00
return nil
}
2016-09-17 10:36:05 +00:00
func deleteKey(repo *repository.Repository, name string) error {
2015-05-09 11:32:52 +00:00
if name == repo.KeyName() {
2016-09-01 20:17:37 +00:00
return errors.Fatal("refusing to remove key currently used to access repository")
2014-11-25 22:18:02 +00:00
}
h := restic.Handle{Type: restic.KeyFile, Name: name}
2017-06-04 09:16:55 +00:00
err := repo.Backend().Remove(context.TODO(), h)
2014-11-25 22:18:02 +00:00
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("removed key %v\n", name)
2014-11-25 22:18:02 +00:00
return nil
}
2016-09-17 10:36:05 +00:00
func changePassword(gopts GlobalOptions, repo *repository.Repository) error {
pw, err := getNewPassword(gopts)
if err != nil {
return err
}
2017-06-04 09:16:55 +00:00
id, err := repository.AddKey(context.TODO(), repo, pw, repo.Key())
if err != nil {
2016-09-01 20:17:37 +00:00
return errors.Fatalf("creating new key failed: %v\n", err)
}
h := restic.Handle{Type: restic.KeyFile, Name: repo.KeyName()}
2017-06-04 09:16:55 +00:00
err = repo.Backend().Remove(context.TODO(), h)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("saved new key as %s\n", id)
return nil
}
2016-09-17 10:36:05 +00:00
func runKey(gopts GlobalOptions, args []string) error {
if len(args) < 1 || (args[0] == "remove" && len(args) != 2) || (args[0] != "remove" && len(args) != 1) {
return errors.Fatal("wrong number of arguments")
2014-12-07 15:30:52 +00:00
}
2017-03-08 19:17:30 +00:00
ctx, cancel := context.WithCancel(gopts.ctx)
defer cancel()
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
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":
lock, err := lockRepo(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2017-03-08 19:17:30 +00:00
return listKeys(ctx, repo)
2014-11-25 22:07:00 +00:00
case "add":
lock, err := lockRepo(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
return addKey(gopts, repo)
case "remove":
lock, err := lockRepoExclusive(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2016-09-01 14:04:29 +00:00
id, err := restic.Find(repo.Backend(), restic.KeyFile, args[1])
2014-11-25 22:18:02 +00:00
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
return deleteKey(repo, id)
2015-04-25 06:42:52 +00:00
case "passwd":
lock, err := lockRepoExclusive(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
return changePassword(gopts, repo)
2014-11-25 21:52:53 +00:00
}
return nil
}