Skip to content

Commit

Permalink
Merge pull request #7 from sumup-bank/allow-key-provider-option
Browse files Browse the repository at this point in the history
feat: Allow key_cb configuration
  • Loading branch information
tlux committed Jan 14, 2020
2 parents f87d00b + 927fb01 commit 4a3d84f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
22 changes: 18 additions & 4 deletions lib/sftp_client/config.ex
Expand Up @@ -18,7 +18,8 @@ defmodule SFTPClient.Config do
{:inet, :inet},
:sftp_vsn,
{:connect_timeout, 5000},
{:operation_timeout, :infinity}
{:operation_timeout, :infinity},
:key_cb
]

@type t :: %__MODULE__{
Expand All @@ -36,7 +37,8 @@ defmodule SFTPClient.Config do
inet: :inet | :inet6,
sftp_vsn: integer,
connect_timeout: timeout,
operation_timeout: timeout
operation_timeout: timeout,
key_cb: tuple
}

@doc """
Expand All @@ -45,6 +47,18 @@ defmodule SFTPClient.Config do
"""
@spec new(t | Keyword.t() | %{optional(atom) => any}) :: t
def new(config_or_opts)
def new(%__MODULE__{} = config), do: config
def new(opts), do: struct!(__MODULE__, opts)
def new(%__MODULE__{} = config), do: config |> set_key_cb()
def new(opts), do: __MODULE__ |> struct!(opts) |> set_key_cb()

defp set_key_cb(%__MODULE__{key_cb: nil} = config) do
%__MODULE__{
config
| key_cb:
{SFTPClient.KeyProvider,
private_key_path: config.private_key_path,
private_key_pass_phrase: config.private_key_pass_phrase}
}
end

defp set_key_cb(config), do: config
end
10 changes: 5 additions & 5 deletions lib/sftp_client/operations/connect.ex
Expand Up @@ -37,6 +37,9 @@ defmodule SFTPClient.Operations.Connect do
5000 ms), can be set to `:infinity` to disable timeout.
* `:operation_timeout` - The operation timeout in milliseconds (defaults to
5000 ms), can be set to `:infinity` to disable timeout.
* `:key_cb` - A 2-item tuple containing:
- A module that implements `:ssh_client_key_api` behaviour.
- `:ssh_client_key_api` behaviour opts.
"""
@spec connect(Config.t() | Keyword.t() | %{optional(atom) => any}) ::
{:ok, Conn.t()} | {:error, term}
Expand Down Expand Up @@ -141,10 +144,6 @@ defmodule SFTPClient.Operations.Connect do

defp get_opts(config) do
Enum.sort([
{:key_cb,
{KeyProvider,
private_key_path: config.private_key_path,
private_key_pass_phrase: config.private_key_pass_phrase}},
{:quiet_mode, true},
{:silently_accept_hosts, true},
{:user_interaction, false}
Expand All @@ -164,7 +163,8 @@ defmodule SFTPClient.Operations.Connect do
:connect_timeout,
:dsa_pass_phrase,
:rsa_pass_phrase,
:ecdsa_pass_phrase
:ecdsa_pass_phrase,
:key_cb
])
|> Enum.reduce([], fn
{_key, nil}, opts ->
Expand Down
24 changes: 23 additions & 1 deletion test/sftp_client/config_test.exs
Expand Up @@ -20,7 +20,10 @@ defmodule SFTPClient.ConfigTest do
connect_timeout: 1000,
dsa_pass_phrase: "dsa_t3$t",
rsa_pass_phrase: "rsa_t3$t",
ecdsa_pass_phrase: "ecdsa_t3$t"
ecdsa_pass_phrase: "ecdsa_t3$t",
key_cb:
{RandomProvider,
private_key_path: :path, private_key_pass_phrase: :phrase}
}}
end

Expand All @@ -43,6 +46,25 @@ defmodule SFTPClient.ConfigTest do
assert config.ecdsa_pass_phrase == nil
end

test "ensures key_cb is set to the expected default" do
private_key_path = "whatever_path"
private_key_pass_phrase = "whatever_pass_phrase"

config =
Config.new(
private_key_path: private_key_path,
private_key_pass_phrase: private_key_pass_phrase
)

assert config.private_key_path == private_key_path
assert config.private_key_pass_phrase == private_key_pass_phrase

assert config.key_cb ==
{SFTPClient.KeyProvider,
private_key_path: config.private_key_path,
private_key_pass_phrase: config.private_key_pass_phrase}
end

test "build config with keyword list", %{data: data} do
config = data |> Keyword.new() |> Config.new()

Expand Down
6 changes: 5 additions & 1 deletion test/sftp_client/operations/connect_test.exs
Expand Up @@ -24,7 +24,11 @@ defmodule SFTPClient.Operations.ConnectTest do
inet: :inet,
sftp_vsn: 2,
connect_timeout: 1000,
operation_timeout: 500
operation_timeout: 500,
key_cb:
{SFTPClient.KeyProvider,
private_key_path: "test/fixtures/ssh_keys/id_rsa",
private_key_pass_phrase: "t3$t"}
}

setup :verify_on_exit!
Expand Down

0 comments on commit 4a3d84f

Please sign in to comment.