|
| 1 | +package backupcontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + "k8s.io/client-go/kubernetes/scheme" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 17 | + |
| 18 | + backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1" |
| 19 | +) |
| 20 | + |
| 21 | +func backupWithSnapshot(name, raw string) *backupsv1alpha1.Backup { |
| 22 | + b := &backupsv1alpha1.Backup{ |
| 23 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-a"}, |
| 24 | + } |
| 25 | + if raw != "" { |
| 26 | + b.Status.UnderlyingResources = &runtime.RawExtension{Raw: []byte(raw)} |
| 27 | + } |
| 28 | + return b |
| 29 | +} |
| 30 | + |
| 31 | +func legacyCNPGSnapshot() string { |
| 32 | + return `{"kind":"CNPGBackupSnapshot","apiVersion":"` + cnpgBackupSnapshotAPIVersion + `",` + |
| 33 | + `"databases":{"app":{"roles":{"admin":["alice"]}}},` + |
| 34 | + `"users":{"alice":{"password":"hunter2","replication":true},"bob":{"password":"s3cret"},"carol":{}},` + |
| 35 | + `"parameters":{"retention":"30d"},"futureField":{"n":12345678901234567890}}` |
| 36 | +} |
| 37 | + |
| 38 | +func newScrubberClient(objs ...client.Object) client.WithWatch { |
| 39 | + s := runtime.NewScheme() |
| 40 | + _ = scheme.AddToScheme(s) |
| 41 | + _ = backupsv1alpha1.AddToScheme(s) |
| 42 | + return clientfake.NewClientBuilder().WithScheme(s).WithObjects(objs...).Build() |
| 43 | +} |
| 44 | + |
| 45 | +func getSnapshot(t *testing.T, c client.Client, name string) map[string]any { |
| 46 | + t.Helper() |
| 47 | + var b backupsv1alpha1.Backup |
| 48 | + if err := c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-a", Name: name}, &b); err != nil { |
| 49 | + t.Fatalf("get Backup %s: %v", name, err) |
| 50 | + } |
| 51 | + if b.Status.UnderlyingResources == nil { |
| 52 | + return nil |
| 53 | + } |
| 54 | + var snap map[string]any |
| 55 | + dec := json.NewDecoder(bytes.NewReader(b.Status.UnderlyingResources.Raw)) |
| 56 | + dec.UseNumber() |
| 57 | + if err := dec.Decode(&snap); err != nil { |
| 58 | + t.Fatalf("decode snapshot of %s: %v", name, err) |
| 59 | + } |
| 60 | + return snap |
| 61 | +} |
| 62 | + |
| 63 | +func TestLegacyPasswordScrubber_RemovesPasswordsAndKeepsTheRest(t *testing.T) { |
| 64 | + c := newScrubberClient(backupWithSnapshot("legacy", legacyCNPGSnapshot())) |
| 65 | + |
| 66 | + if err := (&LegacyPasswordScrubber{Client: c}).Start(context.Background()); err != nil { |
| 67 | + t.Fatalf("Start: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + snap := getSnapshot(t, c, "legacy") |
| 71 | + users := snap["users"].(map[string]any) |
| 72 | + for name, u := range users { |
| 73 | + if _, ok := u.(map[string]any)["password"]; ok { |
| 74 | + t.Errorf("user %s still carries a password", name) |
| 75 | + } |
| 76 | + } |
| 77 | + if len(users) != 3 { |
| 78 | + t.Errorf("users = %v, want alice, bob and carol kept", users) |
| 79 | + } |
| 80 | + if got := users["alice"].(map[string]any)["replication"]; got != true { |
| 81 | + t.Errorf("alice.replication = %v, want true", got) |
| 82 | + } |
| 83 | + if got := snap["parameters"].(map[string]any)["retention"]; got != "30d" { |
| 84 | + t.Errorf("parameters.retention = %v, want 30d", got) |
| 85 | + } |
| 86 | + if _, ok := snap["databases"].(map[string]any)["app"]; !ok { |
| 87 | + t.Errorf("databases.app was dropped: %v", snap["databases"]) |
| 88 | + } |
| 89 | + if got := snap["futureField"].(map[string]any)["n"]; got != json.Number("12345678901234567890") { |
| 90 | + t.Errorf("unknown field lost precision or was dropped: %v", got) |
| 91 | + } |
| 92 | + |
| 93 | + // The restore path must still accept the scrubbed snapshot. |
| 94 | + var b backupsv1alpha1.Backup |
| 95 | + _ = c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-a", Name: "legacy"}, &b) |
| 96 | + if _, _, _, err := unmarshalCNPGBackupSnapshot(&b); err != nil { |
| 97 | + t.Errorf("scrubbed snapshot no longer decodes for restore: %v", err) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestLegacyPasswordScrubber_LeavesOtherBackupsUntouched(t *testing.T) { |
| 102 | + cases := map[string]string{ |
| 103 | + "clean-cnpg": `{"kind":"CNPGBackupSnapshot","apiVersion":"` + cnpgBackupSnapshotAPIVersion + `","users":{"alice":{}}}`, |
| 104 | + "other-kind": `{"kind":"SomeOtherSnapshot","apiVersion":"` + cnpgBackupSnapshotAPIVersion + `","users":{"alice":{"password":"keep"}}}`, |
| 105 | + "other-version": `{"kind":"CNPGBackupSnapshot","apiVersion":"example.com/v9","users":{"alice":{"password":"keep"}}}`, |
| 106 | + "no-snapshot": ``, |
| 107 | + } |
| 108 | + var objs []client.Object |
| 109 | + for name, raw := range cases { |
| 110 | + objs = append(objs, backupWithSnapshot(name, raw)) |
| 111 | + } |
| 112 | + c := newScrubberClient(objs...) |
| 113 | + before := map[string]string{} |
| 114 | + for name := range cases { |
| 115 | + var b backupsv1alpha1.Backup |
| 116 | + _ = c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-a", Name: name}, &b) |
| 117 | + before[name] = b.ResourceVersion |
| 118 | + } |
| 119 | + |
| 120 | + if err := (&LegacyPasswordScrubber{Client: c}).Start(context.Background()); err != nil { |
| 121 | + t.Fatalf("Start: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + for name := range cases { |
| 125 | + var b backupsv1alpha1.Backup |
| 126 | + _ = c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-a", Name: name}, &b) |
| 127 | + if b.ResourceVersion != before[name] { |
| 128 | + t.Errorf("%s was patched but carries nothing to scrub", name) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestLegacyPasswordScrubber_OneBadBackupDoesNotStopTheRest(t *testing.T) { |
| 134 | + c := newScrubberClient( |
| 135 | + backupWithSnapshot("not-an-object", `["not","an","object"]`), |
| 136 | + backupWithSnapshot("a-patch-fails", legacyCNPGSnapshot()), |
| 137 | + backupWithSnapshot("z-legacy", legacyCNPGSnapshot()), |
| 138 | + ) |
| 139 | + failing := interceptor.NewClient(c, interceptor.Funcs{ |
| 140 | + Patch: func(ctx context.Context, cl client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { |
| 141 | + if obj.GetName() == "a-patch-fails" { |
| 142 | + return errors.New("apiserver unavailable") |
| 143 | + } |
| 144 | + return cl.Patch(ctx, obj, patch, opts...) |
| 145 | + }, |
| 146 | + }) |
| 147 | + |
| 148 | + if err := (&LegacyPasswordScrubber{Client: failing}).Start(context.Background()); err != nil { |
| 149 | + t.Fatalf("Start must not fail the manager, got %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + users := getSnapshot(t, c, "z-legacy")["users"].(map[string]any) |
| 153 | + if _, ok := users["alice"].(map[string]any)["password"]; ok { |
| 154 | + t.Error("legacy Backup was not scrubbed after an earlier Backup failed") |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestLegacyPasswordScrubber_ListFailureDoesNotStopTheManager(t *testing.T) { |
| 159 | + failing := interceptor.NewClient(newScrubberClient(), interceptor.Funcs{ |
| 160 | + List: func(context.Context, client.WithWatch, client.ObjectList, ...client.ListOption) error { |
| 161 | + return errors.New("forbidden") |
| 162 | + }, |
| 163 | + }) |
| 164 | + if err := (&LegacyPasswordScrubber{Client: failing}).Start(context.Background()); err != nil { |
| 165 | + t.Fatalf("Start must not fail the manager, got %v", err) |
| 166 | + } |
| 167 | +} |
0 commit comments