Compare commits

...

2 Commits

Author SHA1 Message Date
53a9ede65c
Marginally refactor function to add SSH keys to current agent
Does this improve readability?
2023-07-16 15:12:30 +02:00
1ec3dd24e6
Add some error-handling when adding SSH keyfiles 2023-07-16 15:12:26 +02:00

View File

@ -840,42 +840,53 @@ This is `db-light' and `solarized-light'."
"Synchronously add key in KEY-FILE to currently running ssh-agent. "Synchronously add key in KEY-FILE to currently running ssh-agent.
PASSWORD-FN is supposed to be a function returning the password PASSWORD-FN is supposed to be a function returning the password
for KEY-FILE; PASSWORD-FN is called on demand. for KEY-FILE; PASSWORD-FN is called on demand. If KEY-FILE is
not readable, this function errors out.
This function uses ssh-add to add the key to the currently This function uses ssh-add to add the key to the currently
running ssh-agent and waits for the process to finish." running ssh-agent and waits for the process to finish."
(with-environment-variables (("SSH_ASKPASS_REQUIRE" "never")) (let ((key-file (expand-file-name key-file)))
(let* ((key-file (expand-file-name key-file))
(proc (make-process :name "ssh-add" (unless (file-readable-p key-file)
:buffer nil (user-error "SSH key %s does not exist, aborting" key-file))
:command (list "ssh-add" key-file)
:filter #'(lambda (process output) (with-environment-variables (("SSH_ASKPASS_REQUIRE" "never"))
(cond
((string= (format "Enter passphrase for %s: " (let* ((ssh-add-handle-output #'(lambda (process output)
key-file) (cond
output) ((string= (format "Enter passphrase for %s: "
(process-send-string process (funcall password-fn)) key-file)
(process-send-string process "\n")) output)
((or (save-match-data (process-send-string process (funcall password-fn))
(string-match (format "^Identity added: %s" key-file) (process-send-string process "\n"))
output)) ((or (save-match-data
(string= output "\n")) (string-match (format "^Identity added: %s" key-file)
;; Ignore harmless output output))
t) (string= output "\n"))
(t (message "Unknown output received from ssh-agent: %s" output)))) ;; Ignore harmless output
:sentinel #'(lambda (_ event) t)
(cond (t (message "Unknown output received from ssh-agent: %s" output)))))
((string= event "finished\n")
(message "Successfully added %s to local SSH agent" (ssh-add-handle-event-change #'(lambda (_ event)
key-file)) (cond
(t (message "Adding SSH key %s failed, ssh-add process reached state %s" ((string= event "finished\n")
key-file (message "Successfully added %s to local SSH agent"
event))))))) key-file))
;; We are waiting for the process to finish, to not let its output (t (message "Adding SSH key %s failed, ssh-add process reached state %s"
;; intermingle with others. XXX: is there a more standard way to wait for key-file
;; a process to finish? event)))))
(while (process-live-p proc)
(sit-for 0.2))))) (proc (make-process :name "ssh-add"
:buffer nil
:command (list "ssh-add" key-file)
:filter ssh-add-handle-output
:sentinel ssh-add-handle-event-change)))
;; We are waiting for the process to finish, to not let its output
;; intermingle with others. XXX: is there a more standard way to wait for
;; a process to finish?
(while (process-live-p proc)
(sit-for 0.2))))))
(defun db/ssh-key-hash-from-filename (key-file) (defun db/ssh-key-hash-from-filename (key-file)
"Return the SHA256 hash value of the SSH key located in KEY-FILE. "Return the SHA256 hash value of the SSH key located in KEY-FILE.