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

listener configuration with include_dir not working as expected #2148

Open
momoskitto opened this issue Mar 24, 2021 · 8 comments
Open

listener configuration with include_dir not working as expected #2148

momoskitto opened this issue Mar 24, 2021 · 8 comments

Comments

@momoskitto
Copy link

This has been observed on mosquitto 2.0.9 and also 2.0.8, installed on debian buster using deb http:https://repo.mosquitto.org/debian buster main

The following broker configuration does not work, mosquitto dies with "Error: Address already in use" (this is a minimal configuration to reproduce the issue, not my full configuration):

file /etc/mosquitto/mosquitto.conf:

pid_file /var/run/mosquitto/mosquitto.pid
log_dest file /var/log/mosquitto/mosquitto.log

per_listener_settings true
include_dir /etc/mosquitto/conf.d

Three files in /etc/mosquitto/conf.d:
01-options.conf:

listener 1884
allow_anonymous true

02-tls.conf:

cafile /etc/mosquitto/ca.crt

03-listener2.conf:

listener 1883

Starting mosquitto with mosquitto -c /etc/mosquitto/mosquitto.conf prints the following, then exits with status 1:

1616587041: Loading config file /etc/mosquitto/conf.d/01-options.conf
1616587041: Loading config file /etc/mosquitto/conf.d/02-tls.conf
1616587041: Loading config file /etc/mosquitto/conf.d/03-listener2.conf

the log file /var/log/mosquitto/mosquitto.log contains:

1616587041: mosquitto version 2.0.9 starting
1616587041: Config loaded from /etc/mosquitto/mosquitto.conf.
1616587041: Opening ipv4 listen socket on port 1884.
1616587041: Opening ipv6 listen socket on port 1884.
1616587041: Opening ipv4 listen socket on port 1883.
1616587041: Opening ipv6 listen socket on port 1883.
1616587041: Opening ipv4 listen socket on port 1883.
1616587041: Error: Address already in use

Removing the line cafile /etc/mosquitto/ca.crt from 02-tls.conf and adding it to the end of 01-options.conf allows mosquitto to run. Console out:

1616587172: Loading config file /etc/mosquitto/conf.d/01-options.conf
1616587172: Loading config file /etc/mosquitto/conf.d/02-tls.conf
1616587172: Loading config file /etc/mosquitto/conf.d/03-listener2.conf

and log file:

1616587172: mosquitto version 2.0.9 starting
1616587172: Config loaded from /etc/mosquitto/mosquitto.conf.
1616587172: Opening ipv4 listen socket on port 1884.
1616587172: Opening ipv6 listen socket on port 1884.
1616587172: Opening ipv4 listen socket on port 1883.
1616587172: Opening ipv6 listen socket on port 1883.
1616587172: mosquitto version 2.0.9 running

It appears to me that with the cafile ... line in 02-tls.conf, mosquitto activates a listener on the default port 1883 with this setting, instead of configuring the listener defined in 01-options.conf. The listener on port 1883 defined in 03-listener2.confthen cannot start because the port is already in use. However, moving the line cafile ... to the end of 01-options.conf changes the setting of the listener on port 1884 instead of starting a new listener. From the documentation (man mosquitto.conf), I would have expected the two configurations to be equivalent. If the observed behaviour is as expected, then maybe the documentation could be improved.

@ralight
Copy link
Contributor

ralight commented Mar 25, 2021

Your guess is correct, on starting a new file any listeners defined are no longer the focus of the configuration. My feeling is that this is a documentation error, I think it is more robust to have the listener configuration in a single file. What benefit do you want to get from separating the configurations like this?

@guydingmike
Copy link

I would also like to separate my configuration like this.
In my case I have multiple applications that will use Mosquitto.
They are installed in any combination.
Mosquitto is viewed as a service that by default (by itself) we don't want to be listening on anything.
As each of our applications is installed they define what mosquitto configuration they need, listeners, ACL, users, etc.
(The coordination of port usage etc. is done at a higher level and is outside the scope of the individual applications and mosquitto.)

@momoskitto
Copy link
Author

What benefit do you want to get from separating the configurations like this?

This is not a requirement for me. When I started using mosquitto in version 1.x, I thought it was nice to separate aspects of the configuration into individual files, so I had one config file for general settings, one for the TLS settings, one for ACL. Later, I added a second listener with an additional config file.

