External Connectors
Last verified against: commit 6f2f20b (v1.0)
Estimated Read Time: 6 min
Status: built
Locus Connectors synchronize tables from relational databases into Knowledge Spaces using read-only database roles, cryptographic credential storage, and immutable snapshot versioning.
1. Credentials Vault (Envelope Encryption)
Connector passwords and connection strings are stored using envelope encryption:
- Cipher:
AES-256-GCMwith a unique96-bit (12-byte) initialization vector per write operation. - Master Key (
LOCUS_MASTER_KEYS): Stored in environment variables, never written to disk or the database. - Tenant Data Keys: Stored in table
tenant_keys, encrypted by the active master key. - Additional Authenticated Data (AAD): Ciphertext is cryptographically bound to
f"{tenant_id}|{connector_id}|{purpose}". Ciphertext copied to another tenant or connector fails decryption. - Write-Only Secrets: The API never returns decrypted credentials. It returns only a SHA-256 fingerprint slice and the last 4 characters for identification.
2. Least-Privilege Read-Only Role Setup
Locus requires a dedicated read-only role on the source database. The connector inspects granted permissions prior to running sync operations and rejects connections if the user has INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, or administrative privileges.
Execute the following script in PostgreSQL to create a dedicated read-only user:
-- Step 1: Create dedicated user with strong password CREATE USER locus_readonly WITH PASSWORD 'locus_secret_password_REPLACE_ME'; -- Step 2: Grant read-only access to database and schema GRANT CONNECT ON DATABASE my_production_db TO locus_readonly; GRANT USAGE ON SCHEMA public TO locus_readonly; -- Step 3: Grant SELECT-only on specific tables GRANT SELECT ON TABLE public.products TO locus_readonly; GRANT SELECT ON TABLE public.orders TO locus_readonly; -- Step 4: Revoke write capabilities REVOKE INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON ALL TABLES IN SCHEMA public FROM locus_readonly;
3. Network and SSRF Protection
Before establishing a socket connection, Locus resolves hostnames and evaluates IP ranges against SSRF defenses:
- Localhost and Loopback:
127.0.0.0/8,::1are blocked. - Private Subnets: RFC 1918 ranges (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16) are blocked unless running in local dev mode. - Link-Local / Cloud Metadata:
169.254.169.254(AWS/GCP metadata) and carrier CGNAT blocks are blocked. - DNS Rebinding Prevention: Resolved IP addresses are pinned for the lifecycle of the connection.
4. Snapshot Synchronization Semantics
- Chunked Cursor Traversal: Data is read through server-side cursors in fixed batches (e.g., 500 rows per chunk) to avoid memory spikes.
- Deterministic Primary Key Tracking: A composite SHA-256 hash is computed for each row based on its primary key and column values.
- Granular Diff Calculation: During each sync, Locus computes diff statistics:
added: New primary keys detected.changed: Existing primary keys with altered row hash.removed: Primary keys missing upstream (markedexcludedin new version).unchanged: Identical records.
- Immutable Versions: Prior versions are never mutated; every sync produces a new
SourceVersionrecord with anas_oftimestamp.
5. Testing Connector via API
To test connection parameters programmatically:
# Run in terminal curl -X POST http://127.0.0.1:8000/connectors/test \ -H "Content-Type: application/json" \ -H "Authorization: Bearer locus_session_REPLACE_ME" \ -d '{ "type": "postgres", "config": { "host": "db.example.internal", "port": 5432, "database": "analytics", "user": "locus_readonly", "sslmode": "require" }, "secret": "locus_secret_password_REPLACE_ME" }'