Files
seaweedfs/weed/mount/sse_s3_reader.go
T
a9fda6b50f filer clients: authenticate filer gRPC calls with per-call admin tokens (#13039)
* 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>
2026-11-27 18:53:59 -08:00

237 lines
7.6 KiB
Go

package mount
import (
"context"
"errors"
"fmt"
"io"
"math/rand/v2"
"net/url"
"path"
"slices"
"strings"
"sync"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
smithyhttp "github.com/aws/smithy-go/transport/http"
"google.golang.org/grpc"
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
)
var (
errSSES3Permission = errors.New("S3 gateway denied object read")
errNoS3Gateway = errors.New("no S3 gateway is registered with the masters")
)
// SSES3Reader makes signed range requests for one mounted bucket. The S3
// gateway owns the encryption keys and applies its normal GetObject policy.
type SSES3Reader struct {
cfg aws.Config
endpoints func(context.Context) ([]string, error)
root string
bucket string
clientLock sync.Mutex
client *s3.Client
endpoint string
}
// NewSSES3Reader reads through the S3 gateways the filer's masters list for
// its filer group.
func NewSSES3Reader(ctx context.Context, bucketPath string, filers []pb.ServerAddress, grpcDialOption grpc.DialOption) (*SSES3Reader, error) {
cfg, err := config.LoadDefaultConfig(ctx, config.WithDefaultRegion("us-east-1"))
if err != nil {
return nil, err
}
return &SSES3Reader{
cfg: cfg,
endpoints: func(ctx context.Context) ([]string, error) {
return listS3Endpoints(ctx, filers, grpcDialOption)
},
root: bucketPath + "/",
bucket: path.Base(bucketPath),
}, nil
}
func listS3Endpoints(ctx context.Context, filers []pb.ServerAddress, grpcDialOption grpc.DialOption) ([]string, error) {
var masters []string
var filerGroup string
if err := pb.WithOneOfGrpcFilerClients(false, filers, grpcDialOption, nil, func(client filer_pb.SeaweedFilerClient) error {
resp, err := client.GetFilerConfiguration(ctx, &filer_pb.GetFilerConfigurationRequest{})
if err != nil {
return err
}
masters, filerGroup = resp.Masters, resp.FilerGroup
return nil
}); err != nil {
return nil, err
}
var endpoints []string
var err error
// Only the leader lists the gateways, so a follower answers with none.
for _, master := range masters {
err = pb.WithMasterClient(ctx, false, pb.ServerAddress(master), grpcDialOption, false, func(client master_pb.SeaweedClient) error {
resp, err := client.ListClusterNodes(ctx, &master_pb.ListClusterNodesRequest{ClientType: cluster.S3Type, FilerGroup: filerGroup})
if err != nil {
return err
}
for _, node := range resp.ClusterNodes {
if node.S3Endpoint != "" {
endpoints = append(endpoints, node.S3Endpoint)
}
}
return nil
})
if len(endpoints) > 0 {
return endpoints, nil
}
}
if err != nil {
return nil, err
}
return nil, errNoS3Gateway
}
// gatewayClient picks one of the registered gateways other than failed, using
// HTTPS whenever an HTTPS gateway is registered, and keeps it until a request
// cannot reach it.
func (r *SSES3Reader) gatewayClient(ctx context.Context, failed string) (*s3.Client, string, error) {
r.clientLock.Lock()
defer r.clientLock.Unlock()
if r.client != nil && r.endpoint != failed {
return r.client, r.endpoint, nil
}
endpoints, err := r.endpoints(ctx)
if err != nil {
return nil, "", err
}
endpoints = slices.DeleteFunc(endpoints, func(endpoint string) bool {
u, err := url.Parse(endpoint)
return err != nil || u.Scheme != "https" && u.Scheme != "http" || u.Hostname() == "" || u.User != nil || u.RawQuery != "" || u.Fragment != ""
})
isHTTPS := func(endpoint string) bool {
u, err := url.Parse(endpoint)
return err == nil && u.Scheme == "https"
}
if https := slices.DeleteFunc(slices.Clone(endpoints), func(endpoint string) bool { return !isHTTPS(endpoint) }); len(https) > 0 {
endpoints = https
}
endpoints = slices.DeleteFunc(endpoints, func(endpoint string) bool { return endpoint == failed })
if len(endpoints) == 0 {
return nil, "", errNoS3Gateway
}
endpoint := endpoints[rand.IntN(len(endpoints))]
if !isHTTPS(endpoint) {
glog.Warningf("-sseS3Read: no HTTPS S3 gateway is registered, reading %s over plain HTTP", endpoint)
}
r.client = s3.NewFromConfig(r.cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(endpoint)
o.UsePathStyle = true
})
r.endpoint = endpoint
return r.client, endpoint, nil
}
// getObject retries once on another gateway when the current one cannot be
// reached.
func (r *SSES3Reader) getObject(ctx context.Context, req *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
client, endpoint, err := r.gatewayClient(ctx, "")
if err != nil {
return nil, err
}
obj, err := client.GetObject(ctx, req)
var sendErr *smithyhttp.RequestSendError
if !errors.As(err, &sendErr) || ctx.Err() != nil {
return obj, err
}
r.clientLock.Lock()
if r.client == client {
r.client = nil
}
r.clientLock.Unlock()
retry, _, pickErr := r.gatewayClient(ctx, endpoint)
if pickErr != nil {
return nil, err
}
return retry.GetObject(ctx, req)
}
func sseObjectETag(entry *filer_pb.Entry) string {
if stored := string(entry.GetExtended()[s3_constants.ExtETagKey]); stored != "" {
return `"` + strings.Trim(stored, `"`) + `"`
}
return `"` + filer.ETag(entry) + `"`
}
func (r *SSES3Reader) readAt(ctx context.Context, fullPath, etag, version string, size int64, buf []byte, offset int64) (int64, error) {
key, ok := strings.CutPrefix(fullPath, r.root)
if !ok || key == "" || key == "." || key == ".." || path.IsAbs(key) || path.Clean(key) != key || strings.HasPrefix(key, "../") {
return 0, errNativeSSERead
}
versionFile := false
if dir, name := path.Split(key); version != "" && name == "v_"+version {
if object, found := strings.CutSuffix(strings.TrimSuffix(dir, "/"), s3_constants.VersionsFolder); found && object != "" {
key, versionFile = object, true
}
}
if offset < 0 || size < 0 {
return 0, errors.New("invalid S3 object range")
}
if len(buf) == 0 {
return 0, nil
}
if offset >= size {
return 0, io.EOF
}
n := min(int64(len(buf)), size-offset)
last := offset + n - 1
req := &s3.GetObjectInput{
Bucket: aws.String(r.bucket), Key: aws.String(key),
IfMatch: aws.String(etag), Range: aws.String(fmt.Sprintf("bytes=%d-%d", offset, last)),
}
currentNull := version == "null" && !versionFile
if version != "" && !currentNull {
req.VersionId = aws.String(version)
}
obj, err := r.getObject(ctx, req)
if currentNull && (err == nil && aws.ToString(obj.VersionId) != "" && aws.ToString(obj.VersionId) != version || err != nil && !isS3PermissionError(err)) {
if err == nil {
obj.Body.Close()
}
req.VersionId = aws.String(version)
obj, err = r.getObject(ctx, req)
}
if err != nil {
if isS3PermissionError(err) {
return 0, errSSES3Permission
}
return 0, err
}
defer obj.Body.Close()
wantRange := fmt.Sprintf("bytes %d-%d/%d", offset, last, size)
if aws.ToString(obj.ContentRange) != wantRange || aws.ToInt64(obj.ContentLength) != n ||
aws.ToString(obj.ETag) != etag || obj.ServerSideEncryption != types.ServerSideEncryptionAes256 ||
(version != "" && aws.ToString(obj.VersionId) != version && (req.VersionId != nil || aws.ToString(obj.VersionId) != "")) {
return 0, errors.New("S3 response did not match the encrypted object range")
}
if _, err := io.ReadFull(obj.Body, buf[:n]); err != nil {
return 0, err
}
return n, nil
}
func isS3PermissionError(err error) bool {
var respErr *smithyhttp.ResponseError
return errors.As(err, &respErr) && (respErr.HTTPStatusCode() == 403 || respErr.HTTPStatusCode() == 401)
}