Skip to content

Commit

Permalink
Move SSL Configurations to be Options
Browse files Browse the repository at this point in the history
Moved the SSL configurations to be options that get passed into the
`VintageNetWizard.run_wizard/1` function.

The reason for this is to allow more flexible configuration appoarch
for users of the library.

More over removed the self signed certs as those are not used anymore.
  • Loading branch information
mattludwigs committed Oct 16, 2019
1 parent 9ae5f4f commit 3275a74
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 92 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,19 @@ config :vintage_net_wizard,
port: 4001
```

If SSL is enabled the default port is `443`.

### SSL

To use SSL with the web UI, simply set the configuration flag:
To use SSL with the web UI, you can pass SSL options to
`VintageNetWizard.run_wizard/1`:

```elixir
config :vintage_net_wizard, ssl: true
VintageNetWizard.run_wizard(ssl: [keyfile: "/path/to/key.pem", certfile: "/path/to/cert.pem"])
```

This will default to use a self-signed certificate and key on port `443`.
You can also specify your own certificate, key, and port in the config:

```elixir
config :vintage_net_wizard,
ssl: true,
certfile: "path/to/cert.pem",
keyfile: "path/to/key.pem",
port: 4443
```
To see all available options see `Plug.SSL.configure/1` and Erlang's `:ssl`
module.

### Backends

Expand Down
16 changes: 12 additions & 4 deletions lib/vintage_net_wizard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ defmodule VintageNetWizard do
Run the wizard.
This means the WiFi module will be put into access point
mode and the web server will be started
mode and the web server will be started.
Options:
- `:ssl` - A Keyword list of `:ssl.tls_server_options`
See `Plug.SSL.configure/1` for more information about the
SSL options.
"""
@spec run_wizard() :: :ok
def run_wizard() do
@spec run_wizard([Endpoint.opt()]) :: :ok | {:error, String.t()}
def run_wizard(opts \\ []) do
with :ok <- Backend.reset(),
:ok <- into_ap_mode(),
{:ok, _server} <- start_server(),
{:ok, _server} <- Endpoint.start_server(opts),
:ok <- Backend.start_scan() do
:ok
else
# Already running is still ok
{:error, :already_started} -> :ok
error -> error
end
end

Expand Down
55 changes: 26 additions & 29 deletions lib/vintage_net_wizard/web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule VintageNetWizard.Web.Endpoint do
Supervisor for the Web part of the VintageNet Wizard.
"""
alias VintageNetWizard.Web.Router

use DynamicSupervisor

@type opt :: {:ssl, :ssl.tls_server_option()}

@doc false
def start_link(args) do
DynamicSupervisor.start_link(__MODULE__, args, name: __MODULE__)
Expand All @@ -18,15 +19,17 @@ defmodule VintageNetWizard.Web.Endpoint do
Only one server can be running at a time.
"""
@spec start_server() :: GenServer.on_start() | {:error, :already_started}
def start_server() do
use_ssl? = Application.get_env(:vintage_net_wizard, :ssl)
@spec start_server([opt]) ::
GenServer.on_start() | {:error, :already_started | :no_keyfile | :no_certfile}
def start_server(opts \\ []) do
use_ssl? = Keyword.has_key?(opts, :ssl)

spec = maybe_use_ssl(use_ssl?)

case DynamicSupervisor.start_child(__MODULE__, spec) do
with spec <- maybe_use_ssl(use_ssl?, opts),
{:ok, _pid} = ok <- DynamicSupervisor.start_child(__MODULE__, spec) do
ok
else
{:error, :max_children} -> {:error, :already_started}
ok -> ok
error -> error
end
end

Expand Down Expand Up @@ -58,33 +61,27 @@ defmodule VintageNetWizard.Web.Endpoint do
]
end

defp maybe_use_ssl(_use_ssl = true) do
ssl_dir = ssl_dir()
defp maybe_use_ssl(_use_ssl = true, opts) do
port = Application.get_env(:vintage_net_wizard, :port, 443)
ssl_options = Keyword.get(opts, :ssl)
options = [dispatch: dispatch(), port: port]

Plug.Cowboy.child_spec(
plug: Router,
scheme: :https,
options: [
dispatch: dispatch(),
certfile: Application.get_env(:vintage_net_wizard, :certfile, "#{ssl_dir}/cert.pem"),
keyfile: Application.get_env(:vintage_net_wizard, :keyfile, "#{ssl_dir}/key.pem"),
port: Application.get_env(:vintage_net_wizard, :port, 443)
]
)
end

defp maybe_use_ssl(_no_ssl) do
Plug.Cowboy.child_spec(
plug: Router,
scheme: :http,
options: [
dispatch: dispatch(),
port: Application.get_env(:vintage_net_wizard, :port, 80)
]
options: Keyword.merge(ssl_options, options)
)
end

defp ssl_dir() do
Path.join(:code.priv_dir(:vintage_net_wizard), "ssl")
defp maybe_use_ssl(_no_ssl, _opts) do
{:ok,
Plug.Cowboy.child_spec(
plug: Router,
scheme: :http,
options: [
dispatch: dispatch(),
port: Application.get_env(:vintage_net_wizard, :port, 80)
]
)}
end
end
19 changes: 0 additions & 19 deletions priv/ssl/cert.pem

This file was deleted.

28 changes: 0 additions & 28 deletions priv/ssl/key.pem

This file was deleted.

0 comments on commit 3275a74

Please sign in to comment.