Skip to content

Commit a284aff

Browse files
authored
Merge pull request #2 from LordRobinCbz/develop
fix(config.inc.php/docker-entrypoint.sh,dockerfile,helpers.php): Move TLS logic from entrypoint to php configuration files
2 parents 2ee310d + b78da1f commit a284aff

File tree

4 files changed

+88
-64
lines changed

4 files changed

+88
-64
lines changed

apache/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ RUN set -ex; \
140140

141141
# Copy configuration
142142
COPY config.inc.php /etc/phpmyadmin/config.inc.php
143+
COPY helpers.php /etc/phpmyadmin/helpers.php
143144
RUN chown www-data:www-data -R /etc/phpmyadmin/
144145

145146
# Copy main script

apache/config.inc.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3+
define('SSL_DIR', '/etc/phpmyadmin/ssl');
4+
35
require '/etc/phpmyadmin/config.secret.inc.php';
6+
require '/etc/phpmyadmin/helpers.php';
47

58
/* Ensure we got the environment */
69
$vars = [
@@ -63,6 +66,47 @@
6366
$cfg['PmaAbsoluteUri'] = trim($_ENV['PMA_ABSOLUTE_URI']);
6467
}
6568

69+
if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
70+
if (!is_dir(SSL_DIR)) {
71+
mkdir(SSL_DIR, 0755, true);
72+
}
73+
file_put_contents(SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
74+
$_ENV['PMA_SSL_CA'] = SSL_DIR . '/pma-ssl-ca.pem';
75+
}
76+
77+
/* Decode and save the SSL key from base64 */
78+
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
79+
if (!is_dir(SSL_DIR)) {
80+
mkdir(SSL_DIR, 0755, true);
81+
}
82+
file_put_contents(SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
83+
$_ENV['PMA_SSL_KEY'] = SSL_DIR . '/pma-ssl-key.key';
84+
}
85+
86+
/* Decode and save the SSL certificate from base64 */
87+
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
88+
if (!is_dir(SSL_DIR)) {
89+
mkdir(SSL_DIR, 0755, true);
90+
}
91+
file_put_contents(SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
92+
$_ENV['PMA_SSL_CERT'] = SSL_DIR . '/pma-ssl-cert.pem';
93+
}
94+
95+
/* Decode and save multiple SSL CA certificates from base64 */
96+
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
97+
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
98+
}
99+
100+
/* Decode and save multiple SSL keys from base64 */
101+
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
102+
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
103+
}
104+
105+
/* Decode and save multiple SSL certificates from base64 */
106+
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
107+
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
108+
}
109+
66110
/* Figure out hosts */
67111

68112
/* Fallback to default linked */

apache/docker-entrypoint.sh

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,6 @@ if [ ! -z "${PMA_USER_CONFIG_BASE64}" ]; then
2929
echo "${PMA_USER_CONFIG_BASE64}" | base64 -d > /etc/phpmyadmin/config.user.inc.php
3030
fi
3131

32-
if [ ! -z "${PMA_SSL_CA_BASE64}" ]; then
33-
mkdir -p /etc/phpmyadmin/ssl
34-
echo "Adding the custom pma-ssl-ca from base64."
35-
echo "${PMA_SSL_CA_BASE64}" | base64 -d > /etc/phpmyadmin/ssl/pma-ssl-ca.pem
36-
export "PMA_SSL_CA"="/etc/phpmyadmin/ssl/pma-ssl-ca.pem"
37-
fi
38-
39-
if [ ! -z "${PMA_SSL_KEY_BASE64}" ]; then
40-
mkdir -p /etc/phpmyadmin/ssl
41-
echo "Adding the custom pma-ssl-key from base64."
42-
echo "${PMA_SSL_KEY_BASE64}" | base64 -d > /etc/phpmyadmin/ssl/pma-ssl-key.key
43-
export "PMA_SSL_KEY"="/etc/phpmyadmin/ssl/pma-ssl-key.key"
44-
fi
45-
46-
if [ ! -z "${PMA_SSL_CERT_BASE64}" ]; then
47-
mkdir -p /etc/phpmyadmin/ssl
48-
echo "Adding the custom pma-ssl-cert from base64."
49-
echo "${PMA_SSL_CERT_BASE64}" | base64 -d > /etc/phpmyadmin/ssl/pma-ssl-cert.pem
50-
export "PMA_SSL_CERT"="/etc/phpmyadmin/ssl/pma-ssl-cert.pem"
51-
fi
52-
53-
if [ ! -z "${PMA_SSL_CAS_BASE64}" ]; then
54-
echo "Adding multiples custom pma-ssl-ca from base64."
55-
PMA_SSL_CAS=$(generate_ssl_files "${PMA_SSL_CAS_BASE64}" "CA" "pem")
56-
export "PMA_SSL_CAS"
57-
fi
58-
59-
if [ ! -z "${PMA_SSL_KEYS_BASE64}" ]; then
60-
echo "Adding multiples custom pma-ssl-key from base64."
61-
PMA_SSL_KEYS=$(generate_ssl_files "${PMA_SSL_KEYS_BASE64}" "CERT" "cert")
62-
export "PMA_SSL_KEYS"
63-
fi
64-
65-
if [ ! -z "${PMA_SSL_CERTS_BASE64}" ]; then
66-
echo "Adding multiples custom pma-ssl-cert from base64."
67-
PMA_SSL_CERTS=$(generate_ssl_files "${PMA_SSL_CERTS_BASE64}" "KEY" "key")
68-
export "PMA_SSL_CERTS"
69-
fi
70-
7132
# start: Apache specific settings
7233
if [ -n "${APACHE_PORT+x}" ]; then
7334
echo "Setting apache port to ${APACHE_PORT}."
@@ -89,31 +50,6 @@ get_docker_secret() {
8950
fi
9051
}
9152

