jaimeiniesta

jaimeiniesta

I’m using sftp_ex to connect to an sFTP server, authenticating with my SSH key, and it works out of the box - provided the key is at .ssh/id_rsa and that .ssh/id_rsa.pub has been set up on the remote server.

But, I don’t know how to specify a different SSH key. For example if my key is at .ssh/custom_key how can I use this one?

Also, in my app I’m going to have the private key as an ENV variable, how can I use that instead of having the file at .ssh?

Showing Posts 1 to 7

easco

easco

The first thing that strikes my eye is that the documentation for SftpEx.connect says

Other available options can be found at ssh — OTP 29.0.2 (ssh 6.0.1)

If you look at that documentation then you can pass the type client_options(). client_options() in turn includes the type common_option() and common_option() include key_cb_common_option()

That is the “key callback” options and seems to let you specify a module that follows the ssh_client_key_api. As near as I can tell that means you have to create a module that has a method called user_key that accepts an algorithm specification (like RSA, DSA, etc) and returns the private key that corresponds to that.

You could create such a module. In the implementation of user_key you could take the value of the key in the environment variable and put it into the format recognized by Erlang’s :public_key module. In a recent case I had the key in pem format and I had to combine :public_key.pem_decode and :public_key.pem_entry_decode to extract a key from pem data. user_key would return that key.

Then you pass the name of that module to the SftpEx.connect function as [key_cb: YourModule]

Now… having said all that, I just got that by reading the docs… I haven’t actually tried it.

jaimeiniesta

jaimeiniesta OP

Thanks for your help @easco - for the record we found an easier solution, by using the user_dir option to specify an alternative directory where the id_rsa private key is stored in the server where we connect from.

We also found that when connecting it tried to write the known_hosts file in that directory, so you can either give this directory write permissions, or pre-populate it with this file so it doesn’t need to be updated.

OvermindDL1

OvermindDL1

For note, if you don’t want it to generate a known_hosts file then if you pass in the hosts public keys in the proper option (silently_accept_hosts or something like that) then it will only use those, but it will not accept any others either, which can be quite good for security.

jrissler

jrissler

In case anyone comes across this in the future - I spent a few minutes to figure this out, @easco’s solution works great - here’s how it looks:

Otherwise - followed his idea pretty closely. For brevity, didn’t post include error handling, but this is what it would look like: sftp_client/lib/sftp_client/key_provider.ex at 215ff5bcaeae6ebdd77c04d619c33a20c55eb12c · tlux/sftp_client · GitHub

thbar

thbar

Hi Jaime! By any chance, did you ultimately end up using an ENV variable successfully for this?

I would be potentially interested, to e.g. deal with read-only GitHub deploy keys.

Thanks!

jaimeiniesta

jaimeiniesta OP

Hey Thibaut!

No, we finally chose the user_dir option instead, see my reply on Feb '19 above :slight_smile:

GenericJam

GenericJam

Just to fill in a few more blanks here. If you use @jrissler 's gist you need to tie it into the rest of your code.

{:ok, conn} =
      SftpEx.connect(
        host: @host,
        user: @user,
        key_cb: App.KeyCb
    )

Also, bonus pro tip on SftpEx and the underlying Erlang code. You need to first open the connection which will create the file on the SFTP server and give you back a handle which you can then use as your file reference after that. Then you can stream, upload, download, etc. The docs for this lib are wrong as per my usage.

{:ok, handle} <- SftpEx.open(conn, "/" <> upload_destination)
stream = File.stream!("filename.txt")
    |> Stream.into(SftpEx.stream!(connection, handle))
    |> Stream.run
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews