Public/Private key authentication for Ruby Net::SFTP
Net::SFTP.start
passes its options
hash directly to Net::SSH.start
, so we should look to its documentation. It lists three options that look relevant:
:keys
=> an array of file names of private keys to use for publickey and hostbased authentication:key_data
=> an array of strings, with each element of the array being a raw private key in PEM format.:keys_only
=> set totrue
to use only private keys fromkeys
andkey_data
parameters, even if ssh-agent offers more identities. This option is intended for situations where ssh-agent offers many different identites.
The answer to a related question suggests that you may need to use all three:
Net::SFTP.start(ftp_host, user,
key_data: [],
keys: "tmp/some-certs/privatekey.pem",
keys_only: true)
If you want to use the raw key data from the SOME_PRIVATE_KEY
environment variable instead, it ought to look like this:
Net::SFTP.start(ftp_host, user,
key_data: [ ENV["SOME_PRIVATE_KEY"] ],
keys: [],
keys_only: true)
How do I do SCP with Ruby and a private key?
a brief look at this documentation suggests that it doesn't accept an ssh key option, as you are passing. But assuming you are right and I am wrong on that portion,
without seeing what value you are passing to transfer_file and what is stored in @folder, i can only guess, but assuming that they are both file objects, you can't concatenate the objects. you have to grab their path attributes. you might want to log the value of those two variables to make sure you are getting a path. you may also have better luck using the ruby "#{}"
method to concat string arguments, again guessing here but
path = "#{@folder.path}/#{destination_file.path}" #=> "my_folder/destination_folder
and
scp.upload!(source_file,path, :ssh => @key)
Ruby ssh error Net::SSH::AuthenticationFailed while it works with PHP
I've found how to solve that problem: adding a :auth_methods => ['publickey','password']
parameter in the Net::SSH.start
function.
Related Topics
Understanding Ruby Symbol as Method Call
How to Load Some Activerecord Models from a Yaml File and Save Them to the Db
Ruby on Rails: Yielding Specific Views in a Specific Places in the Layout
Polymorphic Association with Multiple Associations on the Same Model
Using Multiple Controllers in One View in Rails
Paperclip Amazon S3 Setup with Heroku
What Does the Underscore Mean in Literal Numbers
If Else Statements in .Html.Erb in Views
Creating a Hash with Values as Arrays and Default Value as Empty Array
Are There Better Ways to Prevent 'Yield' When No Block Is Passed In
Missing a Template for This Request Format and Variant
Rails Before_Filter for Specific Actions in Controller
Accepts_Nested_Attributes_For Rails 4 Is Not Deleting
What Is the Best Diff Library in Ruby
Unable to Load Gem Cocoa Pods While Creating Repo
Rspec Stubbing Method for Only Specific Arguments
How to Use Basic Authentication with Httparty in a Rails App