Skip to content

Commit

Permalink
only rely on name in options
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE authored and jjcarstens committed Jul 5, 2022
1 parent 1ee34c3 commit cce8d41
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
20 changes: 8 additions & 12 deletions lib/nerves_ssh.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ defmodule NervesSSH do
defp via_name(name), do: {:via, Registry, {NervesSSH.Registry, name}}

@doc false
@spec start_link(Options.t() | {atom(), Options.t()}) :: GenServer.on_start()
def start_link({name, %Options{} = opts}) do
GenServer.start_link(__MODULE__, %{opts | name: name}, name: via_name(name))
end

@spec start_link(Options.t()) :: GenServer.on_start()
def start_link(%Options{} = opts) do
GenServer.start_link(__MODULE__, %{opts | name: :default}, name: via_name(:default))
GenServer.start_link(__MODULE__, opts, name: via_name(opts.name))
end

@doc """
Read the configuration options
"""
@spec configuration :: Options.t()
def configuration(name \\ :default) do
def configuration(name \\ NervesSSH) do
GenServer.call(via_name(name), :configuration)
end

Expand All @@ -52,7 +48,7 @@ defmodule NervesSSH do
See [ssh.daemon_info/1](http://erlang.org/doc/man/ssh.html#daemon_info-1).
"""
@spec info() :: {:ok, keyword()} | {:error, :bad_daemon_ref}
def info(name \\ :default) do
def info(name \\ NervesSSH) do
GenServer.call(via_name(name), :info)
end

Expand All @@ -62,7 +58,7 @@ defmodule NervesSSH do
This will also attempt to save the key in `{USER_DIR}/authorized_keys`
"""
@spec add_authorized_key(String.t()) :: :ok
def add_authorized_key(name \\ :default, key) when is_binary(key) do
def add_authorized_key(name \\ NervesSSH, key) when is_binary(key) do
GenServer.call(via_name(name), {:add_authorized_key, key})
end

Expand All @@ -72,7 +68,7 @@ defmodule NervesSSH do
This will also attempt to remove the key in `{USER_DIR}/authorized_keys`
"""
@spec remove_authorized_key(String.t()) :: :ok
def remove_authorized_key(name \\ :default, key) when is_binary(key) do
def remove_authorized_key(name \\ NervesSSH, key) when is_binary(key) do
GenServer.call(via_name(name), {:remove_authorized_key, key})
end

Expand All @@ -83,15 +79,15 @@ defmodule NervesSSH do
authentication for this user
"""
@spec add_user(String.t(), String.t() | nil) :: :ok
def add_user(name \\ :default, user, password) do
def add_user(name \\ NervesSSH, user, password) do
GenServer.call(via_name(name), {:add_user, [user, password]})
end

@doc """
Remove a user credential from the SSH daemon
"""
@spec remove_user(String.t()) :: :ok
def remove_user(name \\ :default, user) do
def remove_user(name \\ NervesSSH, user) do
GenServer.call(via_name(name), {:remove_user, [user]})
end

Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_ssh/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule NervesSSH.Application do
[]

app_env ->
[{NervesSSH, {:default, Options.with_defaults(app_env)}}]
[{NervesSSH, Options.with_defaults(app_env)}]
end

opts = [strategy: :one_for_one, name: NervesSSH.Supervisor]
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_ssh/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule NervesSSH.Options do
daemon_option_overrides: keyword()
}

defstruct name: :default,
defstruct name: NervesSSH,
authorized_keys: [],
decoded_authorized_keys: [],
user_passwords: [],
Expand Down
14 changes: 6 additions & 8 deletions test/nerves_ssh_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ defmodule NervesSSHTest do
start_supervised!({NervesSSH, nerves_ssh_config()})

# Test we can send SSH command
state = :sys.get_state({:via, Registry, {NervesSSH.Registry, :default}})
state = :sys.get_state({:via, Registry, {NervesSSH.Registry, NervesSSH}})
assert {:ok, "2", 0} == ssh_run("1 + 1")

# Simulate sshd failure. restart
Process.exit(state.sshd, :kill)
Process.sleep(800)

# Test recovery
new_state = :sys.get_state({:via, Registry, {NervesSSH.Registry, :default}})
new_state = :sys.get_state({:via, Registry, {NervesSSH.Registry, NervesSSH}})
assert state.sshd != new_state.sshd

assert {:ok, "4", 0} == ssh_run("2 + 2")
Expand Down Expand Up @@ -263,14 +263,12 @@ defmodule NervesSSHTest do

@tag :has_good_sshd_exec
test "can start multiple named daemons" do
config = nerves_ssh_config()
other_config = Map.update!(config, :port, &(&1 + 1))
config = nerves_ssh_config() |> Map.put(:name, :daemon_a)
other_config = %{config | name: :daemon_b, port: config.port + 1}
# start two servers, starting with identical configs, except the port
start_supervised!(Supervisor.child_spec({NervesSSH, {:daemon_a, config}}, id: :daemon_a))
start_supervised!(Supervisor.child_spec({NervesSSH, config}, id: :daemon_a))

start_supervised!(
Supervisor.child_spec({NervesSSH, {:daemon_b, other_config}}, id: :daemon_b)
)
start_supervised!(Supervisor.child_spec({NervesSSH, other_config}, id: :daemon_b))

assert {:ok, "2", 0} == ssh_run("1 + 1", @key_login)

Expand Down

0 comments on commit cce8d41

Please sign in to comment.