Skip to content

Commit 9b31a14

Browse files
committed
standardize ca-config
Signed-off-by: Simon L. <[email protected]>
1 parent 6b3af00 commit 9b31a14

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

Containers/nextcloud/config/postgres.config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
$CONFIG = array(
44
'pgsql_ssl' => array(
55
'mode' => 'verify-ca',
6-
'rootcert' => '/var/www/html/data/certificates/POSTGRES',
6+
'rootcert' => '/var/www/html/resources/config/ca-bundle.crt',
77
),
88
);
99
}
1010
if (getenv('NEXTCLOUD_TRUSTED_CERTIFICATES_MYSQL')) {
1111
$CONFIG = array(
1212
'dbdriveroptions' => array(
13-
'PDO::MYSQL_ATTR_SSL_CA' => '/var/www/html/data/certificates/MYSQL',
13+
'PDO::MYSQL_ATTR_SSL_CA' => '/var/www/html/resources/config/ca-bundle.crt',
1414
),
1515
);
1616
}

Containers/nextcloud/entrypoint.sh

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,61 @@ run_upgrade_if_needed_due_to_app_update() {
2020
fi
2121
}
2222

23+
create_global_root_cert() {
24+
# Only run if env is set
25+
if env | grep -q NEXTCLOUD_TRUSTED_CERTIFICATES_; then
26+
27+
# Enable debug mode
28+
set -x
29+
30+
# Default vars
31+
CERTIFICATES_ROOT_DIR="/var/www/html/data/certificates"
32+
CERTIFICATE_BUNDLE="/var/www/html/resources/config/ca-bundle.crt"
33+
34+
# Retrieve default root cert bundle
35+
if ! [ -f "$SOURCE_LOCATION/resources/config/ca-bundle.crt" ]; then
36+
echo "Root ca-bundle not found. Only concattening configured NEXTCLOUD_TRUSTED_CERTIFICATES files!"
37+
# Recreate cert file
38+
rm -f "$CERTIFICATE_BUNDLE"
39+
touch "$CERTIFICATE_BUNDLE"
40+
else
41+
# Write default bundle to the target ca file
42+
cat "$SOURCE_LOCATION/resources/config/ca-bundle.crt" > "$CERTIFICATES_ROOT_DIR/ca-bundle.crt"
43+
fi
44+
45+
# Remove old root certs and recreate them with current ones
46+
rm -r "$CERTIFICATES_ROOT_DIR"
47+
mkdir -p "$CERTIFICATES_ROOT_DIR"
48+
49+
# Iterate through certs
50+
TRUSTED_CERTIFICATES="$(env | grep NEXTCLOUD_TRUSTED_CERTIFICATES_ | grep -oP '^[A-Z_a-z0-9]+')"
51+
mapfile -t TRUSTED_CERTIFICATES <<< "$TRUSTED_CERTIFICATES"
52+
for certificate in "${TRUSTED_CERTIFICATES[@]}"; do
53+
54+
# Create new line
55+
echo "" >> "$CERTIFICATE_BUNDLE"
56+
57+
# Check if variable is a simple switch. For example used by postgres and mysql tls connections
58+
if [ "${!certificate}" != "yes" ]; then
59+
# Write out cert to bundle
60+
echo "${!certificate}" >> "$CERTIFICATE_BUNDLE"
61+
fi
62+
63+
# Create file in cer dir
64+
if ! [ -f "$CERTIFICATES_ROOT_DIR/$CERTIFICATE_NAME" ]; then
65+
touch "$CERTIFICATES_ROOT_DIR/$CERTIFICATE_NAME"
66+
fi
67+
68+
done
69+
70+
# Print out bundle one last time
71+
cat "$CERTIFICATE_BUNDLE"
72+
73+
# Disable debug mode
74+
set +x
75+
fi
76+
}
77+
2378
# Adjust DATABASE_TYPE to by Nextcloud supported value
2479
if [ "$DATABASE_TYPE" = postgres ]; then
2580
export DATABASE_TYPE=pgsql
@@ -289,6 +344,9 @@ EOF
289344
echo "$NEXTCLOUD_TRUSTED_CERTIFICATES_MYSQL" > "/var/www/html/data/certificates/MYSQL"
290345
fi
291346

347+
# Create global root cert
348+
create_global_root_cert
349+
292350
echo "Installing with $DATABASE_TYPE database"
293351
# Set a default value for POSTGRES_PORT
294352
if [ -z "$POSTGRES_PORT" ]; then
@@ -459,6 +517,9 @@ EOF
459517
rm "$NEXTCLOUD_DATA_DIR/update.failed"
460518
bash /notify.sh "Nextcloud update to $image_version successful!" "You may inspect the Nextcloud container logs for more information."
461519

520+
# Create global root cert
521+
create_global_root_cert
522+
462523
php /var/www/html/occ app:update --all
463524

464525
run_upgrade_if_needed_due_to_app_update
@@ -649,23 +710,8 @@ else
649710
fi
650711
# AIO app end # Do not remove or change this line!
651712

652-
# Allow to add custom certs to Nextcloud's trusted cert store
653-
if env | grep -q NEXTCLOUD_TRUSTED_CERTIFICATES_; then
654-
set -x
655-
TRUSTED_CERTIFICATES="$(env | grep NEXTCLOUD_TRUSTED_CERTIFICATES_ | grep -oP '^[A-Z_a-z0-9]+')"
656-
mapfile -t TRUSTED_CERTIFICATES <<< "$TRUSTED_CERTIFICATES"
657-
CERTIFICATES_ROOT_DIR="/var/www/html/data/certificates"
658-
mkdir -p "$CERTIFICATES_ROOT_DIR"
659-
for certificate in "${TRUSTED_CERTIFICATES[@]}"; do
660-
# shellcheck disable=SC2001
661-
CERTIFICATE_NAME="$(echo "$certificate" | sed 's|^NEXTCLOUD_TRUSTED_CERTIFICATES_||')"
662-
if ! [ -f "$CERTIFICATES_ROOT_DIR/$CERTIFICATE_NAME" ]; then
663-
echo "${!certificate}" > "$CERTIFICATES_ROOT_DIR/$CERTIFICATE_NAME"
664-
php /var/www/html/occ security:certificates:import "$CERTIFICATES_ROOT_DIR/$CERTIFICATE_NAME"
665-
fi
666-
done
667-
set +x
668-
fi
713+
# Create global root cert
714+
create_global_root_cert
669715

670716
# Notify push
671717
if ! [ -d "/var/www/html/custom_apps/notify_push" ]; then

Containers/notify-push/start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ fi
6868

6969
# Postgres root cert
7070
if [ -f "/nextcloud/data/certificates/POSTGRES" ]; then
71-
CERT_OPTIONS="?sslmode=verify-ca&sslrootcert=/nextcloud/data/certificates/POSTGRES"
71+
CERT_OPTIONS="?sslmode=verify-ca&sslrootcert=/nextcloud/resources/config/ca-bundle.crt"
7272
# Mysql root cert
7373
elif [ -f "/nextcloud/data/certificates/MYSQL" ]; then
74-
CERT_OPTIONS="?sslmode=verify-ca&ssl-ca=/nextcloud/data/certificates/MYSQL"
74+
CERT_OPTIONS="?sslmode=verify-ca&ssl-ca=/nextcloud/resources/config/ca-bundle.crt"
7575
fi
7676

7777
# Set sensitive values as env

0 commit comments

Comments
 (0)