I created this bug entry, because this configuration does not work any more with mosquitto 2, and I found no indication about this being intended, neither in the documentation man mosquitto.conf, nor in the migration guide at https://mosquitto.org/documentation/migrating-to-2-0/.
For me personally, this separation of configuration is absolutely not necessary. I can see the advantages of not doing it, in particular for multiple listeners, to avoid being too dependent on the order in which config files are read. I have changed my configuration accordingly and don't intend to go back, even if this should be possible in a future version.

Assuming that the behaviour of mosquitto is going to stay as it is, I think it would be good to make that more clear in the documentation. Also, I could imagine that it would be useful to log a warning if some configuration option causes the default listener to be started without it being stated explicitely in the configuration file. This would probably also help in situations like described in issue #2103.

@ralight
Copy link
Contributor

ralight commented Apr 3, 2021

The whole point of having the config directory is to allow configuration to be separated, the reason I was asking is because to me personally it makes sense to have a configuration file for a listener with all its settings, but having the listener configuration across multiple files seemed... confusing.

You're right to highlight this though, it wasn't an intentional change.

@guydingmike I'm not quite sure what you mean by the port usage being done at a higher level - do you mean that you really do need to have separate files with listener 1883 and cafile ... in them, and that different processes control those files?

@guydingmike
Copy link

@ralight Sorry, I was unclear by trying to be too concise. I'm new to Mosquitto so it is completely possible I just don't understand how to configure what I'm looking for.

I've created my own RPM of Mosquitto that I would like to be generic so I can install it on various products/systems. I do not want that installation of Mosquitto to start any listeners. I have other RPMs that should install configuration for Mosquitto (in the Mosquitto conf directory). These RPMs (not Mosquitto) "know" what their topics are, what users there are and what ports the listeners should be on. The RPMs require Mosquitto to be installed first.

The idea is that only the application RPMs themselves know what the topics are, who the users are and what ports should be used.

In a similar vein, the Bridge dynamic 2.0 #1926 and Dynamic Topic Bridging #292 are of interest to me. The applications connecting to Mosquitto have the logic and information to make decisions about whether a bridge should be up or down and the configuration for the bridge. The bridge configuration may not be known at Mosquitto startup and/or we might not want the bridge to come up when Mosquitto is started (which would typically be at boot).

Sorry if I went on too long Roger.
PS. Thank you and everyone else who have contributed for making a great product

@VojtechMyslivec
Copy link

I can confirm that I have encountered this issue as well. I upgraded the mosquitto from version 1.6.x to 2.0.10 and I noticed deprecation warning about port option. So I switched to listener as advised, but it leads to not starting mosquitto:

Configuration change:

-port 1883
+listener 1883

leads to

2021-04-21 00:29:42: mosquitto version 2.0.10 starting
2021-04-21 00:29:42: Config loaded from /etc/mosquitto/mosquitto.conf.
2021-04-21 00:29:42: Opening ipv4 listen socket on port 1883.
2021-04-21 00:29:42: Opening ipv6 listen socket on port 1883.
2021-04-21 00:29:42: Opening ipv4 listen socket on port 1883.
2021-04-21 00:29:42: Error: Address already in use

When I switch the configuration option back to port, it seems it works as expected. Also, when I moved all configuration from separated files in conf.d/ (which I used until now to split the config files for better automation) to one mosquitto.conf, it works as expected even with listener option.

I would understand this change. However, it is not mentioned in the documentation neither for the port option nor for the listener option. Only this github issue contains clues about joining the configuration files.

Thanks for the mosquitto BTW 😉

@ralight
Copy link
Contributor

ralight commented Apr 20, 2021

@VojtechMyslivec My bet is that you have another config option that enables the "default listener" - it's still a bug in mosquitto, but you can get around it.

@victorkirov
Copy link

We ran into this today. We have the main config file with:

listener 1883
allow_anonymous true
include_dir /some_dir/conf/

And a second config file in /some_dir/conf/:

certfile /certs/srv.crt
keyfile /certs/srv.key

Running this configuration will attempt to start a listener in the main file and that listener will only have the allow_anonymous true config. It will then try to start another listener because of the second file in the include directory, but because no listener was specified, it attempts to use the default 1883 port.

Adding listener 8883 to the included file starts up another listener on that port that has the TLS certs and allow_anonymous true from the main config.

The solution is to treat the main file as a global shared config for all configurations in the included folder and not specifying a listener in there, and then either specify a listener port in each included config file, or leave blank in one of them to use the default port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants