mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-28 08:17:07 +03:00
match masked camera paths with the running config's environment_vars
Matching a masked path under both the startup and saved environment_vars could pick the wrong input when a saved but unapplied change made two hosts collide, so a role swap saved one input's path for both. Saved paths now resolve only with the block the running config was built from, which is what /config showed.
This commit is contained in:
+21
-29
@@ -791,16 +791,20 @@ def config_save(save_option: str, body: Any = Body(media_type="text/plain")):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None:
|
def _restore_masked_camera_paths(
|
||||||
|
config_data: dict, saved: dict, shown_env_vars: dict[str, str]
|
||||||
|
) -> str | None:
|
||||||
"""Substitute incoming masked camera paths with the saved ones.
|
"""Substitute incoming masked camera paths with the saved ones.
|
||||||
|
|
||||||
The /config response masks ffmpeg input credentials, so the settings UI
|
The /config response masks ffmpeg input credentials, so the settings UI
|
||||||
sends the masked path back when sibling fields (e.g. hwaccel_args) are
|
sends the masked path back when sibling fields (e.g. hwaccel_args) are
|
||||||
edited. Each masked path is matched to the saved input that masks to the
|
edited. Each masked path is matched to the saved input that masks to the
|
||||||
same value, with roles breaking a tie, so deleting or reordering inputs
|
same value, with roles breaking a tie, so deleting or reordering inputs
|
||||||
keeps each path's own credentials. The saved path is restored as written,
|
keeps each path's own credentials. Saved paths resolve with
|
||||||
keeping any {FRIGATE_*} placeholder. Mutates `config_data` in place and
|
`shown_env_vars`, the block the running config and /config used. The saved
|
||||||
returns an error message when a masked path has no single match.
|
path is restored as written, keeping any {FRIGATE_*} placeholder. Mutates
|
||||||
|
`config_data` in place and returns an error message when a masked path has
|
||||||
|
no single match.
|
||||||
"""
|
"""
|
||||||
cameras = config_data.get("cameras")
|
cameras = config_data.get("cameras")
|
||||||
if not isinstance(cameras, dict):
|
if not isinstance(cameras, dict):
|
||||||
@@ -810,8 +814,6 @@ def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None:
|
|||||||
if not isinstance(saved_cameras, dict):
|
if not isinstance(saved_cameras, dict):
|
||||||
saved_cameras = {}
|
saved_cameras = {}
|
||||||
|
|
||||||
saved_env_vars = saved.get("environment_vars")
|
|
||||||
|
|
||||||
for camera_name, camera_data in cameras.items():
|
for camera_name, camera_data in cameras.items():
|
||||||
if not isinstance(camera_data, dict):
|
if not isinstance(camera_data, dict):
|
||||||
continue
|
continue
|
||||||
@@ -838,11 +840,13 @@ def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None:
|
|||||||
path = input_obj.get("path")
|
path = input_obj.get("path")
|
||||||
if not isinstance(path, str) or not is_masked_camera_path(path):
|
if not isinstance(path, str) or not is_masked_camera_path(path):
|
||||||
continue
|
continue
|
||||||
candidates = [
|
with candidate_env_vars(shown_env_vars):
|
||||||
inp
|
candidates = [
|
||||||
for inp in saved_inputs
|
inp
|
||||||
if path in _masked_saved_paths(inp["path"], saved_env_vars)
|
for inp in saved_inputs
|
||||||
]
|
if clean_camera_user_pass(_resolve_frigate_vars(inp["path"]))
|
||||||
|
== path
|
||||||
|
]
|
||||||
if len({inp["path"] for inp in candidates}) > 1:
|
if len({inp["path"] for inp in candidates}) > 1:
|
||||||
candidates = [
|
candidates = [
|
||||||
inp
|
inp
|
||||||
@@ -868,21 +872,6 @@ def _resolve_frigate_vars(value: str) -> str:
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def _masked_saved_paths(path: str, saved_env_vars: Any) -> set[str]:
|
|
||||||
"""Mask a saved camera path the ways /config can show it.
|
|
||||||
|
|
||||||
/config resolves placeholders with the installed environment_vars until a
|
|
||||||
no-restart update swaps in a config parsed with the saved block.
|
|
||||||
"""
|
|
||||||
with candidate_env_vars(saved_env_vars):
|
|
||||||
saved_resolved = _resolve_frigate_vars(path)
|
|
||||||
|
|
||||||
return {
|
|
||||||
clean_camera_user_pass(_resolve_frigate_vars(path)),
|
|
||||||
clean_camera_user_pass(saved_resolved),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _saved_config_from_memory(config: FrigateConfig) -> dict[str, Any]:
|
def _saved_config_from_memory(config: FrigateConfig) -> dict[str, Any]:
|
||||||
"""Shape the in-memory camera inputs like the saved YAML."""
|
"""Shape the in-memory camera inputs like the saved YAML."""
|
||||||
return {
|
return {
|
||||||
@@ -901,14 +890,14 @@ def _saved_config_from_memory(config: FrigateConfig) -> dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def _config_data_to_updates(
|
def _config_data_to_updates(
|
||||||
config_data: dict, saved: dict[str, Any]
|
config_data: dict, saved: dict[str, Any], running: FrigateConfig
|
||||||
) -> tuple[dict[str, Any], str | None]:
|
) -> tuple[dict[str, Any], str | None]:
|
||||||
"""Flatten a /config/set payload after restoring masked credentials.
|
"""Flatten a /config/set payload after restoring masked credentials.
|
||||||
|
|
||||||
`saved` is the config the masked values were taken from. Returns the flat
|
`saved` is the config the masked values were taken from. Returns the flat
|
||||||
updates, or an error message when a masked value can't be restored.
|
updates, or an error message when a masked value can't be restored.
|
||||||
"""
|
"""
|
||||||
error = _restore_masked_camera_paths(config_data, saved)
|
error = _restore_masked_camera_paths(config_data, saved, running.environment_vars)
|
||||||
if error:
|
if error:
|
||||||
return {}, error
|
return {}, error
|
||||||
|
|
||||||
@@ -962,7 +951,9 @@ def _config_set_in_memory(request: Request, body: AppConfigSetBody) -> JSONRespo
|
|||||||
updates = {}
|
updates = {}
|
||||||
if body.config_data:
|
if body.config_data:
|
||||||
updates, error = _config_data_to_updates(
|
updates, error = _config_data_to_updates(
|
||||||
body.config_data, _saved_config_from_memory(request.app.frigate_config)
|
body.config_data,
|
||||||
|
_saved_config_from_memory(request.app.frigate_config),
|
||||||
|
request.app.frigate_config,
|
||||||
)
|
)
|
||||||
if error:
|
if error:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@@ -1143,6 +1134,7 @@ def config_set(request: Request, body: AppConfigSetBody):
|
|||||||
updates, error = _config_data_to_updates(
|
updates, error = _config_data_to_updates(
|
||||||
body.config_data,
|
body.config_data,
|
||||||
saved_config if isinstance(saved_config, dict) else {},
|
saved_config if isinstance(saved_config, dict) else {},
|
||||||
|
request.app.frigate_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
|
|||||||
@@ -669,6 +669,51 @@ class TestConfigSetWildcardPropagation(BaseTestHttp):
|
|||||||
finally:
|
finally:
|
||||||
os.unlink(config_path)
|
os.unlink(config_path)
|
||||||
|
|
||||||
|
@patch.dict(
|
||||||
|
"frigate.util.env._CONFIG_ENV_VARS",
|
||||||
|
{"FRIGATE_HOST_A": "10.0.0.1", "FRIGATE_HOST_B": "10.0.0.2"},
|
||||||
|
)
|
||||||
|
@patch.dict(
|
||||||
|
"frigate.util.env.FRIGATE_ENV_VARS",
|
||||||
|
{"FRIGATE_HOST_A": "10.0.0.1", "FRIGATE_HOST_B": "10.0.0.2"},
|
||||||
|
)
|
||||||
|
@patch("frigate.api.app.find_config_file")
|
||||||
|
def test_masked_paths_match_running_env_vars(self, mock_find_config):
|
||||||
|
"""A saved but not yet applied environment_vars change must not make
|
||||||
|
one input's masked path match another's."""
|
||||||
|
path_a = "rtsp://admin:pw@{FRIGATE_HOST_A}:554/live"
|
||||||
|
path_b = "rtsp://admin:pw@{FRIGATE_HOST_B}:554/live"
|
||||||
|
self._set_inputs([(path_a, "detect"), (path_b, "record")])
|
||||||
|
self.minimal_config["environment_vars"] = {
|
||||||
|
"FRIGATE_HOST_A": "10.0.0.1",
|
||||||
|
"FRIGATE_HOST_B": "10.0.0.2",
|
||||||
|
}
|
||||||
|
app, _ = self._create_app_with_publisher()
|
||||||
|
|
||||||
|
self.minimal_config["environment_vars"]["FRIGATE_HOST_A"] = "10.0.0.2"
|
||||||
|
config_path = self._write_config_file()
|
||||||
|
mock_find_config.return_value = config_path
|
||||||
|
|
||||||
|
try:
|
||||||
|
with AuthTestClient(app) as client:
|
||||||
|
camera = client.get("/config").json()["cameras"]["front_door"]
|
||||||
|
inputs = camera["ffmpeg"]["inputs"]
|
||||||
|
inputs[0]["roles"], inputs[1]["roles"] = ["record"], ["detect"]
|
||||||
|
|
||||||
|
resp = self._save(
|
||||||
|
client, {"cameras": {"front_door": {"ffmpeg": {"inputs": inputs}}}}
|
||||||
|
)
|
||||||
|
self.assertEqual(resp.status_code, 200, resp.text)
|
||||||
|
|
||||||
|
_, saved = self._read_config_file(config_path)
|
||||||
|
saved_inputs = saved["cameras"]["front_door"]["ffmpeg"]["inputs"]
|
||||||
|
self.assertEqual(
|
||||||
|
[(inp["path"], inp["roles"]) for inp in saved_inputs],
|
||||||
|
[(path_a, ["record"]), (path_b, ["detect"])],
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
os.unlink(config_path)
|
||||||
|
|
||||||
@patch("frigate.api.app.find_config_file")
|
@patch("frigate.api.app.find_config_file")
|
||||||
def test_over_masked_path_round_trips(self, mock_find_config):
|
def test_over_masked_path_round_trips(self, mock_find_config):
|
||||||
path = "rtsp://10.0.0.1:554/live?x=a@b"
|
path = "rtsp://10.0.0.1:554/live?x=a@b"
|
||||||
|
|||||||
Reference in New Issue
Block a user