mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* util: move the guarded dialer out of the volume server The deny list and the dialer that re-checks every resolved address at connect time move into weed/util/netguard, with the allow-private knob as a policy, so other outbound clients can dial through the same guard. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mq: fence consumer group position saves by generation A position saved with a generation is written only while the stored one carries no newer generation, or the same generation from another writer: the save reads the stored position and writes on the condition that it is unchanged, routed to the entry's owner filer, and re-reads when another writer got there first. A single consumer that elects itself can then store its offsets without a deposed predecessor overwriting them. Saves without a generation keep overwriting as before. Loading a missing position now reports ErrNotFound instead of trying every filer and marking each one failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: post bucket event batches to a webhook target A webhook destination renders a batch of records as one {"Records":[...]} body, posts it with the target's bearer token, and takes any 2xx as delivered. The renderer is a function the destination holds, a stand-in that refuses until the record renderer lands. It dials through the guarded dialer under a new Local policy, which refuses metadata-service, link-local and unspecified addresses at connect time but lets an operator point a target at a private host or a sidecar on loopback. Redirects are followed only to the endpoint's own host and never down from https. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: forward each target's topic to its destination A Forwarder runs beside the Publisher on every gateway. Each enabled target with a destination gets a lock, s3.notify.<target>, held through LockClient.StartLongLivedLock, so one gateway forwards a target and targets spread across gateways; a topic target has none. The holder runs one loop per partition of the target's topic: fetch from the stored offset at the partition's leader, decode the records, deliver them as one batch, store the next offset. A fetch that stops short at a segment boundary just continues from its next offset. A failed send, a rejected one included, blocks the partition and is retried with capped backoff, and the offset is stored only after the destination took the batch. An offset that retention removed skips to the earliest one and counts the gap. Offsets go to the consumer group offset storage under group forwarder, fenced by the lock generation. On taking the lock a forwarder stores each partition's offset back under its generation, so an older holder's saves are refused and it stops. A stored generation at or above its own means the lock was lost, unless the forwarder has since held the lock through a full renewal, which happens only when the lock server's counter restarted; it then fences above the stored generation. Metrics per target: records delivered, send retries, retention gaps, and lag from the forwarding gateway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: run notification forwarders beside the publisher Every gateway with filers starts a Forwarder with the publisher. It elects itself per target through the gateway's object-write lock client, stores offsets in the filer, reads through the same brokers the publisher uses, follows the target registry as the publisher does, and releases its locks on shutdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: post a test event to a webhook on its own The renderer refuses an s3:TestEvent that shares a body with other records, so a webhook splits a batch around each one and posts them in order, the test event as its own body. The test is a stand-in until the renderer's lands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: skip a bucket event record that can never be delivered A record that does not decode, or that the renderer refuses, used to be retried like a failed send and would block its partition for good. The webhook now renders a whole batch before posting any of it and names the records that cannot render; the forwarder drops those, counts them per target and reason in notification_skipped_records_total, logs each offset once, and sends the rest. Only transport and HTTP failures are retried. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mq: read a fenced position save at the filer that applies it The fenced save read the stored position from whichever filer answered and then sent its conditional write to the key's owner, so with filers that keep their own stores the condition could be checked against an entry other than the one read, failing until replication caught up or judging a stale copy. The storage now takes the ring's owner lookup, the one route-by-key writes already use, and sends both the read and the write to that filer. The gateway hands its object-write lock client's PrimaryForKey to the forwarder offsets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: follow a repartitioned notification topic A forwarder listed its topic's partitions once, when it took the lock, so a repartition left the new partitions unforwarded until the lock moved. It now lists them again every 30 seconds, starts a loop for each new partition, and marks the loops of partitions no longer listed as replaced; such a loop keeps delivering what its partition holds and ends once it has caught up. A failed fetch now looks up the leader of that exact partition, not of any partition with the same range. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: let the first forwarder take the lock before the second asks The election test synced both gateways at once, and each forwarder asks for its lock from its own goroutine, so the second could ask first and store the offsets the test expected from the first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: warn when a partition returns nothing below its high-water mark A fetch that returned no records and kept its next offset below the high-water mark looked like an idle partition, so the forwarder waited without a word while its lag stayed up. It still waits, since skipping would lose the records, but now warns at most once a minute with the topic, partition, offset and high-water mark, so the lag has an explanation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: render webhook bodies with the bucket event renderer The webhook now renders through Render and posts each s3:TestEvent alone by IsTestEvent, replacing the stand-ins it held until the renderer landed. The tests send real records and read the object keys and test request ids back out of the bodies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * netguard: refuse the IPv6 and Alibaba metadata service addresses AWS's IPv6 metadata address fd00:ec2::254 is unique-local and Alibaba's 100.100.100.200 sits in the CGNAT range, so the Private and Local policies let a webhook or remote endpoint reach them. Refuse both under every policy, like 169.254.169.254. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * grafana: chart the notification forwarder metrics Add an S3 Notifications row with delivered records, lag, send retries, and records skipped or lost to retention per target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: follow only webhook redirects that keep the POST On a 301, 302 or 303 Go resends the request as a GET without a body, so an endpoint that redirected and then answered 2xx counted as delivered while it received no records. Only 307 and 308, which keep the method and resend the body, are followed now; any other redirect fails the send and is retried. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: keep webhook endpoint secrets out of errors and logs The guarded dialer names its endpoint in every refusal, and the forwarder logs that error, so a webhook endpoint carrying credentials in its userinfo or a token in its query had them written to the log. The dialer now gets the endpoint as scheme, host and path only, and the parse and request errors no longer echo the URL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mq: load a consumer group position from its owner filer A forwarder taking over read its start offset from whichever filer answered, while its fenced saves read and write at the key's owner. With filers that keep their own stores, a replica still holding an older offset handed the new holder that offset, which it then stored under its higher generation at the owner, replaying everything delivered since. Loads now go to the owner too when the storage knows it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mq: bound consumer group position loads and saves by the caller LoadConsumerGroupPosition and SaveConsumerGroupPosition ran their filer calls under context.Background(), so a filer whose store stalled held a forwarder's save forever: ending the lead waited on it, the lock was never released, and Close hung while the lock kept renewing. Both now take the caller's context; the forwarder gives each call its RPC deadline under the lead's context, so stopping returns and the lock is released. The offset wrappers the broker uses keep their signatures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mq: list a consumer group's positions across partition generations ListConsumerGroupPositions walks every generation and range directory of a topic and returns the group's stored position in each, keyed by partition directory, so a consumer can tell which ranges it holds offsets for that the topic no longer lists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: stop forwarding ranges a repartition removed A repartition drops the old assignments, and the broker serves only assigned ranges, so the loop draining a removed range retried a refused fetch forever, and after a restart nothing looked at it at all. Reading a removed range's flushed data would need a read-only path in the broker. Until then a removed range's loop delivers the batch it holds and ends, and the forwarder logs an error naming each range it stops and, when it takes the lock, each partition directory holding a stored offset the topic no longer lists. The fake broker now refuses unassigned ranges as the real one does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: deliver what a repartition left in the retired ranges The lookup now offers the sealed ranges a repartition retired, and any broker serves them from their files, so the forwarder no longer drops them. On taking the lock and on each re-list it asks for the retired ranges too and runs a loop for each, from its stored offset up to its high-water mark, after which the loop ends; a range it was forwarding as current and that is now retired gets the same end. A read below a retired range's log start counts as a retention gap, as for a live one. A key's records in a retired range precede its records in the ranges that replaced it, and loops running side by side would not keep that order. So the retired ranges go first, one generation at a time, oldest first, and the current ranges start only once none is left. A range listed neither as assigned nor as retired, as while a drained range is not sealed yet, pauses until it is offered again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: match a retired range to its forwarding loop by directory The broker lists a retired range at the second its generation directory is named for, while the assignment the forwarder started its loop from carried nanoseconds. After a repartition the retired entry missed the running loop, so a second loop started on the same stored offset and delivered the batch the first was still retrying. Key the loops by partition directory; the fake broker now lists retired ranges and the fake positions store offsets at that resolution, like the real ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: replace a paused forwarding loop only once it ended A range drained but not sealed yet is listed neither as assigned nor as retired, so its loop is paused and dropped from the lead's loops while it may still be retrying a batch. Once the range was offered as retired a new loop started at the stored offset and sent that batch again. Keep a paused loop until it ends, and start the range again only then. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: hold the new ranges while a drained one is not sealed yet A repartition saves the new ranges before it drains and seals the old ones, so for a while an old range is offered neither as assigned nor as retired, and the forwarder started the new ranges at once: a key's new records could go out before its records still waiting in the old range. On each look at the topic the forwarder now lists the directories holding a stored offset that the topic does not offer, and probes each at its offset; one that may still hold records past it, refused because it is not sealed, holds the new ranges until it is offered as retired and drained. One that answers with nothing past the stored offset is settled, sealed and empty or already delivered, and stays so for the lead. While holding, the forwarder logs at most once a minute and keeps looking even with no loop running. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: have the fake broker serve only the generation a fetch names The broker now refuses a fetch naming a generation other than the one it assigns for the range, unless that generation is sealed and read as retired. The forwarder tests' fake broker matched an assigned range by its range alone, so it served the live generation to a fetch for an older, unsealed one; it now matches the generation too, as the broker does. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: hold the current ranges until the stored offsets can be listed A lead whose first look at the stored offsets failed took that as no old range left unsettled and started the current ranges, delivering a key's newer records ahead of the ones still in a drained range that is not sealed yet. A failed look now holds the current ranges as an unsettled range does, and is retried with backoff and on each partition refresh; the current ranges start only after a look succeeds. Retired ranges and loops already running carry on meanwhile. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3api: leave forwarding off on an unusable broker TLS config The forwarder loaded the grpc.msg_broker client TLS config with LoadClientTLS, which exits the process on a bad one, and every gateway with filers builds a forwarder whether or not it has webhook targets. It now loads it as the publisher does: an unusable config is logged and leaves forwarding off while the gateway keeps serving. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: hold the new ranges for an unsealed old range with no stored offset The check for old ranges still unsealed looked only at ranges holding a forwarder offset. A forwarder starting for the first time while a drained range was not sealed yet had stored none in it, so it started the new ranges and delivered a key's newer records ahead of its older ones. The consumer group listing now names every partition directory of the topic, with no position where the group stored none, and such a range is probed from offset 0. A range with nothing left past that offset, including one whose records retention already removed, is settled. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * mq: check the load in the fenced save race test The test read the stored position back ignoring the load's error, so a failed load panicked on a nil position instead of reporting the error. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * mq: take a partition over at the offset its last holder committed A new forwarder generation claimed a partition by writing the offset it had loaded. When the old generation committed again in between, the claim rewound the stored offset and the new lead replayed what had been delivered since. A fenced save that takes over from an older generation now keeps the offset that generation committed last, rereading it on a conditional write miss, and hands it back in the position, and the forwarder starts from it. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * mq: wait for a position's owner filer instead of asking another A position store routed by owner fell back to any filer while the owner of a key was not known yet, as before the lock ring is learned. That filer's not-found ended the load there, though it may only not have caught up, and a claim on it would start the partition over. Loads and fenced saves now fail until the owner is known, and the forwarder retries them; a store without an owner routing still uses any filer. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: wait for the last checkpoint before a forwarder handoff in tests The resume, repartition and election tests stopped the first forwarder once its records were delivered but possibly before their offsets were stored, so they could pass on a replay rather than on a committed handoff. They now wait for the final offsets first. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: have Close wait for removed targets to release their locks A target removed from the registry had its forwarder cancelled and dropped from the targets, so a Close soon after did not wait for it and returned with that target's lock possibly still held. Removed forwarders are now kept until they are done, and Close waits for them as well. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: hold newer retired generations behind an unsealed older one A range of an old generation still unsealed held back only the current ranges. A generation retired after it was offered and delivered right away, ahead of the older generation's records of the same keys. The unsealed range's generation now counts as the oldest one left, so only that generation's own sealed ranges are forwarded until it is sealed. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: check the forwarder lock right before each send A lead checked its lock at the top of each fetch and after a failed send's backoff, so a lead that lost the lock while it fetched a batch still sent it, and the next holder sent it again. The lock is now checked right before each send. Delivery stays at-least-once: a lock lost during a send cancels that send once the lead notices, which may be after the endpoint took the batch. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: read a retired range past a broker that answers with an error Any broker serves a retired range from its files, but a fetch took the first broker's answer even when it was a storage error, and each retry asked that broker first again, so one broker that could not read the files held the range up for good. A retired fetch answered with an error other than an offset out of range now goes on to the next broker, and returns the error only when none could read it. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: count ranges held back behind an older generation in the lag The lag gauge summed what each running loop reported, so the current ranges held back while an older generation was still unsealed, and the retired generations waiting their turn, showed no lag however much piled up in them. On each partition refresh the lead now reports their lag from the stored offset to the high-water mark, read from the range's leader or the retired listing, and drops the lag of ranges it no longer has. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: hold retired generations too while stored offsets cannot be listed A lead whose look at the stored offsets failed held only the current ranges and went on forwarding retired generations, though an older generation it could not see might still be unsealed and hold earlier records of the same keys. A failed look now holds every range not yet running until a look succeeds; loops already running carry on. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: read stored offsets only of the ranges a lead does not know yet The check for unsettled ranges listed every partition directory and read every stored offset in it one by one under one deadline, before dropping the ranges the topic offers and those already found settled. A topic with many past generations could take longer than the deadline on every refresh and hold a new lead for good. The listing now takes the directories to skip and reads only the remaining offsets, a few at a time, and the lead skips the offered ranges and those it found settled. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: test the failed-listing hold with no unsealed range The failed-listing test also had an older generation unsealed, which holds the newer ranges on its own once a listing succeeds, so it did not show the listing guard by itself. A second case has no unsealed range: nothing goes out while the listing fails, and both the retired and the current range do once it succeeds. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: stop the lead before marking a failed loop ended A forwarding loop that failed, as on being deposed, was marked ended before it cancelled its lead. A partition refresh in between saw a retired range's loop ended, took the range as delivered, and started the next generation's ranges on the lead that was about to stop. The lead is now cancelled first, so no refresh acts on the failed loop. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: count no retention gap below an offset only claimed A lead claiming a partition with no stored offset wrote offset 0 to fence it before reading anything. A lead that stopped there left 0 behind, and the next one, finding the log start past it, counted those offsets as lost though no forwarder had ever been at them. A claim made before any read is now marked as such, a takeover keeps the marker of the entry it replaces, and only an offset a lead read from counts a gap. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: retry a takeover save at the offset it kept A claim's save that took a partition over from an older generation kept that generation's last offset, but when its reply was lost the retry went out with the offset the claim had loaded. The stored entry was by then the claim's own, so the retry kept nothing and rewound the partition, replaying what had been delivered since. A failed save now retries with the offset and marker the attempt settled on. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: store an offset as read from once the first fetch succeeds A claimed offset lost its claimed marker only when a later save moved it, so a lead that read an empty partition and stopped left it marked as never read. Records published and then removed by retention while no gateway forwarded the target were skipped without a gap counted or logged. The first successful fetch now stores the offset as read from, once per claim, even when it has not moved. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: wait for the stalled topic's publisher before reading it in a test The worker sets the topic publisher up on its own goroutine, which on a loaded host can start after Publish has stopped waiting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: keep what a timed-out offset check settled for the next one A check for unsettled ranges that could not read every stored offset within its deadline returned nothing, so each retry read the same ranges again and a long enough history kept the new ranges held for good. The listing now returns the positions it read along with the error and goes on past one that fails, and the check probes those and keeps the ranges it finds settled before reporting the failure, so the next check reads only the rest. The ranges stay held until a check covers them all. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 * s3: say which records the notification lag counts The lag gauge's help, and the dashboard panel with none, read as every undelivered record, but the records of an old range still being sealed after a repartition cannot be read and are not counted. Both now say the gauge covers the current ranges, the ranges held back behind an older generation, and the sealed retired ranges. Claude-Session: https://claude.ai/code/session_011Upvai5AjLiNE5B1HCAn34 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1690 lines
78 KiB
Go
1690 lines
78 KiB
Go
package s3api
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"math"
|
||
"net"
|
||
"net/http"
|
||
"os"
|
||
"slices"
|
||
"strings"
|
||
"sync"
|
||
"sync/atomic"
|
||
"time"
|
||
|
||
"github.com/gorilla/mux"
|
||
"google.golang.org/grpc"
|
||
"google.golang.org/grpc/codes"
|
||
"google.golang.org/grpc/status"
|
||
|
||
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
||
"github.com/seaweedfs/seaweedfs/weed/credential"
|
||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||
"github.com/seaweedfs/seaweedfs/weed/iam/integration"
|
||
"github.com/seaweedfs/seaweedfs/weed/iam/policy"
|
||
"github.com/seaweedfs/seaweedfs/weed/iam/sts"
|
||
"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/pb/s3_lifecycle_pb"
|
||
"github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
|
||
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
|
||
. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3notification"
|
||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||
"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
|
||
"github.com/seaweedfs/seaweedfs/weed/util/grace"
|
||
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
||
util_http_client "github.com/seaweedfs/seaweedfs/weed/util/http/client"
|
||
"github.com/seaweedfs/seaweedfs/weed/util/request_id"
|
||
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
||
)
|
||
|
||
type S3ApiServerOption struct {
|
||
Filers []pb.ServerAddress
|
||
HomeFiler pb.ServerAddress // filer of the embedding process, preferred whenever healthy
|
||
Masters []pb.ServerAddress // For filer discovery
|
||
Port int
|
||
Config string
|
||
DomainName string
|
||
AllowedOrigins []string
|
||
BucketsPath string
|
||
GrpcDialOption grpc.DialOption
|
||
AllowDeleteBucketNotEmpty bool
|
||
AutoCreateBucket bool // create the bucket on upload if it does not exist
|
||
LocalFilerSocket string
|
||
DataCenter string
|
||
FilerGroup string
|
||
IamConfig string // Advanced IAM configuration file path
|
||
ConcurrentUploadLimit int64
|
||
ConcurrentFileUploadLimit int64
|
||
InflightUploadDataTimeout time.Duration
|
||
EnableIam bool // Enable embedded IAM API on the same port
|
||
IamReadOnly bool // Disable IAM write operations on this server
|
||
Cipher bool // encrypt data on volume servers
|
||
Ip string // address advertised to the cluster; empty falls back to BindIp
|
||
BindIp string
|
||
GrpcPort int
|
||
ExternalUrl string // external URL clients use, tried first during signature verification behind a reverse proxy
|
||
DefaultFileMode uint32 // default file permission mode for S3 uploads (e.g. 0660, 0644)
|
||
CacheSizeMB int64 // in-memory chunk cache capacity in MB for the shared ReaderCache; 0 disables
|
||
ReaderCacheSizeMB int64 // memory budget in MiB for downloaded and in-flight reader buffers across all S3 GETs; 0 means unlimited
|
||
MaxMB int32 // filer's -maxMB, read from the filer configuration at startup
|
||
// AllowUntrustedRemoteEndpoints lets a read of a remote-only object dial a
|
||
// mounted endpoint that resolves to a loopback / private / metadata host.
|
||
AllowUntrustedRemoteEndpoints bool
|
||
// AllowBackslashInKey keeps '\' a literal object-key character instead of
|
||
// normalizing it to '/', matching AWS S3 where the two are distinct keys.
|
||
AllowBackslashInKey bool
|
||
// LancePort is advertised to the master so admin can point workers at the
|
||
// Lance Namespace; 0 when this gateway serves none.
|
||
LancePort uint32
|
||
// S3Endpoint is advertised to the master so mounts can read SSE-S3 objects
|
||
// through this gateway.
|
||
S3Endpoint string
|
||
// IamSelfServiceRequiresGrant makes managing one's own access keys
|
||
// (CreateAccessKey, DeleteAccessKey, UpdateAccessKey, ListAccessKeys,
|
||
// GetUser) need an explicit iam: grant instead of being allowed to every
|
||
// authenticated identity.
|
||
IamSelfServiceRequiresGrant bool
|
||
// RoutedLookupTimeout bounds a route-by-key entry lookup at a key owner
|
||
// that is not the current filer; past it the read fails over to the next
|
||
// filer. 0 leaves the lookup unbounded.
|
||
RoutedLookupTimeout time.Duration
|
||
// UnreachableOwnerTTL is how long a key owner whose read just failed is
|
||
// skipped by route-by-key reads. 0 uses defaultUnreachableOwnerTTL.
|
||
UnreachableOwnerTTL time.Duration
|
||
// VolumeLookupTimeout bounds each filer's answer to a volume location
|
||
// lookup before the next filer is asked. 0 uses the FilerClient default.
|
||
VolumeLookupTimeout time.Duration
|
||
// MqBroker is the message queue broker running beside this gateway, the
|
||
// first one bucket events are published through; empty when none runs.
|
||
MqBroker pb.ServerAddress
|
||
}
|
||
|
||
// s3ChunkCacheChunkSizeMB is the assumed chunk size (in MiB) used to convert
|
||
// CacheSizeMB into the entry count the in-memory cache accepts. This matches
|
||
// the default -filer.maxMB for all filer/webdav/mini flag sites. It is NOT a
|
||
// hard limit — larger chunks still get cached, this just means the byte budget
|
||
// is approximate when upload-side chunking is configured larger.
|
||
const s3ChunkCacheChunkSizeMB = 4
|
||
|
||
type S3ApiServer struct {
|
||
s3_pb.UnimplementedSeaweedS3IamCacheServer
|
||
s3_lifecycle_pb.UnimplementedSeaweedS3LifecycleInternalServer
|
||
option *S3ApiServerOption
|
||
iam *IdentityAccessManagement
|
||
iamIntegration *S3IAMIntegration // Advanced IAM integration for JWT authentication
|
||
cb *CircuitBreaker
|
||
randomClientId int32
|
||
filerGuard *security.Guard
|
||
filerClient *wdclient.FilerClient
|
||
masterClient *wdclient.MasterClient
|
||
client util_http_client.HTTPClientInterface
|
||
bucketRegistry *BucketRegistry
|
||
credentialManager *credential.CredentialManager
|
||
bucketConfigCache *BucketConfigCache
|
||
policyEngine *BucketPolicyEngine // Engine for evaluating bucket policies
|
||
inFlightDataSize int64
|
||
inFlightUploads int64
|
||
inFlightDataLimitCond *sync.Cond
|
||
draining atomic.Bool
|
||
embeddedIam *EmbeddedIamApi // Embedded IAM API server (when enabled)
|
||
stsHandlers *STSHandlers // STS HTTP handlers for AssumeRoleWithWebIdentity
|
||
// icebergCredentialRole is the role the Iceberg catalog assumes to vend
|
||
// table-scoped credentials; empty leaves vending off.
|
||
icebergCredentialRole string
|
||
icebergCredentialDuration int64
|
||
// icebergCredentialMinDuration is the floor a vend that asks for a shorter
|
||
// session is raised to; 0 means STS's default floor (900 s).
|
||
icebergCredentialMinDuration int64
|
||
cipher bool // encrypt data on volume servers
|
||
newObjectWriteLock func(bucket, object string) (objectWriteLock, error)
|
||
// objectWriteLockClient resolves a key's owner filer for route-by-key.
|
||
objectWriteLockClient *cluster.LockClient
|
||
// unreachableOwners holds owners (pb.ServerAddress -> expiry time.Time) whose
|
||
// last owner-first read hit a transport error, so route-by-key reads briefly
|
||
// skip them instead of re-dialing a dead owner every request until the ring
|
||
// drops it. Bypasses the gateway's filer health tracking, which no-ops for an
|
||
// owner outside the static -filer list.
|
||
unreachableOwners sync.Map
|
||
// Shared ReaderCache used by the S3 GET streaming path. It lives for the
|
||
// lifetime of the server so that concurrent and repeat reads share a
|
||
// single in-flight download per chunk, and so that no per-request
|
||
// teardown waits on context.Background() fetches. The chunkCache field
|
||
// is nil in this commit; a follow-up wires in an in-memory chunk cache.
|
||
readerCache *filer.ReaderCache
|
||
|
||
// ownerIndexReady caches the presence of the owner index backfill marker.
|
||
ownerIndexReady atomic.Bool
|
||
|
||
versionsHealQueue *versionsHealQueue
|
||
versionsReconcilerStop func()
|
||
|
||
notificationTargets atomic.Pointer[s3notification.Registry]
|
||
notificationPublisher notificationPublisher
|
||
notificationForwarder notificationForwarder
|
||
}
|
||
|
||
type objectWriteLock interface {
|
||
StopShortLivedLock() error
|
||
}
|
||
|
||
const (
|
||
objectWriteLockTTL = 15 * time.Second
|
||
)
|
||
|
||
func NewS3ApiServer(router *mux.Router, option *S3ApiServerOption) (s3ApiServer *S3ApiServer, err error) {
|
||
return NewS3ApiServerWithStore(router, option, "")
|
||
}
|
||
|
||
// AdvertisedHost is the address this server registers with the master, which is
|
||
// how peers reach it — IAM changes are pushed to it over gRPC. It must be the
|
||
// advertised -ip, not the bind address: binding 0.0.0.0 and registering the
|
||
// auto-detected interface makes those pushes dial a host that may not route
|
||
// back here at all (a VPN address, a container-internal IP), and the push then
|
||
// fails silently after a 10s deadline.
|
||
func (option *S3ApiServerOption) AdvertisedHost() string {
|
||
if option.Ip != "" && !isWildcardHost(option.Ip) {
|
||
return option.Ip
|
||
}
|
||
if option.BindIp != "" && !isWildcardHost(option.BindIp) {
|
||
return option.BindIp
|
||
}
|
||
return util.DetectedHostAddress()
|
||
}
|
||
|
||
// isWildcardHost reports whether host is an unspecified address (0.0.0.0, ::,
|
||
// [::]) — one that accepts connections but tells a peer nothing about where to
|
||
// reach us. Host names parse as nil and are addresses in their own right.
|
||
func isWildcardHost(host string) bool {
|
||
ip := net.ParseIP(strings.TrimSuffix(strings.TrimPrefix(host, "["), "]"))
|
||
return ip != nil && ip.IsUnspecified()
|
||
}
|
||
|
||
func NewS3ApiServerWithStore(router *mux.Router, option *S3ApiServerOption, explicitStore string) (s3ApiServer *S3ApiServer, err error) {
|
||
if len(option.Filers) == 0 {
|
||
return nil, fmt.Errorf("at least one filer address is required")
|
||
}
|
||
|
||
startTsNs := time.Now().UnixNano()
|
||
|
||
v := util.GetViper()
|
||
signingKey := v.GetString("jwt.filer_signing.key")
|
||
v.SetDefault("jwt.filer_signing.expires_after_seconds", 10)
|
||
expiresAfterSec := v.GetInt("jwt.filer_signing.expires_after_seconds")
|
||
|
||
readSigningKey := v.GetString("jwt.filer_signing.read.key")
|
||
v.SetDefault("jwt.filer_signing.read.expires_after_seconds", 60)
|
||
readExpiresAfterSec := v.GetInt("jwt.filer_signing.read.expires_after_seconds")
|
||
|
||
v.SetDefault("cors.allowed_origins.values", "*")
|
||
|
||
if len(option.AllowedOrigins) == 0 {
|
||
allowedOrigins := v.GetString("cors.allowed_origins.values")
|
||
domains := strings.Split(allowedOrigins, ",")
|
||
option.AllowedOrigins = domains
|
||
}
|
||
|
||
// Initialize basic/legacy IAM - filerClient not available yet, passed as nil
|
||
iam := NewIdentityAccessManagementWithStore(option, nil, explicitStore)
|
||
|
||
// Initialize bucket policy engine first
|
||
policyEngine := NewBucketPolicyEngine()
|
||
|
||
// Initialize FilerClient for volume location caching
|
||
// Uses the battle-tested vidMap with filer-based lookups
|
||
// Supports multiple filer addresses with automatic failover for high availability
|
||
var filerClient *wdclient.FilerClient
|
||
var masterClient *wdclient.MasterClient
|
||
var objectWriteLockClient *cluster.LockClient
|
||
if len(option.Masters) > 0 {
|
||
// Enable filer discovery via master
|
||
masterMap := make(map[string]pb.ServerAddress)
|
||
for i, addr := range option.Masters {
|
||
masterMap[fmt.Sprintf("master%d", i)] = addr
|
||
}
|
||
clientHost := option.AdvertisedHost()
|
||
masterClient = wdclient.NewMasterClient(option.GrpcDialOption, option.FilerGroup, cluster.S3Type, pb.ServerAddress(util.JoinHostPort(clientHost, option.GrpcPort)), option.DataCenter, "", *pb.NewServiceDiscoveryFromMap(masterMap))
|
||
// Build the object-write lock client and subscribe to the master's
|
||
// lock-ring updates BEFORE starting the master loop, so the initial
|
||
// LockRingUpdate sent on connect isn't dropped (the master only delivers
|
||
// it once per connect). The masterClient already filters updates to this
|
||
// server's filer group.
|
||
if len(option.Filers) > 0 {
|
||
objectWriteLockClient = cluster.NewLockClient(option.GrpcDialOption, option.Filers[0])
|
||
objectWriteLockClient.TokenSource = security.FilerAdminToken
|
||
masterClient.SetOnLockRingUpdateFn(func(update *master_pb.LockRingUpdate) {
|
||
servers := make([]pb.ServerAddress, 0, len(update.Servers))
|
||
for _, s := range update.Servers {
|
||
servers = append(servers, pb.ServerAddress(s))
|
||
}
|
||
objectWriteLockClient.SetRing(servers, update.Version)
|
||
})
|
||
masterClient.SetOnMasterChangeFn(func(previous, current pb.ServerAddress) {
|
||
objectWriteLockClient.ResetRing()
|
||
})
|
||
}
|
||
filerClient = wdclient.NewFilerClient(option.Filers, option.GrpcDialOption, option.DataCenter, &wdclient.FilerClientOption{
|
||
MasterClient: masterClient,
|
||
FilerGroup: option.FilerGroup,
|
||
DiscoveryInterval: 5 * time.Minute,
|
||
HomeFiler: option.HomeFiler,
|
||
GrpcTimeout: option.VolumeLookupTimeout,
|
||
FilerTokenSource: security.FilerAdminToken,
|
||
})
|
||
masterClient.SetOnPeerUpdateFn(filerClient.OnPeerUpdate)
|
||
masterClient.SetOnVolumeLocationChangeFn(filerClient.OnVolumeLocationChange)
|
||
masterClient.SetOnVidMapResetFn(filerClient.OnVidMapReset)
|
||
masterClient.SetLancePort(option.LancePort)
|
||
masterClient.SetS3Endpoint(option.S3Endpoint)
|
||
|
||
// Start the master client connection loop - required for GetMaster() to work
|
||
go masterClient.KeepConnectedToMaster(context.Background())
|
||
|
||
glog.V(1).Infof("S3 API initialized FilerClient with %d filer(s) and discovery enabled (group: %s, masters: %v)",
|
||
len(option.Filers), option.FilerGroup, option.Masters)
|
||
} else {
|
||
filerClient = wdclient.NewFilerClient(option.Filers, option.GrpcDialOption, option.DataCenter, &wdclient.FilerClientOption{
|
||
HomeFiler: option.HomeFiler,
|
||
GrpcTimeout: option.VolumeLookupTimeout,
|
||
FilerTokenSource: security.FilerAdminToken,
|
||
})
|
||
glog.V(1).Infof("S3 API initialized FilerClient with %d filer(s) (no discovery)", len(option.Filers))
|
||
}
|
||
|
||
// Initialize Global SSE-S3 Key Manager early so it's available for IAM fallback
|
||
// This ensures we can access the KEK for STS signing key if needed
|
||
if err := InitializeGlobalSSES3KeyManager(filerClient, option.GrpcDialOption); err != nil {
|
||
glog.Errorf("Failed to initialize SSE-S3 Key Manager: %v", err)
|
||
// We continue, as this might be a transient failure or non-critical for some setups,
|
||
// but IAM fallback to KEK will fail if this didn't succeed.
|
||
}
|
||
|
||
// Update credential store to use FilerClient's current filer for HA
|
||
iam.SetFilerClient(filerClient)
|
||
|
||
// Fan IAM mutations out to peer S3 servers, mirroring the filer-embedded path.
|
||
iamPropagationEnabled := masterClient != nil && iam.credentialManager != nil
|
||
if iamPropagationEnabled {
|
||
iam.credentialManager.SetMasterClient(masterClient, option.GrpcDialOption)
|
||
}
|
||
|
||
// Keep attempting to load configuration from filer now that we have a client
|
||
// The initial load in NewIdentityAccessManagementWithStore might have failed if client was nil
|
||
go func() {
|
||
if err := iam.loadS3ApiConfigurationFromFiler(option); err != nil {
|
||
glog.Warningf("Failed to load IAM config from filer after client update: %v", err)
|
||
}
|
||
}()
|
||
|
||
// Shared ReaderCache for the S3 GET streaming path. Keeping this shared
|
||
// (rather than per-request) avoids the per-request Close(), which would
|
||
// otherwise wait for background chunk downloads that run on
|
||
// context.Background() even after the client disconnects.
|
||
//
|
||
// The underlying ChunkCache is controlled by option.CacheSizeMB below:
|
||
// - CacheSizeMB == 0: a nil *chunk_cache.TieredChunkCache is used (its
|
||
// receiver methods are nil-safe). Completed chunks are not deposited
|
||
// into a cross-request cache — concurrent readers still share in-flight
|
||
// downloads through the ReaderCache's downloaders map, but repeat reads
|
||
// refetch from volume servers.
|
||
// - CacheSizeMB > 0: a chunk_cache.ChunkCacheInMemory is created and
|
||
// wrapped in the ReaderCache, so repeat and concurrent reads hit
|
||
// memory. maxEntries is approximated from the byte budget and the
|
||
// assumed chunk size (s3ChunkCacheChunkSizeMB), clamped to a small
|
||
// floor so tiny caches still function.
|
||
//
|
||
// Downloader slots: each slot holds one in-flight or not-yet-consumed
|
||
// chunk buffer (~4 MiB by default), so this caps both peak memory for
|
||
// in-flight chunks (s3ReaderCacheDownloaderLimit × chunkSize) and the
|
||
// global fetch concurrency across all S3 GET requests. WebDAV uses 32
|
||
// because it typically has a handful of clients; S3 serves many
|
||
// concurrent readers, so we pick a more generous default here.
|
||
const s3ReaderCacheDownloaderLimit = 256
|
||
|
||
// Negative CacheSizeMB is a misconfiguration; fail fast rather than
|
||
// silently behaving like 0.
|
||
if option.CacheSizeMB < 0 {
|
||
return nil, fmt.Errorf("invalid -s3.cacheCapacityMB %d: must be >= 0", option.CacheSizeMB)
|
||
}
|
||
var chunkCache chunk_cache.ChunkCache
|
||
if option.CacheSizeMB > 0 {
|
||
// ccache sizes entries by count; convert the configured byte budget
|
||
// via the assumed chunk size. Clamp to a floor so tiny caches still
|
||
// function.
|
||
maxEntries := option.CacheSizeMB / s3ChunkCacheChunkSizeMB
|
||
if maxEntries < 8 {
|
||
maxEntries = 8
|
||
}
|
||
chunkCache = chunk_cache.NewChunkCacheInMemory(maxEntries)
|
||
// Log the effective capacity after the floor clamp, not the configured
|
||
// value — a user passing `-s3.cacheCapacityMB=1` actually gets 8 entries
|
||
// ≈ 32 MiB because of the floor.
|
||
glog.V(0).Infof("s3 chunk cache enabled: in-memory, ~%dMB (%d chunks of ~%dMB)", maxEntries*s3ChunkCacheChunkSizeMB, maxEntries, s3ChunkCacheChunkSizeMB)
|
||
} else {
|
||
chunkCache = (*chunk_cache.TieredChunkCache)(nil)
|
||
}
|
||
if option.ReaderCacheSizeMB < 0 || option.ReaderCacheSizeMB > math.MaxInt64>>20 {
|
||
return nil, fmt.Errorf("invalid readerCacheSizeMB %d: must be non-negative and fit in an int64 byte budget", option.ReaderCacheSizeMB)
|
||
}
|
||
var readerCacheBudget *filer.ReaderCacheBudget
|
||
if option.ReaderCacheSizeMB > 0 {
|
||
readerCacheBudget = filer.NewReaderCacheBudget(option.ReaderCacheSizeMB << 20)
|
||
}
|
||
readerCache := filer.NewReaderCache(s3ReaderCacheDownloaderLimit, chunkCache, filerClient.GetLookupFileIdFunction(), filerClient, readerCacheBudget)
|
||
|
||
s3ApiServer = &S3ApiServer{
|
||
option: option,
|
||
iam: iam,
|
||
randomClientId: util.RandomInt32(),
|
||
filerGuard: security.NewGuard([]string{}, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec, v.GetString("jwt.filer_signing.previous_key"), v.GetString("jwt.filer_signing.read.previous_key")),
|
||
filerClient: filerClient,
|
||
masterClient: masterClient,
|
||
cb: NewCircuitBreaker(option),
|
||
credentialManager: iam.credentialManager,
|
||
bucketConfigCache: NewBucketConfigCache(60 * time.Minute), // Increased TTL since cache is now event-driven
|
||
policyEngine: policyEngine, // Initialize bucket policy engine
|
||
inFlightDataLimitCond: sync.NewCond(new(sync.Mutex)),
|
||
cipher: option.Cipher,
|
||
readerCache: readerCache,
|
||
}
|
||
|
||
if len(option.Filers) > 0 {
|
||
// Reuse the lock client built in the masters block (already subscribed to
|
||
// ring updates); create a plain one when no masters are configured.
|
||
if objectWriteLockClient == nil {
|
||
objectWriteLockClient = cluster.NewLockClient(option.GrpcDialOption, option.Filers[0])
|
||
objectWriteLockClient.TokenSource = security.FilerAdminToken
|
||
}
|
||
s3ApiServer.objectWriteLockClient = objectWriteLockClient
|
||
s3ApiServer.newObjectWriteLock = func(bucket, object string) (objectWriteLock, error) {
|
||
lockKey := s3ApiServer.objectRouteKey(bucket, object)
|
||
owner := fmt.Sprintf("s3api-%d", s3ApiServer.randomClientId)
|
||
lock := objectWriteLockClient.NewShortLivedLock(lockKey, owner)
|
||
if lock == nil {
|
||
return nil, fmt.Errorf("objectWriteLock: failed to acquire lock for %s", lockKey)
|
||
}
|
||
if err := lock.AttemptToLock(objectWriteLockTTL); err != nil {
|
||
// The initial acquisition already succeeded with the default short TTL.
|
||
// Renewal to a longer TTL is opportunistic to cover slower metadata paths.
|
||
glog.Warningf("objectWriteLock: failed to extend lock TTL for %s: %v", lockKey, err)
|
||
}
|
||
return lock, nil
|
||
}
|
||
}
|
||
|
||
// Set s3a reference in circuit breaker for upload limiting
|
||
s3ApiServer.cb.s3a = s3ApiServer
|
||
|
||
// Pass policy engine to IAM for bucket policy evaluation
|
||
// This avoids circular dependency by not passing the entire S3ApiServer
|
||
iam.policyEngine = policyEngine
|
||
|
||
// Give the policy engine a way to look up the SSE algorithm that was
|
||
// stored at CreateMultipartUpload time, so that UploadPart/UploadPartCopy
|
||
// policy conditions on s3:x-amz-server-side-encryption evaluate correctly.
|
||
policyEngine.MultipartSSELookup = s3ApiServer.getMultipartSSEAlgorithm
|
||
policyEngine.RestrictsPublicBuckets = s3ApiServer.restrictsPublicBuckets
|
||
|
||
// Advanced-IAM authorization evaluates the bucket-policy:<bucket> mirror
|
||
// before any handler runs, so the auth path has to be what triggers the
|
||
// lazy bucket load (and with it the mirror backfill): a grant carried
|
||
// only by a not-yet-mirrored policy would otherwise deny forever, and
|
||
// the denied request never reaches the handlers that load the bucket.
|
||
iam.primeBucketForIAM = func(bucket string) s3err.ErrorCode {
|
||
_, code := s3ApiServer.getBucketConfig(bucket)
|
||
return code
|
||
}
|
||
iam.objectEntryForPolicy = func(r *http.Request, bucket, object string) (map[string][]byte, bool) {
|
||
entry, err := s3ApiServer.resolveObjectEntry(bucket, object, r.URL.Query().Get("versionId"))
|
||
if err != nil || entry == nil {
|
||
return nil, false
|
||
}
|
||
return entry.Extended, true
|
||
}
|
||
// Through the bucket config cache (positive and negative entries), which
|
||
// the IAM path above has often just filled: a denied request costs no
|
||
// extra filer lookup when the answer is cached.
|
||
iam.bucketMissing = func(bucket string) bool {
|
||
_, errCode := s3ApiServer.getBucketConfig(bucket)
|
||
return errCode == s3err.ErrNoSuchBucket
|
||
}
|
||
|
||
// Initialize advanced IAM system if config is provided or explicitly enabled
|
||
if option.IamConfig != "" || option.EnableIam {
|
||
configSource := "defaults"
|
||
if option.IamConfig != "" {
|
||
configSource = option.IamConfig
|
||
}
|
||
glog.V(1).Infof("Loading advanced IAM configuration from: %s", configSource)
|
||
|
||
// Use FilerClient's GetCurrentFiler for HA-aware filer selection
|
||
iamManager, err := loadIAMManagerFromConfig(option.IamConfig, func() string {
|
||
return string(filerClient.GetCurrentFiler())
|
||
}, func() string {
|
||
return signingKey
|
||
})
|
||
if err != nil {
|
||
glog.Errorf("Failed to load IAM configuration: %v", err)
|
||
} else {
|
||
if s3ApiServer.iam.credentialManager != nil {
|
||
iamManager.SetUserStore(s3ApiServer.iam.credentialManager)
|
||
}
|
||
glog.V(1).Infof("IAM Manager loaded, creating integration")
|
||
// Create S3 IAM integration with the loaded IAM manager
|
||
// filerAddress not actually used, just for backward compatibility
|
||
s3iam := NewS3IAMIntegration(iamManager, "")
|
||
|
||
// Set IAM integration in server
|
||
s3ApiServer.iamIntegration = s3iam
|
||
|
||
// Set the integration in the traditional IAM for compatibility.
|
||
// SetIAMIntegration no longer auto-enables auth — see the function comment.
|
||
// Only force isAuthEnabled when the operator actually pointed us at an
|
||
// IAM config file. Without one, EnableIam is the implicit mini default
|
||
// and we must keep the "no credentials = allow all" startup behavior so
|
||
// `docker run seaweedfs` works out of the box (fixes #9557).
|
||
//
|
||
// SetIAMIntegration also flushes the policies already loaded from the
|
||
// credential store into this manager's engine. The earlier config
|
||
// loads (the synchronous one in NewIdentityAccessManagementWithStore
|
||
// and the async goroutine) may have run before iamIntegration was set,
|
||
// in which case their syncRuntimePoliciesToIAMManager call was a no-op
|
||
// and identities relying on policy_names would get AccessDenied.
|
||
iam.SetIAMIntegration(s3iam)
|
||
if option.IamConfig != "" {
|
||
iam.EnableAuthEnforcement()
|
||
}
|
||
|
||
// Initialize STS HTTP handlers for AssumeRoleWithWebIdentity endpoint
|
||
if stsService := iamManager.GetSTSService(); stsService != nil {
|
||
s3ApiServer.stsHandlers = NewSTSHandlers(stsService, iam)
|
||
glog.V(1).Infof("STS HTTP handlers initialized for AssumeRoleWithWebIdentity")
|
||
}
|
||
|
||
glog.V(1).Infof("Advanced IAM system initialized successfully with HA filer support")
|
||
}
|
||
}
|
||
|
||
s3ApiServer.applyTrustedProxies(util.GetViper())
|
||
|
||
// Initialize embedded IAM API if enabled
|
||
if option.EnableIam {
|
||
s3ApiServer.embeddedIam = NewEmbeddedIamApi(s3ApiServer.credentialManager, iam, option.IamReadOnly)
|
||
if option.IamReadOnly {
|
||
glog.V(1).Infof("Embedded IAM API initialized in read-only mode (use -s3.iam.readOnly=false to enable write operations)")
|
||
} else if iamPropagationEnabled {
|
||
glog.V(1).Infof("Embedded IAM API initialized in writable mode (updates propagate to other S3 servers)")
|
||
} else {
|
||
glog.Warningf("Embedded IAM API initialized in writable mode but no master is configured; updates will not be propagated to other S3 servers")
|
||
}
|
||
}
|
||
|
||
if option.Config != "" {
|
||
grace.OnReload(func() {
|
||
if err := s3ApiServer.iam.loadS3ApiConfigurationFromFile(option.Config); err != nil {
|
||
glog.Errorf("fail to load config file %s: %v", option.Config, err)
|
||
} else {
|
||
glog.V(1).Infof("Loaded %d identities from config file %s", len(s3ApiServer.iam.identities), option.Config)
|
||
}
|
||
})
|
||
}
|
||
|
||
// Refresh the JWT signing keys on SIGHUP so an operator can rotate them
|
||
// without restarting; otherwise filer/volume auth stays stuck on the stale
|
||
// key after a rotation.
|
||
grace.OnReload(func() {
|
||
util.LoadConfiguration("security", false)
|
||
v := util.GetViper()
|
||
s3ApiServer.filerGuard.ReloadSigningKeys("jwt.filer_signing")
|
||
s3ApiServer.applyTrustedProxies(v)
|
||
util_http.ReloadJwtSigningReadConfig()
|
||
})
|
||
s3ApiServer.bucketRegistry = NewBucketRegistry(s3ApiServer)
|
||
|
||
// Update IAM with the final filer client (already handled by SetFilerClient above,
|
||
// but this reinforces it if we ever change the flow)
|
||
s3ApiServer.iam.SetFilerClient(s3ApiServer.filerClient)
|
||
if option.LocalFilerSocket == "" {
|
||
if s3ApiServer.client, err = util_http.NewGlobalHttpClient(); err != nil {
|
||
return nil, err
|
||
}
|
||
} else {
|
||
s3ApiServer.client = &http.Client{
|
||
Transport: &http.Transport{
|
||
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
|
||
conn, err := (&net.Dialer{}).DialContext(ctx, "unix", option.LocalFilerSocket)
|
||
if err != nil {
|
||
return util.OutboundDialContext(ctx, "tcp", addr)
|
||
}
|
||
return conn, nil
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
s3ApiServer.registerRouter(router)
|
||
|
||
watched := []string{
|
||
option.BucketsPath,
|
||
filer.IamConfigDirectory,
|
||
filer.IamConfigDirectory + "/identities",
|
||
filer.IamConfigDirectory + "/policies",
|
||
filer.IamConfigDirectory + "/service_accounts",
|
||
filer.IamConfigDirectory + "/groups",
|
||
filer.IamConfigDirectory + "/oidc-providers",
|
||
filer.IamConfigDirectory + "/roles",
|
||
}
|
||
// A role store configured with its own basePath is watched there too.
|
||
if dir := s3ApiServer.roleStoreDir(); !slices.Contains(watched, dir) {
|
||
watched = append(watched, dir)
|
||
}
|
||
s3ApiServer.notificationPublisher = s3ApiServer.newNotificationPublisher()
|
||
s3ApiServer.notificationForwarder = s3ApiServer.newNotificationForwarder()
|
||
s3ApiServer.loadNotificationTargets()
|
||
go s3ApiServer.subscribeMetaEvents("s3", startTsNs, filer.DirectoryEtcRoot, watched)
|
||
|
||
// Start bucket size metrics collection in background
|
||
go s3ApiServer.startBucketSizeMetricsLoop(context.Background())
|
||
|
||
// Bring the bucket owner index up to date with pre-existing buckets
|
||
go s3ApiServer.startBucketOwnerIndexBackfill()
|
||
|
||
// Start the versioning reconciler that drains stranded .versions/
|
||
// pointer-to-missing-file states without waiting for a client GET.
|
||
s3ApiServer.versionsReconcilerStop = s3ApiServer.startVersioningReconciler()
|
||
|
||
// Publish the key mode only once the server is fully constructed so a
|
||
// failed construction cannot flip the mode of a running server.
|
||
SetAllowBackslashInObjectKey(option.AllowBackslashInKey)
|
||
|
||
return s3ApiServer, nil
|
||
}
|
||
|
||
// applyTrustedProxies reads [s3.trusted_proxies] from the security config and
|
||
// propagates the allowlist to the bucket policy engine, the IAM policy
|
||
// engine, and the IAM integration so aws:SourceIp honors forwarded headers
|
||
// only from configured trusted proxies.
|
||
func (s3a *S3ApiServer) applyTrustedProxies(v util.Configuration) {
|
||
whiteList := util.StringSplit(v.GetString("s3.trusted_proxies.white_list"), ",")
|
||
tp := policy_engine.NewTrustedProxies(whiteList)
|
||
if s3a.policyEngine != nil {
|
||
s3a.policyEngine.engine.SetTrustedProxies(tp)
|
||
}
|
||
s3a.iam.SetTrustedProxies(tp)
|
||
if s3a.iamIntegration != nil {
|
||
s3a.iamIntegration.SetTrustedProxies(tp)
|
||
}
|
||
}
|
||
|
||
func (s3a *S3ApiServer) Shutdown() {
|
||
if s3a.versionsReconcilerStop != nil {
|
||
s3a.versionsReconcilerStop()
|
||
}
|
||
if s3a.iam != nil {
|
||
s3a.iam.Shutdown()
|
||
}
|
||
if s3a.notificationPublisher != nil {
|
||
s3a.notificationPublisher.Close()
|
||
}
|
||
if s3a.notificationForwarder != nil {
|
||
s3a.notificationForwarder.Close()
|
||
}
|
||
}
|
||
|
||
// getFilerAddress returns the current active filer address
|
||
// Uses FilerClient's tracked current filer which is updated on successful operations
|
||
// This provides better availability than always using the first filer
|
||
func (s3a *S3ApiServer) getFilerAddress() pb.ServerAddress {
|
||
if s3a.filerClient != nil {
|
||
return s3a.filerClient.GetCurrentFiler()
|
||
}
|
||
// Fallback to first filer if filerClient not initialized
|
||
if len(s3a.option.Filers) > 0 {
|
||
return s3a.option.Filers[0]
|
||
}
|
||
glog.Warningf("getFilerAddress: no filer addresses available")
|
||
return ""
|
||
}
|
||
|
||
// syncBucketPolicyToEngine syncs a bucket policy to the policy engine
|
||
// This helper method centralizes the logic for loading bucket policies into the engine
|
||
// to avoid duplication and ensure consistent error handling
|
||
func (s3a *S3ApiServer) syncBucketPolicyToEngine(bucket string, policyDoc *policy_engine.PolicyDocument) {
|
||
if s3a.policyEngine == nil {
|
||
return
|
||
}
|
||
|
||
if policyDoc != nil {
|
||
if err := s3a.policyEngine.LoadBucketPolicyFromCache(bucket, policyDoc); err != nil {
|
||
glog.Errorf("Failed to sync bucket policy for %s to policy engine: %v", bucket, err)
|
||
}
|
||
} else {
|
||
// No policy - ensure it's removed from engine if it was there
|
||
s3a.policyEngine.DeleteBucketPolicy(bucket)
|
||
}
|
||
}
|
||
|
||
// checkPolicyWithEntry re-evaluates bucket policy with the object entry metadata.
|
||
// This is used by handlers after fetching the entry to enforce tag-based conditions
|
||
// like s3:ExistingObjectTag/<key>.
|
||
//
|
||
// Returns:
|
||
// - s3err.ErrCode: ErrNone if allowed, ErrAccessDenied if denied
|
||
// - bool: true if policy was evaluated (has policy for bucket), false if no policy
|
||
func (s3a *S3ApiServer) checkPolicyWithEntry(r *http.Request, bucket, object, action, principal string, objectEntry map[string][]byte) (s3err.ErrorCode, bool) {
|
||
if s3a.policyEngine == nil {
|
||
return s3err.ErrNone, false
|
||
}
|
||
|
||
// Skip if no policy for this bucket
|
||
hasPolicy := s3a.policyEngine.HasPolicyForBucket(bucket)
|
||
// glog.V(4).Infof("checkPolicyWithEntry: bucket=%s hasPolicy=%v", bucket, hasPolicy)
|
||
if !hasPolicy {
|
||
return s3err.ErrNone, false
|
||
}
|
||
|
||
// Upload handler rechecks use the same effective ACL conditions as authentication without changing the signed request.
|
||
policyRequest, policyCode := putObjectACLPolicyRequest(r, Action(action), bucket, object)
|
||
if policyCode != s3err.ErrNone {
|
||
return policyCode, true
|
||
}
|
||
r = policyRequest
|
||
|
||
identityRaw := GetIdentityFromContext(r)
|
||
var identity *Identity
|
||
if identityRaw != nil {
|
||
if id, ok := identityRaw.(*Identity); ok {
|
||
identity = id
|
||
}
|
||
}
|
||
|
||
var claims map[string]interface{}
|
||
if identity != nil {
|
||
claims = identity.Claims
|
||
}
|
||
|
||
if principal == "" {
|
||
principal = buildPrincipalARN(identity, r)
|
||
}
|
||
|
||
allowed, evaluated, err := s3a.policyEngine.EvaluatePolicy(bucket, object, action, principal, r, claims, objectEntry)
|
||
if err != nil {
|
||
glog.Errorf("checkPolicyWithEntry: error evaluating policy for %s/%s: %v", bucket, object, err)
|
||
return s3err.ErrInternalError, true
|
||
}
|
||
|
||
if !evaluated {
|
||
return s3err.ErrNone, false
|
||
}
|
||
|
||
if !allowed {
|
||
glog.V(3).Infof("checkPolicyWithEntry: policy denied access to %s/%s for principal %s", bucket, object, principal)
|
||
return s3err.ErrAccessDenied, true
|
||
}
|
||
|
||
return s3err.ErrNone, true
|
||
}
|
||
|
||
// recheckPolicyWithObjectEntry performs the second phase of policy evaluation after
|
||
// an object's entry is fetched. It extracts identity from context and checks for
|
||
// tag-based conditions like s3:ExistingObjectTag/<key>.
|
||
//
|
||
// Returns s3err.ErrNone if allowed, or an error code if denied or on error.
|
||
func (s3a *S3ApiServer) recheckPolicyWithObjectEntry(r *http.Request, bucket, object, action string, objectEntry map[string][]byte, handlerName string) s3err.ErrorCode {
|
||
recordServedObjectEntry(r, bucket, object, objectEntry)
|
||
if isAnonymousObjectRead(r) {
|
||
// Refined like writeAnonymousAwareNotFound's masked miss, so a denied
|
||
// existing key and a missing key answer alike on a malformed name.
|
||
identity, _ := GetIdentityFromContext(r).(*Identity)
|
||
return s3a.iam.refineDenialForBucketName(r, identity, s3a.authorizeAnonymousObjectRead(r, bucket, object, objectEntry))
|
||
}
|
||
if s3a.isVersionFileKey(object, objectEntry) {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
identityRaw := GetIdentityFromContext(r)
|
||
var identity *Identity
|
||
if identityRaw != nil {
|
||
var ok bool
|
||
identity, ok = identityRaw.(*Identity)
|
||
if !ok {
|
||
glog.Errorf("%s: unexpected identity type in context for %s/%s", handlerName, bucket, object)
|
||
return s3err.ErrInternalError
|
||
}
|
||
}
|
||
principal := buildPrincipalARN(identity, r)
|
||
errCode, _ := s3a.checkPolicyWithEntry(r, bucket, object, action, principal, objectEntry)
|
||
return errCode
|
||
}
|
||
|
||
// checkVersionReadGrant enforces the version read grant AWS requires alongside
|
||
// a version-scoped action such as s3:GetObjectVersionAttributes. The check runs
|
||
// on a request shaped like a plain versioned GET so the s3:GetObjectVersion
|
||
// action resolves from the request instead of the caller's subresource, and a
|
||
// legacy Read grant still applies.
|
||
func (s3a *S3ApiServer) checkVersionReadGrant(r *http.Request, bucket, object, versionId string, objectEntry map[string][]byte) s3err.ErrorCode {
|
||
versionReadReq := objectKeyPolicyRequest(r, http.MethodGet, bucket, object, versionId)
|
||
if identity, ok := GetIdentityFromContext(r).(*Identity); ok && identity != nil {
|
||
if errCode := s3a.iam.VerifyActionPermission(versionReadReq, identity, ACTION_READ, bucket, object); errCode != s3err.ErrNone {
|
||
return errCode
|
||
}
|
||
}
|
||
return s3a.recheckPolicyWithObjectEntry(versionReadReq, bucket, object, ACTION_READ, objectEntry, "checkVersionReadGrant")
|
||
}
|
||
|
||
// recheckCopySourcePolicy repeats the source object's policy check once its
|
||
// entry is fetched, so conditions on the object's existing tags apply to a
|
||
// CopyObject / UploadPartCopy / RenameObject source the same way they do to a
|
||
// plain GET. The synthesized GET keeps the destination's request shape (PUT,
|
||
// partNumber, ...) from resolving a different action.
|
||
func (s3a *S3ApiServer) recheckCopySourcePolicy(r *http.Request, bucket, object, versionId string, objectEntry map[string][]byte, handlerName string) s3err.ErrorCode {
|
||
keyReq := objectKeyPolicyRequest(r, http.MethodGet, bucket, object, versionId)
|
||
return s3a.recheckPolicyWithObjectEntry(keyReq, bucket, object, string(ACTION_READ), objectEntry, handlerName)
|
||
}
|
||
|
||
// recheckTagConditionedPolicyWithLookup repeats policy evaluation with the
|
||
// object's entry in hand so s3:ExistingObjectTag conditions apply to
|
||
// operations that did not otherwise fetch the entry. It only does the extra
|
||
// lookup when the bucket has a policy or the key may name a version file;
|
||
// entry misses return ErrNone because the handler reports its own NoSuchKey.
|
||
func (s3a *S3ApiServer) recheckTagConditionedPolicyWithLookup(r *http.Request, bucket, object, versionId, action, handlerName string) s3err.ErrorCode {
|
||
if (s3a.policyEngine == nil || !s3a.policyEngine.HasPolicyForBucket(bucket)) && !s3a.hasVersionFileShape(object) {
|
||
return s3err.ErrNone
|
||
}
|
||
entry, err := s3a.resolveObjectEntry(bucket, object, versionId)
|
||
if err != nil {
|
||
// The tag conditions cannot be evaluated without the entry; failing
|
||
// open would let a tag-gated Deny slip through a transient miss.
|
||
if errors.Is(err, filer_pb.ErrNotFound) || status.Code(err) == codes.NotFound {
|
||
return s3err.ErrNone
|
||
}
|
||
glog.Errorf("%s: recheck lookup for %s/%s failed: %v", handlerName, bucket, object, err)
|
||
return s3err.ErrInternalError
|
||
}
|
||
if entry == nil {
|
||
return s3err.ErrNone
|
||
}
|
||
return s3a.recheckPolicyWithObjectEntry(r, bucket, object, action, entry.Extended, handlerName)
|
||
}
|
||
|
||
// checkVersionFileWrite refuses a write whose key is a stored version's
|
||
// physical path, which would replace that version's content in place. The
|
||
// physical entry is read on the logical object's owner, where versions commit.
|
||
func (s3a *S3ApiServer) checkVersionFileWrite(bucket, object, handlerName string) s3err.ErrorCode {
|
||
if !s3a.hasVersionFileShape(object) {
|
||
return s3err.ErrNone
|
||
}
|
||
key := strings.TrimSuffix(NormalizeObjectKey(object), "/")
|
||
dir, name := util.NewFullPath(s3a.bucketDir(bucket), key).DirAndName()
|
||
logicalObject := strings.TrimSuffix(strings.TrimSuffix(key, "/"+name), VersionsFolder)
|
||
entry, err := s3a.getObjectScopedEntryRoutedByKey(bucket, logicalObject, dir, name)
|
||
if errors.Is(err, filer_pb.ErrNotFound) || status.Code(err) == codes.NotFound {
|
||
// The routed lookup may have answered from a replica, whose miss proves nothing.
|
||
if owner := s3a.routableWriteOwner(bucket, logicalObject); owner != "" {
|
||
entry, err = s3a.lookupEntryOnFiler(context.Background(), owner, dir, name)
|
||
}
|
||
}
|
||
if err != nil {
|
||
if errors.Is(err, filer_pb.ErrNotFound) || status.Code(err) == codes.NotFound {
|
||
return s3err.ErrNone
|
||
}
|
||
glog.Errorf("%s: version file lookup for %s/%s failed: %v", handlerName, bucket, object, err)
|
||
return s3err.ErrInternalError
|
||
}
|
||
if entry != nil && s3a.isVersionFileKey(object, entry.Extended) {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
return s3err.ErrNone
|
||
}
|
||
|
||
// recheckPolicyForKeyWithLookup re-decides a first-phase deny on one key for a
|
||
// synthesized method, resolving the entry the way the read path does (versions
|
||
// included) so s3:ExistingObjectTag conditions see the object's real tags.
|
||
// Unlike the deny-side rechecks, an indeterminate result does not grant: the
|
||
// caller reports the original verdict either way, so only a statement that
|
||
// matches with tags in hand can turn a deny into an allow, and a missed lookup
|
||
// never widens access or reveals whether the key exists.
|
||
func (s3a *S3ApiServer) recheckPolicyForKeyWithLookup(r *http.Request, method, bucket, object, versionId, action, handlerName string) s3err.ErrorCode {
|
||
if s3a.policyEngine == nil || !s3a.policyEngine.HasPolicyForBucket(bucket) {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
versioningState, err := s3a.getVersioningState(bucket)
|
||
if err != nil {
|
||
glog.Errorf("%s: recheck versioning state for %s: %v", handlerName, bucket, err)
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
entry, err := s3a.resolveCopySourceEntry(bucket, object, versionId, versioningState)
|
||
entry = prefixObjectSource(entry)
|
||
if err != nil || entry == nil {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
keyReq := objectKeyPolicyRequest(r, method, bucket, object, versionId)
|
||
errCode, checked := s3a.checkPolicyWithEntry(keyReq, bucket, object, action, "", entry.Extended)
|
||
if errCode != s3err.ErrNone || !checked {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
// A bucket-policy Allow never overrides an explicit Deny in the caller's
|
||
// own attached or session policies — the first phase applied that veto,
|
||
// so the tag-aware recheck must too.
|
||
identity, _ := GetIdentityFromContext(r).(*Identity)
|
||
if s3a.iam != nil && s3a.iam.isActionExplicitlyDeniedByApplicablePolicies(keyReq, identity, Action(action), bucket, object) {
|
||
return s3err.ErrAccessDenied
|
||
}
|
||
return s3err.ErrNone
|
||
}
|
||
|
||
// classifyDomainNames classifies domains into path-style and virtual-host style domains.
|
||
// A domain is considered path-style if:
|
||
// 1. It contains a dot (has subdomains)
|
||
// 2. Its parent domain is also in the list of configured domains
|
||
//
|
||
// For example, if domains are ["s3.example.com", "develop.s3.example.com"],
|
||
// then "develop.s3.example.com" is path-style (parent "s3.example.com" is in the list),
|
||
// while "s3.example.com" is virtual-host style.
|
||
func classifyDomainNames(domainNames []string) (pathStyleDomains, virtualHostDomains []string) {
|
||
for _, domainName := range domainNames {
|
||
parts := strings.SplitN(domainName, ".", 2)
|
||
if len(parts) == 2 && slices.Contains(domainNames, parts[1]) {
|
||
// This is a subdomain and its parent is also in the list
|
||
// Register as path-style: domain.com/bucket/object
|
||
pathStyleDomains = append(pathStyleDomains, domainName)
|
||
} else {
|
||
// This is a top-level domain or its parent is not in the list
|
||
// Register as virtual-host style: bucket.domain.com/object
|
||
virtualHostDomains = append(virtualHostDomains, domainName)
|
||
}
|
||
}
|
||
return pathStyleDomains, virtualHostDomains
|
||
}
|
||
|
||
// handleCORSOriginValidation handles the common CORS origin validation logic
|
||
func (s3a *S3ApiServer) handleCORSOriginValidation(w http.ResponseWriter, r *http.Request) bool {
|
||
origin := r.Header.Get("Origin")
|
||
if origin != "" {
|
||
if len(s3a.option.AllowedOrigins) == 0 || s3a.option.AllowedOrigins[0] == "*" {
|
||
origin = "*"
|
||
} else {
|
||
originFound := false
|
||
for _, allowedOrigin := range s3a.option.AllowedOrigins {
|
||
if origin == allowedOrigin {
|
||
originFound = true
|
||
break
|
||
}
|
||
}
|
||
if !originFound {
|
||
writeFailureResponse(w, r, http.StatusForbidden)
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
|
||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||
w.Header().Set("Access-Control-Expose-Headers", "*")
|
||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||
return true
|
||
}
|
||
|
||
// UnifiedPostHandler handles authenticated POST requests to the root path
|
||
// It inspects the Action parameter to dispatch to either STS or IAM handlers
|
||
func (s3a *S3ApiServer) UnifiedPostHandler(w http.ResponseWriter, r *http.Request) {
|
||
// Failures on this route answer in the IAM/STS query-protocol envelope
|
||
// unless the request was actually signed for s3.
|
||
r, reqID := request_id.Ensure(r)
|
||
writeQueryError := func(iamCode string, s3Code s3err.ErrorCode) {
|
||
if requestSigningService(r) == "s3" {
|
||
s3err.WriteErrorResponse(w, r, s3Code)
|
||
return
|
||
}
|
||
writeIamErrorResponse(w, r, reqID, &iamError{Code: iamCode, Error: errors.New(s3err.GetAPIError(s3Code).Description)})
|
||
}
|
||
|
||
// 1. Authenticate (preserves body)
|
||
identity, errCode := s3a.iam.AuthSignatureOnly(r)
|
||
if errCode != s3err.ErrNone {
|
||
writeQueryError(iamAuthErrorCode(errCode), errCode)
|
||
return
|
||
}
|
||
|
||
// 2. Parse Form to get Action
|
||
// Save the body first so we can restore it for STS handler signature verification
|
||
var bodyBytes []byte
|
||
if r.Body != nil {
|
||
var err error
|
||
bodyBytes, err = readRequestBody(r, iamRequestBodyLimit)
|
||
if err != nil {
|
||
glog.Errorf("failed to read request body: %v", err)
|
||
writeQueryError("InvalidInput", s3err.ErrInvalidRequest)
|
||
return
|
||
}
|
||
r.Body.Close()
|
||
// Restore body for ParseForm
|
||
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||
}
|
||
|
||
if err := r.ParseForm(); err != nil {
|
||
writeQueryError("InvalidInput", s3err.ErrInvalidRequest)
|
||
return
|
||
}
|
||
|
||
// Restore body again for downstream handlers (STS needs it for signature verification)
|
||
if bodyBytes != nil {
|
||
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||
}
|
||
|
||
// 3. Dispatch
|
||
action := r.Form.Get("Action")
|
||
if strings.HasPrefix(action, "AssumeRole") || action == "GetCallerIdentity" || action == "GetFederationToken" {
|
||
// STS
|
||
if s3a.stsHandlers == nil {
|
||
s3err.WriteErrorResponse(w, r, s3err.ErrServiceUnavailable)
|
||
return
|
||
}
|
||
// AssumeRoleWithWebIdentity/WithLDAPIdentity carry no SigV4 caller, so
|
||
// identity may be nil here; the STS handlers record their own caller.
|
||
if identity != nil {
|
||
r = r.WithContext(recordIdentityInContext(r, identity))
|
||
}
|
||
s3a.stsHandlers.HandleSTSRequest(w, r)
|
||
} else {
|
||
// IAM
|
||
// IAM API requests must be authenticated - reject nil identity
|
||
if identity == nil {
|
||
writeQueryError("AccessDenied", s3err.ErrAccessDenied)
|
||
return
|
||
}
|
||
|
||
// Store identity in context
|
||
// Always set identity in context when non-nil to ensure downstream handlers have access
|
||
r = r.WithContext(recordIdentityInContext(r, identity))
|
||
|
||
// UserName comes from the body only, the same place DoActions reads it
|
||
// from, so the authorized target and the acted-on target cannot differ.
|
||
if s3a.iam.AuthorizeIamAction(r, identity, action, iamTargetUserName(action, r)) != s3err.ErrNone {
|
||
writeQueryError("AccessDenied", s3err.ErrAccessDenied)
|
||
return
|
||
}
|
||
|
||
// Call Limit middleware + DoActions
|
||
handler, _ := s3a.cb.Limit(s3a.embeddedIam.DoActions, ACTION_WRITE)
|
||
handler.ServeHTTP(w, r)
|
||
}
|
||
}
|
||
|
||
func (s3a *S3ApiServer) registerRouter(router *mux.Router) {
|
||
router.Use(util_http.EscapeSemicolonsInQuery)
|
||
// a request naming two subresources routes one way and authorizes another;
|
||
// reject it rather than guess
|
||
router.Use(rejectAmbiguousSubresource)
|
||
// API Router
|
||
apiRouter := router.PathPrefix("/").Subrouter()
|
||
apiRouter.Use(request_id.Middleware)
|
||
|
||
// S3 Tables API endpoint
|
||
// POST / with X-Amz-Target: S3Tables.<OperationName>
|
||
// plus REST-style endpoints for AWS CLI
|
||
s3a.registerS3TablesRoutes(apiRouter)
|
||
|
||
// Health probes
|
||
apiRouter.Methods(http.MethodGet, http.MethodHead).Path("/status").HandlerFunc(s3a.StatusHandler)
|
||
apiRouter.Methods(http.MethodGet, http.MethodHead).Path("/healthz").HandlerFunc(s3a.StatusHandler)
|
||
apiRouter.Methods(http.MethodGet, http.MethodHead).Path("/readyz").HandlerFunc(s3a.StatusHandler)
|
||
|
||
// Object path pattern with (?s) flag to match newlines in object keys
|
||
const objectPath = "/{object:(?s).+}"
|
||
|
||
// CORS preflight for service-level requests, answered from the global
|
||
// AllowedOrigins rather than a bucket's CORS configuration
|
||
serviceOptionsHandler := func(w http.ResponseWriter, r *http.Request) {
|
||
if s3a.handleCORSOriginValidation(w, r) {
|
||
writeSuccessResponseEmpty(w, r)
|
||
}
|
||
}
|
||
// A path-style bucket router would take the health probe paths as bucket
|
||
// names, so their preflight is registered ahead of each one
|
||
const healthProbePath = "/{probe:status|healthz|readyz}"
|
||
|
||
var routers []*mux.Router
|
||
if s3a.option.DomainName != "" {
|
||
domainNames := strings.Split(s3a.option.DomainName, ",")
|
||
pathStyleDomains, virtualHostDomains := classifyDomainNames(domainNames)
|
||
|
||
// Register path-style domains
|
||
for _, domain := range pathStyleDomains {
|
||
apiRouter.Host(domain).Methods(http.MethodOptions).Path(healthProbePath).HandlerFunc(serviceOptionsHandler)
|
||
routers = append(routers, apiRouter.Host(domain).PathPrefix("/{bucket}").Subrouter())
|
||
apiRouter.Host(domain).
|
||
Methods(http.MethodGet).
|
||
Path("/").
|
||
HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_LIST), "LIST"))
|
||
}
|
||
|
||
// Register virtual-host style domains
|
||
for _, virtualHost := range virtualHostDomains {
|
||
routers = append(routers, apiRouter.Host(
|
||
fmt.Sprintf("%s.%s", "{bucket:.+}", virtualHost)).Subrouter())
|
||
}
|
||
}
|
||
// Always register a Host-less path-style catch-all last so requests that
|
||
// arrive via an IP, an unlisted hostname, or a reverse proxy that rewrites
|
||
// the Host header still match bucket routes. Host-specific routers above
|
||
// take precedence because they were registered first.
|
||
apiRouter.Methods(http.MethodOptions).Path(healthProbePath).HandlerFunc(serviceOptionsHandler)
|
||
routers = append(routers, apiRouter.PathPrefix("/{bucket}").Subrouter())
|
||
|
||
// Get CORS middleware instance with caching
|
||
corsMiddleware := s3a.getCORSMiddleware()
|
||
|
||
for _, bucket := range routers {
|
||
// Reject `..`/`.`/NUL in {bucket} or {object} vars before any handler
|
||
// runs. SkipClean(true) keeps `..` in the matched path; the filer would
|
||
// otherwise collapse it via filepath.Join and cross bucket boundaries.
|
||
bucket.Use(validateRequestPath)
|
||
|
||
// Apply CORS middleware to bucket routers for automatic CORS header handling
|
||
bucket.Use(corsMiddleware.Handler)
|
||
|
||
// Bucket-specific OPTIONS handler for CORS preflight requests
|
||
// No path matcher, so it also catches the path-style bucket root (/bucket),
|
||
// which PathPrefix("/") would miss, as well as /bucket/ and /bucket/object
|
||
bucket.Methods(http.MethodOptions).HandlerFunc(corsMiddleware.HandleOptionsRequest)
|
||
|
||
// each case should follow the next rule:
|
||
// - requesting object with query must precede any other methods
|
||
// - requesting object must precede any methods with buckets
|
||
// - requesting bucket with query must precede raw methods with buckets
|
||
// - requesting bucket must be processed in the end
|
||
|
||
// objects with query
|
||
|
||
// CopyObjectPart
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HeadersRegexp("X-Amz-Copy-Source", `(?i).*?(\/|%2F).*?`).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.CopyObjectPartHandler, ACTION_WRITE)), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
|
||
// PutObjectPart
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectPartHandler, ACTION_WRITE)), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
|
||
// CompleteMultipartUpload
|
||
bucket.Methods(http.MethodPost).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.CompleteMultipartUploadHandler, ACTION_WRITE)), "POST")).Queries("uploadId", "{uploadId:.*}")
|
||
// NewMultipartUpload
|
||
bucket.Methods(http.MethodPost).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.NewMultipartUploadHandler, ACTION_WRITE)), "POST")).Queries("uploads", "")
|
||
// AbortMultipartUpload
|
||
bucket.Methods(http.MethodDelete).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.AbortMultipartUploadHandler, ACTION_WRITE)), "DELETE")).Queries("uploadId", "{uploadId:.*}")
|
||
// GetPart; partNumber is checked by the handler so a ?part request never falls through to ListParts
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetPartHandler, ACTION_READ)), "GET")).Queries("part", "", "uploadId", "{uploadId:.*}")
|
||
// ListObjectParts
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListObjectPartsHandler, ACTION_READ)), "GET")).Queries("uploadId", "{uploadId:.*}")
|
||
// ListMultipartUploads
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListMultipartUploadsHandler, ACTION_READ)), "GET")).Queries("uploads", "")
|
||
|
||
// GetObjectAttributes
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectAttributesHandler, ACTION_READ)), "GET")).Queries("attributes", "")
|
||
// GetObjectTagging
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectTaggingHandler, ACTION_READ)), "GET")).Queries("tagging", "")
|
||
// PutObjectTagging
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectTaggingHandler, ACTION_TAGGING)), "PUT")).Queries("tagging", "")
|
||
// DeleteObjectTagging
|
||
bucket.Methods(http.MethodDelete).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteObjectTaggingHandler, ACTION_TAGGING)), "DELETE")).Queries("tagging", "")
|
||
|
||
// RenameObject
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.RenameObjectHandler, ACTION_WRITE)), "PUT")).Queries("renameObject", "")
|
||
|
||
// PutObjectACL
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectAclHandler, ACTION_WRITE_ACP)), "PUT")).Queries("acl", "")
|
||
// PutObjectRetention
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectRetentionHandler, ACTION_PUT_OBJECT_RETENTION)), "PUT")).Queries("retention", "")
|
||
// PutObjectLegalHold
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectLegalHoldHandler, ACTION_PUT_OBJECT_LEGAL_HOLD)), "PUT")).Queries("legal-hold", "")
|
||
|
||
// GetObjectACL
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectAclHandler, ACTION_READ_ACP)), "GET")).Queries("acl", "")
|
||
// GetObjectRetention
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectRetentionHandler, ACTION_GET_OBJECT_RETENTION)), "GET")).Queries("retention", "")
|
||
// GetObjectLegalHold
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectLegalHoldHandler, ACTION_GET_OBJECT_LEGAL_HOLD)), "GET")).Queries("legal-hold", "")
|
||
|
||
// objects with query
|
||
|
||
// raw objects
|
||
|
||
// HeadObject
|
||
bucket.Methods(http.MethodHead).Path(objectPath).HandlerFunc(track(s3a.AuthWithPublicRead(func(w http.ResponseWriter, r *http.Request) {
|
||
limitedHandler, _ := s3a.cb.Limit(s3a.HeadObjectHandler, ACTION_READ)
|
||
limitedHandler(w, r)
|
||
}, ACTION_READ), "GET"))
|
||
|
||
// GetObject, but directory listing is not supported
|
||
bucket.Methods(http.MethodGet).Path(objectPath).HandlerFunc(track(s3a.AuthWithPublicRead(func(w http.ResponseWriter, r *http.Request) {
|
||
limitedHandler, _ := s3a.cb.Limit(s3a.GetObjectHandler, ACTION_READ)
|
||
limitedHandler(w, r)
|
||
}, ACTION_READ), "GET"))
|
||
|
||
// CopyObject
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HeadersRegexp("X-Amz-Copy-Source", `(?i).*?(\/|%2F).*?`).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.CopyObjectHandler, ACTION_WRITE)), "COPY"))
|
||
// PutObject
|
||
bucket.Methods(http.MethodPut).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectHandler, ACTION_WRITE)), "PUT"))
|
||
// DeleteObject
|
||
bucket.Methods(http.MethodDelete).Path(objectPath).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteObjectHandler, ACTION_WRITE)), "DELETE"))
|
||
|
||
// raw objects
|
||
|
||
// buckets with query
|
||
|
||
// DeleteMultipleObjects
|
||
bucket.Methods(http.MethodPost).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteMultipleObjectsHandler, ACTION_WRITE)), "DELETE")).Queries("delete", "")
|
||
|
||
// GetBucketACL
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketAclHandler, ACTION_READ_ACP)), "GET")).Queries("acl", "")
|
||
// PutBucketACL
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketAclHandler, ACTION_WRITE_ACP)), "PUT")).Queries("acl", "")
|
||
|
||
// GetBucketPolicy
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketPolicyHandler, ACTION_READ)), "GET")).Queries("policy", "")
|
||
// PutBucketPolicy
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketPolicyHandler, ACTION_PUT_BUCKET_POLICY)), "PUT")).Queries("policy", "")
|
||
// DeleteBucketPolicy
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketPolicyHandler, ACTION_DELETE_BUCKET_POLICY)), "DELETE")).Queries("policy", "")
|
||
|
||
// GetBucketCors
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketCorsHandler, ACTION_READ)), "GET")).Queries("cors", "")
|
||
// PutBucketCors
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketCorsHandler, ACTION_PUT_BUCKET_CORS)), "PUT")).Queries("cors", "")
|
||
// DeleteBucketCors
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketCorsHandler, ACTION_DELETE_BUCKET_CORS)), "DELETE")).Queries("cors", "")
|
||
|
||
// GetBucketLifecycleConfiguration
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketLifecycleConfigurationHandler, ACTION_READ)), "GET")).Queries("lifecycle", "")
|
||
// PutBucketLifecycleConfiguration
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketLifecycleConfigurationHandler, ACTION_ADMIN)), "PUT")).Queries("lifecycle", "")
|
||
// DeleteBucketLifecycleConfiguration
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketLifecycleHandler, ACTION_ADMIN)), "DELETE")).Queries("lifecycle", "")
|
||
|
||
// GetBucketLocation
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketLocationHandler, ACTION_READ)), "GET")).Queries("location", "")
|
||
|
||
// GetBucketRequestPayment
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketRequestPaymentHandler, ACTION_READ)), "GET")).Queries("requestPayment", "")
|
||
// PutBucketRequestPayment
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketRequestPaymentHandler, ACTION_ADMIN)), "PUT")).Queries("requestPayment", "")
|
||
|
||
// Static bucket configuration endpoints for AWS-SDK compatibility
|
||
// GetBucketPolicyStatus
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketPolicyStatusHandler, ACTION_READ)), "GET")).Queries("policyStatus", "")
|
||
// GetBucketAccelerateConfiguration
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketAccelerateConfigurationHandler, ACTION_READ)), "GET")).Queries("accelerate", "")
|
||
// PutBucketAccelerateConfiguration
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketAccelerateConfigurationHandler, ACTION_ADMIN)), "PUT")).Queries("accelerate", "")
|
||
// GetBucketLogging
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketLoggingHandler, ACTION_READ)), "GET")).Queries("logging", "")
|
||
// GetBucketNotificationConfiguration
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketNotificationConfigurationHandler, ACTION_READ)), "GET")).Queries("notification", "")
|
||
// PutBucketNotificationConfiguration
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketNotificationConfigurationHandler, ACTION_ADMIN)), "PUT")).Queries("notification", "")
|
||
// GetBucketReplication
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketReplicationHandler, ACTION_READ)), "GET")).Queries("replication", "")
|
||
// GetBucketWebsite
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketWebsiteHandler, ACTION_READ)), "GET")).Queries("website", "")
|
||
|
||
// GetBucketVersioning
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketVersioningHandler, ACTION_READ)), "GET")).Queries("versioning", "")
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketVersioningHandler, ACTION_PUT_BUCKET_VERSIONING)), "PUT")).Queries("versioning", "")
|
||
|
||
// GetObjectLockConfiguration / PutObjectLockConfiguration (bucket-level operations)
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetObjectLockConfigurationHandler, ACTION_GET_BUCKET_OBJECT_LOCK_CONFIG)), "GET")).Queries("object-lock", "")
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutObjectLockConfigurationHandler, ACTION_PUT_BUCKET_OBJECT_LOCK_CONFIG)), "PUT")).Queries("object-lock", "")
|
||
|
||
// GetBucketTagging
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketTaggingHandler, ACTION_TAGGING)), "GET")).Queries("tagging", "")
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketTaggingHandler, ACTION_TAGGING)), "PUT")).Queries("tagging", "")
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketTaggingHandler, ACTION_TAGGING)), "DELETE")).Queries("tagging", "")
|
||
|
||
// GetBucketEncryption
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketEncryptionHandler, ACTION_ADMIN)), "GET")).Queries("encryption", "")
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketEncryptionHandler, ACTION_ADMIN)), "PUT")).Queries("encryption", "")
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketEncryptionHandler, ACTION_ADMIN)), "DELETE")).Queries("encryption", "")
|
||
|
||
// GetPublicAccessBlockHandler
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetPublicAccessBlockHandler, ACTION_ADMIN)), "GET")).Queries("publicAccessBlock", "")
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutPublicAccessBlockHandler, ACTION_ADMIN)), "PUT")).Queries("publicAccessBlock", "")
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeletePublicAccessBlockHandler, ACTION_ADMIN)), "DELETE")).Queries("publicAccessBlock", "")
|
||
|
||
// Empty bucket configuration stubs for AWS-SDK compatibility (analytics, inventory, intelligent-tiering, metrics)
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetAnalyticsConfiguration, ACTION_READ)), "GET")).Queries("analytics", "", "id", "{id:.*}")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListBucketAnalyticsConfigurations, ACTION_READ)), "GET")).Queries("analytics", "")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetInventoryConfiguration, ACTION_READ)), "GET")).Queries("inventory", "", "id", "{id:.*}")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListBucketInventoryConfigurations, ACTION_READ)), "GET")).Queries("inventory", "")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetIntelligentTieringConfiguration, ACTION_READ)), "GET")).Queries("intelligent-tiering", "", "id", "{id:.*}")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListBucketIntelligentTieringConfigurations, ACTION_READ)), "GET")).Queries("intelligent-tiering", "")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetMetricsConfiguration, ACTION_READ)), "GET")).Queries("metrics", "", "id", "{id:.*}")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListBucketMetricsConfigurations, ACTION_READ)), "GET")).Queries("metrics", "")
|
||
|
||
// ListObjectsV2
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.AuthWithPublicRead(func(w http.ResponseWriter, r *http.Request) {
|
||
limitedHandler, _ := s3a.cb.Limit(s3a.ListObjectsV2Handler, ACTION_LIST)
|
||
limitedHandler(w, r)
|
||
}, ACTION_LIST), "LIST")).Queries("list-type", "2")
|
||
|
||
// ListObjectVersions
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.ListObjectVersionsHandler, ACTION_LIST)), "LIST")).Queries("versions", "")
|
||
|
||
// buckets with query
|
||
// PutBucketOwnershipControls
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.PutBucketOwnershipControls, ACTION_ADMIN), "PUT")).Queries("ownershipControls", "")
|
||
|
||
//GetBucketOwnershipControls
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.GetBucketOwnershipControls, ACTION_READ), "GET")).Queries("ownershipControls", "")
|
||
|
||
//DeleteBucketOwnershipControls
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.DeleteBucketOwnershipControls, ACTION_ADMIN), "DELETE")).Queries("ownershipControls", "")
|
||
|
||
// SeaweedFS extension: bucket quota subresource
|
||
// PUT /{bucket}?seaweedfs-quota — set bucket quota (s3:PutBucketQuota)
|
||
// GET /{bucket}?seaweedfs-quota — get bucket quota (s3:GetBucketQuota)
|
||
// Authenticated via SigV4, authorized via dedicated IAM permissions.
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketQuotaHandler, ACTION_PUT_BUCKET_QUOTA)), "PUT")).Queries("seaweedfs-quota", "")
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.GetBucketQuotaHandler, ACTION_GET_BUCKET_QUOTA)), "GET")).Queries("seaweedfs-quota", "")
|
||
|
||
// raw buckets
|
||
|
||
// PostPolicy
|
||
bucket.Methods(http.MethodPost).HeadersRegexp("Content-Type", "multipart/form-data*").HandlerFunc(track(s3a.iam.AuthPostPolicy(s3a.cb.Limit(s3a.PostPolicyBucketHandler, ACTION_WRITE)), "POST"))
|
||
|
||
// HeadBucket
|
||
bucket.Methods(http.MethodHead).HandlerFunc(track(s3a.AuthWithPublicRead(func(w http.ResponseWriter, r *http.Request) {
|
||
limitedHandler, _ := s3a.cb.Limit(s3a.HeadBucketHandler, ACTION_READ)
|
||
limitedHandler(w, r)
|
||
}, ACTION_READ), "GET"))
|
||
|
||
// PutBucket
|
||
bucket.Methods(http.MethodPut).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.PutBucketHandler, ACTION_ADMIN)), "PUT"))
|
||
|
||
// DeleteBucket
|
||
bucket.Methods(http.MethodDelete).HandlerFunc(track(s3a.iam.Auth(s3a.cb.Limit(s3a.DeleteBucketHandler, ACTION_DELETE_BUCKET)), "DELETE"))
|
||
|
||
// ListObjectsV1 (Legacy). This is the catch-all GET on a bucket, so a
|
||
// subresource with no route of its own would be answered with a listing.
|
||
bucket.Methods(http.MethodGet).HandlerFunc(track(s3a.AuthWithPublicRead(func(w http.ResponseWriter, r *http.Request) {
|
||
if subresource, found := unroutedBucketSubresource(r); found {
|
||
glog.V(1).Infof("unimplemented bucket subresource ?%s", subresource)
|
||
s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
|
||
return
|
||
}
|
||
limitedHandler, _ := s3a.cb.Limit(s3a.ListObjectsV1Handler, ACTION_LIST)
|
||
limitedHandler(w, r)
|
||
}, ACTION_LIST), "LIST"))
|
||
|
||
// raw buckets
|
||
|
||
}
|
||
|
||
// Global OPTIONS handler for service-level requests (non-bucket requests)
|
||
// This handles requests like OPTIONS /, OPTIONS /status, OPTIONS /healthz
|
||
// Place this after bucket handlers to avoid interfering with bucket CORS middleware
|
||
apiRouter.Methods(http.MethodOptions).PathPrefix("/").HandlerFunc(
|
||
func(w http.ResponseWriter, r *http.Request) {
|
||
// Only handle if this is not a bucket-specific request
|
||
vars := mux.Vars(r)
|
||
bucket := vars["bucket"]
|
||
if bucket != "" {
|
||
// This is a bucket-specific request, let bucket CORS middleware handle it
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
|
||
serviceOptionsHandler(w, r)
|
||
})
|
||
|
||
// STS API endpoint for AssumeRoleWithWebIdentity
|
||
// POST /?Action=AssumeRoleWithWebIdentity&WebIdentityToken=...
|
||
if s3a.stsHandlers != nil {
|
||
// 1. Explicit query param match (highest priority)
|
||
apiRouter.Methods(http.MethodPost).Path("/").Queries("Action", "AssumeRoleWithWebIdentity").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS"))
|
||
|
||
// AssumeRole - requires SigV4 authentication
|
||
apiRouter.Methods(http.MethodPost).Path("/").Queries("Action", "AssumeRole").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS-AssumeRole"))
|
||
|
||
// AssumeRoleWithLDAPIdentity - uses LDAP credentials
|
||
apiRouter.Methods(http.MethodPost).Path("/").Queries("Action", "AssumeRoleWithLDAPIdentity").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS-LDAP"))
|
||
|
||
// GetCallerIdentity - returns caller identity based on SigV4 authentication
|
||
apiRouter.Methods(http.MethodPost).Path("/").Queries("Action", "GetCallerIdentity").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS-GetCallerIdentity"))
|
||
|
||
// GetFederationToken - requires SigV4 authentication (long-term IAM user credentials)
|
||
apiRouter.Methods(http.MethodPost).Path("/").Queries("Action", "GetFederationToken").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS-GetFederationToken"))
|
||
|
||
glog.V(1).Infof("STS API enabled on S3 port (AssumeRole, AssumeRoleWithWebIdentity, AssumeRoleWithLDAPIdentity, GetCallerIdentity, GetFederationToken)")
|
||
}
|
||
|
||
// Embedded IAM API endpoint
|
||
// POST / (without specific query parameters)
|
||
// Uses AuthIam for granular permission checking
|
||
if s3a.embeddedIam != nil {
|
||
|
||
// 2. Authenticated IAM/STS Post requests
|
||
// Only match if the request appears to be authenticated (AWS Signature)
|
||
// We use a UnifiedPostHandler to dispatch based on Action (STS vs IAM)
|
||
iamMatcher := func(r *http.Request, rm *mux.RouteMatch) bool {
|
||
if getRequestAuthType(r) == authTypeAnonymous {
|
||
return false
|
||
}
|
||
|
||
// IMPORTANT: We do NOT parse the body here.
|
||
// UnifiedPostHandler will handle authentication and body parsing.
|
||
// We only filter out requests that are explicitly targeted at STS via Query params
|
||
// to avoid double-handling, although UnifiedPostHandler would handle them correctly anyway.
|
||
|
||
// Action in Query String is handled by explicit STS routes above
|
||
action := r.URL.Query().Get("Action")
|
||
if action == "AssumeRole" || action == "AssumeRoleWithWebIdentity" || action == "AssumeRoleWithLDAPIdentity" || action == "GetCallerIdentity" || action == "GetFederationToken" {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
apiRouter.Methods(http.MethodPost).Path("/").MatcherFunc(iamMatcher).
|
||
HandlerFunc(track(s3a.UnifiedPostHandler, "IAM-Unified"))
|
||
|
||
glog.V(1).Infof("Embedded IAM API enabled on S3 port")
|
||
}
|
||
|
||
// 3. Fallback STS handler (lowest priority)
|
||
// Catches unauthenticated POST / requests that didn't match specific query params.
|
||
// This primarily handles AssumeRoleWithWebIdentity where parameters are in the POST body.
|
||
if s3a.stsHandlers != nil {
|
||
glog.V(1).Infof("Registering fallback STS handler for unauthenticated POST requests")
|
||
apiRouter.Methods(http.MethodPost).Path("/").
|
||
HandlerFunc(track(s3a.stsHandlers.HandleSTSRequest, "STS-Fallback"))
|
||
}
|
||
|
||
// ListBuckets
|
||
apiRouter.Methods(http.MethodGet).Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_LIST), "LIST"))
|
||
|
||
// NotFound
|
||
apiRouter.NotFoundHandler = http.HandlerFunc(s3err.NotFoundHandler)
|
||
|
||
}
|
||
|
||
// loadIAMManagerFromConfig loads the advanced IAM manager from configuration file
|
||
func loadIAMManagerFromConfig(configPath string, filerAddressProvider func() string, getFilerSigningKey func() string) (*integration.IAMManager, error) {
|
||
var configRoot struct {
|
||
STS *sts.STSConfig `json:"sts"`
|
||
Policy *policy.PolicyEngineConfig `json:"policy"`
|
||
Providers []map[string]interface{} `json:"providers"`
|
||
Roles []*integration.RoleDefinition `json:"roles"`
|
||
// OIDCProviderStore selects where IAM-managed OIDC providers persist.
|
||
// Absent, they live in memory and are lost on restart.
|
||
OIDCProviderStore *integration.OIDCProviderStoreConfig `json:"oidcProviderStore"`
|
||
// RoleStore selects where roles persist. Absent, they live in memory and
|
||
// only the roles in this file exist.
|
||
RoleStore *integration.RoleStoreConfig `json:"roleStore"`
|
||
Policies []struct {
|
||
Name string `json:"name"`
|
||
Document *policy.PolicyDocument `json:"document"`
|
||
} `json:"policies"`
|
||
}
|
||
|
||
if configPath != "" {
|
||
// Read configuration file
|
||
configData, err := os.ReadFile(configPath)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||
}
|
||
|
||
if err := json.Unmarshal(configData, &configRoot); err != nil {
|
||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||
}
|
||
} else {
|
||
glog.V(1).Infof("No IAM config file provided; using defaults")
|
||
// Initialize with empty config which will trigger defaults below
|
||
}
|
||
|
||
// Ensure STS config exists so we can apply defaults later
|
||
if configRoot.STS == nil {
|
||
configRoot.STS = &sts.STSConfig{}
|
||
}
|
||
|
||
// Ensure a valid policy engine config exists
|
||
if configRoot.Policy == nil {
|
||
configRoot.Policy = &policy.PolicyEngineConfig{}
|
||
}
|
||
if configRoot.Policy.StoreType == "" {
|
||
configRoot.Policy.StoreType = sts.StoreTypeMemory
|
||
}
|
||
if configRoot.Policy.DefaultEffect == "" {
|
||
// Secure default when an explicit IAM config file is provided:
|
||
// omitted defaultEffect should be Deny to avoid unintentional privilege escalation.
|
||
// Keep zero-config startup behavior (no config file path) open for memory store.
|
||
if configPath == "" && configRoot.Policy.StoreType == sts.StoreTypeMemory {
|
||
configRoot.Policy.DefaultEffect = sts.EffectAllow
|
||
} else {
|
||
configRoot.Policy.DefaultEffect = sts.EffectDeny
|
||
}
|
||
glog.V(1).Infof("Using policy defaults: DefaultEffect=%s, StoreType=%s", configRoot.Policy.DefaultEffect, configRoot.Policy.StoreType)
|
||
}
|
||
|
||
// With no IAM config file there is nothing static for a persisted
|
||
// provider or role to shadow or outlive, so those created at runtime
|
||
// default to the filer, where restarts and peer S3 servers see them. A
|
||
// config file keeps the in-memory defaults unless it sets
|
||
// oidcProviderStore / roleStore.
|
||
persistByDefault := configPath == "" && filerAddressProvider != nil
|
||
oidcProviderStore := configRoot.OIDCProviderStore
|
||
if oidcProviderStore == nil && persistByDefault {
|
||
oidcProviderStore = &integration.OIDCProviderStoreConfig{StoreType: "filer"}
|
||
}
|
||
roleStore := configRoot.RoleStore
|
||
if roleStore == nil {
|
||
roleStore = &integration.RoleStoreConfig{StoreType: sts.StoreTypeMemory}
|
||
if persistByDefault {
|
||
roleStore = &integration.RoleStoreConfig{StoreType: "filer"}
|
||
}
|
||
}
|
||
|
||
// Create IAM configuration
|
||
iamConfig := &integration.IAMConfig{
|
||
STS: configRoot.STS,
|
||
Policy: configRoot.Policy,
|
||
Roles: roleStore,
|
||
OIDCProviders: oidcProviderStore,
|
||
}
|
||
|
||
// Apply default signing key if not present in config
|
||
if iamConfig.STS != nil && len(iamConfig.STS.SigningKey) == 0 {
|
||
// 1. Try server-configured signing key (security.toml / CLI)
|
||
if key := getFilerSigningKey(); key != "" {
|
||
iamConfig.STS.SigningKey = []byte(key)
|
||
glog.V(1).Infof("Using default filer signing key for STS service")
|
||
} else {
|
||
// 2. Try cluster-wide SSE-S3 Master Key (KEK) from Filer
|
||
// This ensures zero-config consistency across the cluster
|
||
if kek := GetSSES3KeyManager().GetMasterKey(); len(kek) > 0 {
|
||
iamConfig.STS.SigningKey = kek
|
||
glog.V(1).Infof("Using SSE-S3 Master Key (KEK) for STS service")
|
||
} else {
|
||
// 3. Fail if no signing key is available
|
||
// This ensures consistency across multiple S3 servers and secure operation
|
||
return nil, fmt.Errorf("no signing key found for STS service; please provide 'signingKey' in IAM config, configure 'jwt.filer_signing.key' in security.toml, or ensure SSE-S3 is initialized")
|
||
}
|
||
}
|
||
}
|
||
|
||
// Initialize IAM manager
|
||
iamManager := integration.NewIAMManager()
|
||
if err := iamManager.Initialize(iamConfig, filerAddressProvider); err != nil {
|
||
return nil, fmt.Errorf("failed to initialize IAM manager: %w", err)
|
||
}
|
||
|
||
// Load identity providers
|
||
providerFactory := sts.NewProviderFactory()
|
||
for _, providerConfig := range configRoot.Providers {
|
||
// Check for required fields with explicit type assertion
|
||
name, ok := providerConfig["name"].(string)
|
||
if !ok || name == "" {
|
||
glog.Warningf("Skipping provider with invalid or missing name: %+v", providerConfig)
|
||
continue
|
||
}
|
||
providerType, ok := providerConfig["type"].(string)
|
||
if !ok || providerType == "" {
|
||
glog.Warningf("Skipping provider %s with invalid or missing type", name)
|
||
continue
|
||
}
|
||
|
||
// Fix: providerConfig["roleMapping"] might be missing from "config" map if configured externally
|
||
// We inject it into the config map so the factory can find it
|
||
configMap, ok := providerConfig["config"].(map[string]interface{})
|
||
if !ok {
|
||
glog.Warningf("Validation failed for provider %s: config must be a map", name)
|
||
continue
|
||
}
|
||
|
||
if roleMapping, ok := providerConfig["roleMapping"]; ok {
|
||
configMap["roleMapping"] = roleMapping
|
||
}
|
||
|
||
provider, err := providerFactory.CreateProvider(&sts.ProviderConfig{
|
||
Name: name,
|
||
Type: providerType,
|
||
Enabled: true,
|
||
Config: configMap,
|
||
})
|
||
if err != nil {
|
||
glog.Warningf("Failed to create provider %s: %v", providerConfig["name"], err)
|
||
continue
|
||
}
|
||
if provider != nil {
|
||
if err := iamManager.RegisterIdentityProvider(provider); err != nil {
|
||
glog.Warningf("Failed to register provider %s: %v", providerConfig["name"], err)
|
||
} else {
|
||
glog.V(1).Infof("Registered identity provider: %s", providerConfig["name"])
|
||
}
|
||
}
|
||
}
|
||
|
||
// Load policies
|
||
for _, policyDef := range configRoot.Policies {
|
||
if err := iamManager.CreatePolicy(context.Background(), "", policyDef.Name, policyDef.Document); err != nil {
|
||
glog.Warningf("Failed to create policy %s: %v", policyDef.Name, err)
|
||
}
|
||
}
|
||
|
||
// Load roles
|
||
iamManager.LoadStaticRoles(context.Background(), configRoot.Roles)
|
||
|
||
glog.V(1).Infof("Loaded %d providers, %d policies and %d roles from config", len(configRoot.Providers), len(configRoot.Policies), len(configRoot.Roles))
|
||
|
||
return iamManager, nil
|
||
}
|
||
|
||
// AuthenticateRequest authenticates the request and returns the identity name and object
|
||
func (s3a *S3ApiServer) AuthenticateRequest(r *http.Request) (string, interface{}, s3err.ErrorCode) {
|
||
if s3a.iam == nil {
|
||
return "", nil, s3err.ErrAccessDenied
|
||
}
|
||
identity, err := s3a.iam.AuthenticateRequest(r)
|
||
if identity != nil {
|
||
return identity.Name, identity, err
|
||
}
|
||
return "", nil, err
|
||
}
|
||
|
||
// FilerSigningKey is the current jwt.filer_signing.key, which follows a
|
||
// security.toml reload.
|
||
func (s3a *S3ApiServer) FilerSigningKey() security.SigningKey {
|
||
if s3a.filerGuard == nil {
|
||
return nil
|
||
}
|
||
return s3a.filerGuard.SigningKey()
|
||
}
|
||
|
||
// AuthEnabled reports whether requests must carry valid credentials. With no
|
||
// identities configured, every request is allowed as the admin.
|
||
func (s3a *S3ApiServer) AuthEnabled() bool {
|
||
return s3a.iam == nil || s3a.iam.isEnabled()
|
||
}
|
||
|
||
// DefaultAllow returns whether access is allowed by default when no policy is found
|
||
func (s3a *S3ApiServer) DefaultAllow() bool {
|
||
if s3a.iam == nil || s3a.iam.iamIntegration == nil {
|
||
return false
|
||
}
|
||
return s3a.iam.iamIntegration.DefaultAllow()
|
||
}
|
||
|
||
// IsConfiguredAccountID reports whether id is the account of a configured identity.
|
||
func (s3a *S3ApiServer) IsConfiguredAccountID(id string) bool {
|
||
return s3a.iam != nil && s3a.iam.isConfiguredAccountID(id)
|
||
}
|
||
|
||
// TrustedOIDCIssuers returns the configured OIDC issuer set.
|
||
func (s3a *S3ApiServer) TrustedOIDCIssuers(ctx context.Context) []string {
|
||
if s3a.iam == nil {
|
||
return nil
|
||
}
|
||
if s3iam, ok := s3a.iam.iamIntegration.(*S3IAMIntegration); ok {
|
||
return s3iam.trustedOIDCIssuers(ctx)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ValidateS3Credential validates an S3 access key / secret key pair.
|
||
// Returns the identity name and identity object on success.
|
||
func (s3a *S3ApiServer) ValidateS3Credential(accessKey, secretKey string) (string, interface{}, error) {
|
||
if s3a.iam == nil {
|
||
return "", nil, fmt.Errorf("IAM not configured")
|
||
}
|
||
identity, cred, found := s3a.iam.LookupByAccessKey(accessKey)
|
||
if !found {
|
||
return "", nil, fmt.Errorf("access key not found")
|
||
}
|
||
if cred.SecretKey != secretKey {
|
||
return "", nil, fmt.Errorf("invalid secret key")
|
||
}
|
||
if identity.Disabled {
|
||
return "", nil, fmt.Errorf("identity is disabled")
|
||
}
|
||
if cred.isCredentialExpired() {
|
||
return "", nil, fmt.Errorf("credential expired")
|
||
}
|
||
if cred.Status == "Inactive" {
|
||
return "", nil, fmt.Errorf("credential is inactive")
|
||
}
|
||
return identity.Name, identity, nil
|
||
}
|
||
|
||
// GetCredentialByAccessKey looks up a credential by access key.
|
||
// Returns the identity name, identity object, and secret key.
|
||
// Used for verifying Iceberg OAuth Bearer tokens with the exact credential
|
||
// that was used to sign the token.
|
||
func (s3a *S3ApiServer) GetCredentialByAccessKey(accessKey string) (string, interface{}, string, error) {
|
||
if s3a.iam == nil {
|
||
return "", nil, "", fmt.Errorf("IAM not configured")
|
||
}
|
||
identity, cred, found := s3a.iam.LookupByAccessKey(accessKey)
|
||
if !found {
|
||
return "", nil, "", fmt.Errorf("access key not found")
|
||
}
|
||
if identity.Disabled {
|
||
return "", nil, "", fmt.Errorf("identity is disabled")
|
||
}
|
||
if cred.isCredentialExpired() {
|
||
return "", nil, "", fmt.Errorf("credential expired")
|
||
}
|
||
if cred.Status == "Inactive" {
|
||
return "", nil, "", fmt.Errorf("credential is inactive")
|
||
}
|
||
return identity.Name, identity, cred.SecretKey, nil
|
||
}
|