From c5829e9ffc7d5a6e32325881847aedb98eabed74 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 14 Apr 2018 18:40:57 -0600 Subject: [PATCH 1/2] key: Add flag for non-interactive password changes --- cmd/restic/cmd_key.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index 7552c778d..d81be6bb9 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -3,6 +3,9 @@ package main import ( "context" "fmt" + "io/ioutil" + "os" + "strings" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" @@ -23,8 +26,13 @@ The "key" command manages keys (passwords) for accessing the repository. }, } +var newPasswordFile string + func init() { cmdRoot.AddCommand(cmdKey) + + flags := cmdKey.Flags() + flags.StringVarP(&newPasswordFile, "new-password-file", "", "", "the file from which to load a new password") } func listKeys(ctx context.Context, s *repository.Repository) error { @@ -64,6 +72,10 @@ func getNewPassword(gopts GlobalOptions) (string, error) { return testKeyNewPassword, nil } + 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 @@ -182,3 +194,11 @@ func runKey(gopts GlobalOptions, args []string) error { 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") +} From 1a26355dbe742d740a99d7ccd3f51e773f3e7a26 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 14 Apr 2018 18:49:33 -0600 Subject: [PATCH 2/2] Add changelog file --- changelog/unreleased/pull-1720 | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog/unreleased/pull-1720 diff --git a/changelog/unreleased/pull-1720 b/changelog/unreleased/pull-1720 new file mode 100644 index 000000000..90f23add4 --- /dev/null +++ b/changelog/unreleased/pull-1720 @@ -0,0 +1,7 @@ +Enhancement: Add --new-password-file flag for non-interactive password changes + +This makes it possible to change a repository password without being prompted. + +https://github.com/restic/restic/issues/827 +https://github.com/restic/restic/pull/1720 +https://forum.restic.net/t/changing-repo-password-without-prompt/591