update key in db

This commit is contained in:
juancwu 2026-01-11 18:09:44 -05:00
commit f68d7c2c94
4 changed files with 58 additions and 1 deletions

View file

@ -23,7 +23,7 @@ func startEphemeralAgent(pemData []byte, target string) (string, func(), error)
if err != nil { if err != nil {
if _, ok := err.(*ssh.PassphraseMissingError); ok { if _, ok := err.(*ssh.PassphraseMissingError); ok {
fmt.Printf("\033[1;32m? Gosh:\033[0m Key for \033[1m%s\033[0m is encrypted. Enter passphrase: ", target) fmt.Printf("\033[1;32mGosh:\033[0m Key for \033[1m%s\033[0m is encrypted. Enter passphrase: ", target)
pass, readErr := term.ReadPassword(int(syscall.Stdin)) pass, readErr := term.ReadPassword(int(syscall.Stdin))
fmt.Println() fmt.Println()
if readErr != nil { if readErr != nil {

15
cmd.go
View file

@ -41,3 +41,18 @@ func handleListKey(dbPath string) error {
w.Flush() w.Flush()
return nil return nil
} }
func handleUpdateKey(dbPath, userPattern, hostPattern, keyPath string) error {
db, err := initDB(dbPath)
if err != nil {
return err
}
defer db.Close()
err = updateKey(db, userPattern, hostPattern, keyPath)
if err != nil {
return err
}
return nil
}

28
db.go
View file

@ -93,3 +93,31 @@ func listkeys(db *sql.DB) ([]KeyRecord, error) {
return keys, nil return keys, nil
} }
func updateKey(db *sql.DB, userPattern, hostPattern, keyPath string) error {
pemData, err := os.ReadFile(keyPath)
if err != nil {
return fmt.Errorf("failed to read key: %w", err)
}
res, err := db.Exec(
"UPDATE keys SET encrypted_pem=?, comment=? WHERE user_pattern = ? AND host_pattern = ?;",
pemData, "Updated from "+keyPath, userPattern, hostPattern,
)
if err != nil {
return fmt.Errorf("failed to update key: %w", err)
}
rows, err := res.RowsAffected()
if err == nil {
if rows == 0 {
fmt.Printf("No key found with user '%s' and host '%s'.\n", userPattern, hostPattern)
} else {
fmt.Printf("Key for %s@%s updated successfully.\n", userPattern, hostPattern)
}
} else {
fmt.Println("Warning: could not verify update result.", err)
}
return nil
}

14
main.go
View file

@ -12,6 +12,7 @@ func main() {
fmt.Fprintf(os.Stderr, "\nManagement Commands:\n") fmt.Fprintf(os.Stderr, "\nManagement Commands:\n")
fmt.Fprintf(os.Stderr, " %s [flags] list-keys\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s [flags] list-keys\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [flags] add-key <user_pattern> <host_pattern> <path_to_key>\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s [flags] add-key <user_pattern> <host_pattern> <path_to_key>\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [flags] update-key <user_pattern> <host_pattern> <path_to_key>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nFlags:\n") fmt.Fprintf(os.Stderr, "\nFlags:\n")
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -51,6 +52,19 @@ func main() {
os.Exit(1) os.Exit(1)
} }
os.Exit(0) os.Exit(0)
case "update-key":
if argc != 4 {
fmt.Println("Usage: gosh update-key <user_pattern> <host_pattern> <path_to_key>")
os.Exit(1)
}
err := handleUpdateKey(*dbPath, args[1], args[2], args[3])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
os.Exit(0)
} }
startSSH(*dbPath, args) startSSH(*dbPath, args)