Add go2rtc binary config to expose api only on localhost (#129025)

This commit is contained in:
Robert Resch 2024-10-23 11:53:50 +02:00 committed by GitHub
parent 2c79173d20
commit a37bd824d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 11 deletions

View File

@ -9,12 +9,28 @@ from homeassistant.core import HomeAssistant
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_TERMINATE_TIMEOUT = 5 _TERMINATE_TIMEOUT = 5
# Default configuration for HA
# - Api is listening only on localhost
# - Disable rtsp listener
# - Clear default ice servers
_GO2RTC_CONFIG = """
api:
listen: "127.0.0.1:1984"
rtsp:
listen: ""
webrtc:
ice_servers: []
"""
def _create_temp_file() -> str: def _create_temp_file() -> str:
"""Create temporary config file.""" """Create temporary config file."""
# Set delete=False to prevent the file from being deleted when the file is closed # Set delete=False to prevent the file from being deleted when the file is closed
# Linux is clearing tmp folder on reboot, so no need to delete it manually # Linux is clearing tmp folder on reboot, so no need to delete it manually
with NamedTemporaryFile(prefix="go2rtc", suffix=".yaml", delete=False) as file: with NamedTemporaryFile(prefix="go2rtc_", suffix=".yaml", delete=False) as file:
file.write(_GO2RTC_CONFIG.encode())
return file.name return file.name
@ -43,8 +59,6 @@ class Server:
self._process = await asyncio.create_subprocess_exec( self._process = await asyncio.create_subprocess_exec(
self._binary, self._binary,
"-c", "-c",
"webrtc.ice_servers=[]",
"-c",
config_file, config_file,
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT, stderr=asyncio.subprocess.STDOUT,

View File

@ -4,7 +4,7 @@ import asyncio
from collections.abc import Generator from collections.abc import Generator
import logging import logging
import subprocess import subprocess
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, Mock, patch
import pytest import pytest
@ -21,13 +21,14 @@ def server(hass: HomeAssistant) -> Server:
@pytest.fixture @pytest.fixture
def mock_tempfile() -> Generator[MagicMock]: def mock_tempfile() -> Generator[Mock]:
"""Fixture to mock NamedTemporaryFile.""" """Fixture to mock NamedTemporaryFile."""
with patch( with patch(
"homeassistant.components.go2rtc.server.NamedTemporaryFile" "homeassistant.components.go2rtc.server.NamedTemporaryFile", autospec=True
) as mock_tempfile: ) as mock_tempfile:
mock_tempfile.return_value.__enter__.return_value.name = "test.yaml" file = mock_tempfile.return_value.__enter__.return_value
yield mock_tempfile file.name = "test.yaml"
yield file
@pytest.fixture @pytest.fixture
@ -42,11 +43,11 @@ def mock_process() -> Generator[MagicMock]:
yield mock_popen yield mock_popen
@pytest.mark.usefixtures("mock_tempfile")
async def test_server_run_success( async def test_server_run_success(
mock_process: MagicMock, mock_process: MagicMock,
server: Server, server: Server,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
mock_tempfile: Mock,
) -> None: ) -> None:
"""Test that the server runs successfully.""" """Test that the server runs successfully."""
# Simulate process output # Simulate process output
@ -63,13 +64,23 @@ async def test_server_run_success(
mock_process.assert_called_once_with( mock_process.assert_called_once_with(
TEST_BINARY, TEST_BINARY,
"-c", "-c",
"webrtc.ice_servers=[]",
"-c",
"test.yaml", "test.yaml",
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )
# Verify that the config file was written
mock_tempfile.write.assert_called_once_with(b"""
api:
listen: "127.0.0.1:1984"
rtsp:
listen: ""
webrtc:
ice_servers: []
""")
# Check that server read the log lines # Check that server read the log lines
for entry in ("log line 1", "log line 2"): for entry in ("log line 1", "log line 2"):
assert ( assert (