Skip to content

Commit

Permalink
BREAKING feat(dbapi): switch to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
peterthomassen committed Aug 26, 2020
1 parent 149150c commit 5c4b999
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 57 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a docker-compose application providing the basic stack for deSEC name se
- `nslord`: Eventually authoritative DNS server (PowerDNS). DNSSEC keying material is generated here.
- `nsmaster`: Stealth authoritative DNS server (PowerDNS). Receives fully signed AXFR zone transfers from `nslord`. No access to keys.
- `api`: RESTful API to create deSEC users and domains, see [documentation](https://desec.readthedocs.io/).
- `dbapi`, `dblord`, `dbmaster`: MariaDB database services for `api`, `nslord`, and `nsmaster`, respectively.
- `dbapi`, `dblord`, `dbmaster`: Postgres database for `api`, MariaDB databases for `nslord` and `nsmaster`, respectively.
- `www`: nginx instance serving static web site content and proxying to `api`
- `celery`: A shadow instance of the `api` code for performing asynchronous tasks (email delivery).
- `rabbitmq`: `celery`'s queue
Expand Down Expand Up @@ -52,7 +52,7 @@ Although most configuration is contained in this repository, some external depen
- `DESECSTACK_API_EMAIL_PORT`: port for sending email
- `DESECSTACK_API_SECRETKEY`: Django secret
- `DESECSTACK_API_PSL_RESOLVER`: Resolver IP address to use for PSL lookups. If empty, the system's default resolver is used.
- `DESECSTACK_DBAPI_PASSWORD_desec`: mysql password for desecapi
- `DESECSTACK_DBAPI_PASSWORD_desec`: database password for desecapi
- `DESECSTACK_MINIMUM_TTL_DEFAULT`: minimum TTL users can set for RRsets. The setting is per domain, and the default defined here is used on domain creation.
- nslord-related
- `DESECSTACK_DBLORD_PASSWORD_pdns`: mysql password for pdns on nslord
Expand Down Expand Up @@ -82,8 +82,8 @@ Production:

Storage
-------
All important data is stored in the databases managed by the `db*` containers. They use Docker volumes which, by default, reside in `/var/lib/docker/volumes/desecstack_{dbapi,dblord,dbmaster}_mysql`.
This is the location you will want to back up. (Be sure to follow standard MySQL backup practices, i.e. make sure things are consistent.)
All important data is stored in the databases managed by the `db*` containers. They use Docker volumes which, by default, reside in `/var/lib/docker/volumes/desec-stack_{dbapi_postgres,dblord_mysql,dbmaster_mysql}`.
This is the location you will want to back up. (Be sure to follow standard MySQL/Postgres backup practices, i.e. make sure things are consistent.)

API Versions and Roadmap
------------------------
Expand Down Expand Up @@ -135,10 +135,10 @@ While there are certainly many ways to get started hacking desec-stack, here is
For desec-stack, [docker](https://docs.docker.com/install/linux/docker-ce/ubuntu/) and [docker-compose](https://docs.docker.com/compose/install/) are required.
Further tools that are required to start hacking are git and curl.
Recommended, but not strictly required for desec-stack development is to use certbot along with Let's Encrypt and PyCharm.
jq, httpie, libmariadbclient-dev, python3-dev (>= 3.8) and python3-venv (>= 3.8) are useful if you want to follow this guide.
jq, httpie, libpq-dev, python3-dev (>= 3.8) and python3-venv (>= 3.8) are useful if you want to follow this guide.
The webapp requires nodejs. To install everything you need for this guide except docker and docker-compose, use

sudo apt install certbot curl git httpie jq libmariadbclient-dev nodejs npm python3-dev python3-venv libmemcached-dev
sudo apt install certbot curl git httpie jq libpq-dev nodejs npm python3-dev python3-venv libmemcached-dev

1. **Get the code.** Clone this repository to your favorite location.

Expand Down
4 changes: 2 additions & 2 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.8-alpine

RUN apk add --no-cache bash dcron sqlite
RUN apk add --no-cache bash dcron postgresql-client sqlite

RUN mkdir /usr/src/app
WORKDIR /usr/src/app
Expand All @@ -9,7 +9,7 @@ ENV PIP_DISABLE_PIP_VERSION_CHECK=1
ENV PIP_NO_CACHE_DIR=1

COPY requirements.txt /usr/src/app/
RUN apk add --no-cache gcc freetype-dev libffi-dev musl-dev libmemcached-dev mariadb-connector-c-dev jpeg-dev zlib-dev \
RUN apk add --no-cache gcc freetype-dev libffi-dev musl-dev libmemcached-dev postgresql-dev jpeg-dev zlib-dev \
&& pip install -r requirements.txt \
&& apk --no-cache del gcc
RUN pip freeze
Expand Down
11 changes: 1 addition & 10 deletions api/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,12 @@

DATABASES = {
'default': {
'ENGINE': 'django_prometheus.db.backends.mysql',
'ENGINE': 'django_prometheus.db.backends.postgresql',
'NAME': 'desec',
'USER': 'desec',
'PASSWORD': os.environ['DESECSTACK_DBAPI_PASSWORD_desec'],
'HOST': 'dbapi',
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
'TEST': {
'CHARSET': 'utf8mb4',
'COLLATION': 'utf8mb4_bin',
},
},

}

CACHES = {
Expand Down
6 changes: 0 additions & 6 deletions api/desecapi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,6 @@ def is_empty(data_item):
# so for parallel requests, we can get integrity errors due to duplicate keys.
# This will be considered a 429-error, even though re-sending the request will be successful.
except OperationalError as e:
try:
if e.args[0] == 1213:
# 1213 is mysql for deadlock, other OperationalErrors are treated elsewhere or not treated at all
raise ConcurrencyException from e
except (AttributeError, KeyError):
pass
raise e
except (IntegrityError, models.RRset.DoesNotExist) as e:
raise ConcurrencyException from e
Expand Down
2 changes: 1 addition & 1 deletion api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ django-celery-email~=3.0.0
django-prometheus~=2.0.0
dnspython~=1.16.0
httpretty~=0.9.0
mysqlclient~=1.4.0
psycopg2~=2.8.5
prometheus-client~=0.8.0 # added to control django-prometheus' dependency version
psl-dns~=1.0
pylibmc~=1.6.1
Expand Down
2 changes: 1 addition & 1 deletion api/wait
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

# wait for api database to come up
host=dbapi; port=3306; n=120; i=0; while ! (echo > /dev/tcp/$host/$port) 2> /dev/null; do [[ $i -eq $n ]] && >&2 echo "$host:$port not up after $n seconds, exiting" && exit 1; echo "waiting for $host:$port to come up"; sleep 1; i=$((i+1)); done
host=dbapi; port=5432; n=120; i=0; while ! (echo > /dev/tcp/$host/$port) 2> /dev/null; do [[ $i -eq $n ]] && >&2 echo "$host:$port not up after $n seconds, exiting" && exit 1; echo "waiting for $host:$port to come up"; sleep 1; i=$((i+1)); done

# wait for pdns api to come up
host=nslord; port=8081; n=120; i=0; while ! (echo > /dev/tcp/$host/$port) 2> /dev/null; do [[ $i -eq $n ]] && >&2 echo "$host:$port not up after $n seconds, exiting" && exit 1; echo "waiting for $host:$port to come up"; sleep 1; i=$((i+1)); done
17 changes: 9 additions & 8 deletions dbapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
FROM mariadb:10.3
FROM postgres:12-alpine

# Use random throw-away root password. Our init scripts switch authentication to socket logins only
ENV MYSQL_RANDOM_ROOT_PASSWORD=yes
RUN apk add --no-cache pwgen

# install tools used in init script
RUN set -ex && apt-get update && apt-get -y install gettext-base && apt-get clean && rm -rf /var/lib/apt/lists/*
ADD docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

COPY initdb.d/* /docker-entrypoint-initdb.d/
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/
USER postgres

# mountable storage
VOLUME /var/lib/mysql
VOLUME /var/lib/postgresql/data

COPY entrypoint-wrapper.sh /usr/local/bin/
ENTRYPOINT ["entrypoint-wrapper.sh"]
CMD ["postgres"]
15 changes: 15 additions & 0 deletions dbapi/docker-entrypoint-initdb.d/init-user-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

# Get the postgres user or set it to a default value
if [ -n $POSTGRES_USER ]; then pg_user=$POSTGRES_USER; else pg_user="postgres"; fi
# Get the postgres db or set it to a default value
if [ -n $POSTGRES_DB ]; then pg_db=$POSTGRES_DB; else pg_db=$POSTGRES_USER; fi

if [ -n "$POSTGRES_NON_ROOT_USER" ]; then
psql -v ON_ERROR_STOP=1 --username "$pg_user" --dbname "$pg_db" <<-EOSQL
CREATE USER $POSTGRES_NON_ROOT_USER with encrypted password '$POSTGRES_NON_ROOT_USER_PASSWORD';
GRANT CREATE, CONNECT ON DATABASE $pg_db TO $POSTGRES_NON_ROOT_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE, INSERT, DELETE, REFERENCES ON TABLES TO $POSTGRES_NON_ROOT_USER;
EOSQL
fi
6 changes: 6 additions & 0 deletions dbapi/entrypoint-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -Eeo pipefail

# This password is set for the postgres user when initializing the database. It is not needed and thus not printed.
export POSTGRES_PASSWORD=$(pwgen -1 -s 32)
/usr/local/bin/docker-entrypoint.sh "$@"
6 changes: 0 additions & 6 deletions dbapi/initdb.d/00-init.sh

This file was deleted.

2 changes: 0 additions & 2 deletions dbapi/initdb.d/00-init.sql

This file was deleted.

7 changes: 0 additions & 7 deletions dbapi/initdb.d/00-init.sql.var

This file was deleted.

4 changes: 0 additions & 4 deletions dbapi/initdb.d/99-finish.sql

This file was deleted.

16 changes: 16 additions & 0 deletions dbapi/pg_hba.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
#host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
#host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication all trust
#host replication all 127.0.0.1/32 scram-sha-256
#host replication all ::1/128 scram-sha-256

host desec desec all scram-sha-256
host all all all reject
15 changes: 11 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ services:
build: dbapi
image: desec/dedyn-dbapi:latest
init: true
user: mysql:mysql
user: postgres:postgres
shm_size: 256M
volumes:
- dbapi_mysql:/var/lib/mysql
- dbapi_postgres:/var/lib/postgresql/data
- ./dbapi/pg_hba.conf:/usr/local/src/pg_hba.conf:ro
environment:
- DESECSTACK_IPV4_REAR_PREFIX16
- DESECSTACK_DBAPI_PASSWORD_desec
- POSTGRES_DB=desec
- POSTGRES_HOST_AUTH_METHOD=reject
- POSTGRES_INITDB_ARGS=--auth-host=scram-sha-256
- POSTGRES_NON_ROOT_USER=desec
- POSTGRES_NON_ROOT_USER_PASSWORD=${DESECSTACK_DBAPI_PASSWORD_desec}
networks:
- rearapi_dbapi
command: ["postgres", "-c", "hba_file=/usr/local/src/pg_hba.conf"]
logging:
driver: "syslog"
options:
Expand Down Expand Up @@ -352,7 +359,7 @@ services:
restart: unless-stopped

volumes:
dbapi_mysql:
dbapi_postgres:
dblord_mysql:
dbmaster_mysql:
openvpn-server_logs:
Expand Down

0 comments on commit 5c4b999

Please sign in to comment.