aido-onyx/smb-connector/README.md
Naeem Dosh afac2c42ae SMB connector: mount CIFS shares inside the container (like Lobehub)
Client asked to mount the SMB share in Docker rather than on the host. Added an
opt-in in-container CIFS mount:
- entrypoint.sh mounts each source's share (mount -t cifs) from env
  (SMB_SOURCE_<N>_UNC or _SERVER+_SHARE, _USERNAME/_PASSWORD/_DOMAIN/_VERS),
  read-only, credentials via a 0600 file (never in the process list), then drops
  from root to the unprivileged app user via gosu and execs the sync app;
  unmounts cleanly on stop.
- Dockerfile installs cifs-utils + gosu, runs the mount entrypoint.
- docker-compose.smb.yml adds cap_add [SYS_ADMIN, DAC_READ_SEARCH] +
  apparmor:unconfined (privileged fallback noted).
- .env.example + README document in-container mode; legacy host-bind-mount mode
  still works (omit _UNC/_SERVER for that source).

Entrypoint logic validated (POSIX sh check + mocked dry-run: correct mount args,
guest fallback, creds file, privilege drop). Live CIFS round-trip to be run
against a real SMB server; the sync engine is already E2E-verified.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-08 23:14:46 +05:00

112 lines
5.5 KiB
Markdown

# AIDO SMB → Onyx Connector (Phase 1)
Syncs files from one or more **SMB/CIFS shares** into Onyx via the public
**Ingestion API**, with full production behaviour the POC was missing:
- **Update & delete sync** — changed files are re-indexed in place; files removed
from a share are deleted from Onyx (reconciled every cycle).
- **Self-provisioning** — creates/reuses the Onyx **API key** and one **connector
(cc_pair)** per source automatically; idempotent across restarts.
- **Multi-source via Docker config** — any number of shares, defined purely in env.
- **Logical path mapping** — each document is labelled with the path users
recognise (`\\company\share\…`), mapped from the local mount via config.
It runs as a **sidecar container** and touches **no Onyx source code** — only the
public ingestion + admin APIs — so it survives Onyx upgrades.
> Phase 2 (separate) hides the broken file-preview for these SMB documents in the
> Onyx UI and shows the logical path instead, via the white-label overlay.
## How it works
```
SMB share ──(read-only mount)──▶ smb_connector ──ingestion API──▶ Onyx
│
manifest.json (path → document_id + content hash)
```
Each cycle, per source:
1. Walk the share, hash each eligible file.
2. **new / changed** (hash differs) → `POST /onyx-api/ingestion` (upsert by stable id).
3. **gone** (in manifest, not on disk) → `DELETE /onyx-api/ingestion/{id}`.
4. Reconcile against Onyx's own doc list so deletes still happen if the manifest
is lost.
Document ids are stable (`smb::<source>::<hash of relative path>`), so re-indexing
a changed file **updates** it rather than duplicating.
## Setup
```bash
cd smb-connector
cp .env.example .env
# set ONYX_API_KEY (or ONYX_ADMIN_EMAIL/PASSWORD to auto-create one)
# define your shares in SMB_SOURCES (JSON) or SMB_SOURCE_1_* vars
```
Then bring it up (same `onyx-network`):
```bash
docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d --build smb_connector
docker compose logs -f smb_connector
```
### SMB mount — two modes
**A. In-container mount (recommended, no host mount).** The connector mounts each
CIFS share itself; you just give it the share + credentials via env:
```
SMB_SOURCE_1_NAME=Finance
SMB_SOURCE_1_UNC=//nas.company.local/finance # or _SERVER + _SHARE
SMB_SOURCE_1_MOUNT=/mnt/smb/finance # where to mount it in the container
SMB_SOURCE_1_LOGICAL=\\company\finance # path shown in Onyx
SMB_SOURCE_1_USERNAME=svc_onyx
SMB_SOURCE_1_PASSWORD=… # kept in a 0600 creds file, never in the process list
SMB_SOURCE_1_DOMAIN=COMPANY # optional
# SMB_SOURCE_1_VERS=3.0 # SMB dialect, default 3.0
```
This needs two Linux capabilities on the container (already set in
`docker-compose.smb.yml`): `cap_add: [SYS_ADMIN, DAC_READ_SEARCH]` +
`security_opt: [apparmor:unconfined]` (or `privileged: true` if your host is
stricter). The host kernel must have the `cifs` module (standard on Linux). The
share is mounted **read-only** and unmounted cleanly on container stop.
**B. Host mount (legacy).** Mount the share on the host and bind-mount it into the
container, and **omit** the `_UNC`/`_SERVER` vars for that source so it isn't
CIFS-mounted a second time — `_MOUNT` then just points at the bind-mounted path.
## Configuration
| Var | Purpose |
|-----|---------|
| `ONYX_API_URL` | Onyx API base (e.g. `http://api_server:8080`) |
| `ONYX_API_KEY` | API key; if empty, auto-created from admin creds and cached |
| `ONYX_ADMIN_EMAIL` / `ONYX_ADMIN_PASSWORD` | Bootstrap creds to auto-create the key + connectors |
| `SMB_SOURCES` | JSON list of `{name, mount, logical, cc_pair_id?}` |
| `SMB_SOURCE_<N>_NAME/_MOUNT/_LOGICAL/_CC_PAIR_ID` | Indexed alternative to `SMB_SOURCES` |
| `SMB_SOURCE_<N>_UNC` (or `_SERVER`+`_SHARE`) | In-container CIFS mount: the share to mount at `_MOUNT` |
| `SMB_SOURCE_<N>_USERNAME/_PASSWORD/_DOMAIN/_VERS` | CIFS credentials + dialect (default `3.0`); omit user for guest |
| `INGESTION_INTERVAL_SECONDS` | Reconcile interval (default 300) |
| `INCLUDE_EXTENSIONS` | File types to ingest (default pdf,docx,txt,md,csv,xlsx,html) |
| `MAX_FILE_MB` | Skip files larger than this (default 50) |
State (manifest + cached key/cc_pair) lives in the mounted `/app/state` volume.
## Notes / scope
- **Extraction** is the light set (PDF, DOCX, XLSX, CSV, TXT/MD, HTML). Legacy
`.doc`/`.xls` and scanned-image PDFs are skipped (no OCR) — Phase-1 scope.
- **Auth for provisioning**: creating the API key/connectors needs one admin
credential the first time. At steady state only the cached API key is used.
- **API-key auto-creation requires `LICENSE_ENFORCEMENT_ENABLED=false`** on the Onyx
deployment (verified against v4.5.2): the `/admin/api-key` endpoint is EE-tier-gated
and returns HTTP 402 when enforcement is on (the default). CE/white-label deployments
run with it off, so this is fine — and if you'd rather not rely on it, just supply a
pre-made `ONYX_API_KEY`. Connector/cc_pair creation and the ingestion API itself are
**not** tier-gated and work on plain CE.
- **Document ids are URL-safe** (`smb_<source>_<hash>`), matching how Onyx stores/returns
them, so update and delete reconcile reliably (verified end-to-end).
- **`smb://` links** can't open from a browser; the logical path is stored for
display (Phase 2 surfaces it in the UI and hides the dead preview).