92-
# This function generates SSL files from a base64 encoded string.
93-
# Arguments:
94-
# 1. base64_string: A comma-separated string of base64 encoded SSL files.
95-
# 2. prefix: A prefix to be used in the output file names.
96-
# 3. extension: The file extension to be used for the output files.
97-
# The function creates a directory for the SSL files, decodes each base64 string,
98-
# writes the decoded content to a file, and returns a comma-separated list of the generated file paths.
99-
#
100-
generate_ssl_files() {
101-
local base64_string="${1}"
102-
local output_dir="/etc/phpmyadmin/ssl"
103-
mkdir -p "${output_dir}"
104-
IFS=',' read -ra FILES <<< "${base64_string}"
105-
local counter=1
106-
local ssl_files=""
107-
for file in "${FILES[@]}"; do
108-
local output_file="${output_dir}/pma-ssl-${2}-${counter}.${3}"
109-
echo "${file}" | base64 -d > "${output_file}"
110-
ssl_files="${ssl_files}${output_file},"
111-
counter=$((counter + 1))
112-
done
113-
ssl_files="${ssl_files%,}"
114-
echo "${ssl_files}"
115-
}
116-
11753
get_docker_secret PMA_USER
11854
get_docker_secret PMA_PASSWORD
11955
get_docker_secret MYSQL_ROOT_PASSWORD

apache/helpers.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
class SslFileGenerationException extends Exception {}
4+
5+
define('OUTPUT_DIR', '/etc/phpmyadmin/ssl');
6+
7+
/**
8+
* Helper function to decode and save multiple SSL files from base64.
9+
*
10+
* @param string $base64_string The base64 encoded string containing multiple SSL files separated by commas.
11+
* If no commas are present, the entire string is treated as a single file.
12+
* @param string $prefix The prefix to use for the generated SSL file names.
13+
* @param string $extension The file extension to use for the generated SSL files.
14+
* @return string A comma-separated list of paths to the generated SSL files.
15+
*/
16+
function decodeAndSaveSslFiles($base64_string, $prefix, $extension) {
17+
// Ensure the output directory exists
18+
if (!is_dir(OUTPUT_DIR)) {
19+
mkdir(OUTPUT_DIR, 0755, true);
20+
}
21+
22+
// Split the base64 string into an array of files
23+
$files = strpos($base64_string, ',') !== false ? explode(',', $base64_string) : [$base64_string];
24+
$counter = 1;
25+
$ssl_files = [];
26+
27+
// Process each file
28+
foreach ($files as $file) {
29+
$output_file = OUTPUT_DIR . "/pma-ssl-$prefix-$counter.$extension";
30+
31+
// Write the decoded file to the output directory
32+
if (file_put_contents($output_file, base64_decode($file)) === false) {
33+
throw new SslFileGenerationException("Failed to write to $output_file");
34+
}
35+
36+
// Add the output file path to the list
37+
$ssl_files[] = $output_file;
38+
$counter++;
39+
}
40+
41+
// Return a comma-separated list of the generated file paths
42+
return implode(',', $ssl_files);
43+
}

0 commit comments

Comments
 (0)