Skip to content

Commit c3648c4

Browse files
authored
Merge pull request #2 from rjanja/radams/flip-accept-hosts-default
Flip default behavior of whether to accept hosts
2 parents e56d7d8 + f0a1dc7 commit c3648c4

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Options
2525

2626
* `identity`: `IO.device` providing the ssh private key (required)
2727
* `known_hosts`: `IO.device` providing the known hosts list. If providing a File IO, it should have been opened in `:write` mode (required)
28-
* `accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
28+
* `silently_accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
2929

3030
`SSHClientKeyApi` is meant to primarily be used via the convenience function
3131
`with_config`:
3232

3333
```elixir
3434
key = File.open!("path/to/keyfile.pub")
3535
known_hosts = File.open!("path/to/known_hosts")
36-
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts, accept_hosts: false)
36+
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts, silently_accept_hosts: true)
3737
```
3838

3939
The result can then be passed as an option when creating an SSH connection.

lib/ssh_client_key_api.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule SSHClientKeyAPI do
1414
```
1515
key = File.open!("path/to/keyfile")
1616
known_hosts = File.open!("path/to/known_hosts")
17-
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts, accept_hosts: false)
17+
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts, silently_accept_hosts: true)
1818
```
1919
2020
The result can be passed as an option when creating an `SSHKit.SSH.Connection`:
@@ -25,7 +25,7 @@ defmodule SSHClientKeyAPI do
2525
2626
- `identity`: `IO.device` providing the ssh key (required)
2727
- `known_hosts`: `IO.device` providing the known hosts list. If providing a File IO, it should have been opened in `:write` mode (required)
28-
- `accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
28+
- `silently_accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
2929
"""
3030

3131
@spec with_options(opts :: list) :: {atom, list}
@@ -36,7 +36,7 @@ defmodule SSHClientKeyAPI do
3636
3737
- `identity`: `IO.device` providing the ssh key (required)
3838
- `known_hosts`: `IO.device` providing the known hosts list. If providing a File IO, it should have been opened in `:write` mode (required)
39-
- `accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
39+
- `silently_accept_hosts`: `boolean` silently accept and add new hosts to the known hosts. By default only known hosts will be accepted.
4040
4141
by default it will use the the files found in `System.user_home!`
4242
@@ -45,7 +45,7 @@ defmodule SSHClientKeyAPI do
4545
```
4646
key = File.open!("path/to/keyfile")
4747
known_hosts = File.open!("path/to/known_hosts")
48-
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts, accept_hosts: false)
48+
cb = SSHClientKeyAPI.with_options(identity: key, known_hosts: known_hosts)
4949
SSHKit.SSH.connect("example.com", key_cb: cb)
5050
```
5151
@@ -60,7 +60,7 @@ defmodule SSHClientKeyAPI do
6060
end
6161

6262
def add_host_key(hostname, key, opts) do
63-
case accept_hosts(opts) do
63+
case silently_accept_hosts(opts) do
6464
true ->
6565
opts
6666
|> known_hosts_data
@@ -73,14 +73,14 @@ defmodule SSHClientKeyAPI do
7373
"""
7474
Error: unknown fingerprint found for #{inspect hostname} #{inspect key}.
7575
You either need to add a known good fingerprint to your known hosts file for this host,
76-
*or* pass the accept_hosts option to your client key callback
76+
*or* pass the silently_accept_hosts option to your client key callback
7777
"""
7878
{:error, message}
7979
end
8080
end
8181

8282
def is_host_key(key, hostname, alg, opts) when alg in @key_algorithms do
83-
accept_hosts(opts) || opts
83+
silently_accept_hosts(opts) || opts
8484
|> known_hosts_data
8585
|> to_string
8686
|> :public_key.ssh_decode(:known_hosts)
@@ -112,8 +112,8 @@ defmodule SSHClientKeyAPI do
112112
cb_opts(opts)[:identity_data]
113113
end
114114

115-
defp accept_hosts(opts) do
116-
cb_opts(opts)[:accept_hosts]
115+
defp silently_accept_hosts(opts) do
116+
cb_opts(opts)[:silently_accept_hosts]
117117
end
118118

119119
defp known_hosts(opts) do
@@ -152,7 +152,7 @@ defmodule SSHClientKeyAPI do
152152
opts
153153
|> Keyword.put_new_lazy(:identity, &default_identity/0)
154154
|> Keyword.put_new_lazy(:known_hosts, &default_known_hosts/0)
155-
|> Keyword.put_new(:accept_hosts, true)
155+
|> Keyword.put_new(:silently_accept_hosts, false)
156156
end
157157

158158
end

test/ssh_client_key_api_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ github.com,192.30.252.128 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9
5757
}
5858
end
5959

60-
test "add_host_key writes an entry to known hosts if accept_hosts is true", %{known_hosts: known_hosts} do
60+
test "add_host_key writes an entry to known hosts if silently_accept_hosts is true", %{known_hosts: known_hosts} do
6161
SSHClientKeyAPI.add_host_key(
6262
"example.com",
6363
@host_key,
64-
[key_cb_private: [accept_hosts: true, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]]
64+
[key_cb_private: [silently_accept_hosts: true, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]]
6565
)
6666
:file.position(known_hosts, :bof)
6767
result = IO.binread(known_hosts, :all)
6868
assert result =~ "example.com"
6969
end
7070

71-
test "add_host_key returns an error if accept_hosts is false", %{known_hosts: known_hosts} do
71+
test "add_host_key returns an error if silently_accept_hosts is false", %{known_hosts: known_hosts} do
7272
result = SSHClientKeyAPI.add_host_key(
7373
"example.com",
7474
@host_key,
75-
[key_cb_private: [accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
75+
[key_cb_private: [silently_accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
7676
assert {:error, _message} = result
7777
end
7878

@@ -81,7 +81,7 @@ github.com,192.30.252.128 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9
8181
@host_key,
8282
'github.com',
8383
:"ssh-dsa",
84-
[key_cb_private: [accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
84+
[key_cb_private: [silently_accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
8585
assert result
8686
end
8787

@@ -90,7 +90,7 @@ github.com,192.30.252.128 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9
9090
@host_key,
9191
'other.com',
9292
:"ssh-dsa",
93-
[key_cb_private: [accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
93+
[key_cb_private: [silently_accept_hosts: false, known_hosts: known_hosts, known_hosts_data: IO.binread(known_hosts, :all)]])
9494
refute result
9595
end
9696

0 commit comments

Comments
 (0)