Skip to content

Commit

Permalink
RBAC grant v2 api
Browse files Browse the repository at this point in the history
Signed-off-by: shaoting-huang <[email protected]>
  • Loading branch information
shaoting-huang committed Nov 21, 2024
1 parent 3d596ff commit bd66ccf
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ type Client interface {
Grant(ctx context.Context, role string, objectType entity.PriviledgeObjectType, object string, privilege string, options ...entity.OperatePrivilegeOption) error
// Revoke removes privilege from role.
Revoke(ctx context.Context, role string, objectType entity.PriviledgeObjectType, object string, privilege string, options ...entity.OperatePrivilegeOption) error
// GrantV2 adds privilege for role.
GrantV2(ctx context.Context, role string, privilege string, dbName string, colName string) error
// RevokeV2 removes privilege from role.
RevokeV2(ctx context.Context, role string, privilege string, dbName string, colName string) error

// GetLoadingProgress get the collection or partitions loading progress
GetLoadingProgress(ctx context.Context, collectionName string, partitionNames []string) (int64, error)
Expand Down
10 changes: 10 additions & 0 deletions client/client_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ const (
MGetLoadingProgress ServiceMethod = 110
MGetLoadState ServiceMethod = 111
MAlterCollectionField ServiceMethod = 112
MOperatePrivilegeV2 ServiceMethod = 113

MCreatePartition ServiceMethod = 201
MDropPartition ServiceMethod = 202
Expand Down Expand Up @@ -501,6 +502,15 @@ func (m *MockServer) AlterCollection(ctx context.Context, req *milvuspb.AlterCol
return SuccessStatus()
}

func (m *MockServer) OperatePrivilegeV2(ctx context.Context, req *milvuspb.OperatePrivilegeV2Request) (*commonpb.Status, error) {
f := m.GetInjection(MOperatePrivilegeV2)
if f != nil {
r, err := f(ctx, req)
return r.(*commonpb.Status), err
}
return SuccessStatus()
}

func (m *MockServer) CreatePartition(ctx context.Context, req *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) {
f := m.GetInjection(MCreatePartition)
if f != nil {
Expand Down
56 changes: 56 additions & 0 deletions client/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,62 @@ func (c *GrpcClient) Revoke(ctx context.Context, role string, objectType entity.
return handleRespStatus(resp)
}

// GrantV2 adds object privilege for role without object type
func (c *GrpcClient) GrantV2(ctx context.Context, role string, privilege string, dbName string, colName string) error {
if c.Service == nil {
return ErrClientNotReady
}

req := &milvuspb.OperatePrivilegeV2Request{
Role: &milvuspb.RoleEntity{
Name: role,
},
Grantor: &milvuspb.GrantorEntity{
Privilege: &milvuspb.PrivilegeEntity{
Name: privilege,
},
},
Type: milvuspb.OperatePrivilegeType_Grant,
DbName: dbName,
CollectionName: colName,
}

resp, err := c.Service.OperatePrivilegeV2(ctx, req)
if err != nil {
return err
}

return handleRespStatus(resp)
}

// Revoke removes privilege from role without object type
func (c *GrpcClient) RevokeV2(ctx context.Context, role string, privilege string, dbName string, colName string) error {
if c.Service == nil {
return ErrClientNotReady
}

req := &milvuspb.OperatePrivilegeV2Request{
Role: &milvuspb.RoleEntity{
Name: role,
},
Grantor: &milvuspb.GrantorEntity{
Privilege: &milvuspb.PrivilegeEntity{
Name: privilege,
},
},
Type: milvuspb.OperatePrivilegeType_Revoke,
DbName: dbName,
CollectionName: colName,
}

resp, err := c.Service.OperatePrivilegeV2(ctx, req)
if err != nil {
return err
}

return handleRespStatus(resp)
}

func (c *GrpcClient) BackupRBAC(ctx context.Context) (*entity.RBACMeta, error) {
if c.Service == nil {
return nil, ErrClientNotReady
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-faker/faker/v4 v4.1.0
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241108105827-266fb751b620
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241120015424-93892e628c69
github.com/stretchr/testify v1.8.1
github.com/tidwall/gjson v1.14.4
github.com/x448/float16 v0.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241108105827-266fb751b620 h1:0IWUDtDloift7cQHalhdjuVkL/3qSeiXFqR7MofZBkg=
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241108105827-266fb751b620/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241120015424-93892e628c69 h1:Qt0Bv2Fum3EX3OlkuQYHJINBzeU4oEuHy2lXSfB/gZw=
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241120015424-93892e628c69/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
55 changes: 55 additions & 0 deletions mocks/MilvusServiceServer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd66ccf

Please sign in to comment.