Merge pull request #59 from ckan/Potential-Security-Updates

Potential security updates
This commit is contained in:
Brett Jones 2023-07-31 16:23:22 +01:00 committed by GitHub
commit 1928074f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 181 additions and 58 deletions

15
.env
View File

@ -13,13 +13,18 @@ NGINX_PORT_HOST=81
NGINX_SSLPORT_HOST=8443 NGINX_SSLPORT_HOST=8443
# CKAN databases # CKAN databases
POSTGRES_USER=ckan POSTGRES_USER=postgres
POSTGRES_PASSWORD=ckan POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
POSTGRES_HOST=db
CKAN_DB_USER=ckandbuser
CKAN_DB_PASSWORD=ckandbpassword
CKAN_DB=ckandb
DATASTORE_READONLY_USER=datastore_ro DATASTORE_READONLY_USER=datastore_ro
DATASTORE_READONLY_PASSWORD=datastore DATASTORE_READONLY_PASSWORD=datastore
POSTGRES_HOST=db DATASTORE_DB=datastore
CKAN_SQLALCHEMY_URL=postgresql://ckan:ckan@db/ckan CKAN_SQLALCHEMY_URL=postgresql://ckandbuser:ckandbpassword@db/ckandb
CKAN_DATASTORE_WRITE_URL=postgresql://ckan:ckan@db/datastore CKAN_DATASTORE_WRITE_URL=postgresql://ckandbuser:ckandbpassword@db/datastore
CKAN_DATASTORE_READ_URL=postgresql://datastore_ro:datastore@db/datastore CKAN_DATASTORE_READ_URL=postgresql://datastore_ro:datastore@db/datastore
# Test database connections # Test database connections

View File

