Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@
#
{Credo.Check.Design.AliasUsage,
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
# TODO comments are acceptable in our codebase, but we want to see them locally
{Credo.Check.Design.TagTODO, [priority: :ignore]},
{Credo.Check.Design.TagFIXME, []},
# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagTODO, [exit_status: 0]},

#
## Readability Checks
#
Expand All @@ -99,8 +95,9 @@
{Credo.Check.Readability.LargeNumbers, []},
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
{Credo.Check.Readability.ModuleAttributeNames, []},
{Credo.Check.Readability.ModuleDoc, []},
{Credo.Check.Readability.ModuleNames, []},
# ModuleDoc is not required in our codebase, but we want to see them locally
{Credo.Check.Readability.ModuleDoc, [priority: :ignore]},
{Credo.Check.Readability.MultiAlias, []},
{Credo.Check.Readability.ParenthesesInCondition, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, [parens: true]},
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ jobs:
- name: Check spelling
run: mix spellweaver.check

- name: Run Credo (won't fail the build)
run: mix credo --mute-exit-status
- name: Run Credo
run: mix credo --min-priority low

- name: DB Setup
run: mix ecto.migrate.reset
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ defmodule NervesHub.Accounts do
alias NervesHub.Accounts.OrgUser
alias NervesHub.Accounts.RemoveAccount
alias NervesHub.Accounts.User
alias NervesHub.Accounts.UserNotifier
alias NervesHub.Accounts.UserToken
alias NervesHub.Devices
alias NervesHub.Devices.Device
alias NervesHub.Products.Product
alias NervesHub.Accounts.UserNotifier

alias NervesHub.Repo

Expand Down
5 changes: 3 additions & 2 deletions lib/nerves_hub/accounts/user_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ defmodule NervesHub.Accounts.UserNotifier do
alias NervesHub.Emails.ConfirmationTemplate
alias NervesHub.Emails.LoginWithGoogleReminderTemplate
alias NervesHub.Emails.OrgUserAddedTemplate
alias NervesHub.Emails.PasswordResetTemplate
alias NervesHub.Emails.PasswordResetConfirmationTemplate
alias NervesHub.Emails.PasswordResetTemplate
alias NervesHub.Emails.PasswordUpdatedTemplate
alias NervesHub.Emails.TellOrgUserAddedTemplate
alias NervesHub.Emails.TellOrgUserInvitedTemplate
Expand All @@ -17,6 +17,7 @@ defmodule NervesHub.Accounts.UserNotifier do
alias NervesHub.Emails.WelcomeTemplate

alias NervesHub.SwooshMailer, as: Mailer
alias Phoenix.HTML

def deliver_confirmation_instructions(user, confirmation_url) do
assigns = %{
Expand Down Expand Up @@ -207,7 +208,7 @@ defmodule NervesHub.Accounts.UserNotifier do

text =
module.text_render(assigns)
|> Phoenix.HTML.Safe.to_iodata()
|> HTML.Safe.to_iodata()
|> IO.iodata_to_binary()

{html, text}
Expand Down
5 changes: 3 additions & 2 deletions lib/nerves_hub/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule NervesHub.Application do

require Logger

alias NervesHub.Telemetry.Customizations, as: TelemetryCustomizations

def start(_type, _args) do
case System.cmd("fwup", ["--version"], env: []) do
{_, 0} ->
Expand Down Expand Up @@ -57,8 +59,7 @@ defmodule NervesHub.Application do
:ok = :httpc.set_option(:ipfamily, :inet6fb4)
end

:ok = NervesHub.Telemetry.Customizations.setup()

:ok = TelemetryCustomizations.setup()
:ok = OpentelemetryBandit.setup()
:ok = OpentelemetryPhoenix.setup(adapter: :bandit)
:ok = OpentelemetryOban.setup(trace: [:jobs])
Expand Down
6 changes: 4 additions & 2 deletions lib/nerves_hub/certificate.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule NervesHub.Certificate do
alias X509.Certificate.Extension, as: X509Extension

import X509.ASN1,
only: [
extension: 1,
Expand All @@ -17,7 +19,7 @@ defmodule NervesHub.Certificate do
def get_aki(otp_certificate) do
otp_certificate
|> X509.Certificate.extensions()
|> X509.Certificate.Extension.find(:authority_key_identifier)
|> X509Extension.find(:authority_key_identifier)
|> extension()
|> Keyword.get(:extnValue)
|> authority_key_identifier()
Expand All @@ -27,7 +29,7 @@ defmodule NervesHub.Certificate do
def get_ski(otp_certificate) do
otp_certificate
|> X509.Certificate.extensions()
|> X509.Certificate.Extension.find(:subject_key_identifier)
|> X509Extension.find(:subject_key_identifier)
|> case do
nil ->
nil
Expand Down
40 changes: 21 additions & 19 deletions lib/nerves_hub/device_ssl_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ defmodule NervesHub.DeviceSSLTransport do

@behaviour ThousandIsland.Transport

alias ThousandIsland.Transports.SSL, as: SSLTransport

@impl ThousandIsland.Transport
defdelegate listen(port, user_options), to: ThousandIsland.Transports.SSL
defdelegate listen(port, user_options), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate accept(listener_socket), to: ThousandIsland.Transports.SSL
defdelegate accept(listener_socket), to: SSLTransport

@impl ThousandIsland.Transport
def handshake(socket) do
if NervesHub.RateLimit.increment() do
:telemetry.execute([:nerves_hub, :rate_limit, :accepted], %{count: 1})

ThousandIsland.Transports.SSL.handshake(socket)
SSLTransport.handshake(socket)
else
:telemetry.execute([:nerves_hub, :rate_limit, :rejected], %{count: 1})

Expand All @@ -33,50 +35,50 @@ defmodule NervesHub.DeviceSSLTransport do
end

@impl ThousandIsland.Transport
defdelegate upgrade(socket, opts), to: ThousandIsland.Transports.SSL
defdelegate upgrade(socket, opts), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate controlling_process(socket, pid), to: ThousandIsland.Transports.SSL
defdelegate controlling_process(socket, pid), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate recv(socket, length, timeout), to: ThousandIsland.Transports.SSL
defdelegate recv(socket, length, timeout), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate send(socket, data), to: ThousandIsland.Transports.SSL
defdelegate send(socket, data), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate sendfile(socket, filename, offset, length), to: ThousandIsland.Transports.SSL
defdelegate sendfile(socket, filename, offset, length), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate getopts(socket, options), to: ThousandIsland.Transports.SSL
defdelegate getopts(socket, options), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate setopts(socket, options), to: ThousandIsland.Transports.SSL
defdelegate setopts(socket, options), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate shutdown(socket, way), to: ThousandIsland.Transports.SSL
defdelegate shutdown(socket, way), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate close(socket), to: ThousandIsland.Transports.SSL
defdelegate close(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate sockname(socket), to: ThousandIsland.Transports.SSL
defdelegate sockname(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate peername(socket), to: ThousandIsland.Transports.SSL
defdelegate peername(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate peercert(socket), to: ThousandIsland.Transports.SSL
defdelegate peercert(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate secure?(), to: ThousandIsland.Transports.SSL
defdelegate secure?(), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate getstat(socket), to: ThousandIsland.Transports.SSL
defdelegate getstat(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate negotiated_protocol(socket), to: ThousandIsland.Transports.SSL
defdelegate negotiated_protocol(socket), to: SSLTransport

@impl ThousandIsland.Transport
defdelegate connection_information(socket), to: ThousandIsland.Transports.SSL
defdelegate connection_information(socket), to: SSLTransport
end
38 changes: 11 additions & 27 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule NervesHub.Devices do
alias NervesHub.Products.Product
alias NervesHub.Repo
alias NervesHub.TaskSupervisor, as: Tasks
alias Phoenix.Channel.Server, as: ChannelServer

def get_device(device_id) when is_integer(device_id) do
Repo.get(Device, device_id)
Expand Down Expand Up @@ -668,13 +669,9 @@ defmodule NervesHub.Devices do

{:ok, device} = update_device(device, %{firmware_validation_status: "validated"})

Phoenix.Channel.Server.broadcast_from!(
NervesHub.PubSub,
self(),
"device:#{device.identifier}:internal",
"firmware:validated",
%{}
)
event = "firmware:validated"
topic = "device:#{device.identifier}:internal"
ChannelServer.broadcast_from!(NervesHub.PubSub, self(), topic, event, %{})

{:ok, device}
end
Expand Down Expand Up @@ -1021,13 +1018,8 @@ defmodule NervesHub.Devices do
firmware_uuid: firmware_uuid
}

_ =
Phoenix.Channel.Server.broadcast(
NervesHub.PubSub,
"orchestrator:deployment:#{device.deployment_id}",
"device-online",
payload
)
topic = "orchestrator:deployment:#{device.deployment_id}"
_ = ChannelServer.broadcast(NervesHub.PubSub, topic, "device-online", payload)

:ok
end
Expand All @@ -1037,13 +1029,8 @@ defmodule NervesHub.Devices do
end

def deployment_device_updated(device) do
_ =
Phoenix.Channel.Server.broadcast(
NervesHub.PubSub,
"orchestrator:deployment:#{device.deployment_id}",
"device-updated",
%{}
)
topic = "orchestrator:deployment:#{device.deployment_id}"
_ = ChannelServer.broadcast(NervesHub.PubSub, topic, "device-updated", %{})

:ok
end
Expand Down Expand Up @@ -1185,12 +1172,9 @@ defmodule NervesHub.Devices do
{:ok, device} = result ->
_ =
if device.deployment_id do
Phoenix.Channel.Server.broadcast(
NervesHub.PubSub,
"orchestrator:deployment:#{device.deployment_id}",
"device-updated",
%{}
)
event = "device-updated"
topic = "orchestrator:deployment:#{device.deployment_id}"
ChannelServer.broadcast(NervesHub.PubSub, topic, event, %{})
end

result
Expand Down
10 changes: 3 additions & 7 deletions lib/nerves_hub/devices/log_lines.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule NervesHub.Devices.LogLines do
alias NervesHub.AnalyticsRepo
alias NervesHub.Devices.Device
alias NervesHub.Devices.LogLine
alias Phoenix.Channel.Server, as: ChannelServer

import Ecto.Query

Expand Down Expand Up @@ -53,13 +54,8 @@ defmodule NervesHub.Devices.LogLines do
{:ok, log_line} ->
_ = AnalyticsRepo.insert_all(LogLine, [changeset.changes], settings: [async_insert: 1])

_ =
Phoenix.Channel.Server.broadcast(
NervesHub.PubSub,
"device:#{device.identifier}:internal",
"logs:received",
log_line
)
topic = "device:#{device.identifier}:internal"
_ = ChannelServer.broadcast(NervesHub.PubSub, topic, "logs:received", log_line)

{:ok, log_line}

Expand Down
6 changes: 4 additions & 2 deletions lib/nerves_hub/emails/closing_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ defmodule NervesHub.Emails.ClosingBlock do
use MjmlEEx.Component, mode: :runtime
use NervesHubWeb, :html

alias Phoenix.HTML

@impl MjmlEEx.Component
def render(_assigns) do
"""
<mj-section padding-top="0px">
<mj-column>
<mj-text font-size="20px" font-family="sans-serif">
#{support_section() |> Phoenix.HTML.Safe.to_iodata()}
#{HTML.Safe.to_iodata(support_section())}
</mj-text>
#{signoff() |> Phoenix.HTML.Safe.to_iodata()}
#{HTML.Safe.to_iodata(signoff())}
</mj-column>
</mj-section>
"""
Expand Down
13 changes: 4 additions & 9 deletions lib/nerves_hub/extensions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule NervesHub.Extensions do
critical firmware update.
"""

alias Phoenix.Channel.Server, as: ChannelServer

@callback handle_in(event :: String.t(), Phoenix.Channel.payload(), Phoenix.Socket.t()) ::
{:noreply, Phoenix.Socket.t()}
| {:noreply, Phoenix.Socket.t(), timeout() | :hibernate}
Expand Down Expand Up @@ -77,15 +79,8 @@ defmodule NervesHub.Extensions do
end

def broadcast_extension_event(target, event, extension) do
Phoenix.Channel.Server.broadcast_from!(
NervesHub.PubSub,
self(),
topic(target),
event,
%{
"extensions" => [extension]
}
)
params = %{"extensions" => [extension]}
ChannelServer.broadcast_from!(NervesHub.PubSub, self(), topic(target), event, params)
end

defp topic(%NervesHub.Devices.Device{} = device), do: "device:#{device.id}:extensions"
Expand Down
12 changes: 4 additions & 8 deletions lib/nerves_hub/extensions/geo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule NervesHub.Extensions.Geo do
@behaviour NervesHub.Extensions

alias NervesHub.Devices.Connections
alias Phoenix.Channel.Server, as: ChannelServer

@impl NervesHub.Extensions
def description() do
Expand Down Expand Up @@ -49,14 +50,9 @@ defmodule NervesHub.Extensions.Geo do
@impl NervesHub.Extensions
def handle_in("location:update", location, socket) do
Connections.merge_update_metadata(socket.assigns.reference_id, %{location: location})

_ =
Phoenix.Channel.Server.broadcast(
NervesHub.PubSub,
"device:#{socket.assigns.device.identifier}:internal",
"location:updated",
location
)
event = "location:updated"
topic = "device:#{socket.assigns.device.identifier}:internal"
_ = ChannelServer.broadcast(NervesHub.PubSub, topic, event, location)

{:noreply, socket}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/nerves_hub/extensions/health.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule NervesHub.Extensions.Health do
alias NervesHub.Devices.HealthStatus
alias NervesHub.Devices.Metrics
alias NervesHub.Helpers.Logging
alias Phoenix.Channel.Server, as: ChannelServer

require Logger

Expand Down Expand Up @@ -109,7 +110,6 @@ defmodule NervesHub.Extensions.Health do

defp device_internal_broadcast!(device, event, payload) do
topic = "device:#{device.id}:extensions"

Phoenix.Channel.Server.broadcast_from!(NervesHub.PubSub, self(), topic, event, payload)
ChannelServer.broadcast_from!(NervesHub.PubSub, self(), topic, event, payload)
end
end
Loading