fix: [GoSDK] Return role without grants (#40284)

Related to #40274

Previousy DescribeRole returns only roles with grants, this PR add
select role action to check role existence.

Also added database properties related option

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
This commit is contained in:
congqixia 2025-03-03 18:05:59 +08:00 committed by GitHub
parent 16aa123185
commit 16d468c875
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 19 deletions

View File

@ -62,17 +62,25 @@ type CreateDatabaseOption interface {
type createDatabaseOption struct {
dbName string
Properties map[string]string
}
func (opt *createDatabaseOption) Request() *milvuspb.CreateDatabaseRequest {
return &milvuspb.CreateDatabaseRequest{
DbName: opt.dbName,
Properties: entity.MapKvPairs(opt.Properties),
}
}
func (opt *createDatabaseOption) WithProperty(key string, val any) *createDatabaseOption {
opt.Properties[key] = fmt.Sprintf("%v", val)
return opt
}
func NewCreateDatabaseOption(dbName string) *createDatabaseOption {
return &createDatabaseOption{
dbName: dbName,
Properties: make(map[string]string),
}
}

View File

@ -128,30 +128,36 @@ func (c *Client) DropRole(ctx context.Context, opt DropRoleOption, callOpts ...g
}
func (c *Client) DescribeRole(ctx context.Context, option DescribeRoleOption, callOptions ...grpc.CallOption) (*entity.Role, error) {
req := option.Request()
var role *entity.Role
err := c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
resp, err := milvusService.SelectGrant(ctx, req, callOptions...)
if err := merr.CheckRPCCall(resp, err); err != nil {
roleResp, err := milvusService.SelectRole(ctx, option.SelectRoleRequest(), callOptions...)
if err := merr.CheckRPCCall(roleResp, err); err != nil {
return err
}
if len(resp.GetEntities()) == 0 {
if len(roleResp.GetResults()) == 0 {
return errors.New("role not found")
}
role = &entity.Role{
RoleName: req.GetEntity().GetRole().GetName(),
Privileges: lo.Map(resp.GetEntities(), func(g *milvuspb.GrantEntity, _ int) entity.GrantItem {
RoleName: roleResp.GetResults()[0].GetRole().GetName(),
}
resp, err := milvusService.SelectGrant(ctx, option.Request(), callOptions...)
if err := merr.CheckRPCCall(resp, err); err != nil {
return err
}
role.Privileges = lo.Map(resp.GetEntities(), func(g *milvuspb.GrantEntity, _ int) entity.GrantItem {
return entity.GrantItem{
Object: g.Object.GetName(),
Object: g.GetObject().GetName(),
ObjectName: g.GetObjectName(),
RoleName: g.GetRole().GetName(),
Grantor: g.GetGrantor().GetUser().GetName(),
Privilege: g.GetGrantor().GetPrivilege().GetName(),
}
}),
}
})
return nil
})
return role, err

View File

@ -234,6 +234,7 @@ func NewDropRoleOption(roleName string) *dropDropRoleOption {
}
type DescribeRoleOption interface {
SelectRoleRequest() *milvuspb.SelectRoleRequest
Request() *milvuspb.SelectGrantRequest
}
@ -241,6 +242,14 @@ type describeRoleOption struct {
roleName string
}
func (opt *describeRoleOption) SelectRoleRequest() *milvuspb.SelectRoleRequest {
return &milvuspb.SelectRoleRequest{
Role: &milvuspb.RoleEntity{
Name: opt.roleName,
},
}
}
func (opt *describeRoleOption) Request() *milvuspb.SelectGrantRequest {
return &milvuspb.SelectGrantRequest{
Entity: &milvuspb.GrantEntity{

View File

@ -299,6 +299,16 @@ func (s *RoleSuite) TestDescribeRole() {
s.Run("success", func() {
roleName := fmt.Sprintf("role_%s", s.randString(5))
s.mock.EXPECT().SelectRole(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, r *milvuspb.SelectRoleRequest) (*milvuspb.SelectRoleResponse, error) {
s.Equal(roleName, r.GetRole().GetName())
return &milvuspb.SelectRoleResponse{
Results: []*milvuspb.RoleResult{
{
Role: &milvuspb.RoleEntity{Name: roleName},
},
},
}, nil
}).Once()
s.mock.EXPECT().SelectGrant(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, r *milvuspb.SelectGrantRequest) (*milvuspb.SelectGrantResponse, error) {
s.Equal(roleName, r.GetEntity().GetRole().GetName())
return &milvuspb.SelectGrantResponse{
@ -329,7 +339,7 @@ func (s *RoleSuite) TestDescribeRole() {
})
s.Run("failure", func() {
s.mock.EXPECT().SelectGrant(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
s.mock.EXPECT().SelectRole(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
_, err := s.client.DescribeRole(ctx, NewDescribeRoleOption("role"))
s.Error(err)