mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
master: apply the whitelist to management gRPC RPCs (#13022)
* master: gate operator-only management RPCs on the whitelist CollectionDelete, DisableVacuum, EnableVacuum, VolumeGrow, ResumeVolumeGrowth, LeaseAdminToken and ReleaseAdminToken now call checkGrpcAdminAuth, matching the whitelist their HTTP counterparts already enforce. VolumeGrow stays open to the in-process growth loop, and callers on the local gRPC socket of weed server/mini are local. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: admit unknown masters over KeepConnected only from the whitelist A KeepConnected registration as a master pulls that address into the raft quorum. Accept it from any caller only for this master and its -peers; any other master address now needs a whitelisted caller, the same check RaftAddServer applies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: test that every RPC is gated or listed with a reason Port the volume server's coverage test so a new master RPC cannot skip checkGrpcAdminAuth without an explicit exemption. The proto and AST helpers now take the proto file and receiver name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: admit callers on the local gRPC socket weed server and weed mini route same-host gRPC dials through a unix socket, whose peer address carries no IP. With -whiteList set the volume server refused the in-process master's AllocateVolume, so no volume could be grown. The Rust volume server serves gRPC over TCP only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: match a KeepConnected master against configured peers by gRPC endpoint Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pb: bind the local gRPC socket for the owner only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: warn when a deleted bucket's collection is left on the master Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pb: bind the local gRPC socket in a private directory before moving it into place Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: report an unconfirmed bucket collection delete as such Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a00dbff13b
commit
d2276f531e
15 files changed
+381
-22
No files matched your search
@@ -172,7 +172,9 @@ func (f *Filer) DeleteEntryMetaAndData(ctx context.Context, p util.FullPath, isR
|
||||
// the same, or a master that is down parks this handler indefinitely and
|
||||
// every client retry behind it parks another
|
||||
collectionCtx, cancelCollection := context.WithTimeout(context.WithoutCancel(ctx), collectionDeleteTimeout)
|
||||
f.DoDeleteCollection(collectionCtx, collectionName)
|
||||
if err := f.DoDeleteCollection(collectionCtx, collectionName); err != nil {
|
||||
glog.Warningf("bucket %s is deleted but the deletion of its collection %s is not confirmed: %v", entry.Name(), collectionName, err)
|
||||
}
|
||||
cancelCollection()
|
||||
}
|
||||
// drop bucket-labeled series held by this process; the S3 gateway
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -163,7 +164,7 @@ func ServeGrpcOnLocalSocket(grpcServer *grpc.Server, grpcPort int) {
|
||||
if socketPath == "" {
|
||||
return
|
||||
}
|
||||
listener := ListenOnLocalSocket(socketPath)
|
||||
listener := listenOnLocalGrpcSocket(socketPath)
|
||||
if listener == nil {
|
||||
return
|
||||
}
|
||||
@@ -171,6 +172,33 @@ func ServeGrpcOnLocalSocket(grpcServer *grpc.Server, grpcPort int) {
|
||||
go serveGrpcOnLocalListener(grpcServer, socketPath, listener)
|
||||
}
|
||||
|
||||
// listenOnLocalGrpcSocket binds the socket for this process only: gRPC
|
||||
// servers trust their local socket callers as processes on this host. It is
|
||||
// bound inside a private directory and moved into place once restricted, so
|
||||
// other users can never connect to it.
|
||||
func listenOnLocalGrpcSocket(socketPath string) net.Listener {
|
||||
dir, err := os.MkdirTemp(filepath.Dir(socketPath), ".sock")
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to create a private directory for Unix socket %s: %v", socketPath, err)
|
||||
return nil
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
privatePath := filepath.Join(dir, "s")
|
||||
listener := ListenOnLocalSocket(privatePath)
|
||||
if listener == nil {
|
||||
return nil
|
||||
}
|
||||
if err = os.Chmod(privatePath, 0600); err == nil {
|
||||
err = os.Rename(privatePath, socketPath)
|
||||
}
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to restrict Unix socket %s: %v", socketPath, err)
|
||||
listener.Close()
|
||||
return nil
|
||||
}
|
||||
return listener
|
||||
}
|
||||
|
||||
// localSocketCheckInterval is how often a served socket path is checked for
|
||||
// removal by something else, such as a temp cleaner aging /tmp.
|
||||
var localSocketCheckInterval = time.Minute
|
||||
@@ -204,7 +232,7 @@ func serveGrpcOnLocalListener(grpcServer *grpc.Server, socketPath string, listen
|
||||
}
|
||||
// Bind before closing the old listener, so a failed bind is
|
||||
// retried on the next check and shutdown still ends this loop.
|
||||
replacement := ListenOnLocalSocket(socketPath)
|
||||
replacement := listenOnLocalGrpcSocket(socketPath)
|
||||
if replacement == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -127,6 +127,53 @@ func TestServeGrpcOnLocalSocketRecreatesRemovedSocket(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeGrpcOnLocalSocketIsOwnerOnly(t *testing.T) {
|
||||
isolateLocalGrpcSockets(t)
|
||||
prevInterval := localSocketCheckInterval
|
||||
localSocketCheckInterval = 10 * time.Millisecond
|
||||
t.Cleanup(func() { localSocketCheckInterval = prevInterval })
|
||||
|
||||
grpcServer, port := startHealthGrpcServer(t)
|
||||
socketDir := localSocketDir(t)
|
||||
socketPath := filepath.Join(socketDir, "grpc.sock")
|
||||
RegisterLocalGrpcSocket("127.0.0.1", port, socketPath)
|
||||
ServeGrpcOnLocalSocket(grpcServer, port)
|
||||
waitForOwnerOnlySocket(t, socketPath)
|
||||
|
||||
if err := os.Remove(socketPath); err != nil {
|
||||
t.Fatalf("remove socket: %v", err)
|
||||
}
|
||||
waitForOwnerOnlySocket(t, socketPath)
|
||||
conn, err := grpc.NewClient("unix:"+socketPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial socket: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if _, err := healthpb.NewHealthClient(conn).Check(ctx, &healthpb.HealthCheckRequest{}); err != nil {
|
||||
t.Fatalf("health check over the socket: %v", err)
|
||||
}
|
||||
if entries, err := os.ReadDir(socketDir); err != nil || len(entries) != 1 {
|
||||
t.Fatalf("socket dir should hold only the socket: %v %v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
func waitForOwnerOnlySocket(t *testing.T, socketPath string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
info, err := os.Lstat(socketPath)
|
||||
if err == nil && info.Mode()&os.ModeSocket != 0 && info.Mode().Perm() == 0600 {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("socket %s is not owner-only: %v %v", socketPath, info, err)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func waitForSocketFile(t *testing.T, socketPath string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ungatedMasterRPCs are the Seaweed master RPCs that run without
|
||||
// checkGrpcAdminAuth, each with the reason it stays open. As on the volume
|
||||
// server the split is by caller: the whitelist holds masters, shell hosts and
|
||||
// workers, while volume servers, filers, gateways and clients are not
|
||||
// necessarily on it.
|
||||
var ungatedMasterRPCs = map[string]string{
|
||||
"SendHeartbeat": "volume servers register and report their volumes",
|
||||
"Assign": "filers, gateways and clients assign file ids",
|
||||
"StreamAssign": "filers assign file ids over a stream",
|
||||
"LookupVolume": "every reader resolves volume locations",
|
||||
"LookupEcVolume": "every reader resolves EC shard locations",
|
||||
"VacuumVolume": "filer remote-cache eviction vacuums the volumes it emptied",
|
||||
"VolumeMarkReadonly": "volume servers report their own read-only state",
|
||||
"GetMasterConfiguration": "volume servers, filers and gateways load cluster settings at startup",
|
||||
"Ping": "liveness probe; targets are limited to known peers",
|
||||
|
||||
"Statistics": "read-only usage, the filer's statfs",
|
||||
"CollectionList": "read-only, filers and S3 gateways list collections",
|
||||
"CollectionStatistics": "read-only, S3 bucket size metrics",
|
||||
"VolumeList": "read-only topology",
|
||||
"VolumeListStream": "read-only topology",
|
||||
"ListClusterNodes": "read-only cluster membership",
|
||||
"GetAdminLockStatus": "read-only admin lock holder",
|
||||
"RaftListClusterServers": "read-only raft membership",
|
||||
}
|
||||
|
||||
func TestMasterAdminAuthCoverage(t *testing.T) {
|
||||
declared := rpcNamesFromProto(t, "master.proto")
|
||||
if len(declared) < 25 {
|
||||
t.Fatalf("parsed only %d RPCs from the proto, expected the full service", len(declared))
|
||||
}
|
||||
|
||||
gated := gatedServerMethods(t, "MasterServer")
|
||||
|
||||
declaredSet := make(map[string]struct{}, len(declared))
|
||||
for _, rpc := range declared {
|
||||
declaredSet[rpc] = struct{}{}
|
||||
_, exempt := ungatedMasterRPCs[rpc]
|
||||
switch {
|
||||
case gated[rpc] && exempt:
|
||||
t.Errorf("%s calls checkGrpcAdminAuth but is also listed in ungatedMasterRPCs", rpc)
|
||||
case !gated[rpc] && !exempt:
|
||||
t.Errorf("%s does not call checkGrpcAdminAuth and is not listed in ungatedMasterRPCs; "+
|
||||
"gate it, or add it with the reason it must stay open", rpc)
|
||||
}
|
||||
}
|
||||
|
||||
var stale []string
|
||||
for rpc := range ungatedMasterRPCs {
|
||||
if _, ok := declaredSet[rpc]; !ok {
|
||||
stale = append(stale, rpc)
|
||||
}
|
||||
}
|
||||
sort.Strings(stale)
|
||||
for _, rpc := range stale {
|
||||
t.Errorf("ungatedMasterRPCs lists %q, which is not an RPC in master.proto", rpc)
|
||||
}
|
||||
}
|
||||
@@ -562,6 +562,12 @@ func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServ
|
||||
}
|
||||
peerAddress := pb.ServerAddress(clientAddress)
|
||||
|
||||
if req.ClientType == cluster.MasterType && !ms.isConfiguredMaster(peerAddress) {
|
||||
if err := ms.checkGrpcAdminAuth(stream.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// buffer by 1 so we don't end up getting stuck writing to stopChan forever
|
||||
stopChan := make(chan bool, 1)
|
||||
|
||||
|
||||
@@ -132,6 +132,9 @@ func (locks *AdminLocks) deleteLockLocked(lockName string) {
|
||||
|
||||
func (ms *MasterServer) LeaseAdminToken(ctx context.Context, req *master_pb.LeaseAdminTokenRequest) (*master_pb.LeaseAdminTokenResponse, error) {
|
||||
resp := &master_pb.LeaseAdminTokenResponse{}
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
if !ms.Topo.IsLeader() {
|
||||
return resp, raft.NotLeaderError
|
||||
@@ -158,6 +161,9 @@ func (ms *MasterServer) LeaseAdminToken(ctx context.Context, req *master_pb.Leas
|
||||
|
||||
func (ms *MasterServer) ReleaseAdminToken(ctx context.Context, req *master_pb.ReleaseAdminTokenRequest) (*master_pb.ReleaseAdminTokenResponse, error) {
|
||||
resp := &master_pb.ReleaseAdminTokenResponse{}
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
// a follower holds no lock state; answering success here would fake a
|
||||
// release while the leader keeps the lock until it expires
|
||||
if !ms.Topo.IsLeader() {
|
||||
@@ -219,6 +225,24 @@ func (ms *MasterServer) isKnownPingTarget(ctx context.Context, target string, ta
|
||||
return false
|
||||
}
|
||||
|
||||
// isConfiguredMaster reports whether addr is this master or one of -peers,
|
||||
// compared by gRPC endpoint since that is what raft admission dials.
|
||||
func (ms *MasterServer) isConfiguredMaster(addr pb.ServerAddress) bool {
|
||||
grpcAddress := addr.ToGrpcAddress()
|
||||
if ms.option != nil && ms.option.Master.ToGrpcAddress() == grpcAddress {
|
||||
return true
|
||||
}
|
||||
if ms.MasterClient == nil {
|
||||
return false
|
||||
}
|
||||
for _, master := range ms.MasterClient.ListCachedMasters() {
|
||||
if master.ToGrpcAddress() == grpcAddress {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (ms *MasterServer) Ping(ctx context.Context, req *master_pb.PingRequest) (resp *master_pb.PingResponse, pingErr error) {
|
||||
resp = &master_pb.PingResponse{
|
||||
StartTimeNs: time.Now().UnixNano(),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -22,11 +21,11 @@ func leaseAdminLock(ms *MasterServer, client string, prev *master_pb.LeaseAdminT
|
||||
if prev != nil {
|
||||
req.PreviousToken, req.PreviousLockTime = prev.Token, prev.LockTsNs
|
||||
}
|
||||
return ms.LeaseAdminToken(context.Background(), req)
|
||||
return ms.LeaseAdminToken(ctxFromHost("127.0.0.1"), req)
|
||||
}
|
||||
|
||||
func releaseAdminLock(ms *MasterServer, lease *master_pb.LeaseAdminTokenResponse) error {
|
||||
_, err := ms.ReleaseAdminToken(context.Background(), &master_pb.ReleaseAdminTokenRequest{
|
||||
_, err := ms.ReleaseAdminToken(ctxFromHost("127.0.0.1"), &master_pb.ReleaseAdminTokenRequest{
|
||||
LockName: "admin", PreviousToken: lease.Token, PreviousLockTime: lease.LockTsNs,
|
||||
})
|
||||
return err
|
||||
|
||||
@@ -39,6 +39,9 @@ func (ms *MasterServer) CollectionList(ctx context.Context, req *master_pb.Colle
|
||||
}
|
||||
|
||||
func (ms *MasterServer) CollectionDelete(ctx context.Context, req *master_pb.CollectionDeleteRequest) (*master_pb.CollectionDeleteResponse, error) {
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ms.Topo.IsLeader() {
|
||||
return nil, raft.NotLeaderError
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"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/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/topology"
|
||||
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
||||
)
|
||||
|
||||
func TestKeepConnected_RejectsUnknownMasterFromUnwhitelistedPeer(t *testing.T) {
|
||||
leader, _ := newInmemRaftLeader(t, "localhost:9333")
|
||||
topo := topology.NewTopology("test", nil, 0, 0, false)
|
||||
topo.HashicorpRaft = leader
|
||||
ms := &MasterServer{
|
||||
option: &MasterOption{Master: "localhost:9333"},
|
||||
Topo: topo,
|
||||
guard: security.NewGuard([]string{"127.0.0.1"}, "", 0, "", 0, "", ""),
|
||||
}
|
||||
stream := &keepConnectedOnce{
|
||||
ctx: ctxFromHost("10.13.37.66"),
|
||||
req: &master_pb.KeepConnectedRequest{ClientType: cluster.MasterType, ClientAddress: "10.99.99.99:19999"},
|
||||
}
|
||||
if err := ms.KeepConnected(stream); status.Code(err) != codes.PermissionDenied {
|
||||
t.Fatalf("expected PermissionDenied, got %v", err)
|
||||
}
|
||||
if servers := leader.GetConfiguration().Configuration().Servers; len(servers) != 1 {
|
||||
t.Fatalf("unknown master was admitted into raft: %v", servers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepConnected_RejectsConfiguredPeerHostOnAnotherGrpcPort(t *testing.T) {
|
||||
leader, _ := newInmemRaftLeader(t, "localhost:9333")
|
||||
topo := topology.NewTopology("test", nil, 0, 0, false)
|
||||
topo.HashicorpRaft = leader
|
||||
peers := map[string]pb.ServerAddress{"10.0.0.2:9333": "10.0.0.2:9333.19333"}
|
||||
ms := &MasterServer{
|
||||
option: &MasterOption{Master: "localhost:9333"},
|
||||
Topo: topo,
|
||||
guard: security.NewGuard([]string{"127.0.0.1"}, "", 0, "", 0, "", ""),
|
||||
MasterClient: wdclient.NewMasterClient(grpc.EmptyDialOption{}, "", cluster.MasterType, "localhost:9333", "", "", *pb.NewServiceDiscoveryFromMap(peers)),
|
||||
}
|
||||
stream := &keepConnectedOnce{
|
||||
ctx: ctxFromHost("10.13.37.66"),
|
||||
req: &master_pb.KeepConnectedRequest{ClientType: cluster.MasterType, ClientAddress: "10.0.0.2:9333.29999"},
|
||||
}
|
||||
if err := ms.KeepConnected(stream); status.Code(err) != codes.PermissionDenied {
|
||||
t.Fatalf("expected PermissionDenied, got %v", err)
|
||||
}
|
||||
if servers := leader.GetConfiguration().Configuration().Servers; len(servers) != 1 {
|
||||
t.Fatalf("caller-chosen endpoint was admitted into raft: %v", servers)
|
||||
}
|
||||
for _, addr := range []pb.ServerAddress{"10.0.0.2:9333", "10.0.0.2:9333.19333", "localhost:9333.19333"} {
|
||||
if !ms.isConfiguredMaster(addr) {
|
||||
t.Errorf("isConfiguredMaster(%q) = false, want true", addr)
|
||||
}
|
||||
}
|
||||
if ms.isConfiguredMaster("10.0.0.2:9333.29999") {
|
||||
t.Errorf("isConfiguredMaster accepted a configured host on another gRPC port")
|
||||
}
|
||||
}
|
||||
|
||||
type keepConnectedOnce struct {
|
||||
grpc.ServerStream
|
||||
ctx context.Context
|
||||
req *master_pb.KeepConnectedRequest
|
||||
sent bool
|
||||
}
|
||||
|
||||
func (s *keepConnectedOnce) Context() context.Context { return s.ctx }
|
||||
|
||||
func (s *keepConnectedOnce) Recv() (*master_pb.KeepConnectedRequest, error) {
|
||||
if s.sent {
|
||||
return nil, io.EOF
|
||||
}
|
||||
s.sent = true
|
||||
return s.req, nil
|
||||
}
|
||||
|
||||
func (s *keepConnectedOnce) Send(*master_pb.KeepConnectedResponse) error { return nil }
|
||||
@@ -16,22 +16,24 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
)
|
||||
|
||||
// checkGrpcAdminAuth authorizes the raft membership RPCs that mutate cluster
|
||||
// quorum. It mirrors the volume server's gate: the caller's peer IP is matched
|
||||
// checkGrpcAdminAuth authorizes the management RPCs that operator tooling
|
||||
// calls. It mirrors the volume server's gate: the caller's peer IP is matched
|
||||
// against the master's -whiteList. With no whitelist configured IsWhiteListed
|
||||
// allows everyone, so default and single-master deployments are unaffected;
|
||||
// operators who set a whitelist get these RPCs locked down to it. The cluster's
|
||||
// own dead-peer eviction no longer dials these RPCs (it uses the local raft
|
||||
// handle), so the only remaining callers are operator tooling.
|
||||
// operators who set a whitelist get these RPCs locked down to it. Callers on
|
||||
// the local gRPC socket of weed server/mini are processes on this host.
|
||||
func (ms *MasterServer) checkGrpcAdminAuth(ctx context.Context) error {
|
||||
if ms.guard == nil {
|
||||
return nil
|
||||
}
|
||||
pr, ok := peer.FromContext(ctx)
|
||||
if !ok {
|
||||
glog.V(0).Infof("gRPC raft admin auth failed: no peer info")
|
||||
glog.V(0).Infof("gRPC master admin auth failed: no peer info")
|
||||
return status.Error(codes.PermissionDenied, "no peer info")
|
||||
}
|
||||
if _, ok := pr.Addr.(*net.UnixAddr); ok {
|
||||
return nil
|
||||
}
|
||||
addr := pr.Addr.String()
|
||||
var host string
|
||||
if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok {
|
||||
@@ -42,7 +44,7 @@ func (ms *MasterServer) checkGrpcAdminAuth(ctx context.Context) error {
|
||||
host = addr
|
||||
}
|
||||
if !ms.guard.IsWhiteListed(host) {
|
||||
glog.V(0).Infof("gRPC raft admin auth failed: %s is not whitelisted (remote: %s)", host, addr)
|
||||
glog.V(0).Infof("gRPC master admin auth failed: %s is not whitelisted (remote: %s)", host, addr)
|
||||
return status.Errorf(codes.PermissionDenied, "not authorized: %s", host)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -81,3 +81,65 @@ func TestRaftMembershipRPCs_NoWhitelistFailsOpen(t *testing.T) {
|
||||
t.Errorf("RaftAddServer: empty whitelist should fail open, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMasterAdminRPCs_RejectUnwhitelisted(t *testing.T) {
|
||||
ms := newAdminLockMaster()
|
||||
ms.guard = security.NewGuard([]string{"127.0.0.1"}, "", 0, "", 0, "", "")
|
||||
ctx := ctxFromHost("10.13.37.66")
|
||||
|
||||
calls := map[string]func() error{
|
||||
"CollectionDelete": func() error {
|
||||
_, err := ms.CollectionDelete(ctx, &master_pb.CollectionDeleteRequest{Name: "c"})
|
||||
return err
|
||||
},
|
||||
"DisableVacuum": func() error {
|
||||
_, err := ms.DisableVacuum(ctx, &master_pb.DisableVacuumRequest{})
|
||||
return err
|
||||
},
|
||||
"EnableVacuum": func() error {
|
||||
_, err := ms.EnableVacuum(ctx, &master_pb.EnableVacuumRequest{})
|
||||
return err
|
||||
},
|
||||
"VolumeGrow": func() error {
|
||||
_, err := ms.VolumeGrow(ctx, &master_pb.VolumeGrowRequest{WritableVolumeCount: 1})
|
||||
return err
|
||||
},
|
||||
"ResumeVolumeGrowth": func() error {
|
||||
_, err := ms.ResumeVolumeGrowth(ctx, &master_pb.ResumeVolumeGrowthRequest{})
|
||||
return err
|
||||
},
|
||||
"LeaseAdminToken": func() error {
|
||||
_, err := ms.LeaseAdminToken(ctx, &master_pb.LeaseAdminTokenRequest{LockName: "admin"})
|
||||
return err
|
||||
},
|
||||
"ReleaseAdminToken": func() error {
|
||||
_, err := ms.ReleaseAdminToken(ctx, &master_pb.ReleaseAdminTokenRequest{LockName: "admin"})
|
||||
return err
|
||||
},
|
||||
}
|
||||
for name, call := range calls {
|
||||
if err := call(); status.Code(err) != codes.PermissionDenied {
|
||||
t.Errorf("%s: expected PermissionDenied, got %v", name, err)
|
||||
}
|
||||
}
|
||||
if ms.Topo.IsVacuumDisabled() {
|
||||
t.Error("DisableVacuum from an unwhitelisted caller disabled vacuum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMasterAdminRPCs_AllowLocalSocketAndGrowthLoop(t *testing.T) {
|
||||
ms := &MasterServer{
|
||||
Topo: topology.NewTopology("test", nil, 0, 0, false),
|
||||
guard: security.NewGuard([]string{"10.0.0.1"}, "", 0, "", 0, "", ""),
|
||||
}
|
||||
local := peer.NewContext(context.Background(), &peer.Peer{Addr: &net.UnixAddr{Name: "@", Net: "unix"}})
|
||||
if _, err := ms.DisableVacuum(local, &master_pb.DisableVacuumRequest{}); err != nil {
|
||||
t.Fatalf("DisableVacuum over the local socket: %v", err)
|
||||
}
|
||||
if !ms.Topo.IsVacuumDisabled() {
|
||||
t.Error("DisableVacuum over the local socket did not disable vacuum")
|
||||
}
|
||||
if _, err := ms.VolumeGrow(context.Background(), &master_pb.VolumeGrowRequest{WritableVolumeCount: 1}); status.Code(err) == codes.PermissionDenied {
|
||||
t.Errorf("in-process VolumeGrow was refused: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/peer"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
@@ -473,6 +474,9 @@ func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumV
|
||||
}
|
||||
|
||||
func (ms *MasterServer) DisableVacuum(ctx context.Context, req *master_pb.DisableVacuumRequest) (*master_pb.DisableVacuumResponse, error) {
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The caller explicitly indicates whether this disable request comes
|
||||
// from the vacuum plugin monitor. Track ownership so the safety net
|
||||
// in the vacuum loop won't override an operator's intentional disable.
|
||||
@@ -486,6 +490,9 @@ func (ms *MasterServer) DisableVacuum(ctx context.Context, req *master_pb.Disabl
|
||||
}
|
||||
|
||||
func (ms *MasterServer) EnableVacuum(ctx context.Context, req *master_pb.EnableVacuumRequest) (*master_pb.EnableVacuumResponse, error) {
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.GetByPlugin() {
|
||||
ms.Topo.EnableVacuumByPlugin()
|
||||
} else {
|
||||
@@ -540,6 +547,9 @@ func (ms *MasterServer) VolumeMarkReadonly(ctx context.Context, req *master_pb.V
|
||||
}
|
||||
|
||||
func (ms *MasterServer) ResumeVolumeGrowth(ctx context.Context, req *master_pb.ResumeVolumeGrowthRequest) (*master_pb.ResumeVolumeGrowthResponse, error) {
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ms.Topo.IsLeader() {
|
||||
return nil, raft.NotLeaderError
|
||||
}
|
||||
@@ -547,6 +557,12 @@ func (ms *MasterServer) ResumeVolumeGrowth(ctx context.Context, req *master_pb.R
|
||||
}
|
||||
|
||||
func (ms *MasterServer) VolumeGrow(ctx context.Context, req *master_pb.VolumeGrowRequest) (*master_pb.VolumeGrowResponse, error) {
|
||||
// The growth loop calls this in-process, without a gRPC peer.
|
||||
if _, fromPeer := peer.FromContext(ctx); fromPeer {
|
||||
if err := ms.checkGrpcAdminAuth(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if !ms.Topo.IsLeader() {
|
||||
return nil, raft.NotLeaderError
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ import (
|
||||
// allow-all branch in IsWhiteListed accepts them, with a whitelist they're
|
||||
// denied as expected.
|
||||
//
|
||||
// Callers on the local gRPC socket of weed server/mini are processes on this
|
||||
// host, such as the in-process master allocating volumes.
|
||||
//
|
||||
// Failed authorization attempts are logged so an operator running with a
|
||||
// configured whitelist can spot misconfigured callers and probe attempts.
|
||||
func (vs *VolumeServer) checkGrpcAdminAuth(ctx context.Context) error {
|
||||
@@ -54,6 +57,9 @@ func (vs *VolumeServer) checkGrpcAdminAuth(ctx context.Context) error {
|
||||
glog.V(0).Infof("gRPC admin auth failed: no peer info")
|
||||
return status.Error(codes.PermissionDenied, "no peer info")
|
||||
}
|
||||
if _, ok := pr.Addr.(*net.UnixAddr); ok {
|
||||
return nil
|
||||
}
|
||||
addr := pr.Addr.String()
|
||||
var host string
|
||||
if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok {
|
||||
|
||||
@@ -58,12 +58,12 @@ var ungatedVolumeServerRPCs = map[string]string{
|
||||
// side of the boundary it sits on -- an allowlist this size drifts otherwise,
|
||||
// which is how the gate ended up covering less than half the service.
|
||||
func TestVolumeServerAdminAuthCoverage(t *testing.T) {
|
||||
declared := rpcNamesFromProto(t)
|
||||
declared := rpcNamesFromProto(t, "volume_server.proto")
|
||||
if len(declared) < 40 {
|
||||
t.Fatalf("parsed only %d RPCs from the proto, expected the full service", len(declared))
|
||||
}
|
||||
|
||||
gated := gatedVolumeServerMethods(t)
|
||||
gated := gatedServerMethods(t, "VolumeServer")
|
||||
|
||||
for _, rpc := range declared {
|
||||
_, exempt := ungatedVolumeServerRPCs[rpc]
|
||||
@@ -94,9 +94,9 @@ func TestVolumeServerAdminAuthCoverage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func rpcNamesFromProto(t *testing.T) []string {
|
||||
func rpcNamesFromProto(t *testing.T, protoFile string) []string {
|
||||
t.Helper()
|
||||
path := filepath.Join("..", "pb", "volume_server.proto")
|
||||
path := filepath.Join("..", "pb", protoFile)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
@@ -110,11 +110,11 @@ func rpcNamesFromProto(t *testing.T) []string {
|
||||
return names
|
||||
}
|
||||
|
||||
// gatedVolumeServerMethods reports which methods on *VolumeServer call
|
||||
// gatedServerMethods reports which methods on *receiver call
|
||||
// checkGrpcAdminAuth anywhere in their body. It walks the AST rather than
|
||||
// scanning a fixed window of lines so a guard placed after an early
|
||||
// maintenance-mode check still counts.
|
||||
func gatedVolumeServerMethods(t *testing.T) map[string]bool {
|
||||
func gatedServerMethods(t *testing.T, receiver string) map[string]bool {
|
||||
t.Helper()
|
||||
fset := token.NewFileSet()
|
||||
pkgs, err := parser.ParseDir(fset, ".", func(fi os.FileInfo) bool {
|
||||
@@ -132,7 +132,7 @@ func gatedVolumeServerMethods(t *testing.T) map[string]bool {
|
||||
if !ok || fn.Recv == nil || fn.Body == nil {
|
||||
continue
|
||||
}
|
||||
if !isVolumeServerReceiver(fn.Recv) {
|
||||
if !isServerReceiver(fn.Recv, receiver) {
|
||||
continue
|
||||
}
|
||||
ast.Inspect(fn.Body, func(n ast.Node) bool {
|
||||
@@ -149,7 +149,7 @@ func gatedVolumeServerMethods(t *testing.T) map[string]bool {
|
||||
return gated
|
||||
}
|
||||
|
||||
func isVolumeServerReceiver(recv *ast.FieldList) bool {
|
||||
func isServerReceiver(recv *ast.FieldList, receiver string) bool {
|
||||
if len(recv.List) != 1 {
|
||||
return false
|
||||
}
|
||||
@@ -158,5 +158,5 @@ func isVolumeServerReceiver(recv *ast.FieldList) bool {
|
||||
return false
|
||||
}
|
||||
ident, ok := star.X.(*ast.Ident)
|
||||
return ok && ident.Name == "VolumeServer"
|
||||
return ok && ident.Name == receiver
|
||||
}
|
||||
@@ -53,6 +53,16 @@ func TestBatchDelete_DeniesNonWhitelistedPeer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckGrpcAdminAuth_AllowsLocalSocket(t *testing.T) {
|
||||
vs := &VolumeServer{
|
||||
guard: security.NewGuard([]string{"10.0.0.1"}, "", 0, "", 0, "", ""),
|
||||
}
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: &net.UnixAddr{Name: "@", Net: "unix"},
|
||||
})
|
||||
require.NoError(t, vs.checkGrpcAdminAuth(ctx))
|
||||
}
|
||||
|
||||
type syncCountingBackend struct {
|
||||
backend.BackendStorageFile
|
||||
syncs atomic.Int32
|
||||
|
||||
Reference in new issue
Block a user