Files
seaweedfs/weed/replication/digest.go
T
9ffb5fe185 filer, s3, filer.sync: one digest per cluster, set by the filer's -digest and followed by its clients (#12913)
* util, filer: choose the digest at filer startup with -digest

NewMd5 and NewSha1 returned SHA-256 cut to size whenever the process ran
under GODEBUG=fips140=only, so the kind of digest a cluster stores followed
each process's own environment. They now follow a digest mode set once at
startup: util.SetDigest("md5"|"sha256"), or "" to keep following
fips140.Enforced(). The default is unchanged.

weed filer -digest, weed server -filer.digest and weed mini -filer.digest
set it; fips140=only refuses md5 at startup. The leveldb2/leveldb3
directory-key marker follows the mode, as do the places that compare a
NewMd5 digest with Content-MD5 or pass it on as one: S3 PutObject, filer
HTTP uploads, the Content-MD5 the filer returns, and the uploader's
Content-MD5 to volume servers. Places that verify Content-MD5 with a real
MD5 (volume servers, S3 request bodies and archive uploads) keep keying on
fips140.Enforced(), since they can check it whenever MD5 is available.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, command: report the filer's digest and make its clients use it

GetFilerConfigurationResponse carries the filer's digest mode in a new
field 16, digest. weed s3, mount, webdav, sftp, iam and filer.copy adopt
it before they serve or write, so a gateway started with a different
GODEBUG writes the same kind of ETag and chunk digest as the rest of the
cluster. A process under fips140=only refuses a filer that reports md5. A
filer that reports no digest predates the field; the client then keeps
its own mode and logs a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.sync, s3 sink: keep digests within one kind

filer.sync copies entry digests and ETags as they are, so syncing a filer
with digest sha256 into one with md5 left SHA-256 ETags in the md5
cluster. It now reads both filers' digests and refuses to start when they
differ, with a warning when either filer does not report one.

The S3 sink sent the entry digest as Content-MD5 unless its own process
ran under fips140=only. From a sha256 source into a target that checks
Content-MD5 every single-part upload failed with BadDigest and the backup
retried it forever. It now asks the source filer for its digest once and
forwards the entry digest only when that is md5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.meta.backup, filer.backup, filer.replicate: keep digests within one kind

filer.meta.backup opened its backup store in its own digest mode, so a copy
of a sha256 filer made by a process outside fips140=only got MD5 directory
keys and could not be opened as that filer's store. It now adopts the source
filer's digest before the store opens.

filer.backup and filer.replicate with a filer sink copied entry digests into
the target filer without comparing digests, as filer.sync did before. Both
now refuse, and exit, when the source and target filers report different
digests. replication.CheckDigestsMatch is the shared check; filer.sync uses
it too.

weed server and weed mini apply -filer.digest before they start the master
and volume server, so every part of the process hashes with one digest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, filer.sync: refuse a peer filer with another digest; exit on mismatch

Filers peering through the meta aggregator now compare digests. A peer with
a store of its own would replay entries holding the other kind of digest
into this filer's store, so its changes are refused and the reason logged
on every reconnect. A fresh filer bootstrapping from such a peer refuses the
copy, so a new filer whose digest differs from the existing filers' exits at
startup. A peer on the same store writes into it directly, and refusing its
stream would only starve this filer's subscribers, so that mismatch is
logged as an error and the stream kept.

filer.sync exits with status 1 when the two filers use different digests,
as filer.backup and filer.replicate do: it is a configuration error a
restart cannot fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: -digest help says a store keeps the digest it was created with

The help text said existing data keeps the digest it was written with,
which reads as if the flag could be changed on a store with data. Stores
that hash keys stop finding existing entries under another digest, so the
text now says to set it when the store is created, and that switching means
copying the metadata into a new store with fs.meta.save and fs.meta.load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, s3, replication, admin, iceberg: address review of the digest checks

- A filer checks its peers' digests on every start, not only at the fresh-store
  bootstrap: the store signature is written when the store opens, so a fresh
  filer refused at bootstrap restarted as an initialized store and served
  without its peers' files.
- Under digest sha256 with MD5 available (not fips140=only), a client's
  Content-MD5 is checked against a separate real MD5 of the body, in S3 object,
  part and directory-marker uploads and in filer HTTP uploads. It goes
  unchecked only when no MD5 can be computed.
- filer.backup and filer.replicate exit only on a DigestMismatchError; a filer
  that cannot be reached is retried, so a backup started before its target is
  up resumes once the target answers.
- The admin file browser and Iceberg compaction write chunks into a filer they
  do not follow at process level. They read that filer's digest and pass it to
  the chunk upload (ChunkedUploadOption.Digest), so their content and chunk
  digests match the filer's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer, s3 sink: retire digest-refused peers; no Content-MD5 for an unknown digest

- A peer refused for its digest counts as unreachable: the check now runs
  before notePeerReachable and its error wraps errPeerUnreachable, so the
  subscribe loop retires the peer from the low watermarks after the usual
  grace instead of keeping its zero watermark, which held every aggregated
  subscriber at the settled horizon.
- When the source filer's digest cannot be read, the S3 sink no longer sends
  the entry digest as Content-MD5: it may be a SHA-256 digest the target
  would reject.
- TestCheckPeerDigest skips the cases that need MD5 locally under
  GODEBUG=fips140=only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg, util: tests for the per-filer digest

