home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.52k stars 30.71k forks source link

mqtt integration: setting TLS settings only if client certificate is set? #125930

Closed stevenengland closed 1 month ago

stevenengland commented 1 month ago

The problem

Hi there,

this is regarding the line https://github.com/home-assistant/core/blob/94916ebbd184fe77e7efa6915a5ab28000047fb9/homeassistant/components/mqtt/client.py#L343

Doesn't that mean that

tls_version
tls_insecure

are only set if

CONF_CERTIFICATE

is also set? Shouldn't these options be set regardless of a client certificate being used?

Maybe this leads to errors like I face and that I described here: https://community.home-assistant.io/t/mqtt-integration-errno-104-connection-reset-by-peer/770346

What version of Home Assistant Core has the issue?

latest

What was the last working version of Home Assistant Core?

latest

What type of installation are you running?

Home Assistant OS

Integration causing the issue

mqtt

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 month ago

Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this issue as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `mqtt` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign mqtt` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


mqtt documentation mqtt source (message by IssueLinks)

jbouwh commented 1 month ago

There are multiple settings. The first is a certificate is expected (default is off). Then the root certificate can be installed, or if set to automatic, the certificate is expected to be trusted.

The validation of the certificate it self can be ignored. E.g. if there is an issue with the trust chain.

Then there is an option to use a client certificate for authentication. This only works of the broker has a secure connection. This option is not required. So you can have a username password with a secure connection, a client certificate or both.

stevenengland commented 1 month ago

Hi @jbouwh thanks for jumping in. But I am not sure if I correctly understand your explaination. What I need is the following:

This is what this would look like in MQTT Explorer: 11ecb52ae49cfddace7781d29b1029839d99d626

Is this covered by the current implementation HA?

From what I see so far I would say no. Here is why I think so:

Am I mistaken here? Where else are these options set that I think I need for my setup?

Kind regards!

jbouwh commented 1 month ago

If the certificate on your broker is self signed. Use the CA certificate and upload it. You can leave validation on.

stevenengland commented 1 month ago

And what about not setting a cert at all? Would that be a valid feature request?

jbouwh commented 1 month ago

IMO you do not need to turn off certificate validation is the broker certificate is trusted or if you supply the correct CA certificate (in case of a self signed certificate). There's no issue with the config flow options imo.

stevenengland commented 1 month ago

Okay, after reading the MQTT manual again I must admit that this flow is described there. I think my irritation origniates because I am not used to a flow that asks me for a CA if I want to use a completely untrusted but encrypted connection.

jbouwh commented 1 month ago

The default is not using TLS, so no broker cert validation is done, we do not need a feature for that. Why do you want an untrusted encrypted connection? If you use your own certificates, this is not needed.

jbouwh commented 1 month ago

About your issue description:

tls_version and tls_insecure are only used if the broker validation is set, and thus we support TLS. In other cases it does not make sense to pass a tls_version or to tell the tls connection is insecure as there is no TLS connection.

The whole idea of allowing an untrusted encrypted connection makes sense if you cannot or not want to supply the CA root cert / issuing cert. So IMO you do not need the option. But the tls_insecure option is part of the advanced settings.

I am going to close this as there is not an issue with Home Assistant Core.