Files
seaweedfs/weed/command/mini_admin_auth_test.go
T
Chris Lu 8ac5133abe admin: default to port 12535, keep 23646 as a best-effort compat listener (#11816)
* admin: default to port 12535, keep 23646 as a best-effort compat listener

The default admin HTTP port 23646 derives a worker gRPC port of 33646
(port + GrpcPortOffset), inside the Linux default ephemeral range
(32768+). One of our own startup dials can claim 33646 before the admin
binds it, killing startup. This is the same failure #10209 fixed for
the test port allocator, still reachable via the mini default (#10838)
and via standalone `weed admin` defaults.

Move the default admin port to 12535 (gRPC 22535) everywhere, keeping
the http+10000 convention so worker -admin derivation, the Rust worker
and host:http.grpc addressing keep working:

- weed admin -port, weed mini -admin.port
- weed worker -admin, the plugin worker fallback, Rust worker and
  weed-lance-worker defaults
- help text, README, helm values, docker compose, tests

So old workers, bookmarks and scripts keep working, admin also serves
the same handler and the worker gRPC server on the previous default
(-port.legacy / -admin.port.legacy, default 23646, 0 disables). The
legacy gRPC port binds best-effort: it lives in the ephemeral range, so
a failed bind only warns instead of reintroducing the startup failure.
weed mini reserves it early like the primary port and keeps it out of
its own port relocation. grpc.Server tracks every listener handed to
Serve, so the compat listener stops with the primary.

Also warn when an explicitly configured admin port puts the derived
gRPC port at or above the ephemeral floor, the other fix suggested in
the issue.

Supersedes the pinned-grpc approach in #11814: pinning -admin.port.grpc
off the +10000 convention breaks external workers that derive the admin
gRPC port from the HTTP address. ClickHouse/ClickHouse#115512 pins
-admin.port on their side; this makes that pin unnecessary.

Fixes #10838

* admin: let services win the legacy ports, close compat listener on failure

Review findings on the compat listeners:

- Do not put the legacy ports in mini's reservedPorts: an explicit
  -s3.port=23646 or -master.port.grpc=33646 must be honored, not rejected
  or stolen by the early reservation. Mini now checks the finalized port
  set and skips only the conflicting compat listener, so HTTP and gRPC
  compat degrade independently and the service keeps its port.
- Also skip the legacy HTTP listener when a mini service owns the port:
  a wildcard-bound service can still leave loopback bindable, which would
  let the compat listener shadow it for loopback clients.
- Close legacyServer's raw listener in the deferred cleanup: Serve may
  not have registered it yet when startAdminServer returns early.
- Helm: add admin.legacyPort (default 23646) and expose the legacy
  http/grpc ports through the admin Service, container ports and
  NetworkPolicy so older workers resolved via the Service keep working.

* helm: only pass -port.legacy when it differs from the binary default

ct install deploys the released image with the new chart, and 4.48 does
not know -port.legacy, so the admin pod crash-loops. Omitting the flag at
the default value is behavior-identical on new binaries (their default
is also 23646) and keeps older releases installable; legacyPort: 0 or a
custom port still renders the flag.
2026-10-18 21:21:05 -07:00

287 lines
7.9 KiB
Go

package command
import (
"net"
"strconv"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb"
)
// weed mini must resolve admin credentials from security.toml [admin] /
// WEED_ADMIN_* env vars the same way the standalone `weed admin` command does.
// This exercises the production fallback so the flag-name -> viper-key mapping
// stays correct, in particular the read-only keys where the mini flag
// (admin.readOnlyUser) and viper key (admin.readonly.user) differ.
// TestApplyMiniAdminCredentialFallbackFromEnv verifies those environment
// fallbacks without starting the full mini cluster.
func TestApplyMiniAdminCredentialFallbackFromEnv(t *testing.T) {
adminUser, adminPassword, readOnlyUser, readOnlyPassword := "admin", "", "", ""
options := &AdminOptions{
adminUser: &adminUser,
adminPassword: &adminPassword,
readOnlyUser: &readOnlyUser,
readOnlyPassword: &readOnlyPassword,
}
t.Setenv("WEED_ADMIN_USER", "env-admin")
t.Setenv("WEED_ADMIN_PASSWORD", "env-secret")
t.Setenv("WEED_ADMIN_READONLY_USER", "env-ro")
t.Setenv("WEED_ADMIN_READONLY_PASSWORD", "env-ro-secret")
applyMiniAdminCredentialFallback(options)
checks := []struct {
name string
got string
want string
}{
{"adminUser", *options.adminUser, "env-admin"},
{"adminPassword", *options.adminPassword, "env-secret"},
{"readOnlyUser", *options.readOnlyUser, "env-ro"},
{"readOnlyPassword", *options.readOnlyPassword, "env-ro-secret"},
}
for _, c := range checks {
if c.got != c.want {
t.Errorf("%s = %q, want %q", c.name, c.got, c.want)
}
}
}
// TestMiniAdminBindIP covers the authentication-dependent HTTP bind policy.
func TestMiniAdminBindIP(t *testing.T) {
tests := []struct {
name string
requestedIP string
passwordConfigured bool
mtlsConfigured bool
allowInsecure bool
want string
}{
{
name: "unauthenticated wildcard binds to loopback",
requestedIP: "0.0.0.0",
want: "127.0.0.1",
},
{
name: "unauthenticated IPv6 wildcard binds to loopback",
requestedIP: "::",
want: "127.0.0.1",
},
{
name: "existing loopback bind is preserved",
requestedIP: "127.0.0.1",
want: "127.0.0.1",
},
{
name: "password permits requested bind",
requestedIP: "0.0.0.0",
passwordConfigured: true,
want: "0.0.0.0",
},
{
name: "mTLS permits requested bind",
requestedIP: "0.0.0.0",
mtlsConfigured: true,
want: "0.0.0.0",
},
{
name: "explicit insecure opt-out permits requested bind",
requestedIP: "0.0.0.0",
allowInsecure: true,
want: "0.0.0.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := miniAdminBindIP(
tt.requestedIP,
tt.passwordConfigured,
tt.mtlsConfigured,
tt.allowInsecure,
)
if got != tt.want {
t.Fatalf("miniAdminBindIP() = %q, want %q", got, tt.want)
}
})
}
}
// TestMiniAdminWorkerBindDefaultsToLoopback protects the worker control-plane default.
func TestMiniAdminWorkerBindDefaultsToLoopback(t *testing.T) {
if miniAdminWorkerBindIP == nil {
t.Fatal("mini Admin worker bind flag is not initialized")
}
if got, want := *miniAdminWorkerBindIP, "127.0.0.1"; got != want {
t.Fatalf("default mini Admin worker bind IP = %q, want %q", got, want)
}
}
// TestListenMiniAdminWorkerUsesRequestedAddress verifies the production
// listener is actually restricted to the configured loopback address.
func TestListenMiniAdminWorkerUsesRequestedAddress(t *testing.T) {
listener, err := listenMiniAdminWorker("127.0.0.1", 0)
if err != nil {
t.Fatalf("listen for mini Admin worker: %v", err)
}
t.Cleanup(func() {
_ = listener.Close()
})
address, ok := listener.Addr().(*net.TCPAddr)
if !ok {
t.Fatalf("listener address type = %T, want *net.TCPAddr", listener.Addr())
}
if !address.IP.IsLoopback() {
t.Fatalf("listener IP = %s, want loopback", address.IP)
}
}
// TestMiniAdminWorkerAddressUsesFinalGrpcPort verifies that local workers do
// not have to discover or infer a custom Admin worker gRPC port.
func TestMiniAdminWorkerAddressUsesFinalGrpcPort(t *testing.T) {
tests := []struct {
name string
ip string
want string
wantGrpc string
}{
{
name: "IPv4",
ip: "127.0.0.1",
want: "127.0.0.1:23646.34567",
wantGrpc: "127.0.0.1:34567",
},
{
name: "IPv6",
ip: "::1",
want: "::1:23646.34567",
wantGrpc: "[::1]:34567",
},
{
name: "IPv4 wildcard dials loopback",
ip: "0.0.0.0",
want: "127.0.0.1:23646.34567",
wantGrpc: "127.0.0.1:34567",
},
{
name: "IPv6 wildcard dials loopback",
ip: "::",
want: "::1:23646.34567",
wantGrpc: "[::1]:34567",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
address := miniAdminWorkerAddress(tt.ip, 23646, 34567)
if address != tt.want {
t.Fatalf("miniAdminWorkerAddress() = %q, want %q", address, tt.want)
}
if got := pb.ServerToGrpcAddress(address); got != tt.wantGrpc {
t.Fatalf("pb.ServerToGrpcAddress() = %q, want %q", got, tt.wantGrpc)
}
})
}
}
// TestMiniAdminWorkerDialIP verifies wildcard listener addresses are converted
// into valid same-family destinations without rewriting specific addresses.
func TestMiniAdminWorkerDialIP(t *testing.T) {
tests := []struct {
bindIP string
want string
}{
{bindIP: "0.0.0.0", want: "127.0.0.1"},
{bindIP: "::", want: "::1"},
{bindIP: "[::]", want: "::1"},
{bindIP: "192.0.2.10", want: "192.0.2.10"},
{bindIP: "2001:db8::10", want: "2001:db8::10"},
{bindIP: "worker.internal", want: "worker.internal"},
}
for _, tt := range tests {
t.Run(tt.bindIP, func(t *testing.T) {
if got := miniAdminWorkerDialIP(tt.bindIP); got != tt.want {
t.Fatalf("miniAdminWorkerDialIP(%q) = %q, want %q", tt.bindIP, got, tt.want)
}
})
}
}
// TestMiniAdminAdvertisedIP verifies that the welcome message uses the
// selected loopback address but replaces wildcard binds with a reachable host.
func TestMiniAdminAdvertisedIP(t *testing.T) {
oldMiniIP := miniIp
oldAdminIP := miniAdminOptions.ip
t.Cleanup(func() {
miniIp = oldMiniIP
miniAdminOptions.ip = oldAdminIP
})
detectedIP := "192.0.2.10"
miniIp = &detectedIP
tests := []struct {
name string
adminIP string
expected string
}{
{
name: "selected loopback",
adminIP: "127.0.0.1",
expected: "127.0.0.1",
},
{
name: "IPv4 wildcard",
adminIP: "0.0.0.0",
expected: detectedIP,
},
{
name: "IPv6 wildcard",
adminIP: "::",
expected: detectedIP,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adminIP := tt.adminIP
miniAdminOptions.ip = &adminIP
if got := miniAdminAdvertisedIP(); got != tt.expected {
t.Fatalf("miniAdminAdvertisedIP() = %q, want %q", got, tt.expected)
}
})
}
}
func TestDefaultAdminGrpcPortBelowEphemeralFloor(t *testing.T) {
if grpcPort := defaultAdminHTTPPort + GrpcPortOffset; grpcPort >= linuxEphemeralPortFloor {
t.Fatalf("default admin HTTP port %d derives gRPC port %d, which must stay below ephemeral floor %d",
defaultAdminHTTPPort, grpcPort, linuxEphemeralPortFloor)
}
for _, f := range []struct {
name string
flagName string
def string
}{
{"mini", "admin.port", strconv.Itoa(defaultAdminHTTPPort)},
{"mini", "admin.port.legacy", strconv.Itoa(defaultAdminHTTPLegacyPort)},
{"admin", "port", strconv.Itoa(defaultAdminHTTPPort)},
{"admin", "port.legacy", strconv.Itoa(defaultAdminHTTPLegacyPort)},
} {
cmd := cmdMini
if f.name == "admin" {
cmd = cmdAdmin
}
flag := cmd.Flag.Lookup(f.flagName)
if flag == nil {
t.Fatalf("%s: flag %s not registered", f.name, f.flagName)
}
if flag.DefValue != f.def {
t.Fatalf("%s -%s default = %s, want %s so its gRPC port stays below the ephemeral floor", f.name, f.flagName, flag.DefValue, f.def)
}
}
}