-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SSL Support - completion of PR#5 #36
Open
ElvinEfendi
wants to merge
26
commits into
openresty:master
Choose a base branch
from
Shopify:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3f077a0
add ssl support
sitano e9a373d
reuse ctx.type to allow https
sitano 282f5b8
Merge branch 'master' into ssl_support
ElvinEfendi 0ee70dd
a blank line before the else line
ElvinEfendi 212b25a
session reuse
ElvinEfendi 19a0b13
initial ssl testing
ElvinEfendi 20e6ba3
when ssl_verify, fail if certificate missmatch
ElvinEfendi a63f03f
added tests for SSL health check
ElvinEfendi 6819ea7
removed trailing whitespace
ElvinEfendi 3034363
assert that ssl session is reused
ElvinEfendi e568b74
make ssl session reuse optional
ElvinEfendi 44fcbef
document ssl_reuse_session flag
ElvinEfendi 9eaa01c
return test plan back with the adjusted number
ElvinEfendi 0623e89
Merge pull request #1 from Shopify/ssl_support
ElvinEfendi ba4bdf3
avoid permission requirement by using port > 1024
ElvinEfendi 62ff3e1
Merge pull request #2 from Shopify/use-different-port
ElvinEfendi 683b771
make sure session is declared as local variable
ElvinEfendi 63eaad5
make sure line lengths do not exceed 80
ElvinEfendi ec633a8
use new *_by_lua_block directive
ElvinEfendi 109526c
Merge pull request #3 from Shopify/comply-with-lua-releng-requirements
ElvinEfendi e09b29e
bump version
ElvinEfendi 6227b39
add missing comma
ElvinEfendi ce88ef0
let users to pass custom server_name to tcpsock:sslhandshake function
ElvinEfendi bb498ff
Merge pull request #5 from Shopify/custom-server-name-option
ElvinEfendi 6731f95
add status_table API with tests
ElvinEfendi 5a26fec
Merge pull request #4 from Shopify/status-table-api
ElvinEfendi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ local wait = ngx.thread.wait | |
local pcall = pcall | ||
|
||
local _M = { | ||
_VERSION = '0.03' | ||
_VERSION = '0.05' | ||
} | ||
|
||
if not ngx.config | ||
|
@@ -230,16 +230,29 @@ local function check_peer(ctx, id, peer, is_backup) | |
ok, err = sock:connect(name) | ||
end | ||
if not ok then | ||
if not peer.down then | ||
errlog("failed to connect to ", name, ": ", err) | ||
return peer_error(ctx, is_backup, id, peer, | ||
"failed to connect to ", name, ": ", err) | ||
end | ||
|
||
if ctx.type == "https" then | ||
local session, err = sock:sslhandshake(ctx.session, | ||
ctx.ssl_server_name or name, | ||
ctx.ssl_verify) | ||
if not session then | ||
peer_error(ctx, is_backup, id, peer, | ||
"failed to do SSL handshake: ", name, ": ", err) | ||
return sock:close() | ||
end | ||
if ctx.ssl_reuse_session then | ||
ctx.session = session | ||
end | ||
return peer_fail(ctx, is_backup, id, peer) | ||
end | ||
|
||
local bytes, err = sock:send(req) | ||
if not bytes then | ||
return peer_error(ctx, is_backup, id, peer, | ||
"failed to send request to ", name, ": ", err) | ||
peer_error(ctx, is_backup, id, peer, | ||
"failed to send request to ", name, ": ", err) | ||
return sock:close() | ||
end | ||
|
||
local status_line, err = sock:receive() | ||
|
@@ -260,16 +273,15 @@ local function check_peer(ctx, id, peer, is_backup) | |
peer_error(ctx, is_backup, id, peer, | ||
"bad status line from ", name, ": ", | ||
status_line) | ||
sock:close() | ||
return | ||
return sock:close() | ||
end | ||
|
||
local status = tonumber(sub(status_line, from, to)) | ||
if not statuses[status] then | ||
peer_error(ctx, is_backup, id, peer, "bad status code from ", | ||
name, ": ", status) | ||
sock:close() | ||
return | ||
peer_error(ctx, is_backup, id, peer, | ||
"bad status code from ", name, ": ", | ||
status) | ||
return sock:close() | ||
end | ||
end | ||
|
||
|
@@ -530,10 +542,14 @@ function _M.spawn_checker(opts) | |
return nil, "\"type\" option required" | ||
end | ||
|
||
if typ ~= "http" then | ||
return nil, "only \"http\" type is supported right now" | ||
if typ ~= "http" and typ ~= "https" then | ||
return nil, "no support for this protocol type" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to point out in the error message what the unsupported type is |
||
end | ||
|
||
local ssl_verify = opts.ssl_verify and true | ||
|
||
local ssl_reuse_session = opts.ssl_reuse_session and true | ||
|
||
local http_req = opts.http_req | ||
if not http_req then | ||
return nil, "\"http_req\" option required" | ||
|
@@ -611,6 +627,10 @@ function _M.spawn_checker(opts) | |
upstream = u, | ||
primary_peers = preprocess_peers(ppeers), | ||
backup_peers = preprocess_peers(bpeers), | ||
type = typ, | ||
ssl_verify = ssl_verify, | ||
ssl_reuse_session = ssl_reuse_session, | ||
ssl_server_name = opts.ssl_server_name, | ||
http_req = http_req, | ||
timeout = timeout, | ||
interval = interval, | ||
|
@@ -620,6 +640,7 @@ function _M.spawn_checker(opts) | |
statuses = statuses, | ||
version = 0, | ||
concurrency = concur, | ||
session = nil, | ||
} | ||
|
||
local ok, err = new_timer(0, check, ctx) | ||
|
@@ -701,4 +722,67 @@ function _M.status_page() | |
return concat(bits) | ||
end | ||
|
||
local function gen_peers_status_table(dict, peers, name, is_backup) | ||
local npeers = #peers | ||
|
||
for i = 1, npeers do | ||
local peer = peers[i] | ||
|
||
local oks, err = dict:get(gen_peer_key("ok:", name, is_backup, i - 1)) | ||
if oks then | ||
peer.checks_ok = oks | ||
if oks > 0 then | ||
peer.unhealthy = false | ||
peer.checks_fail = 0 | ||
end | ||
end | ||
|
||
if peer.unhealthy == nil then | ||
local key = gen_peer_key("nok:", name, is_backup, i - 1) | ||
local fails, err = dict:get(key) | ||
if fails then | ||
peer.checks_fail = fails | ||
if fails > 0 then | ||
peer.unhealthy = true | ||
peer.checks_ok = 0 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
function _M.status_table(shm) | ||
local result = {} | ||
local dict = shared[shm] | ||
|
||
local us, err = get_upstreams() | ||
if not us then | ||
return nil, "failed to get upstream names: " .. err | ||
end | ||
|
||
local n = #us | ||
for i = 1, n do | ||
local u = us[i] | ||
|
||
local ppeers, err = get_primary_peers(u) | ||
if not ppeers then | ||
return nil, "failed to get primary peers: " .. err | ||
end | ||
gen_peers_status_table(dict, ppeers, u, false) | ||
|
||
local bpeers, err = get_backup_peers(u) | ||
if not bpeers then | ||
return nil, "failed to get backup peers: " .. err | ||
end | ||
gen_peers_status_table(dict, bpeers, u, true) | ||
|
||
result[u] = { | ||
primary_peers = ppeers, | ||
backup_peers = bpeers, | ||
} | ||
end | ||
|
||
return result | ||
end | ||
|
||
return _M |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do not add such a comment in the sample code, here just keep the options related to https, detailed explanation is placed below