Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow key_cb configuration #7

Merged
merged 4 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/sftp_client/config.ex
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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