@@ -382,6 +382,7 @@ function _M.new(opts)
382382 last_auth_time = now (), -- save last Authentication time
383383 last_refresh_jwt_err = nil ,
384384 jwt_token = nil , -- last Authentication token
385+ grpc_token = nil , -- token used in gRPC
385386 is_auth = not not (user and password ),
386387 user = user ,
387388 password = password ,
@@ -444,7 +445,19 @@ function _M.new(opts)
444445 end
445446 cli .conn = conn
446447
447- return setmetatable (cli , grpc_mt )
448+ cli = setmetatable (cli , grpc_mt )
449+
450+ if cli .user then
451+ local auth_req = {name = cli .user , password = cli .password }
452+ local res , err = cli :grpc_call (" etcdserverpb.Auth" , " Authenticate" , auth_req )
453+ if not res then
454+ return nil , err
455+ end
456+
457+ cli .grpc_token = res .body .token
458+ end
459+
460+ return cli
448461 end
449462
450463 local sema , err = semaphore .new ()
@@ -996,6 +1009,22 @@ local function create_watch_request(key, attr)
9961009end
9971010
9981011
1012+ local get_grpc_metadata
1013+ do
1014+ local metadata = {
1015+ {" token" , " " }
1016+ }
1017+ function get_grpc_metadata (self )
1018+ if self .grpc_token then
1019+ metadata [1 ][2 ] = self .grpc_token
1020+ return metadata
1021+ end
1022+
1023+ return nil
1024+ end
1025+ end
1026+
1027+
9991028function _grpc_M .create_grpc_watch_stream (self , key , attr , opts )
10001029 key = utils .get_real_key (self .key_prefix , key )
10011030 attr .range_end = get_range_end (key )
@@ -1012,6 +1041,8 @@ function _grpc_M.create_grpc_watch_stream(self, key, attr, opts)
10121041 self .call_opts .timeout = self .timeout * 1000
10131042 end
10141043
1044+ self .call_opts .metadata = get_grpc_metadata (self )
1045+
10151046 local st , err = conn :new_server_stream (" etcdserverpb.Watch" , " Watch" , req , self .call_opts )
10161047 if not st then
10171048 return nil , err
@@ -1132,6 +1163,7 @@ function _grpc_M.grpc_call(self, serv, meth, attr, key, val, opts)
11321163 self .call_opts .timeout = self .timeout * 1000
11331164 end
11341165 self .call_opts .int64_encoding = self .grpc .INT64_AS_STRING
1166+ self .call_opts .metadata = get_grpc_metadata (self )
11351167
11361168 local res , err = conn :call (serv , meth , attr , self .call_opts )
11371169 return self :convert_grpc_to_http_res (res ), err
0 commit comments