mirror of
https://github.com/hanzoai/sql.git
synced 2026-11-15 19:38:30 +00:00
A connection that chooses the oauth method presents a token signed RS256 by the sql key. Trust is the <kid>.pem public files in hanzo_iam.dir, read on every connection, so a removed file revokes its key at once; nothing here calls IAM. The key must be RSA of 2048 bits or more, in a file of at most 16 KiB. The token is at most 8192 bytes, and each of its three parts decodes into a buffer with its own capacity, so the size check is not the only bound. iss is https://hanzo.id, aud holds hanzo-sql, typ is sql (a claim; the JOSE header's typ is not read), act is absent, sub is sql:<role>, exp and iat are required, exp is at most 60 s past, iat and nbf (optional) at most 60 s ahead, and exp - iat is at most 600 s. The claims are not parsed until the signature verifies. JSON is read with PostgreSQL's jsonapi, escapes decoded, and a member a check names may appear once however it is spelled. authn_id is the role, and the connection is authorized only when it equals the role requested (delegate_ident_mapping=1). A refusal logs one reason from a fixed list and never names the client's role; a key file that cannot be read is also named by its path, for the operator. Built with PGXS against PostgreSQL 18 and copied into the image. The TAP test signs tokens with keys it makes and checks each refusal by the reason logged: the size bound at 8192 and 8193 bytes, the key rules (EC, DSA, 1024-bit RSA, private key, certificate, empty, directory, 16 KiB and over), and JSON escapes in member names and values. make fuzz builds a libFuzzer target over the parser, run with -max_len=9500 from seeds on the bound. CI does not run either; LLM.md says so. Claude-Session: https://claude.ai/code/session_01SCPMGSQsLNF2YtHFL4bwyp
196 lines
9.0 KiB
Docker
196 lines
9.0 KiB
Docker
ARG PG_MAJOR=18
|
|
|
|
# Stage 1: Build all extensions on Debian (documentdb requires glibc toolchain)
|
|
FROM postgres:${PG_MAJOR}-bookworm AS builder
|
|
ARG PG_MAJOR
|
|
|
|
ENV DEBIAN_FRONTEND=noninteractive
|
|
|
|
# Install build dependencies for pgvector, pg_cron, and pg_documentdb
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
build-essential \
|
|
cmake \
|
|
git \
|
|
curl \
|
|
ca-certificates \
|
|
pkg-config \
|
|
postgresql-server-dev-${PG_MAJOR} \
|
|
libpq-dev \
|
|
libicu-dev \
|
|
libkrb5-dev \
|
|
libssl-dev \
|
|
# PostGIS (runtime dep for documentdb CREATE EXTENSION)
|
|
postgresql-${PG_MAJOR}-postgis-3 \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# --- pgvector ---
|
|
RUN cd /tmp && \
|
|
curl -sL https://github.com/hanzoai/sql-vector/archive/refs/heads/master.tar.gz | tar xz && \
|
|
cd sql-vector-master && \
|
|
make clean && \
|
|
make OPTFLAGS="" PG_CONFIG=/usr/bin/pg_config && \
|
|
make install PG_CONFIG=/usr/bin/pg_config && \
|
|
cd / && rm -rf /tmp/sql-vector-master
|
|
|
|
# --- pg_cron ---
|
|
ARG PG_CRON_VERSION=v1.6.7
|
|
RUN cd /tmp && \
|
|
curl -sL https://github.com/citusdata/pg_cron/archive/refs/tags/${PG_CRON_VERSION}.tar.gz | tar xz && \
|
|
cd pg_cron-* && \
|
|
make PG_CONFIG=/usr/bin/pg_config && \
|
|
make install PG_CONFIG=/usr/bin/pg_config && \
|
|
cd / && rm -rf /tmp/pg_cron-*
|
|
|
|
# --- pg_documentdb dependencies: libbson, Intel Decimal Math Lib, pcre2 ---
|
|
|
|
ARG LIBBSON_VERSION=1.28.0
|
|
RUN cd /tmp && \
|
|
curl -sL https://github.com/mongodb/mongo-c-driver/releases/download/${LIBBSON_VERSION}/mongo-c-driver-${LIBBSON_VERSION}.tar.gz | tar xz && \
|
|
cd mongo-c-driver-${LIBBSON_VERSION}/build && \
|
|
cmake -DENABLE_MONGOC=ON -DMONGOC_ENABLE_ICU=OFF -DENABLE_ICU=OFF \
|
|
-DCMAKE_C_FLAGS="-fPIC -g" -DCMAKE_BUILD_TYPE=Release \
|
|
-DCMAKE_INSTALL_PREFIX=/usr .. && \
|
|
make -j$(nproc) install && \
|
|
cd / && rm -rf /tmp/mongo-c-driver-*
|
|
|
|
ARG INTEL_MATH_LIB_VERSION=applied/2.0u3-1
|
|
RUN cd /tmp && \
|
|
mkdir -p intelmathlib/lib/intelmathlib && \
|
|
cd intelmathlib/lib/intelmathlib && \
|
|
git init && \
|
|
git remote add origin https://git.launchpad.net/ubuntu/+source/intelrdfpmath && \
|
|
git fetch --depth 1 origin "${INTEL_MATH_LIB_VERSION}" && \
|
|
git checkout FETCH_HEAD && \
|
|
cd LIBRARY && \
|
|
make -j$(nproc) _CFLAGS_OPT=-fPIC CC=gcc CALL_BY_REF=0 GLOBAL_RND=0 GLOBAL_FLAGS=0 UNCHANGED_BINARY_FLAGS=0 && \
|
|
cd /tmp/intelmathlib && \
|
|
# Create pkg-config file
|
|
printf 'prefix=/usr/lib/intelmathlib\nlibdir=${prefix}/LIBRARY\nincludedir=${prefix}/LIBRARY/src\n\nName: intelmathlib\nDescription: Intel Decimal Floating point math library\nVersion: 2.0 Update 2\nCflags: -I${includedir}\nLibs: -L${libdir} -lbid\n' > intelmathlib.pc && \
|
|
cp -R lib/intelmathlib /usr/lib/ && \
|
|
cp intelmathlib.pc /usr/lib/pkgconfig/ && \
|
|
cd / && rm -rf /tmp/intelmathlib
|
|
|
|
ARG PCRE2_VERSION=10.40
|
|
RUN cd /tmp && \
|
|
curl -sL https://github.com/PCRE2Project/pcre2/releases/download/pcre2-${PCRE2_VERSION}/pcre2-${PCRE2_VERSION}.tar.gz | tar xz && \
|
|
cd pcre2-${PCRE2_VERSION} && \
|
|
./configure --prefix=/usr --disable-shared --enable-static --enable-jit && \
|
|
make -j$(nproc) AM_CFLAGS=-fPIC install && \
|
|
cd / && rm -rf /tmp/pcre2-*
|
|
|
|
# --- zap_fdw (ZAP binary protocol — disabled by default via zap.enabled GUC) ---
|
|
COPY contrib/zap_fdw /tmp/zap_fdw
|
|
RUN cd /tmp/zap_fdw && \
|
|
make USE_PGXS=1 PG_CONFIG=/usr/bin/pg_config && \
|
|
make USE_PGXS=1 PG_CONFIG=/usr/bin/pg_config install && \
|
|
cd / && rm -rf /tmp/zap_fdw
|
|
|
|
# --- hanzo_iam (OAuth validator for hanzo-sql tokens: oauth_validator_libraries) ---
|
|
COPY contrib/hanzo_iam /tmp/hanzo_iam
|
|
RUN cd /tmp/hanzo_iam && \
|
|
make PG_CONFIG=/usr/bin/pg_config && \
|
|
make PG_CONFIG=/usr/bin/pg_config install && \
|
|
cd / && rm -rf /tmp/hanzo_iam
|
|
|
|
# --- pg_documentdb (core + api + extended_rum) ---
|
|
ARG DOCUMENTDB_VERSION=main
|
|
RUN cd /tmp && \
|
|
curl -sL https://github.com/microsoft/documentdb/archive/refs/heads/${DOCUMENTDB_VERSION}.tar.gz | tar xz && \
|
|
cd documentdb-* && \
|
|
make -C pg_documentdb_core PG_CONFIG=/usr/bin/pg_config install && \
|
|
make -C pg_documentdb PG_CONFIG=/usr/bin/pg_config install && \
|
|
make -C pg_documentdb_extended_rum PG_CONFIG=/usr/bin/pg_config install && \
|
|
cd / && rm -rf /tmp/documentdb-*
|
|
|
|
# Stage 2: Runtime image (Debian for glibc compat with documentdb)
|
|
FROM postgres:${PG_MAJOR}-bookworm
|
|
|
|
LABEL maintainer="[email protected]"
|
|
LABEL org.opencontainers.image.source="https://github.com/hanzoai/sql"
|
|
LABEL org.opencontainers.image.description="Hanzo SQL - PostgreSQL with pgvector, pg_cron, and pg_documentdb"
|
|
|
|
# Runtime deps: libpq (pg_cron), PostGIS (documentdb geospatial), ICU
|
|
ARG PG_MAJOR=18
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
libpq5 \
|
|
postgresql-${PG_MAJOR}-postgis-3 \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy Intel Decimal Math Library (runtime dep for documentdb)
|
|
COPY --from=builder /usr/lib/intelmathlib /usr/lib/intelmathlib
|
|
|
|
# Copy pgvector extension
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/vector.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/vector* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy pg_cron extension
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_cron.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/pg_cron* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy pg_documentdb_core extension
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb_core.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/documentdb_core* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy pg_documentdb extension
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/documentdb* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy pg_documentdb_extended_rum extension
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb_extended_rum_core.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb_extended_rum.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/documentdb_extended_rum* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy zap_fdw extension (disabled by default — set zap.enabled=true to activate)
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/zap_fdw.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/zap_fdw* /usr/share/postgresql/${PG_MAJOR}/extension/
|
|
|
|
# Copy hanzo_iam, the OAuth validator (oauth_validator_libraries = 'hanzo_iam';
|
|
# trusts the <kid>.pem files in hanzo_iam.dir)
|
|
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/hanzo_iam.so /usr/lib/postgresql/${PG_MAJOR}/lib/
|
|
|
|
# Custom postgresql.conf tuned for AI workloads
|
|
COPY conf/postgresql.conf /etc/postgresql/postgresql.conf
|
|
|
|
# Init scripts: enable extensions on startup
|
|
COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
|
|
|
|
# Our names on the outside, upstream's on the inside — see entrypoint.sh.
|
|
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
|
|
|
# sqlha runs a member of a replicated cluster under Patroni: a leader lease in the
|
|
# Kubernetes API, streaming replication, and promotion when the leader is gone.
|
|
# A single sql runs sqld as before; see sqlha.sh.
|
|
ARG PATRONI_VERSION=4.1.5
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
python3 python3-venv python3-psycopg2 \
|
|
&& python3 -m venv --system-site-packages /opt/patroni \
|
|
&& /opt/patroni/bin/pip install --no-cache-dir "patroni==${PATRONI_VERSION}" \
|
|
&& ln -s /opt/patroni/bin/patroni /usr/local/bin/patroni \
|
|
&& ln -s /opt/patroni/bin/patronictl /usr/local/bin/patronictl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
COPY sqlha.sh /usr/local/bin/sqlha
|
|
|
|
# reconcile is the sidecar of a sql pod: on the leader it makes roles, databases,
|
|
# the DCS block and pg_hba equal the declared file the pod mounts. It runs in the
|
|
# patroni venv, which sees psycopg2 and carries PyYAML; --help imports both, so a
|
|
# build whose python lacks either fails here and not in a pod.
|
|
COPY bin/reconcile /usr/local/bin/reconcile
|
|
RUN /usr/local/bin/reconcile --help >/dev/null
|
|
|
|
# `sqld` is the daemon, matching luxd / hanzod / zood / kvd — the fleet names a
|
|
# long-running server <thing>d. A SYMLINK rather than a rename: the server
|
|
# re-execs itself by path and the extensions resolve against the upstream name,
|
|
# so pointing a Hanzo name at it is the rename that is safe to make in the
|
|
# image. Renaming the binary is a change to the fork's build, not its packaging.
|
|
RUN ln -sf /usr/lib/postgresql/${PG_MAJOR}/bin/postgres /usr/local/bin/sqld
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=15s --timeout=3s --start-period=30s --retries=3 \
|
|
CMD pg_isready -U "${SQL_USER:?}" || exit 1
|
|
|
|
EXPOSE 5432
|
|
|
|
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
|
CMD ["sqld", "-c", "config_file=/etc/postgresql/postgresql.conf"]
|
|
|