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

Issue #263 resolved
Brett Herford-Fell
created an issue

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?

Comments (11)

  1. Lars Kanis

    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.

  2. Michael Granger repo owner

    @Daya Sharma:

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

    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
    
  3. Daya Sharma

    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.

  4. Michael Granger repo owner

    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.,:

    [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.

  5. Log in to comment