2024-03-15 11:42:22 +01:00
|
|
|
// Copyright 2024 - offen.software <hioffen@posteo.de>
|
2024-02-13 19:21:57 +01:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-11 10:11:23 +02:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2024-02-13 19:21:57 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
"filippo.io/age"
|
2024-08-11 10:11:23 +02:00
|
|
|
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
2024-02-13 19:21:57 +01:00
|
|
|
openpgp "github.com/ProtonMail/go-crypto/openpgp/v2"
|
2024-02-16 15:35:42 +01:00
|
|
|
"github.com/offen/docker-volume-backup/internal/errwrap"
|
2024-02-13 19:21:57 +01:00
|
|
|
)
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func countTrue(b ...bool) int {
|
|
|
|
c := int(0)
|
|
|
|
for _, v := range b {
|
|
|
|
if v {
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
// encryptArchive encrypts the backup file using PGP and the configured passphrase or publickey(s).
|
|
|
|
// In case no passphrase or publickey is given it returns early, leaving the backup file
|
|
|
|
// untouched.
|
|
|
|
func (s *script) encryptArchive() error {
|
|
|
|
useGPGSymmetric := s.c.GpgPassphrase != ""
|
|
|
|
useGPGAsymmetric := s.c.GpgPublicKeyRing != ""
|
|
|
|
useAgeSymmetric := s.c.AgePassphrase != ""
|
|
|
|
useAgeAsymmetric := len(s.c.AgePublicKeys) > 0
|
|
|
|
switch nconfigured := countTrue(
|
|
|
|
useGPGSymmetric,
|
|
|
|
useGPGAsymmetric,
|
|
|
|
useAgeSymmetric,
|
|
|
|
useAgeAsymmetric,
|
|
|
|
); nconfigured {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
// ok!
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(
|
|
|
|
"error in selecting archive encryption method: expected 0 or 1 to be configured, %d methods are configured",
|
|
|
|
nconfigured,
|
|
|
|
)
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
if useGPGSymmetric {
|
|
|
|
return s.encryptWithGPGSymmetric()
|
|
|
|
} else if useGPGAsymmetric {
|
|
|
|
return s.encryptWithGPGAsymmetric()
|
|
|
|
} else if useAgeSymmetric || useAgeAsymmetric {
|
|
|
|
ar, err := s.getConfiguredAgeRecipients()
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrap(err, "failed to get configured age recipients")
|
|
|
|
}
|
|
|
|
return s.encryptWithAge(ar)
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
2024-08-19 22:49:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (s *script) getConfiguredAgeRecipients() ([]age.Recipient, error) {
|
|
|
|
if s.c.AgePassphrase == "" && len(s.c.AgePublicKeys) == 0 {
|
|
|
|
return nil, fmt.Errorf("no age recipients configured")
|
|
|
|
}
|
|
|
|
recipients := []age.Recipient{}
|
|
|
|
if len(s.c.AgePublicKeys) > 0 {
|
|
|
|
for _, pk := range s.c.AgePublicKeys {
|
|
|
|
pkr, err := age.ParseX25519Recipient(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrap(err, "failed to parse age public key")
|
|
|
|
}
|
|
|
|
recipients = append(recipients, pkr)
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
2024-08-19 22:49:49 +02:00
|
|
|
if s.c.AgePassphrase != "" {
|
|
|
|
if len(recipients) != 0 {
|
|
|
|
return nil, fmt.Errorf("age encryption must only be enabled via passphrase or public key, not both")
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
r, err := age.NewScryptRecipient(s.c.AgePassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrap(err, "failed to create scrypt identity from age passphrase")
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
2024-08-19 22:49:49 +02:00
|
|
|
recipients = append(recipients, r)
|
|
|
|
}
|
|
|
|
return recipients, nil
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (s *script) encryptWithAge(rec []age.Recipient) error {
|
|
|
|
return s.doEncrypt("age", func(ciphertextWriter io.Writer) (io.WriteCloser, error) {
|
|
|
|
return age.Encrypt(ciphertextWriter, rec...)
|
|
|
|
})
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (s *script) encryptWithGPGSymmetric() error {
|
|
|
|
return s.doEncrypt("gpg", func(ciphertextWriter io.Writer) (io.WriteCloser, error) {
|
|
|
|
_, name := path.Split(s.file)
|
|
|
|
return openpgp.SymmetricallyEncrypt(ciphertextWriter, []byte(s.c.GpgPassphrase), &openpgp.FileHints{
|
|
|
|
FileName: name,
|
|
|
|
}, nil)
|
|
|
|
})
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
type closeAllWriter struct {
|
|
|
|
io.Writer
|
|
|
|
closers []io.Closer
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (c *closeAllWriter) Close() (err error) {
|
|
|
|
for _, cl := range c.closers {
|
|
|
|
err = errors.Join(err, cl.Close())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
var _ io.WriteCloser = (*closeAllWriter)(nil)
|
2024-08-11 10:11:23 +02:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (s *script) encryptWithGPGAsymmetric() error {
|
|
|
|
return s.doEncrypt("gpg", func(ciphertextWriter io.Writer) (_ io.WriteCloser, outerr error) {
|
|
|
|
entityList, err := openpgp.ReadArmoredKeyRing(bytes.NewReader([]byte(s.c.GpgPublicKeyRing)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrap(err, "error parsing armored keyring")
|
|
|
|
}
|
|
|
|
|
|
|
|
armoredWriter, err := armor.Encode(ciphertextWriter, "PGP MESSAGE", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrap(err, "error preparing encryption")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if outerr != nil {
|
|
|
|
_ = armoredWriter.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, name := path.Split(s.file)
|
|
|
|
encWriter, err := openpgp.Encrypt(armoredWriter, entityList, nil, nil, &openpgp.FileHints{
|
|
|
|
FileName: name,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &closeAllWriter{
|
|
|
|
Writer: encWriter,
|
|
|
|
closers: []io.Closer{encWriter, armoredWriter},
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
2024-02-13 19:21:57 +01:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
func (s *script) doEncrypt(
|
|
|
|
extension string,
|
|
|
|
encryptor func(ciphertextWriter io.Writer) (io.WriteCloser, error),
|
|
|
|
) (outerr error) {
|
|
|
|
encFile := fmt.Sprintf("%s.%s", s.file, extension)
|
2024-02-13 19:21:57 +01:00
|
|
|
s.registerHook(hookLevelPlumbing, func(error) error {
|
2024-08-19 22:49:49 +02:00
|
|
|
if err := remove(encFile); err != nil {
|
|
|
|
return errwrap.Wrap(err, "error removing encrypted file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
s.logger.Info(
|
2024-08-19 22:49:49 +02:00
|
|
|
fmt.Sprintf("Removed encrypted file `%s`.", encFile),
|
2024-02-13 19:21:57 +01:00
|
|
|
)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
outFile, err := os.Create(encFile)
|
2024-02-13 19:21:57 +01:00
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error opening out file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
defer func() {
|
|
|
|
if err := outFile.Close(); err != nil {
|
2024-08-19 22:49:49 +02:00
|
|
|
outerr = errors.Join(outerr, errwrap.Wrap(err, "error closing out file"))
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
}()
|
2024-02-13 19:21:57 +01:00
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
dst, err := encryptor(outFile)
|
2024-02-13 19:21:57 +01:00
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error encrypting backup file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
defer func() {
|
2024-08-19 22:49:49 +02:00
|
|
|
if err := dst.Close(); err != nil {
|
|
|
|
outerr = errors.Join(outerr, errwrap.Wrap(err, "error closing encrypted backup file"))
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
}()
|
2024-02-13 19:21:57 +01:00
|
|
|
|
|
|
|
src, err := os.Open(s.file)
|
|
|
|
if err != nil {
|
2024-08-19 22:49:49 +02:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error opening backup file %q", s.file))
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
2024-08-11 10:11:23 +02:00
|
|
|
defer func() {
|
|
|
|
if err := src.Close(); err != nil {
|
2024-08-19 22:49:49 +02:00
|
|
|
outerr = errors.Join(outerr, errwrap.Wrap(err, "error closing backup file"))
|
2024-08-11 10:11:23 +02:00
|
|
|
}
|
|
|
|
}()
|
2024-02-13 19:21:57 +01:00
|
|
|
|
|
|
|
if _, err := io.Copy(dst, src); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error writing ciphertext to file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
|
2024-08-19 22:49:49 +02:00
|
|
|
s.file = encFile
|
2024-02-13 19:21:57 +01:00
|
|
|
s.logger.Info(
|
2024-08-19 22:49:49 +02:00
|
|
|
fmt.Sprintf("Encrypted backup using %q, saving as %q", extension, s.file),
|
2024-02-13 19:21:57 +01:00
|
|
|
)
|
2024-08-19 22:49:49 +02:00
|
|
|
|
|
|
|
return
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|