secrets.lisp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (in-package #:chatikbot)
  2. (defvar *secret-ring* nil "GPG keyring path")
  3. (defvar *secret-pass-store* nil "pass store dir")
  4. (defvar *secret-pass-bin* "pass" "pass util binary")
  5. (defun %secret/pass (cmd path &key input (output :string) error-output)
  6. (let ((input-stream (when input (make-string-input-stream input))))
  7. (unwind-protect
  8. (uiop:run-program
  9. (format nil "~@[GNUPGHOME=~A ~]~@[PASSWORD_STORE_DIR=~A ~]~A ~A~@[ ~{~A~^/~}~]"
  10. *secret-ring* *secret-pass-store* *secret-pass-bin*
  11. cmd path)
  12. :input input-stream :output output :error-output error-output)
  13. (when input-stream
  14. (close input-stream)))))
  15. (defun secret/get (path)
  16. (handler-case
  17. (let ((*read-eval* nil))
  18. (values (read-from-string (%secret/pass "show" path))))
  19. (error () (values))))
  20. (defun secret/set (path value)
  21. (%secret/pass "insert --force --multiline" path
  22. :input (prin1-to-string value) :output nil :error-output :string))
  23. (defun secret/del (path)
  24. (%secret/pass "rm --force" path))
  25. (defun secret/wipe (data)
  26. (cond
  27. ((stringp data) (fill data #\Space))
  28. ((vectorp data) (fill data 0))
  29. ((consp data)
  30. (secret/wipe (car data))
  31. (secret/wipe (cdr data)))))
  32. (defmacro secret/with ((var path) &body body)
  33. `(let ((,var (ignore-errors (secret/get ,path))))
  34. (unwind-protect
  35. (progn ,@body)
  36. (secret/wipe ,var))))