Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: admin execute iam (#13036)

Danny Xu49d0c54365f8
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.go
index 429c462230..dbf85ec9b5 100644
--- a/backend/api/v1/acl.go
+++ b/backend/api/v1/acl.go
@@ -76,7 +76,7 @@ func (in *ACLInterceptor) ACLInterceptor(ctx context.Context, request any, serve
if !ok {
return nil, status.Errorf(codes.Internal, "auth context not found")
}
- if err := in.populateRawResources(ctx, authContext, request, serverInfo.FullMethod); err != nil {
+ if err := populateRawResources(ctx, in.store, authContext, request, serverInfo.FullMethod); err != nil {
return nil, status.Errorf(codes.Internal, "failed to populate raw resources %s", err)
}
@@ -87,7 +87,7 @@ func (in *ACLInterceptor) ACLInterceptor(ctx context.Context, request any, serve
return nil, status.Errorf(codes.Unauthenticated, "unauthenticated for method %q", serverInfo.FullMethod)
}
- ok, extra, err := in.doIAMPermissionCheck(ctx, serverInfo.FullMethod, user, authContext)
+ ok, extra, err := doIAMPermissionCheck(ctx, in.iamManager, serverInfo.FullMethod, user, authContext)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to check permission for method %q, extra %v, err: %v", serverInfo.FullMethod, extra, err)
}
@@ -126,43 +126,52 @@ func (in *ACLInterceptor) ACLStreamInterceptor(request any, ss grpc.ServerStream
}
ctx = context.WithValue(ctx, common.RoleContextKey, role)
ctx = context.WithValue(ctx, common.UserContextKey, user)
- ss = overrideStream{ServerStream: ss, childCtx: ctx}
+ ss = &overrideStream{ServerStream: ss, childCtx: ctx, iamManager: in.iamManager, store: in.store, user: user, fullMethod: serverInfo.FullMethod}
}
- authContextAny := ctx.Value(common.AuthContextKey)
+ return handler(request, ss)
+}
+
+type overrideStream struct {
+ grpc.ServerStream
+
+ childCtx context.Context
+ iamManager *iam.Manager
+ store *store.Store
+ user *store.UserMessage
+ fullMethod string
+}
+
+func (o overrideStream) Context() context.Context {
+ return o.childCtx
+}
+
+func (o *overrideStream) RecvMsg(request any) error {
+ authContextAny := o.childCtx.Value(common.AuthContextKey)
authContext, ok := authContextAny.(*common.AuthContext)
if !ok {
return status.Errorf(codes.Internal, "auth context not found")
}
- if err := in.populateRawResources(ctx, authContext, request, serverInfo.FullMethod); err != nil {
+ if err := populateRawResources(o.childCtx, o.store, authContext, request, o.fullMethod); err != nil {
return status.Errorf(codes.Internal, "failed to populate raw resources %s", err)
}
- if auth.IsAuthenticationAllowed(serverInfo.FullMethod, authContext) {
- return handler(request, ss)
+ if auth.IsAuthenticationAllowed(o.fullMethod, authContext) {
+ return o.ServerStream.RecvMsg(request)
}
- if user == nil {
- return status.Errorf(codes.Unauthenticated, "unauthenticated for method %q", serverInfo.FullMethod)
+ if o.user == nil {
+ return status.Errorf(codes.Unauthenticated, "unauthenticated for method %q", o.fullMethod)
}
- ok, extra, err := in.doIAMPermissionCheck(ctx, serverInfo.FullMethod, user, authContext)
+ ok, extra, err := doIAMPermissionCheck(o.childCtx, o.iamManager, o.fullMethod, o.user, authContext)
if err != nil {
- return status.Errorf(codes.Internal, "failed to check permission for method %q, extra %v, err: %v", serverInfo.FullMethod, extra, err)
+ return status.Errorf(codes.Internal, "failed to check permission for method %q, extra %v, err: %v", o.fullMethod, extra, err)
}
if !ok {
- return status.Errorf(codes.PermissionDenied, "permission denied for method %q, user does not have permission %q, extra %v", serverInfo.FullMethod, authContext.Permission, extra)
+ return status.Errorf(codes.PermissionDenied, "permission denied for method %q, user does not have permission %q, extra %v", o.fullMethod, authContext.Permission, extra)
}
- return handler(request, ss)
-}
-
-type overrideStream struct {
- childCtx context.Context
- grpc.ServerStream
-}
-
-func (s overrideStream) Context() context.Context {
- return s.childCtx
+ return o.ServerStream.RecvMsg(request)
}
func (in *ACLInterceptor) getUser(ctx context.Context) (*store.UserMessage, error) {
@@ -200,7 +209,7 @@ func hasPath(fieldMask *fieldmaskpb.FieldMask, want string) bool {
return false
}
-func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod string, user *store.UserMessage, authContext *common.AuthContext) (bool, []string, error) {
+func doIAMPermissionCheck(ctx context.Context, iamManager *iam.Manager, fullMethod string, user *store.UserMessage, authContext *common.AuthContext) (bool, []string, error) {
if auth.IsAuthenticationAllowed(fullMethod, authContext) {
return true, nil, nil
}
@@ -212,7 +221,7 @@ func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod s
return false, nil, errors.Errorf("no resource found for IAM auth method")
}
if authContext.HasWorkspaceResource() {
- ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user)
+ ok, err := iamManager.CheckPermission(ctx, authContext.Permission, user)
if err != nil {
return false, nil, err
}
@@ -222,7 +231,7 @@ func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod s
}
projectIDs := authContext.GetProjectResources()
if len(projectIDs) > 0 {
- ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user, projectIDs...)
+ ok, err := iamManager.CheckPermission(ctx, authContext.Permission, user, projectIDs...)
if err != nil {
return false, projectIDs, err
}
@@ -236,7 +245,7 @@ func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod s
var projectRegex = regexp.MustCompile(`^projects/[^/]+`)
var databaseRegex = regexp.MustCompile(`^instances/[^/]+/databases/[^/]+`)
-func (in *ACLInterceptor) populateRawResources(ctx context.Context, authContext *common.AuthContext, request any, method string) error {
+func populateRawResources(ctx context.Context, stores *store.Store, authContext *common.AuthContext, request any, method string) error {
if authContext.AllowWithoutCredential {
return nil
}
@@ -263,7 +272,7 @@ func (in *ACLInterceptor) populateRawResources(ctx context.Context, authContext
case strings.HasPrefix(resource.Name, "instances/") && strings.Contains(resource.Name, "/databases/"):
match := databaseRegex.FindString(resource.Name)
if match != "" {
- database, err := getDatabaseMessage(ctx, in.store, match)
+ database, err := getDatabaseMessage(ctx, stores, match)
if err != nil {
return errors.Wrapf(err, "failed to get database %q", match)
}
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.go
index ab508f270f..9dfc1290aa 100644
--- a/backend/api/v1/audit.go
+++ b/backend/api/v1/audit.go
@@ -112,7 +112,7 @@ func (in *AuditInterceptor) AuditInterceptor(ctx context.Context, request any, s
return response, rerr
}
-type AuditStream struct {
+type auditStream struct {
grpc.ServerStream
needAudit bool
curRequest any
@@ -121,7 +121,7 @@ type AuditStream struct {
storage *store.Store
}
-func (s *AuditStream) RecvMsg(request any) error {
+func (s *auditStream) RecvMsg(request any) error {
err := s.ServerStream.RecvMsg(request)
if err != nil {
return err
@@ -133,7 +133,7 @@ func (s *AuditStream) RecvMsg(request any) error {
return nil
}
-func (s *AuditStream) SendMsg(resp any) error {
+func (s *auditStream) SendMsg(resp any) error {
err := s.ServerStream.SendMsg(resp)
if err != nil {
return err
@@ -149,12 +149,12 @@ func (s *AuditStream) SendMsg(resp any) error {
}
func (in *AuditInterceptor) AuditStreamInterceptor(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
- overrideStream, ok := ss.(overrideStream)
+ overrideStream, ok := ss.(*overrideStream)
if !ok {
return errors.New("type assertions failed: grpc.ServerStream -> overrideStream")
}
- auditStream := &AuditStream{
+ auditStream := &auditStream{
ServerStream: overrideStream,
needAudit: isStreamAuditMethod(info.FullMethod),
ctx: overrideStream.childCtx,
diff --git a/frontend/src/types/proto/v1/sql_service.ts b/frontend/src/types/proto/v1/sql_service.ts
index 0ba19107b8..146756b2ce 100644
--- a/frontend/src/types/proto/v1/sql_service.ts
+++ b/frontend/src/types/proto/v1/sql_service.ts
@@ -3508,7 +3508,7 @@ export const SQLServiceDefinition = {
101,
]),
],
- 800016: [new Uint8Array([2])],
+ 800016: [new Uint8Array([1])],
578365826: [
new Uint8Array([18, 18, 16, 47, 118, 49, 58, 97, 100, 109, 105, 110, 69, 120, 101, 99, 117, 116, 101]),
],
diff --git a/proto/generated-go/v1/sql_service.pb.go b/proto/generated-go/v1/sql_service.pb.go
index 5071a06093..635f53fd6f 100644
--- a/proto/generated-go/v1/sql_service.pb.go
+++ b/proto/generated-go/v1/sql_service.pb.go
@@ -2230,9 +2230,11 @@ var file_v1_sql_service_proto_rawDesc = []byte{
0x75, 0x6c, 0x74, 0x73, 0x12, 0x2d, 0x0a, 0x07, 0x61, 0x64, 0x76, 0x69, 0x63, 0x65, 0x73, 0x18,
0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65,
0x2e, 0x76, 0x31, 0x2e, 0x41, 0x64, 0x76, 0x69, 0x63, 0x65, 0x52, 0x07, 0x61, 0x64, 0x76, 0x69,
- 0x63, 0x65, 0x73, 0x22, 0xc7, 0x01, 0x0a, 0x13, 0x41, 0x64, 0x6d, 0x69, 0x6e, 0x45, 0x78, 0x65,
- 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e,
- 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,
+ 0x63, 0x65, 0x73, 0x22, 0xe7, 0x01, 0x0a, 0x13, 0x41, 0x64, 0x6d, 0x69, 0x6e, 0x45, 0x78, 0x65,
+ 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x32, 0x0a, 0x04, 0x6e,
+ 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x1e, 0xe2, 0x41, 0x01, 0x02, 0xfa,
+ 0x41, 0x17, 0x0a, 0x15, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
+ 0x2f, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,
0x33, 0x0a, 0x13, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x64, 0x61,
0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x42, 0x02, 0x18, 0x01,
0x52, 0x12, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x44, 0x61, 0x74, 0x61,
@@ -2531,7 +2533,7 @@ var file_v1_sql_service_proto_rawDesc = []byte{
0x73, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x41, 0x64, 0x6d, 0x69, 0x6e, 0x45, 0x78, 0x65, 0x63, 0x75,
0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x39, 0x8a, 0xea, 0x30, 0x19,
0x62, 0x62, 0x2e, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x73, 0x2e, 0x61, 0x64, 0x6d,
- 0x69, 0x6e, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x90, 0xea, 0x30, 0x02, 0x82, 0xd3, 0xe4,
+ 0x69, 0x6e, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x90, 0xea, 0x30, 0x01, 0x82, 0xd3, 0xe4,
0x93, 0x02, 0x12, 0x12, 0x10, 0x2f, 0x76, 0x31, 0x3a, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x45, 0x78,
0x65, 0x63, 0x75, 0x74, 0x65, 0x28, 0x01, 0x30, 0x01, 0x12, 0x92, 0x01, 0x0a, 0x14, 0x53, 0x65,
0x61, 0x72, 0x63, 0x68, 0x51, 0x75, 0x65, 0x72, 0x79, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x69,
diff --git a/proto/v1/sql_service.proto b/proto/v1/sql_service.proto
index 21b5ef529c..38e2710e78 100644
--- a/proto/v1/sql_service.proto
+++ b/proto/v1/sql_service.proto
@@ -41,7 +41,7 @@ service SQLService {
rpc AdminExecute(stream AdminExecuteRequest) returns (stream AdminExecuteResponse) {
option (google.api.http) = {get: "/v1:adminExecute"};
option (bytebase.v1.permission) = "bb.instances.adminExecute";
- option (bytebase.v1.auth_method) = CUSTOM;
+ option (bytebase.v1.auth_method) = IAM;
}
rpc SearchQueryHistories(SearchQueryHistoriesRequest) returns (SearchQueryHistoriesResponse) {
@@ -146,7 +146,10 @@ message ExecuteResponse {
message AdminExecuteRequest {
// The name is the instance name to execute the query against.
// Format: instances/{instance}/databases/{databaseName}
- string name = 1;
+ string name = 1 [
+ (google.api.field_behavior) = REQUIRED,
+ (google.api.resource_reference) = {type: "bytebase.com/Database"}
+ ];
string connection_database = 2 [deprecated = true];