aboutsummaryrefslogtreecommitdiff
path: root/internal/crud/cli_new_ssh.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-19 18:33:40 +0530
committerishaan <contact@ishaan.cc>2026-07-19 18:33:40 +0530
commite48a02646f60b6fdff8db5c4105896756584ac63 (patch)
tree3371785e68bfe6b14065589ba7fd417d75742595 /internal/crud/cli_new_ssh.go
parentd6449a232e6bd9eb46132c7403a05ac1a756b619 (diff)
reorganize
Diffstat (limited to 'internal/crud/cli_new_ssh.go')
-rw-r--r--internal/crud/cli_new_ssh.go234
1 files changed, 234 insertions, 0 deletions
diff --git a/internal/crud/cli_new_ssh.go b/internal/crud/cli_new_ssh.go
new file mode 100644
index 0000000..5479490
--- /dev/null
+++ b/internal/crud/cli_new_ssh.go
@@ -0,0 +1,234 @@
+package crud
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "strings"
+
+ "gitlab.com/whiteboardguy/eno/internal/crypt"
+ "gitlab.com/whiteboardguy/eno/internal/editor"
+ "gitlab.com/whiteboardguy/eno/internal/utils"
+)
+
+func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) {
+ useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true)
+ if err != nil {
+ return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !useDefault {
+ fmt.Println("Warning: you will not be able to open or edit this note again.")
+ fmt.Println(" make sure you've used the '--edit' flag.")
+ }
+
+ addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false)
+ if err != nil {
+ return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
+ }
+
+ pubKeys := [][]byte{}
+
+ if useDefault {
+ pubKeys = append(pubKeys, defaultPubKeyBytes)
+ }
+
+ if addMore {
+ for {
+ fmt.Println("Enter public keys (\033[1mcomma-separated for multiple\033[0m):")
+ fmt.Println("Example: ~/keys/bob.pub, ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AA..., bob@github, james@gitlab")
+ fmt.Print("Public Keys: ")
+ utils.StdinScanner.Scan()
+ if err := utils.StdinScanner.Err(); err != nil {
+ return nil, false, fmt.Errorf("failed to read public key: %v", err)
+ }
+
+ input := strings.TrimSpace(utils.StdinScanner.Text())
+ if input == "" {
+ continue
+ }
+
+ for part := range strings.SplitSeq(input, ",") {
+ part = strings.TrimSpace(part)
+ if part == "" {
+ continue
+ }
+
+ if strings.Contains(part, "@") && !strings.HasPrefix(part, "ssh-") {
+ parts := strings.Split(part, "@")
+ if len(parts) != 2 {
+ return nil, false, fmt.Errorf("unsupported source or invalid key format: %s", part)
+ }
+
+ username, service := parts[0], parts[1]
+ body, err := utils.GetPubKeyFromService(username, service)
+ if err != nil {
+ return nil, false, fmt.Errorf("failed to get public key from %s: %v", part, err)
+ }
+ selectedKeys, err := selectServiceKeys(part, body)
+ if err != nil {
+ fmt.Printf("Warning: %s: %v\n", service, err)
+ continue
+ }
+ for _, k := range selectedKeys {
+ pubKeys = append(pubKeys, bytes.TrimSpace(k))
+ }
+ continue
+ }
+
+ var keyBytes []byte
+ if strings.HasPrefix(part, "ssh-") {
+ keyBytes = []byte(part)
+ } else {
+ var err error
+ keyBytes, err = os.ReadFile(part)
+ if err != nil {
+ return nil, false, fmt.Errorf("failed to read public key file %s: %v", part, err)
+ }
+ }
+ pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes))
+ }
+
+ more, err := utils.AskUserYesNo("Add more public keys?", false)
+ if err != nil {
+ return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
+ }
+ if !more {
+ break
+ }
+ }
+ }
+
+ return pubKeys, useDefault, nil
+}
+
+func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
+ pubKeys, _, err := PromptRecipientKeys(defaultPubKeyBytes)
+ if err != nil {
+ return err
+ }
+ if len(pubKeys) == 0 {
+ return fmt.Errorf("at least one recipient public key is required")
+ }
+
+ tmptxt := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...)
+
+ encrypted, _, err := crypt.EncryptBytesSSH(tmptxt, pubKeys)
+ if err != nil {
+ return err
+ }
+
+ fmt.Println(output)
+ file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ _, err = file.Write(encrypted)
+ if err != nil {
+ return err
+ }
+
+ file.Sync()
+
+ // utils.ClearTerm()
+ fmt.Println("New note saved at:", file.Name())
+
+ return nil
+}
+
+func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath string, editorString string) error {
+ pubKeys, useDefault, err := PromptRecipientKeys(defaultPubKeyBytes)
+ if err != nil {
+ return err
+ }
+ if len(pubKeys) == 0 {
+ return fmt.Errorf("at least one recipient public key is required")
+ }
+
+ noteInfo, err := os.Stat(output)
+ preExisting := err == nil
+
+ initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...)
+ encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys)
+ if err != nil {
+ return err
+ }
+ crypt.TryMlock(noteKey)
+ defer clear(noteKey)
+
+ file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return err
+ }
+
+ _, err = file.Write(encrypted)
+ if err != nil {
+ return err
+ }
+
+ file.Sync()
+ file.Close()
+
+ ciphertext, err := os.ReadFile(output)
+ if err != nil {
+ return err
+ }
+
+ tmpFile, err := utils.SecureTempFile()
+ if err != nil {
+ return err
+ }
+
+ var plaintext []byte
+ if useDefault {
+ plaintext, err = crypt.DecryptBytesSSH(ciphertext, defaultPrivKeyPath)
+ } else {
+ plaintext, err = crypt.DecryptBytesSSHWithKey(ciphertext, noteKey)
+ }
+ if err != nil {
+ return err
+ }
+ crypt.TryMlock(plaintext)
+ defer clear(plaintext)
+
+ defer deferredCleanTmpFile(tmpFile, len(plaintext))
+ defer os.Remove(tmpFile.Name())
+
+ _, err = tmpFile.Write(plaintext)
+ if err != nil {
+ return err
+ }
+
+ err = editor.OpenWithEditor(tmpFile.Name(), editorString)
+ if err != nil {
+ return err
+ }
+
+ newContent, err := os.ReadFile(tmpFile.Name())
+ if err != nil {
+ return err
+ }
+ crypt.TryMlock(newContent)
+ defer clear(newContent)
+
+ if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil {
+ fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err)
+ }
+
+ encrypted, _, err = crypt.EncryptBytesSSH(newContent, pubKeys)
+ if err != nil {
+ return err
+ }
+
+ perm := os.FileMode(0600)
+ if preExisting {
+ perm = noteInfo.Mode().Perm()
+ }
+
+ err = os.WriteFile(output, encrypted, perm)
+
+ fmt.Println("New note saved at:", output)
+
+ return err
+}