Skip to content

Commit 6b0fbff

Browse files
alexwlchannickkhyl
authored andcommitted
tka: move RemoveAll() to CompactableChonk
I added a RemoveAll() method on tka.Chonk in tailscale#17946, but it's only used in the node to purge local AUMs. We don't need it in the SQLite storage, which currently implements tka.Chonk, so move it to CompactableChonk instead. Also add some automated tests, as a safety net. Updates tailscale/corp#33599 Change-Id: I54de9ccf1d6a3d29b36a94eccb0ebd235acd4ebc Signed-off-by: Alex Chan <[email protected]> (cherry picked from commit c17ba64)
1 parent 90d3cb3 commit 6b0fbff

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

tka/tailchonk.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ type Chonk interface {
5858
// as a hint to pick the correct chain in the event that the Chonk stores
5959
// multiple distinct chains.
6060
LastActiveAncestor() (*AUMHash, error)
61-
62-
// RemoveAll permanently and completely clears the TKA state. This should
63-
// be called when the user disables Tailnet Lock.
64-
RemoveAll() error
6561
}
6662

6763
// CompactableChonk implementation are extensions of Chonk, which are
@@ -80,6 +76,10 @@ type CompactableChonk interface {
8076
// PurgeAUMs permanently and irrevocably deletes the specified
8177
// AUMs from storage.
8278
PurgeAUMs(hashes []AUMHash) error
79+
80+
// RemoveAll permanently and completely clears the TKA state. This should
81+
// be called when the user disables Tailnet Lock.
82+
RemoveAll() error
8383
}
8484

8585
// Mem implements in-memory storage of TKA state, suitable for

tstest/chonktest/chonktest.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package chonktest
99
import (
1010
"bytes"
1111
"encoding/binary"
12+
"errors"
1213
"math/rand"
1314
"os"
1415
"testing"
@@ -253,4 +254,50 @@ func RunCompactableChonkTests(t *testing.T, newChonk func(t *testing.T) tka.Comp
253254
t.Fatalf("ChildAUMs() output differs (-want, +got):\n%s", diff)
254255
}
255256
})
257+
258+
t.Run("RemoveAll", func(t *testing.T) {
259+
t.Parallel()
260+
chonk := newChonk(t)
261+
parentHash := randHash(t, 1)
262+
data := []tka.AUM{
263+
{
264+
MessageKind: tka.AUMRemoveKey,
265+
KeyID: []byte{1, 2},
266+
PrevAUMHash: parentHash[:],
267+
},
268+
{
269+
MessageKind: tka.AUMRemoveKey,
270+
KeyID: []byte{3, 4},
271+
PrevAUMHash: parentHash[:],
272+
},
273+
}
274+
275+
if err := chonk.CommitVerifiedAUMs(data); err != nil {
276+
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
277+
}
278+
279+
// Check we can retrieve the AUMs we just stored
280+
for _, want := range data {
281+
got, err := chonk.AUM(want.Hash())
282+
if err != nil {
283+
t.Fatalf("could not get %s: %v", want.Hash(), err)
284+
}
285+
if diff := cmp.Diff(want, got); diff != "" {
286+
t.Errorf("stored AUM %s differs (-want, +got):\n%s", want.Hash(), diff)
287+
}
288+
}
289+
290+
// Call RemoveAll() to drop all the AUM state
291+
if err := chonk.RemoveAll(); err != nil {
292+
t.Fatalf("RemoveAll failed: %v", err)
293+
}
294+
295+
// Check we can no longer retrieve the previously-stored AUMs
296+
for _, want := range data {
297+
aum, err := chonk.AUM(want.Hash())
298+
if !errors.Is(err, os.ErrNotExist) {
299+
t.Fatalf("expected os.ErrNotExist for %s, instead got aum=%v, err=%v", want.Hash(), aum, err)
300+
}
301+
}
302+
})
256303
}

0 commit comments

Comments
 (0)