Refuse a disabled connector before falling back to a legacy tool secret

This commit is contained in:
arc53-machine committed 2026-09-29 00:41:57 +01:00
1 parent a260ca44f1
commit cdc5084e02
3 files changed
+30 -1

No files matched your search

+4 -1
View File
@@ -716,9 +716,13 @@ def create_api_key_connection(
``(row, created)``.
Raises:
ConnectorDisabled: An admin turned the connector off. Checked first,
so callers that fall back to a legacy path on the other errors
never do so for a disabled connector.
ValueError: A required credential field is missing.
EncryptionKeyNotConfigured: See :func:`ensure_can_store_credentials`.
"""
ensure_connector_allowed(conn, definition.key)
fields = {f.key: f for f in definition.credential_fields}
if fields:
missing = [f.label for f in fields.values() if f.required and not str(credentials.get(f.key) or "").strip()]
@@ -728,7 +732,6 @@ def create_api_key_connection(
else:
credentials = {k: v for k, v in credentials.items() if v not in (None, "")}
ensure_can_store_credentials()
ensure_connector_allowed(conn, definition.key)
secret_values = {k: v for k, v in credentials.items() if (fields.get(k).secret if fields.get(k) else True)}
repo = ConnectorSessionsRepository(conn)
existing, account_label = _api_key_account(
@@ -300,6 +300,21 @@ class TestApiKeyConnections:
pg_conn, "alice", catalog.get_definition("brave"), {"token": "secret-key-1234"},
)
def test_disabled_connector_refused_before_other_errors(self, pg_conn, monkeypatch):
from docsgpt.connectors import catalog
from docsgpt.core.settings import settings
from docsgpt.security.encryption import DEFAULT_ENCRYPTION_KEY
from docsgpt.storage.db.repositories.connector_policies import ConnectorPoliciesRepository
monkeypatch.setattr(settings, "ENCRYPTION_SECRET_KEY", DEFAULT_ENCRYPTION_KEY)
monkeypatch.setattr(settings, "AUTH_TYPE", "session_jwt")
ConnectorPoliciesRepository(pg_conn).upsert("telegram", enabled=False)
telegram = catalog.get_definition("telegram")
# Callers fall back to a legacy per-tool secret on these other errors.
for credentials in ({"token": "123456:ABCDEFG"}, {}):
with pytest.raises(service.ConnectorDisabled):
service.create_api_key_connection(pg_conn, "alice", telegram, credentials)
def test_default_key_allowed_single_user(self, pg_conn, monkeypatch):
from docsgpt.connectors import catalog
from docsgpt.core.settings import settings
+11
View File
@@ -136,6 +136,17 @@ class TestCreateConnectedTool:
resp = _call(app, CreateTool, _telegram())
assert resp.status_code == 403
def test_disabled_connector_is_refused_on_the_default_key(self, app, pg_conn):
from docsgpt.api.user.tools.routes import CreateTool
from docsgpt.storage.db.repositories.connector_policies import ConnectorPoliciesRepository
ConnectorPoliciesRepository(pg_conn).upsert("telegram", enabled=False)
with _db(pg_conn), patch.object(service, "ensure_can_store_credentials",
side_effect=service.EncryptionKeyNotConfigured("set a key")):
resp = _call(app, CreateTool, _telegram())
assert resp.status_code == 403
assert pg_conn.execute(text("SELECT count(*) FROM user_tools")).scalar() == 0
def test_default_key_on_multi_user_falls_back_to_the_tool(self, app, pg_conn):
from docsgpt.api.user.tools.routes import CreateTool