mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
* sftp: read downloads as the file's logical content Downloads read through filer.NewFileReader, which stops at the last stored byte: holes between chunks, a tail no chunk stores and the zeros after partial inline content came back missing, so sparse files downloaded short or with later bytes shifted. Read chunked files through filer.ChunkReadAt with one shared ReaderCache, set up as WebDAV's: it serves FileSize bytes, zero-fills what no chunk stores, resolves manifests, prefetches and re-looks-up moved volumes. Closing a handle only releases its stream, as S3 does. Inline content is read up to the file's size, then zeros. This replaces sftpd's own block cache, whose LRU helper is now unused. * sftp: read large downloads in sections, as the FUSE mount does ChunkReadAt walks the file's chunk views from the start on every read, so a sequential download of a file with many chunks costs time quadratic in its chunk count. Read through filer.ChunkGroup instead, the reader of a mount's file handle: it keeps chunks in 64 MiB sections, returns EOF at the file size for any offset, and holds every open file's buffers to one shared budget (the mount's default 256 MiB). * sftp: cache remote-only content on open instead of refusing it A file whose content only a remote store holds is copied into the cluster with CacheRemoteObjectToLocalCluster when it is opened for reading, as a mount's read and the filer's HTTP read do, and then read from the cached chunks. It is still refused when the remote store cannot be read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Denis Maier <denis@dAir.lab> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
484 lines
19 KiB
Go
484 lines
19 KiB
Go
package sftpd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pkg/sftp"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type lockTestFiler struct {
|
|
filer_pb.UnimplementedSeaweedFilerServer
|
|
mu sync.Mutex
|
|
entries map[string]*filer_pb.Entry
|
|
deletes []*filer_pb.DeleteEntryRequest
|
|
renames []*filer_pb.AtomicRenameEntryRequest
|
|
updates int
|
|
volume string
|
|
// remote holds what CacheRemoteObjectToLocalCluster stores for a path
|
|
remote map[string]*filer_pb.Entry
|
|
|
|
lastUpdate *filer_pb.UpdateEntryRequest
|
|
updateErr error
|
|
}
|
|
|
|
func (f *lockTestFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if entry, ok := f.entries[req.Directory+"/"+req.Name]; ok {
|
|
return &filer_pb.LookupDirectoryEntryResponse{Entry: entry}, nil
|
|
}
|
|
return nil, status.Error(codes.NotFound, "no entry is found")
|
|
}
|
|
|
|
func (f *lockTestFiler) DeleteEntry(_ context.Context, req *filer_pb.DeleteEntryRequest) (*filer_pb.DeleteEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.deletes = append(f.deletes, req)
|
|
return &filer_pb.DeleteEntryResponse{}, nil
|
|
}
|
|
|
|
func (f *lockTestFiler) deleteRequests() []*filer_pb.DeleteEntryRequest {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]*filer_pb.DeleteEntryRequest(nil), f.deletes...)
|
|
}
|
|
|
|
const lockTestFilerConf = `{"locations":[{"locationPrefix":"/buckets/worm/","worm":true},{"locationPrefix":"/buckets/worm-1h/","worm":true,"wormRetentionTimeSeconds":3600}]}`
|
|
|
|
func newLockTestServer(t *testing.T, entries map[string]*filer_pb.Entry) (*SftpServer, *lockTestFiler) {
|
|
t.Helper()
|
|
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}
|
|
}
|
|
}
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
fake := &lockTestFiler{entries: entries}
|
|
grpcServer := grpc.NewServer()
|
|
filer_pb.RegisterSeaweedFilerServer(grpcServer, fake)
|
|
go func() { _ = grpcServer.Serve(lis) }()
|
|
t.Cleanup(grpcServer.Stop)
|
|
|
|
_, port, _ := net.SplitHostPort(lis.Addr().String())
|
|
return &SftpServer{
|
|
filerAddr: pb.ServerAddress("127.0.0.1:0." + port),
|
|
grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
user: &user.User{
|
|
Username: "alice",
|
|
Uid: 1000,
|
|
Gid: 1000,
|
|
HomeDir: "/buckets",
|
|
Permissions: map[string][]string{"/buckets": {PermAll}},
|
|
},
|
|
}, fake
|
|
}
|
|
|
|
func lockedEntry(name string, extended map[string]string) *filer_pb.Entry {
|
|
entry := &filer_pb.Entry{Name: name, Extended: map[string][]byte{}}
|
|
for k, v := range extended {
|
|
entry.Extended[k] = []byte(v)
|
|
}
|
|
return entry
|
|
}
|
|
|
|
func retainUntil(d time.Duration) string {
|
|
return strconv.FormatInt(time.Now().Add(d).Unix(), 10)
|
|
}
|
|
|
|
func TestRemoveRefusesProtectedEntries(t *testing.T) {
|
|
wormAt := time.Now().Add(-2 * time.Hour).UnixNano()
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
entry *filer_pb.Entry
|
|
refused bool
|
|
}{
|
|
{"legal hold", "/buckets/b/f.versions/v_1", lockedEntry("v_1", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}), true},
|
|
{"compliance retention", "/buckets/b/f.versions/v_2", lockedEntry("v_2", map[string]string{s3_constants.ExtObjectLockModeKey: s3_constants.RetentionModeCompliance, s3_constants.ExtRetentionUntilDateKey: retainUntil(time.Hour)}), true},
|
|
{"governance retention", "/buckets/b/f.versions/v_3", lockedEntry("v_3", map[string]string{s3_constants.ExtObjectLockModeKey: s3_constants.RetentionModeGovernance, s3_constants.ExtRetentionUntilDateKey: retainUntil(time.Hour)}), true},
|
|
{"expired retention", "/buckets/b/f.versions/v_4", lockedEntry("v_4", map[string]string{s3_constants.ExtObjectLockModeKey: s3_constants.RetentionModeCompliance, s3_constants.ExtRetentionUntilDateKey: retainUntil(-time.Hour)}), false},
|
|
{"legal hold off", "/buckets/b/f.versions/v_5", lockedEntry("v_5", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOff}), false},
|
|
{"delete marker", "/buckets/b/f.versions/v_6", lockedEntry("v_6", map[string]string{s3_constants.ExtDeleteMarkerKey: "true", s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}), false},
|
|
{"worm", "/buckets/worm/f", &filer_pb.Entry{Name: "f", WormEnforcedAtTsNs: wormAt}, true},
|
|
{"worm retention elapsed", "/buckets/worm-1h/f", &filer_pb.Entry{Name: "f", WormEnforcedAtTsNs: wormAt}, false},
|
|
{"worm not enforced yet", "/buckets/worm/g", &filer_pb.Entry{Name: "g"}, false},
|
|
{"no worm rule", "/buckets/b/f", &filer_pb.Entry{Name: "f", WormEnforcedAtTsNs: wormAt}, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{tt.path: tt.entry})
|
|
err := fs.removeEntry(tt.path)
|
|
deletes := fake.deleteRequests()
|
|
if tt.refused {
|
|
if !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("rm: got %v, want os.ErrPermission", err)
|
|
}
|
|
if len(deletes) != 0 {
|
|
t.Fatalf("rm of a protected entry reached the filer")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("rm: %v", err)
|
|
}
|
|
if len(deletes) != 1 {
|
|
t.Fatalf("got %d DeleteEntry calls, want 1", len(deletes))
|
|
}
|
|
for _, key := range []string{s3_constants.ExtLegalHoldKey, s3_constants.ExtObjectLockModeKey, s3_constants.ExtRetentionUntilDateKey} {
|
|
got, ok := deletes[0].ExpectedExtended[key]
|
|
if !ok || string(got) != string(tt.entry.Extended[key]) {
|
|
t.Fatalf("ExpectedExtended[%s] = %q (present %v), want %q", key, got, ok, tt.entry.Extended[key])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRmdirRefusesLockedDirectoryMarker(t *testing.T) {
|
|
marker := lockedEntry("dir", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn})
|
|
marker.IsDirectory = true
|
|
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{"/buckets/b/dir": marker})
|
|
if err := fs.removeDir("/buckets/b/dir"); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("rmdir: got %v, want os.ErrPermission", err)
|
|
}
|
|
if len(fake.deleteRequests()) != 0 {
|
|
t.Fatalf("rmdir of a locked directory marker reached the filer")
|
|
}
|
|
}
|
|
|
|
func (f *lockTestFiler) AtomicRenameEntry(_ context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.renames = append(f.renames, req)
|
|
return &filer_pb.AtomicRenameEntryResponse{}, nil
|
|
}
|
|
|
|
func (f *lockTestFiler) renameRequests() []*filer_pb.AtomicRenameEntryRequest {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]*filer_pb.AtomicRenameEntryRequest(nil), f.renames...)
|
|
}
|
|
|
|
func TestRenameRefusesProtectedSourceAndTarget(t *testing.T) {
|
|
held := func(name string) *filer_pb.Entry {
|
|
return lockedEntry(name, map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn})
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
entries map[string]*filer_pb.Entry
|
|
from string
|
|
to string
|
|
refused bool
|
|
}{
|
|
{"locked source", map[string]*filer_pb.Entry{"/buckets/b/f.versions/v_1": held("v_1")}, "/buckets/b/f.versions/v_1", "/buckets/b/moved", true},
|
|
{"locked target", map[string]*filer_pb.Entry{"/buckets/b/x": {Name: "x"}, "/buckets/b/f.versions/v_1": held("v_1")}, "/buckets/b/x", "/buckets/b/f.versions/v_1", true},
|
|
{"worm source", map[string]*filer_pb.Entry{"/buckets/worm/f": {Name: "f", WormEnforcedAtTsNs: time.Now().UnixNano()}}, "/buckets/worm/f", "/buckets/b/f", true},
|
|
{"unprotected", map[string]*filer_pb.Entry{"/buckets/b/x": {Name: "x"}, "/buckets/b/y": {Name: "y"}}, "/buckets/b/x", "/buckets/b/y", false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tt.entries["/buckets/b"] = &filer_pb.Entry{Name: "b", IsDirectory: true}
|
|
tt.entries["/buckets/b/f.versions"] = &filer_pb.Entry{Name: "f.versions", IsDirectory: true}
|
|
fs, fake := newLockTestServer(t, tt.entries)
|
|
err := fs.renameEntry(tt.from, tt.to)
|
|
renames := fake.renameRequests()
|
|
if tt.refused {
|
|
if !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("rename: got %v, want os.ErrPermission", err)
|
|
}
|
|
if len(renames) != 0 {
|
|
t.Fatalf("rename of a protected entry reached the filer")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("rename: %v", err)
|
|
}
|
|
if len(renames) != 1 {
|
|
t.Fatalf("got %d AtomicRenameEntry calls, want 1", len(renames))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (f *lockTestFiler) UpdateEntry(_ context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.updateErr != nil {
|
|
return nil, f.updateErr
|
|
}
|
|
f.updates++
|
|
f.lastUpdate = req
|
|
return &filer_pb.UpdateEntryResponse{}, nil
|
|
}
|
|
|
|
func TestWriteAndSetstatRefuseProtectedEntries(t *testing.T) {
|
|
entries := map[string]*filer_pb.Entry{
|
|
"/buckets/b": {Name: "b", IsDirectory: true},
|
|
"/buckets/b/held": lockedEntry("held", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}),
|
|
"/buckets/worm": {Name: "worm", IsDirectory: true},
|
|
"/buckets/worm/f": {Name: "f", WormEnforcedAtTsNs: time.Now().UnixNano()},
|
|
"/buckets/b/unlocked": {Name: "unlocked"},
|
|
}
|
|
fs, fake := newLockTestServer(t, entries)
|
|
for _, p := range []string{"/b/held", "/worm/f"} {
|
|
req := sftp.NewRequest("Put", p)
|
|
if w, err := fs.newFileWriter(req); !errors.Is(err, os.ErrPermission) {
|
|
if closer, ok := w.(io.Closer); ok {
|
|
closer.Close()
|
|
}
|
|
t.Fatalf("overwrite of %s: got %v, want os.ErrPermission", p, err)
|
|
}
|
|
if err := fs.setFileStat("/buckets"+p, sftp.FileAttrFlags{}, &sftp.FileStat{}); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("setstat of %s: got %v, want os.ErrPermission", p, err)
|
|
}
|
|
}
|
|
if fake.updates != 0 {
|
|
t.Fatalf("setstat of a protected entry reached the filer")
|
|
}
|
|
if err := fs.setFileStat("/buckets/b/unlocked", sftp.FileAttrFlags{}, &sftp.FileStat{}); err != nil {
|
|
t.Fatalf("setstat of an unprotected entry: %v", err)
|
|
}
|
|
if _, ok := fake.lastUpdate.ExpectedExtended[s3_constants.ExtLegalHoldKey]; !ok {
|
|
t.Fatalf("setstat is not pinned to the Object Lock state it checked")
|
|
}
|
|
}
|
|
|
|
func TestRenameOntoItselfKeepsProtectedEntry(t *testing.T) {
|
|
p := "/buckets/b/f.versions/v_1"
|
|
fs, _ := newLockTestServer(t, map[string]*filer_pb.Entry{
|
|
"/buckets/b/f.versions": {Name: "f.versions", IsDirectory: true},
|
|
p: lockedEntry("v_1", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}),
|
|
})
|
|
if err := fs.renameEntry(p, p); err != nil {
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestUploadRechecksTargetAtClose(t *testing.T) {
|
|
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
|
"/buckets/b": {Name: "b", IsDirectory: true},
|
|
"/buckets/b/dir": {Name: "dir", IsDirectory: true},
|
|
"/buckets/b/f": {Name: "f"},
|
|
})
|
|
if w, err := fs.newFileWriter(sftp.NewRequest("Put", "/b/dir")); err == nil {
|
|
w.(io.Closer).Close()
|
|
t.Fatalf("upload onto a directory was accepted")
|
|
}
|
|
|
|
w, err := fs.newFileWriter(uploadRequest("/b/f"))
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
if _, err := w.WriteAt([]byte("x"), 0); err != nil {
|
|
t.Fatalf("write: %v", err)
|
|
}
|
|
fake.mu.Lock()
|
|
fake.entries["/buckets/b/f"] = lockedEntry("f", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn})
|
|
fake.entries["/buckets/b/f"].Attributes = &filer_pb.FuseAttributes{FileMode: 0644, Uid: 1000}
|
|
fake.mu.Unlock()
|
|
if err := w.(io.Closer).Close(); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("close after a hold was set: got %v, want os.ErrPermission", err)
|
|
}
|
|
}
|
|
|
|
func (f *lockTestFiler) ListEntries(req *filer_pb.ListEntriesRequest, stream grpc.ServerStreamingServer[filer_pb.ListEntriesResponse]) error {
|
|
f.mu.Lock()
|
|
var children []*filer_pb.Entry
|
|
for p, entry := range f.entries {
|
|
if dir, name := path.Split(p); strings.TrimSuffix(dir, "/") == req.Directory && name > req.StartFromFileName {
|
|
children = append(children, entry)
|
|
}
|
|
}
|
|
f.mu.Unlock()
|
|
sort.Slice(children, func(i, j int) bool { return children[i].Name < children[j].Name })
|
|
for _, entry := range children {
|
|
if err := stream.Send(&filer_pb.ListEntriesResponse{Entry: entry}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestRenameRefusesDirectoryHoldingProtectedEntries(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dir string
|
|
child *filer_pb.Entry
|
|
refused bool
|
|
}{
|
|
{"locked version below", "/buckets/b/d", lockedEntry("v_1", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}), true},
|
|
{"under a WORM rule", "/buckets/worm/d", &filer_pb.Entry{Name: "v_1"}, true},
|
|
{"unprotected", "/buckets/b/d", lockedEntry("v_1", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOff}), false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
parent := path.Dir(tt.dir)
|
|
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
|
parent: {Name: path.Base(parent), IsDirectory: true},
|
|
tt.dir: {Name: "d", IsDirectory: true},
|
|
tt.dir + "/f.versions": {Name: "f.versions", IsDirectory: true},
|
|
tt.dir + "/f.versions/v_1": tt.child,
|
|
"/buckets/b": {Name: "b", IsDirectory: true},
|
|
})
|
|
err := fs.renameEntry(tt.dir, "/buckets/b/moved")
|
|
if tt.refused {
|
|
if !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("rename: got %v, want os.ErrPermission", err)
|
|
}
|
|
if len(fake.renameRequests()) != 0 {
|
|
t.Fatalf("rename of a protected subtree reached the filer")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("rename: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPutFileAppliesBucketDefaultRetention(t *testing.T) {
|
|
bucket := func(name string, extended map[string]string) *filer_pb.Entry {
|
|
entry := lockedEntry(name, extended)
|
|
entry.IsDirectory = true
|
|
return entry
|
|
}
|
|
fs, _ := newLockTestServer(t, map[string]*filer_pb.Entry{
|
|
"/buckets/days": bucket("days", map[string]string{s3_constants.ExtObjectLockEnabledKey: s3_constants.ObjectLockEnabled, s3_constants.ExtObjectLockDefaultModeKey: s3_constants.RetentionModeGovernance, s3_constants.ExtObjectLockDefaultDaysKey: "2"}),
|
|
"/buckets/years": bucket("years", map[string]string{s3_constants.ExtObjectLockEnabledKey: s3_constants.ObjectLockEnabled, s3_constants.ExtObjectLockDefaultModeKey: s3_constants.RetentionModeCompliance, s3_constants.ExtObjectLockDefaultYearsKey: "1"}),
|
|
"/buckets/no-default": bucket("no-default", map[string]string{s3_constants.ExtObjectLockEnabledKey: s3_constants.ObjectLockEnabled}),
|
|
"/buckets/plain": bucket("plain", nil),
|
|
})
|
|
var gotHeader http.Header
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotHeader = r.Header.Clone()
|
|
_, _ = w.Write([]byte(`{}`))
|
|
}))
|
|
t.Cleanup(ts.Close)
|
|
_, grpcPort, _ := net.SplitHostPort(fs.filerAddr.ToGrpcAddress())
|
|
fs.filerAddr = pb.ServerAddress(strings.TrimPrefix(ts.URL, "http://") + "." + grpcPort)
|
|
|
|
now := time.Now()
|
|
tests := []struct {
|
|
path string
|
|
mode string
|
|
retainUntil time.Time
|
|
}{
|
|
{"/buckets/days/a/b.txt", s3_constants.RetentionModeGovernance, now.AddDate(0, 0, 2)},
|
|
{"/buckets/years/f", s3_constants.RetentionModeCompliance, now.AddDate(1, 0, 0)},
|
|
{"/buckets/no-default/f", "", time.Time{}},
|
|
{"/buckets/plain/f", "", time.Time{}},
|
|
{"/buckets/missing/f", "", time.Time{}},
|
|
{"/buckets/days", "", time.Time{}},
|
|
{"/home/days/f", "", time.Time{}},
|
|
}
|
|
for _, bucketsDir := range []string{"/buckets", "/buckets/", "//buckets"} {
|
|
fs.bucketsDir = bucketsDir
|
|
for _, tt := range tests {
|
|
gotHeader = nil
|
|
if err := fs.putFile(tt.path, strings.NewReader("data"), nil, 0644, nil); err != nil {
|
|
t.Fatalf("putFile %s: %v", tt.path, err)
|
|
}
|
|
mode := gotHeader[s3_constants.ExtObjectLockModeKey]
|
|
until := gotHeader[s3_constants.ExtRetentionUntilDateKey]
|
|
if tt.mode == "" {
|
|
if mode != nil || until != nil {
|
|
t.Errorf("%s %s: got retention %v until %v, want none", fs.bucketsDir, tt.path, mode, until)
|
|
}
|
|
continue
|
|
}
|
|
if len(mode) != 1 || mode[0] != tt.mode {
|
|
t.Errorf("%s %s: mode %v, want %s", fs.bucketsDir, tt.path, mode, tt.mode)
|
|
}
|
|
if len(until) != 1 {
|
|
t.Errorf("%s: retain-until %v", tt.path, until)
|
|
continue
|
|
}
|
|
sec, err := strconv.ParseInt(until[0], 10, 64)
|
|
if err != nil || sec < tt.retainUntil.Unix()-5 || sec > tt.retainUntil.Unix()+5 {
|
|
t.Errorf("%s: retain-until %s, want about %d", tt.path, until[0], tt.retainUntil.Unix())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// chmod sends only permission bits: the stored type must stay, or the filer
|
|
// refuses to turn a folder into a file.
|
|
func TestChmodKeepsEntryType(t *testing.T) {
|
|
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
|
"/buckets/d": {Name: "d", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{FileMode: uint32(os.ModeDir | 0o700)}},
|
|
"/buckets/f": {Name: "f", Attributes: &filer_pb.FuseAttributes{FileMode: 0o644}},
|
|
})
|
|
for _, c := range []struct {
|
|
path string
|
|
sent uint32
|
|
want os.FileMode
|
|
}{
|
|
{"/buckets/d", 0o1750, os.ModeDir | os.ModeSticky | 0o750},
|
|
{"/buckets/f", 0o600, 0o600},
|
|
} {
|
|
if err := fs.setFileStat(c.path, sftp.FileAttrFlags{Permissions: true}, &sftp.FileStat{Mode: c.sent}); err != nil {
|
|
t.Fatalf("chmod %s: %v", c.path, err)
|
|
}
|
|
if got := os.FileMode(fake.lastUpdate.GetEntry().GetAttributes().GetFileMode()); got != c.want {
|
|
t.Errorf("chmod %s stored mode %v, want %v", c.path, got, c.want)
|
|
}
|
|
}
|
|
}
|