Skip to content

Commit

Permalink
Add go2rtc binary config to expose api only on localhost (home-assist…
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored and zxdavb committed Oct 24, 2024
1 parent 2e85d35 commit 6c62020
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
20 changes: 17 additions & 3 deletions homeassistant/components/go2rtc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
_LOGGER = logging.getLogger(__name__)
_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:
"""Create temporary config file."""
# 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
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


Expand Down Expand Up @@ -43,8 +59,6 @@ async def start(self) -> None:
self._process = await asyncio.create_subprocess_exec(
self._binary,
"-c",
"webrtc.ice_servers=[]",
"-c",
config_file,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
Expand Down
27 changes: 19 additions & 8 deletions tests/components/go2rtc/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Generator
import logging
import subprocess
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, Mock, patch

import pytest

Expand All @@ -21,13 +21,14 @@ def server(hass: HomeAssistant) -> Server:


@pytest.fixture
def mock_tempfile() -> Generator[MagicMock]:
def mock_tempfile() -> Generator[Mock]:
"""Fixture to mock NamedTemporaryFile."""
with patch(
"homeassistant.components.go2rtc.server.NamedTemporaryFile"
"homeassistant.components.go2rtc.server.NamedTemporaryFile", autospec=True
) as mock_tempfile:
mock_tempfile.return_value.__enter__.return_value.name = "test.yaml"
yield mock_tempfile
file = mock_tempfile.return_value.__enter__.return_value
file.name = "test.yaml"
yield file


@pytest.fixture
Expand All @@ -42,11 +43,11 @@ def mock_process() -> Generator[MagicMock]:
yield mock_popen


@pytest.mark.usefixtures("mock_tempfile")
async def test_server_run_success(
mock_process: MagicMock,
server: Server,
caplog: pytest.LogCaptureFixture,
mock_tempfile: Mock,
) -> None:
"""Test that the server runs successfully."""
# Simulate process output
Expand All @@ -63,13 +64,23 @@ async def test_server_run_success(
mock_process.assert_called_once_with(
TEST_BINARY,
"-c",
"webrtc.ice_servers=[]",
"-c",
"test.yaml",
stdout=subprocess.PIPE,
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
for entry in ("log line 1", "log line 2"):
assert (
Expand Down

0 comments on commit 6c62020

Please sign in to comment.