@@ -32,20 +32,6 @@ func parseMD(rawToken string) (username, password string) {
3232 return
3333}
3434
35- func validSourceID (ctx context.Context , authorization []string ) bool {
36- if len (authorization ) < 1 {
37- // log.Warn("key not found in header", zap.String("key", util.HeaderSourceID))
38- return false
39- }
40- // token format: base64<sourceID>
41- token := authorization [0 ]
42- sourceID , err := crypto .Base64Decode (token )
43- if err != nil {
44- return false
45- }
46- return sourceID == util .MemberCredID
47- }
48-
4935func GrpcAuthInterceptor (authFunc grpc_auth.AuthFunc ) grpc.UnaryServerInterceptor {
5036 return func (ctx context.Context , req interface {}, info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (interface {}, error ) {
5137 var newCtx context.Context
@@ -76,48 +62,44 @@ func AuthenticationInterceptor(ctx context.Context) (context.Context, error) {
7662 if globalMetaCache == nil {
7763 return nil , merr .WrapErrServiceUnavailable ("internal: Milvus Proxy is not ready yet. please wait" )
7864 }
79- // check:
80- // 1. if rpc call from a member (like index/query/data component)
81- // 2. if rpc call from sdk
65+ // check if rpc call from sdk
8266 if Params .CommonCfg .AuthorizationEnabled .GetAsBool () {
83- if ! validSourceID (ctx , md [strings .ToLower (util .HeaderSourceID )]) {
84- authStrArr := md [strings .ToLower (util .HeaderAuthorize )]
67+ authStrArr := md [strings .ToLower (util .HeaderAuthorize )]
8568
86- if len (authStrArr ) < 1 {
87- log .Warn ("key not found in header" )
88- return nil , status .Error (codes .Unauthenticated , "missing authorization in header" )
89- }
69+ if len (authStrArr ) < 1 {
70+ log .Warn ("key not found in header" )
71+ return nil , status .Error (codes .Unauthenticated , "missing authorization in header" )
72+ }
9073
91- // token format: base64<username:password>
92- // token := strings.TrimPrefix(authorization[0], "Bearer ")
93- token := authStrArr [0 ]
94- rawToken , err := crypto .Base64Decode (token )
74+ // token format: base64<username:password>
75+ // token := strings.TrimPrefix(authorization[0], "Bearer ")
76+ token := authStrArr [0 ]
77+ rawToken , err := crypto .Base64Decode (token )
78+ if err != nil {
79+ log .Warn ("fail to decode the token" , zap .Error (err ))
80+ return nil , status .Error (codes .Unauthenticated , "invalid token format" )
81+ }
82+
83+ if ! strings .Contains (rawToken , util .CredentialSeperator ) {
84+ user , err := VerifyAPIKey (rawToken )
9585 if err != nil {
96- log .Warn ("fail to decode the token " , zap .Error (err ))
97- return nil , status .Error (codes .Unauthenticated , "invalid token format " )
86+ log .Warn ("fail to verify apikey " , zap .Error (err ))
87+ return nil , status .Error (codes .Unauthenticated , "auth check failure, please check api key is correct " )
9888 }
99-
100- if ! strings .Contains (rawToken , util .CredentialSeperator ) {
101- user , err := VerifyAPIKey (rawToken )
102- if err != nil {
103- log .Warn ("fail to verify apikey" , zap .Error (err ))
104- return nil , status .Error (codes .Unauthenticated , "auth check failure, please check api key is correct" )
105- }
106- metrics .UserRPCCounter .WithLabelValues (user ).Inc ()
107- userToken := fmt .Sprintf ("%s%s%s" , user , util .CredentialSeperator , util .PasswordHolder )
108- md [strings .ToLower (util .HeaderAuthorize )] = []string {crypto .Base64Encode (userToken )}
109- md [util .HeaderToken ] = []string {rawToken }
110- ctx = metadata .NewIncomingContext (ctx , md )
111- } else {
112- // username+password authentication
113- username , password := parseMD (rawToken )
114- if ! passwordVerify (ctx , username , password , globalMetaCache ) {
115- log .Warn ("fail to verify password" , zap .String ("username" , username ))
116- // NOTE: don't use the merr, because it will cause the wrong retry behavior in the sdk
117- return nil , status .Error (codes .Unauthenticated , "auth check failure, please check username and password are correct" )
118- }
119- metrics .UserRPCCounter .WithLabelValues (username ).Inc ()
89+ metrics .UserRPCCounter .WithLabelValues (user ).Inc ()
90+ userToken := fmt .Sprintf ("%s%s%s" , user , util .CredentialSeperator , util .PasswordHolder )
91+ md [strings .ToLower (util .HeaderAuthorize )] = []string {crypto .Base64Encode (userToken )}
92+ md [util .HeaderToken ] = []string {rawToken }
93+ ctx = metadata .NewIncomingContext (ctx , md )
94+ } else {
95+ // username+password authentication
96+ username , password := parseMD (rawToken )
97+ if ! passwordVerify (ctx , username , password , globalMetaCache ) {
98+ log .Warn ("fail to verify password" , zap .String ("username" , username ))
99+ // NOTE: don't use the merr, because it will cause the wrong retry behavior in the sdk
100+ return nil , status .Error (codes .Unauthenticated , "auth check failure, please check username and password are correct" )
120101 }
102+ metrics .UserRPCCounter .WithLabelValues (username ).Inc ()
121103 }
122104 }
123105 return ctx , nil
0 commit comments