Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add basic authorization interceptor (#4051)
Danny Xufb95a203b521
diff --git a/api/member.go b/api/member.goindex 9cac446ec1..fa98f1bb0d 100644--- a/api/member.go+++ b/api/member.go@@ -26,6 +26,8 @@ const (DBA Role = "DBA"// Developer is the DEVELOPER role.Developer Role = "DEVELOPER"+ // UnknownRole is the unknown role.+ UnknownRole Role = "UNKNOWN")// Member is the API message for a member.diff --git a/common/context.go b/common/context.goindex 38e9dd7130..40e5d148d3 100644--- a/common/context.go+++ b/common/context.go@@ -6,4 +6,6 @@ type ContextKey intconst (// PrincipalIDContextKey is the key name used to store principal id in the context.PrincipalIDContextKey ContextKey = iota+ // RoleContextKey is the key name used to store principal role in the context.+ RoleContextKey)diff --git a/server/api/auth/acl.go b/server/api/auth/acl.gonew file mode 100644index 0000000000..6dc64ca871--- /dev/null+++ b/server/api/auth/acl.go@@ -0,0 +1,49 @@+package auth++import (+ "context"++ "google.golang.org/grpc"+ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/status"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+)++// ACLInterceptor is the unary interceptor for gRPC API.+func (in *APIAuthInterceptor) ACLInterceptor(ctx context.Context, req interface{}, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {+ role, err := in.getWorkspaceRole(ctx, serverInfo.FullMethod)+ if err != nil {+ return nil, status.Errorf(codes.PermissionDenied, err.Error())+ }+ // Stores principalID into context.+ childCtx := context.WithValue(ctx, common.RoleContextKey, role)++ // TODO(d): implement the authorization.++ return handler(childCtx, req)+}++func (in *APIAuthInterceptor) getWorkspaceRole(ctx context.Context, fullMethod string) (api.Role, error) {+ if isAuthenticationAllowed(fullMethod) {+ return api.Owner, nil+ }+ // If RBAC feature is not enabled, all users are treated as OWNER.+ if !in.licenseService.IsFeatureEnabled(api.FeatureRBAC) {+ return api.Owner, nil+ }++ principalID := ctx.Value(common.PrincipalIDContextKey).(int)+ member, err := in.store.GetMemberByPrincipalID(ctx, principalID)+ if err != nil {+ return api.UnknownRole, status.Errorf(codes.PermissionDenied, "failed to get member for principal %v in processing authorize request.", principalID)+ }+ if member == nil {+ return api.UnknownRole, status.Errorf(codes.PermissionDenied, "member not found for principal %v in processing authorize request.", principalID)+ }+ if member.RowStatus == api.Archived {+ return api.UnknownRole, status.Errorf(codes.PermissionDenied, "The principal %v has been deactivated by the admin.", principalID)+ }+ return member.Role, nil+}diff --git a/server/api/auth/auth.go b/server/api/auth/auth.goindex e09ca013a6..6f48ec93e6 100644--- a/server/api/auth/auth.go+++ b/server/api/auth/auth.go@@ -18,6 +18,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"+ enterpriseAPI "github.com/bytebase/bytebase/enterprise/api""github.com/bytebase/bytebase/store")@@ -32,32 +33,27 @@ const (// APIAuthInterceptor is the auth interceptor for gRPC server.type APIAuthInterceptor struct {- store *store.Store- secret string- mode common.ReleaseMode+ store *store.Store+ secret string+ licenseService enterpriseAPI.LicenseService+ mode common.ReleaseMode}// New returns a new API auth interceptor.-func New(store *store.Store, secret string, mode common.ReleaseMode) *APIAuthInterceptor {+func New(store *store.Store, secret string, licenseService enterpriseAPI.LicenseService, mode common.ReleaseMode) *APIAuthInterceptor {return &APIAuthInterceptor{- store: store,- secret: secret,- mode: mode,+ store: store,+ secret: secret,+ licenseService: licenseService,+ mode: mode,}}-var authAllowlistMethods = []string{- "/bytebase.v1.AuthService/Login",-}--// UnaryInterceptor is the unary interceptor for gRPC API.-func (in *APIAuthInterceptor) UnaryInterceptor(ctx context.Context, req interface{}, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {+// AuthenticationInterceptor is the unary interceptor for gRPC API.+func (in *APIAuthInterceptor) AuthenticationInterceptor(ctx context.Context, req interface{}, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {// TODO(d): skips actuator, GET /subscription request, OpenAPI SQL endpoint.- // Skip the allowlisted methods for auth interceptor.- for _, allow := range authAllowlistMethods {- if strings.HasPrefix(serverInfo.FullMethod, allow) {- return handler(ctx, req)- }+ if isAuthenticationAllowed(serverInfo.FullMethod) {+ return handler(ctx, req)}md, ok := metadata.FromIncomingContext(ctx)diff --git a/server/api/auth/config.go b/server/api/auth/config.gonew file mode 100644index 0000000000..0c5ba6e54b--- /dev/null+++ b/server/api/auth/config.go@@ -0,0 +1,21 @@+package auth++import (+ "fmt"+ "strings"+)++const apiPackagePrefix = "/bytebase.v1."++var authenticationAllowlistMethods = []string{+ "AuthService/Login",+}++func isAuthenticationAllowed(fullMethod string) bool {+ for _, allow := range authenticationAllowlistMethods {+ if strings.HasPrefix(fullMethod, fmt.Sprintf("%s%s", apiPackagePrefix, allow)) {+ return true+ }+ }+ return false+}diff --git a/server/server.go b/server/server.goindex 47e69a4fee..c88a27e0d2 100644--- a/server/server.go+++ b/server/server.go@@ -298,8 +298,10 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {}))// Setup the gRPC and grpc-gateway.- authProvider := auth.New(s.store, s.secret, profile.Mode)- s.grpcServer = grpc.NewServer(grpc.UnaryInterceptor(authProvider.UnaryInterceptor))+ authProvider := auth.New(s.store, s.secret, s.licenseService, profile.Mode)+ s.grpcServer = grpc.NewServer(+ grpc.ChainUnaryInterceptor(authProvider.AuthenticationInterceptor, authProvider.ACLInterceptor),+ )mux := runtime.NewServeMux()v1pb.RegisterGreeterServiceServer(s.grpcServer, &v1.GreeterServerImpl{})v1pb.RegisterAuthServiceServer(s.grpcServer, v1.NewAuthService(s.store, s.secret, &profile))