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. Brett Herford-Fell reporter

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

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

  3. Daya Sharma

    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 ?

  4. Michael Granger repo owner

    @dayasharma:

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

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

  7. Michael Granger repo owner

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

  8. Log in to comment