Skip to content

Commit 3935975

Browse files
style: ruff format (auto)
1 parent 8393571 commit 3935975

3 files changed

Lines changed: 66 additions & 54 deletions

File tree

src/config/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"yes",
8181
)
8282

83+
8384
# Skip the OpenSearch security context setup (roles, role mappings,
8485
# all_access admin pin). When true, OpenRAG assumes the security context
8586
# is managed externally (e.g., by Traefik in CPD or by a SaaS platform

tests/unit/services/test_skip_os_security_setup_startup.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,34 @@ async def test_setup_runs_when_flag_false(monkeypatch):
3838
monkeypatch.setattr("config.settings.IBM_AUTH_ENABLED", False, raising=False)
3939

4040
setup_mock = AsyncMock()
41-
with patch(
42-
"utils.opensearch_utils.setup_opensearch_security", setup_mock
43-
), patch.object(orchestrator, "wait_for_opensearch", AsyncMock()), patch.object(
44-
orchestrator, "init_index", AsyncMock()
45-
), patch.object(
46-
orchestrator, "configure_alerting_security", AsyncMock()
47-
), patch.object(
48-
orchestrator, "_reingest_default_docs_on_upgrade_if_needed",
49-
AsyncMock(return_value=False),
50-
), patch.object(
51-
orchestrator, "_update_mcp_server_urls", AsyncMock()
41+
with (
42+
patch("utils.opensearch_utils.setup_opensearch_security", setup_mock),
43+
patch.object(orchestrator, "wait_for_opensearch", AsyncMock()),
44+
patch.object(orchestrator, "init_index", AsyncMock()),
45+
patch.object(orchestrator, "configure_alerting_security", AsyncMock()),
46+
patch.object(
47+
orchestrator,
48+
"_reingest_default_docs_on_upgrade_if_needed",
49+
AsyncMock(return_value=False),
50+
),
51+
patch.object(orchestrator, "_update_mcp_server_urls", AsyncMock()),
5252
):
5353
# Force the post-security work to exit early — config.edited=False
5454
# short-circuits both the recovery init_index and the flow check.
5555
with patch.object(
56-
orchestrator, "get_openrag_config",
57-
MagicMock(return_value=MagicMock(edited=False, knowledge=MagicMock(embedding_model=None))),
56+
orchestrator,
57+
"get_openrag_config",
58+
MagicMock(
59+
return_value=MagicMock(edited=False, knowledge=MagicMock(embedding_model=None))
60+
),
5861
):
5962
services = _services_stub()
6063
services["task_service"] = MagicMock()
6164
services["document_service"] = MagicMock()
6265
services["langflow_file_service"] = MagicMock()
6366
services["session_manager"] = MagicMock()
6467
services["langflow_mcp_service"] = MagicMock()
65-
services["flows_service"] = MagicMock(
66-
ensure_flows_exist=AsyncMock(return_value=set())
67-
)
68+
services["flows_service"] = MagicMock(ensure_flows_exist=AsyncMock(return_value=set()))
6869
await orchestrator.startup_tasks(services)
6970

7071
assert setup_mock.await_count == 1, (
@@ -88,38 +89,38 @@ async def test_setup_skipped_when_flag_true(monkeypatch):
8889
logger_spy = MagicMock()
8990
monkeypatch.setattr(orchestrator, "logger", logger_spy)
9091

91-
with patch(
92-
"utils.opensearch_utils.setup_opensearch_security", setup_mock
93-
), patch.object(orchestrator, "wait_for_opensearch", AsyncMock()), patch.object(
94-
orchestrator, "init_index", AsyncMock()
95-
), patch.object(
96-
orchestrator, "configure_alerting_security", AsyncMock()
97-
), patch.object(
98-
orchestrator, "_reingest_default_docs_on_upgrade_if_needed",
99-
AsyncMock(return_value=False),
100-
), patch.object(
101-
orchestrator, "_update_mcp_server_urls", AsyncMock()
92+
with (
93+
patch("utils.opensearch_utils.setup_opensearch_security", setup_mock),
94+
patch.object(orchestrator, "wait_for_opensearch", AsyncMock()),
95+
patch.object(orchestrator, "init_index", AsyncMock()),
96+
patch.object(orchestrator, "configure_alerting_security", AsyncMock()),
97+
patch.object(
98+
orchestrator,
99+
"_reingest_default_docs_on_upgrade_if_needed",
100+
AsyncMock(return_value=False),
101+
),
102+
patch.object(orchestrator, "_update_mcp_server_urls", AsyncMock()),
102103
):
103104
with patch.object(
104-
orchestrator, "get_openrag_config",
105-
MagicMock(return_value=MagicMock(edited=False, knowledge=MagicMock(embedding_model=None))),
105+
orchestrator,
106+
"get_openrag_config",
107+
MagicMock(
108+
return_value=MagicMock(edited=False, knowledge=MagicMock(embedding_model=None))
109+
),
106110
):
107111
services = _services_stub()
108112
services["task_service"] = MagicMock()
109113
services["document_service"] = MagicMock()
110114
services["langflow_file_service"] = MagicMock()
111115
services["session_manager"] = MagicMock()
112116
services["langflow_mcp_service"] = MagicMock()
113-
services["flows_service"] = MagicMock(
114-
ensure_flows_exist=AsyncMock(return_value=set())
115-
)
117+
services["flows_service"] = MagicMock(ensure_flows_exist=AsyncMock(return_value=set()))
116118
await orchestrator.startup_tasks(services)
117119

118120
assert setup_mock.await_count == 0, (
119121
"setup_opensearch_security must NOT run when OPENRAG_SKIP_OS_SECURITY_SETUP is true"
120122
)
121123
info_messages = [call.args[0] for call in logger_spy.info.call_args_list if call.args]
122-
assert any(
123-
"Skipping OpenSearch security setup at startup" in msg
124-
for msg in info_messages
125-
), f"expected skip log line not emitted; got: {info_messages}"
124+
assert any("Skipping OpenSearch security setup at startup" in msg for msg in info_messages), (
125+
f"expected skip log line not emitted; got: {info_messages}"
126+
)

tests/unit/test_skip_os_security_setup_init_index.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ async def test_security_setup_called_when_flag_false(monkeypatch):
4343
os_client = _fake_os_client()
4444
setup_mock = AsyncMock()
4545

46-
with patch(
47-
"utils.opensearch_utils.setup_opensearch_security", setup_mock
48-
), patch.object(init_mod, "wait_for_opensearch", AsyncMock()), patch.object(
49-
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
50-
), patch.object(init_mod, "get_index_name", return_value="documents"):
46+
with (
47+
patch("utils.opensearch_utils.setup_opensearch_security", setup_mock),
48+
patch.object(init_mod, "wait_for_opensearch", AsyncMock()),
49+
patch.object(
50+
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
51+
),
52+
patch.object(init_mod, "get_index_name", return_value="documents"),
53+
):
5154
await init_mod.init_index(opensearch_client=os_client, admin_username="alice")
5255

5356
assert setup_mock.await_count == 1
@@ -70,18 +73,20 @@ async def test_security_setup_skipped_when_flag_true(monkeypatch):
7073
logger_spy = MagicMock()
7174
monkeypatch.setattr(init_mod, "logger", logger_spy)
7275

73-
with patch(
74-
"utils.opensearch_utils.setup_opensearch_security", setup_mock
75-
), patch.object(init_mod, "wait_for_opensearch", AsyncMock()), patch.object(
76-
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
77-
), patch.object(init_mod, "get_index_name", return_value="documents"):
76+
with (
77+
patch("utils.opensearch_utils.setup_opensearch_security", setup_mock),
78+
patch.object(init_mod, "wait_for_opensearch", AsyncMock()),
79+
patch.object(
80+
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
81+
),
82+
patch.object(init_mod, "get_index_name", return_value="documents"),
83+
):
7884
await init_mod.init_index(opensearch_client=os_client, admin_username="bob")
7985

8086
assert setup_mock.await_count == 0
8187
info_messages = [call.args[0] for call in logger_spy.info.call_args_list if call.args]
8288
assert any(
83-
"Skipping OpenSearch security setup during init_index" in msg
84-
for msg in info_messages
89+
"Skipping OpenSearch security setup during init_index" in msg for msg in info_messages
8590
), f"expected skip log line not emitted; got: {info_messages}"
8691

8792

@@ -96,15 +101,20 @@ async def test_index_creation_still_runs_when_flag_true(monkeypatch):
96101

97102
os_client = _fake_os_client()
98103

99-
with patch(
100-
"utils.opensearch_utils.setup_opensearch_security", AsyncMock()
101-
), patch.object(init_mod, "wait_for_opensearch", AsyncMock()), patch.object(
102-
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
103-
), patch.object(init_mod, "get_index_name", return_value="documents"):
104+
with (
105+
patch("utils.opensearch_utils.setup_opensearch_security", AsyncMock()),
106+
patch.object(init_mod, "wait_for_opensearch", AsyncMock()),
107+
patch.object(
108+
init_mod, "create_index_body", AsyncMock(return_value={"settings": {}, "mappings": {}})
109+
),
110+
patch.object(init_mod, "get_index_name", return_value="documents"),
111+
):
104112
await init_mod.init_index(opensearch_client=os_client)
105113

106114
# The three indices: documents, knowledge_filters, api_keys.
107-
created_indices = {call.kwargs.get("index") for call in os_client.indices.create.await_args_list}
115+
created_indices = {
116+
call.kwargs.get("index") for call in os_client.indices.create.await_args_list
117+
}
108118
assert "documents" in created_indices
109119
assert "knowledge_filters" in created_indices
110120
assert "api_keys" in created_indices, (

0 commit comments

Comments
 (0)