sftp: read WORM rules from a filer.conf stored in chunks

ReadFilerConf without a master client reads only inline content, so a
chunked filer.conf looked empty and WORM went unenforced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-24 11:48:28 -08:00
1 parent 9cc3cc8779
commit c1375116d3
2 files changed
+62 -2

No files matched your search

+28 -1
View File
@@ -2,6 +2,7 @@
package sftpd
import (
"bytes"
"context"
"encoding/json"
"errors"
@@ -753,7 +754,7 @@ func (fs *SftpServer) wormEnforcedForEntry(p string, entry *filer_pb.Entry) (boo
if entry.WormEnforcedAtTsNs == 0 {
return false, nil
}
fc, err := filer.ReadFilerConf(fs.filerAddr, fs.grpcDialOption, nil)
fc, err := fs.readFilerConf()
if err != nil {
return false, err
}
@@ -767,6 +768,32 @@ func (fs *SftpServer) wormEnforcedForEntry(p string, entry *filer_pb.Entry) (boo
return time.Since(time.Unix(0, entry.WormEnforcedAtTsNs)).Seconds() < float64(rule.WormRetentionTimeSeconds), nil
}
func (fs *SftpServer) readFilerConf() (*filer.FilerConf, error) {
fc := filer.NewFilerConf()
confPath := string(util.NewFullPath(filer.DirectoryEtcSeaweedFS, filer.FilerConfName))
entry, err := fs.getEntry(confPath)
if errors.Is(err, os.ErrNotExist) {
return fc, nil
}
if err != nil {
return nil, err
}
content := entry.Content
if len(content) == 0 && len(entry.GetChunks()) > 0 {
var buf bytes.Buffer
if err := fs.copyExistingContent(confPath, &buf); err != nil {
return nil, fmt.Errorf("read %s: %w", confPath, err)
}
content = buf.Bytes()
}
if len(content) > 0 {
if err := fc.LoadFromBytes(content); err != nil {
return nil, fmt.Errorf("parse %s: %w", confPath, err)
}
}
return fc, nil
}
// objectLockState makes the filer refuse the delete if the entry's Object
// Lock state changed after it was checked.
func objectLockState(entry *filer_pb.Entry) map[string][]byte {
+34 -1
View File
@@ -5,6 +5,8 @@ import (
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"strconv"
"sync"
@@ -29,6 +31,7 @@ type lockTestFiler struct {
deletes []*filer_pb.DeleteEntryRequest
renames []*filer_pb.AtomicRenameEntryRequest
updates int
volume string
}
func (f *lockTestFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
@@ -57,7 +60,9 @@ const lockTestFilerConf = `{"locations":[{"locationPrefix":"/buckets/worm/","wor
func newLockTestServer(t *testing.T, entries map[string]*filer_pb.Entry) (*SftpServer, *lockTestFiler) {
t.Helper()
entries["/etc/seaweedfs/filer.conf"] = &filer_pb.Entry{Name: "filer.conf", Content: []byte(lockTestFilerConf), Attributes: &filer_pb.FuseAttributes{}}
if _, ok := entries["/etc/seaweedfs/filer.conf"]; !ok {
entries["/etc/seaweedfs/filer.conf"] = &filer_pb.Entry{Name: "filer.conf", Content: []byte(lockTestFilerConf), Attributes: &filer_pb.FuseAttributes{}}
}
for _, entry := range entries {
if entry.Attributes == nil {
entry.Attributes = &filer_pb.FuseAttributes{FileMode: 0644, Uid: 1000}
@@ -261,3 +266,31 @@ func TestRenameOntoItselfKeepsProtectedEntry(t *testing.T) {
t.Fatalf("rename onto itself: %v", err)
}
}
func (f *lockTestFiler) LookupVolume(_ context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
resp := &filer_pb.LookupVolumeResponse{LocationsMap: make(map[string]*filer_pb.Locations)}
for _, vid := range req.VolumeIds {
resp.LocationsMap[vid] = &filer_pb.Locations{Locations: []*filer_pb.Location{{Url: f.volume}}}
}
return resp, nil
}
func TestRemoveReadsChunkedFilerConf(t *testing.T) {
volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(lockTestFilerConf))
}))
defer volume.Close()
conf := &filer_pb.Entry{
Name: "filer.conf",
Attributes: &filer_pb.FuseAttributes{FileSize: uint64(len(lockTestFilerConf))},
Chunks: []*filer_pb.FileChunk{fileChunk("3,01", 0, int64(len(lockTestFilerConf)))},
}
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
"/etc/seaweedfs/filer.conf": conf,
"/buckets/worm/f": {Name: "f", WormEnforcedAtTsNs: time.Now().UnixNano()},
})
fake.volume = volume.Listener.Addr().String()
if err := fs.removeEntry("/buckets/worm/f"); !errors.Is(err, os.ErrPermission) {
t.Fatalf("rm under a WORM rule from a chunked filer.conf: got %v, want os.ErrPermission", err)
}
}