Skip to content

Commit

Permalink
fix: [GoSDK] Pass base64 passwd content instead of raw data (#40268)
Browse files Browse the repository at this point in the history
Related to #40261

Also add some options for create collection options and refine some
behavior

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Feb 28, 2025
1 parent bc8e02d commit 0c74e40
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
6 changes: 5 additions & 1 deletion client/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ func (gi GenericIndex) Params() map[string]string {
return m
}

func (gi GenericIndex) WithMetricType(metricType MetricType) {
gi.baseIndex.metricType = metricType
}

// NewGenericIndex create generic index instance
func NewGenericIndex(name string, params map[string]string) Index {
func NewGenericIndex(name string, params map[string]string) GenericIndex {
return GenericIndex{
baseIndex: baseIndex{
name: name,
Expand Down
63 changes: 62 additions & 1 deletion client/milvusclient/collection_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,67 @@ func ExampleClient_CreateCollection_ttl() {
}
}

func ExampleClient_CreateCollection_quickSetup() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

collectionName := `quick_setup_1`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}

err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512))
if err != nil {
// handle error
}
}

func ExampleClient_CreateCollection_quickSetupWithIndexParams() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

collectionName := `quick_setup_2`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}

err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).WithIndexOptions(
milvusclient.NewCreateIndexOption(collectionName, "vector", index.NewHNSWIndex(entity.L2, 64, 128)),
))
if err != nil {
log.Println(err.Error())
// handle error
}
}

func ExampleClient_CreateCollection_quickSetupCustomize() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

collectionName := `quick_setup_3`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}

err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).
WithVarcharPK(true, 64).
WithShardNum(1),
)
if err != nil {
log.Println(err.Error())
// handle error
}
}

func ExampleClient_CreateCollection_consistencyLevel() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -245,7 +306,7 @@ func ExampleClient_RenameCollection() {
}
}

func ExampleClient_AlterCollection_setTTL() {
func ExampleClient_AlterCollectionProperties_setTTL() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
26 changes: 23 additions & 3 deletions client/milvusclient/collection_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (opt *createCollectionOption) WithVarcharPK(varcharPK bool, maxLen int) *cr
}

func (opt *createCollectionOption) WithIndexOptions(indexOpts ...CreateIndexOption) *createCollectionOption {
opt.indexOptions = append(opt.indexOptions, indexOpts...)
opt.indexOptions = indexOpts
return opt
}

Expand All @@ -102,6 +102,26 @@ func (opt *createCollectionOption) WithConsistencyLevel(cl entity.ConsistencyLev
return opt
}

func (opt *createCollectionOption) WithMetricType(metricType entity.MetricType) *createCollectionOption {
opt.metricType = metricType
return opt
}

func (opt *createCollectionOption) WithPKFieldName(name string) *createCollectionOption {
opt.pkFieldName = name
return opt
}

func (opt *createCollectionOption) WithVectorFieldName(name string) *createCollectionOption {
opt.vectorFieldName = name
return opt
}

func (opt *createCollectionOption) WithNumPartitions(numPartitions int64) *createCollectionOption {
opt.numPartitions = numPartitions
return opt
}

func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest {
// fast create collection
if opt.isFast {
Expand Down Expand Up @@ -140,12 +160,12 @@ func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest {

func (opt *createCollectionOption) Indexes() []CreateIndexOption {
// fast create
if opt.isFast {
if opt.isFast && opt.indexOptions == nil {
return []CreateIndexOption{
NewCreateIndexOption(opt.name, opt.vectorFieldName, index.NewGenericIndex("", map[string]string{})),
}
}
return nil
return opt.indexOptions
}

func (opt *createCollectionOption) IsFast() bool {
Expand Down
4 changes: 3 additions & 1 deletion client/milvusclient/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func (s *CollectionSuite) TestCreateCollectionOptions() {
s.True(collSchema.GetEnableDynamicField())

collectionName = fmt.Sprintf("test_collection_%s", s.randString(6))
opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).WithDynamicSchema(false)
opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).
WithPKFieldName("pk").WithVectorFieldName("embedding").WithMetricType(entity.L2).
WithDynamicSchema(false)
req = opt.Request()
s.Equal(collectionName, req.GetCollectionName())
s.EqualValues(1, req.GetShardsNum())
Expand Down
7 changes: 4 additions & 3 deletions client/milvusclient/rbac_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package milvusclient

import (
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
"github.com/milvus-io/milvus/pkg/v2/util/crypto"
)

type ListUserOption interface {
Expand Down Expand Up @@ -71,7 +72,7 @@ type createUserOption struct {
func (opt *createUserOption) Request() *milvuspb.CreateCredentialRequest {
return &milvuspb.CreateCredentialRequest{
Username: opt.userName,
Password: opt.password,
Password: crypto.Base64Encode(opt.password),
}
}

Expand All @@ -95,8 +96,8 @@ type updatePasswordOption struct {
func (opt *updatePasswordOption) Request() *milvuspb.UpdateCredentialRequest {
return &milvuspb.UpdateCredentialRequest{
Username: opt.userName,
OldPassword: opt.oldPassword,
NewPassword: opt.newPassword,
OldPassword: crypto.Base64Encode(opt.oldPassword),
NewPassword: crypto.Base64Encode(opt.newPassword),
}
}

Expand Down
7 changes: 4 additions & 3 deletions client/milvusclient/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
"github.com/milvus-io/milvus/pkg/v2/util/crypto"
"github.com/milvus-io/milvus/pkg/v2/util/merr"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *UserSuite) TestCreateUser() {
password := s.randString(12)
s.mock.EXPECT().CreateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ccr *milvuspb.CreateCredentialRequest) (*commonpb.Status, error) {
s.Equal(userName, ccr.GetUsername())
s.Equal(password, ccr.GetPassword())
s.Equal(crypto.Base64Encode(password), ccr.GetPassword())
return merr.Success(), nil
}).Once()

Expand All @@ -121,8 +122,8 @@ func (s *UserSuite) TestUpdatePassword() {
newPassword := s.randString(12)
s.mock.EXPECT().UpdateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ucr *milvuspb.UpdateCredentialRequest) (*commonpb.Status, error) {
s.Equal(userName, ucr.GetUsername())
s.Equal(oldPassword, ucr.GetOldPassword())
s.Equal(newPassword, ucr.GetNewPassword())
s.Equal(crypto.Base64Encode(oldPassword), ucr.GetOldPassword())
s.Equal(crypto.Base64Encode(newPassword), ucr.GetNewPassword())
return merr.Success(), nil
}).Once()

Expand Down

0 comments on commit 0c74e40

Please sign in to comment.