aboutsummaryrefslogtreecommitdiff
path: root/cmd/eno/cobra_ssh.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/eno/cobra_ssh.go')
-rw-r--r--cmd/eno/cobra_ssh.go171
1 files changed, 171 insertions, 0 deletions
diff --git a/cmd/eno/cobra_ssh.go b/cmd/eno/cobra_ssh.go
new file mode 100644
index 0000000..58a5a44
--- /dev/null
+++ b/cmd/eno/cobra_ssh.go
@@ -0,0 +1,171 @@
+package main
+
+import (
+ "bufio"
+ "crypto"
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/spf13/cobra"
+ "gitlab.com/whiteboardguy/eno/internal/crypt"
+ "gitlab.com/whiteboardguy/eno/internal/utils"
+ "golang.org/x/crypto/ssh"
+)
+
+func expandPath(path string) string {
+ if strings.HasPrefix(path, "~") {
+ home, err := os.UserHomeDir()
+ if err == nil {
+ rest := strings.TrimPrefix(path[1:], "/")
+ path = filepath.Join(home, rest)
+ }
+ }
+ return path
+}
+
+var sshCmd = &cobra.Command{
+ Use: "ssh",
+ Short: "ssh auth in Eno notes",
+ Args: cobra.MinimumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ },
+}
+
+var sshInitCmd = &cobra.Command{
+ Use: "init",
+ Short: "setup ssh for Eno notes",
+ Args: cobra.ExactArgs(0),
+ Run: func(cmd *cobra.Command, args []string) {
+ scanner := bufio.NewScanner(os.Stdin)
+
+ if Conf.SSHInitialized {
+ cmd.PrintErrln("SSH is already initialized")
+ ok, err := utils.AskUserYesNo("Reinitialize SSH?", false)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ if !ok {
+ cmd.PrintErrln("SSH init aborted")
+ os.Exit(0)
+ }
+ }
+
+ privateKeyPath := expandPath("~/.ssh/id_ed25519")
+ if _, err := os.Stat(privateKeyPath); err == nil {
+ ok, err := utils.AskUserYesNo("Use default SSH key at ~/.ssh/id_ed25519?", true)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ if !ok {
+ privateKeyPath = ""
+ }
+ } else {
+ privateKeyPath = ""
+ }
+
+ if privateKeyPath == "" {
+ fmt.Print("Enter path to SSH private key: ")
+ if !scanner.Scan() {
+ cmd.PrintErrln("failed to read input")
+ os.Exit(1)
+ }
+ privateKeyPath = expandPath(strings.TrimSpace(scanner.Text()))
+ }
+
+ publicKeyPath := privateKeyPath + ".pub"
+ if _, err := os.Stat(publicKeyPath); os.IsNotExist(err) {
+ cmd.PrintErrln("public key not found at " + publicKeyPath)
+ fmt.Print("Enter path to SSH public key: ")
+ if !scanner.Scan() {
+ cmd.PrintErrln("failed to read input")
+ os.Exit(1)
+ }
+ publicKeyPath = strings.TrimSpace(scanner.Text())
+ publicKeyPath = expandPath(publicKeyPath)
+ }
+
+ givenPubKeyBytes, err := os.ReadFile(publicKeyPath)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+
+ privKeyBytes, err := os.ReadFile(privateKeyPath)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ crypt.TryMlock(privKeyBytes)
+ defer clear(privKeyBytes)
+
+ privKey, err := ssh.ParseRawPrivateKey(privKeyBytes)
+ if err != nil {
+ if _, ok := errors.AsType[*ssh.PassphraseMissingError](err); ok {
+ fmt.Printf("SSH Key Locked!\n")
+ fmt.Printf("Enter the ssh key passphrase: ")
+ sshPassword, err := utils.TakeSecureUserInput()
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ crypt.TryMlock(sshPassword)
+ defer clear(sshPassword)
+
+ privKey, err = ssh.ParseRawPrivateKeyWithPassphrase(privKeyBytes, []byte(sshPassword))
+ } else {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ }
+
+ signer, ok := privKey.(crypto.Signer)
+ if !ok {
+ cmd.PrintErrln("failed to extract public key from private key")
+ os.Exit(1)
+ }
+ extractedPubKeyRaw := signer.Public()
+
+ givenPubKey, _, _, _, err := ssh.ParseAuthorizedKey(givenPubKeyBytes)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+
+ extractedPubKey, err := ssh.NewPublicKey(extractedPubKeyRaw)
+ if err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+
+ if ssh.FingerprintSHA256(extractedPubKey) != ssh.FingerprintSHA256(givenPubKey) {
+ cmd.PrintErrln("public key file does not match private key")
+ os.Exit(1)
+ }
+
+ if err := updateConfig("DefaultSSHPrivPath", privateKeyPath); err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ if err := updateConfig("DefaultSSHPubPath", publicKeyPath); err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+ if err := updateConfig("SSHInitialized", true); err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+
+ if err := updateConfig("SSHInitialized", true); err != nil {
+ cmd.PrintErrln(err)
+ os.Exit(1)
+ }
+
+
+ cmd.Println("ssh initialized with public key:", publicKeyPath)
+ },
+}