Revoke special password implementation for SQL Org source blocks

Turns out this can be done by a simple function call, as demonstrated by
https://magnus.therning.org/2024-09-01-improving-how-i-handle-secrets-in-my-work-notes.html.
So we just extend `org-password-manager-get-password-by-id` accordingly
and use that instead.  No need to patch the default implementation of
Org!
This commit is contained in:
Daniel Borchmann 2024-11-16 12:30:20 +01:00
parent 3d38510284
commit 5a25d5d069
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 16 additions and 25 deletions

17
init.el
View File

@ -1239,22 +1239,7 @@ accordingly."
(funcall orig-fun (funcall orig-fun
host port user host port user
(password-read (format "Password for %s@%s: " user database)) (password-read (format "Password for %s@%s: " user database))
database)))) database))))))
(define-advice org-babel-execute:sql (:around
(orig-fun body params)
retrieve-password-by-function)
"Allow to set :dbpassword by an ID of an Org items having the PASSWORD property."
(let* ((dbpassword-id (cdr (assq :dbpassword-by-id params)))
(params params))
(when dbpassword-id
(setq params (cons (cons :dbpassword
(let* ((pom (or (org-id-find dbpassword-id 'marker)
(user-error "Cannot find ID %s" dbpassword-id))))
(or (org-entry-get pom "PASSWORD")
(user-error "No PASSWORD property at ID %s" dbpassword-id))))
params)))
(funcall orig-fun body params)))))
;; Exporting ;; Exporting

View File

@ -1164,10 +1164,14 @@ cache if that's in use."
(when (derived-mode-p 'org-agenda-mode) (when (derived-mode-p 'org-agenda-mode)
(org-agenda-redo))) (org-agenda-redo)))
(defun org-password-manager-get-password-by-id (id) (defun org-password-manager-get-password-by-id (id &optional return-as-value)
"Retrieve password from Org item identified by ID. "Retrieve password from Org item identified by ID.
The password is assumed to be stored at the PASSWORD property." The password is assumed to be stored at the PASSWORD property.
When RETURN-AS-VALUE is nil, the password is copied to the
clipboard as with `org-password-manager-get-password', which see.
Otherwise, the password is returned as value from this function
and can be used for further processing."
(require 'org-password-manager) (require 'org-password-manager)
(let ((pom (org-id-find id 'marker))) (let ((pom (org-id-find id 'marker)))
@ -1179,13 +1183,15 @@ The password is assumed to be stored at the PASSWORD property."
(when (null pw) (when (null pw)
(user-error "PASSWORD property not set for “%s”" heading)) (user-error "PASSWORD property not set for “%s”" heading))
(funcall interprogram-cut-function pw) (if return-as-value
(run-at-time org-password-manager-default-password-wait-time pw
nil (funcall interprogram-cut-function pw)
(lambda () (funcall interprogram-cut-function ""))) (run-at-time org-password-manager-default-password-wait-time
(message "Password for “%s” securly copied to system clipboard; will be overwritten in %s." nil
heading (lambda () (funcall interprogram-cut-function "")))
org-password-manager-default-password-wait-time)))) (message "Password for “%s” securly copied to system clipboard; will be overwritten in %s."
heading
org-password-manager-default-password-wait-time)))))
(defhydra hydra-org-jump (:color blue) (defhydra hydra-org-jump (:color blue)
;; Quote %, as otherwise they would be misinterpreted as format characters ;; Quote %, as otherwise they would be misinterpreted as format characters