@ -13,13 +13,18 @@ NGINX_PORT_HOST=81
NGINX_SSLPORT_HOST=8443 NGINX_SSLPORT_HOST=8443
# CKAN databases # CKAN databases
POSTGRES_USER=ckan POSTGRES_USER=postgres
POSTGRES_PASSWORD=ckan POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
POSTGRES_HOST=db
CKAN_DB_USER=ckandbuser
CKAN_DB_PASSWORD=ckandbpassword
CKAN_DB=ckandb
DATASTORE_READONLY_USER=datastore_ro DATASTORE_READONLY_USER=datastore_ro
DATASTORE_READONLY_PASSWORD=datastore DATASTORE_READONLY_PASSWORD=datastore
POSTGRES_HOST=db DATASTORE_DB=datastore
CKAN_SQLALCHEMY_URL=postgresql://ckan:ckan@db/ckan CKAN_SQLALCHEMY_URL=postgresql://ckandbuser:ckandbpassword@db/ckandb
CKAN_DATASTORE_WRITE_URL=postgresql://ckan:ckan@db/datastore CKAN_DATASTORE_WRITE_URL=postgresql://ckandbuser:ckandbpassword@db/datastore
CKAN_DATASTORE_READ_URL=postgresql://datastore_ro:datastore@db/datastore CKAN_DATASTORE_READ_URL=postgresql://datastore_ro:datastore@db/datastore
# Test database connections # Test database connections
@ -30,7 +35,7 @@ TEST_CKAN_DATASTORE_READ_URL=postgresql://datastore_ro:datastore@db/datastore_te
# CKAN core # CKAN core
CKAN_VERSION=2.10.0 CKAN_VERSION=2.10.0
CKAN_SITE_ID=default CKAN_SITE_ID=default
CKAN_SITE_URL=http://ckan:5000 CKAN_SITE_URL=https://localhost:8443
CKAN_PORT=5000 CKAN_PORT=5000
CKAN_PORT_HOST=5000 CKAN_PORT_HOST=5000
CKAN___BEAKER__SESSION__SECRET=CHANGE_ME CKAN___BEAKER__SESSION__SECRET=CHANGE_ME

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ _service-provider/*
_solr/schema.xml _solr/schema.xml
_src/* _src/*
local/* local/*
.env

View File

@ -206,7 +206,7 @@ running the latest version of Datapusher.
## 10. NGINX ## 10. NGINX
The base Docker Compose configuration uses an NGINX image as the front-end (ie: reverse proxy). It includes HTTPS running on port number 8443 and an HTTP port (81). A "self-signed" SSL certificate is generated beforehand and the server certificate and key files are included. The NGINX `server_name` directive and the `CN` field in the SSL certificate have been both set to 'localhost'. This should obviously not be used for production. The base Docker Compose configuration uses an NGINX image as the front-end (ie: reverse proxy). It includes HTTPS running on port number 8443. A "self-signed" SSL certificate is generated as part of the ENTRYPOINT. The NGINX `server_name` directive and the `CN` field in the SSL certificate have been both set to 'localhost'. This should obviously not be used for production.
Creating the SSL cert and key files as follows: Creating the SSL cert and key files as follows:
`openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=DE/ST=Berlin/L=Berlin/O=None/CN=localhost" -keyout ckan-local.key -out ckan-local.crt` `openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=DE/ST=Berlin/L=Berlin/O=None/CN=localhost" -keyout ckan-local.key -out ckan-local.crt`

View File

@ -43,18 +43,15 @@ services:
container_name: ${POSTGRESQL_CONTAINER_NAME} container_name: ${POSTGRESQL_CONTAINER_NAME}
build: build:
context: postgresql/ context: postgresql/
args:
- DATASTORE_READONLY_PASSWORD=${DATASTORE_READONLY_PASSWORD}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
environment: environment:
- DATASTORE_READONLY_PASSWORD=${DATASTORE_READONLY_PASSWORD} - DATASTORE_READONLY_PASSWORD
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_PASSWORD
- PGDATA=/var/lib/postgresql/data/db - CKAN_DB_PASSWORD
volumes: volumes:
- pg_data:/var/lib/postgresql/data - pg_data:/var/lib/postgresql/data
restart: unless-stopped restart: unless-stopped
healthcheck: healthcheck:
test: ["CMD", "pg_isready", "-U", "ckan"] test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]
solr: solr:
container_name: ${SOLR_CONTAINER_NAME} container_name: ${SOLR_CONTAINER_NAME}

View File

@ -1,5 +1,6 @@
version: "3" version: "3"
volumes: volumes:
ckan_storage: ckan_storage:
pg_data: pg_data:
@ -12,11 +13,13 @@ services:
build: build:
context: nginx/ context: nginx/
dockerfile: Dockerfile dockerfile: Dockerfile
networks:
- webnet
- ckannet
depends_on: depends_on:
ckan: ckan:
condition: service_healthy condition: service_healthy
ports: ports:
- "0.0.0.0:${NGINX_PORT_HOST}:${NGINX_PORT}"
- "0.0.0.0:${NGINX_SSLPORT_HOST}:${NGINX_SSLPORT}" - "0.0.0.0:${NGINX_SSLPORT_HOST}:${NGINX_SSLPORT}"
ckan: ckan:
@ -26,6 +29,11 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
args: args:
- TZ=${TZ} - TZ=${TZ}
networks:
- ckannet
- dbnet
- solrnet
- redisnet
env_file: env_file:
- .env - .env
depends_on: depends_on:
@ -35,8 +43,6 @@ services:
condition: service_healthy condition: service_healthy
redis: redis:
condition: service_healthy condition: service_healthy
ports:
- "0.0.0.0:${CKAN_PORT_HOST}:${CKAN_PORT}"
volumes: volumes:
- ckan_storage:/var/lib/ckan - ckan_storage:/var/lib/ckan
restart: unless-stopped restart: unless-stopped
@ -45,6 +51,9 @@ services:
datapusher: datapusher:
container_name: ${DATAPUSHER_CONTAINER_NAME} container_name: ${DATAPUSHER_CONTAINER_NAME}
networks:
- ckannet
- dbnet
image: ckan/ckan-base-datapusher:${DATAPUSHER_VERSION} image: ckan/ckan-base-datapusher:${DATAPUSHER_VERSION}
restart: unless-stopped restart: unless-stopped
healthcheck: healthcheck:
@ -54,21 +63,28 @@ services:
container_name: ${POSTGRESQL_CONTAINER_NAME} container_name: ${POSTGRESQL_CONTAINER_NAME}
build: build:
context: postgresql/ context: postgresql/
args: networks:
- DATASTORE_READONLY_PASSWORD=${DATASTORE_READONLY_PASSWORD} - dbnet
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
environment: environment:
- DATASTORE_READONLY_PASSWORD=${DATASTORE_READONLY_PASSWORD} - POSTGRES_USER
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_PASSWORD
- PGDATA=/var/lib/postgresql/data/db - POSTGRES_DB
- CKAN_DB_USER
- CKAN_DB_PASSWORD
- CKAN_DB
- DATASTORE_READONLY_USER
- DATASTORE_READONLY_PASSWORD
- DATASTORE_DB
volumes: volumes:
- pg_data:/var/lib/postgresql/data - pg_data:/var/lib/postgresql/data
restart: unless-stopped restart: unless-stopped
healthcheck: healthcheck:
test: ["CMD", "pg_isready", "-U", "ckan"] test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]
solr: solr:
container_name: ${SOLR_CONTAINER_NAME} container_name: ${SOLR_CONTAINER_NAME}
networks:
- solrnet
image: ckan/ckan-solr:${SOLR_IMAGE_VERSION} image: ckan/ckan-solr:${SOLR_IMAGE_VERSION}
volumes: volumes:
- solr_data:/var/solr - solr_data:/var/solr
@ -79,6 +95,18 @@ services:
redis: redis:
container_name: ${REDIS_CONTAINER_NAME} container_name: ${REDIS_CONTAINER_NAME}
image: redis:${REDIS_VERSION} image: redis:${REDIS_VERSION}
networks:
- redisnet
restart: unless-stopped restart: unless-stopped
healthcheck: healthcheck:
test: ["CMD", "redis-cli", "-e", "QUIT"] test: ["CMD", "redis-cli", "-e", "QUIT"]
networks:
webnet:
ckannet:
solrnet:
internal: true
dbnet:
internal: true
redisnet:
internal: true

View File

@ -2,11 +2,22 @@ FROM nginx:stable-alpine
ENV NGINX_DIR=/etc/nginx ENV NGINX_DIR=/etc/nginx
RUN apk update --no-cache && \
apk upgrade --no-cache && \
apk add --no-cache openssl
COPY setup/nginx.conf ${NGINX_DIR}/nginx.conf COPY setup/nginx.conf ${NGINX_DIR}/nginx.conf
COPY setup/index.html /usr/share/nginx/html/index.html COPY setup/index.html /usr/share/nginx/html/index.html
COPY setup/default.conf ${NGINX_DIR}/conf.d/ COPY setup/default.conf ${NGINX_DIR}/conf.d/
RUN mkdir -p ${NGINX_DIR}/certs RUN mkdir -p ${NGINX_DIR}/certs
COPY setup/ckan-local.* ${NGINX_DIR}/certs/
EXPOSE 81 ENTRYPOINT \
openssl req \
-subj '/C=DE/ST=Berlin/L=Berlin/O=None/CN=localhost' \
-x509 -newkey rsa:4096 \
-nodes -keyout /etc/nginx/ssl/default_key.pem \
-keyout ${NGINX_DIR}/certs/ckan-local.key \
-out ${NGINX_DIR}/certs/ckan-local.crt \
-days 365 && \
nginx -g 'daemon off;'

View File

@ -1,12 +1,24 @@
server { server {
listen 80; #listen 80;
listen [::]:80; #listen [::]:80;
listen 443 ssl; listen 443 ssl;
listen [::]:443 ssl; listen [::]:443 ssl;
server_name localhost; server_name localhost;
ssl_certificate /etc/nginx/certs/ckan-local.crt; ssl_certificate /etc/nginx/certs/ckan-local.crt;
ssl_certificate_key /etc/nginx/certs/ckan-local.key; ssl_certificate_key /etc/nginx/certs/ckan-local.key;
# TLS 1.2 & 1.3 only
ssl_protocols TLSv1.2 TLSv1.3;
# Disable weak ciphers
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
# SSL sessions
ssl_session_timeout 1d;
# ssl_session_cache dfine in stream and http
ssl_session_tickets off;
#access_log /var/log/nginx/host.access.log main; #access_log /var/log/nginx/host.access.log main;
location / { location / {
@ -20,13 +32,15 @@ server {
proxy_cache_key $host$scheme$proxy_host$request_uri; proxy_cache_key $host$scheme$proxy_host$request_uri;
} }
error_page 404 /404.html; error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;
# redirect server error pages to the static page /50x.html # redirect server error pages to the static page /error.html
# #
error_page 500 502 503 504 /50x.html; location = /error.html {
location = /50x.html { ssi on;
root /usr/share/nginx/html; internal;
auth_basic off;
root /usr/share/nginx/html;
} }
} }

View File

@ -22,14 +22,70 @@ http {
access_log /var/log/nginx/access.log main; access_log /var/log/nginx/access.log main;
sendfile on; sendfile on;
#tcp_nopush on; tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
keepalive_timeout 65; keepalive_timeout 65;
#gzip on; # Don't expose Nginx version
server_tokens off;
# Prevent clickjacking attacks
add_header X-Frame-Options "SAMEORIGIN";
# Mitigate Cross-Site scripting attack
add_header X-XSS-Protection "1; mode=block";
# Enable gzip encryption
gzip on;
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=cache:30m max_size=250m; proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=cache:30m max_size=250m;
proxy_temp_path /tmp/nginx_proxy 1 2; proxy_temp_path /tmp/nginx_proxy 1 2;
include /etc/nginx/conf.d/*.conf; include /etc/nginx/conf.d/*.conf;
# Error status text
map $status $status_text {
400 'Bad Request';
401 'Unauthorized';
402 'Payment Required';
403 'Forbidden';
404 'Not Found';
405 'Method Not Allowed';
406 'Not Acceptable';
407 'Proxy Authentication Required';
408 'Request Timeout';
409 'Conflict';
410 'Gone';
411 'Length Required';
412 'Precondition Failed';
413 'Payload Too Large';
414 'URI Too Long';
415 'Unsupported Media Type';
416 'Range Not Satisfiable';
417 'Expectation Failed';
418 'I\'m a teapot';
421 'Misdirected Request';
422 'Unprocessable Entity';
423 'Locked';
424 'Failed Dependency';
425 'Too Early';
426 'Upgrade Required';
428 'Precondition Required';
429 'Too Many Requests';
431 'Request Header Fields Too Large';
451 'Unavailable For Legal Reasons';
500 'Internal Server Error';
501 'Not Implemented';
502 'Bad Gateway';
503 'Service Unavailable';
504 'Gateway Timeout';
505 'HTTP Version Not Supported';
506 'Variant Also Negotiates';
507 'Insufficient Storage';
508 'Loop Detected';
510 'Not Extended';
511 'Network Authentication Required';
default 'Something is wrong';
}
} }

View File

@ -1,13 +1,4 @@
FROM postgres:12-alpine FROM postgres:12-alpine
# Allow connections; we don't map out any ports so only linked docker containers can connect
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
# Customize default user/pass/db
ENV POSTGRES_DB ckan
ENV POSTGRES_USER ckan
ARG POSTGRES_PASSWORD
ARG DATASTORE_READONLY_PASSWORD
# Include extra setup scripts (eg datastore) # Include extra setup scripts (eg datastore)
ADD docker-entrypoint-initdb.d /docker-entrypoint-initdb.d ADD docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

View File

@ -0,0 +1,7 @@
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE ROLE "$CKAN_DB_USER" NOSUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD '$CKAN_DB_PASSWORD';
CREATE DATABASE "$CKAN_DB" OWNER "$CKAN_DB_USER" ENCODING 'utf-8';
EOSQL

View File

@ -1,4 +0,0 @@
\set datastore_ro_password '\'' `echo $DATASTORE_READONLY_PASSWORD` '\''
CREATE ROLE datastore_ro NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD :datastore_ro_password;
CREATE DATABASE datastore OWNER ckan ENCODING 'utf-8';

View File

@ -0,0 +1,7 @@
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE ROLE "$DATASTORE_READONLY_USER" NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD '$DATASTORE_READONLY_PASSWORD';
CREATE DATABASE "$DATASTORE_DB" OWNER "$CKAN_DB_USER" ENCODING 'utf-8';
EOSQL

View File

@ -1,2 +0,0 @@
CREATE DATABASE ckan_test OWNER ckan ENCODING 'utf-8';
CREATE DATABASE datastore_test OWNER ckan ENCODING 'utf-8';

View File

@ -0,0 +1,7 @@
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE ckan_test OWNER "$CKAN_DB_USER" ENCODING 'utf-8';
CREATE DATABASE datastore_test OWNER "$CKAN_DB_USER" ENCODING 'utf-8';
EOSQL