Files
seaweedfs/weed/command/iam.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

163 lines
6.3 KiB
Go

package command
import (
"context"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/util/version"
"time"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/iamapi"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/grace"
// Import credential stores to register them
_ "github.com/seaweedfs/seaweedfs/weed/credential/filer_etc"
_ "github.com/seaweedfs/seaweedfs/weed/credential/memory"
_ "github.com/seaweedfs/seaweedfs/weed/credential/postgres"
)
var (
iamStandaloneOptions IamOptions
)
type IamOptions struct {
filer *string
masters *string
ip *string
ipBind *string
listenOnLocalhost *bool
port *int
idleTimeout *int
selfServiceRequiresGrant *bool
}
func init() {
cmdIam.Run = runIam // break init cycle
iamStandaloneOptions.filer = cmdIam.Flag.String("filer", "localhost:8888", "comma-separated filer server addresses for high availability")
iamStandaloneOptions.masters = cmdIam.Flag.String("master", "localhost:9333", "comma-separated master servers")
iamStandaloneOptions.ip = cmdIam.Flag.String("ip", util.DetectedHostAddress(), "iam server http listen ip address")
iamStandaloneOptions.ipBind = cmdIam.Flag.String("ip.bind", "", "ip address to bind to. If empty, default to same as -ip option.")
iamStandaloneOptions.port = cmdIam.Flag.Int("port", 8111, "iam server http listen port")
iamStandaloneOptions.idleTimeout = cmdIam.Flag.Int("idleTimeout", 10, "connection idle seconds")
iamStandaloneOptions.selfServiceRequiresGrant = cmdIam.Flag.Bool("selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
}
var cmdIam = &Command{
UsageLine: "iam [-port=8111] [-filer=<ip:port>[,<ip:port>]...] [-master=<ip:port>,<ip:port>]",
Short: "[DEPRECATED] start a standalone iam API compatible server",
Long: `[DEPRECATED] start a standalone iam API compatible server.
DEPRECATION NOTICE:
The standalone 'weed iam' command is deprecated and will be removed in a future release.
The IAM API is now embedded in the S3 server by default. Simply use 'weed s3' instead,
which provides both S3 and IAM APIs on the same port (enabled by default with -iam=true).
This simplifies deployment by running a single server instead of two separate servers,
following the pattern used by MinIO and Ceph RGW.
To use the embedded IAM API:
weed s3 -port=8333 # IAM API is available on the same port
To disable the embedded IAM API (if you prefer the old behavior):
weed s3 -iam=false # Run S3 without IAM
weed iam -port=8111 # Run IAM separately (deprecated)
Multiple filer addresses can be specified for high availability, separated by commas.`,
}
func runIam(cmd *Command, args []string) bool {
glog.Warningf("================================================================================")
glog.Warningf("DEPRECATION WARNING: 'weed iam' is deprecated and will be removed in a future release.")
glog.Warningf("The IAM API is now embedded in 'weed s3' by default (use -iam=true, which is the default).")
glog.Warningf("Please migrate to using 'weed s3' which provides both S3 and IAM APIs on the same port.")
glog.Warningf("================================================================================")
return iamStandaloneOptions.startIamServer()
}
func (iamopt *IamOptions) startIamServer() bool {
filerAddresses := pb.ServerAddresses(*iamopt.filer).ToAddresses()
util.LoadSecurityConfiguration()
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
var digest string
for {
err := pb.WithOneOfGrpcFilerClients(false, filerAddresses, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
if err != nil {
return fmt.Errorf("get filer configuration: %v", err)
}
glog.V(0).Infof("IAM read filer configuration: %s", resp)
digest = resp.Digest
return nil
})
if err != nil {
glog.V(0).Infof("wait to connect to filers %v", filerAddresses)
time.Sleep(time.Second)
} else {
glog.V(0).Infof("connected to filers %v", filerAddresses)
break
}
}
adoptFilerDigest(*iamopt.filer, digest)
masters := pb.ServerAddresses(*iamopt.masters).ToAddressMap()
router := mux.NewRouter().SkipClean(true)
iamApiServer, iamApiServer_err := iamapi.NewIamApiServer(router, &iamapi.IamServerOption{
Masters: masters,
Filers: filerAddresses,
Port: *iamopt.port,
GrpcDialOption: grpcDialOption,
IamSelfServiceRequiresGrant: *iamopt.selfServiceRequiresGrant,
})
glog.V(0).Info("NewIamApiServer created")
if iamApiServer_err != nil {
glog.Fatalf("IAM API Server startup error: %v", iamApiServer_err)
}
// Register shutdown handler to prevent goroutine leak
grace.OnInterrupt(func() {
iamApiServer.Shutdown()
})
// The loopback companion listener is only kept while -ip.bind is unset and
// the bind address falls back to -ip. An explicit -ip.bind pins the port to
// exactly that address.
bindIp := *iamopt.ip
listenOnLocalhost := iamopt.ipBind == nil || *iamopt.ipBind == ""
if iamopt.ipBind != nil && *iamopt.ipBind != "" {
bindIp = *iamopt.ipBind
}
if iamopt.listenOnLocalhost != nil {
listenOnLocalhost = *iamopt.listenOnLocalhost
}
idleTimeout := idleTimeoutSeconds(iamopt.idleTimeout)
listenAddress := fmt.Sprintf(":%d", *iamopt.port)
iamApiListener, iamApiLocalListener, err := util.NewIpAndLocalListeners(bindIp, *iamopt.port, idleTimeout, listenOnLocalhost)
if err != nil {
glog.Fatalf("IAM API Server listener on %s error: %v", listenAddress, err)
}
glog.V(0).Infof("Start Seaweed IAM API Server %s at http port %d", version.Version(), *iamopt.port)
if iamApiLocalListener != nil {
go func() {
if err = newHttpServer(router, nil).Serve(iamApiLocalListener); err != nil {
glog.Errorf("IAM API Server Fail to serve: %v", err)
}
}()
}
if err = newHttpServer(router, nil).Serve(iamApiListener); err != nil {
glog.Fatalf("IAM API Server Fail to serve: %v", err)
}
return true
}