Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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