-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat: implement support for SSL connection #24
Conversation
(waiting on API validation before updating the docs) |
Hey @leafo, did you get any chance to take a look at this? If you have any feedback I would make the necessary changes. I hope not to be pushy! Thanks :) |
Didn't get a chance to look yet, was sick last week. Hope to do so soon, thanks. |
hey @leafo, hope you're feeling better, let us know if there's anything we can help with on our end ;) |
sorry for the delay! (I had a confernece and got sick again...) will check this out ASAP |
Sure, no problem! I feel you, I too got sick twice this winter 😡 😷 |
|
||
connect: => | ||
@sock = socket.new! | ||
ok, err = @sock\connect @host, @port | ||
return nil, err unless ok | ||
|
||
if @sock\getreusedtimes! == 0 | ||
if @ssl | ||
success, err = @send_ssl_message! | ||
return nil, err unless success |
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.
when ssl_required
is false and ssl
is true, then send_ssl_message
returns nil with no error message when the ssl connection can't be established. Probably not what you intended?
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.
Yes we should return true
! I added this just before submitting the patching without testing it, totally guilty.
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 also probably want to disconnect too if the server does not support SSL connections:
elseif @ssl_required
@disconnect!
nil, "the server does not support SSL connections"
Additionally, we are missing the handling of ErrorMessage responses:
The frontend should also be prepared to handle an ErrorMessage response to SSLRequest from the server. This would only occur if the server predates the addition of SSL support to PostgreSQL. In this case the connection must be closed, but the frontend might choose to open a fresh connection and proceed without requesting SSL.
Implements client-server SSL connection. We assume to be in an ngx_lua environment, where 'tcpsock:sslhandshake()' is available. ngx_lua only supports server authentication via 'lua_ssl_trusted_certificate'. In plain Lua/JIT, we rely on LuaSec (the same way we rely on LuaSocket already) and a fallback method is provided which follows the ngx_lua method signature, plus accept LuaSec options. LuaSec allows for server and client authentication. An option is provided to require that the server supports SSL, otherwise, aborts the connection. New options (defaults): ``` { ssl = false, ssl_verify = false, ssl_required = false, cafile = nil, -- LuaSec cert = nil, -- LuaSec, key = nil -- LuaSec } ``` Caveats: - One change had to be done: the LuaSocket proxy metatable does not cache its original methods anymore. If it does, the old socket is retained from previous calls to 'send()' or 'receive()', and since LuaSec closes the socket when wrapping it, further calls do not succeed anymore. - Not easily testable in CI and test suite. I added a simple test, but `ssl_required` and other options are harder to test on Travis. I tested all options manually with Lua and ngx_lua, on a server that does not accept non-SSL connections.
bfb2e73
to
aa5c21c
Compare
I just updated the patch, thanks for the review. Changes:
When/if you're ok with this, let me know and I'll push some documentation! |
@thibaultcha I'm happy to write some docs if it helps to progress this PR - let me know if you want a hand. |
Any update on this PR? |
any update on this PR? |
Happy to wrap this up if @leafo has any more suggestions. If everything looks good code-wise, I'll quickly provide the documentation for this! |
sorry for being MIA for so long, I got caught up with a bunch of work and have been neglecting my opensource projects. I just tested it out and it appears to be working fine. I wrote a basic test suite for the luasocket implementation: https://github.com/leafo/pgmoon/blob/leafo/ssl/spec/postgres.sh Everything looks good: https://travis-ci.org/leafo/pgmoon/jobs/140978776 Only strange thing I noticed is when the socket is closed postgres prints this to the log
And google tells me this: https://devcenter.heroku.com/articles/postgres-logs-errors#log-unexpected-eof-on-client-connection I'm not sure if it's worth trying to figure out, I didn't see anything obvious that might have caused it. Anyway, I'm planning on merging everything in unless you want to look into the log message. If you want to add some docs to the readme too that would be great. |
I just pushed a commit which adds some documentation about SSL connections. Trying to keep it as simple as possible yet still mention the semi-optional LuaSec dependency. |
About the improperly-closed connection issue, I really have no clue what could be causing this and I'm not sure if this could come from LuaSec either... Nothing changed in |
Documentation changed merged, new version deployed to luarocks.org: https://luarocks.org/modules/leafo/pgmoon Thanks for the patch, once again, sorry about being so slow. Hope it didn't interrupt anything. |
Hi,
A stab at SSL connections (#9).
Implements client-server SSL connection. We assume to be in an ngx_lua
environment, where 'tcpsock:sslhandshake()' is available. ngx_lua only
supports server authentication via 'lua_ssl_trusted_certificate'.
In plain Lua/JIT, we rely on LuaSec (the same way we rely on LuaSocket
already) and a fallback method is provided which follows the ngx_lua
method signature, plus accept LuaSec options. LuaSec allows for server
and client authentication.
An option is provided to require that the server supports SSL,
otherwise, aborts the connection, as suggested in section 43.2.9 of the
protocol flow.
New options (defaults):
From those options, only
ssl
is included as a class attribute for simplicity(is
ssl
isfalse
, none of those options make sense), not sure if they shouldall be there or not.
Caveats:
cache its original methods anymore. If it does, the old socket is
retained from previous calls to 'send()' or 'receive()', and since
LuaSec closes the socket when wrapping it, further calls do not
succeed anymore.
but
ssl_required
and other options are harder to test on Travis. Itested all options manually with Lua and ngx_lua, on a server that
does not accept non-SSL connections.
Usage:
Let me know!