mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* Add per-call filer authentication sources and pooled client wrappers * Authenticate gateway filer calls with per-call admin tokens * Authenticate shell admin and synchronization filer calls * Authenticate workers MQ and remaining filer client paths * Cache filer client signing keys and sign subscriptions with the shared key Re-reading security.toml on every filer RPC cost about 125us against 8us for signing. Keep the keys for up to a second, re-reading at once when a WEED_ override or the configuration directory changes. When the file exists but cannot be read, keep the last keys read, else the startup keys, as FilerAdminKeys does, so a process without a key stays tokenless. Metadata subscriptions prefer the shared key, as filer peers do, because older filers verify subscription admin tokens only with it. * Authenticate admin-script, maintenance and filer.conf shell paths Plugin worker admin scripts and master maintenance scripts build their own shell options and carried no filer token source; fs.configure and s3.bucket.quota.check read filer.conf without it. * filer.cat: report a failed read; isolate key overrides in the admin-script test filer.cat ignored the error from its filer call and exited successfully, leaving a truncated -output file. Report the error and exit with failure. The admin-script token test now clears the WEED_ signing-key overrides, which take precedence over security.toml. * security: refuse gRPC keys too short to sign; query engine uses client TLS Under GODEBUG=fips140=only, hmac.New panics on keys under 14 bytes. The startup check now covers jwt.filer_signing.grpc_key and grpc_previous_key, and filer client token sources check the keys they read, so a reloaded or per-endpoint security file with a short key is refused instead of panicking when the next RPC signs. The query engine's broker client dialed every master, filer and broker in plaintext, so the admin token it now sends could cross the network in the clear. It dials with the grpc.client TLS settings instead, and weed-sql loads security.toml as weed-db does. * security: per-endpoint admin tokens ignore WEED_ key overrides FilerAdminTokenFromFile honored WEED_JWT_FILER_SIGNING_GRPC_KEY and WEED_JWT_FILER_SIGNING_KEY. Those are process-wide, so filer.sync with -a.security and -b.security signed both clusters' admin tokens with the one key, handing each cluster a token valid on the other. A per-endpoint file now supplies its own keys only. * weed-sql, weed-db: refuse to start on an unreadable security.toml Both only warned when security.toml was present but could not be read or parsed. They then dialed without the grpc.client TLS settings while the filer token source could still sign admin tokens from WEED_ environment keys, so the tokens could travel in plaintext. They now exit with an error that does not quote the file; a missing file is still fine. * pb: take the filer token source in WithGrpcFilerClient and WithOneOfGrpcFilerClients Fold WithGrpcFilerClientAuth and WithOneOfGrpcFilerClientsAuth into the existing helpers instead of keeping two names for each. Callers without a token source pass nil. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: return the read error from ReadConfiguration Fold ReadConfigurationWithError into ReadConfiguration. The error is returned and not logged, since parser errors can quote values; the existing reload callers already warn when no file was read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: keep the test-only filerAdminToken helper in the test It had no production caller. The test helper now resolves keys through filerClientKeysFrom, as the token sources do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: read security.toml once in LoadSecurityConfigurationOrError It validated the file, then merged it with a second read whose failure was ignored and whose parser error was logged. It now merges with one read and logs nothing from the parser. Viper's search also treats a directory the process cannot search as holding no file; a candidate that cannot be checked for lack of permission is now refused too. * security: verify admin subscription tokens with the gRPC clock leeway CheckGrpcMetadataSubscription checked admin tokens strictly, while unary filer calls allow jwt.filer_signing.clock_leeway_seconds. With ten-second client admin tokens, a subscriber whose clock trailed the filer's by more than ten seconds was refused with a matching key. It now takes the leeway, and the filer passes its configured one. The leeway check moves from the server package to security.IsFilerAdminTokenWithLeeway so both use it. * util: start without security.toml only when every candidate is confirmed absent A candidate whose check fails for any reason other than not existing, such as a symlink loop, is refused like one the process cannot search. --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
5.1 KiB
Go
128 lines
5.1 KiB
Go
package command
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// The offset key is a persistence contract: a changed form orphans saved
|
|
// checkpoints, so the historical forms must survive verbatim and the
|
|
// target-scoped form must stay stable.
|
|
func TestGetSignaturePrefixByPath_KeyForms(t *testing.T) {
|
|
cases := []struct {
|
|
sourcePath, targetPath, want string
|
|
}{
|
|
// historical forms, kept so existing deployments resume unchanged
|
|
{"/", "/", "sync."},
|
|
{"/data", "/", "sync./data"},
|
|
// the target path participates once it deviates from "/"
|
|
{"/", "/backup-a", "sync.\x00/backup-a"},
|
|
{"/data", "/backup-a", "sync./data\x00/backup-a"},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := getSignaturePrefixByPath(tc.sourcePath, tc.targetPath); got != tc.want {
|
|
t.Errorf("getSignaturePrefixByPath(%q, %q) = %q, want %q", tc.sourcePath, tc.targetPath, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The NUL separator keeps the key injective: a source directory whose name
|
|
// spells out a separator must not alias a different (source, target) pair.
|
|
func TestGetSignaturePrefixByPath_NoAliasing(t *testing.T) {
|
|
if a, b := getSignaturePrefixByPath("/archive=>/backup", "/"), getSignaturePrefixByPath("/archive", "/backup"); a == b {
|
|
t.Errorf("source path spelling a separator aliases key %q", a)
|
|
}
|
|
}
|
|
|
|
// Two syncs from the same source filer and path to different directories on
|
|
// the same target filer must keep separate checkpoints, or the running one
|
|
// pushes the shared offset past events the other never processed.
|
|
func TestGetSignaturePrefixByPath_DistinctTargets(t *testing.T) {
|
|
a := getSignaturePrefixByPath("/", "/backup-a")
|
|
b := getSignaturePrefixByPath("/", "/backup-b")
|
|
if a == b {
|
|
t.Errorf("different target paths share offset key %q", a)
|
|
}
|
|
if root := getSignaturePrefixByPath("/", "/"); a == root {
|
|
t.Errorf("non-root target path shares offset key %q with the root form", a)
|
|
}
|
|
}
|
|
|
|
// Over the real KV wire path: a sync that predates target-scoped keys resumes
|
|
// from the historical key, and once each sync checkpoints under its own key
|
|
// they no longer disturb each other.
|
|
func TestSyncOffset_HistoricalFallbackThenIndependence(t *testing.T) {
|
|
filerAddr, dial := startKvFiler(t)
|
|
const sourceSig = int32(12345)
|
|
historical := getSignaturePrefixByPath("/data", "/")
|
|
prefixA := getSignaturePrefixByPath("/data", "/backup-a")
|
|
prefixB := getSignaturePrefixByPath("/data", "/backup-b")
|
|
|
|
// pre-upgrade state: both syncs shared the historical key
|
|
if err := setOffset(dial, filerAddr, nil, historical, sourceSig, 111); err != nil {
|
|
t.Fatalf("seed historical offset: %v", err)
|
|
}
|
|
if got, err := getOffsetWithFallback(dial, filerAddr, nil, prefixA, sourceSig, historical, sourceSig); err != nil || got != 111 {
|
|
t.Fatalf("A fallback read = (%d, %v), want (111, nil)", got, err)
|
|
}
|
|
|
|
// A checkpoints under its own key; B still resumes from the historical one
|
|
if err := setOffset(dial, filerAddr, nil, prefixA, sourceSig, 222); err != nil {
|
|
t.Fatalf("write A offset: %v", err)
|
|
}
|
|
if got, err := getOffsetWithFallback(dial, filerAddr, nil, prefixA, sourceSig, historical, sourceSig); err != nil || got != 222 {
|
|
t.Fatalf("A read = (%d, %v), want (222, nil)", got, err)
|
|
}
|
|
if got, err := getOffsetWithFallback(dial, filerAddr, nil, prefixB, sourceSig, historical, sourceSig); err != nil || got != 111 {
|
|
t.Fatalf("B read = (%d, %v), want (111, nil)", got, err)
|
|
}
|
|
}
|
|
|
|
// TestSyncOffset_StaleWriteDropped: a checkpoint write issued before a write
|
|
// that already landed under the same key is stale and must not apply, while
|
|
// a deliberately regressed value issued later — a resume floor or an
|
|
// operator seed — still does.
|
|
func TestSyncOffset_StaleWriteDropped(t *testing.T) {
|
|
filerAddr, dial := startKvFiler(t)
|
|
prefix := getSignaturePrefixByPath("/data", "/backup-stale")
|
|
sourceSig := int32(77)
|
|
|
|
if err := setOffset(dial, filerAddr, nil, prefix, sourceSig, 300); err != nil {
|
|
t.Fatalf("seed offset: %v", err)
|
|
}
|
|
|
|
// Pin the checkpoint past the next write's issuance seq, simulating a
|
|
// write that was issued before a newer one already landed.
|
|
syncKey := []byte(prefix + "____")
|
|
util.Uint32toBytes(syncKey[len(prefix):len(prefix)+4], uint32(sourceSig))
|
|
mapKey := string(filerAddr) + "\x00" + string(syncKey)
|
|
offsetCheckpointWrites.Lock()
|
|
checkpoint := offsetCheckpointWrites.byKey[mapKey]
|
|
offsetCheckpointWrites.Unlock()
|
|
if checkpoint == nil {
|
|
t.Fatal("no checkpoint state for the seeded key")
|
|
}
|
|
checkpoint.Lock()
|
|
checkpoint.lastSeq = offsetWriteSeq.Load() + 1000
|
|
checkpoint.Unlock()
|
|
|
|
if err := setOffset(dial, filerAddr, nil, prefix, sourceSig, 100); err != nil {
|
|
t.Fatalf("stale write: %v", err)
|
|
}
|
|
if got, err := getOffset(dial, filerAddr, nil, prefix, sourceSig); err != nil || got != 300 {
|
|
t.Fatalf("after stale write = (%d, %v), want (300, nil)", got, err)
|
|
}
|
|
|
|
// a later-issued regression is deliberate and must apply
|
|
checkpoint.Lock()
|
|
checkpoint.lastSeq = 0
|
|
checkpoint.Unlock()
|
|
if err := setOffset(dial, filerAddr, nil, prefix, sourceSig, 199); err != nil {
|
|
t.Fatalf("floored write: %v", err)
|
|
}
|
|
if got, err := getOffset(dial, filerAddr, nil, prefix, sourceSig); err != nil || got != 199 {
|
|
t.Fatalf("after floored write = (%d, %v), want (199, nil)", got, err)
|
|
}
|
|
}
|