Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MQTT SSL #140

Merged
merged 3 commits into from
Sep 8, 2019
Merged
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
3 changes: 2 additions & 1 deletion config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ if System.get_env("DISABLE_MQTT") != "true" do
config :teslamate, :mqtt,
host: System.fetch_env!("MQTT_HOST"),
username: System.get_env("MQTT_USERNAME"),
password: System.get_env("MQTT_PASSWORD")
password: System.get_env("MQTT_PASSWORD"),
ssl: System.get_env("MQTT_SSL")
end

config :logger,
Expand Down
26 changes: 19 additions & 7 deletions lib/teslamate/mqtt/mqtt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ defmodule TeslaMate.Mqtt do
defp config do
auth = Application.get_env(:teslamate, :mqtt)

[
user_name: Keyword.get(auth, :username),
password: Keyword.get(auth, :password),
server: {Tortoise.Transport.Tcp, host: Keyword.get(auth, :host), port: 1883},
handler: {Tortoise.Handler.Logger, []},
subscriptions: []
]
if(Keyword.get(auth, :ssl) == "false") do
[
user_name: Keyword.get(auth, :username),
password: Keyword.get(auth, :password),
server: {Tortoise.Transport.Tcp, host: Keyword.get(auth, :host), port: 1883},
handler: {Tortoise.Handler.Logger, []},
subscriptions: []
]
else
[
user_name: Keyword.get(auth, :username),
password: Keyword.get(auth, :password),
server:
{Tortoise.Transport.SSL,
host: Keyword.get(auth, :host), port: 8883, verify: :verify_none},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one would expect security be default i.e. certificates should be verified when MQTT_SSL is enabled.

Do you use a self-signed certificate? Or could we add a CA store with the :cacertfile option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I use a self-signed certificate, hence the verify_none option. But a CA store option would work as well.

Copy link
Collaborator

@adriankumpf adriankumpf Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! Then let's add a CA store and make:verify_none optional e.g. with another env var? I'll try to integrate https://github.com/ericmj/castore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Looking forward to test.

Thanks!

handler: {Tortoise.Handler.Logger, []},
subscriptions: []
]
end
end

defp generate_client_id do
Expand Down