From 49d9d0c1cdc9af91cac1ad674bb8d1512b5d2c04 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 21 Oct 2026 20:02:49 -0700 Subject: [PATCH] iam: test legacy identity.json write racing the migration rename Teach the fake filer server AtomicRenameEntry with a pre-rename hook, and cover both the clean migration path and a legacy write that lands between the load-time read and the rename. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../filer_etc/filer_etc_identity_test.go | 62 +++++++++++++++++++ .../filer_etc/filer_etc_policy_test.go | 24 +++++++ 2 files changed, 86 insertions(+) diff --git a/weed/credential/filer_etc/filer_etc_identity_test.go b/weed/credential/filer_etc/filer_etc_identity_test.go index 27d182c234..14c79b79d0 100644 --- a/weed/credential/filer_etc/filer_etc_identity_test.go +++ b/weed/credential/filer_etc/filer_etc_identity_test.go @@ -2,6 +2,7 @@ package filer_etc import ( "context" + "sync" "testing" "github.com/seaweedfs/seaweedfs/weed/filer" @@ -142,3 +143,64 @@ func TestLoadConfigurationSucceedsOnValidFiles(t *testing.T) { } assert.ElementsMatch(t, []string{"alice", "bob"}, names) } + +// A legacy client that rewrites identity.json between the migration's +// load-time read and its rename strands the newer content in +// identity.json.old. The migration must pick up those entries instead of +// losing them. +func TestLoadConfigurationMigratesLegacyWriteRacingWithRename(t *testing.T) { + ctx := context.Background() + store, server := newPolicyTestStoreWithServer(t) + + v1 := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]}]}`) + v2 := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]},{"name":"bob","credentials":[{"accessKey":"AK2","secretKey":"SK2"}]}]}`) + putEntry(t, server, filer.IamConfigDirectory, IamLegacyIdentityFile, v1) + + var once sync.Once + server.beforeAtomicRename = func(dir, name string) { + once.Do(func() { + server.mu.Lock() + defer server.mu.Unlock() + key := filerEntryKey(dir, name) + entry := cloneEntry(server.entries[key]) + entry.Content = v2 + server.entries[key] = entry + }) + } + + _, err := store.LoadConfiguration(ctx) + require.NoError(t, err) + + bob, err := store.GetUser(ctx, "bob") + require.NoError(t, err) + assert.Equal(t, "bob", bob.Name) + + alice, err := store.GetUser(ctx, "alice") + require.NoError(t, err) + assert.Equal(t, "alice", alice.Name) + + _, found, err := store.readInsideFiler(ctx, filer.IamConfigDirectory, IamLegacyIdentityFile) + require.NoError(t, err) + assert.False(t, found) +} + +// Without a concurrent write, the migration still renames the legacy file and +// writes out every identity. +func TestLoadConfigurationMigratesLegacyFile(t *testing.T) { + ctx := context.Background() + store, server := newPolicyTestStoreWithServer(t) + + legacy := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]}]}`) + putEntry(t, server, filer.IamConfigDirectory, IamLegacyIdentityFile, legacy) + + _, err := store.LoadConfiguration(ctx) + require.NoError(t, err) + + alice, err := store.GetUser(ctx, "alice") + require.NoError(t, err) + assert.Equal(t, "alice", alice.Name) + + _, found, err := store.readInsideFiler(ctx, filer.IamConfigDirectory, IamLegacyIdentityFile) + require.NoError(t, err) + assert.False(t, found) +} diff --git a/weed/credential/filer_etc/filer_etc_policy_test.go b/weed/credential/filer_etc/filer_etc_policy_test.go index 47b034ebc9..eb0823a1b1 100644 --- a/weed/credential/filer_etc/filer_etc_policy_test.go +++ b/weed/credential/filer_etc/filer_etc_policy_test.go @@ -32,6 +32,7 @@ type policyTestFilerServer struct { afterListEntry func(string, string) beforeDelete func(string, string) error beforeUpdate func(string, string) error + beforeAtomicRename func(string, string) } func newPolicyTestFilerServer() *policyTestFilerServer { @@ -139,6 +140,29 @@ func (s *policyTestFilerServer) DeleteEntry(_ context.Context, req *filer_pb.Del return &filer_pb.DeleteEntryResponse{}, nil } +func (s *policyTestFilerServer) AtomicRenameEntry(_ context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) { + s.mu.RLock() + beforeAtomicRename := s.beforeAtomicRename + s.mu.RUnlock() + if beforeAtomicRename != nil { + beforeAtomicRename(req.OldDirectory, req.OldName) + } + + s.mu.Lock() + defer s.mu.Unlock() + + oldKey := filerEntryKey(req.OldDirectory, req.OldName) + entry, found := s.entries[oldKey] + if !found { + return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error()) + } + delete(s.entries, oldKey) + renamed := cloneEntry(entry) + renamed.Name = req.NewName + s.entries[filerEntryKey(req.NewDirectory, req.NewName)] = renamed + return &filer_pb.AtomicRenameEntryResponse{}, nil +} + func newPolicyTestStore(t *testing.T) *FilerEtcStore { store, _ := newPolicyTestStoreWithServer(t) return store