Skip to content
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

NoMethodError: undefined method `ssl_in_use?' for #<PG::Connection:0x007fc19b3bf308> #263

Closed
ged opened this issue Aug 2, 2017 · 11 comments

Comments

@ged
Copy link
Owner

ged commented Aug 2, 2017

Original report by Brett Herford-Fell (Bitbucket: brettskiii, GitHub: brettskiii).



ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
PG 0.20.0 (build 838985377b48)
Server version: 90304
Client version: 90314

NoMethodError: undefined method `ssl_in_use?' for #PG::Connection:0x007fc19b3bf308

Does this method exist / should it? It's in the docs but not in the gem?

@ged
Copy link
Owner Author

ged commented Aug 2, 2017

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


The SSL functions in pg require PostgreSQL version 9.5 or newer. See the
History.rdoc at
v0.19.0.

@ged
Copy link
Owner Author

ged commented Aug 2, 2017

Original comment by Brett Herford-Fell (Bitbucket: brettskiii, GitHub: brettskiii).


OK cool, would it be easier to return "unsupported PostgreSQL version" or something because the methods not being there is kind of confusing

@ged
Copy link
Owner Author

ged commented Aug 2, 2017

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


This is how many functions in pg are implemented. This way it's easy to
query for availability of functions by respond_to? without calling the
function.

However I'll add a PostgreSQL version note in the method documentation.

@ged
Copy link
Owner Author

ged commented Aug 2, 2017

Original comment by Brett Herford-Fell (Bitbucket: brettskiii, GitHub: brettskiii).


OK thank you.

@ged
Copy link
Owner Author

ged commented Aug 14, 2017

Original comment by Daya Sharma (Bitbucket: daya, GitHub: daya).


I would like to use the ssl_in_use? method to confirm SSL is used in connection from Rails, so in lieu of this method what would you recommend I use ?

@ged
Copy link
Owner Author

ged commented Aug 14, 2017

Original comment by Michael Granger (Bitbucket: ged, GitHub: ged).


@dayasharma:

Assuming that conn is the raw PG::Connection object:

#!ruby
if !conn.respond_to?( :ssl_in_use? )
    raise "You are using an older version of PostgreSQL"
elsif !conn.ssl_in_use?
    raise "This connection is not using SSL."
end

@ged
Copy link
Owner Author

ged commented Aug 14, 2017

Original comment by Daya Sharma (Bitbucket: daya, GitHub: daya).


Since the method is not implemented conn.respond_to?(:ssl_in_use?) will return false, therefore I have no way of confirming if the connection is using SSL. Am I right?

As a side note I would like to understand the purpose of documenting the method when its not actually implemented or at least is not accessible in PG gem.

FYI, I am using PG gem 0.21.0 and PostgreSQL 9.6.4 with Rails 5.0.1

Thanks for your help Michael.

@ged
Copy link
Owner Author

ged commented Aug 15, 2017

Original comment by Michael Granger (Bitbucket: ged, GitHub: ged).


Since the method is not implemented conn.respond_to?(:ssl_in_use?) will return false, therefore I have no way of confirming if the connection is using SSL. Am I right?

Correct, there are no other reliable ways via the client library to check for the use of SSL that'll work before 9.5. You can check whether or not OpenSSL in particular is in use by calling PGgetssl, which returns null if it's not, but we didn't bind that call since it's OpenSSL-specific. If your libpq uses some other SSL, it'd still return null even if the connection is using SSL via a different library.

As a side note I would like to understand the purpose of documenting the method when its not actually implemented or at least is not accessible in PG gem.

It is accessible if your underlying Postgres client library is recent enough. E.g.,:

#!ruby
[1] pry(main)> PG.library_version
=> 90603
[2] pry(main)> c = PG.connect( host: 'localhost', dbname: 'fm', sslmode: 'require', sslcompression: 'on' )
=> #<PG::Connection:0x007fa104301a40>
[3] pry(main)> c.ssl_in_use?
=> true

Since Ruby already has a means of introspection to check for the availability of a method (#respond_to?), we opted to use that instead of raising an exception like NotImplemented. Exceptions should not be used for flow control; a predicate is better than requiring someone call the method and rescue. I will agree, however, that methods which aren't available for all versions of Postgres should probably mention which versions they are available for. We're going to stop supporting unsupported versions of PostgreSQL with the release of version 1.0.0, so I'll make an effort to ensure that's done for the methods which remain.

@ged
Copy link
Owner Author

ged commented Aug 15, 2017

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


If the client side SSL methods aren't available, it's still possible to use the server side functions. They are available in all maintained PostgreSQL versions: https://www.postgresql.org/docs/9.2/static/sslinfo.html

I already added version information to methods on the master branch.

@ged
Copy link
Owner Author

ged commented Aug 15, 2017

Original comment by Michael Granger (Bitbucket: ged, GitHub: ged).


Oh I didn't know about those! I should have expected Postgres would have something like that. Thanks for pointing that out, Lars.

@ged
Copy link
Owner Author

ged commented Sep 5, 2017

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


Add PostgreSQL version information to conditional functions

Fixes #263 : https://bitbucket.org/ged/ruby-pg/issues/263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant