Skip to content

Commit cb75414

Browse files
committed
Mosquitto - make security persistent + extend documentation
Implements changes proposed in issue #68: 1. Add a volume mapping to the template `service.yml`: ``` - ./volumes/mosquitto/pwfile:/mosquitto/pwfile ``` 2. Add this line to the template `directoryfix.sh`: ``` sudo mkdir -p ./volumes/mosquitto/pwfile/ ``` 3. Change the relevant line in the template `mosquitto.conf` to read: ``` #password_file /mosquitto/pwfile/pwfile ``` 4. Change the documentation so that the command reads: ``` mosquitto_passwd -c /mosquitto/pwfile/pwfile «MYUSER» ``` Extends Mosquitto documentation to: * explain logging options * elaborate on setting up security * explain how to run Mosquitto as root (rather than user 1883) in situations where that might be appropriate Also adds a timestamp format line to the template `mosquitto.conf`: ``` log_timestamp_format %Y-%m-%dT%H:%M:%S ``` This causes log entries to have human-readable timestamps. The timestamps are UTC which is, I think, a huge improvement on seconds since 1/1/70. It would be nicer if the timestamps could be converted to local time but I could not find a configuration option to make that happen (setting TZ didn't do the trick either).
1 parent 1b6ea53 commit cb75414

File tree

4 files changed

+164
-7
lines changed

4 files changed

+164
-7
lines changed

.templates/mosquitto/directoryfix.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if [ $(grep -c 'user: \"1883\"' ./services/mosquitto/service.yml) -eq 1 ]; then
77
echo "...found user 1883"
88
sudo mkdir -p ./volumes/mosquitto/data/
99
sudo mkdir -p ./volumes/mosquitto/log/
10+
sudo mkdir -p ./volumes/mosquitto/pwfile/
1011
sudo chown -R 1883:1883 ./volumes/mosquitto/
1112
fi
1213

.templates/mosquitto/mosquitto.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ persistence_location /mosquitto/data/
55
log_dest stdout
66

77
#Uncomment to enable passwords
8-
#password_file /mosquitto/config/pwfile
8+
#password_file /mosquitto/pwfile/pwfile
99
#allow_anonymous false
1010

1111
#Uncomment to enable filters
1212
#acl_file /mosquitto/config/filter.acl
13+
14+
log_timestamp_format %Y-%m-%dT%H:%M:%S

.templates/mosquitto/service.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
volumes:
1010
- ./volumes/mosquitto/data:/mosquitto/data
1111
- ./volumes/mosquitto/log:/mosquitto/log
12+
- ./volumes/mosquitto/pwfile:/mosquitto/pwfile
1213
- ./services/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
1314
- ./services/mosquitto/filter.acl:/mosquitto/config/filter.acl
1415

docs/Mosquitto.md

Lines changed: 159 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,168 @@
11
# Mosquitto
2+
23
## References
34
- [Docker](https://hub.docker.com/_/eclipse-mosquitto)
45
- [Website](https://mosquitto.org/)
6+
- [mosquitto.conf](https://mosquitto.org/man/mosquitto-conf-5.html) documentation
7+
- [Setting up passwords](https://www.youtube.com/watch?v=1msiFQT_flo) video
8+
9+
## Definitions
10+
11+
- `docker-compose.yml`~/IOTstack/docker-compose.yml
12+
- `mosquitto.conf`~/IOTstack/services/mosquitto/mosquitto.conf
13+
- `mosquitto.log`~/IOTstack/volumes/mosquitto/log/mosquitto.log
14+
- `volumes/mosquitto`~/IOTstack/volumes/mosquitto/
15+
16+
## Logging
17+
18+
Mosquitto logging is controlled by `mosquitto.conf`. This is the default configuration:
19+
20+
```
21+
#log_dest file /mosquitto/log/mosquitto.log
22+
# To avoid flash wearing
23+
log_dest stdout
24+
```
25+
26+
When `log_dest` is set to `stdout`, you inspect Mosquitto's logs like this:
27+
28+
```
29+
$ docker logs mosquitto
30+
```
31+
32+
Logs written to `stdout` are ephemeral and will disappear when your IOTstack is restarted but this configuration reduces wear and tear on your SD card.
33+
34+
The alternative, which *may* be more appropriate if you are running on an SSD or HD, is to change `mosquitto.conf` to be like this:
535

6-
[Setting up passwords](https://www.youtube.com/watch?v=1msiFQT_flo)
36+
```
37+
log_dest file /mosquitto/log/mosquitto.log
38+
# To avoid flash wearing
39+
#log_dest stdout
40+
```
41+
42+
and then restart Mosquitto:
43+
44+
```
45+
$ cd ~/IOTstack
46+
$ docker-compose restart mosquitto
47+
```
48+
49+
With this configuration, you inspect Mosquitto's logs like this:
50+
51+
```
52+
$ tail ~/IOTstack/volumes/mosquitto/log/mosquitto.log
53+
```
54+
55+
Logs written to `mosquitto.log` do not disappear when your IOTstack is restarted. They persist until you take action to prune the file.
756

857
## Security
9-
By default, the Mosquitto container has no password. You can leave it that way if you like but its always a good idea to secure your services.
1058

11-
Step 1
12-
To add the password run `./services/mosquitto/terminal.sh`, I put some helper text in the script. Basically, you use the `mosquitto_passwd -c /mosquitto/config/pwfile MYUSER` command, replacing MYUSER with your username. it will then ask you to type your password and confirm it. exiting with `exit`.
59+
By default, the Mosquitto container has no password. You can leave it that way if you like but it's always a good idea to secure your services.
60+
61+
Assuming your IOTstack is running:
62+
63+
1. Open a shell in the mosquitto container:
64+
65+
```
66+
$ docker exec -it mosquitto sh
67+
```
68+
69+
2. In the following, replace «MYUSER» with the username you want to use for controlling access to Mosquitto and then run these commands:
70+
71+
```
72+
$ mosquitto_passwd -c /mosquitto/pwfile/pwfile «MYUSER»
73+
$ exit
74+
```
75+
76+
`mosquitto_passwd` will ask you to type a password and confirm it.
77+
78+
The path on the right hand side of:
79+
80+
```
81+
-c /mosquitto/pwfile/pwfile
82+
```
83+
84+
is **inside** the container. **Outside** the container, it maps to:
85+
86+
```
87+
~/IOTstack/volumes/mosquitto/pwfile/pwfile
88+
```
89+
90+
You should be able to see the result of setting a username and password like this:
91+
92+
```
93+
$ cat ~/IOTstack/volumes/mosquitto/pwfile/pwfile
94+
MYUSER:$6$lBYlxjWtLON0fm96$3qgcEyr/nKvxk3C2Jk36kkILJK7nLdIeLhuywVOVkVbJUjBeqUmCLOA/T6qAq2+hyyJdZ52ALTi+onMEEaM0qQ==
95+
$
96+
```
97+
98+
3. Open `mosquitto.conf` in a text editor. Find this line:
99+
100+
```
101+
#password_file /mosquitto/pwfile/pwfile
102+
```
103+
104+
Remove the # in front of password_file. Save.
105+
106+
4. Restart Mosquitto:
107+
108+
```
109+
$ cd ~/IOTstack
110+
$ docker-compose restart mosquitto
111+
```
112+
113+
5. Use the new credentials where necessary (eg Node-Red).
114+
115+
Notes:
116+
117+
* You can revert to password-disabled state by going back to step 3, re-inserting the "#", then restarting Mosquitto as per step 4.
118+
* If mosquitto keeps restarting after you implement password checking, the most likely explanation will be something wrong with the password file. Implement the advice in the previous note.
119+
120+
## Running as root
121+
122+
By default, the Mosquitto container is launched as root but then downgrades its privileges to run as user ID 1883.
123+
124+
Mosquitto is unusual because most containers just accept the privileges they were launched with. In most cases, that means containers run as root.
125+
126+
> <small>Don't make the mistake of thinking this means that processes running **inside** containers can do whatever they like to your host system. A process inside a container is **contained**. What a process can affect **outside** its container is governed by the port, device and volume mappings you see in the `docker-compose.yml`.</small>
127+
128+
You can check how mosquitto has been launched like this:
129+
130+
```
131+
$ ps -eo euser,ruser,suser,fuser,comm | grep mosquitto
132+
EUSER RUSER SUSER FUSER COMMAND
133+
1883 1883 1883 1883 mosquitto
134+
```
135+
136+
If you have a use-case that needs Mosquitto to run with root privileges:
137+
138+
1. Open `docker-compose.yml` in a text editor and find this:
139+
140+
```
141+
mosquitto:
142+
… [snip] …
143+
user: "1883"
144+
```
145+
146+
change it to:
147+
148+
```
149+
mosquitto:
150+
… [snip] …
151+
user: "0"
152+
```
153+
154+
2. Edit `mosquitto.conf` to add this line:
155+
156+
```
157+
user root
158+
```
159+
160+
3. Apply the change:
13161

14-
Step 2
15-
Edit the file called services/mosquitto/mosquitto.conf and remove the comment in front of password_file. Restart the container with `docker-compose restart mosquitto`. Type those credentials into Node-red etc.
162+
```
163+
$ cd ~/IOTstack
164+
$ docker-compose stop mosquitto
165+
$ docker-compose up -d
166+
```
167+
168+
> <small>A clean install of Mosquitto via the IOTstack menu sets everything in `volumes/mosquitto` to user and group 1883. That permission structure will still work if you change Mosquitto to run with root privileges. However, running as root **may** have the side effect of changing privilege levels within `volumes/mosquitto`. Keep this in mind if you decide to switch back to running Mosquitto as user 1883 because it is less likely to work.</small>

0 commit comments

Comments
 (0)