The Iceberg tests' fake filer serves GetFilerConfiguration, which uploads now
call to read the target filer's digest; it reports none, as a filer that
predates the setting does. TestNewContentMd5 computes a real MD5, so it skips
under GODEBUG=fips140=only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: bound each startup peer digest read with a timeout

CheckPeerDigests read each peer's configuration with the caller's context,
which at startup has no deadline, so a peer that accepted the connection but
never answered held the filer's startup. Each read now has its own
peerDigestReadTimeout (10s, inside the one-minute announce cap), after which
the peer is logged and skipped like an unreachable one.

setDigestForTest restores the previous digest setting instead of the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util, s3, filer: one-shot digests for data held in memory

Md5For hashes with md5.Sum or sha256.Sum256 instead of writing into a
hash.Hash, and the Content-MD5 checks of S3 directory markers and of filer
uploads saved inside the filer use BodyMd5Of on the bytes they already hold.
Streaming uploads keep their io.TeeReader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: hash Content-MD5 on the upload's digest hash, not on its body

teeContentMd5 wrapped the request body in a TeeReader before
uploadRequestToChunks. CodeQL's go/reflected-xss query follows the body
from r.Body through the chunk upload to upload_content.go's
w.Write(prepared.data), which it resolves to the response-writer
wrappers in s3vectors, s3tables, webdav and server/common.go. Those
flows exist on master from other sources (extract, tagging, the
multipart POST); the wrapped body put them through lines this PR
changed, so CodeQL reported six of them as new.

newUploadHash now builds the digest hash that uploadReaderToChunks and
the inline-append paths already tee the body into, and adds the
separate MD5 there; uploadBodyMd5 reads it back for the check. The body
reaches uploadRequestToChunks as on master. Behaviour is unchanged:
under digest sha256 with MD5 available, Content-MD5 is checked against
the body's MD5, including empty and inline appends.

Checked with CodeQL 2.27.6 (go-queries 1.6.16, ReflectedXss.ql) on this
tree: no flow of the nine reflected-XSS results passes a changed line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* replication: keep the ReadFilerSignature name and return the digest with it

Renaming it to ReadFilerSignatureAndDigest left its other callers with an
undefined function. Keep the name and add the digest to what it returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util: keep HashToInt32 off the digest mode

HashToInt32 names things that processes derive on their own, such as a
mount's local socket path and MQ partition key hashes. weed mount adopts
a -digest=sha256 filer's digest before it names its socket, while
weed shell mount.configure keeps md5 and dialed a different socket.
Follow GODEBUG only, as before the digest setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.meta.backup: wait for the source filer's digest instead of exiting

A backup with a saved offset used to start its stream loop and retry
until the source filer answered. Reading the digest first made it exit
when started before the filer. Retry the read; exit only when the
digest is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-24 20:17:26 -08:00

69 lines
2.5 KiB
Go

package replication
import (
"errors"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
)
// DigestMismatchError reports two filers that use different digests. It is a
// configuration error a restart cannot fix, unlike a filer that cannot be
// reached.
type DigestMismatchError struct {
FilerA, DigestA, FilerB, DigestB string
}
func (e *DigestMismatchError) Error() string {
return fmt.Sprintf("filer %s uses digest %s and filer %s uses digest %s; replication would copy digests of one kind into the other", e.FilerA, e.DigestA, e.FilerB, e.DigestB)
}
// IsDigestMismatch reports whether err is or wraps a DigestMismatchError.
func IsDigestMismatch(err error) bool {
var mismatch *DigestMismatchError
return errors.As(err, &mismatch)
}
// CheckDigestsMatch refuses replication between filers that use different
// digests, with a DigestMismatchError: replication copies entry digests and
// ETags as they are, so one side would hold digests of the other kind. A
// filer that reports none predates the setting and cannot be checked.
func CheckDigestsMatch(filerA, digestA, filerB, digestB string) error {
switch {
case digestA == "" || digestB == "":
glog.Warningf("cannot check that filers %s and %s use the same digest: one of them does not report it", filerA, filerB)
case digestA != digestB:
return &DigestMismatchError{FilerA: filerA, DigestA: digestA, FilerB: filerB, DigestB: digestB}
}
return nil
}
// digestTarget is a sink that writes into a filer and can say which digest
// that filer uses.
type digestTarget interface {
TargetFiler() string
TargetDigest() (string, error)
}
// CheckSinkDigest refuses a sink that writes into a filer whose digest differs
// from the source filer's, with a DigestMismatchError; a filer that cannot be
// reached gives an ordinary error the caller can retry. Sinks that do not
// write into a filer (S3, cloud storage, local files) are not checked: they
// hold no filer digests.
func CheckSinkDigest(sourceFiler string, sourceDigest func() (string, error), dataSink sink.ReplicationSink) error {
target, ok := dataSink.(digestTarget)
if !ok {
return nil
}
digestA, err := sourceDigest()
if err != nil {
return fmt.Errorf("read digest of source filer %s: %w", sourceFiler, err)
}
digestB, err := target.TargetDigest()
if err != nil {
return fmt.Errorf("read digest of target filer %s: %w", target.TargetFiler(), err)
}
return CheckDigestsMatch(sourceFiler, digestA, target.TargetFiler(), digestB)
}