-
Notifications
You must be signed in to change notification settings - Fork 18
/
default.ex
53 lines (43 loc) · 1.25 KB
/
default.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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