mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-03 15:47:23 -04:00
Fix import cycle (over SSH keyfile passphrase input)
This commit is contained in:
+2
-2
@@ -32,7 +32,7 @@ func TempInitCli() {
|
|||||||
configSSH := inputBinary("Configure SSH settings (for synchronization)?")
|
configSSH := inputBinary("Configure SSH settings (for synchronization)?")
|
||||||
if configSSH {
|
if configSSH {
|
||||||
// necessary SSH info
|
// necessary SSH info
|
||||||
fmt.Print(AnsiBold+"Note:"+backend.AnsiReset, "Only key-based authentication is supported (keys may optionally be passphrase-protected).\nThe remote server must already be in your ~/.ssh/known_hosts file.\n\n")
|
fmt.Println(AnsiBold + "\nNote:" + backend.AnsiReset + " Only key-based authentication is supported (keys may optionally be passphrase-protected).\nThe remote server must already be in your ~/.ssh/known_hosts file.")
|
||||||
sshUser := input("Remote SSH username:")
|
sshUser := input("Remote SSH username:")
|
||||||
sshIP := input("Remote SSH IP address:")
|
sshIP := input("Remote SSH IP address:")
|
||||||
sshPort := input("Remote SSH port:")
|
sshPort := input("Remote SSH port:")
|
||||||
@@ -40,7 +40,7 @@ func TempInitCli() {
|
|||||||
sshKeyProtected := inputBinary("Is the identity file password-protected?")
|
sshKeyProtected := inputBinary("Is the identity file password-protected?")
|
||||||
|
|
||||||
// write config file
|
// write config file
|
||||||
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshIdentity": sshKey, "sshIDProtected": strconv.FormatBool(sshKeyProtected)})
|
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected)})
|
||||||
|
|
||||||
// generate device ID
|
// generate device ID
|
||||||
sync.DeviceIDGen()
|
sync.DeviceIDGen()
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ func input(prompt string) string {
|
|||||||
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
|
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
|
// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
|
||||||
func InputHidden(prompt string) string {
|
func inputHidden(prompt string) string {
|
||||||
fmt.Print("\n" + prompt + " ")
|
fmt.Print("\n" + prompt + " ")
|
||||||
byteInput, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
|
byteInput, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
|
||||||
password := string(byteInput)
|
password := string(byteInput)
|
||||||
|
|||||||
+3
-4
@@ -3,7 +3,6 @@ package sync
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rwinkhart/MUTN/src/backend"
|
"github.com/rwinkhart/MUTN/src/backend"
|
||||||
"github.com/rwinkhart/MUTN/src/cli"
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/crypto/ssh/knownhosts"
|
"golang.org/x/crypto/ssh/knownhosts"
|
||||||
"os"
|
"os"
|
||||||
@@ -19,7 +18,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetSSHOutput runs a command over SSH and returns the output
|
// 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 {
|
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)
|
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
||||||
var sshUserConfig []string
|
var sshUserConfig []string
|
||||||
@@ -57,7 +56,7 @@ func GetSSHOutput(cmd string, manualSync bool) string {
|
|||||||
if keyFileProtected != "true" {
|
if keyFileProtected != "true" {
|
||||||
parsedKey, err = ssh.ParsePrivateKey(key)
|
parsedKey, err = ssh.ParsePrivateKey(key)
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
fmt.Println(backend.AnsiError+"Sync failed - Unable to parse private key:", keyFile+backend.AnsiReset)
|
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) {
|
func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string) {
|
||||||
// get remote output over SSH
|
// get remote output over SSH
|
||||||
clientDeviceID, _ := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices")
|
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"
|
// split output into slice based on occurrences of "\x1d"
|
||||||
outputSlice := strings.Split(output, "\x1d")
|
outputSlice := strings.Split(output, "\x1d")
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user