FROM python:3.12-slim

LABEL org.opencontainers.image.title="ch03-vuln-app" \
      org.opencontainers.image.description="Deliberately vulnerable Flask app for Chapter 3 labs"

WORKDIR /app

# Install build deps for cryptography wheel
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    gcc libffi-dev \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY vuln_app.py .

# Initialise the SQLite database at build time so the first request is fast
RUN python -c "
import sqlite3
DB='/tmp/ch03_lab.db'
con=sqlite3.connect(DB)
cur=con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, password TEXT, role TEXT)')
cur.execute('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, owner TEXT, content TEXT)')
for r in [('alice','password123','user'),('bob','hunter2','user'),('admin','supersecret','admin')]:
    cur.execute('INSERT OR IGNORE INTO users (name,password,role) VALUES (?,?,?)',r)
for r in [('alice',\"Alice private note\"),('admin','Admin config: DB_HOST=db.internal')]:
    cur.execute('INSERT OR IGNORE INTO notes (owner,content) VALUES (?,?)',r)
con.commit(); con.close()
print('DB seeded')
"

EXPOSE 5000

CMD ["python", "vuln_app.py"]
