Skip to content

Commit 3a1b2ce

Browse files
jiangke-srkejiang
andauthored
create default root user for authentication (milvus-io#16545) (milvus-io#16549)
Signed-off-by: kejiang <[email protected]> Co-authored-by: kejiang <[email protected]>
1 parent e86cb5a commit 3a1b2ce

File tree

8 files changed

+63
-9
lines changed

8 files changed

+63
-9
lines changed

internal/proxy/impl.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"os"
2424
"strconv"
2525

26+
"github.com/milvus-io/milvus/internal/util"
27+
2628
"go.uber.org/zap"
2729

2830
"github.com/milvus-io/milvus/internal/common"
@@ -4165,6 +4167,12 @@ func (node *Proxy) UpdateCredential(ctx context.Context, req *milvuspb.UpdateCre
41654167

41664168
func (node *Proxy) DeleteCredential(ctx context.Context, req *milvuspb.DeleteCredentialRequest) (*commonpb.Status, error) {
41674169
log.Debug("DeleteCredential", zap.String("role", typeutil.RootCoordRole), zap.String("username", req.Username))
4170+
if req.Username == util.UserRoot {
4171+
return &commonpb.Status{
4172+
ErrorCode: commonpb.ErrorCode_DeleteCredentialFailure,
4173+
Reason: "user root cannot be deleted",
4174+
}, nil
4175+
}
41684176
result, err := node.rootCoord.DeleteCredential(ctx, req)
41694177
if err != nil { // for error like conntext timeout etc.
41704178
log.Error("delete credential fail", zap.String("username", req.Username), zap.Error(err))

internal/proxy/proxy_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ func TestProxy(t *testing.T) {
19711971
})
19721972

19731973
username := "test_username_" + funcutil.RandomString(15)
1974-
password := "xxx"
1974+
password := "password"
19751975

19761976
wg.Add(1)
19771977
t.Run("credential CREATE api", func(t *testing.T) {
@@ -2020,7 +2020,7 @@ func TestProxy(t *testing.T) {
20202020
defer wg.Done()
20212021

20222022
// 2. update credential
2023-
newPassword := "yyy"
2023+
newPassword := "new_password"
20242024
constructUpdateCredentialRequest := func() *milvuspb.UpdateCredentialRequest {
20252025
return &milvuspb.UpdateCredentialRequest{
20262026
Base: nil,
@@ -2076,7 +2076,7 @@ func TestProxy(t *testing.T) {
20762076
defer wg.Done()
20772077

20782078
// 3. get credential
2079-
newPassword := "yyy"
2079+
newPassword := "new_password"
20802080
constructGetCredentialRequest := func() *rootcoordpb.GetCredentialRequest {
20812081
return &rootcoordpb.GetCredentialRequest{
20822082
Base: nil,
@@ -2902,6 +2902,14 @@ func TestProxy(t *testing.T) {
29022902
assert.NotEqual(t, commonpb.ErrorCode_Success, resp.ErrorCode)
29032903
})
29042904

2905+
wg.Add(1)
2906+
t.Run("DeleteCredential fail, user root cannot be deleted", func(t *testing.T) {
2907+
defer wg.Done()
2908+
resp, err := proxy.DeleteCredential(shortCtx, &milvuspb.DeleteCredentialRequest{Username: "root"})
2909+
assert.NoError(t, err)
2910+
assert.NotEqual(t, commonpb.ErrorCode_Success, resp.ErrorCode)
2911+
})
2912+
29052913
wg.Add(1)
29062914
t.Run("DeleteCredential fail, timeout", func(t *testing.T) {
29072915
defer wg.Done()

internal/proxy/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ func ValidateUsername(username string) error {
520520
}
521521

522522
func ValidatePassword(password string) error {
523-
if int64(len(password)) > Params.ProxyCfg.MaxPasswordLength {
524-
msg := "The length of password must be less than " +
525-
strconv.FormatInt(Params.ProxyCfg.MaxPasswordLength, 10) + " characters."
523+
if int64(len(password)) < Params.ProxyCfg.MinPasswordLength || int64(len(password)) > Params.ProxyCfg.MaxPasswordLength {
524+
msg := "The length of password must be great than " + strconv.FormatInt(Params.ProxyCfg.MinPasswordLength, 10) +
525+
" and less than " + strconv.FormatInt(Params.ProxyCfg.MaxPasswordLength, 10) + " characters."
526526
return errors.New(msg)
527527
}
528528
return nil

internal/proxy/util_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,11 @@ func TestValidateUsername(t *testing.T) {
550550
func TestValidatePassword(t *testing.T) {
551551
Params.InitOnce()
552552
// only spaces
553-
res := ValidatePassword(" ")
554-
assert.Nil(t, res)
553+
res := ValidatePassword("")
554+
assert.NotNil(t, res)
555555
//
556556
res = ValidatePassword("1abc")
557-
assert.Nil(t, res)
557+
assert.NotNil(t, res)
558558
//
559559
res = ValidatePassword("a1^7*).,")
560560
assert.Nil(t, res)

internal/rootcoord/root_coord.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
"syscall"
3030
"time"
3131

32+
"github.com/milvus-io/milvus/internal/util"
33+
"github.com/milvus-io/milvus/internal/util/crypto"
34+
3235
"github.com/milvus-io/milvus/internal/util/dependency"
3336

3437
"github.com/golang/protobuf/proto"
@@ -1119,6 +1122,13 @@ func (c *Core) Init() error {
11191122
c.impTaskKv,
11201123
c.CallImportService,
11211124
)
1125+
// init data
1126+
encryptedRootPassword, _ := crypto.PasswordEncrypt(util.DefaultRootPassword)
1127+
initError = c.MetaTable.AddCredential(&internalpb.CredentialInfo{Username: util.UserRoot, EncryptedPassword: encryptedRootPassword})
1128+
if initError != nil {
1129+
return
1130+
}
1131+
log.Debug("RootCoord init user root done")
11221132
})
11231133
if initError != nil {
11241134
log.Debug("RootCoord init error", zap.Error(initError))

internal/util/constant.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ const (
2626
// MemberCredID id for Milvus members (data/index/query node/coord component)
2727
MemberCredID = "@@milvus-member@@"
2828
CredentialSeperator = ":"
29+
UserRoot = "root"
30+
DefaultRootPassword = "Milvus"
2931
)

internal/util/paramtable/component_param.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ type proxyConfig struct {
408408
MsgStreamTimeTickBufSize int64
409409
MaxNameLength int64
410410
MaxUsernameLength int64
411+
MinPasswordLength int64
411412
MaxPasswordLength int64
412413
MaxFieldNum int64
413414
MaxShardNum int32
@@ -433,6 +434,7 @@ func (p *proxyConfig) init(base *BaseTable) {
433434

434435
p.initMsgStreamTimeTickBufSize()
435436
p.initMaxNameLength()
437+
p.initMinPasswordLength()
436438
p.initMaxUsernameLength()
437439
p.initMaxPasswordLength()
438440
p.initMaxFieldNum()
@@ -477,6 +479,15 @@ func (p *proxyConfig) initMaxUsernameLength() {
477479
p.MaxUsernameLength = maxUsernameLength
478480
}
479481

482+
func (p *proxyConfig) initMinPasswordLength() {
483+
str := p.Base.LoadWithDefault("proxy.minPasswordLength", "6")
484+
minPasswordLength, err := strconv.ParseInt(str, 10, 64)
485+
if err != nil {
486+
panic(err)
487+
}
488+
p.MinPasswordLength = minPasswordLength
489+
}
490+
480491
func (p *proxyConfig) initMaxPasswordLength() {
481492
str := p.Base.LoadWithDefault("proxy.maxPasswordLength", "256")
482493
maxPasswordLength, err := strconv.ParseInt(str, 10, 64)

internal/util/paramtable/component_param_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ func TestComponentParam(t *testing.T) {
157157
Params.initMaxNameLength()
158158
})
159159

160+
shouldPanic(t, "proxy.maxUsernameLength", func() {
161+
Params.Base.Save("proxy.maxUsernameLength", "abc")
162+
Params.initMaxUsernameLength()
163+
})
164+
165+
shouldPanic(t, "proxy.minPasswordLength", func() {
166+
Params.Base.Save("proxy.minPasswordLength", "abc")
167+
Params.initMinPasswordLength()
168+
})
169+
170+
shouldPanic(t, "proxy.maxPasswordLength", func() {
171+
Params.Base.Save("proxy.maxPasswordLength", "abc")
172+
Params.initMaxPasswordLength()
173+
})
174+
160175
shouldPanic(t, "proxy.maxFieldNum", func() {
161176
Params.Base.Save("proxy.maxFieldNum", "abc")
162177
Params.initMaxFieldNum()

0 commit comments

Comments
 (0)