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: get resource from request (#12925)
Danny Xu1a90306aaa88
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.goindex 5c1768bfd6..162b196177 100644--- a/backend/api/v1/acl.go+++ b/backend/api/v1/acl.go@@ -4,9 +4,12 @@ import ("context""log/slog"+ annotationsproto "google.golang.org/genproto/googleapis/api/annotations""google.golang.org/grpc""google.golang.org/grpc/codes""google.golang.org/grpc/status"+ "google.golang.org/protobuf/proto"+ "google.golang.org/protobuf/reflect/protoreflect""google.golang.org/protobuf/types/known/fieldmaskpb""github.com/pkg/errors"@@ -366,3 +369,46 @@ func (in *ACLInterceptor) checkIAMPermissionInstancesGet(ctx context.Context, us}return len(databases) > 0, nil}++func getResourceFromRequest(request any) string {+ var resource string+ pm, ok := request.(proto.Message)+ if !ok {+ return ""+ }+ mr := pm.ProtoReflect()++ parentDesc := mr.Descriptor().Fields().ByName("parent")+ if parentDesc != nil {+ v := mr.Get(parentDesc)+ return v.String()+ }+ nameDesc := mr.Descriptor().Fields().ByName("name")+ if nameDesc != nil {+ v := mr.Get(nameDesc)+ return v.String()+ }++ mr.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {+ if fd.Name() == "name" {+ resource = v.String()+ return false+ }+ fdm := fd.Message()+ if fdm == nil {+ return true+ }+ if !proto.HasExtension(fdm.Options(), annotationsproto.E_Resource) {+ return true+ }++ nameDesc := fdm.Fields().ByName("name")+ if nameDesc != nil {+ rn := v.Message().Get(nameDesc)+ resource = rn.String()+ return false+ }+ return false+ })+ return resource+}diff --git a/backend/api/v1/acl_test.go b/backend/api/v1/acl_test.gonew file mode 100644index 0000000000..eb3acab689--- /dev/null+++ b/backend/api/v1/acl_test.go@@ -0,0 +1,49 @@+package v1++import (+ "testing"++ "github.com/stretchr/testify/require"++ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"+)++func TestGetResourceFromRequest(t *testing.T) {+ tests := []struct {+ request any+ want string+ }{+ {+ request: &v1pb.LoginRequest{Email: "[email protected]"},+ want: "",+ },+ {+ request: &v1pb.CreateDatabaseGroupRequest{+ Parent: "projects/hello",+ },+ want: "projects/hello",+ },+ {+ request: &v1pb.UpdateProjectRequest{+ Project: &v1pb.Project{+ Name: "projects/hello",+ },+ },+ want: "projects/hello",+ },+ {+ // The instance has not been annotated with resource yet.+ request: &v1pb.UpdateInstanceRequest{+ Instance: &v1pb.Instance{+ Name: "instances/hello",+ },+ },+ want: "",+ },+ }++ for _, tt := range tests {+ got := getResourceFromRequest(tt.request)+ require.Equal(t, tt.want, got)+ }+}