mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
feat(master): the authorization table and the server's interceptors
Phase 0, S3 (master part): masterRules covers all 24 blockmaster methods (PHASE0-AUTH section 3); the gRPC server takes Config.Auth's interceptors, or development-mode ones when authentication is not configured, and Config.ServerOptions. T2: every registered method has a rule and every rule a method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
34dbc862cd
commit
b807b3f00d
3 files changed
+114
-1
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package master
|
||||
|
||||
import (
|
||||
"github.com/seaweedfs/seaweed-block/core/auth"
|
||||
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
|
||||
"github.com/seaweedfs/seaweed-block/core/rpc/link"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// masterRules is the authorization table of every blockmaster gRPC method
|
||||
// (phase 0): the minimum person role, and whether a node may call it. A node
|
||||
// call's own-server-id check is the handler's (auth.RequireNode); a forced
|
||||
// move's admin check is MoveWorkspace's (auth.RequireRole). A method missing
|
||||
// here is refused (TestMasterAuthorizationTableCoversEveryMethod).
|
||||
var masterRules = map[string]auth.Rule{
|
||||
control.ObservationService_ReportHeartbeat_FullMethodName: {Node: true},
|
||||
control.ObservationService_ReportClusterEvent_FullMethodName: {Min: auth.RoleOperator},
|
||||
|
||||
control.AssignmentService_SubscribeAssignments_FullMethodName: {Node: true},
|
||||
|
||||
control.EvidenceService_QueryVolumeStatus_FullMethodName: {Min: auth.RoleViewer, Node: true},
|
||||
|
||||
control.ClusterEvidenceService_GetClusterStatus_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.ClusterEvidenceService_ListVolumes_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.ClusterEvidenceService_GetVolumeStatus_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.ClusterEvidenceService_GetVolumeTimeline_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.ClusterEvidenceService_WatchClusterEvents_FullMethodName: {Min: auth.RoleViewer},
|
||||
|
||||
control.LifecycleService_CreateVolume_FullMethodName: {Min: auth.RoleOperator},
|
||||
control.LifecycleService_DeleteVolume_FullMethodName: {Min: auth.RoleAdmin},
|
||||
control.LifecycleService_MarkVolumeDeleting_FullMethodName: {Min: auth.RoleAdmin},
|
||||
control.LifecycleService_AbortVolumeDeleting_FullMethodName: {Min: auth.RoleAdmin},
|
||||
control.LifecycleService_CommitVolumeDeleting_FullMethodName: {Min: auth.RoleAdmin},
|
||||
|
||||
control.FailbackService_ExecuteFailback_FullMethodName: {Min: auth.RoleAdmin},
|
||||
|
||||
control.WorkspaceService_RegisterWorkspaceBinding_FullMethodName: {Min: auth.RoleAdmin},
|
||||
control.WorkspaceService_PlaceWorkspace_FullMethodName: {Min: auth.RoleOperator},
|
||||
control.WorkspaceService_RenewWorkspaceLease_FullMethodName: {Node: true},
|
||||
control.WorkspaceService_AckWorkspaceRevoked_FullMethodName: {Node: true},
|
||||
control.WorkspaceService_RevokeWorkspace_FullMethodName: {Min: auth.RoleOperator},
|
||||
control.WorkspaceService_MoveWorkspace_FullMethodName: {Min: auth.RoleOperator}, // forced: admin, in the handler
|
||||
control.WorkspaceService_QueryWorkspace_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.WorkspaceService_QueryWorkspaceLostRange_FullMethodName: {Min: auth.RoleViewer},
|
||||
control.WorkspaceService_PreviewWorkspaceMove_FullMethodName: {Min: auth.RoleViewer},
|
||||
}
|
||||
|
||||
// serverAuth returns the interceptor chain of the master's gRPC server: the
|
||||
// authenticator's when authentication is configured, else development mode
|
||||
// (plaintext, loopback only; every call marked as such).
|
||||
func serverAuth(a *auth.Authenticator) link.ServerConfig {
|
||||
if a == nil {
|
||||
return link.ServerConfig{
|
||||
Unary: []grpc.UnaryServerInterceptor{auth.DevUnaryInterceptor()},
|
||||
Stream: []grpc.StreamServerInterceptor{auth.DevStreamInterceptor()},
|
||||
}
|
||||
}
|
||||
return link.ServerConfig{
|
||||
Unary: []grpc.UnaryServerInterceptor{a.UnaryInterceptor(masterRules)},
|
||||
Stream: []grpc.StreamServerInterceptor{a.StreamInterceptor(masterRules)},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package master
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// T2 (phase 0): every gRPC method the master registers has a row in the
|
||||
// authorization table, and every row names a registered method; a new RPC
|
||||
// cannot ship without a row (an unlisted method is refused at run time).
|
||||
func TestMasterAuthorizationTableCoversEveryMethod(t *testing.T) {
|
||||
h, err := New(Config{Listen: "127.0.0.1:0", AuthorityStoreDir: t.TempDir()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { closeTestMaster(t, h) })
|
||||
registered := map[string]bool{}
|
||||
for svc, info := range h.grpc.GetServiceInfo() {
|
||||
for _, m := range info.Methods {
|
||||
registered["/"+svc+"/"+m.Name] = true
|
||||
}
|
||||
}
|
||||
var missing, stale []string
|
||||
for m := range registered {
|
||||
if _, ok := masterRules[m]; !ok {
|
||||
missing = append(missing, m)
|
||||
}
|
||||
}
|
||||
for m := range masterRules {
|
||||
if !registered[m] {
|
||||
stale = append(stale, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(missing)
|
||||
sort.Strings(stale)
|
||||
if len(missing) > 0 || len(stale) > 0 {
|
||||
t.Fatalf("authorization table out of date: methods without a rule %v; rules without a method %v", missing, stale)
|
||||
}
|
||||
if len(registered) != 24 {
|
||||
t.Fatalf("%d registered methods; the table was written for 24: review the new ones", len(registered))
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/auth"
|
||||
"github.com/seaweedfs/seaweed-block/core/authority"
|
||||
"github.com/seaweedfs/seaweed-block/core/host/bootstrap"
|
||||
"github.com/seaweedfs/seaweed-block/core/lifecycle"
|
||||
@@ -39,6 +40,14 @@ import (
|
||||
// Config holds the master host construction inputs. Flag parsing
|
||||
// lives in cmd/blockmaster; this type is the testable seam.
|
||||
type Config struct {
|
||||
// Auth authenticates and authorizes every gRPC call (phase 0): nil is
|
||||
// development mode (plaintext; every call is marked so, and the node
|
||||
// checks admit it).
|
||||
Auth *auth.Authenticator
|
||||
// ServerOptions are added to the gRPC server (transport credentials,
|
||||
// connection age).
|
||||
ServerOptions []grpc.ServerOption
|
||||
|
||||
// AuthorityStoreDir is the directory passed to
|
||||
// authority.AcquireStoreLock + NewFileAuthorityStore. Must be
|
||||
// non-empty.
|
||||
@@ -320,7 +329,7 @@ func New(cfg Config) (*Host, error) {
|
||||
WriteTimeout: statusWriteTimeout, IdleTimeout: statusIdleTimeout}
|
||||
}
|
||||
|
||||
grpcSrv := link.NewServer(link.ServerConfig{})
|
||||
grpcSrv := link.NewServer(serverAuth(cfg.Auth), cfg.ServerOptions...)
|
||||
svc := newServices(h)
|
||||
control.RegisterObservationServiceServer(grpcSrv, svc)
|
||||
control.RegisterAssignmentServiceServer(grpcSrv, svc)
|
||||
|
||||
Reference in new issue
Block a user