Merge pull request #1720 from mholt/new-password-flag

key: Add --new-password flag for non-interactive password changes
This commit is contained in:
Alexander Neumann 2018-04-20 14:52:41 +02:00
commit 7877797c7e
2 changed files with 27 additions and 0 deletions

View file

@ -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

View file

@ -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")
}