Führe die Konfigurationsansicht der Connectoren ein, damit Paperless und IMAP ihre Felder selbst deklarieren.
Das Modul liefert ein Feldmanifest und validiert es; ein leeres Geheimnisfeld behält das gespeicherte. Die Oberfläche rendert die Felder generisch in einer Detailansicht je Zugang mit Prüfstand, Fähigkeiten und Verwendung. Die providerspezifischen Formulare in Sources.jsx entfallen, die flachen API-Felder bleiben als Kompatibilität. Das Standardfach eines IMAP-Kontos öffnet der Picker zuerst. Die Backend-Tests wurden in der Entwicklungsumgebung nicht ausgeführt; der Lauf auf develop steht aus. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c29bd29f34
commit
eb58dce4cd
20 files changed
+1071
-244
No files matched your search
@@ -304,7 +304,7 @@ class ConnectorIn(APIModel):
|
||||
password: str | None = None
|
||||
mailbox: str | None = Field(default=None, max_length=200)
|
||||
security: Literal["ssl", "starttls"] | None = None
|
||||
config: dict[str, str] | None = None
|
||||
config: dict[str, str | int | None] | None = None
|
||||
|
||||
|
||||
class SourceAssignmentIn(APIModel):
|
||||
|
||||
@@ -21,10 +21,13 @@ from app.sourcebind import assign_reference
|
||||
from app.sourceport import capability_payload, with_capabilities
|
||||
from app.sourceregistry import get_source, require_source
|
||||
from app.sources import (
|
||||
connector_detail,
|
||||
delete_connector,
|
||||
imap_preview,
|
||||
list_connectors,
|
||||
owned_connector,
|
||||
paperless_content,
|
||||
parse_connector,
|
||||
probe,
|
||||
save_connector,
|
||||
update_connector,
|
||||
@@ -52,11 +55,16 @@ def source_capabilities(principal: Principal = Depends(get_principal)):
|
||||
|
||||
@router.post("/api/v1/connectors", status_code=201)
|
||||
def post_connector(body: ConnectorIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
name, settings, secret = _connector_parts(body)
|
||||
name, settings, secret = _connector_parts(body, None)
|
||||
row = save_connector(conn, principal.account_id, body.kind, name, settings, secret)
|
||||
return row
|
||||
|
||||
|
||||
@router.get("/api/v1/connectors/{connector_id}")
|
||||
def get_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
return connector_detail(conn, principal.account_id, connector_id)
|
||||
|
||||
|
||||
@router.patch("/api/v1/connectors/{connector_id}")
|
||||
def patch_connector(
|
||||
connector_id: UUID,
|
||||
@@ -64,12 +72,10 @@ def patch_connector(
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
name, settings, secret = _connector_parts(body)
|
||||
current = next((item for item in list_connectors(conn, principal.account_id) if item["id"] == str(connector_id)), None)
|
||||
if current is None:
|
||||
raise not_found()
|
||||
current = owned_connector(conn, principal.account_id, connector_id)
|
||||
if current["kind"] != body.kind:
|
||||
raise unprocessable("Der Zugang bleibt bei seiner Quellenart.")
|
||||
name, settings, secret = _connector_parts(body, current)
|
||||
return update_connector(conn, principal.account_id, connector_id, name, settings, secret)
|
||||
|
||||
|
||||
@@ -382,8 +388,10 @@ def assign_source(
|
||||
return {"id": str(element_id), "connector_id": str(body.connector_id), "resolution": "bound"}
|
||||
|
||||
|
||||
def _connector_parts(body: ConnectorIn) -> tuple[str, dict, str]:
|
||||
return require_source(body.kind).parse_config(body.model_dump())
|
||||
def _connector_parts(body: ConnectorIn, current: dict | None) -> tuple[str, dict, str]:
|
||||
"""The registered module validates. Flat fields of the first release are folded into ``config``."""
|
||||
flat = body.model_dump(exclude={"kind", "name", "config"})
|
||||
return parse_connector(body.kind, body.name, flat, body.config, current)
|
||||
|
||||
|
||||
_SEARCH_FLAGS = {
|
||||
|
||||
@@ -73,6 +73,15 @@ def capability_payload() -> dict:
|
||||
|
||||
return {
|
||||
"sources": [module.capabilities for module in registered_sources()],
|
||||
"modules": [
|
||||
{
|
||||
"kind": module.kind,
|
||||
"label": module.label,
|
||||
"representation": module.representation,
|
||||
"config_fields": [item.public() for item in module.config_fields],
|
||||
}
|
||||
for module in registered_sources()
|
||||
],
|
||||
"editors": EDITORS,
|
||||
"protection_profiles": {
|
||||
"legacy-plaintext": "cleartext",
|
||||
|
||||
@@ -7,6 +7,46 @@ from typing import Callable
|
||||
|
||||
from app.core.errors import unprocessable
|
||||
|
||||
FIELD_TYPES = ("text", "url", "number", "password", "select")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ConfigField:
|
||||
"""One configuration field a module declares for its connector instances.
|
||||
|
||||
At most one field per module is a secret. Its value is stored apart from
|
||||
the settings and is never handed out again.
|
||||
"""
|
||||
|
||||
key: str
|
||||
label: str
|
||||
type: str = "text"
|
||||
required: bool = False
|
||||
secret: bool = False
|
||||
default: str | int | None = None
|
||||
options: tuple[tuple[str, str], ...] = ()
|
||||
help: str = ""
|
||||
placeholder: str = ""
|
||||
minimum: int | None = None
|
||||
maximum: int | None = None
|
||||
max_length: int = 200
|
||||
|
||||
def public(self) -> dict:
|
||||
return {
|
||||
"key": self.key,
|
||||
"label": self.label,
|
||||
"type": self.type,
|
||||
"required": self.required,
|
||||
"secret": self.secret,
|
||||
"default": self.default,
|
||||
"options": [{"value": value, "label": label} for value, label in self.options],
|
||||
"help": self.help,
|
||||
"placeholder": self.placeholder,
|
||||
"minimum": self.minimum,
|
||||
"maximum": self.maximum,
|
||||
"max_length": self.max_length,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class SourceModule:
|
||||
@@ -14,11 +54,14 @@ class SourceModule:
|
||||
label: str
|
||||
representation: str
|
||||
capabilities: dict
|
||||
parse_config: Callable[[dict], tuple[str, dict, str]]
|
||||
probe: Callable
|
||||
find: Callable
|
||||
open_original: Callable
|
||||
export_original: Callable
|
||||
# A module either declares fields (generic validation) or parses the payload itself.
|
||||
parse_config: Callable[[dict], tuple[str, dict, str]] | None = None
|
||||
config_fields: tuple[ConfigField, ...] = ()
|
||||
normalize_config: Callable[[dict, str], dict] | None = None
|
||||
navigate: Callable | None = None
|
||||
preview: Callable | None = None
|
||||
facets: Callable | None = None
|
||||
@@ -28,6 +71,13 @@ _MODULES: dict[str, SourceModule] = {}
|
||||
|
||||
|
||||
def register_source(module: SourceModule) -> None:
|
||||
if module.parse_config is None and not module.config_fields:
|
||||
raise ValueError(f"Quelle {module.kind} braucht Konfigurationsfelder oder parse_config.")
|
||||
if sum(1 for item in module.config_fields if item.secret) > 1:
|
||||
raise ValueError(f"Quelle {module.kind} darf höchstens ein Geheimnisfeld haben.")
|
||||
for item in module.config_fields:
|
||||
if item.type not in FIELD_TYPES:
|
||||
raise ValueError(f"Feldtyp {item.type} ist unbekannt.")
|
||||
_MODULES[module.kind] = module
|
||||
|
||||
|
||||
@@ -52,3 +102,61 @@ def require_source(kind: str) -> SourceModule:
|
||||
|
||||
def accepts_reference(source: str) -> bool:
|
||||
return source == "url" or source in _MODULES
|
||||
|
||||
|
||||
def _blank(value) -> bool:
|
||||
return value is None or (isinstance(value, str) and not value.strip())
|
||||
|
||||
|
||||
def _coerce(field: ConfigField, value):
|
||||
if field.type == "number":
|
||||
try:
|
||||
number = int(str(value).strip())
|
||||
except (TypeError, ValueError):
|
||||
raise unprocessable(f"{field.label} muss eine ganze Zahl sein.") from None
|
||||
if field.minimum is not None and number < field.minimum:
|
||||
raise unprocessable(f"{field.label} ist zu klein.")
|
||||
if field.maximum is not None and number > field.maximum:
|
||||
raise unprocessable(f"{field.label} ist zu groß.")
|
||||
return number
|
||||
text = str(value) if field.secret else str(value).strip()
|
||||
if len(text) > field.max_length:
|
||||
raise unprocessable(f"{field.label} ist zu lang.")
|
||||
if field.type == "select" and text not in {option for option, _label in field.options}:
|
||||
raise unprocessable(f"{field.label} ist keine zulässige Auswahl.")
|
||||
return text
|
||||
|
||||
|
||||
def parse_fields(
|
||||
fields: tuple[ConfigField, ...],
|
||||
values: dict,
|
||||
current_settings: dict | None = None,
|
||||
current_secret: str = "",
|
||||
) -> tuple[dict, str]:
|
||||
"""Validate declared fields. Unknown keys are refused, a blank secret keeps the stored one."""
|
||||
known = {item.key for item in fields}
|
||||
extra = sorted(key for key, value in values.items() if key not in known and not _blank(value))
|
||||
if extra:
|
||||
raise unprocessable("Unbekanntes Konfigurationsfeld: " + ", ".join(extra[:3]) + ".")
|
||||
current_settings = current_settings or {}
|
||||
settings: dict = {}
|
||||
secret = current_secret
|
||||
for item in fields:
|
||||
raw = values.get(item.key)
|
||||
if item.secret:
|
||||
if _blank(raw):
|
||||
if not current_secret and item.required:
|
||||
raise unprocessable(f"{item.label} fehlt.")
|
||||
continue
|
||||
secret = _coerce(item, raw)
|
||||
continue
|
||||
if _blank(raw):
|
||||
raw = current_settings.get(item.key)
|
||||
if _blank(raw):
|
||||
raw = item.default
|
||||
if _blank(raw):
|
||||
if item.required:
|
||||
raise unprocessable(f"{item.label} fehlt.")
|
||||
continue
|
||||
settings[item.key] = _coerce(item, raw)
|
||||
return settings, secret
|
||||
+155
-34
@@ -44,15 +44,47 @@ from app.sourceaccess import (
|
||||
release_for_external,
|
||||
)
|
||||
from app.sourceport import CAPABILITIES
|
||||
from app.sourceregistry import SourceModule, get_source, register_source
|
||||
from app.sourceregistry import ConfigField, SourceModule, get_source, parse_fields, register_source, require_source
|
||||
|
||||
|
||||
_PUBLIC_COLUMNS = """
|
||||
id, kind, name, settings, created_at,
|
||||
secret <> '' AS has_secret,
|
||||
last_probe_at, last_probe_ok, last_probe_detail
|
||||
"""
|
||||
|
||||
|
||||
def parse_connector(
|
||||
kind: str,
|
||||
name: str,
|
||||
flat: dict,
|
||||
config: dict | None,
|
||||
current: dict | None = None,
|
||||
) -> tuple[str, dict, str]:
|
||||
"""Validate a connector body with the registered module. ``current`` is the stored row on change."""
|
||||
module = require_source(kind)
|
||||
name = " ".join(str(name or "").split())
|
||||
values = {key: value for key, value in flat.items() if value is not None}
|
||||
values.update(config or {})
|
||||
if module.config_fields:
|
||||
settings, secret = parse_fields(
|
||||
module.config_fields,
|
||||
values,
|
||||
current["settings"] if current else None,
|
||||
current["secret"] if current else "",
|
||||
)
|
||||
if module.normalize_config is not None:
|
||||
settings = module.normalize_config(settings, secret)
|
||||
return name, settings, secret
|
||||
return module.parse_config({**values, "kind": kind, "name": name, "config": config or {}})
|
||||
|
||||
|
||||
def save_connector(conn, owner_id: UUID, kind: str, name: str, settings: dict, secret: str) -> dict:
|
||||
row = conn.execute(
|
||||
"""
|
||||
f"""
|
||||
INSERT INTO connectors (owner_id, kind, name, settings, secret)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
RETURNING id, kind, name, settings, created_at
|
||||
RETURNING {_PUBLIC_COLUMNS}
|
||||
""",
|
||||
(owner_id, kind, name, Json(settings), secret),
|
||||
).fetchone()
|
||||
@@ -63,11 +95,12 @@ def update_connector(conn, owner_id: UUID, connector_id: UUID, name: str, settin
|
||||
"""Credential or name change on the same instance. The id does not move."""
|
||||
current = _owned(conn, owner_id, connector_id)
|
||||
row = conn.execute(
|
||||
"""
|
||||
f"""
|
||||
UPDATE connectors
|
||||
SET name = %s, settings = %s, secret = %s
|
||||
SET name = %s, settings = %s, secret = %s,
|
||||
last_probe_at = NULL, last_probe_ok = NULL, last_probe_detail = NULL
|
||||
WHERE id = %s AND owner_id = %s
|
||||
RETURNING id, kind, name, settings, created_at
|
||||
RETURNING {_PUBLIC_COLUMNS}
|
||||
""",
|
||||
(name, Json(settings), secret, current["id"], owner_id),
|
||||
).fetchone()
|
||||
@@ -76,8 +109,8 @@ def update_connector(conn, owner_id: UUID, connector_id: UUID, name: str, settin
|
||||
|
||||
def list_connectors(conn, owner_id: UUID) -> list[dict]:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, kind, name, settings, created_at
|
||||
f"""
|
||||
SELECT {_PUBLIC_COLUMNS}
|
||||
FROM connectors WHERE owner_id = %s
|
||||
ORDER BY kind, name
|
||||
""",
|
||||
@@ -86,6 +119,30 @@ def list_connectors(conn, owner_id: UUID) -> list[dict]:
|
||||
return [_public(row) for row in rows]
|
||||
|
||||
|
||||
def connector_detail(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
"""One own instance with its use. The secret itself is never part of it."""
|
||||
row = conn.execute(
|
||||
f"SELECT {_PUBLIC_COLUMNS} FROM connectors WHERE id = %s AND owner_id = %s",
|
||||
(connector_id, owner_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
used = conn.execute(
|
||||
"""
|
||||
SELECT count(*) AS total
|
||||
FROM external_references er
|
||||
JOIN elements e ON e.id = er.element_id
|
||||
WHERE (er.connector_id = %s OR er.detail ->> 'connector_id' = %s)
|
||||
AND e.deleted_at IS NULL AND e.owner_subject_id = %s
|
||||
""",
|
||||
(connector_id, str(connector_id), owner_id),
|
||||
).fetchone()
|
||||
body = _public(row)
|
||||
body["references"] = int(used["total"])
|
||||
body["protection_profile"] = "legacy-plaintext"
|
||||
return body
|
||||
|
||||
|
||||
def delete_connector(conn, owner_id: UUID, connector_id: UUID) -> None:
|
||||
row = conn.execute(
|
||||
"DELETE FROM connectors WHERE id = %s AND owner_id = %s RETURNING id",
|
||||
@@ -101,11 +158,30 @@ def probe(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
if module is None:
|
||||
raise unprocessable("Diese Quelle ist nicht registriert.")
|
||||
release_for_external(conn)
|
||||
result = module.probe(row)
|
||||
try:
|
||||
result = module.probe(row)
|
||||
except Problem as exc:
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
_record_probe(conn, row["id"], False, exc.detail)
|
||||
conn.commit()
|
||||
raise
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
_record_probe(conn, row["id"], bool(result.get("ok", True)), str(result.get("detail") or ""))
|
||||
return result
|
||||
|
||||
|
||||
def _record_probe(conn, connector_id: UUID, ok: bool, detail: str) -> None:
|
||||
"""Display only. The detail comes from the module and must not carry a secret."""
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE connectors
|
||||
SET last_probe_at = now(), last_probe_ok = %s, last_probe_detail = %s
|
||||
WHERE id = %s
|
||||
""",
|
||||
(ok, detail[:300] or None, connector_id),
|
||||
)
|
||||
|
||||
|
||||
def _paperless_choice(value, label: str) -> str | None:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
@@ -325,6 +401,11 @@ def _owned(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
return row
|
||||
|
||||
|
||||
def owned_connector(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
"""The stored row of an own instance, secret included. For server-side use only."""
|
||||
return _owned(conn, owner_id, connector_id)
|
||||
|
||||
|
||||
def _public(row) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
@@ -333,6 +414,14 @@ def _public(row) -> dict:
|
||||
"settings": row["settings"],
|
||||
"created_at": row["created_at"].strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"capabilities": (get_source(row["kind"]).capabilities if get_source(row["kind"]) else None),
|
||||
"has_secret": bool(row["has_secret"]),
|
||||
"probe": {
|
||||
"at": row["last_probe_at"].strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"ok": bool(row["last_probe_ok"]),
|
||||
"detail": row["last_probe_detail"] or "",
|
||||
}
|
||||
if row["last_probe_at"]
|
||||
else None,
|
||||
}
|
||||
|
||||
|
||||
@@ -1456,32 +1545,62 @@ def _search_rfc822(client: imaplib.IMAP4, expected_id: str, max_bytes: int) -> l
|
||||
return found
|
||||
|
||||
|
||||
def _paperless_config(payload: dict) -> tuple[str, dict, str]:
|
||||
base_url = str(payload.get("base_url") or "").strip()
|
||||
token = str(payload.get("token") or "").strip()
|
||||
name = str(payload.get("name") or "").strip()
|
||||
if not base_url or len(token) < 8:
|
||||
raise unprocessable("Paperless braucht eine Adresse und einen API-Schlüssel.")
|
||||
return name, {"base_url": clean_base_url(base_url)}, token
|
||||
PAPERLESS_FIELDS = (
|
||||
ConfigField(
|
||||
"base_url",
|
||||
"Adresse",
|
||||
type="url",
|
||||
required=True,
|
||||
placeholder="https://paperless.example",
|
||||
help="Adresse deiner Paperless-Instanz ohne Pfad.",
|
||||
max_length=300,
|
||||
),
|
||||
ConfigField(
|
||||
"token",
|
||||
"API-Schlüssel",
|
||||
type="password",
|
||||
required=True,
|
||||
secret=True,
|
||||
help="Den Schlüssel erzeugst du in Paperless unter deinem Profil.",
|
||||
max_length=200,
|
||||
),
|
||||
)
|
||||
|
||||
IMAP_FIELDS = (
|
||||
ConfigField("host", "Server", required=True, placeholder="imap.example", max_length=200),
|
||||
ConfigField("port", "Port", type="number", required=True, default=993, minimum=1, maximum=65535),
|
||||
ConfigField(
|
||||
"security",
|
||||
"Verbindung",
|
||||
type="select",
|
||||
required=True,
|
||||
default="ssl",
|
||||
options=(("ssl", "SSL"), ("starttls", "STARTTLS")),
|
||||
),
|
||||
ConfigField("username", "Benutzername", required=True, max_length=200),
|
||||
ConfigField("password", "Passwort", type="password", required=True, secret=True, max_length=500),
|
||||
ConfigField(
|
||||
"mailbox",
|
||||
"Standardfach",
|
||||
default="INBOX",
|
||||
help="Dieses Fach öffnet die Auswahl zuerst.",
|
||||
max_length=200,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _imap_config(payload: dict) -> tuple[str, dict, str]:
|
||||
host = str(payload.get("host") or "").strip()
|
||||
username = str(payload.get("username") or "").strip()
|
||||
password = str(payload.get("password") or "")
|
||||
name = str(payload.get("name") or "").strip()
|
||||
if not host or not username or not password:
|
||||
raise unprocessable("Ein E-Mail-Konto braucht Server, Benutzername und Passwort.")
|
||||
if " " in host or len(host) > 200:
|
||||
def _paperless_normalize(settings: dict, secret: str) -> dict:
|
||||
if len(secret.strip()) < 8:
|
||||
raise unprocessable("Der API-Schlüssel ist zu kurz.")
|
||||
return {**settings, "base_url": clean_base_url(settings["base_url"])}
|
||||
|
||||
|
||||
def _imap_normalize(settings: dict, secret: str) -> dict:
|
||||
del secret
|
||||
if " " in settings["host"]:
|
||||
raise unprocessable("Der Mailserver ist ungültig.")
|
||||
port = payload.get("port") or 993
|
||||
return name, {
|
||||
"host": host,
|
||||
"port": int(port),
|
||||
"username": username,
|
||||
"mailbox": str(payload.get("mailbox") or "INBOX").strip() or "INBOX",
|
||||
"security": payload.get("security") or "ssl",
|
||||
}, password
|
||||
_check_mailbox(settings.get("mailbox") or "INBOX")
|
||||
return settings
|
||||
|
||||
|
||||
def _paperless_find(conn, owner_id: UUID, connector_id: UUID | None, query: str, options: dict) -> dict:
|
||||
@@ -1624,7 +1743,8 @@ def _install_sources() -> None:
|
||||
label="Paperless",
|
||||
representation="pdf",
|
||||
capabilities=CAPABILITIES["paperless"],
|
||||
parse_config=_paperless_config,
|
||||
config_fields=PAPERLESS_FIELDS,
|
||||
normalize_config=_paperless_normalize,
|
||||
probe=paperless.probe,
|
||||
find=paperless.find,
|
||||
open_original=read_linked_paperless,
|
||||
@@ -1638,7 +1758,8 @@ def _install_sources() -> None:
|
||||
label="E-Mail",
|
||||
representation="email",
|
||||
capabilities=CAPABILITIES["imap"],
|
||||
parse_config=_imap_config,
|
||||
config_fields=IMAP_FIELDS,
|
||||
normalize_config=_imap_normalize,
|
||||
probe=imap.probe,
|
||||
find=imap.find,
|
||||
open_original=_imap_bytes,
|
||||
|
||||
Reference in new issue
Block a user