From 224289d4046f0cc1f321c6dee940f35d7968999a Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 17 May 2024 12:25:10 -0400 Subject: [PATCH] Fix import cycle (over SSH keyfile passphrase input) --- src/sync/client.go | 7 +++---- src/sync/input.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/sync/input.go diff --git a/src/sync/client.go b/src/sync/client.go index 7e09f02..b074db0 100644 --- a/src/sync/client.go +++ b/src/sync/client.go @@ -3,7 +3,6 @@ package sync import ( "fmt" "github.com/rwinkhart/MUTN/src/backend" - "github.com/rwinkhart/MUTN/src/cli" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" "os" @@ -19,7 +18,7 @@ const ( ) // GetSSHOutput runs a command over SSH and returns the output -// only supports key-based authentication (passphrase-protected keys are supported in a CLI environment) TODO create a more generic interface for passphrase input +// only supports key-based authentication (passphrases are supported for CLI-based implementations) func GetSSHOutput(cmd string, manualSync bool) string { // get SSH config info, exit if not configured (displaying an error if the sync job was called manually) var sshUserConfig []string @@ -57,7 +56,7 @@ func GetSSHOutput(cmd string, manualSync bool) string { if keyFileProtected != "true" { parsedKey, err = ssh.ParsePrivateKey(key) } else { - parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(cli.InputHidden("Enter passphrase for \""+keyFile+"\":"))) // TODO test passphrase-protected keys + parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, inputKeyFilePassphrase()) // TODO test passphrase-protected keys } if err != nil { fmt.Println(backend.AnsiError+"Sync failed - Unable to parse private key:", keyFile+backend.AnsiReset) @@ -113,7 +112,7 @@ func GetSSHOutput(cmd string, manualSync bool) string { func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string) { // get remote output over SSH clientDeviceID, _ := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices") - output := GetSSHOutput("libmuttonserver fetch "+clientDeviceID[0].Name(), manualSync) + output := GetSSHOutput("libmuttonserver fetch "+clientDeviceID[0].Name(), manualSync) // TODO fix potential index error if clientDeviceID was not configured // split output into slice based on occurrences of "\x1d" outputSlice := strings.Split(output, "\x1d") diff --git a/src/sync/input.go b/src/sync/input.go new file mode 100644 index 0000000..d848d3a --- /dev/null +++ b/src/sync/input.go @@ -0,0 +1,16 @@ +package sync + +import ( + "fmt" + "golang.org/x/crypto/ssh/terminal" + "os" +) + +// inputKeyFilePassphrase prompts the user for a passphrase for an SSH key file +// TODO support non-CLI implementations +func inputKeyFilePassphrase() []byte { + fmt.Print("\nEnter passphrase for your SSH keyfile: ") + passphrase, _ := terminal.ReadPassword(int(os.Stdin.Fd())) + fmt.Println() + return passphrase +}