rekey.scm (2061B)
1 #!/run/current-system/profile/bin/guile --no-auto-compile 2 !# 3 4 (define-module (epistemia secrets rekey) 5 #:use-module (ice-9 format) 6 #:use-module (ice-9 match)) 7 8 (define age-id "/data/ssh/id_ed25519") 9 (define age-pub "/data/ssh/id_ed25519.pub") 10 11 ;; use ssh-keyscan to get these, or ~/.ssh/known_hosts 12 (define saklas "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINmdSO3VpL2Y18LV74dikABLEN2hE/abSlyoaiNn5kiR") 13 (define demiurge "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAxKtA4uvSPOckdHqxxVhQ8cQxl1k6bwzQ0u6hpCAiqc") 14 (define hastur "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDRBXSHca8R2M37jzVj87neBZ9TqZ+R0A0yaZCIFiNaA") 15 16 (define mappings 17 `(("email_13f0" . ,saklas) 18 ("email_vinbiz" . ,saklas) 19 ("email_vineetk" . ,saklas) 20 ("awg0_saklas" . ,saklas) 21 ("awg0_saklas_header" . ,saklas) 22 ("saklas_smtpd_dkim" . ,saklas) 23 ("saklas_smtpd_passwd" . ,saklas) 24 ("saklas_smtpd_virtuals" . ,saklas) 25 ("wg0_hastur" . ,hastur) 26 ("awg0_hastur" . ,hastur) 27 ("awg0_hastur_header" . ,hastur) 28 ("wpa_supplicant" . ,hastur))) 29 30 (define (run fmt . args) 31 (zero? (system (apply format #f fmt args)))) 32 33 (for-each 34 (match-lambda 35 ((base . key) 36 (let ((enc (string-append base ".age")) 37 (tmp (string-append base ".age.tmp"))) 38 (cond 39 ;; rekey existing encrypted file 40 ((file-exists? enc) 41 (format #t "rekeying ~a...~%" enc) 42 (if (run "age -d -i ~a ~a | age -e -R ~a -r '~a' -o ~a" 43 age-id enc age-pub key tmp) 44 (rename-file tmp enc) 45 (format (current-error-port) "failed to rekey ~a~%" base))) 46 47 ;; otherwise encrypt fresh plaintext 48 ((file-exists? base) 49 (format #t "encrypting ~a...~%" base) 50 (if (run "age -e -R ~a -r '~a' -o ~a ~a" 51 age-pub key enc base) 52 (delete-file base) 53 (format (current-error-port) "failed to encrypt ~a~%" base))) 54 55 (else 56 (format (current-error-port) "file not found: ~a~%" base)))))) 57 mappings)