test / test (push) Canceled after 0s
C-A1, C-Q4, D-guide:258, :291, :722. certbot's live/<host>/*.pem are relative links into ../../archive/<host>/ (eff-certbot docs, "Where are my certificates?": point a server at them "or create symlinks"). The guide had `ln -s /etc/letsencrypt/live/<host> certs` and the nginx fragment mounted live/<host> alone: inside the container the links led into a directory it did not have - postfix-config.sh's [ -f ] false, no copy, Postfix without a certificate - and the panel's backup, reading certs/ through /selfpost-deploy, skipped links silently. Now certs/fullchain.pem and certs/privkey.pem are links to /etc/letsencrypt/live/<host>/, and /etc/letsencrypt is mounted read-only at the same path: a commented line in deploy/docker-compose.yml for the Apache scenario, mounted in the nginx fragment (./nginx/certbot-etc). The guide's steps add certbot's own commands for a server that is not root - chmod 0755 live archive, chgrp 998 + chmod 0640 the key, which certbot keeps on renewal - so the panel reads them (Health, backup). Postfix's side follows links already ([ -f ], cp, cmp, openssl). The vhost comment says the same. The backup table: links are followed by the panel's backup; a stopped tar stores them as links unless -h (GNU tar). internal/backup: addTree follows, for certs/ only, a link to a regular file and archives that file under the link's name; a link to nothing reachable is an error naming it and the mount. Tests: TestCertsLinksAreFollowed (certs/ -> live/ -> archive/, the archive holds the files; the tree gone, Check names the dangling link; Linux). Stand (2026-10-15, make e2e, exit 0): the stand mounts ./letsencrypt at /etc/letsencrypt; new certbot_layout_followed puts a new pair into letsencrypt/archive/, relative links in live/, absolute links in certs/, recreates the container: 465 serves the new serial, Health names no renewal waiting, and the full backup holds certs/*.pem as regular files with the archive's bytes. go vet and go test ./... pass on the server (24 packages); the Linux-only tests of this session run there and pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
362 lines
12 KiB
Go
362 lines
12 KiB
Go
package backup
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mixeme/selfpost/internal/store"
|
|
)
|
|
|
|
// seedDataDir builds a realistic /data tree: a migrated SQLite database plus the
|
|
// DKIM key, SASL and transient files a backup must include or exclude. It also
|
|
// seeds a deploy root beside data/ with compose, .env, and certs/.
|
|
func seedDataDir(t *testing.T) (dataDir, dbPath, deployRoot string) {
|
|
t.Helper()
|
|
deployRoot = t.TempDir()
|
|
dataDir = filepath.Join(deployRoot, "data")
|
|
if err := os.MkdirAll(dataDir, 0o750); err != nil {
|
|
t.Fatalf("mkdir data: %v", err)
|
|
}
|
|
dbPath = filepath.Join(dataDir, "selfpost.db")
|
|
|
|
st, err := store.Open(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
if _, err := st.AddDomain("example.com", "selfpost"); err != nil {
|
|
t.Fatalf("add domain: %v", err)
|
|
}
|
|
if err := st.Close(); err != nil {
|
|
t.Fatalf("close store: %v", err)
|
|
}
|
|
|
|
writeFile(t, filepath.Join(dataDir, "opendkim", "keys", "example.com", "selfpost.private"), "PRIVATE KEY")
|
|
writeFile(t, filepath.Join(dataDir, "sasl", "sasldb2"), "SASLDB")
|
|
writeFile(t, filepath.Join(dataDir, "postfix", "sender_login_maps"), "@example.com login")
|
|
writeFile(t, filepath.Join(dataDir, "antispam", "deny_domain.map"), "evil.example\n")
|
|
writeFile(t, filepath.Join(dataDir, "postfix", "queue", "deferred", "sample"), "queue-file")
|
|
// Transient files that must NOT be archived.
|
|
writeFile(t, filepath.Join(dataDir, "setup-token"), "secret-token")
|
|
writeFile(t, filepath.Join(dataDir, "selfpost.db-wal"), "wal")
|
|
writeFile(t, filepath.Join(dataDir, "selfpost.db-shm"), "shm")
|
|
// Postfix's delivery log and its rotated files: diagnostic output, not
|
|
// state, and the bulkiest thing under /data. The line is composed by hand;
|
|
// only its absence from the archive matters.
|
|
writeFile(t, filepath.Join(dataDir, "log", "mail.log"), "Aug 8 07:26:41 mail postfix/smtp[1]: ABC: to=<a@example.net>, status=sent (ok)")
|
|
writeFile(t, filepath.Join(dataDir, "log", "mail.log.1"), "older")
|
|
writeFile(t, filepath.Join(deployRoot, ComposeFileName), "services:\n selfpost:\n image: test\n")
|
|
writeFile(t, filepath.Join(deployRoot, EnvFileName), "SELFPOST_HOSTNAME=mail.example.com\n")
|
|
writeFile(t, filepath.Join(deployRoot, CertsDirName, "fullchain.pem"), "CERT")
|
|
writeFile(t, filepath.Join(deployRoot, CertsDirName, "privkey.pem"), "KEY")
|
|
return dataDir, dbPath, deployRoot
|
|
}
|
|
|
|
func writeFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
|
|
t.Fatalf("mkdir %s: %v", path, err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o640); err != nil {
|
|
t.Fatalf("write %s: %v", path, err)
|
|
}
|
|
}
|
|
|
|
// readArchive returns the set of regular-file entries (name -> content) in a
|
|
// gzip tar produced by Create.
|
|
func readArchive(t *testing.T, data []byte) map[string]string {
|
|
t.Helper()
|
|
gz, err := gzip.NewReader(bytes.NewReader(data))
|
|
if err != nil {
|
|
t.Fatalf("gzip: %v", err)
|
|
}
|
|
tr := tar.NewReader(gz)
|
|
out := map[string]string{}
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("tar next: %v", err)
|
|
}
|
|
if hdr.Typeflag != tar.TypeReg {
|
|
continue
|
|
}
|
|
b, err := io.ReadAll(tr)
|
|
if err != nil {
|
|
t.Fatalf("tar read %s: %v", hdr.Name, err)
|
|
}
|
|
out[hdr.Name] = string(b)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestCreateIncludesStateExcludesTransient(t *testing.T) {
|
|
dataDir, dbPath, deployRoot := seedDataDir(t)
|
|
|
|
var buf bytes.Buffer
|
|
if err := Create(&buf, Params{DataDir: dataDir, DBPath: dbPath, Version: "1.2.3", DeployRoot: deployRoot}); err != nil {
|
|
t.Fatalf("Create: %v", err)
|
|
}
|
|
files := readArchive(t, buf.Bytes())
|
|
|
|
// Present.
|
|
for _, name := range []string{
|
|
DataArchivePrefix + ManifestName,
|
|
DataArchivePrefix + "selfpost.db",
|
|
DataArchivePrefix + "opendkim/keys/example.com/selfpost.private",
|
|
DataArchivePrefix + "sasl/sasldb2",
|
|
DataArchivePrefix + "postfix/sender_login_maps",
|
|
// The filter lists as the spam filter reads them. They are rewritten
|
|
// from the database on the first start after a restore; carrying them
|
|
// means the filter has its lists before the panel is up.
|
|
DataArchivePrefix + "antispam/deny_domain.map",
|
|
ComposeFileName,
|
|
EnvFileName,
|
|
CertsDirName + "/fullchain.pem",
|
|
CertsDirName + "/privkey.pem",
|
|
} {
|
|
if _, ok := files[name]; !ok {
|
|
t.Errorf("archive missing %s", name)
|
|
}
|
|
}
|
|
// Excluded.
|
|
for _, name := range []string{
|
|
DataArchivePrefix + "setup-token",
|
|
DataArchivePrefix + "selfpost.db-wal",
|
|
DataArchivePrefix + "selfpost.db-shm",
|
|
DataArchivePrefix + "log/mail.log",
|
|
DataArchivePrefix + "log/mail.log.1",
|
|
DataArchivePrefix + "postfix/queue/deferred/sample",
|
|
} {
|
|
if _, ok := files[name]; ok {
|
|
t.Errorf("archive should not contain %s", name)
|
|
}
|
|
}
|
|
|
|
// Manifest is well-formed and carries the version.
|
|
var m Manifest
|
|
if err := json.Unmarshal([]byte(files[DataArchivePrefix+ManifestName]), &m); err != nil {
|
|
t.Fatalf("manifest json: %v", err)
|
|
}
|
|
if m.Format != FormatFull || m.Version != "1.2.3" {
|
|
t.Errorf("manifest = %+v, want format=%s version=1.2.3", m, FormatFull)
|
|
}
|
|
|
|
// The archived selfpost.db is a real, openable SQLite snapshot with our data.
|
|
snapPath := filepath.Join(t.TempDir(), "restored.db")
|
|
if err := os.WriteFile(snapPath, []byte(files[DataArchivePrefix+"selfpost.db"]), 0o640); err != nil {
|
|
t.Fatalf("write snapshot: %v", err)
|
|
}
|
|
st, err := store.Open(snapPath)
|
|
if err != nil {
|
|
t.Fatalf("open snapshot: %v", err)
|
|
}
|
|
defer st.Close()
|
|
domains, err := st.ListDomains()
|
|
if err != nil {
|
|
t.Fatalf("list domains from snapshot: %v", err)
|
|
}
|
|
if len(domains) != 1 || domains[0].Name != "example.com" {
|
|
t.Errorf("snapshot domains = %+v, want one example.com", domains)
|
|
}
|
|
}
|
|
|
|
func TestCreateRequiresDeployRoot(t *testing.T) {
|
|
dataDir, dbPath, _ := seedDataDir(t)
|
|
var buf bytes.Buffer
|
|
if err := Create(&buf, Params{DataDir: dataDir, DBPath: dbPath, Version: "1.0.0"}); err == nil {
|
|
t.Fatal("Create without DeployRoot succeeded")
|
|
}
|
|
}
|
|
|
|
func writeManifest(t *testing.T, dir, format, version string) string {
|
|
t.Helper()
|
|
path := filepath.Join(dir, ManifestName)
|
|
b, _ := json.Marshal(Manifest{Format: format, Version: version, CreatedAt: "now"})
|
|
if err := os.WriteFile(path, b, 0o600); err != nil {
|
|
t.Fatalf("write manifest: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestCheckRestoreNoManifestIsNormalStart(t *testing.T) {
|
|
restored, err := CheckRestore(filepath.Join(t.TempDir(), "manifest.json"), "1.0.0")
|
|
if err != nil {
|
|
t.Errorf("CheckRestore with no manifest = %v, want nil", err)
|
|
}
|
|
if restored {
|
|
t.Error("CheckRestore with no manifest reported a restore")
|
|
}
|
|
}
|
|
|
|
func TestCheckRestoreMatchConsumesManifest(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := writeManifest(t, dir, FormatFull, "1.0.0")
|
|
restored, err := CheckRestore(path, "1.0.0")
|
|
if err != nil {
|
|
t.Fatalf("CheckRestore matching = %v, want nil", err)
|
|
}
|
|
if !restored {
|
|
t.Fatal("CheckRestore matching did not report a restore")
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Errorf("manifest should be consumed after a matching restore, stat err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCheckRestoreVersionMismatchRefusesAndKeeps(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := writeManifest(t, dir, FormatFull, "1.0.0")
|
|
restored, err := CheckRestore(path, "2.0.0")
|
|
if err == nil {
|
|
t.Fatal("CheckRestore mismatch = nil, want error")
|
|
}
|
|
if restored {
|
|
t.Error("CheckRestore mismatch reported a restore")
|
|
}
|
|
if !strings.Contains(err.Error(), "1.0.0") || !strings.Contains(err.Error(), "2.0.0") {
|
|
t.Errorf("error should name both versions: %v", err)
|
|
}
|
|
if _, statErr := os.Stat(path); statErr != nil {
|
|
t.Errorf("manifest must be kept on mismatch so the operator can switch images: %v", statErr)
|
|
}
|
|
}
|
|
|
|
func TestCheckRestoreWrongFormatRejected(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := writeManifest(t, dir, "something-else", "1.0.0")
|
|
restored, err := CheckRestore(path, "1.0.0")
|
|
if err == nil {
|
|
t.Error("CheckRestore accepted a non-backup manifest")
|
|
}
|
|
if restored {
|
|
t.Error("CheckRestore wrong format reported a restore")
|
|
}
|
|
}
|
|
|
|
// No archive holds the Postfix queue; the rest is there, the private key
|
|
// included, and Check agrees that it can be made.
|
|
func TestCreateLeavesTheQueueOut(t *testing.T) {
|
|
dataDir, dbPath, deployRoot := seedDataDir(t)
|
|
p := Params{DataDir: dataDir, DBPath: dbPath, Version: "1.2.3", DeployRoot: deployRoot}
|
|
|
|
if err := Check(p); err != nil {
|
|
t.Fatalf("Check: %v", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := Create(&buf, p); err != nil {
|
|
t.Fatalf("Create: %v", err)
|
|
}
|
|
files := readArchive(t, buf.Bytes())
|
|
for name := range files {
|
|
if strings.HasPrefix(name, DataArchivePrefix+QueueDir) {
|
|
t.Errorf("the panel's archive holds %s", name)
|
|
}
|
|
}
|
|
for _, name := range []string{
|
|
DataArchivePrefix + "selfpost.db",
|
|
DataArchivePrefix + "opendkim/keys/example.com/selfpost.private",
|
|
DataArchivePrefix + "sasl/sasldb2",
|
|
DataArchivePrefix + "postfix/sender_login_maps",
|
|
ComposeFileName, EnvFileName,
|
|
CertsDirName + "/fullchain.pem",
|
|
CertsDirName + "/privkey.pem",
|
|
} {
|
|
if _, ok := files[name]; !ok {
|
|
t.Errorf("archive missing %s", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A file under certs/ that may not be read is told apart from any other
|
|
// failure, so the panel can say what to do about it.
|
|
func TestUnreadableCert(t *testing.T) {
|
|
root := filepath.Join("srv", "deploy")
|
|
denied := func(path string) error {
|
|
return fmt.Errorf("backup: open %s: %w", path, &fs.PathError{Op: "open", Path: path, Err: fs.ErrPermission})
|
|
}
|
|
if name, ok := UnreadableCert(denied(filepath.Join(root, "certs", "privkey.pem")), root); !ok || name != "certs/privkey.pem" {
|
|
t.Errorf("certs/privkey.pem: got %q, %v", name, ok)
|
|
}
|
|
if name, ok := UnreadableCert(denied(filepath.Join(root, ".env")), root); ok {
|
|
t.Errorf(".env was taken for a certificate file: %q", name)
|
|
}
|
|
if name, ok := UnreadableCert(denied(filepath.Join("data", "postfix", "x")), root); ok {
|
|
t.Errorf("a data file was taken for a certificate file: %q", name)
|
|
}
|
|
missing := &fs.PathError{Op: "open", Path: filepath.Join(root, "certs", "privkey.pem"), Err: fs.ErrNotExist}
|
|
if _, ok := UnreadableCert(missing, root); ok {
|
|
t.Error("a missing file was taken for one that may not be read")
|
|
}
|
|
}
|
|
|
|
// Check finds what Create would stumble over, before anything is written.
|
|
func TestCheckReportsWhatCannotBeRead(t *testing.T) {
|
|
dataDir, dbPath, deployRoot := seedDataDir(t)
|
|
if err := os.Remove(filepath.Join(deployRoot, EnvFileName)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Check(Params{DataDir: dataDir, DBPath: dbPath, Version: "1.2.3", DeployRoot: deployRoot}); err == nil {
|
|
t.Fatal("Check passed with .env missing from the deploy root")
|
|
}
|
|
}
|
|
|
|
// certs/ as certbot's advice leaves it: links into live/, which are relative
|
|
// links into archive/ (C-Q4). The archive holds the files, under the links'
|
|
// names; a link to nothing reachable stops the backup, naming it.
|
|
func TestCertsLinksAreFollowed(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("symbolic links need privileges on Windows; the container is Linux")
|
|
}
|
|
dataDir, dbPath, deployRoot := seedDataDir(t)
|
|
le := t.TempDir()
|
|
writeFile(t, filepath.Join(le, "archive", "mail.example.com", "fullchain1.pem"), "CERT FROM CERTBOT")
|
|
writeFile(t, filepath.Join(le, "archive", "mail.example.com", "privkey1.pem"), "KEY FROM CERTBOT")
|
|
live := filepath.Join(le, "live", "mail.example.com")
|
|
if err := os.MkdirAll(live, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certs := filepath.Join(deployRoot, CertsDirName)
|
|
for _, f := range []string{"fullchain", "privkey"} {
|
|
if err := os.Symlink("../../archive/mail.example.com/"+f+"1.pem", filepath.Join(live, f+".pem")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Remove(filepath.Join(certs, f+".pem"))
|
|
if err := os.Symlink(filepath.Join(live, f+".pem"), filepath.Join(certs, f+".pem")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
p := Params{DataDir: dataDir, DBPath: dbPath, Version: "1.2.3", DeployRoot: deployRoot}
|
|
if err := Check(p); err != nil {
|
|
t.Fatalf("Check: %v", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := Create(&buf, p); err != nil {
|
|
t.Fatalf("Create: %v", err)
|
|
}
|
|
files := readArchive(t, buf.Bytes())
|
|
if files[CertsDirName+"/fullchain.pem"] != "CERT FROM CERTBOT" || files[CertsDirName+"/privkey.pem"] != "KEY FROM CERTBOT" {
|
|
t.Fatalf("certs in the archive: %q, %q", files[CertsDirName+"/fullchain.pem"], files[CertsDirName+"/privkey.pem"])
|
|
}
|
|
|
|
// /etc/letsencrypt not mounted: the link leads nowhere.
|
|
if err := os.RemoveAll(le); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := Check(p)
|
|
if err == nil || !strings.Contains(err.Error(), "certs/fullchain.pem is a link to a file this container cannot reach") {
|
|
t.Fatalf("Check with a dangling link: %v", err)
|
|
}
|
|
}
|