Files
seaweedfs/weed/server/volume_grpc_remote_test.go
T
Chris LuandClaude Opus 5.5 284d491587 s3: forward notification topics to webhook targets (#13245)
* 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>
2026-12-01 21:45:41 -08:00

1202 lines
45 KiB
Go

package weed_server
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"io"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
s3remote "github.com/seaweedfs/seaweedfs/weed/remote_storage/s3"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
"github.com/seaweedfs/seaweedfs/weed/util/netguard"
)
// stubLookup returns a resolver func that maps the supplied hostnames to
// the supplied IP addresses, and errors for any host that is not in the map.
func stubLookup(t *testing.T, mapping map[string][]net.IP) func(ctx context.Context, host string) ([]net.IPAddr, error) {
t.Helper()
return func(_ context.Context, host string) ([]net.IPAddr, error) {
ips, ok := mapping[host]
if !ok {
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
}
out := make([]net.IPAddr, 0, len(ips))
for _, ip := range ips {
out = append(out, net.IPAddr{IP: ip})
}
return out, nil
}
}
func TestValidateRemoteEndpoint(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
netguard.LookupIPAddr = stubLookup(t, map[string][]net.IP{
"s3.us-east-1.amazonaws.com": {net.ParseIP("52.216.10.10")},
"internal.example.com": {net.ParseIP("127.0.0.1")},
"linklocal.example.com": {net.ParseIP("169.254.10.20")},
"private.example.com": {net.ParseIP("10.1.2.3")},
"private172.example.com": {net.ParseIP("172.20.0.5")},
"private192.example.com": {net.ParseIP("192.168.1.1")},
"cgnat.example.com": {net.ParseIP("100.64.0.42")},
})
cases := []struct {
name string
endpoint string
wantErr bool
wantSub string
}{
{
name: "empty",
endpoint: "",
wantErr: true,
wantSub: "empty",
},
{
name: "loopback literal",
endpoint: "http://127.0.0.1:8080",
wantErr: true,
wantSub: "loopback",
},
{
name: "ipv6 loopback",
endpoint: "http://[::1]:8080",
wantErr: true,
wantSub: "loopback",
},
{
name: "imds ipv4",
endpoint: "http://169.254.169.254/",
wantErr: true,
wantSub: "metadata",
},
{
name: "unspecified ipv4",
endpoint: "http://0.0.0.0/",
wantErr: true,
wantSub: "unspecified",
},
{
name: "link-local ipv6",
endpoint: "http://[fe80::1]/",
wantErr: true,
wantSub: "link-local",
},
{
name: "ftp scheme",
endpoint: "ftp://example.com/",
wantErr: true,
wantSub: "http or https",
},
{
name: "missing scheme",
endpoint: "example.com/",
wantErr: true,
wantSub: "http or https",
},
{
name: "imds hostname",
endpoint: "http://metadata.google.internal/",
wantErr: true,
wantSub: "metadata service",
},
{
name: "imds short hostname",
endpoint: "http://metadata/",
wantErr: true,
wantSub: "metadata service",
},
{
name: "host resolves to loopback",
endpoint: "https://internal.example.com/",
wantErr: true,
wantSub: "loopback",
},
{
name: "host resolves to link-local",
endpoint: "https://linklocal.example.com/",
wantErr: true,
wantSub: "link-local",
},
{
name: "rfc1918 10/8 literal",
endpoint: "http://10.0.0.1/",
wantErr: true,
wantSub: "private",
},
{
name: "rfc1918 172.16/12 literal",
endpoint: "http://172.16.5.5/",
wantErr: true,
wantSub: "private",
},
{
name: "rfc1918 192.168/16 literal",
endpoint: "http://192.168.0.1/",
wantErr: true,
wantSub: "private",
},
{
name: "cgnat literal",
endpoint: "http://100.64.0.1/",
wantErr: true,
wantSub: "CGNAT",
},
{
name: "host resolves to rfc1918 10/8",
endpoint: "https://private.example.com/",
wantErr: true,
wantSub: "private",
},
{
name: "host resolves to rfc1918 172/12",
endpoint: "https://private172.example.com/",
wantErr: true,
wantSub: "private",
},
{
name: "host resolves to rfc1918 192.168/16",
endpoint: "https://private192.example.com/",
wantErr: true,
wantSub: "private",
},
{
name: "host resolves to cgnat",
endpoint: "https://cgnat.example.com/",
wantErr: true,
wantSub: "CGNAT",
},
{
name: "nat64 imds",
endpoint: "http://[64:ff9b::a9fe:a9fe]/",
wantErr: true,
wantSub: "metadata",
},
{
name: "nat64 loopback",
endpoint: "http://[64:ff9b::7f00:1]/",
wantErr: true,
wantSub: "loopback",
},
{
name: "6to4 private",
endpoint: "http://[2002:a00:1::]/",
wantErr: true,
wantSub: "private",
},
{
name: "teredo loopback",
endpoint: "http://[2001:0:4136:e378:8000:63bf:80ff:fffe]/",
wantErr: true,
wantSub: "loopback",
},
{
name: "ipv4-compatible loopback",
endpoint: "http://[::7f00:1]/",
wantErr: true,
wantSub: "loopback",
},
{
name: "nat64 public passes",
endpoint: "http://[64:ff9b::808:808]/",
wantErr: false,
},
{
name: "6to4 public passes",
endpoint: "http://[2002:808:808::]/",
wantErr: false,
},
{
name: "teredo public passes",
endpoint: "http://[2001::f7f7:f7f7]/",
wantErr: false,
},
{
name: "ipv4-compatible public passes",
endpoint: "http://[::808:808]/",
wantErr: false,
},
{
name: "nat64 non-wellknown-prefix not decoded",
endpoint: "http://[64:ff9b:1::a9fe:a9fe]/",
wantErr: false,
},
{
name: "public s3",
endpoint: "https://s3.us-east-1.amazonaws.com/",
wantErr: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := validateRemoteEndpoint(context.Background(), tc.endpoint)
if tc.wantErr {
if err == nil {
t.Fatalf("expected error for %q, got nil", tc.endpoint)
}
if tc.wantSub != "" && !strings.Contains(err.Error(), tc.wantSub) {
t.Fatalf("expected error to contain %q, got %v", tc.wantSub, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error for %q: %v", tc.endpoint, err)
}
})
}
}
func TestValidateRemoteEndpointResolverFailure(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
resolveErr := errors.New("simulated DNS failure")
netguard.LookupIPAddr = func(_ context.Context, _ string) ([]net.IPAddr, error) {
return nil, resolveErr
}
err := validateRemoteEndpoint(context.Background(), "https://does-not-resolve.example.com/")
if err == nil {
t.Fatal("expected error when resolver fails")
}
if !strings.Contains(err.Error(), "resolve remote endpoint host") {
t.Fatalf("expected resolver error wrapping, got %v", err)
}
}
// TestGuardedDialerRebind simulates a DNS rebinding attack: the host first
// resolves to a public address (passing validateRemoteEndpoint) and then
// flips to 127.0.0.1 on the very next lookup (what the AWS SDK would do at
// dial time). The dial path must refuse the loopback answer instead of
// connecting to it.
func TestGuardedDialerRebind(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
const host = "rebind.example.com"
endpoint := "https://" + host + "/"
var calls atomic.Int32
netguard.LookupIPAddr = func(_ context.Context, name string) ([]net.IPAddr, error) {
if name != host {
return nil, &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
}
if calls.Add(1) == 1 {
return []net.IPAddr{{IP: net.ParseIP("52.216.10.10")}}, nil
}
return []net.IPAddr{{IP: net.ParseIP("127.0.0.1")}}, nil
}
if err := validateRemoteEndpoint(context.Background(), endpoint); err != nil {
t.Fatalf("first-pass validation should accept public IP, got %v", err)
}
dial := guardedDialer(endpoint)
conn, err := dial(context.Background(), "tcp", host+":443")
if conn != nil {
conn.Close()
t.Fatalf("guarded dialer must refuse loopback rebind, got conn")
}
if err == nil || !strings.Contains(err.Error(), "loopback") {
t.Fatalf("guarded dialer should fail with loopback error, got %v", err)
}
}
// TestRemoteEndpointGuardCoversS3CompatibleSiblings confirms the SSRF guard
// reaches every S3-SDK-backed provider, not just type "s3". It replays the two
// steps FetchAndWriteNeedle performs before building the client: resolve the
// endpoint the type would dial, then validate it against the deny-list. A
// sibling type pointed at an internal address must be rejected.
func TestRemoteEndpointGuardCoversS3CompatibleSiblings(t *testing.T) {
cases := []struct {
conf *remote_pb.RemoteConf
wantSub string
}{
{&remote_pb.RemoteConf{Type: "wasabi", WasabiEndpoint: "http://169.254.169.254/"}, "metadata"},
{&remote_pb.RemoteConf{Type: "b2", BackblazeEndpoint: "http://127.0.0.1/"}, "loopback"},
{&remote_pb.RemoteConf{Type: "aliyun", AliyunEndpoint: "http://192.168.0.1/"}, "private"},
{&remote_pb.RemoteConf{Type: "tencent", TencentEndpoint: "http://100.64.0.1/"}, "CGNAT"},
{&remote_pb.RemoteConf{Type: "baidu", BaiduEndpoint: "http://169.254.169.254/"}, "metadata"},
{&remote_pb.RemoteConf{Type: "filebase", FilebaseEndpoint: "http://172.16.0.1/"}, "private"},
{&remote_pb.RemoteConf{Type: "storj", StorjEndpoint: "http://10.0.0.5/"}, "private"},
{&remote_pb.RemoteConf{Type: "contabo", ContaboEndpoint: "http://[::1]/"}, "loopback"},
}
for _, tc := range cases {
endpoint, ok := s3remote.S3CompatibleEndpoint(tc.conf)
if !ok {
t.Errorf("type %q: not recognized as S3-compatible, guard would be skipped", tc.conf.Type)
continue
}
err := validateRemoteEndpoint(context.Background(), endpoint)
if err == nil {
t.Errorf("type %q: expected endpoint %q to be rejected", tc.conf.Type, endpoint)
continue
}
if !strings.Contains(err.Error(), tc.wantSub) {
t.Errorf("type %q: error %q missing %q", tc.conf.Type, err, tc.wantSub)
}
}
}
// TestRemoteEndpointGuardCoversAzure confirms the SSRF guard reaches the azure
// backend, which dials a caller-supplied AzureEndpoint. It replays the two
// steps FetchAndWriteNeedle performs before building the client: resolve the
// endpoint the type would dial via guardedRemoteClient, then validate it. An
// azure conf pointed at an internal address must be rejected.
func TestRemoteEndpointGuardCoversAzure(t *testing.T) {
cases := []struct {
name string
conf *remote_pb.RemoteConf
wantSub string
}{
{"imds", &remote_pb.RemoteConf{Type: "azure", AzureEndpoint: "https://169.254.169.254/"}, "metadata"},
{"loopback", &remote_pb.RemoteConf{Type: "azure", AzureEndpoint: "https://127.0.0.1/"}, "loopback"},
{"private", &remote_pb.RemoteConf{Type: "azure", AzureEndpoint: "https://10.0.0.5/"}, "private"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
endpoint, _, ok := guardedRemoteClient(tc.conf)
if !ok {
t.Fatalf("azure endpoint %q not guarded, the SSRF check would be skipped", tc.conf.AzureEndpoint)
}
err := validateRemoteEndpoint(context.Background(), endpoint)
if err == nil {
t.Fatalf("expected endpoint %q to be rejected", endpoint)
}
if !strings.Contains(err.Error(), tc.wantSub) {
t.Fatalf("error %q missing %q", err, tc.wantSub)
}
})
}
}
// TestValidateReplicaTarget covers the replica upload leg of
// FetchAndWriteNeedle. Replica targets are peer volume servers, so unlike the
// remote endpoint they may sit on a private network; the guard still rejects
// loopback / link-local / unspecified hosts and any target that is not a bare
// host:port, since a scheme, path or query would move the upload to a different
// URL through the format string.
func TestValidateReplicaTarget(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
netguard.LookupIPAddr = stubLookup(t, map[string][]net.IP{
"peer.example.com": {net.ParseIP("10.0.0.7")},
"loop.example.com": {net.ParseIP("127.0.0.1")},
"linklocal.example.com": {net.ParseIP("169.254.169.254")},
})
cases := []struct {
name string
target string
wantErr bool
wantSub string
}{
// A path plus a trailing "?a=" would otherwise swallow ?type=replicate.
{"embedded path and query", "127.0.0.1:7000/status/x/?a=", true, "bare host:port"},
{"loopback literal", "127.0.0.1:8080", true, "loopback"},
{"ipv6 loopback", "[::1]:8080", true, "loopback"},
{"metadata literal", "169.254.169.254:80", true, "metadata"},
{"unspecified", "0.0.0.0:8080", true, "unspecified"},
{"metadata hostname", "metadata:80", true, "metadata"},
{"scheme rejected", "http://10.0.0.7:8080", true, "bare host:port"},
{"path rejected", "10.0.0.7:8080/x", true, "bare host:port"},
{"query rejected", "10.0.0.7:8080?a=b", true, "bare host:port"},
{"userinfo rejected", "user@10.0.0.7:8080", true, "bare host:port"},
{"missing port literal", "10.0.0.7", true, "bare host:port"},
{"missing port hostname", "peer.example.com", true, "bare host:port"},
{"empty", "", true, "empty"},
{"resolves to loopback", "loop.example.com:8080", true, "loopback"},
{"resolves to link-local", "linklocal.example.com:8080", true, "metadata"},
// Legitimate peer volume servers on private networks must pass.
{"private peer literal", "10.0.0.7:8080", false, ""},
{"private 192 peer", "192.168.1.5:8080", false, ""},
{"private peer hostname", "peer.example.com:8080", false, ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := validateReplicaTarget(context.Background(), tc.target)
if tc.wantErr {
if err == nil {
t.Fatalf("expected error for %q, got nil", tc.target)
}
if tc.wantSub != "" && !strings.Contains(err.Error(), tc.wantSub) {
t.Fatalf("expected error to contain %q, got %v", tc.wantSub, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error for %q: %v", tc.target, err)
}
})
}
}
// TestGuardedRemoteClientSkipsFixedHostBackends confirms backends that only
// reach a fixed provider host bypass the endpoint guard: azure with no explicit
// endpoint (public cloud, host derived from the account) and unrelated types.
func TestGuardedRemoteClientSkipsFixedHostBackends(t *testing.T) {
for _, conf := range []*remote_pb.RemoteConf{
{Type: "azure", AzureAccountName: "acct"},
{Type: "gcs"},
nil,
} {
if _, _, ok := guardedRemoteClient(conf); ok {
t.Errorf("conf %+v should not be guarded", conf)
}
}
}
// TestGuardedRemoteClientAzureBuildsGuardedClient exercises the whole azure
// path: a public endpoint passes validation and the constructor builds a client
// through the guarded HTTP transport.
func TestGuardedRemoteClientAzureBuildsGuardedClient(t *testing.T) {
conf := &remote_pb.RemoteConf{
Type: "azure",
AzureAccountName: "testaccount",
AzureAccountKey: "aW52YWxpZGtleQ==",
AzureEndpoint: "https://testaccount.blob.core.usgovcloudapi.net/",
}
endpoint, makeClient, ok := guardedRemoteClient(conf)
if !ok {
t.Fatal("azure with an endpoint should be guarded")
}
client, err := makeClient(newGuardedHTTPClient(endpoint))
if err != nil {
t.Fatalf("build guarded azure client: %v", err)
}
if client == nil {
t.Fatal("expected a client")
}
}
// TestGuardedRemoteClientGuardsGcsTokenURL confirms the token endpoint named by
// inline gcs credentials is the endpoint the guard validates, so a loopback
// token_uri is refused while the Google default passes.
func TestGuardedRemoteClientGuardsGcsTokenURL(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
netguard.LookupIPAddr = stubLookup(t, map[string][]net.IP{
"oauth2.googleapis.com": {net.ParseIP("142.250.72.10")},
})
endpoint, makeClient, ok := guardedRemoteClient(&remote_pb.RemoteConf{
Type: "gcs",
GcsGoogleApplicationCredentials: `{"type":"service_account","token_uri":"http://127.0.0.1:9/token"}`,
})
if !ok {
t.Fatal("gcs conf with inline credentials should be guarded")
}
if endpoint != "http://127.0.0.1:9/token" {
t.Errorf("endpoint = %q, want the credential token_uri", endpoint)
}
if err := validateRemoteEndpoint(context.Background(), endpoint); err == nil {
t.Error("expected the loopback token endpoint to be rejected")
}
if makeClient == nil {
t.Error("expected a constructor")
}
endpoint, _, ok = guardedRemoteClient(&remote_pb.RemoteConf{
Type: "gcs",
GcsGoogleApplicationCredentials: `{"type":"service_account"}`,
})
if !ok {
t.Fatal("gcs conf with inline credentials should be guarded")
}
if err := validateRemoteEndpoint(context.Background(), endpoint); err != nil {
t.Errorf("default token endpoint %q should pass: %v", endpoint, err)
}
}
// TestCheckGcsCredentials confirms only inline credentials that carry their own
// key material are accepted. The federated types name a url, file or executable
// that the SDK reads the token from, none of which the endpoint guard sees.
func TestCheckGcsCredentials(t *testing.T) {
rejected := []string{
"/etc/hostname",
"~/creds.json",
`{`,
`{}`,
`{"type":"external_account","token_url":"http://127.0.0.1:9/v1/token","credential_source":{"url":"http://169.254.169.254/latest/meta-data/"}}`,
`{"type":"external_account","token_url":"http://127.0.0.1:9/v1/token","credential_source":{"file":"/etc/shadow"}}`,
`{"type":"external_account","credential_source":{"executable":{"command":"/bin/sh"}}}`,
`{"type":"external_account_authorized_user","token_url":"http://127.0.0.1:9/v1/token"}`,
`{"type":"impersonated_service_account","service_account_impersonation_url":"http://127.0.0.1:9/x"}`,
}
for _, creds := range rejected {
if err := checkGcsCredentials(creds); err == nil {
t.Errorf("expected %q to be rejected", creds)
}
}
accepted := []string{
"",
`{"type":"service_account","client_email":"a@b.com","private_key":"k"}`,
`{"type":"service_account","token_uri":"https://oauth2.googleapis.com/token"}`,
`{"type":"authorized_user","refresh_token":"r"}`,
}
for _, creds := range accepted {
if err := checkGcsCredentials(creds); err != nil {
t.Errorf("expected %q to be accepted, got %v", creds, err)
}
}
}
// TestGuardedDialerLiteralBlocked confirms that a literal blocked IP target
// is refused without any DNS lookup.
func TestGuardedDialerLiteralBlocked(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
netguard.LookupIPAddr = func(_ context.Context, name string) ([]net.IPAddr, error) {
t.Fatalf("resolver should not be called for IP literal target, got lookup of %q", name)
return nil, nil
}
dial := guardedDialer("http://10.0.0.5:80")
conn, err := dial(context.Background(), "tcp", "10.0.0.5:80")
if conn != nil {
conn.Close()
t.Fatalf("guarded dialer must refuse rfc1918 literal, got conn")
}
if err == nil || !strings.Contains(err.Error(), "private") {
t.Fatalf("guarded dialer should fail with private-address error, got %v", err)
}
}
// TestGuardedReplicaDialerRebind confirms the replica upload's dial-time guard
// refuses a hostname that rebinds to loopback after validateReplicaTarget, yet
// keeps letting private peers through (netguard.Private).
func TestGuardedReplicaDialerRebind(t *testing.T) {
originalLookup := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = originalLookup })
const host = "replica.example.com"
var calls atomic.Int32
netguard.LookupIPAddr = func(_ context.Context, name string) ([]net.IPAddr, error) {
if name != host {
return nil, &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
}
if calls.Add(1) == 1 {
return []net.IPAddr{{IP: net.ParseIP("52.216.10.10")}}, nil
}
return []net.IPAddr{{IP: net.ParseIP("127.0.0.1")}}, nil
}
// Up-front validation sees the public answer and accepts the target.
if err := validateReplicaTarget(context.Background(), host+":8080"); err != nil {
t.Fatalf("public replica target should validate, got %v", err)
}
// The dial then re-resolves to loopback and must refuse it.
dial := netguard.DialContext(host+":8080", netguard.Private)
conn, err := dial(context.Background(), "tcp", host+":8080")
if conn != nil {
conn.Close()
t.Fatalf("guarded replica dialer must refuse loopback rebind, got conn")
}
if err == nil || !strings.Contains(err.Error(), "loopback") {
t.Fatalf("expected loopback refusal, got %v", err)
}
// A private literal peer is allowed through: the dial is attempted (and here
// fails on the already-cancelled context) rather than blocked as private.
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, perr := netguard.DialContext("10.0.0.5:80", netguard.Private)(ctx, "tcp", "10.0.0.5:80"); perr != nil && strings.Contains(perr.Error(), "private") {
t.Fatalf("private peer must be allowed by the replica dialer, got %v", perr)
}
}
// TestBuildGuardedRemoteStorageClient confirms the shared builder refuses a
// caller-influenced endpoint that resolves to a blocked address, and a gcs
// credentials path, while allowUntrusted falls back to the plain builder.
func TestBuildGuardedRemoteStorageClient(t *testing.T) {
loopbackS3 := &remote_pb.RemoteConf{
Name: "poc",
Type: "s3",
S3Endpoint: "http://127.0.0.1:8000",
S3AccessKey: "k",
S3SecretKey: "s",
S3Region: "us-east-1",
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), loopbackS3, false); err == nil {
t.Error("expected a loopback s3 endpoint to be rejected")
} else if !strings.Contains(err.Error(), "reject remote endpoint") {
t.Errorf("error = %v, want reject remote endpoint", err)
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), loopbackS3, true); err != nil {
t.Errorf("allowUntrusted should build the client: %v", err)
}
gcsPathCreds := &remote_pb.RemoteConf{
Name: "poc",
Type: "gcs",
GcsGoogleApplicationCredentials: "/etc/hostname",
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), gcsPathCreds, false); err == nil {
t.Error("expected a non-credentials file path to be rejected")
} else if !strings.Contains(err.Error(), "reject remote credentials") {
t.Errorf("error = %v, want reject remote credentials", err)
} else if strings.Contains(err.Error(), "/etc/hostname") {
t.Errorf("error must not leak the file path: %v", err)
}
// A file path that points to valid GCS credentials should be accepted.
credsFile := filepath.Join(t.TempDir(), "service-account.json")
validCreds := `{"type":"service_account","token_uri":"https://oauth2.googleapis.com/token","client_email":"sa@example.iam.gserviceaccount.com","private_key":"-----BEGIN PRIVATE KEY-----\nMIIBVwIBADANBgkqhkiG9w0BAQEFAASCAUEwggE9AgEAAkEAxY\n-----END PRIVATE KEY-----\n","private_key_id":"key1"}`
if err := os.WriteFile(credsFile, []byte(validCreds), 0600); err != nil {
t.Fatalf("write creds file: %v", err)
}
gcsFileCreds := &remote_pb.RemoteConf{
Name: "good",
Type: "gcs",
GcsGoogleApplicationCredentials: credsFile,
}
if err := checkGcsCredentials(credsFile); err != nil {
t.Errorf("valid gcs credentials file should pass: %v", err)
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), gcsFileCreds, false); err != nil {
t.Errorf("valid gcs credentials file should build: %v", err)
}
// A nonexistent path must be rejected without leaking the path in the error.
gcsMissingCreds := &remote_pb.RemoteConf{
Name: "missing",
Type: "gcs",
GcsGoogleApplicationCredentials: filepath.Join(t.TempDir(), "does-not-exist.json"),
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), gcsMissingCreds, false); err == nil {
t.Error("expected a nonexistent credentials file to be rejected")
} else if strings.Contains(err.Error(), "does-not-exist") {
t.Errorf("error must not leak the file path: %v", err)
}
}
// TestValidateRemoteConfForLoad confirms the load-time validator (injected into
// the filer's FilerRemoteStorage) rejects a RemoteConf whose endpoint resolves
// to a blocked address, while allowUntrusted skips the check. A conf whose type
// dials a fixed provider host (no caller-supplied endpoint) passes.
func TestValidateRemoteConfForLoad(t *testing.T) {
loopbackS3 := &remote_pb.RemoteConf{
Name: "poc",
Type: "s3",
S3Endpoint: "http://127.0.0.1:8000",
S3Region: "us-east-1",
}
if err := ValidateRemoteConfForLoad(context.Background(), loopbackS3, false); err == nil {
t.Error("expected a loopback s3 endpoint to be rejected at load")
} else if !strings.Contains(err.Error(), "reject remote endpoint") {
t.Errorf("error = %v, want reject remote endpoint", err)
}
// allowUntrusted mirrors the volume server opt-out.
if err := ValidateRemoteConfForLoad(context.Background(), loopbackS3, true); err != nil {
t.Errorf("allowUntrusted should accept the conf: %v", err)
}
// A non-S3-compatible type with no caller-supplied endpoint dials a fixed
// provider host, so there is nothing caller-influenced to deny.
fixedHost := &remote_pb.RemoteConf{Name: "fixed", Type: "gcs"}
if err := ValidateRemoteConfForLoad(context.Background(), fixedHost, false); err != nil {
t.Errorf("fixed-host provider should pass: %v", err)
}
// nil conf is a no-op.
if err := ValidateRemoteConfForLoad(context.Background(), nil, false); err != nil {
t.Errorf("nil conf should be a no-op: %v", err)
}
// A hostname endpoint is not resolved at load time (DNS is left to the
// build-time guard at dial), so it must pass even if it would resolve to a
// blocked address. This prevents transient DNS failures from disabling
// working mounts during /etc/remote reload.
hostnameS3 := &remote_pb.RemoteConf{
Name: "host",
Type: "s3",
S3Endpoint: "http://internal.example.com",
S3Region: "us-east-1",
}
if err := ValidateRemoteConfForLoad(context.Background(), hostnameS3, false); err != nil {
t.Errorf("hostname endpoint should pass at load (DNS deferred to dial): %v", err)
}
// A standard AWS S3 config with no custom endpoint (empty S3Endpoint) has
// no caller-supplied endpoint to guard — the AWS SDK derives the regional
// endpoint. Both the load-time validator and the build-time guard must
// accept it so standard AWS S3 mounts keep working.
standardS3 := &remote_pb.RemoteConf{
Name: "aws",
Type: "s3",
S3Region: "us-east-1",
}
if err := ValidateRemoteConfForLoad(context.Background(), standardS3, false); err != nil {
t.Errorf("standard AWS S3 (empty endpoint) should pass: %v", err)
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), standardS3, false); err != nil {
t.Errorf("standard AWS S3 (empty endpoint) should build: %v", err)
}
// A non-s3 S3-compatible type with an empty endpoint is a misconfiguration
// (the AWS SDK would derive an AWS endpoint). The guard must reject it
// rather than fall through to the unguarded cache.
aliyunNoEndpoint := &remote_pb.RemoteConf{
Name: "aliyun",
Type: "aliyun",
AliyunRegion: "cn-hangzhou",
}
if err := ValidateRemoteConfForLoad(context.Background(), aliyunNoEndpoint, false); err == nil {
t.Error("aliyun with empty endpoint should be rejected at load")
}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), aliyunNoEndpoint, false); err == nil {
t.Error("aliyun with empty endpoint should be rejected by the guard")
}
}
// TestGuardedPeerHTTPClientCarriesClusterTLS confirms the replica-upload
// client borrows the shared global client's TLS configuration — an https
// volume server expects the https.client cert and CA — while the remote
// storage client stays on a plain transport.
func TestGuardedPeerHTTPClientCarriesClusterTLS(t *testing.T) {
globalTransport := util_http.GetGlobalHttpClient().GetClientTransport()
original := globalTransport.TLSClientConfig
globalTransport.TLSClientConfig = &tls.Config{ServerName: "volume-peer.test"}
t.Cleanup(func() { globalTransport.TLSClientConfig = original })
peer := newGuardedPeerHTTPClient("10.0.0.1:8080")
if peer.Transport.(*http.Transport).TLSClientConfig != globalTransport.TLSClientConfig {
t.Error("peer upload client should share the global TLS config")
}
if peer.CheckRedirect == nil {
t.Error("peer upload client should share the global redirect policy")
}
if remote := newGuardedHTTPClient("10.0.0.1:8080").Transport.(*http.Transport); remote.TLSClientConfig != nil {
t.Error("remote storage client should not carry cluster TLS config")
}
}
// peerIPv4 returns a non-loopback IPv4 of this host — the only kind of
// address the peer dialer connects to — or skips the test when none exists.
func peerIPv4(t *testing.T) net.IP {
t.Helper()
addrs, err := net.InterfaceAddrs()
if err != nil {
t.Skipf("list interface addresses: %v", err)
}
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ip := ipnet.IP.To4(); ip != nil && !ip.IsLoopback() && !ip.IsLinkLocalUnicast() {
return ip
}
}
t.Skip("no non-loopback IPv4 for the peer dialer to reach")
return nil
}
// peerTLSServer runs handler over TLS on a non-loopback IPv4 under a
// self-signed cert carrying that IP, and returns its host:port plus a pool
// trusting the cert.
func peerTLSServer(t *testing.T, handler http.Handler) (string, *x509.CertPool) {
t.Helper()
peerIP := peerIPv4(t)
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("generate key: %v", err)
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "volume-peer"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
IPAddresses: []net.IP{peerIP},
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
t.Fatalf("create certificate: %v", err)
}
parsed, err := x509.ParseCertificate(der)
if err != nil {
t.Fatalf("parse certificate: %v", err)
}
pool := x509.NewCertPool()
pool.AddCert(parsed)
listener, err := net.Listen("tcp", net.JoinHostPort(peerIP.String(), "0"))
if err != nil {
t.Skipf("bind non-loopback %s: %v", peerIP, err)
}
server := &httptest.Server{
Listener: listener,
Config: &http.Server{Handler: handler},
TLS: &tls.Config{Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}}},
}
server.StartTLS()
t.Cleanup(server.Close)
return listener.Addr().String(), pool
}
// usePeerCA points the shared global client's transport at pool for the test.
func usePeerCA(t *testing.T, pool *x509.CertPool) {
t.Helper()
transport := util_http.GetGlobalHttpClient().GetClientTransport()
original := transport.TLSClientConfig
transport.TLSClientConfig = &tls.Config{RootCAs: pool}
t.Cleanup(func() { transport.TLSClientConfig = original })
}
// TestGuardedPeerHTTPClientUploadsOverTLS sends a real replicate upload
// through the guarded peer client to a TLS server — the global client's CA
// config and the https scheme must carry it, so a hardcoded http:// URL
// could never reach the handler.
func TestGuardedPeerHTTPClientUploadsOverTLS(t *testing.T) {
var sawReplicate atomic.Bool
addr, pool := peerTLSServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.TLS == nil {
t.Error("peer upload arrived over plaintext")
}
sawReplicate.Store(r.URL.Query().Get("type") == "replicate")
io.Copy(io.Discard, r.Body)
w.WriteHeader(http.StatusNoContent)
}))
usePeerCA(t, pool)
uploader := operation.NewUploaderWithHttpClient(newGuardedPeerHTTPClient(addr))
data := []byte("needle-data")
result, err := uploader.UploadData(t.Context(), data, &operation.UploadOption{
UploadUrl: "https://" + addr + "/1,01637037d6?type=replicate",
IsReplication: true,
})
if err != nil {
t.Fatalf("upload to TLS peer: %v", err)
}
if !sawReplicate.Load() {
t.Error("peer never saw a ?type=replicate upload")
}
if result == nil || result.Size != uint32(len(data)) {
t.Errorf("unexpected upload result %+v", result)
}
}
// TestFetchAndWriteReplicaBoundsASilentPeer: a replica that accepts the
// connection and never answers fails the write at the bound.
func TestFetchAndWriteReplicaBoundsASilentPeer(t *testing.T) {
listener, err := net.Listen("tcp", net.JoinHostPort(peerIPv4(t).String(), "0"))
if err != nil {
t.Fatal(err)
}
defer listener.Close()
defer func(original time.Duration) { fetchAndWriteReplicaTimeout = original }(fetchAndWriteReplicaTimeout)
fetchAndWriteReplicaTimeout = time.Second
addr := listener.Addr().String()
uploader := operation.NewUploaderWithHttpClient(newGuardedPeerHTTPClient(addr))
done := make(chan error, 1)
go func() {
done <- fetchAndWriteReplica(t.Context(), uploader, []byte("needle-data"), &operation.UploadOption{
UploadUrl: "http://" + addr + "/1,01637037d6?type=replicate",
IsReplication: true,
})
}()
select {
case err := <-done:
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("silent replica: got %v, want %v", err, context.DeadlineExceeded)
}
case <-time.After(10 * time.Second):
t.Fatal("the write to a silent replica is still waiting")
}
}
func TestFetchAndWriteReplicaBoundGrowsWithTheChunk(t *testing.T) {
const mib = replicaMinBytesPerSec
for _, tc := range []struct {
size int
want time.Duration
}{
{0, fetchAndWriteReplicaTimeout},
{mib / 2, fetchAndWriteReplicaTimeout + 500*time.Millisecond},
{1024 * mib, fetchAndWriteReplicaTimeout + 1024*time.Second},
} {
if got := fetchAndWriteReplicaBound(tc.size); got != tc.want {
t.Errorf("fetchAndWriteReplicaBound(%d) = %v, want %v", tc.size, got, tc.want)
}
}
}
// TestGuardedPeerHTTPClientRejectsHTTPSDowngrade pins the downgrade guard the
// global client carries: an HTTPS peer answering with a redirect to a
// reachable http server must fail the upload rather than resend the body and
// auth header in plaintext. The plaintext target sits on an address the peer
// dialer accepts, so only the redirect policy can stop the resend.
func TestGuardedPeerHTTPClientRejectsHTTPSDowngrade(t *testing.T) {
var sawPlaintext atomic.Bool
plaintext := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sawPlaintext.Store(true)
io.Copy(io.Discard, r.Body)
w.WriteHeader(http.StatusNoContent)
}))
listener, err := net.Listen("tcp", net.JoinHostPort(peerIPv4(t).String(), "0"))
if err != nil {
t.Skipf("bind non-loopback plaintext target: %v", err)
}
plaintext.Listener = listener
plaintext.Start()
defer plaintext.Close()
var sawUpload atomic.Bool
addr, pool := peerTLSServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sawUpload.Store(true)
io.Copy(io.Discard, r.Body)
http.Redirect(w, r, "http://"+listener.Addr().String()+"/latest/meta-data", http.StatusTemporaryRedirect)
}))
usePeerCA(t, pool)
uploader := operation.NewUploaderWithHttpClient(newGuardedPeerHTTPClient(addr))
if _, err := uploader.UploadData(t.Context(), []byte("needle-data"), &operation.UploadOption{
UploadUrl: "https://" + addr + "/1,01637037d6?type=replicate",
IsReplication: true,
MaxAttempts: 1,
}); err == nil {
t.Error("HTTPS→HTTP redirect must fail the upload, not resend the body in plaintext")
}
if sawPlaintext.Load() {
t.Error("redirect resend reached the plaintext server")
}
if !sawUpload.Load() {
t.Error("HTTPS peer never saw the original upload")
}
}
// guardedCacheTest empties the process-wide remote client cache before and
// after a test and stubs DNS with a lookup counter.
func guardedCacheTest(t *testing.T, ips map[string][]net.IP) *atomic.Int64 {
t.Helper()
remote_storage.ResetRemoteStorageClients()
t.Cleanup(remote_storage.ResetRemoteStorageClients)
original := netguard.LookupIPAddr
t.Cleanup(func() { netguard.LookupIPAddr = original })
var lookups atomic.Int64
stub := stubLookup(t, ips)
netguard.LookupIPAddr = func(ctx context.Context, host string) ([]net.IPAddr, error) {
lookups.Add(1)
return stub(ctx, host)
}
return &lookups
}
// The same configuration gets the same client; a rotated key a new one; a loopback endpoint under a cached name is
// still refused (it is another configuration: a miss, checked when it is built).
func TestBuildGuardedRemoteStorageClientReusesOneClientPerConf(t *testing.T) {
guardedCacheTest(t, nil)
conf := &remote_pb.RemoteConf{
Name: "reuse",
Type: "s3",
S3Endpoint: "https://93.184.216.34", // a public address literal: validated without DNS
S3AccessKey: "k",
S3SecretKey: "s",
S3Region: "us-east-1",
}
first, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if err != nil {
t.Fatalf("build: %v", err)
}
for i := 0; i < 5; i++ {
again, err := BuildGuardedRemoteStorageClient(context.Background(), proto.Clone(conf).(*remote_pb.RemoteConf), false)
if err != nil {
t.Fatalf("build again: %v", err)
}
if again != first {
t.Fatalf("call %d built a new client for the same configuration", i)
}
}
rotated := proto.Clone(conf).(*remote_pb.RemoteConf)
rotated.S3SecretKey = "s2"
if next, err := BuildGuardedRemoteStorageClient(context.Background(), rotated, false); err != nil || next == first {
t.Errorf("a changed configuration (a rotated key) must get a new client (err %v)", err)
}
loop := proto.Clone(conf).(*remote_pb.RemoteConf)
loop.S3Endpoint = "http://127.0.0.1:8000"
if _, err := BuildGuardedRemoteStorageClient(context.Background(), loop, false); err == nil ||
!strings.Contains(err.Error(), "reject remote endpoint") {
t.Errorf("a loopback endpoint under a cached name must still be rejected, got %v", err)
}
}
// The endpoint's DNS is checked when a client is built (a cache miss), not on every call; the guarded dialer still
// checks every new connection.
func TestBuildGuardedRemoteStorageClientResolvesOnlyOnAMiss(t *testing.T) {
lookups := guardedCacheTest(t, map[string][]net.IP{"s3.example.test": {net.ParseIP("52.216.10.10")}})
conf := &remote_pb.RemoteConf{Name: "dns", Type: "s3", S3Endpoint: "https://s3.example.test",
S3AccessKey: "k", S3SecretKey: "s", S3Region: "us-east-1"}
for i := 0; i < 10; i++ {
if _, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err != nil {
t.Fatalf("build: %v", err)
}
}
if n := lookups.Load(); n != 1 {
t.Fatalf("%d DNS lookups for 10 calls with one configuration, want 1", n)
}
}
// The shared transport keeps a burst's connections for reuse and caps them per host (WEED_REMOTE_MAX_CONNS_PER_HOST).
func TestGuardedHTTPClientBoundsConnectionsPerHost(t *testing.T) {
tr := newGuardedHTTPClient("https://93.184.216.34").Transport.(*http.Transport)
if tr.MaxIdleConnsPerHost < 32 || tr.MaxConnsPerHost != 64 {
t.Fatalf("MaxIdleConnsPerHost %d (want >= 32), MaxConnsPerHost %d (want 64)", tr.MaxIdleConnsPerHost, tr.MaxConnsPerHost)
}
t.Setenv("WEED_REMOTE_MAX_CONNS_PER_HOST", "8")
if got := newGuardedHTTPClient("https://93.184.216.34").Transport.(*http.Transport).MaxConnsPerHost; got != 8 {
t.Fatalf("MaxConnsPerHost %d with WEED_REMOTE_MAX_CONNS_PER_HOST=8", got)
}
}
// A gcs credentials file is part of the key by its content: the same file shares one client, a changed file builds anew.
func TestBuildGuardedRemoteStorageClientGcsFileCredentialsByContent(t *testing.T) {
guardedCacheTest(t, map[string][]net.IP{"oauth2.googleapis.com": {net.ParseIP("142.250.0.95")}})
credsFile := filepath.Join(t.TempDir(), "service-account.json")
write := func(email string) {
creds := `{"type":"service_account","token_uri":"https://oauth2.googleapis.com/token","client_email":"` + email + `","private_key":"-----BEGIN PRIVATE KEY-----\nMIIBVwIBADANBgkqhkiG9w0BAQEFAASCAUEwggE9AgEAAkEAxY\n-----END PRIVATE KEY-----\n","private_key_id":"key1"}`
if err := os.WriteFile(credsFile, []byte(creds), 0600); err != nil {
t.Fatal(err)
}
}
write("sa@example.iam.gserviceaccount.com")
conf := &remote_pb.RemoteConf{Name: "gcsfile", Type: "gcs", GcsGoogleApplicationCredentials: credsFile}
first, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if err != nil {
t.Fatalf("build: %v", err)
}
again, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if again != first {
t.Fatal("the same credentials file must share one client")
}
write("other@example.iam.gserviceaccount.com")
if next, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err != nil || next == first {
t.Fatalf("a changed credentials file must build a new client (err %v)", err)
}
}
// An s3-compatible remote without keys (the SDK's default chain) is keyed by the shared credentials file and the AWS_*
// variables: unchanged, one client; changed, a new one.
func TestBuildGuardedRemoteStorageClientS3DefaultChainByCredentials(t *testing.T) {
guardedCacheTest(t, nil)
shared := filepath.Join(t.TempDir(), "credentials")
if err := os.WriteFile(shared, []byte("[default]\naws_access_key_id = a\naws_secret_access_key = b\n"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("AWS_SHARED_CREDENTIALS_FILE", shared)
conf := &remote_pb.RemoteConf{Name: "chain", Type: "s3", S3Endpoint: "https://93.184.216.34", S3Region: "us-east-1"}
first, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if err != nil {
t.Fatalf("build: %v", err)
}
if again, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false); again != first {
t.Fatal("unchanged credentials must share one client")
}
if err := os.WriteFile(shared, []byte("[default]\naws_access_key_id = c\naws_secret_access_key = d\n"), 0600); err != nil {
t.Fatal(err)
}
if next, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err != nil || next == first {
t.Fatalf("a changed shared credentials file must build a new client (err %v)", err)
}
awsConfig := filepath.Join(t.TempDir(), "config")
if err := os.WriteFile(awsConfig, []byte("[default]\nregion = us-east-1\n"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("AWS_CONFIG_FILE", awsConfig)
withConfig, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if err != nil {
t.Fatalf("build: %v", err)
}
if err := os.WriteFile(awsConfig, []byte("[default]\nregion = us-west-2\n"), 0600); err != nil {
t.Fatal(err)
}
if next, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false); next == withConfig {
t.Fatal("a changed shared config file must build a new client")
}
t.Setenv("AWS_PROFILE", "other")
prev, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
t.Setenv("AWS_PROFILE", "third")
if next, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false); next == prev {
t.Fatal("a changed AWS_PROFILE must build a new client")
}
}
// An s3-compatible remote whose keys come from its type's environment fallback is keyed by them too: a changed variable
// builds a new client even though nothing in the configuration changed.
func TestBuildGuardedRemoteStorageClientS3EnvironmentCredentials(t *testing.T) {
guardedCacheTest(t, nil)
t.Setenv("ALICLOUD_ACCESS_KEY_ID", "a")
t.Setenv("ALICLOUD_ACCESS_KEY_SECRET", "b")
conf := &remote_pb.RemoteConf{Name: "env", Type: "aliyun", AliyunEndpoint: "https://93.184.216.34"}
first, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false)
if err != nil {
t.Fatalf("build: %v", err)
}
if again, _ := BuildGuardedRemoteStorageClient(context.Background(), conf, false); again != first {
t.Fatal("unchanged environment credentials must share one client")
}
t.Setenv("ALICLOUD_ACCESS_KEY_SECRET", "c")
if next, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err != nil || next == first {
t.Fatalf("a changed environment credential must build a new client (err %v)", err)
}
}
// A keyed s3 remote reads the credentials mode too, so an unknown value fails its next build instead of returning the cached client.
func TestBuildGuardedRemoteStorageClientS3KeysFollowTheCredentialsMode(t *testing.T) {
guardedCacheTest(t, nil)
t.Setenv("WEED_REMOTE_S3_CREDENTIALS", "")
conf := &remote_pb.RemoteConf{Name: "keyed", Type: "s3", S3AccessKey: "a", S3SecretKey: "b", S3Region: "us-east-1", S3Endpoint: "https://93.184.216.34"}
if _, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err != nil {
t.Fatalf("build: %v", err)
}
t.Setenv("WEED_REMOTE_S3_CREDENTIALS", "bogus")
if _, err := BuildGuardedRemoteStorageClient(context.Background(), conf, false); err == nil {
t.Fatal("an unknown credentials mode must fail the build of a cached keyed client")
}
}
func TestGuardedCredentialsVersionFollowsTheWorkloadSource(t *testing.T) {
conf := &remote_pb.RemoteConf{Type: "s3", Name: "r", S3Region: "us-east-1"}
t.Setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://169.254.170.23/v1/credentials")
for _, k := range []string{"AWS_CONTAINER_AUTHORIZATION_TOKEN", "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE",
"AWS_ROLE_SESSION_NAME", "AWS_REGION", "AWS_DEFAULT_REGION", "AWS_STS_REGIONAL_ENDPOINTS",
"AWS_EC2_METADATA_SERVICE_ENDPOINT", "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE",
"AWS_EC2_METADATA_DISABLED", "AWS_EC2_METADATA_V1_DISABLED",
"AWS_USE_FIPS_ENDPOINT", "AWS_USE_DUALSTACK_ENDPOINT", "AWS_CA_BUNDLE"} {
t.Setenv(k, "")
before := guardedCredentialsVersion(conf)
t.Setenv(k, "changed")
if guardedCredentialsVersion(conf) == before {
t.Errorf("%s changed, the guarded client's version did not", k)
}
t.Setenv(k, "")
}
}