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>
This commit is contained in:
Naeem Dosh 2026-09-08 23:14:46 +05:00
parent 909a541c6b
commit afac2c42ae
5 changed files with 200 additions and 26 deletions

View file

@ -1,5 +1,5 @@
# ───────────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────────
# AIDO SMB → Onyx sync connector (Phase 1) # AIDO SMB → Onyx sync connector
# Copy to .env and adjust. The connector runs as a sidecar next to Onyx. # Copy to .env and adjust. The connector runs as a sidecar next to Onyx.
# ───────────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────────
@ -21,18 +21,38 @@ INCLUDE_EXTENSIONS=pdf,docx,txt,md,csv,xlsx,html,htm
MAX_FILE_MB=50 MAX_FILE_MB=50
LOG_LEVEL=INFO LOG_LEVEL=INFO
# ── Sources ────────────────────────────────────────────────────────────────── # ── Sources: SMB mounted INSIDE the container (recommended) ──────────────────
# Preferred: one JSON list (scales to any number of shares, no code changes). # Give each share its SMB connection details and the connector mounts it itself
# 'mount' = local path where the SMB share is mounted (read-only) in this container # (CIFS) — no host mount required. Uses the indexed style so each source can carry
# 'logical' = the path users recognise; shown in Onyx's citation view # credentials. Requires the cap_add + apparmor lines in docker-compose.smb.yml.
# 'cc_pair_id' (optional) = use a specific connector; auto-provisioned if omitted. #
SMB_SOURCES=[{"name":"Finance","mount":"/shares/finance","logical":"\\\\company\\finance"},{"name":"Legal","mount":"/shares/legal","logical":"\\\\company\\legal"}] # _MOUNT = where to mount it inside the container (any path)
# _UNC = //server/share (or set _SERVER + _SHARE)
# _LOGICAL = the path users recognise; shown in Onyx's citation view
# _USERNAME/_PASSWORD/_DOMAIN = CIFS credentials (omit user for guest)
# _VERS = SMB dialect, default 3.0
# _CC_PAIR_ID (optional) = use a specific connector; auto-provisioned if omitted
SMB_SOURCE_1_NAME=Finance
SMB_SOURCE_1_UNC=//nas.company.local/finance
SMB_SOURCE_1_MOUNT=/mnt/smb/finance
SMB_SOURCE_1_LOGICAL=\\company\finance
SMB_SOURCE_1_USERNAME=svc_onyx
SMB_SOURCE_1_PASSWORD=change-me
SMB_SOURCE_1_DOMAIN=COMPANY
# SMB_SOURCE_1_VERS=3.0
# …or the indexed style instead of SMB_SOURCES:
# SMB_SOURCE_1_NAME=Finance
# SMB_SOURCE_1_MOUNT=/shares/finance
# SMB_SOURCE_1_LOGICAL=\\company\finance
# SMB_SOURCE_1_CC_PAIR_ID=
# SMB_SOURCE_2_NAME=Legal # SMB_SOURCE_2_NAME=Legal
# SMB_SOURCE_2_MOUNT=/shares/legal # SMB_SOURCE_2_SERVER=nas.company.local
# SMB_SOURCE_2_SHARE=legal
# SMB_SOURCE_2_MOUNT=/mnt/smb/legal
# SMB_SOURCE_2_LOGICAL=\\company\legal # SMB_SOURCE_2_LOGICAL=\\company\legal
# SMB_SOURCE_2_USERNAME=svc_onyx
# SMB_SOURCE_2_PASSWORD=change-me
# SMB_SOURCE_2_DOMAIN=COMPANY
# ── Legacy: host-mounted shares (still supported) ────────────────────────────
# If you'd rather mount on the host and bind-mount into the container, OMIT the
# _UNC/_SERVER lines for that source (so it isn't CIFS-mounted here) and point
# _MOUNT at the bind-mounted path. You can also use the JSON form:
# SMB_SOURCES=[{"name":"Finance","mount":"/mnt/smb/finance","logical":"\\\\company\\finance"}]

View file

