2022-04-11 19:49:34 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/util"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/util/crypto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validAuth validates the authentication
|
|
|
|
func TestValidAuth(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
// no metadata
|
|
|
|
res := validAuth(ctx, nil)
|
|
|
|
assert.False(t, res)
|
|
|
|
// illegal metadata
|
|
|
|
res = validAuth(ctx, []string{"xxx"})
|
|
|
|
assert.False(t, res)
|
|
|
|
// normal metadata
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
|
|
|
queryCoord := &MockQueryCoordClientInterface{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2022-04-11 19:49:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
res = validAuth(ctx, []string{crypto.Base64Encode("mockUser:mockPass")})
|
|
|
|
assert.True(t, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidSourceID(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
// no metadata
|
|
|
|
res := validSourceID(ctx, nil)
|
|
|
|
assert.False(t, res)
|
|
|
|
// illegal metadata
|
|
|
|
res = validSourceID(ctx, []string{"invalid_sourceid"})
|
|
|
|
assert.False(t, res)
|
|
|
|
// normal sourceId
|
|
|
|
res = validSourceID(ctx, []string{crypto.Base64Encode(util.MemberCredID)})
|
|
|
|
assert.True(t, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthenticationInterceptor(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-04-19 16:35:39 +08:00
|
|
|
Params.CommonCfg.AuthorizationEnabled = true // mock authorization is turned on
|
2022-04-11 19:49:34 +08:00
|
|
|
// no metadata
|
|
|
|
_, err := AuthenticationInterceptor(ctx)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
// mock metacache
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
|
|
|
queryCoord := &MockQueryCoordClientInterface{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err = InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2022-04-11 19:49:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
// with invalid metadata
|
|
|
|
md := metadata.Pairs("xxx", "yyy")
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
_, err = AuthenticationInterceptor(ctx)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
// with valid username/password
|
|
|
|
md = metadata.Pairs(util.HeaderAuthorize, crypto.Base64Encode("mockUser:mockPass"))
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
_, err = AuthenticationInterceptor(ctx)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
// with valid sourceId
|
|
|
|
md = metadata.Pairs("sourceid", crypto.Base64Encode(util.MemberCredID))
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
_, err = AuthenticationInterceptor(ctx)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|