mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* util: SHA-256 stands in for MD5 and SHA-1 under fips140=only GODEBUG=fips140=only panics on MD5 and SHA-1. NewMd5 and NewSha1 return those hashes as before, and under fips140=only SHA-256 cut to the same size, so digests keep their length. Md5, HashStringToLong and HashToInt32 use NewMd5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer stores: hash directory keys with NewMd5 and NewSha1 leveldb2, leveldb3, rocksdb and arangodb hash directory keys with MD5, tikv with SHA-1, which GODEBUG=fips140=only refuses, so the filer panicked at startup. They now use util.NewMd5 and util.NewSha1. Keys made under fips140=only differ from MD5 keys, so leveldb2 and leveldb3 mark an empty database opened under fips140=only and refuse a database whose keys are of the other kind instead of showing it empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * operation, volume: no Content-MD5 checks under fips140=only Under GODEBUG=fips140=only the uploader's digest is not an MD5, so it is no longer sent as Content-MD5, which volume servers outside that mode would reject; the digest still becomes the chunk ETag. A Go volume server in that mode keeps a client's Content-MD5 unchecked instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, filer: ETags and content digests with util.NewMd5 ETags, entry Md5 attributes and chunk digests now use util.Md5 and util.NewMd5, so under GODEBUG=fips140=only they are SHA-256 cut to 16 bytes instead of panicking. That digest cannot check a client's Content-MD5, so in that mode Content-MD5 is accepted unchecked, and the filer stops returning Content-MD5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, iam, master: SHA-1 names with util.NewSha1 Multipart upload ids, IAM policy ids, the OIDC provider, audit and session revocation file names, and the master's read-only copy dir hash names with SHA-1. They now use util.NewSha1, which GODEBUG=fips140=only turns into truncated SHA-256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * postgres, oidc, update: refuse MD5 and SHA-1 checks under fips140=only PostgreSQL md5 authentication, OIDC TLS thumbprints (SHA-1 by definition) and weed update's MD5 check of the download cannot run under GODEBUG=fips140=only. They now fail with an error saying so instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: no empty-body MD5 when Content-MD5 is not checked Under fips140=only a bodyless signed request still reached md5.Sum(nil) before the nil check and panicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: empty copy part ETag from the digest it stores Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: skip the native MD5 test under fips140=only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3sink: no Content-MD5 under fips140=only The entry's Md5 attribute is truncated SHA-256 there, which an S3 destination checking Content-MD5 would reject. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: move OIDC provider and revocation records to this mode's name The record names hash the ARN or JTI with util.NewSha1, which differs under GODEBUG=fips140=only. On the first use of a filer, each store renames records whose name does not match the one derived from their content, so lookups, revocation checks and deletes find records written in the other mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: test the rename with the names each fips140 mode derives Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: rename a record only if it is unchanged, under its new name's lock Two processes renaming after a mode switch could both list a record; if one renamed it and the provider was then deleted, the other wrote it back. The rename is now one filer transaction locked on the new name, like the stores' conditional writes, that requires the old entry to be unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
327 lines
12 KiB
Go
327 lines
12 KiB
Go
package mount
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mount_peer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// errPeerReadSkipped signals that the read didn't go through the peer
|
|
// path for a benign reason (local cache hit, no peer owner, etc.) rather
|
|
// than a genuine failure. Callers fall through to the volume path just
|
|
// as they would for a real error, but suppress the "peer read failed"
|
|
// log that would otherwise mislead operators into thinking peer sharing
|
|
// is broken.
|
|
var errPeerReadSkipped = fmt.Errorf("peer read skipped (not an error)")
|
|
|
|
// peerLookupTimeout bounds the ChunkLookup RPC. Short because the result
|
|
// is consumed on the read critical path.
|
|
const peerLookupTimeout = 500 * time.Millisecond
|
|
|
|
// peerFetchTimeout bounds a single FetchChunk stream; on expiry we fall
|
|
// through to the next holder or the volume server.
|
|
const peerFetchTimeout = 5 * time.Second
|
|
|
|
// maxPeerFetchChunkBytes caps how much we will accept from a single
|
|
// FetchChunk stream. gRPC per-message size is already capped by the
|
|
// server option; this is belt-and-suspenders against a runaway peer.
|
|
const maxPeerFetchChunkBytes = 64 * 1024 * 1024
|
|
|
|
// tryPeerRead attempts to satisfy a read from a peer mount's chunk cache.
|
|
// Returns (bytesRead, modifiedTsNs, nil) on success. On any failure it
|
|
// returns (0, 0, err) so the caller falls through to
|
|
// entryChunkGroup.ReadDataAt (the volume-server path).
|
|
//
|
|
// Flow:
|
|
// 1. Resolve the offset's leaf chunk (flattening manifests).
|
|
// 2. Ask the HRW owner mount for current holders via ChunkLookup.
|
|
// 3. For each holder (LRU order from PR #5), open a FetchChunk stream,
|
|
// assemble frames into a size-bounded buffer, and verify MD5
|
|
// end-to-end against FileChunk.ETag.
|
|
// 4. On success, populate chunk_cache and enqueue an announce so
|
|
// other mounts can discover us as a new holder.
|
|
//
|
|
// chunks is a snapshot captured under the LockedEntry lock by the caller.
|
|
func (fh *FileHandle) tryPeerRead(ctx context.Context, fileSize int64, buff []byte, offset int64, chunks []*filer_pb.FileChunk) (int64, int64, error) {
|
|
if fh.wfs.peerRegistrar == nil || fh.wfs.peerConnPool == nil {
|
|
return 0, 0, fmt.Errorf("peer sharing not configured")
|
|
}
|
|
|
|
// Resolve offset → leaf chunk, flattening any manifest indirection.
|
|
readStop := offset + int64(len(buff))
|
|
if readStop > fileSize {
|
|
readStop = fileSize
|
|
}
|
|
dataChunks, _, err := filer.ResolveChunkManifest(ctx, fh.wfs.LookupFn(), chunks, offset, readStop, fh.wfs.CacheInvalidator())
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("resolve manifest: %w", err)
|
|
}
|
|
targetChunk, chunkOffset := findChunkContaining(dataChunks, offset)
|
|
if targetChunk == nil {
|
|
return 0, 0, fmt.Errorf("no leaf chunk for offset %d", offset)
|
|
}
|
|
// Reject reads that cross a chunk boundary. We only fetch one chunk
|
|
// here, and the caller (readFromChunks) maps a short non-error
|
|
// return to "success, zero-fill the rest" — which would silently
|
|
// corrupt reads that should actually span two chunks. Bail so the
|
|
// fallback ReadDataAt path can handle the multi-chunk case
|
|
// correctly. See weedfs_file_read.go short-read semantics.
|
|
chunkEnd := targetChunk.Offset + int64(targetChunk.Size)
|
|
if readStop > chunkEnd {
|
|
return 0, 0, errPeerReadSkipped
|
|
}
|
|
|
|
// Fail fast when the chunk is already cached locally: the fallback
|
|
// ReadDataAt path will satisfy the read from chunkCache with no RPCs,
|
|
// so dialing a peer (ChunkLookup + FetchChunk) would be pure overhead.
|
|
if fh.wfs.chunkCache != nil && fh.wfs.chunkCache.IsInCache(targetChunk.FileId, true) {
|
|
return 0, 0, errPeerReadSkipped
|
|
}
|
|
|
|
selfAddr := ""
|
|
if fh.wfs.peerGrpcServer != nil {
|
|
selfAddr = fh.wfs.peerGrpcServer.SelfAddr()
|
|
}
|
|
|
|
owner := fh.wfs.peerRegistrar.OwnerFor(targetChunk.FileId)
|
|
if owner == "" || owner == selfAddr {
|
|
return 0, 0, fmt.Errorf("no peer owner for fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
holders, err := peerLookupHolders(ctx, fh.wfs.peerConnPool.Dialer(), owner, targetChunk.FileId)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("peer lookup: %w", err)
|
|
}
|
|
if len(holders) == 0 {
|
|
return 0, 0, fmt.Errorf("no peer holder for fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
// Re-rank holders by locality (same rack > same DC > elsewhere), keeping
|
|
// the server's LRU order stable within each bucket. The server caps the
|
|
// list at maxLookupHolders, so this is always a small N.
|
|
sortHoldersByLocality(holders, fh.wfs.option.PeerDataCenter, fh.wfs.option.PeerRack)
|
|
|
|
dial := fh.wfs.peerConnPool.Dialer()
|
|
for _, h := range holders {
|
|
if h.addr == selfAddr {
|
|
continue
|
|
}
|
|
data, ferr := fetchChunkFromPeer(ctx, dial, h.addr, targetChunk.FileId, targetChunk.Size, targetChunk.ETag)
|
|
if ferr != nil {
|
|
glog.V(2).Infof("peer-fetch %s from %s: %v", targetChunk.FileId, h.addr, ferr)
|
|
continue
|
|
}
|
|
if fh.wfs.chunkCache != nil {
|
|
fh.wfs.chunkCache.SetChunk(targetChunk.FileId, data)
|
|
}
|
|
if fh.wfs.peerAnnouncer != nil {
|
|
fh.wfs.peerAnnouncer.EnqueueAnnounce(targetChunk.FileId)
|
|
}
|
|
if chunkOffset >= int64(len(data)) {
|
|
return 0, 0, fmt.Errorf("peer returned short chunk")
|
|
}
|
|
// Cap the copy to whichever is smaller: the caller's buffer, or
|
|
// the remaining bytes before logical EOF (readStop already
|
|
// clamped to fileSize). FileChunk.Size can legitimately exceed
|
|
// the logical file length when the last chunk is partially
|
|
// written and the filer stored the full padded buffer, so a
|
|
// naïve copy(buff, data[chunkOffset:]) would return bytes past
|
|
// EOF. The caller treats a short non-error return as success
|
|
// and zero-fills the tail, so returning too many bytes here is
|
|
// a correctness bug, not just wasted I/O.
|
|
remaining := readStop - offset
|
|
available := int64(len(data)) - chunkOffset
|
|
maxCopy := int64(len(buff))
|
|
if remaining < maxCopy {
|
|
maxCopy = remaining
|
|
}
|
|
if available < maxCopy {
|
|
maxCopy = available
|
|
}
|
|
copied := copy(buff[:maxCopy], data[chunkOffset:chunkOffset+maxCopy])
|
|
return int64(copied), targetChunk.ModifiedTsNs, nil
|
|
}
|
|
return 0, 0, fmt.Errorf("no peer served fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
func findChunkContaining(chunks []*filer_pb.FileChunk, offset int64) (*filer_pb.FileChunk, int64) {
|
|
for _, c := range chunks {
|
|
start := c.Offset
|
|
stop := c.Offset + int64(c.Size)
|
|
if offset >= start && offset < stop {
|
|
return c, offset - start
|
|
}
|
|
}
|
|
return nil, 0
|
|
}
|
|
|
|
// peerHolder is a holder entry carried through the fetcher: addr for
|
|
// dialing plus the DC/Rack labels the owner recorded at announce time, so
|
|
// the fetcher can re-rank by its own locality before dialing.
|
|
type peerHolder struct {
|
|
addr string
|
|
dc string
|
|
rack string
|
|
}
|
|
|
|
// peerLookupHolders calls ChunkLookup on the given HRW owner and returns
|
|
// the holders in the server-reported (LRU) order along with their locality
|
|
// labels. The pooled dialer is passed in from the caller.
|
|
func peerLookupHolders(ctx context.Context, dial MountPeerDialer, ownerAddr, fid string) ([]peerHolder, error) {
|
|
client, closeFn, err := dial(ctx, ownerAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closeFn()
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, peerLookupTimeout)
|
|
defer cancel()
|
|
resp, err := client.ChunkLookup(callCtx, &mount_peer_pb.ChunkLookupRequest{FileIds: []string{fid}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
set, ok := resp.PeersByFid[fid]
|
|
if !ok || set == nil {
|
|
return nil, nil
|
|
}
|
|
out := make([]peerHolder, 0, len(set.Peers))
|
|
for _, p := range set.Peers {
|
|
out = append(out, peerHolder{addr: p.PeerAddr, dc: p.DataCenter, rack: p.Rack})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// localityBucket scores how "close" a peer is to self. Lower is better:
|
|
//
|
|
// 0 = same rack in same DC (shortest hop)
|
|
// 1 = same DC, different rack (cross-rack but still intra-DC)
|
|
// 2 = different DC, or unknown (anything else)
|
|
//
|
|
// Missing labels on either side fall to bucket 2 — we don't claim locality
|
|
// we can't prove.
|
|
func localityBucket(selfDC, selfRack, peerDC, peerRack string) int {
|
|
if selfDC == "" || peerDC == "" || selfDC != peerDC {
|
|
return 2
|
|
}
|
|
if selfRack != "" && peerRack != "" && selfRack == peerRack {
|
|
return 0
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// sortHoldersByLocality stable-sorts holders so the most local peers are
|
|
// tried first. The server-returned order is LRU (freshest holder first),
|
|
// and sort.SliceStable preserves that ordering within each locality
|
|
// bucket, so among equally-local peers we still prefer the freshest.
|
|
func sortHoldersByLocality(holders []peerHolder, selfDC, selfRack string) {
|
|
if len(holders) < 2 {
|
|
return
|
|
}
|
|
sort.SliceStable(holders, func(i, j int) bool {
|
|
return localityBucket(selfDC, selfRack, holders[i].dc, holders[i].rack) <
|
|
localityBucket(selfDC, selfRack, holders[j].dc, holders[j].rack)
|
|
})
|
|
}
|
|
|
|
// fetchChunkFromPeer server-streams a chunk from the given peer and
|
|
// verifies the MD5 of the assembled bytes against expectedETag.
|
|
//
|
|
// expectedSize (when > 0) is the authoritative chunk length from the
|
|
// filer entry; we pre-allocate exactly that to avoid slice growth and
|
|
// reject streams that overshoot it. A zero expectedSize falls back to
|
|
// maxPeerFetchChunkBytes as a safety ceiling.
|
|
func fetchChunkFromPeer(ctx context.Context, dial MountPeerDialer, peerAddr, fid string, expectedSize uint64, expectedETag string) ([]byte, error) {
|
|
client, closeFn, err := dial(ctx, peerAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closeFn()
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, peerFetchTimeout)
|
|
defer cancel()
|
|
|
|
stream, err := client.FetchChunk(callCtx, &mount_peer_pb.FetchChunkRequest{
|
|
FileId: fid,
|
|
ExpectedEtag: expectedETag,
|
|
ExpectedSize: expectedSize,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// capHint pre-sizes the assembly buffer from the filer-reported chunk
|
|
// size. When the caller didn't know the size (expectedSize == 0), we
|
|
// let append grow the buffer rather than reserve the 64 MiB ceiling —
|
|
// typical chunks are a few MiB, and the maxPeerFetchChunkBytes check
|
|
// during Recv is the real safety ceiling.
|
|
capHint := expectedSize
|
|
if capHint > maxPeerFetchChunkBytes {
|
|
capHint = maxPeerFetchChunkBytes
|
|
}
|
|
buf := make([]byte, 0, capHint)
|
|
|
|
for {
|
|
resp, rerr := stream.Recv()
|
|
if rerr == io.EOF {
|
|
break
|
|
}
|
|
if rerr != nil {
|
|
if status.Code(rerr) == codes.NotFound {
|
|
return nil, fmt.Errorf("peer not cached")
|
|
}
|
|
return nil, rerr
|
|
}
|
|
if len(buf)+len(resp.Data) > int(maxPeerFetchChunkBytes) {
|
|
return nil, fmt.Errorf("peer response exceeds max chunk size %d", maxPeerFetchChunkBytes)
|
|
}
|
|
buf = append(buf, resp.Data...)
|
|
}
|
|
|
|
if expectedSize > 0 && uint64(len(buf)) != expectedSize {
|
|
return nil, fmt.Errorf("peer returned %d bytes, expected %d", len(buf), expectedSize)
|
|
}
|
|
|
|
// Per-chunk integrity check — the peer is treated as untrusted.
|
|
// FileChunk.ETag is UploadResult.ContentMd5, which is base64 of the
|
|
// raw 16-byte MD5 (see weed/operation/upload_content.go:64 and
|
|
// filer's ETagChunks which decodes via util.Base64Md5ToBytes).
|
|
// Compare raw digests so we don't reject every valid peer response
|
|
// because of an encoding mismatch.
|
|
if expectedETag != "" {
|
|
got := util.Md5(buf)
|
|
if !etagMatchesMD5(expectedETag, got) {
|
|
return nil, fmt.Errorf("etag mismatch: peer=%s got=%x want=%s", peerAddr, got, expectedETag)
|
|
}
|
|
}
|
|
return buf, nil
|
|
}
|
|
|
|
// etagMatchesMD5 compares a stored FileChunk.ETag string to a raw 16-byte
|
|
// MD5 digest. FileChunk.ETag is produced as base64(md5) on upload, but
|
|
// some older chunks (and tests) use hex(md5). Accept either.
|
|
func etagMatchesMD5(etag string, rawMD5 []byte) bool {
|
|
if len(rawMD5) != md5.Size {
|
|
return false
|
|
}
|
|
if dec := util.Base64Md5ToBytes(etag); len(dec) == md5.Size {
|
|
return bytes.Equal(dec, rawMD5)
|
|
}
|
|
if dec, err := hex.DecodeString(etag); err == nil && len(dec) == md5.Size {
|
|
return bytes.Equal(dec, rawMD5)
|
|
}
|
|
return false
|
|
}
|