victronenergy / venus

Victron Energy Unix/Linux OS
https://github.com/victronenergy/venus/wiki
589 stars 75 forks source link

Prepare MQTT services & settings for VictronConnect #480

Open mpvader opened 5 years ago

mpvader commented 5 years ago

Status (updated by MVA on 2020-01-29)

All changes in naming of feature on/off switching in Venus OS, as well as what services they enable and disable, and ports that they open and close, are completed, and implemented per v2.40. Also that completes everything when connecting to Venus OS with VictronConnect over MQTT via VRM, ie. remotely.

Remaining todo list

See also below section 3, security details and passwords, for details.

Details

0. Requirements of the change

1. End result of the settings and their functionality

There will be three settings relating to MQTT:

  1. VRM Two Way Communication - enable/disable
  2. MQTT on LAN (ssl) - enable/disable
  3. MQTT on LAN (plaintext) - enable/disable (can only be enabled if its secure variant is also enabled)

Functionality:

If either of them is enabled, start dbus-mqtt & mqtt-rpc & mosquitto. So technically all is available.

And then open/close ports using iptables and enable/disable the bridge configs to our cloud brokers to control the individual features:

2. Migration:

To not break any existing systems:

3. Security and passwords

Everything via VRM, is already secured. The connection between the local broker and the VRM broker is MQTT over SSL, using a Victron root CA certificate and pre-shared token. The connection between the client and the VRM-broker is also over SSL, and uses the VRM system for authentication.

We also want to secure local connections that use MQTT. The idea is to self generate a SSL certificate and private key pair on the Venus device at boot. And every new VictronConnect install that connects to that device, locally, will ask the user once to accept the certificate, showing its hash (aka finger print). On Remote Console, the same is visible, for cautious people to go and check before they accept it. If and how its possible to that in VictronConnect, needs to be looked into.

Also there will be a password on MQTT. But then preferably make a general password; instead of adding yet another one to the list (Remote Console & WiFi Access point). One option is to re-use the WiFi password for that on the Venus GX-es; in other words, promoting that to a general access password. Note that the CCGX does not have a factory installed password. Also note that the WiFi Password is currently not changeable on the Venus GX. But, with the planned new feature to have a reset button, that is easily over come.

On the topic of passwords and such, note that there is another one coming: the bluetooth pin code.

mpvader commented 5 years ago

Implemention details for the MQTT on LAN (insecure) setting

[1] https://github.com/victronenergy/gui/commit/972a978374e7ab13b56bb72d28a856e9490c30ee [2] https://github.com/victronenergy/localsettings/commit/c23cbd49e4e73fddf720a9e284adbc114c4826f4

mpvader commented 5 years ago

1 - Testing migration:

(after downgrading, remember to remove the newly made settings)

2 - Testing ip tables and bindings:

2.1 - Only MQTT on LAN enabled

Only VRM two-way enabled:

1883, 8883 and 9001 must all be disallowed.

root@beaglebone:~# iptables -nvL 
Chain INPUT (policy ACCEPT 45 packets, 9456 bytes)
 pkts bytes target     prot opt in     out     source               destination         
18053 3811K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 1234  415K new-conn   all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 290 packets, 135K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain new-conn (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9001 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8883 reject-with icmp-port-unreachable

PASS

2.2 - Enabling MQTT on LAN

Allow 8883:

root@beaglebone:~# iptables -nvL 
Chain INPUT (policy ACCEPT 45 packets, 8360 bytes)
 pkts bytes target     prot opt in     out     source               destination         
26784 4763K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 1840  596K new-conn   all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 123 packets, 173K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain new-conn (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9001 reject-with icmp-port-unreachable
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8883

Allows 8883: PASS.

2.3 - Enable MQTT on LAN (insecure)

oot@beaglebone:~# iptables -nvL 
Chain INPUT (policy ACCEPT 74 packets, 26998 bytes)
 pkts bytes target     prot opt in     out     source               destination         
26835 4814K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 2114  684K new-conn   all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 29 packets, 28016 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain new-conn (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8883
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9001
root@beaglebone:~# 

Allows all three: PASS

2.4 - Disable all again

root@beaglebone:~# iptables -nvL 
Chain INPUT (policy ACCEPT 38 packets, 8032 bytes)
 pkts bytes target     prot opt in     out     source               destination         
26890 4899K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 2158  693K new-conn   all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 95 packets, 99223 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain new-conn (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8883 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:9001 reject-with icmp-port-unreachable

PASS.

2.5 - Checking that all ports are actually closed

netcat -v 192.168.178.122 9001 -w 5 and similar: all PASS.

3 - Testing functionality

3.1 - dbus-mqtt, mosquitto & mqtt-rpc are either all running or all not running.

PASS for all switch combinations.

3.2 - mqtt-rpc works on VRM when VRM two-way communication is enabled.

PASS

3.3 - VRM two-way communication

PASS (both for enabled and disabled).

3.4 - dbus-mqtt.. ??

skipped; I don't know how to easily test this.

3.5 - Start without settings aka default settings:

svc -d /service/localsettings
rm /data/conf/settings.xml
rm -rf /data/conf/mosquitto.d
rm /data/conf/mqtt_password.txt