Skip to content

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

chore: populate resource (#12939)

Danny Xudf01f6ee29e8
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.go
index a3c2b2613c..0810ea883c 100644
--- a/backend/api/v1/acl.go
+++ b/backend/api/v1/acl.go
@@ -3,6 +3,7 @@ package v1
import (
"context"
"log/slog"
+ "strings"
annotationsproto "google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/grpc"
@@ -60,7 +61,10 @@ func (in *ACLInterceptor) ACLInterceptor(ctx context.Context, request any, serve
authContextAny := ctx.Value(common.AuthContextKey)
authContext, ok := authContextAny.(*common.AuthContext)
if !ok {
- return nil, status.Errorf(codes.Internal, "auth context not found2")
+ return nil, status.Errorf(codes.Internal, "auth context not found")
+ }
+ if err := in.populateRawResources(authContext, request, serverInfo.FullMethod); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to populate raw resources %s", err)
}
if auth.IsAuthenticationAllowed(serverInfo.FullMethod, authContext) {
@@ -99,7 +103,10 @@ func (in *ACLInterceptor) ACLStreamInterceptor(request any, ss grpc.ServerStream
authContextAny := ctx.Value(common.AuthContextKey)
authContext, ok := authContextAny.(*common.AuthContext)
if !ok {
- return status.Errorf(codes.Internal, "auth context not found3")
+ return status.Errorf(codes.Internal, "auth context not found")
+ }
+ if err := in.populateRawResources(authContext, request, serverInfo.FullMethod); err != nil {
+ return status.Errorf(codes.Internal, "failed to populate raw resources %s", err)
}
if auth.IsAuthenticationAllowed(serverInfo.FullMethod, authContext) {
@@ -245,7 +252,17 @@ func (in *ACLInterceptor) checkIAMPermissionInstancesGet(ctx context.Context, us
return len(databases) > 0, nil
}
-func getResourceFromRequest(request any) string {
+func (*ACLInterceptor) populateRawResources(authContext *common.AuthContext, request any, method string) error {
+ name := getResourceFromRequest(request, method)
+ if name != "" {
+ authContext.Resources = append(authContext.Resources, &common.Resource{
+ Name: name,
+ })
+ }
+ return nil
+}
+
+func getResourceFromRequest(request any, method string) string {
var resource string
pm, ok := request.(proto.Message)
if !ok {
@@ -254,15 +271,28 @@ func getResourceFromRequest(request any) string {
mr := pm.ProtoReflect()
parentDesc := mr.Descriptor().Fields().ByName("parent")
- if parentDesc != nil {
+ if parentDesc != nil && proto.HasExtension(parentDesc.Options(), annotationsproto.E_ResourceReference) {
v := mr.Get(parentDesc)
return v.String()
}
nameDesc := mr.Descriptor().Fields().ByName("name")
- if nameDesc != nil {
+ if nameDesc != nil && proto.HasExtension(nameDesc.Options(), annotationsproto.E_ResourceReference) {
v := mr.Get(nameDesc)
return v.String()
}
+ // This is primarily used by Get/SetIAMPolicy().
+ resourceDesc := mr.Descriptor().Fields().ByName("resource")
+ if resourceDesc != nil && proto.HasExtension(resourceDesc.Options(), annotationsproto.E_ResourceReference) {
+ v := mr.Get(resourceDesc)
+ return v.String()
+ }
+ methodTokens := strings.Split(method, "/")
+ if len(methodTokens) != 3 {
+ return ""
+ }
+ if strings.HasPrefix(methodTokens[2], "Create") {
+ return ""
+ }
mr.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if fd.Name() == "name" {
diff --git a/backend/api/v1/acl_test.go b/backend/api/v1/acl_test.go
index eb3acab689..3d2a477683 100644
--- a/backend/api/v1/acl_test.go
+++ b/backend/api/v1/acl_test.go
@@ -11,17 +11,21 @@ import (
func TestGetResourceFromRequest(t *testing.T) {
tests := []struct {
request any
+ method string
want string
}{
{
request: &v1pb.LoginRequest{Email: "[email protected]"},
+ method: "/bytebase.v1.AuthService/Login",
want: "",
},
+ // The database group has not been annotated with resource yet.
{
request: &v1pb.CreateDatabaseGroupRequest{
Parent: "projects/hello",
},
- want: "projects/hello",
+ method: "/bytebase.v1.DatabaseGroupService/CreateDatabaseGroup",
+ want: "",
},
{
request: &v1pb.UpdateProjectRequest{
@@ -29,7 +33,8 @@ func TestGetResourceFromRequest(t *testing.T) {
Name: "projects/hello",
},
},
- want: "projects/hello",
+ method: "/bytebase.v1.ProjectService/UpdateProject",
+ want: "projects/hello",
},
{
// The instance has not been annotated with resource yet.
@@ -38,12 +43,13 @@ func TestGetResourceFromRequest(t *testing.T) {
Name: "instances/hello",
},
},
- want: "",
+ method: "/bytebase.v1.InstanceService/UpdateInstance",
+ want: "",
},
}
for _, tt := range tests {
- got := getResourceFromRequest(tt.request)
- require.Equal(t, tt.want, got)
+ got := getResourceFromRequest(tt.request, tt.method)
+ require.Equal(t, tt.want, got, tt.method)
}
}
diff --git a/backend/common/context.go b/backend/common/context.go
index 8637fcc9cb..76ed3b9874 100644
--- a/backend/common/context.go
+++ b/backend/common/context.go
@@ -35,8 +35,16 @@ const (
AuthMethodCustom
)
+type Resource struct {
+ Type string
+ Name string
+ Project string
+ Workspace bool
+}
+
type AuthContext struct {
AllowWithoutCredential bool
Permission string
AuthMethod AuthMethod
+ Resources []*Resource
}