Packages
nerves_hub_link
1.4.1
2.12.0
2.11.1
2.11.0
retired
2.10.2
2.10.1
2.10.0
2.9.0
2.9.0-rc.3
2.9.0-rc.2
2.9.0-rc.1
2.8.1
2.8.0
2.7.3
2.7.2
2.7.0
retired
2.6.0
2.5.2
2.5.1
2.5.0
2.4.0
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.1
1.0.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.2
0.10.1
retired
0.10.0
retired
0.10.0-rc.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.6
Manage your Nerves fleet by connecting it to NervesHub
Current section
Files
Jump to
Current section
Files
lib/nerves_hub_link/configurators/default.ex
defmodule NervesHubLink.Configurator.Default do
@behaviour NervesHubLink.Configurator
alias NervesHubLink.{Certificate, Configurator.Config}
@cert "nerves_hub_cert"
@key "nerves_hub_key"
@impl NervesHubLink.Configurator
def build(%Config{} = config) do
ssl =
config.ssl
|> maybe_add_cacerts()
|> maybe_add_cert()
|> maybe_add_key()
|> maybe_add_sni(config)
%{config | ssl: ssl}
end
defp maybe_add_cacerts(socket_opts) do
Keyword.put_new(socket_opts, :cacerts, Certificate.ca_certs())
end
defp maybe_add_cert(socket_opts) do
if socket_opts[:cert] || socket_opts[:certfile] do
# option already provided
socket_opts
else
cert =
Nerves.Runtime.KV.get(@cert)
|> Certificate.pem_to_der()
Keyword.put(socket_opts, :cert, cert)
end
end
defp maybe_add_key(socket_opts) do
if socket_opts[:key] || socket_opts[:keyfile] do
socket_opts
else
key =
Nerves.Runtime.KV.get(@key)
|> Certificate.key_pem_to_der()
Keyword.put(socket_opts, :key, {:ECPrivateKey, key})
end
end
defp maybe_add_sni(socket_opts, %{device_api_sni: sni}) do
Keyword.put_new(socket_opts, :server_name_indication, to_charlist(sni))
end
end