@ -1,17 +1,26 @@
# AIDO SMB → Onyx sync connector (Phase 1) # AIDO SMB → Onyx sync connector
FROM python:3.12-slim FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
# cifs-utils: mount SMB/CIFS shares inside the container (no host mount needed).
# gosu: drop from root (needed for mount) to the unprivileged app user at runtime.
RUN apt-get update \
&& apt-get install -y --no-install-recommends cifs-utils gosu \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app WORKDIR /app
COPY requirements.txt ./ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
COPY app/ ./app/ COPY app/ ./app/
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Non-root; state dir is a mounted volume so the manifest survives restarts. # App runs as this unprivileged user (entrypoint mounts as root, then drops to it).
# State dir is a mounted volume so the manifest survives restarts.
RUN useradd -u 10001 -m runner && mkdir -p /app/state && chown -R runner /app/state RUN useradd -u 10001 -m runner && mkdir -p /app/state && chown -R runner /app/state
USER runner
VOLUME ["/app/state"] VOLUME ["/app/state"]
CMD ["python", "-m", "app.main"] # Root entrypoint mounts CIFS shares, then execs the app via gosu as `runner`.
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -44,14 +44,39 @@ cp .env.example .env
# define your shares in SMB_SOURCES (JSON) or SMB_SOURCE_1_* vars # define your shares in SMB_SOURCES (JSON) or SMB_SOURCE_1_* vars
``` ```
Mount each SMB share read-only on the host, then bind-mount it at the `mount` Then bring it up (same `onyx-network`):
path used in `SMB_SOURCES` (see `docker-compose.smb.yml`). Then:
```bash ```bash
docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d --build smb_connector docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d --build smb_connector
docker compose logs -f 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 ## Configuration
| Var | Purpose | | Var | Purpose |
@ -61,6 +86,8 @@ docker compose logs -f smb_connector
| `ONYX_ADMIN_EMAIL` / `ONYX_ADMIN_PASSWORD` | Bootstrap creds to auto-create the key + connectors | | `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_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>_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) | | `INGESTION_INTERVAL_SECONDS` | Reconcile interval (default 300) |
| `INCLUDE_EXTENSIONS` | File types to ingest (default pdf,docx,txt,md,csv,xlsx,html) | | `INCLUDE_EXTENSIONS` | File types to ingest (default pdf,docx,txt,md,csv,xlsx,html) |
| `MAX_FILE_MB` | Skip files larger than this (default 50) | | `MAX_FILE_MB` | Skip files larger than this (default 50) |

View file

@ -1,11 +1,12 @@
# Add this service to your Onyx docker-compose (same `onyx-network`). # Add this service to your Onyx docker-compose (same `onyx-network`).
# Mount each SMB share read-only at the `mount` path referenced in .env / SMB_SOURCES.
# #
# docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d smb_connector # docker compose -f docker-compose.yml -f smb-connector/docker-compose.smb.yml up -d smb_connector
# #
# Host-side SMB mount example (Linux): # The connector mounts the SMB/CIFS shares INSIDE the container (default mode) —
# sudo mount -t cifs //server/finance /mnt/smb/finance -o ro,username=svc,password=***,uid=10001 # no host mount needed. Just give it the share + credentials via env (see
# then bind-mount /mnt/smb/finance -> /shares/finance below. # smb-connector/.env.example). For that, the container needs the two capabilities
# and the unconfined apparmor profile below (required for mount.cifs in a
# container; the host also needs the `cifs` kernel module, which is standard).
services: services:
smb_connector: smb_connector:
build: ./smb-connector build: ./smb-connector
@ -20,12 +21,24 @@ services:
depends_on: depends_on:
api_server: api_server:
condition: service_healthy condition: service_healthy
# --- 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: volumes:
# Persist the sync manifest + cached api-key/cc_pair (survives restarts). # Persist the sync manifest + cached api-key/cc_pair (survives restarts).
- ./smb-connector/state:/app/state - ./smb-connector/state:/app/state
# Mount each SMB share read-only at the path used in SMB_SOURCES: # NOTE: no host bind-mounts of the shares are needed in in-container mode.
- /mnt/smb/finance:/shares/finance:ro # (Legacy host-mount mode still works: bind-mount the share here and OMIT
- /mnt/smb/legal:/shares/legal:ro # the SMB_SOURCE_*_SERVER/_UNC env for that source, e.g.
# - /mnt/smb/finance:/mnt/smb/finance:ro )
logging: logging:
driver: json-file driver: json-file
options: options:

105
smb-connector/entrypoint.sh Normal file
View file

@ -0,0 +1,105 @@
#!/bin/sh
# ─────────────────────────────────────────────────────────────────────────────
# AIDO SMB connector entrypoint.
#
# Mounts each configured SMB/CIFS share *inside the container* (no host mount
# needed), then drops privileges and runs the sync app. Mounting is opt-in per
# source: a source is CIFS-mounted only if it provides SMB connection details
# (SMB_SOURCE_<N>_UNC or _SERVER + _SHARE). Sources without them are assumed to
# be bind-mounted from the host at their _MOUNT path (the original Phase-1 mode),
# so both styles work side by side.
#
# Per-source env (indexed style):
# SMB_SOURCE_<N>_NAME label (also used by the app)
# SMB_SOURCE_<N>_MOUNT where to mount inside the container (e.g. /mnt/smb/finance)
# SMB_SOURCE_<N>_UNC //server/share (or use _SERVER + _SHARE)
# SMB_SOURCE_<N>_SERVER server host/ip \_ combined into //server/share
# SMB_SOURCE_<N>_SHARE share name /
# SMB_SOURCE_<N>_USERNAME CIFS username (omit for guest)
# SMB_SOURCE_<N>_PASSWORD CIFS password
# SMB_SOURCE_<N>_DOMAIN AD domain/workgroup (optional)
# SMB_SOURCE_<N>_VERS SMB dialect, default 3.0
# SMB_SOURCE_<N>_MOUNT_OPTS extra raw mount -o options (optional)
#
# Requires the container to have CAP_SYS_ADMIN + DAC_READ_SEARCH and an
# unconfined apparmor profile (see docker-compose.smb.yml).
# ─────────────────────────────────────────────────────────────────────────────
set -eu
APP_UID="${APP_UID:-10001}"
APP_GID="${APP_GID:-10001}"
MOUNTED=""
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) [entrypoint] $*"; }
cleanup() {
# Unmount in reverse so the app's file handles are released cleanly on stop.
for m in $MOUNTED; do
umount "$m" 2>/dev/null && log "unmounted $m" || log "warn: could not unmount $m"
done
}
trap cleanup EXIT INT TERM
mount_one() {
n="$1"
eval unc="\${SMB_SOURCE_${n}_UNC:-}"
eval server="\${SMB_SOURCE_${n}_SERVER:-}"
eval share="\${SMB_SOURCE_${n}_SHARE:-}"
eval mnt="\${SMB_SOURCE_${n}_MOUNT:-}"
eval user="\${SMB_SOURCE_${n}_USERNAME:-}"
eval pass="\${SMB_SOURCE_${n}_PASSWORD:-}"
eval domain="\${SMB_SOURCE_${n}_DOMAIN:-}"
eval vers="\${SMB_SOURCE_${n}_VERS:-3.0}"
eval extra="\${SMB_SOURCE_${n}_MOUNT_OPTS:-}"
# No SMB connection details → host-bind-mount mode; nothing to do here.
if [ -z "$unc" ] && [ -z "$server" ]; then
return 0
fi
[ -n "$unc" ] || unc="//${server}/${share}"
if [ -z "$mnt" ]; then
log "ERROR: SMB_SOURCE_${n}_MOUNT is required when mounting $unc inside the container"
exit 1
fi
mkdir -p "$mnt"
# Credentials via a 0600 file so the password never appears in the process list.
cred="/tmp/.smbcred_${n}"
{
[ -n "$user" ] && echo "username=$user"
[ -n "$pass" ] && echo "password=$pass"
[ -n "$domain" ] && echo "domain=$domain"
} > "$cred"
chmod 600 "$cred"
opts="ro,vers=${vers},uid=${APP_UID},gid=${APP_GID},file_mode=0440,dir_mode=0550,nounix,serverino"
if [ -n "$user" ]; then
opts="${opts},credentials=${cred}"
else
opts="${opts},guest"
fi
[ -n "$extra" ] && opts="${opts},${extra}"
log "mounting $unc -> $mnt (vers=${vers}${user:+, user=$user}${domain:+, domain=$domain})"
if mount -t cifs "$unc" "$mnt" -o "$opts"; then
MOUNTED="$mnt $MOUNTED"
rm -f "$cred"
else
rm -f "$cred"
log "ERROR: failed to mount $unc at $mnt — check credentials, host reachability, and that the container has CAP_SYS_ADMIN"
exit 1
fi
}
# Walk SMB_SOURCE_1.., mounting any that carry connection details.
i=1
while :; do
eval name="\${SMB_SOURCE_${i}_NAME:-}"
[ -n "$name" ] || break
mount_one "$i"
i=$((i + 1))
done
# Hand off to the sync app as the unprivileged runner.
log "starting sync app as uid=${APP_UID}"
exec gosu "${APP_UID}:${APP_GID}" python -m app.main