Add a -key-passphrase option to pass in passphrase for RSA private key

This option is mainly for the web GUI which currently doesn't have a way to
specify the passphrase to decrypt the RSA private key.
This commit is contained in:
Gilbert Chen
2020-07-05 20:58:07 -04:00
parent 1da7e2b536
commit 089e19f8e6

View File

@@ -212,15 +212,20 @@ func runScript(context *cli.Context, storageName string, phase string) bool {
return true return true
} }
func loadRSAPrivateKey(keyFile string, preference *duplicacy.Preference, backupManager *duplicacy.BackupManager, resetPasswords bool) { func loadRSAPrivateKey(keyFile string, passphrase string, preference *duplicacy.Preference, backupManager *duplicacy.BackupManager, resetPasswords bool) {
if keyFile == "" { if keyFile == "" {
return return
} }
prompt := fmt.Sprintf("Enter the passphrase for %s:", keyFile) prompt := fmt.Sprintf("Enter the passphrase for %s:", keyFile)
passphrase := duplicacy.GetPassword(*preference, "rsa_passphrase", prompt, false, resetPasswords) if passphrase == "" {
backupManager.LoadRSAPrivateKey(keyFile, passphrase) passphrase = duplicacy.GetPassword(*preference, "rsa_passphrase", prompt, false, resetPasswords)
duplicacy.SavePassword(*preference, "rsa_passphrase", passphrase) backupManager.LoadRSAPrivateKey(keyFile, passphrase)
duplicacy.SavePassword(*preference, "rsa_passphrase", passphrase)
} else {
backupManager.LoadRSAPrivateKey(keyFile, passphrase)
}
} }
func initRepository(context *cli.Context) { func initRepository(context *cli.Context) {
@@ -821,7 +826,7 @@ func restoreRepository(context *cli.Context) {
backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, preference.NobackupFile, preference.FiltersFile) backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, preference.NobackupFile, preference.FiltersFile)
duplicacy.SavePassword(*preference, "password", password) duplicacy.SavePassword(*preference, "password", password)
loadRSAPrivateKey(context.String("key"), preference, backupManager, false) loadRSAPrivateKey(context.String("key"), context.String("key-passphrase"), preference, backupManager, false)
backupManager.SetupSnapshotCache(preference.Name) backupManager.SetupSnapshotCache(preference.Name)
backupManager.Restore(repository, revision, true, quickMode, threads, overwrite, deleteMode, setOwner, showStatistics, patterns) backupManager.Restore(repository, revision, true, quickMode, threads, overwrite, deleteMode, setOwner, showStatistics, patterns)
@@ -874,7 +879,7 @@ func listSnapshots(context *cli.Context) {
showChunks := context.Bool("chunks") showChunks := context.Bool("chunks")
// list doesn't need to decrypt file chunks; but we need -key here so we can reset the passphrase for the private key // list doesn't need to decrypt file chunks; but we need -key here so we can reset the passphrase for the private key
loadRSAPrivateKey(context.String("key"), preference, backupManager, resetPassword) loadRSAPrivateKey(context.String("key"), "", preference, backupManager, resetPassword)
backupManager.SetupSnapshotCache(preference.Name) backupManager.SetupSnapshotCache(preference.Name)
backupManager.SnapshotManager.ListSnapshots(id, revisions, tag, showFiles, showChunks) backupManager.SnapshotManager.ListSnapshots(id, revisions, tag, showFiles, showChunks)
@@ -919,7 +924,7 @@ func checkSnapshots(context *cli.Context) {
backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "") backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "")
duplicacy.SavePassword(*preference, "password", password) duplicacy.SavePassword(*preference, "password", password)
loadRSAPrivateKey(context.String("key"), preference, backupManager, false) loadRSAPrivateKey(context.String("key"), context.String("key-passphrase"), preference, backupManager, false)
id := preference.SnapshotID id := preference.SnapshotID
if context.Bool("all") { if context.Bool("all") {
@@ -977,7 +982,7 @@ func printFile(context *cli.Context) {
backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "") backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "")
duplicacy.SavePassword(*preference, "password", password) duplicacy.SavePassword(*preference, "password", password)
loadRSAPrivateKey(context.String("key"), preference, backupManager, false) loadRSAPrivateKey(context.String("key"), context.String("key-passphrase"), preference, backupManager, false)
backupManager.SetupSnapshotCache(preference.Name) backupManager.SetupSnapshotCache(preference.Name)
@@ -1035,7 +1040,7 @@ func diff(context *cli.Context) {
backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "") backupManager := duplicacy.CreateBackupManager(preference.SnapshotID, storage, repository, password, "", "")
duplicacy.SavePassword(*preference, "password", password) duplicacy.SavePassword(*preference, "password", password)
loadRSAPrivateKey(context.String("key"), preference, backupManager, false) loadRSAPrivateKey(context.String("key"), context.String("key-passphrase"), preference, backupManager, false)
backupManager.SetupSnapshotCache(preference.Name) backupManager.SetupSnapshotCache(preference.Name)
backupManager.SnapshotManager.Diff(repository, snapshotID, revisions, path, compareByHash, preference.NobackupFile, preference.FiltersFile) backupManager.SnapshotManager.Diff(repository, snapshotID, revisions, path, compareByHash, preference.NobackupFile, preference.FiltersFile)
@@ -1184,7 +1189,7 @@ func copySnapshots(context *cli.Context) {
sourceManager.SetupSnapshotCache(source.Name) sourceManager.SetupSnapshotCache(source.Name)
duplicacy.SavePassword(*source, "password", sourcePassword) duplicacy.SavePassword(*source, "password", sourcePassword)
loadRSAPrivateKey(context.String("key"), source, sourceManager, false) loadRSAPrivateKey(context.String("key"), context.String("key-passphrase"), source, sourceManager, false)
_, destination := getRepositoryPreference(context, context.String("to")) _, destination := getRepositoryPreference(context, context.String("to"))
@@ -1510,6 +1515,11 @@ func main() {
Usage: "the RSA private key to decrypt file chunks", Usage: "the RSA private key to decrypt file chunks",
Argument: "<private key>", Argument: "<private key>",
}, },
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Argument: "<private key passphrase>",
},
}, },
Usage: "Restore the repository to a previously saved snapshot", Usage: "Restore the repository to a previously saved snapshot",
ArgsUsage: "[--] [pattern] ...", ArgsUsage: "[--] [pattern] ...",
@@ -1621,6 +1631,11 @@ func main() {
Usage: "the RSA private key to decrypt file chunks", Usage: "the RSA private key to decrypt file chunks",
Argument: "<private key>", Argument: "<private key>",
}, },
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Argument: "<private key passphrase>",
},
cli.IntFlag{ cli.IntFlag{
Name: "threads", Name: "threads",
Value: 1, Value: 1,
@@ -1655,6 +1670,11 @@ func main() {
Usage: "the RSA private key to decrypt file chunks", Usage: "the RSA private key to decrypt file chunks",
Argument: "<private key>", Argument: "<private key>",
}, },
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Argument: "<private key passphrase>",
},
}, },
Usage: "Print to stdout the specified file, or the snapshot content if no file is specified", Usage: "Print to stdout the specified file, or the snapshot content if no file is specified",
ArgsUsage: "[<file>]", ArgsUsage: "[<file>]",
@@ -1688,6 +1708,11 @@ func main() {
Usage: "the RSA private key to decrypt file chunks", Usage: "the RSA private key to decrypt file chunks",
Argument: "<private key>", Argument: "<private key>",
}, },
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Argument: "<private key passphrase>",
},
}, },
Usage: "Compare two snapshots or two revisions of a file", Usage: "Compare two snapshots or two revisions of a file",
ArgsUsage: "[<file>]", ArgsUsage: "[<file>]",
@@ -1965,6 +1990,11 @@ func main() {
Usage: "the RSA private key to decrypt file chunks from the source storage", Usage: "the RSA private key to decrypt file chunks from the source storage",
Argument: "<private key>", Argument: "<private key>",
}, },
cli.StringFlag{
Name: "key-passphrase",
Usage: "the passphrase to decrypt the RSA private key",
Argument: "<private key passphrase>",
},
}, },
Usage: "Copy snapshots between compatible storages", Usage: "Copy snapshots between compatible storages",
ArgsUsage: " ", ArgsUsage: " ",