Skip to content

Commit 33b537a

Browse files
authored
feat(gRPC): support user/password (#198)
1 parent 80bc2d5 commit 33b537a

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/resty/etcd/proto.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,21 @@ service Lease {
499499
// LeaseLeases lists all existing leases.
500500
rpc LeaseLeases(LeaseLeasesRequest) returns (LeaseLeasesResponse) {}
501501
}
502+
503+
message AuthenticateRequest {
504+
string name = 1;
505+
string password = 2;
506+
}
507+
508+
message AuthenticateResponse {
509+
ResponseHeader header = 1;
510+
// token is an authorized token that can be used in succeeding RPCs
511+
string token = 2;
512+
}
513+
514+
service Auth {
515+
// Authenticate processes an authenticate request.
516+
rpc Authenticate(AuthenticateRequest) returns (AuthenticateResponse) {}
517+
}
518+
502519
]]

lib/resty/etcd/v3.lua

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)
9961009
end
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+
9991028
function _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

Comments
 (0)