restic/cmd/restic/cmd_key.go

260 lines
6.1 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"
"encoding/json"
"io/ioutil"
"os"
"strings"
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"
2018-08-19 19:31:53 +00:00
"github.com/restic/restic/internal/ui/table"
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 [flags] [list|add|remove|passwd] [ID]",
Short: "Manage keys (passwords)",
2016-09-17 10:36:05 +00:00
Long: `
The "key" command manages keys (passwords) for accessing the repository.
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
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 {
2022-10-02 21:24:37 +00:00
return runKey(cmd.Context(), globalOptions, args)
2016-09-17 10:36:05 +00:00
},
}
2014-12-07 15:30:52 +00:00
var (
newPasswordFile string
keyUsername string
keyHostname string
)
2014-11-30 21:39:58 +00:00
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdKey)
flags := cmdKey.Flags()
flags.StringVarP(&newPasswordFile, "new-password-file", "", "", "`file` from which to read the new password")
flags.StringVarP(&keyUsername, "user", "", "", "the username for new keys")
flags.StringVarP(&keyHostname, "host", "", "", "the hostname for new keys")
2014-11-30 21:39:58 +00:00
}
func listKeys(ctx context.Context, s *repository.Repository, gopts GlobalOptions) error {
2018-08-19 19:31:53 +00:00
type keyInfo struct {
Current bool `json:"current"`
ID string `json:"id"`
UserName string `json:"userName"`
HostName string `json:"hostName"`
Created string `json:"created"`
}
2018-08-19 19:31:53 +00:00
var keys []keyInfo
err := s.List(ctx, restic.KeyFile, func(id restic.ID, size int64) error {
2017-06-04 09:16:55 +00:00
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)
return nil
2014-11-25 21:52:53 +00:00
}
key := keyInfo{
Current: id.String() == s.KeyName(),
ID: id.Str(),
UserName: k.Username,
HostName: k.Hostname,
2018-11-02 19:36:15 +00:00
Created: k.Created.Local().Format(TimeFormat),
2014-11-27 22:26:19 +00:00
}
2018-08-19 19:31:53 +00:00
keys = append(keys, key)
return nil
2018-08-19 19:31:53 +00:00
})
if err != nil {
return err
2015-03-28 10:50:23 +00:00
}
2014-11-25 21:52:53 +00:00
2018-08-19 19:31:53 +00:00
if gopts.JSON {
return json.NewEncoder(globalOptions.stdout).Encode(keys)
}
tab := table.New()
tab.AddColumn(" ID", "{{if .Current}}*{{else}} {{end}}{{ .ID }}")
tab.AddColumn("User", "{{ .UserName }}")
tab.AddColumn("Host", "{{ .HostName }}")
tab.AddColumn("Created", "{{ .Created }}")
for _, key := range keys {
tab.AddRow(key)
}
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
}
if newPasswordFile != "" {
return loadPasswordFromFile(newPasswordFile)
}
// 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,
2020-08-19 19:42:08 +00:00
"enter new password: ",
2015-06-21 13:01:52 +00:00
"enter password again: ")
}
2021-10-31 22:01:47 +00:00
func addKey(ctx context.Context, repo *repository.Repository, gopts GlobalOptions) error {
2016-09-17 10:36:05 +00:00
pw, err := getNewPassword(gopts)
if err != nil {
return err
}
2021-10-31 22:01:47 +00:00
id, err := repository.AddKey(ctx, repo, pw, keyUsername, keyHostname, 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
}
2021-10-31 22:01:47 +00:00
err = switchToNewKeyAndRemoveIfBroken(ctx, repo, id, pw)
if err != nil {
return err
}
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
}
func deleteKey(ctx context.Context, 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}
err := repo.Backend().Remove(ctx, 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
}
2021-10-31 22:01:47 +00:00
func changePassword(ctx context.Context, repo *repository.Repository, gopts GlobalOptions) error {
2016-09-17 10:36:05 +00:00
pw, err := getNewPassword(gopts)
if err != nil {
return err
}
2021-10-31 22:01:47 +00:00
id, err := repository.AddKey(ctx, 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)
}
oldID := repo.KeyName()
2021-10-31 22:01:47 +00:00
err = switchToNewKeyAndRemoveIfBroken(ctx, repo, id, pw)
if err != nil {
return err
}
h := restic.Handle{Type: restic.KeyFile, Name: oldID}
2021-10-31 22:01:47 +00:00
err = repo.Backend().Remove(ctx, h)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("saved new key as %s\n", id)
return nil
}
func switchToNewKeyAndRemoveIfBroken(ctx context.Context, repo *repository.Repository, key *repository.Key, pw string) error {
// Verify new key to make sure it really works. A broken key can render the
// whole repository inaccessible
err := repo.SearchKey(ctx, pw, 0, key.Name())
if err != nil {
// the key is invalid, try to remove it
h := restic.Handle{Type: restic.KeyFile, Name: key.Name()}
_ = repo.Backend().Remove(ctx, h)
return errors.Fatalf("failed to access repository with new key: %v", err)
}
return nil
}
func runKey(ctx context.Context, 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
}
repo, err := OpenRepository(ctx, 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, ctx, err := lockRepo(ctx, repo)
defer unlockRepo(lock)
if err != nil {
return err
}
return listKeys(ctx, repo, gopts)
2014-11-25 22:07:00 +00:00
case "add":
lock, ctx, err := lockRepo(ctx, repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2021-10-31 22:01:47 +00:00
return addKey(ctx, repo, gopts)
case "remove":
lock, ctx, err := lockRepoExclusive(ctx, repo)
defer unlockRepo(lock)
if err != nil {
return err
}
id, err := restic.Find(ctx, repo.Backend(), restic.KeyFile, args[1])
2014-11-25 22:18:02 +00:00
if err != nil {
return err
}
2021-10-31 22:01:47 +00:00
return deleteKey(ctx, repo, id)
2015-04-25 06:42:52 +00:00
case "passwd":
lock, ctx, err := lockRepoExclusive(ctx, repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2021-10-31 22:01:47 +00:00
return changePassword(ctx, repo, gopts)
2014-11-25 21:52:53 +00:00
}
return nil
}
func loadPasswordFromFile(pwdFile string) (string, error) {
s, err := ioutil.ReadFile(pwdFile)
if os.IsNotExist(err) {
return "", errors.Fatalf("%s does not exist", pwdFile)
}
return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile")
}