-
Notifications
You must be signed in to change notification settings - Fork 1
Harden Cloud Agent env secrets and email_records.is_read migration #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seonghobae
merged 12 commits into
fix/email-shared-send-rate-limit
from
cursor/bc-6ba03a0c-13df-4248-a679-e55574a1ec39-156e
Aug 20, 2026
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd23ca8
fix(db): make fresh-database schema bootstrap work end-to-end
cursoragent d8ef79b
fix(email-import): avoid NUL byte in Postgres advisory-lock key
cursoragent 4069017
chore(env): add Cloud Agent dev environment (backend + frontend + Pos…
cursoragent 0989f72
fix(env): keep Cloud Agent Postgres secrets off the psql command line
cursoragent 62c83b8
fix(db): guard email_records.is_read on the alembic path
cursoragent b070e85
docs: record Cloud Agent env contract and NUL advisory-lock anti-pattern
cursoragent 13b8aaa
test(cloud-agent): reject unpinned pip self-upgrade
seonghobae e44cf45
fix(cloud-agent): remove unpinned pip self-upgrade
seonghobae 1aadc63
security(cloud-agent): document fixed-argv subprocess boundary
seonghobae e2836ec
Merge branch 'develop' into cursor/bc-6ba03a0c-13df-4248-a679-e55574a…
opencode-agent[bot] 503f62d
fix(db): stack read-state migration after send buckets
seonghobae 8f6c841
Merge commit '3d08f5fe829e5111d6bd3e641e5272ad6ad74ccb' into codex/pr…
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "name": "Naruon dev (backend + frontend + Postgres/pgvector)", | ||
| "user": "ubuntu", | ||
| "install": "bash .cursor/install.sh", | ||
| "start": "bash .cursor/start.sh", | ||
| "terminals": [ | ||
| { | ||
| "name": "backend", | ||
| "command": "cd backend && . .venv/bin/activate && python scripts/start_backend.py --host 0.0.0.0 --port 8000" | ||
| }, | ||
| { | ||
| "name": "frontend", | ||
| "command": "cd frontend && corepack pnpm@11.5.3 dev --port 3000 --hostname 0.0.0.0" | ||
| } | ||
| ], | ||
| "ports": [ | ||
| { "name": "backend", "port": 8000 }, | ||
| { "name": "frontend", "port": 3000 } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env bash | ||
| # Idempotent repository bootstrap for Naruon Cloud Agent environments. | ||
| # | ||
| # Prepares durable, source-derived state after checkout: | ||
| # * system packages (PostgreSQL 16 + pgvector, Python venv/build tooling) | ||
| # * the backend virtualenv + pinned Python requirements | ||
| # * the frontend pnpm dependency tree | ||
| # | ||
| # Per-boot service startup (Postgres, schema migrations, dev secrets) lives in | ||
| # .cursor/start.sh so it re-runs on every VM boot, including builds. | ||
| set -euo pipefail | ||
|
|
||
| REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| cd "$REPO_ROOT" | ||
|
|
||
| echo "==> [install] system packages (postgresql-16, pgvector, python venv, build tools)" | ||
| export DEBIAN_FRONTEND=noninteractive | ||
| sudo apt-get update -qq | ||
| sudo apt-get install -y -qq \ | ||
| postgresql-16 \ | ||
| postgresql-16-pgvector \ | ||
| postgresql-client-16 \ | ||
| python3.12-venv \ | ||
| python3.12-dev \ | ||
| build-essential | ||
|
|
||
| echo "==> [install] backend virtualenv + requirements" | ||
| cd "$REPO_ROOT/backend" | ||
| if [ ! -x ".venv/bin/python" ]; then | ||
| python3 -m venv .venv | ||
| fi | ||
| # shellcheck disable=SC1091 | ||
| . .venv/bin/activate | ||
| python -m pip install --upgrade pip | ||
| python -m pip install --require-hashes -r requirements-hashes.txt | ||
|
|
||
| echo "==> [install] frontend dependencies (pnpm@11.5.3)" | ||
| cd "$REPO_ROOT/frontend" | ||
| corepack enable | ||
| corepack prepare pnpm@11.5.3 --activate | ||
| corepack pnpm@11.5.3 install --frozen-lockfile | ||
|
|
||
| echo "==> [install] done" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env bash | ||
| # Per-boot runtime reconciliation for Naruon Cloud Agent environments. | ||
| # | ||
| # Runs on every VM start (idempotent): brings up the PostgreSQL cluster, | ||
| # materializes a local dev .env with generated secrets on first boot, ensures | ||
| # the app database + pgvector extension exist, and applies Alembic migrations. | ||
| # | ||
| # Dependency installation lives in .cursor/install.sh; this script only | ||
| # reconciles per-boot state and then returns so the backend/frontend terminals | ||
| # can start. | ||
| set -euo pipefail | ||
|
|
||
| REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| cd "$REPO_ROOT" | ||
|
|
||
| ENV_FILE="$HOME/.env" | ||
| PY="$REPO_ROOT/backend/.venv/bin/python" | ||
|
|
||
| echo "==> [start] ensuring PostgreSQL 16 cluster is online" | ||
| if ! pg_lsclusters -h 2>/dev/null | awk '{print $4}' | grep -q online; then | ||
| sudo pg_ctlcluster 16 main start || true | ||
| fi | ||
| # Wait for the server to accept connections before touching it. | ||
| for _ in $(seq 1 30); do | ||
| if sudo -u postgres pg_isready -q; then break; fi | ||
| sleep 1 | ||
| done | ||
| if ! sudo -u postgres pg_isready -q; then | ||
| echo "==> [start] PostgreSQL did not become ready" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "==> [start] generating local dev .env on first boot (secrets are per-VM)" | ||
| if [ ! -f "$ENV_FILE" ]; then | ||
| "$PY" - "$ENV_FILE" <<'PYGEN' | ||
| import os, secrets, sys | ||
| from pathlib import Path | ||
| from cryptography.fernet import Fernet | ||
|
|
||
| env_path = Path(sys.argv[1]) | ||
| db_password = secrets.token_urlsafe(24) | ||
| hmac_secret = secrets.token_urlsafe(48) | ||
| enc_key = Fernet.generate_key().decode() | ||
|
|
||
| env_path.write_text( | ||
| "# Naruon local dev environment (generated per-VM; not committed).\n" | ||
| f"DATABASE_URL=postgresql+asyncpg://postgres:{db_password}@127.0.0.1:5432/ai_email\n" | ||
| f"AUTH_SESSION_HMAC_SECRET={hmac_secret}\n" | ||
| f"ENCRYPTION_KEY={enc_key}\n" | ||
| "DEBUG=false\n" | ||
| "RUNTIME_ENVIRONMENT=development\n" | ||
| "ENABLE_PROMETHEUS_METRICS=false\n" | ||
| "ALLOWED_CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000," | ||
| "http://localhost:8000,http://127.0.0.1:8000\n" | ||
| "SMTP_MODE=simulated\n" | ||
| "OPENAI_API_KEY=\n" | ||
| "OPENAI_EMBEDDING_MODEL=text-embedding-3-small\n" | ||
| "OPENAI_MODEL=gpt-4o\n", | ||
| encoding="utf-8", | ||
| ) | ||
| os.chmod(env_path, 0o600) | ||
| print(f"wrote {env_path}") | ||
| PYGEN | ||
| fi | ||
|
|
||
| echo "==> [start] reconciling database role, database, and pgvector extension" | ||
| # Keep the local postgres role secret in sync with DATABASE_URL without | ||
| # interpolating it into SQL or the process argument list. | ||
| "$PY" "$REPO_ROOT/backend/scripts/reconcile_local_postgres_role.py" --env-file "$ENV_FILE" | ||
| if ! sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='ai_email'" | grep -q 1; then | ||
| sudo -u postgres createdb ai_email | ||
| fi | ||
| sudo -u postgres psql -d ai_email -v ON_ERROR_STOP=1 \ | ||
| -c "CREATE EXTENSION IF NOT EXISTS vector;" >/dev/null | ||
|
|
||
| echo "==> [start] applying database migrations (alembic upgrade head)" | ||
| cd "$REPO_ROOT/backend" | ||
| "$PY" scripts/migrate_db.py | ||
|
|
||
| echo "==> [start] done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Guard email_records.is_read with NOT NULL DEFAULT true. | ||
|
|
||
| Revision ID: 0018_email_record_read_state | ||
| Revises: 0017_merge_newsdom_carddav_heads | ||
|
|
||
| ``0011_email_read_state`` only mutates the retired ``emails`` table. Fresh | ||
| databases already receive ``email_records.is_read`` from ``0001`` | ||
| ``create_all`` plus the current model ``server_default``. Existing databases | ||
| whose ``email_records`` row predates that column (or lacks a server default) | ||
| still need a guarded additive revision so raw INSERTs that omit ``is_read`` | ||
| succeed. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| revision = "0018_email_record_read_state" | ||
| down_revision = "0017_merge_newsdom_carddav_heads" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| _EMAIL_RECORDS_TABLE = "email_records" | ||
| _READ_STATE_COLUMN = "is_read" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| bind = op.get_bind() | ||
| inspector = sa.inspect(bind) | ||
| if not inspector.has_table(_EMAIL_RECORDS_TABLE): | ||
| return | ||
| columns = { | ||
| column["name"]: column | ||
| for column in inspector.get_columns(_EMAIL_RECORDS_TABLE) | ||
| } | ||
| if _READ_STATE_COLUMN not in columns: | ||
| op.add_column( | ||
| _EMAIL_RECORDS_TABLE, | ||
| sa.Column( | ||
| _READ_STATE_COLUMN, | ||
| sa.Boolean(), | ||
| nullable=False, | ||
| server_default=sa.text("true"), | ||
| ), | ||
| ) | ||
| return | ||
| if columns[_READ_STATE_COLUMN].get("default") is None: | ||
| op.alter_column( | ||
| _EMAIL_RECORDS_TABLE, | ||
| _READ_STATE_COLUMN, | ||
| existing_type=sa.Boolean(), | ||
| existing_nullable=False, | ||
| server_default=sa.text("true"), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # No-op: ``create_all`` and later model metadata may already own this | ||
| # column. Dropping it would remove a default this revision did not | ||
| # necessarily create. | ||
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.