diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
| commit | 6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (patch) | |
| tree | 4c2c85b78b07e877ca18e833d07e8624712e8a4c /cmd | |
| parent | 9eb2b774e9f0522a3547644453bf929dd560f05f (diff) | |
ssh
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/eno/cobra.go | 26 | ||||
| -rw-r--r-- | cmd/eno/cobra_new.go | 38 | ||||
| -rw-r--r-- | cmd/eno/cobra_ssh.go | 171 | ||||
| -rw-r--r-- | cmd/eno/config.go | 38 |
4 files changed, 247 insertions, 26 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go index 9e4d4d2..ced2bc2 100644 --- a/cmd/eno/cobra.go +++ b/cmd/eno/cobra.go @@ -56,7 +56,7 @@ var encryptCmd = &cobra.Command{ crypt.TryMlock(password) defer clear(password) - encrypted, err := crypt.EncryptRawBytes(fileBytes, password, crypt.Pass) + encrypted, err := crypt.EncryptBytesPass(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) @@ -107,7 +107,7 @@ var decryptCmd = &cobra.Command{ crypt.TryMlock(password) defer clear(password) - plaintext, err := crypt.DecryptRawBytes(fileBytes, password) + plaintext, err := crypt.DecryptBytesPass(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) @@ -159,7 +159,21 @@ var editCmd = &cobra.Command{ target = strings.TrimSuffix(target, ".eno") target += ".eno" - err = crud.EditNote(target, Conf.EditorString) + authMode, err := utils.CheckFileAuthMode(target) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + switch authMode { + case crypt.SSH: + err = crud.EditNoteSSH(target, Conf.DefaultSSHPrivPath, Conf.EditorString) + case crypt.Pass: + err = crud.EditNotePass(target, Conf.EditorString) + default: + cmd.PrintErrln("Error: unknown auth mode:", authMode) + os.Exit(1) + } if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) @@ -176,7 +190,9 @@ func init() { newCmd.Flags().StringP("output", "o", "", "output file") newCmd.Flags().BoolP("edit", "e", false, "edit after creation") - newCmd.Flags().BoolP("ssh", "ssh", false, "use your ssh for auth") + newCmd.Flags().Bool("ssh", false, "use your ssh for auth") + + sshCmd.AddCommand(sshInitCmd) - cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd) + cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd, sshCmd) } diff --git a/cmd/eno/cobra_new.go b/cmd/eno/cobra_new.go index 4a7caeb..d424fea 100644 --- a/cmd/eno/cobra_new.go +++ b/cmd/eno/cobra_new.go @@ -34,8 +34,6 @@ var newCmd = &cobra.Command{ os.Exit(1) } - - noteName := strings.Join(args, " ") if output == "" { if strings.ContainsRune(noteName, filepath.Separator) { @@ -57,17 +55,45 @@ var newCmd = &cobra.Command{ } } - if editFlag { - err = crud.NewNoteEdit(output, Conf.EditorString) + if sshFlag && editFlag { + pubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } + + err = crud.NewNoteSSHEdit(output, pubKeyBytes, Conf.DefaultSSHPrivPath, Conf.EditorString) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + return } - _ = sshFlag + if sshFlag { + pubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + + err = crud.NewNoteSSH(output, pubKeyBytes) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + return + } + + if editFlag { + err = crud.NewNotePassEdit(output, Conf.EditorString) + if err != nil { + cmd.PrintErrln("Error:", err) + os.Exit(1) + } + } - err = crud.NewNote(output) + err = crud.NewNotePass(output) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) 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) + }, +} diff --git a/cmd/eno/config.go b/cmd/eno/config.go index b1bb407..3551522 100644 --- a/cmd/eno/config.go +++ b/cmd/eno/config.go @@ -14,7 +14,8 @@ type Config struct { EditorString string DataDir string ConfPath string - DefaultSSH string + DefaultSSHPrivPath string + DefaultSSHPubPath string SSHInitialized bool } var Conf Config @@ -59,7 +60,6 @@ func initConfig() error { f, err := os.Open(Conf.ConfPath) if err != nil { if errors.Is(err, os.ErrNotExist) { - fmt.Fprintf(os.Stderr, "no config — run 'eno init'\n") return nil } return fmt.Errorf("reading config: %w", err) @@ -85,14 +85,18 @@ func initConfig() error { if v := strings.TrimSpace(val); v != "" { Conf.DataDir = v } - case "DEFAULT_SSH": - if v := strings.TrimSpace(val); v != "" { - Conf.DefaultSSH = v - } case "SSH_INIT": if v, err := strconv.ParseBool(strings.TrimSpace(val)); err == nil { Conf.SSHInitialized = v } + case "DEFAULT_SSH_PRIV": + if v := strings.TrimSpace(val); v != "" { + Conf.DefaultSSHPrivPath = v + } + case "DEFAULT_SSH_PUB": + if v := strings.TrimSpace(val); v != "" { + Conf.DefaultSSHPubPath = v + } } } return scanner.Err() @@ -112,11 +116,14 @@ func handlerInit() error { defer f.Close() defaultConfLines := []string{ - fmt.Sprintf("EDITOR_STRING=%s", Conf.EditorString), - fmt.Sprintf("DATA_DIR=%s", Conf.DataDir), - fmt.Sprintf("SSH_INIT=%b", Conf.SSHInitialized), - "# Change 'DEFAULT_SSH' to a private ssh key you want to use regularly.", - "# DEFAULT_SSH=/home/user/.ssh/id_ed25519", + fmt.Sprintf("EDITOR_STRING=%s\n", Conf.EditorString), + + fmt.Sprintf("DATA_DIR=%s\n", Conf.DataDir), + + fmt.Sprintf("SSH_INIT=%t\n", Conf.SSHInitialized), + + "# Change 'DEFAULT_SSH_PRIV' to a private ssh key you want to use regularly to encrypt.", + "# Change 'DEFAULT_SSH_PUB' to a private ssh key you want to use regularly to decrypt.", } _, err = fmt.Fprintf(f, "%s\n", strings.Join(defaultConfLines, "\n")) @@ -128,10 +135,11 @@ func handlerInit() error { } var configFieldKey = map[string]string{ - "EditorString": "EDITOR_STRING", - "DataDir": "DATA_DIR", - "DefaultSSH": "DEFAULT_SSH", - "SSHInitialized": "SSH_INIT", + "EditorString": "EDITOR_STRING", + "DataDir": "DATA_DIR", + "DefaultSSHPrivPath": "DEFAULT_SSH_PRIV", + "DefaultSSHPubPath": "DEFAULT_SSH_PUB", + "SSHInitialized": "SSH_INIT", } func updateConfig(key string, val any) error { |