Zoltán
Zoltán

Reputation: 22206

Ruby hidden input password

I wrote a Ruby script that's trying to connect to a Postgres database hosted on Heroku.

If I use a hardcoded password, or if I load the password using gets, everything works fine. However, if I load the password using IO.noecho, I get the following exception:

storing.rb:11:in `initialize': FATAL:  password authentication failed for user "***" (PG::ConnectionBad)
FATAL:  no pg_hba.conf entry for host "****", user "***", database "***", SSL off
    from storing.rb:11:in `new'
    from storing.rb:11:in `create_conn'
    from fetch_currencies.rb:11:in `<main>'

Here's my code:

def create_conn(password)
    conn = PGconn.connect(
        :host => '***',
        :port => 5432,
        :dbname => '***',
        :user => '***',
        :password => password)
    return conn
end

puts 'Postgres DB password:'
pass = STDIN.noecho(&:gets)
conn = create_conn(pass)

I tried printing the password after loading it, as well as checking whether it's a String, and everything seems to be fine. What could be the problem?

Upvotes: 2

Views: 101

Answers (1)

Zolt&#225;n
Zolt&#225;n

Reputation: 22206

The problem, of course, was that I didn't chomp the input, so I guess the terminating new line character was also passed as part of the password.

The right way to go is then

pass = STDIN.noecho(&:gets).chomp

Upvotes: 2

Related Questions