Skip to content

Commit 4a6da8d

Browse files
authored
fix: SSL handshaking isn't executed in request_chunk (for watch) (#89)
1 parent c67af05 commit 4a6da8d

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

lib/resty/etcd/v3.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,17 @@ function _M.new(opts)
146146
end
147147

148148
for _, host in ipairs(http_hosts) do
149-
local m, err = re_match(host, [[\/\/([\d.\w]+):(\d+)]], "jo")
149+
local m, err = re_match(host, [[([^\/]+)\:\/\/([\d.\w]+):(\d+)]], "jo")
150150
if not m then
151151
return nil, "invalid http host: " .. err
152152
end
153153

154154
tab_insert(endpoints, {
155155
full_prefix = host .. utils.normalize(api_prefix),
156156
http_host = host,
157-
host = m[1] or "127.0.0.1",
158-
port = m[2] or "2379",
157+
scheme = m[1],
158+
host = m[2] or "127.0.0.1",
159+
port = m[3] or "2379",
159160
api_prefix = api_prefix,
160161
})
161162
end
@@ -450,7 +451,7 @@ local function txn(self, opts_arg, compare, success, failure)
450451
end
451452

452453

453-
local function request_chunk(self, method, host, port, path, opts, timeout)
454+
local function request_chunk(self, method, scheme, host, port, path, opts, timeout)
454455
local body, err, _
455456
if opts and opts.body and tab_nkeys(opts.body) > 0 then
456457
body, err = encode_json(opts.body)
@@ -493,6 +494,18 @@ local function request_chunk(self, method, host, port, path, opts, timeout)
493494
return nil, err
494495
end
495496

497+
if scheme == "https" then
498+
local verify = true
499+
if self.ssl_verify == false then
500+
verify = false
501+
end
502+
503+
ok, err = http_cli:ssl_handshake(nil, host, verify)
504+
if not ok then
505+
return nil, err
506+
end
507+
end
508+
496509
local res
497510
res, err = http_cli:request({
498511
method = method,
@@ -639,6 +652,7 @@ local function watch(self, key, attr)
639652
local endpoint = choose_endpoint(self)
640653

641654
local callback_fun, err, http_cli = request_chunk(self, 'POST',
655+
endpoint.scheme,
642656
endpoint.host,
643657
endpoint.port,
644658
endpoint.api_prefix .. '/watch', opts,

t/v3/tls.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,61 @@ GET /t
104104
[error]
105105
--- response_body
106106
err: 18: self signed certificate
107+
108+
109+
110+
=== TEST 3: watch(key)
111+
--- http_config eval: $::HttpConfig
112+
--- config
113+
location /t {
114+
content_by_lua_block {
115+
local etcd, err = require("resty.etcd").new({
116+
protocol = "v3",
117+
ssl_verify = false,
118+
http_host = {
119+
"https://127.0.0.1:12379",
120+
"https://127.0.0.1:22379",
121+
"https://127.0.0.1:32379",
122+
}
123+
})
124+
125+
check_res(etcd, err)
126+
127+
local res, err = etcd:set("/test", "abc")
128+
check_res(res, err)
129+
130+
ngx.timer.at(0.1, function ()
131+
etcd:set("/test", "bcd3")
132+
end)
133+
134+
local cur_time = ngx.now()
135+
local body_chunk_fun, err = etcd:watch("/test", {timeout = 0.5})
136+
if not body_chunk_fun then
137+
ngx.say("failed to watch: ", err)
138+
end
139+
140+
local idx = 0
141+
while true do
142+
local chunk, err = body_chunk_fun()
143+
144+
if not chunk then
145+
if err then
146+
ngx.say(err)
147+
end
148+
break
149+
end
150+
151+
idx = idx + 1
152+
ngx.say(idx, ": ", require("cjson").encode(chunk.result))
153+
end
154+
}
155+
}
156+
--- request
157+
GET /t
158+
--- no_error_log
159+
[error]
160+
--- response_body_like eval
161+
qr/1:.*"created":true.*
162+
2:.*"value":"bcd3".*
163+
timeout/
164+
--- timeout: 5

utils/check-lua-code-style.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)