mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Explain TLS failures in the IPP config flow (#195834)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c7894211d7
commit
b0e7d438b7
@@ -7,6 +7,7 @@ import probatio
|
||||
from pyipp import (
|
||||
IPP,
|
||||
IPPConnectionError,
|
||||
IPPConnectionTLSError,
|
||||
IPPConnectionUpgradeRequired,
|
||||
IPPError,
|
||||
IPPParseError,
|
||||
@@ -44,6 +45,35 @@ DATA_SCHEMA = probatio.Schema(
|
||||
)
|
||||
|
||||
|
||||
# OpenSSL reasons with a more specific message than "tls_error".
|
||||
_TLS_ERROR_KEYS = (
|
||||
("CERTIFICATE_VERIFY_FAILED", "tls_certificate"),
|
||||
("WRONG_VERSION_NUMBER", "tls_not_offered"),
|
||||
("RECORD_LAYER_FAILURE", "tls_not_offered"),
|
||||
("PACKET_LENGTH_TOO_LONG", "tls_not_offered"),
|
||||
("HANDSHAKE_FAILURE", "tls_unsupported"),
|
||||
("PROTOCOL_VERSION", "tls_unsupported"),
|
||||
("INSUFFICIENT_SECURITY", "tls_unsupported"),
|
||||
("NO_PROTOCOLS_AVAILABLE", "tls_unsupported"),
|
||||
("UNSUPPORTED_PROTOCOL", "tls_unsupported"),
|
||||
("NO_CIPHERS_AVAILABLE", "tls_unsupported"),
|
||||
)
|
||||
|
||||
|
||||
def _tls_reason_key(reason: str) -> str:
|
||||
"""Return the translation key for an OpenSSL failure reason."""
|
||||
for code, key in _TLS_ERROR_KEYS:
|
||||
if code in reason:
|
||||
return key
|
||||
return "tls_error"
|
||||
|
||||
|
||||
def _tls_error_key(err: IPPConnectionTLSError) -> str:
|
||||
"""Return the translation key for a failed TLS handshake."""
|
||||
_LOGGER.debug("TLS connection to printer failed: %s", err.reason, exc_info=err)
|
||||
return _tls_reason_key(err.reason)
|
||||
|
||||
|
||||
async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
|
||||
"""Validate the user input allows us to connect.
|
||||
|
||||
@@ -86,6 +116,8 @@ class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
info = await validate_input(self.hass, user_input)
|
||||
except IPPConnectionUpgradeRequired:
|
||||
return self._show_setup_form({"base": "connection_upgrade"})
|
||||
except IPPConnectionTLSError as err:
|
||||
return self._show_setup_form({"base": _tls_error_key(err)})
|
||||
except IPPConnectionError, IPPResponseError:
|
||||
_LOGGER.debug("IPP Connection/Response Error", exc_info=True)
|
||||
return self._show_setup_form({"base": "cannot_connect"})
|
||||
@@ -155,6 +187,8 @@ class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
info = await validate_input(self.hass, self.discovery_info)
|
||||
except IPPConnectionUpgradeRequired:
|
||||
return self.async_abort(reason="connection_upgrade")
|
||||
except IPPConnectionTLSError as err:
|
||||
return self.async_abort(reason=_tls_error_key(err))
|
||||
except IPPConnectionError, IPPResponseError:
|
||||
_LOGGER.debug("IPP Connection/Response Error", exc_info=True)
|
||||
return self.async_abort(reason="cannot_connect")
|
||||
@@ -229,6 +263,8 @@ class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
info = await validate_input(self.hass, user_input)
|
||||
except IPPConnectionUpgradeRequired:
|
||||
errors["base"] = "connection_upgrade"
|
||||
except IPPConnectionTLSError as err:
|
||||
errors["base"] = _tls_error_key(err)
|
||||
except IPPConnectionError, IPPResponseError:
|
||||
_LOGGER.debug("IPP Connection/Response Error", exc_info=True)
|
||||
errors["base"] = "cannot_connect"
|
||||
|
||||
@@ -8,13 +8,21 @@
|
||||
"ipp_error": "Encountered IPP error.",
|
||||
"ipp_version_error": "IPP version not supported by printer.",
|
||||
"parse_error": "Failed to parse response from printer.",
|
||||
"tls_certificate": "The printer's certificate could not be verified.",
|
||||
"tls_error": "The secure (TLS) connection to the printer failed.",
|
||||
"tls_not_offered": "The printer did not answer with TLS on the discovered port.",
|
||||
"tls_unsupported": "The printer rejected the secure connection. It may not support any of the TLS versions or cipher suites that Home Assistant offers.",
|
||||
"unique_id_required": "Device missing unique identification required for discovery."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"connection_upgrade": "Failed to connect to printer. Please try again with SSL/TLS option checked.",
|
||||
"ipp_error": "[%key:component::ipp::config::abort::ipp_error%]",
|
||||
"ipp_version_error": "[%key:component::ipp::config::abort::ipp_version_error%]"
|
||||
"ipp_version_error": "[%key:component::ipp::config::abort::ipp_version_error%]",
|
||||
"tls_certificate": "The printer's certificate could not be verified. If the printer uses a self-signed certificate, uncheck the option to verify the SSL certificate.",
|
||||
"tls_error": "[%key:component::ipp::config::abort::tls_error%]",
|
||||
"tls_not_offered": "The printer did not answer with TLS on this port. Uncheck the SSL/TLS option, or check the port.",
|
||||
"tls_unsupported": "[%key:component::ipp::config::abort::tls_unsupported%]"
|
||||
},
|
||||
"flow_title": "{name}",
|
||||
"step": {
|
||||
|
||||
@@ -7,6 +7,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
from pyipp import (
|
||||
IPPConnectionError,
|
||||
IPPConnectionTLSError,
|
||||
IPPConnectionUpgradeRequired,
|
||||
IPPError,
|
||||
IPPParseError,
|
||||
@@ -174,6 +175,79 @@ async def test_zeroconf_connection_upgrade_required(
|
||||
assert result["reason"] == "connection_upgrade"
|
||||
|
||||
|
||||
TLS_ERRORS = [
|
||||
pytest.param("SSLV3_ALERT_HANDSHAKE_FAILURE", "tls_unsupported", id="handshake"),
|
||||
pytest.param("TLSV1_ALERT_PROTOCOL_VERSION", "tls_unsupported", id="version"),
|
||||
pytest.param(
|
||||
"TLSV1_ALERT_INSUFFICIENT_SECURITY",
|
||||
"tls_unsupported",
|
||||
id="insufficient_security",
|
||||
),
|
||||
pytest.param("WRONG_VERSION_NUMBER", "tls_not_offered", id="not_tls"),
|
||||
pytest.param(
|
||||
"CERTIFICATE_VERIFY_FAILED: self-signed certificate",
|
||||
"tls_certificate",
|
||||
id="certificate",
|
||||
),
|
||||
pytest.param("connection reset during handshake", "tls_error", id="other"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("reason", "error"), TLS_ERRORS)
|
||||
async def test_user_tls_error(
|
||||
hass: HomeAssistant,
|
||||
mock_ipp_config_flow: MagicMock,
|
||||
reason: str,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test we explain a failed TLS handshake on the user form."""
|
||||
mock_ipp_config_flow.printer.side_effect = IPPConnectionTLSError(
|
||||
f"TLS handshake with IPP server failed: {reason}", reason=reason
|
||||
)
|
||||
|
||||
user_input = {**MOCK_USER_INPUT, CONF_SSL: True}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["step_id"] == "user"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_ipp_config_flow.printer.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=user_input
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "cfe92100-67c4-11d4-a45f-f8d027761251"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("reason", "error"), TLS_ERRORS)
|
||||
async def test_zeroconf_tls_error(
|
||||
hass: HomeAssistant,
|
||||
mock_ipp_config_flow: MagicMock,
|
||||
reason: str,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test we abort the zeroconf flow with an explanation of the TLS failure."""
|
||||
mock_ipp_config_flow.printer.side_effect = IPPConnectionTLSError(
|
||||
f"TLS handshake with IPP server failed: {reason}", reason=reason
|
||||
)
|
||||
|
||||
discovery_info = dataclasses.replace(MOCK_ZEROCONF_IPPS_SERVICE_INFO)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=discovery_info,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == error
|
||||
|
||||
|
||||
async def test_user_parse_error(
|
||||
hass: HomeAssistant,
|
||||
mock_ipp_config_flow: MagicMock,
|
||||
@@ -665,6 +739,36 @@ async def test_reconfigure_zeroconf_uuid_matching_device_serial(
|
||||
assert mock_config_entry.data[CONF_HOST] == "192.168.1.32"
|
||||
|
||||
|
||||
async def test_reconfigure_flow_tls_error(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_ipp_config_flow: MagicMock,
|
||||
) -> None:
|
||||
"""Test the reconfigure flow explains a TLS failure and can recover."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
mock_ipp_config_flow.printer.side_effect = IPPConnectionTLSError(
|
||||
"TLS handshake with IPP server failed: WRONG_VERSION_NUMBER",
|
||||
reason="WRONG_VERSION_NUMBER",
|
||||
)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=RECONFIGURE_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["errors"] == {"base": "tls_not_offered"}
|
||||
|
||||
mock_ipp_config_flow.printer.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=RECONFIGURE_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "error"),
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user