SMB connector: per-connector group access + standalone Docker mode

1. Optional Onyx group per connector. Set SMB_SOURCE_<N>_GROUPS (or
   "groups" in SMB_SOURCES JSON, or SMB_DEFAULT_GROUP_IDS as a fallback)
   to a list of Onyx user-group ids. The connector is then created
   PRIVATE and restricted to those groups; empty stays PUBLIC. Applied
   only at connector creation, as requested. Needs the Business licence.

2. Standalone deployment. New docker-compose.standalone.yml runs the
   connector as its own Compose project next to Onyx (no -f injection),
   joining Onyx's network (onyx_default, override via ONYX_NETWORK) to
   reach the API by service name. Suits Compose management tools that
   cannot handle multiple injected compose files. The legacy injected
   compose still works.

Verified: group-id parsing across all three source styles, the
private+groups vs public payload, standalone compose config, and the
full index/sync simulation (still green).
This commit is contained in:
Naeem Dosh 2026-09-24 14:20:39 +05:00
parent 9a886ac835
commit 26d3262cfb
5 changed files with 125 additions and 6 deletions

View file

@ -44,7 +44,23 @@ cp .env.example .env
# define your shares in SMB_SOURCES (JSON) or SMB_SOURCE_1_* vars
```
Then bring it up (same `onyx-network`):
Then bring it up. Two deployment modes:
**A. Standalone (recommended) — its own Compose project next to Onyx.**
No `-f` injection, so a Compose management tool can handle it on its own. It
joins Onyx's network to reach the API by service name.
```bash
cd smb-connector
docker compose -f docker-compose.standalone.yml up -d --build
docker compose -f docker-compose.standalone.yml logs -f
```
Onyx's compose has `name: onyx`, so its network is `onyx_default` (the default).
If your Onyx project uses another name, set `ONYX_NETWORK=<project>_default`.
**B. Injected into the Onyx compose (legacy).** Adds the service to the Onyx
project via a second compose file:
```bash
docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d --build smb_connector
@ -92,6 +108,9 @@ CIFS-mounted a second time — `_MOUNT` then just points at the bind-mounted pat
| `INCLUDE_EXTENSIONS` | File types to ingest (default pdf,docx,txt,md,csv,xlsx,html) |
| `MAX_FILE_MB` | Skip files larger than this (default 50) |
| `SMB_WRITE_INDEX` | Write a per-source index (table-of-contents) document listing every file, so the assistant is aware of the full set (default on; `0`/`false` to disable) |
| `SMB_SOURCE_<N>_GROUPS` | Onyx user-group ids (comma-separated) allowed to access this connector. Set → connector created **private** and restricted to those groups; empty → **public**. Applied only at connector creation. Needs the Onyx Business licence. In `SMB_SOURCES` JSON use `"groups": [id, …]`. |
| `SMB_DEFAULT_GROUP_IDS` | Fallback group ids for any source that has none set |
| `ONYX_NETWORK` | Standalone mode only: name of Onyx's Docker network (default `onyx_default`) |
State (manifest + cached key/cc_pair) lives in the mounted `/app/state` volume.

View file

@ -37,6 +37,11 @@ class SourceConfig:
# Optional explicit connector/credential pair id. When omitted the connector
# is auto-provisioned (idempotently) and the id cached in the state file.
cc_pair_id: int | None = None
# Onyx user-group ids that may access this connector. When set, the connector
# is created PRIVATE and restricted to these groups; empty means PUBLIC.
# Applied only when the connector is first provisioned (not on later runs).
# Group permissions need the Onyx Business licence.
group_ids: list[int] = field(default_factory=list)
def normalized_logical(self) -> str:
# Trim trailing separators so path joins are clean.
@ -73,7 +78,28 @@ def _split_ext(value: str) -> set[str]:
}
def _parse_group_ids(value) -> list[int]:
"""Accept a JSON list, a comma-separated string, or a single id."""
if value is None or value == "":
return []
if isinstance(value, (list, tuple)):
items = value
else:
items = str(value).split(",")
out: list[int] = []
for item in items:
s = str(item).strip()
if not s:
continue
try:
out.append(int(s))
except ValueError as exc:
raise SystemExit(f"Invalid Onyx group id '{s}': {exc}") from exc
return out
def _load_sources() -> list[SourceConfig]:
default_groups = _parse_group_ids(os.environ.get("SMB_DEFAULT_GROUP_IDS"))
raw = os.environ.get("SMB_SOURCES", "").strip()
if raw:
try:
@ -90,6 +116,7 @@ def _load_sources() -> list[SourceConfig]:
mount=str(item["mount"]),
logical=str(item.get("logical") or item["mount"]),
cc_pair_id=(int(item["cc_pair_id"]) if item.get("cc_pair_id") else None),
group_ids=_parse_group_ids(item.get("groups")) or default_groups,
)
)
return sources
@ -109,6 +136,8 @@ def _load_sources() -> list[SourceConfig]:
mount=mount,
logical=os.environ.get(f"SMB_SOURCE_{i}_LOGICAL") or mount,
cc_pair_id=int(cc) if cc else None,
group_ids=_parse_group_ids(os.environ.get(f"SMB_SOURCE_{i}_GROUPS"))
or default_groups,
)
)
i += 1
@ -123,6 +152,7 @@ def _load_sources() -> list[SourceConfig]:
cc_pair_id=(
int(os.environ["CC_PAIR_ID"]) if os.environ.get("CC_PAIR_ID") else None
),
group_ids=_parse_group_ids(os.environ.get("SMB_GROUPS")) or default_groups,
)
)
return sources

