From 7fada806af1ef922550b2a5bd573846e4a28e85c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Jan 2021 10:46:15 -1000 Subject: [PATCH] Downgrade dhcp log message error message when running without CAP_NET_RAW (#45157) --- homeassistant/components/dhcp/__init__.py | 8 +++++- tests/components/dhcp/test_init.py | 33 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index b677325726c..b45add24ee9 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -2,6 +2,7 @@ import fnmatch import logging +import os from threading import Event, Thread from scapy.error import Scapy_Exception @@ -70,7 +71,12 @@ class DHCPWatcher(Thread): stop_filter=lambda _: self._stop_event.is_set(), ) except (Scapy_Exception, OSError) as ex: - _LOGGER.info("Cannot watch for dhcp packets: %s", ex) + if os.geteuid() == 0: + _LOGGER.error("Cannot watch for dhcp packets: %s", ex) + else: + _LOGGER.debug( + "Cannot watch for dhcp packets without root or CAP_NET_RAW: %s", ex + ) return def handle_dhcp_packet(self, packet): diff --git a/tests/components/dhcp/test_init.py b/tests/components/dhcp/test_init.py index d4d22a5f929..04cbc401b08 100644 --- a/tests/components/dhcp/test_init.py +++ b/tests/components/dhcp/test_init.py @@ -281,8 +281,8 @@ async def test_setup_and_stop(hass): wait_event.set() -async def test_setup_fails(hass): - """Test we handle sniff setup failing.""" +async def test_setup_fails_as_root(hass, caplog): + """Test we handle sniff setup failing as root.""" assert await async_setup_component( hass, @@ -293,10 +293,37 @@ async def test_setup_fails(hass): wait_event = threading.Event() - with patch("homeassistant.components.dhcp.sniff", side_effect=Scapy_Exception): + with patch("os.geteuid", return_value=0), patch( + "homeassistant.components.dhcp.sniff", side_effect=Scapy_Exception + ): hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done() wait_event.set() + assert "Cannot watch for dhcp packets" in caplog.text + + +async def test_setup_fails_non_root(hass, caplog): + """Test we handle sniff setup failing as non-root.""" + + assert await async_setup_component( + hass, + dhcp.DOMAIN, + {}, + ) + await hass.async_block_till_done() + + wait_event = threading.Event() + + with patch("os.geteuid", return_value=10), patch( + "homeassistant.components.dhcp.sniff", side_effect=Scapy_Exception + ): + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() + + hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) + await hass.async_block_till_done() + wait_event.set() + assert "Cannot watch for dhcp packets without root or CAP_NET_RAW" in caplog.text