mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-30 17:00:48 +01:00
* remote: reuse one guarded remote storage client per configuration BuildGuardedRemoteStorageClient (the filer's lazy remote stat and list, remote deletes and renames, the volume server's cold reads, the S3 gateway's remote reads) built a new client, with its own http.Transport, on every call. Each transport kept its connection idle for IdleConnTimeout (60s) and was never reused or closed, so a steady stream of remote lookups held one open connection per request. An endpoint that caps connections per source then refuses every new connection. - remote_storage: GetRemoteStorage's per-name cache becomes a bounded LRU (WEED_REMOTE_CLIENT_CACHE_MAX, default 64) keyed by kind, name, a hash of the whole configuration and a credentials version, built outside the lock once per key (singleflight) from a copy of the configuration. A client leaving the cache has its idle connections closed. GetRemoteStorageWith exposes the cache to other builders. - BuildGuardedRemoteStorageClient caches the guarded client per configuration. The endpoint is checked against the deny-list when a client is built; every new connection still dials through the guarded dialer. A gcs credentials file, and for an S3-compatible remote using the default credential chain the shared credentials file and AWS_* variables, are part of the key by content. - The shared transport keeps 32 idle connections per host, caps connections per host (WEED_REMOTE_MAX_CONNS_PER_HOST, default 64) and has dial, TLS handshake and response header deadlines, so a hung remote frees its slots. - The filer drops the clients of removed remotes (and all but the newest client per name) on every /etc/remote reload, also when /etc/remote does not exist; reloads are serialized. * remote: drop clients whose build spans a reload or whose remote is gone * server: hash the shared AWS config file into the guarded credential version * server: bound a stalled body on the shared remote transport * remote: waive path-injection on the configured credentials file * remote: keep caching remotes the local filer does not name * remote: drop the inline codeql waiver the analyzer ignores * remote: version the plain gcs client's credentials file * remote: hash the environment's inline gcs credentials GOOGLE_APPLICATION_CREDENTIALS can hold the JSON itself, which the gcs client accepts; it is not in the configuration hash, so a changed value kept the old client. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: version the well-known gcs credentials file With neither the configuration nor GOOGLE_APPLICATION_CREDENTIALS set, the SDK reads application_default_credentials.json; rotating it left the cache key unchanged and the old client behind. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: share the gcs credentials version with the guarded client The guarded gcs client resolves the same environment and well-known credentials as the plain one; keying it only on a configured path left those rotations invisible to the cache. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: version s3 keys resolved from the environment A remote whose access and secret keys both arrive through its type's environment fallback (aliyun, baidu, contabo, filebase, storj, tencent) never entered the default-chain branch of guardedCredentialsVersion, so an in-process environment change kept serving the stale cached client. The version now hashes the resolved keys for every S3-compatible remote. * volume: reuse one remote storage client per configuration (rust) fetch_and_write_needle built a fresh aws_sdk_s3::Client per request, so every cold read kept its connector's idle connections without ever reusing them — the same leak the Go side fixes. Cache clients keyed by the configuration's protobuf encoding in an LRU of WEED_REMOTE_CLIENT_CACHE_MAX entries (default 64), mirroring weed/remote_storage/client_cache.go. An evicted entry drops the client's last Arc and with it the connector's idle connections. The per-request endpoint validation is unchanged: Rust has no dial-time guard yet, so the up-front DNS check stays on every call. --------- Co-authored-by: user.email <chrislusf@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
180 lines
6.9 KiB
Go
180 lines
6.9 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestFilerRemoteStorage_FindRemoteStorageClient(t *testing.T) {
|
|
conf := &remote_pb.RemoteConf{
|
|
Name: "s7",
|
|
Type: "s3",
|
|
}
|
|
rs := NewFilerRemoteStorage()
|
|
rs.storageNameToConf[conf.Name] = conf
|
|
|
|
rs.mapDirectoryToRemoteStorage("/a/b/c", &remote_pb.RemoteStorageLocation{
|
|
Name: "s7",
|
|
Bucket: "some",
|
|
Path: "/dir",
|
|
})
|
|
|
|
_, _, found := rs.FindRemoteStorageClient("/a/b/c/d/e/f")
|
|
assert.Equal(t, true, found, "find storage client")
|
|
|
|
_, _, found2 := rs.FindRemoteStorageClient("/a/b")
|
|
assert.Equal(t, false, found2, "should not find storage client")
|
|
|
|
_, _, found3 := rs.FindRemoteStorageClient("/a/b/c")
|
|
assert.Equal(t, false, found3, "should not find storage client")
|
|
|
|
_, _, found4 := rs.FindRemoteStorageClient("/a/b/cc")
|
|
assert.Equal(t, false, found4, "should not find storage client")
|
|
}
|
|
|
|
func TestFilerRemoteStorage_FindMountDirectory_LongestPrefixWins(t *testing.T) {
|
|
conf := &remote_pb.RemoteConf{Name: "store", Type: "s3"}
|
|
rs := NewFilerRemoteStorage()
|
|
rs.storageNameToConf[conf.Name] = conf
|
|
|
|
rs.mapDirectoryToRemoteStorage("/buckets/mybucket", &remote_pb.RemoteStorageLocation{
|
|
Name: "store",
|
|
Bucket: "bucket-root",
|
|
Path: "/",
|
|
})
|
|
rs.mapDirectoryToRemoteStorage("/buckets/mybucket/prefix", &remote_pb.RemoteStorageLocation{
|
|
Name: "store",
|
|
Bucket: "bucket-prefix",
|
|
Path: "/",
|
|
})
|
|
|
|
tests := []struct {
|
|
path string
|
|
wantMount string
|
|
wantBucket string
|
|
}{
|
|
{"/buckets/mybucket/file.txt", "/buckets/mybucket", "bucket-root"},
|
|
{"/buckets/mybucket/prefix/file.txt", "/buckets/mybucket/prefix", "bucket-prefix"},
|
|
{"/buckets/mybucket/prefix/sub/file.txt", "/buckets/mybucket/prefix", "bucket-prefix"},
|
|
}
|
|
for _, tt := range tests {
|
|
mountDir, loc := rs.FindMountDirectory(util.FullPath(tt.path))
|
|
assert.Equal(t, util.FullPath(tt.wantMount), mountDir, "mount dir for %s", tt.path)
|
|
if assert.NotNil(t, loc, "location for %s", tt.path) {
|
|
assert.Equal(t, tt.wantBucket, loc.Bucket, "bucket for %s", tt.path)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLoadRemoteStorageConfigurationsAndMapping_RejectsBlockedEndpoint
|
|
// reproduces the unauthenticated-plant SSRF: a RemoteConf whose S3 endpoint is
|
|
// a loopback address is written under /etc/remote. With the SSRF deny-list
|
|
// validator injected (as the filer server does), the conf is dropped at load
|
|
// so the lazy-fetch path can never resolve a client for it; a conf whose
|
|
// endpoint passes validation is loaded as before.
|
|
func TestLoadRemoteStorageConfigurationsAndMapping_RejectsBlockedEndpoint(t *testing.T) {
|
|
loopbackConf := &remote_pb.RemoteConf{
|
|
Name: "evil",
|
|
Type: "s3",
|
|
S3Endpoint: "http://127.0.0.1:8000",
|
|
S3Region: "us-east-1",
|
|
}
|
|
okConf := &remote_pb.RemoteConf{
|
|
Name: "good",
|
|
Type: "s3",
|
|
S3Endpoint: "https://s3.example.com",
|
|
S3Region: "us-east-1",
|
|
}
|
|
loopbackBytes, _ := proto.Marshal(loopbackConf)
|
|
okBytes, _ := proto.Marshal(okConf)
|
|
|
|
store := newStubFilerStore()
|
|
store.entries[string(DirectoryEtcRemote)+"/evil.conf"] = &Entry{
|
|
FullPath: util.FullPath(string(DirectoryEtcRemote) + "/evil.conf"),
|
|
Attr: Attr{Mode: 0644},
|
|
Content: loopbackBytes,
|
|
}
|
|
store.entries[string(DirectoryEtcRemote)+"/good.conf"] = &Entry{
|
|
FullPath: util.FullPath(string(DirectoryEtcRemote) + "/good.conf"),
|
|
Attr: Attr{Mode: 0644},
|
|
Content: okBytes,
|
|
}
|
|
|
|
rs := NewFilerRemoteStorage()
|
|
// Validator mirrors the filer server injection: reject loopback endpoints.
|
|
rs.SetConfValidator(func(_ context.Context, conf *remote_pb.RemoteConf) error {
|
|
if conf.GetS3Endpoint() == "http://127.0.0.1:8000" {
|
|
return assertError("reject remote endpoint: loopback")
|
|
}
|
|
return nil
|
|
})
|
|
f := newTestFiler(t, store, rs)
|
|
|
|
require.NoError(t, f.RemoteStorage.LoadRemoteStorageConfigurationsAndMapping(f))
|
|
|
|
_, _, foundEvil := f.RemoteStorage.GetRemoteStorageClient("evil")
|
|
assert.False(t, foundEvil, "loopback-endpoint conf must be dropped at load")
|
|
_, _, foundGood := f.RemoteStorage.GetRemoteStorageClient("good")
|
|
assert.True(t, foundGood, "valid-endpoint conf must still be loaded")
|
|
}
|
|
|
|
// assertError is a tiny error type so the test validator can return a sentinel
|
|
// without importing fmt.
|
|
type assertError string
|
|
|
|
func (e assertError) Error() string { return string(e) }
|
|
|
|
// Every reload drops the cached clients of remotes that are no longer
|
|
// configured, also when /etc/remote is gone entirely, so a removed remote keeps
|
|
// no client and no open connections.
|
|
func TestLoadRemoteStorageConfigurationsAndMapping_DropsClientsOfRemovedRemotes(t *testing.T) {
|
|
remote_storage.ResetRemoteStorageClients()
|
|
t.Cleanup(remote_storage.ResetRemoteStorageClients)
|
|
closed := map[string]int{}
|
|
cache := func(name string) {
|
|
conf := &remote_pb.RemoteConf{Name: name, Type: "s3", S3Endpoint: "https://s3.example.com", S3Region: "us-east-1"}
|
|
_, err := remote_storage.GetRemoteStorageWith("guarded", conf, "", func(*remote_pb.RemoteConf) (remote_storage.RemoteStorageClient, func(), error) {
|
|
return &stubRemoteClient{}, func() { closed[name]++ }, nil
|
|
})
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// /etc/remote holds "good" only: the client of "gone" is dropped and closed, "good"'s kept
|
|
goodBytes, _ := proto.Marshal(&remote_pb.RemoteConf{Name: "good", Type: "s3", S3Endpoint: "https://s3.example.com", S3Region: "us-east-1"})
|
|
store := newStubFilerStore()
|
|
store.entries[string(DirectoryEtcRemote)+"/good.conf"] = &Entry{
|
|
FullPath: util.FullPath(string(DirectoryEtcRemote) + "/good.conf"), Attr: Attr{Mode: 0644}, Content: goodBytes,
|
|
}
|
|
cache("good")
|
|
cache("gone")
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
require.NoError(t, f.RemoteStorage.LoadRemoteStorageConfigurationsAndMapping(f))
|
|
assert.Zero(t, closed["good"], "a configured remote keeps its client")
|
|
assert.Positive(t, closed["gone"], "a removed remote's client is closed")
|
|
|
|
// no /etc/remote at all (the listing answers ErrNotFound): every remote is gone
|
|
empty := newTestFiler(t, newStubFilerStore(), NewFilerRemoteStorage())
|
|
empty.Store = NewFilerStoreWrapper(etcRemoteNotFoundStore{newStubFilerStore()})
|
|
require.NoError(t, empty.RemoteStorage.LoadRemoteStorageConfigurationsAndMapping(empty))
|
|
assert.Positive(t, closed["good"], "with no /etc/remote, no remote keeps a client")
|
|
}
|
|
|
|
// a store in which /etc/remote does not exist: its listing answers filer_pb.ErrNotFound
|
|
type etcRemoteNotFoundStore struct{ *stubFilerStore }
|
|
|
|
func (s etcRemoteNotFoundStore) ListDirectoryEntries(context.Context, util.FullPath, string, bool, int64, ListEachEntryFunc) (string, error) {
|
|
return "", filer_pb.ErrNotFound
|
|
}
|
|
|
|
func (s etcRemoteNotFoundStore) ListDirectoryPrefixedEntries(context.Context, util.FullPath, string, bool, int64, string, ListEachEntryFunc) (string, error) {
|
|
return "", filer_pb.ErrNotFound
|
|
}
|