| 1 | package backup |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestSignVerify(t *testing.T) { |
| 16 | sec := SigningSecret("bgbk_x") |
| 17 | now := time.Unix(1_800_000_000, 0) |
| 18 | body := []byte(`{"archive":"a"}`) |
| 19 | req, _ := http.NewRequest("POST", "http://b/api/v1/rollback", bytes.NewReader(body)) |
| 20 | Sign(req, sec, "inst", body, now) |
| 21 | if err := Verify(req, sec, body, now.Add(time.Minute)); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if Verify(req, sec, []byte(`{"archive":"b"}`), now) == nil { |
| 25 | t.Fatal("andere body geaccepteerd") |
| 26 | } |
| 27 | if Verify(req, SigningSecret("bgbk_y"), body, now) == nil { |
| 28 | t.Fatal("ander geheim geaccepteerd") |
| 29 | } |
| 30 | if Verify(req, sec, body, now.Add(10*time.Minute)) == nil { |
| 31 | t.Fatal("oude timestamp geaccepteerd") |
| 32 | } |
| 33 | req.URL.Path = "/api/v1/notify" |
| 34 | if Verify(req, sec, body, now) == nil { |
| 35 | t.Fatal("ander pad geaccepteerd") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestKeyLifecycle(t *testing.T) { |
| 40 | m := &Manager{Dir: t.TempDir()} |
| 41 | if _, ok := m.Check("bgbk_nope"); ok { |
| 42 | t.Fatal("check zonder key") |
| 43 | } |
| 44 | key, c, err := m.NewKey("tim") |
| 45 | if err != nil || !strings.HasPrefix(key, KeyPrefix) || c.KeyHash == "" { |
| 46 | t.Fatal(key, err) |
| 47 | } |
| 48 | raw, _ := os.ReadFile(filepath.Join(m.Dir, "backup.json")) |
| 49 | if strings.Contains(string(raw), key) { |
| 50 | t.Fatal("key staat leesbaar op schijf") |
| 51 | } |
| 52 | fi, _ := os.Stat(filepath.Join(m.Dir, "backup.json")) |
| 53 | if fi.Mode().Perm() != 0o600 { |
| 54 | t.Fatalf("mode %v", fi.Mode()) |
| 55 | } |
| 56 | if _, ok := m.Check(key); !ok { |
| 57 | t.Fatal("eigen key geweigerd") |
| 58 | } |
| 59 | if _, err := m.Connect("http://backup:9000/", "srv1"); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | c, _ = m.Load() |
| 63 | if !c.Connected() || c.ServerURL != "http://backup:9000" || c.SignSecret != SigningSecret(key) { |
| 64 | t.Fatalf("%+v", c) |
| 65 | } |
| 66 | key2, _, _ := m.NewKey("tim") |
| 67 | if _, ok := m.Check(key); ok { |
| 68 | t.Fatal("oude key werkt nog") |
| 69 | } |
| 70 | if _, ok := m.Check(key2); !ok { |
| 71 | t.Fatal("nieuwe key geweigerd") |
| 72 | } |
| 73 | if c, _ := m.Load(); c.Connected() { |
| 74 | t.Fatal("nieuwe key houdt oude server vast") |
| 75 | } |
| 76 | id1, _ := m.InstanceID() |
| 77 | id2, _ := m.InstanceID() |
| 78 | if id1 == "" || id1 != id2 { |
| 79 | t.Fatal("instance-id niet vast") |
| 80 | } |
| 81 | _ = m.Revoke() |
| 82 | if _, ok := m.Check(key2); ok { |
| 83 | t.Fatal("ingetrokken key werkt") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestCleanLogical(t *testing.T) { |
| 88 | good := []string{"state/bluegit.db", "state/secret.key", "state/ssh/host_ed25519", "data/repos/tim/a.git/HEAD", "data/releases/tim/a/v1/x.tgz"} |
| 89 | bad := []string{"", "/etc/passwd", "state/../x", "state/storage_path", "state/backup.json", "state/instance_id", "data/work/1/x", "data/releases/.incoming/x", "data/repos", "state/ssh", "x/y", "data/repos/../../etc", "state//secret.key", "data\\repos\\x"} |
| 90 | for _, p := range good { |
| 91 | if _, ok := CleanLogical(p); !ok { |
| 92 | t.Errorf("geweigerd: %s", p) |
| 93 | } |
| 94 | } |
| 95 | for _, p := range bad { |
| 96 | if _, ok := CleanLogical(p); ok { |
| 97 | t.Errorf("toegestaan: %s", p) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func write(t *testing.T, p, s string) { |
| 103 | t.Helper() |
| 104 | if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if err := os.WriteFile(p, []byte(s), 0o644); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func fakeTree(t *testing.T) Tree { |
| 113 | st, data := t.TempDir(), t.TempDir() |
| 114 | write(t, filepath.Join(st, "bluegit.db"), "live-db") |
| 115 | write(t, filepath.Join(st, "secret.key"), "k") |
| 116 | write(t, filepath.Join(st, "storage_path"), data) |
| 117 | write(t, filepath.Join(st, "backup.json"), "{}") |
| 118 | write(t, filepath.Join(st, "ssh", "host_ed25519"), "hk") |
| 119 | write(t, filepath.Join(data, "repos", "tim", "a.git", "HEAD"), "ref: refs/heads/master") |
| 120 | write(t, filepath.Join(data, "repos", "tim", "a.git", "index.lock"), "x") |
| 121 | write(t, filepath.Join(data, "releases", ".incoming", "x"), "x") |
| 122 | write(t, filepath.Join(data, "releases", "tim", "a", "v1", "a.bin"), "bin") |
| 123 | write(t, filepath.Join(data, "work", "1", "f"), "x") |
| 124 | _ = os.Symlink("/etc/passwd", filepath.Join(data, "repos", "tim", "evil")) |
| 125 | return Tree{StateDir: st, DataDir: data, Snapshot: func(dst string) error { |
| 126 | return os.WriteFile(dst, []byte("snapshot-db"), 0o600) |
| 127 | }} |
| 128 | } |
| 129 | |
| 130 | func TestManifestAndResolve(t *testing.T) { |
| 131 | tr := fakeTree(t) |
| 132 | m, err := tr.Build("inst") |
| 133 | if err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | var paths []string |
| 137 | for _, e := range m.Files { |
| 138 | paths = append(paths, e.Path) |
| 139 | } |
| 140 | got := strings.Join(paths, ",") |
| 141 | want := "data/releases/tim/a/v1/a.bin,data/repos/tim/a.git/HEAD,state/bluegit.db,state/secret.key,state/ssh/host_ed25519" |
| 142 | if got != want { |
| 143 | t.Fatalf("manifest\n got %s\nwant %s", got, want) |
| 144 | } |
| 145 | if m.Files[2].SHA256 == "" { |
| 146 | t.Fatal("database zonder hash") |
| 147 | } |
| 148 | p, err := tr.Resolve(DBPath) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | if b, _ := os.ReadFile(p); string(b) != "snapshot-db" { |
| 153 | t.Fatalf("database komt niet uit de kopie: %s", b) |
| 154 | } |
| 155 | for _, bad := range []string{"state/storage_path", "data/repos/tim/evil", "data/repos/../../etc/passwd"} { |
| 156 | if _, err := tr.Resolve(bad); err == nil { |
| 157 | t.Errorf("resolve %s toegestaan", bad) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | type tf struct { |
| 163 | name, body string |
| 164 | typ byte |
| 165 | } |
| 166 | |
| 167 | func tgz(t *testing.T, files []tf) []byte { |
| 168 | var buf bytes.Buffer |
| 169 | gz := gzip.NewWriter(&buf) |
| 170 | tw := tar.NewWriter(gz) |
| 171 | for _, f := range files { |
| 172 | typ := f.typ |
| 173 | if typ == 0 { |
| 174 | typ = tar.TypeReg |
| 175 | } |
| 176 | h := &tar.Header{Name: f.name, Mode: 0o644, Size: int64(len(f.body)), Typeflag: typ, ModTime: time.Unix(1_700_000_000, 0)} |
| 177 | if typ == tar.TypeSymlink { |
| 178 | h.Size, h.Linkname = 0, "/etc/passwd" |
| 179 | } |
| 180 | if err := tw.WriteHeader(h); err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | if typ == tar.TypeReg { |
| 184 | _, _ = tw.Write([]byte(f.body)) |
| 185 | } |
| 186 | } |
| 187 | tw.Close() |
| 188 | gz.Close() |
| 189 | return buf.Bytes() |
| 190 | } |
| 191 | |
| 192 | func TestStageAndApply(t *testing.T) { |
| 193 | tr := fakeTree(t) |
| 194 | archive := tgz(t, []tf{ |
| 195 | {name: "state/bluegit.db", body: "old-db"}, |
| 196 | {name: "state/secret.key", body: "old-key"}, |
| 197 | {name: "data/repos/tim/b.git/HEAD", body: "ref: refs/heads/main"}, |
| 198 | }) |
| 199 | n, err := tr.Stage(bytes.NewReader(archive)) |
| 200 | if err != nil || n != 3 { |
| 201 | t.Fatal(n, err) |
| 202 | } |
| 203 | if b, _ := os.ReadFile(filepath.Join(tr.StateDir, "bluegit.db")); string(b) != "live-db" { |
| 204 | t.Fatal("stage raakte de live database") |
| 205 | } |
| 206 | if !HasPending(tr.StateDir) { |
| 207 | t.Fatal("geen READY") |
| 208 | } |
| 209 | ok, err := ApplyPending(tr.StateDir) |
| 210 | if err != nil || !ok { |
| 211 | t.Fatal(ok, err) |
| 212 | } |
| 213 | check := func(p, want string) { |
| 214 | t.Helper() |
| 215 | b, err := os.ReadFile(p) |
| 216 | if want == "" { |
| 217 | if err == nil { |
| 218 | t.Errorf("%s bestaat nog", p) |
| 219 | } |
| 220 | return |
| 221 | } |
| 222 | if string(b) != want { |
| 223 | t.Errorf("%s = %q, wil %q", p, b, want) |
| 224 | } |
| 225 | } |
| 226 | check(filepath.Join(tr.StateDir, "bluegit.db"), "old-db") |
| 227 | check(filepath.Join(tr.StateDir, "secret.key"), "old-key") |
| 228 | check(filepath.Join(tr.StateDir, "ssh", "host_ed25519"), "") |
| 229 | check(filepath.Join(tr.StateDir, "backup.json"), "{}") |
| 230 | check(filepath.Join(tr.StateDir, "storage_path"), tr.DataDir) |
| 231 | check(filepath.Join(tr.DataDir, "repos", "tim", "b.git", "HEAD"), "ref: refs/heads/main") |
| 232 | check(filepath.Join(tr.DataDir, "repos", "tim", "a.git", "HEAD"), "") |
| 233 | check(filepath.Join(tr.DataDir, "releases", "tim", "a", "v1", "a.bin"), "") |
| 234 | check(filepath.Join(tr.DataDir, "work", "1", "f"), "x") |
| 235 | if HasPending(tr.StateDir) { |
| 236 | t.Fatal("READY blijft staan") |
| 237 | } |
| 238 | if ok, _ := ApplyPending(tr.StateDir); ok { |
| 239 | t.Fatal("tweede keer toegepast") |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func TestStageRejects(t *testing.T) { |
| 244 | cases := map[string][]tf{ |
| 245 | "traversal": {{name: "state/bluegit.db", body: "x"}, {name: "state/secret.key", body: "k"}, {name: "data/repos/../../../etc/x", body: "x"}}, |
| 246 | "symlink": {{name: "state/bluegit.db", body: "x"}, {name: "state/secret.key", body: "k"}, {name: "data/repos/a/l", typ: tar.TypeSymlink}}, |
| 247 | "onbekend": {{name: "state/bluegit.db", body: "x"}, {name: "state/secret.key", body: "k"}, {name: "state/storage_path", body: "/"}}, |
| 248 | "zonder-db": {{name: "state/secret.key", body: "k"}}, |
| 249 | "zonder-key": {{name: "state/bluegit.db", body: "x"}}, |
| 250 | } |
| 251 | for name, files := range cases { |
| 252 | tr := fakeTree(t) |
| 253 | if _, err := tr.Stage(bytes.NewReader(tgz(t, files))); err == nil { |
| 254 | t.Errorf("%s: geaccepteerd", name) |
| 255 | } |
| 256 | if HasPending(tr.StateDir) { |
| 257 | t.Errorf("%s: READY na fout", name) |
| 258 | } |
| 259 | if _, err := os.Stat(filepath.Join(tr.StateDir, ".restore")); err == nil { |
| 260 | t.Errorf("%s: staging blijft staan", name) |
| 261 | } |
| 262 | } |
| 263 | if _, err := fakeTree(t).Stage(strings.NewReader("geen gzip")); err == nil { |
| 264 | t.Error("geen gzip geaccepteerd") |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Vaste vector: bluegit-backup (internal/protocol) test dezelfde waarden. |
| 269 | func TestSignatureVector(t *testing.T) { |
| 270 | sec := SigningSecret("bgbk_test") |
| 271 | if sec != "c04d0bdbac2260c010f61c9b063b28026eed1d2292c928708708821449e72c2a" { |
| 272 | t.Fatalf("geheim %s", sec) |
| 273 | } |
| 274 | if got := Signature(sec, "post", "/api/v1/notify", "1800000000", []byte("{}")); got != "51c5ec72528304632383637b99dfda1e14b68194bbec2d4faa36c9d5ef892548" { |
| 275 | t.Fatalf("handtekening %s", got) |
| 276 | } |
| 277 | } |