View file

@ -151,15 +151,20 @@ class OnyxClient:
return int(row["cc_pair_id"])
return None
def provision_cc_pair(self, name: str) -> int:
def provision_cc_pair(self, name: str, group_ids: list[int] | None = None) -> int:
"""Create an ingestion-API connector + mock credential + cc_pair. Returns
the cc_pair id. Idempotent: reuses an existing one with the same name."""
the cc_pair id. Idempotent: reuses an existing one with the same name.
When ``group_ids`` is given, the connector is created PRIVATE and
restricted to those Onyx user groups (needs the Business licence).
Access is set only at creation; an existing connector is reused as-is."""
existing = self.find_cc_pair_id_by_name(name)
if existing:
logger.info("Reusing existing connector '%s' (cc_pair_id=%s)", name, existing)
self.set_cc_pair_active(existing)
return existing
groups = group_ids or []
connector = {
"name": name,
"source": "ingestion_api",
@ -168,9 +173,11 @@ class OnyxClient:
"refresh_freq": None,
"prune_freq": None,
"indexing_start": None,
"access_type": "public",
"groups": [],
"access_type": "private" if groups else "public",
"groups": groups,
}
if groups:
logger.info("Creating connector '%s' private, restricted to groups %s", name, groups)
resp = self._request(
"POST", "/manage/admin/connector-with-mock-credential", json=connector,
auth=self._admin_auth,

View file

@ -175,7 +175,10 @@ def sync_source(client: OnyxClient, cfg: Config, source: SourceConfig, state: St
ss = state.source(source.name)
cc_pair_id = source.cc_pair_id or ss.cc_pair_id
if not cc_pair_id:
cc_pair_id = client.provision_cc_pair(f"{cfg.connector_name_prefix} - {source.name}")
cc_pair_id = client.provision_cc_pair(
f"{cfg.connector_name_prefix} - {source.name}",
group_ids=source.group_ids,
)
ss.cc_pair_id = cc_pair_id
state.save()

View file

@ -0,0 +1,60 @@
# Standalone SMB connector — its own Compose project, next to Onyx.
#
# Run it on its own (no `-f` injection into the Onyx compose):
#
# cd smb-connector
# docker compose -f docker-compose.standalone.yml up -d --build
#
# It reaches Onyx over Onyx's existing Docker network. Onyx's compose has
# `name: onyx` and no custom network, so its network is `onyx_default`. If your
# Onyx project uses a different name, set ONYX_NETWORK to <project>_default.
# The connector talks to the API by service name (http://api_server:8080).
#
# In-container CIFS mounting works the same as before: give the share +
# credentials via env (see .env.example). The two capabilities and the
# unconfined apparmor profile below are required for mount.cifs in a container.
name: aido-smb-connector
services:
smb_connector:
build: .
image: livtec/aido-smb-connector:local
container_name: aido-smb-connector
restart: unless-stopped
environment:
# Reach the Onyx API over the shared network (override if needed).
- ONYX_API_URL=${ONYX_API_URL:-http://api_server:8080}
env_file:
- path: .env
required: false
networks:
- onyx_net
# --- required to mount SMB/CIFS shares inside the container ---------------
cap_add:
- SYS_ADMIN
- DAC_READ_SEARCH
security_opt:
- apparmor:unconfined
# If your host's Docker still refuses the mount, use privileged mode instead
# of the two cap_add lines above:
# privileged: true
volumes:
# Persist the sync manifest + cached api-key/cc_pair (survives restarts).
- ./state:/app/state
# Legacy host-mount mode still works: bind-mount a share here and OMIT the
# SMB_SOURCE_*_SERVER/_UNC env for that source, e.g.
# - /mnt/smb/finance:/mnt/smb/finance:ro
logging:
driver: json-file
options:
max-size: "50m"
max-file: "6"
# Onyx's network, created by the Onyx compose project. Joined as external so
# this stays a separate Compose project. Override the name with ONYX_NETWORK.
networks:
onyx_net:
external: true
name: ${ONYX_NETWORK:-onyx_default}