diff --git a/smb-connector/.env.example b/smb-connector/.env.example index da782a3..50030e1 100644 --- a/smb-connector/.env.example +++ b/smb-connector/.env.example @@ -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. # ───────────────────────────────────────────────────────────────────────────── @@ -21,18 +21,38 @@ INCLUDE_EXTENSIONS=pdf,docx,txt,md,csv,xlsx,html,htm MAX_FILE_MB=50 LOG_LEVEL=INFO -# ── Sources ────────────────────────────────────────────────────────────────── -# Preferred: one JSON list (scales to any number of shares, no code changes). -# 'mount' = local path where the SMB share is mounted (read-only) in this container -# 'logical' = the path users recognise; shown in Onyx's citation view -# '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"}] +# ── Sources: SMB mounted INSIDE the container (recommended) ────────────────── +# Give each share its SMB connection details and the connector mounts it itself +# (CIFS) — no host mount required. Uses the indexed style so each source can carry +# credentials. Requires the cap_add + apparmor lines in docker-compose.smb.yml. +# +# _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_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_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"}] diff --git a/smb-connector/Dockerfile b/smb-connector/Dockerfile index e9a657e..49f9687 100644 --- a/smb-connector/Dockerfile +++ b/smb-connector/Dockerfile @@ -1,17 +1,26 @@ -# AIDO SMB → Onyx sync connector (Phase 1) +# AIDO SMB → Onyx sync connector FROM python:3.12-slim 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 COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt 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 -USER runner VOLUME ["/app/state"] -CMD ["python", "-m", "app.main"] +# Root entrypoint mounts CIFS shares, then execs the app via gosu as `runner`. +ENTRYPOINT ["/entrypoint.sh"] diff --git a/smb-connector/README.md b/smb-connector/README.md index 4480291..eaefb15 100644 --- a/smb-connector/README.md +++ b/smb-connector/README.md @@ -44,14 +44,39 @@ cp .env.example .env # 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` -path used in `SMB_SOURCES` (see `docker-compose.smb.yml`). Then: +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 | @@ -61,6 +86,8 @@ docker compose logs -f smb_connector | `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__NAME/_MOUNT/_LOGICAL/_CC_PAIR_ID` | Indexed alternative to `SMB_SOURCES` | +| `SMB_SOURCE__UNC` (or `_SERVER`+`_SHARE`) | In-container CIFS mount: the share to mount at `_MOUNT` | +| `SMB_SOURCE__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) | diff --git a/smb-connector/docker-compose.smb.yml b/smb-connector/docker-compose.smb.yml index cfb49d7..140897c 100644 --- a/smb-connector/docker-compose.smb.yml +++ b/smb-connector/docker-compose.smb.yml @@ -1,11 +1,12 @@ # 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 # -# Host-side SMB mount example (Linux): -# sudo mount -t cifs //server/finance /mnt/smb/finance -o ro,username=svc,password=***,uid=10001 -# then bind-mount /mnt/smb/finance -> /shares/finance below. +# The connector mounts the SMB/CIFS shares INSIDE the container (default mode) — +# no host mount needed. Just give it the share + credentials via env (see +# 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: smb_connector: build: ./smb-connector @@ -20,12 +21,24 @@ services: depends_on: api_server: 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: # Persist the sync manifest + cached api-key/cc_pair (survives restarts). - ./smb-connector/state:/app/state - # Mount each SMB share read-only at the path used in SMB_SOURCES: - - /mnt/smb/finance:/shares/finance:ro - - /mnt/smb/legal:/shares/legal:ro + # NOTE: no host bind-mounts of the shares are needed in in-container mode. + # (Legacy host-mount mode still works: bind-mount the 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: diff --git a/smb-connector/entrypoint.sh b/smb-connector/entrypoint.sh new file mode 100644 index 0000000..cf6f706 --- /dev/null +++ b/smb-connector/entrypoint.sh @@ -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__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__NAME label (also used by the app) +# SMB_SOURCE__MOUNT where to mount inside the container (e.g. /mnt/smb/finance) +# SMB_SOURCE__UNC //server/share (or use _SERVER + _SHARE) +# SMB_SOURCE__SERVER server host/ip \_ combined into //server/share +# SMB_SOURCE__SHARE share name / +# SMB_SOURCE__USERNAME CIFS username (omit for guest) +# SMB_SOURCE__PASSWORD CIFS password +# SMB_SOURCE__DOMAIN AD domain/workgroup (optional) +# SMB_SOURCE__VERS SMB dialect, default 3.0 +# SMB_SOURCE__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