carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package objstore
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "testing"
9 "time"
10
11 "github.com/carlosframework/platform/internal/release"
12)
13
14func resultFixture(key, verdict, version string) *ScanResult {
15 return &ScanResult{
16 Key: key, Verdict: verdict, Version: version,
17 Engine: "clamav", Signature: "ClamAV 1.4.1/27500/x", At: time.Now().UTC(),
18 }
19}
20
21// THE test for this file, and the third appearance of this bug class in the
22// design's reviews.
23//
24// Without the version binding: v1 scanned, the tag not written because the
25// ten-tag cap fired, the result says clean; the tenant overwrites with v2; the
26// tag is still absent, so the door falls through to the result object and
27// answers clean for bytes nobody has read. That is the laundering the verdict
28// tag's own binding exists to stop, reintroduced by the mechanism meant to
29// close a gap in it.
30func TestResultObjectIsVersionBound(t *testing.T) {
31 ctx := context.Background()
32 s := release.NewDirStore(t.TempDir())
33
34 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil {
35 t.Fatal(err)
36 }
37
38 got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v1")
39 if err != nil || got == nil || got.Verdict != "clean" {
40 t.Fatalf("read at v1 = %+v, %v", got, err)
41 }
42
43 // The object is overwritten. The result must not answer for the new bytes.
44 got, err = ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v2")
45 if err != nil {
46 t.Fatal(err)
47 }
48 if got != nil {
49 t.Fatalf("a result written for v1 answered for v2: %+v", got)
50 }
51}
52
53// The second write for a key is a REPLACE. release.Store.Put has only
54// create-only and replace, so a create-only second write returns
55// ErrCASConflict and silently drops the newer verdict — leaving the door on
56// "pending" until some sweep happens to tag the object.
57func TestResultObjectReplacesOnSecondWrite(t *testing.T) {
58 ctx := context.Background()
59 s := release.NewDirStore(t.TempDir())
60
61 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil {
62 t.Fatal(err)
63 }
64 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "infected", "v2")); err != nil {
65 t.Fatalf("second write: %v", err)
66 }
67
68 got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v2")
69 if err != nil || got == nil {
70 t.Fatalf("read at v2 = %+v, %v", got, err)
71 }
72 if got.Verdict != "infected" {
73 t.Fatalf("verdict = %q, want the v2 verdict — the newer one was dropped", got.Verdict)
74 }
75}
76
77// Absent is a state, not an error.
78func TestReadScanResultAbsent(t *testing.T) {
79 ctx := context.Background()
80 s := release.NewDirStore(t.TempDir())
81
82 got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/never", "v1")
83 if err != nil || got != nil {
84 t.Fatalf("= %+v, %v", got, err)
85 }
86}
87
88// An unknown schema refuses rather than being read as a verdict. Same rule as
89// the record and the sidecar, and for the same reason: this one can be the
90// only thing standing between an app and a payload.
91func TestScanResultSchemaRefusesUnknown(t *testing.T) {
92 ctx := context.Background()
93 s := release.NewDirStore(t.TempDir())
94
95 if _, err := s.Put(ctx, ScanResultKey("bab", "titogo", "uploads/x"),
96 []byte(`{"schema":99,"key":"uploads/x","verdict":"clean","version":"v1"}`), ""); err != nil {
97 t.Fatal(err)
98 }
99 if _, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v1"); err == nil {
100 t.Fatal("an unknown schema was read as a verdict")
101 }
102}
103
104// The result lives under the APP's prefix, so PurgeApp removes it and the
105// name-claim gate can see it. A top-level results prefix would survive app
106// deletion invisibly — and, because materialize counts any key under an app
107// prefix and fails toward keeping the row, would resurrect the deleted app.
108func TestScanResultKeyIsUnderTheAppPrefix(t *testing.T) {
109 got := ScanResultKey("bab", "titogo", "uploads/x")
110 if want := release.AppPrefix("bab", "titogo"); len(got) <= len(want) || got[:len(want)] != want {
111 t.Fatalf("key %q is not under %q", got, want)
112 }
113 // Distinct object keys must not collide.
114 if ScanResultKey("bab", "titogo", "a") == ScanResultKey("bab", "titogo", "b") {
115 t.Fatal("two object keys share a result object")
116 }
117 // PINNED, not merely "deterministic". A key that is stable within one
118 // build is stable enough to pass a self-comparison and still be a
119 // different key from the one the last build wrote — and every result
120 // written before the change becomes an object nothing will ever read or
121 // delete, under the prefix that decides whether the app's name can be
122 // claimed again.
123 if want := release.AppPrefix("bab", "titogo") +
124 "scan-results/c681b33175d57d52dc74238a2e327f0982614eeab6142d8e893dd83cb617488b.json"; got != want {
125 t.Fatalf("key = %q, want %q — the key format moved and the old results are now unreachable", got, want)
126 }
127}
128
129// The fallback set is deleted with the app's consent, and swept by age
130// otherwise. Nothing else removes one: the drain drops a result only when a
131// LATER scan records a real verdict on the same key, which for a permanently
132// untaggable object never happens.
133func TestResultObjectCollectedByAge(t *testing.T) {
134 ctx := context.Background()
135 dir := t.TempDir()
136 s := release.NewDirStore(dir)
137
138 for _, k := range []string{"uploads/old", "uploads/new"} {
139 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture(k, "clean", "v1")); err != nil {
140 t.Fatal(err)
141 }
142 }
143 old := time.Now().Add(-48 * time.Hour)
144 aged := filepath.Join(dir, filepath.FromSlash(ScanResultKey("bab", "titogo", "uploads/old")))
145 if err := os.Chtimes(aged, old, old); err != nil {
146 t.Fatal(err)
147 }
148
149 n, err := CollectScanResults(ctx, s, "bab", "titogo", time.Now().Add(-QueueTTL))
150 if err != nil {
151 t.Fatal(err)
152 }
153 if n != 1 {
154 t.Fatalf("collected %d, want the one past its TTL", n)
155 }
156 if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/old")); !errors.Is(err, os.ErrNotExist) {
157 t.Fatalf("the aged result survived: %v", err)
158 }
159 if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/new")); err != nil {
160 t.Fatalf("the age sweep took a result still inside its TTL: %v", err)
161 }
162}
163
164// A store that cannot rank by modification time is SKIPPED, not an error —
165// the same degradation storagePass accepts for SizeLister. A tick that failed
166// over a capability a fake lacks would be a tick that stopped draining.
167func TestCollectScanResultsWithoutModLister(t *testing.T) {
168 if _, err := CollectScanResults(context.Background(), plainStore{}, "bab", "titogo", time.Now()); !errors.Is(err, ErrNoModLister) {
169 t.Fatalf("err = %v, want ErrNoModLister", err)
170 }
171}
172
173// plainStore implements release.Store and nothing optional.
174type plainStore struct{ release.Store }
175
176// A whole app's results go when the app's consent does.
177func TestDeleteScanResults(t *testing.T) {
178 ctx := context.Background()
179 s := release.NewDirStore(t.TempDir())
180 for _, k := range []string{"uploads/a", "uploads/b"} {
181 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture(k, "clean", "v1")); err != nil {
182 t.Fatal(err)
183 }
184 }
185 if err := DeleteScanResults(ctx, s, "bab", "titogo"); err != nil {
186 t.Fatal(err)
187 }
188 left, err := s.List(ctx, ScanResultsPrefix("bab", "titogo"))
189 if err != nil || len(left) != 0 {
190 t.Fatalf("results survived: %v, %v", left, err)
191 }
192 // Deleting an empty set is not an error.
193 if err := DeleteScanResults(ctx, s, "bab", "titogo"); err != nil {
194 t.Fatal(err)
195 }
196}
197
198// Deleted when a later scan DOES persist a verdict, so the fallback cannot
199// outlive the real answer. Deleting an absent one is not an error.
200func TestDeleteScanResult(t *testing.T) {
201 ctx := context.Background()
202 s := release.NewDirStore(t.TempDir())
203
204 if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil {
205 t.Fatal(err)
206 }
207 if err := DeleteScanResult(ctx, s, "bab", "titogo", "uploads/x"); err != nil {
208 t.Fatal(err)
209 }
210 if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/x")); !errors.Is(err, os.ErrNotExist) {
211 t.Fatalf("result survived deletion: %v", err)
212 }
213 if err := DeleteScanResult(ctx, s, "bab", "titogo", "uploads/x"); err != nil {
214 t.Fatalf("deleting an absent result: %v", err)
215 }
216}
217