Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutual TLS (mTLS) support for remote database connections in PhpMyAdmin #448

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ docker run --name phpmyadmin -d -e PMA_HOSTS='sslhost,nosslhost' -e PMA_SSLS='1,
* ``PMA_SOCKET`` - define socket file for the MySQL connection
* ``PMA_SOCKETS`` - define comma separated list of socket files for the MySQL connections
* ``PMA_SSL`` - when set to 1, defines SSL usage for the MySQL connection
* ``PMA_SSLS`` - comma separated list of `0` and `1` defining SSL usage for the corresponding MySQL connections
* ``PMA_SSL_VERIFY`` - when set to 1, enables SSL certificate verification for the MySQL connection.
* ``PMA_SSL_VERIFIES`` - comma-separated list of `0` and `1` to enable or disable SSL certificate verification for multiple MySQL connections.
* ``PMA_SSL_CA_BASE64`` - in the context of mutual TLS security, allows setting your CA file as a base64 string inside the default `config.inc.php`.
* ``PMA_SSL_CAS_BASE64`` - in the context of mutual TLS security, allows setting multiple CA files as a comma-separated list of base64 strings inside the default `config.inc.php`.
* ``PMA_SSL_CERT_BASE64`` - in the context of mutual TLS security, allows setting your CERT file as a base64 string inside the default `config.inc.php`.
* ``PMA_SSL_CERTS_BASE64`` - in the context of mutual TLS security, allows setting multiple CERT files as a comma-separated list of base64 strings inside the default `config.inc.php`.
* ``PMA_SSL_KEY_BASE64`` - in the context of mutual TLS security, allows setting your KEY file as a base64 string inside the default `config.inc.php`.
* ``PMA_SSL_KEYS_BASE64`` - in the context of mutual TLS security, allows setting multiple KEY files as a comma-separated list of base64 strings inside the default `config.inc.php`.
* ``PMA_USER`` and ``PMA_PASSWORD`` - define username and password to use only with the `config` authentication method
* ``PMA_ABSOLUTE_URI`` - the full URL to phpMyAdmin. Sometimes needed when used in a reverse-proxy configuration. Don't set this unless needed. See [documentation](https://docs.phpmyadmin.net/en/latest/config.html#cfg_PmaAbsoluteUri).
* ``PMA_CONFIG_BASE64`` - if set, this option will override the default `config.inc.php` with the base64 decoded contents of the variable
Expand Down
1 change: 1 addition & 0 deletions apache/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ RUN set -ex; \

# Copy configuration
COPY config.inc.php /etc/phpmyadmin/config.inc.php
COPY helpers.php /etc/phpmyadmin/helpers.php
RUN chown www-data:www-data -R /etc/phpmyadmin/

# Copy main script
Expand Down
72 changes: 72 additions & 0 deletions apache/config.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

define('SSL_DIR', '/etc/phpmyadmin/ssl');
williamdes marked this conversation as resolved.
Show resolved Hide resolved

require '/etc/phpmyadmin/config.secret.inc.php';
require '/etc/phpmyadmin/helpers.php';

/* Ensure we got the environment */
$vars = [
Expand Down Expand Up @@ -28,7 +31,15 @@
'PMA_UPLOADDIR',
'PMA_SAVEDIR',
'PMA_SSL',
'PMA_SSL_VERIFY',
'PMA_SSL_CA',
'PMA_SSL_KEY',
'PMA_SSL_CERT',
'PMA_SSLS',
'PMA_SSL_VERIFIES',
'PMA_SSL_CAS',
'PMA_SSL_KEYS',
'PMA_SSL_CERTS'
];

foreach ($vars as $var) {
Expand All @@ -55,6 +66,47 @@
$cfg['PmaAbsoluteUri'] = trim($_ENV['PMA_ABSOLUTE_URI']);
}

if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
$_ENV['PMA_SSL_CA'] = SSL_DIR . '/pma-ssl-ca.pem';
}

/* Decode and save the SSL key from base64 */
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
$_ENV['PMA_SSL_KEY'] = SSL_DIR . '/pma-ssl-key.key';
}

/* Decode and save the SSL certificate from base64 */
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
$_ENV['PMA_SSL_CERT'] = SSL_DIR . '/pma-ssl-cert.pem';
}

/* Decode and save multiple SSL CA certificates from base64 */
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
}

/* Decode and save multiple SSL keys from base64 */
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
}

/* Decode and save multiple SSL certificates from base64 */
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
}

/* Figure out hosts */

/* Fallback to default linked */
Expand All @@ -66,11 +118,19 @@
$verbose = [$_ENV['PMA_VERBOSE']];
$ports = [$_ENV['PMA_PORT']];
$ssls = [$_ENV['PMA_SSL']];
$ssl_verifies = [$_ENV['PMA_SSL_VERIFY']];
$ssl_cas = [$_ENV['PMA_SSL_CA']];
$ssl_keys = [$_ENV['PMA_SSL_KEY']];
$ssl_certs = [$_ENV['PMA_SSL_CERT']];
} elseif (! empty($_ENV['PMA_HOSTS'])) {
$hosts = array_map('trim', explode(',', $_ENV['PMA_HOSTS']));
$verbose = array_map('trim', explode(',', $_ENV['PMA_VERBOSES']));
$ports = array_map('trim', explode(',', $_ENV['PMA_PORTS']));
$ssls = array_map('trim', explode(',', $_ENV['PMA_SSLS']));
$ssl_verifies = array_map('trim', explode(',', $_ENV['PMA_SSL_VERIFIES']));
$ssl_cas = array_map('trim', explode(',', $_ENV['PMA_SSL_CAS']));
$ssl_keys = array_map('trim', explode(',', $_ENV['PMA_SSL_KEYS']));
$ssl_certs = array_map('trim', explode(',', $_ENV['PMA_SSL_CERTS']));
}

if (! empty($_ENV['PMA_SOCKET'])) {
Expand All @@ -84,6 +144,18 @@
if (isset($ssls[$i - 1]) && $ssls[$i - 1] === '1') {
$cfg['Servers'][$i]['ssl'] = $ssls[$i - 1];
}
if (isset($ssl_verifies[$i - 1]) && $ssl_verifies[$i - 1] === '1') {
$cfg['Servers'][$i]['ssl_verify'] = $ssl_verifies[$i - 1];
}
if (isset($ssl_cas[$i - 1])) {
$cfg['Servers'][$i]['ssl_ca'] = $ssl_cas[$i - 1];
}
if (isset($ssl_keys[$i - 1])) {
$cfg['Servers'][$i]['ssl_key'] = $ssl_keys[$i - 1];
}
if (isset($ssl_certs[$i - 1])) {
$cfg['Servers'][$i]['ssl_cert'] = $ssl_certs[$i - 1];
}
$cfg['Servers'][$i]['host'] = $hosts[$i - 1];
if (isset($verbose[$i - 1])) {
$cfg['Servers'][$i]['verbose'] = $verbose[$i - 1];
Expand Down
44 changes: 44 additions & 0 deletions apache/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
williamdes marked this conversation as resolved.
Show resolved Hide resolved

class SslFileGenerationException extends Exception {}
LordRobinCbz marked this conversation as resolved.
Show resolved Hide resolved

define('OUTPUT_DIR', '/etc/phpmyadmin/ssl');
williamdes marked this conversation as resolved.
Show resolved Hide resolved

/**
* Helper function to decode and save multiple SSL files from base64.
*
* @param string $base64_string The base64 encoded string containing multiple SSL files separated by commas.
* If no commas are present, the entire string is treated as a single file.
* @param string $prefix The prefix to use for the generated SSL file names.
* @param string $extension The file extension to use for the generated SSL files.
* @return string A comma-separated list of paths to the generated SSL files.
*/
function decodeAndSaveSslFiles(string $base64_string, string $prefix, string $extension): array {
// Ensure the output directory exists
if (!is_dir(OUTPUT_DIR)) {
mkdir(OUTPUT_DIR, 0755, true);
}

// Split the base64 string into an array of files
$files = strpos($base64_string, ',') !== false ? explode(',', $base64_string) : [$base64_string];
$counter = 1;
$ssl_files = [];

// Process each file
foreach ($files as $file) {
$output_file = OUTPUT_DIR . "/pma-ssl-$prefix-$counter.$extension";

// Write the decoded file to the output directory
if (file_put_contents($output_file, base64_decode($file)) === false) {
echo 'Failed to write to ' . $output_file;
exit(1);
}

// Add the output file path to the list
$ssl_files[] = $output_file;
$counter++;
}

// Return a comma-separated list of the generated file paths
return implode(',', $ssl_files);
}
1 change: 1 addition & 0 deletions fpm-alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ RUN set -ex; \

# Copy configuration
COPY config.inc.php /etc/phpmyadmin/config.inc.php
COPY helpers.php /etc/phpmyadmin/helpers.php
RUN chown www-data:www-data -R /etc/phpmyadmin/

# Copy main script
Expand Down
72 changes: 72 additions & 0 deletions fpm-alpine/config.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

define('SSL_DIR', '/etc/phpmyadmin/ssl');

require '/etc/phpmyadmin/config.secret.inc.php';
require '/etc/phpmyadmin/helpers.php';

/* Ensure we got the environment */
$vars = [
Expand Down Expand Up @@ -28,7 +31,15 @@
'PMA_UPLOADDIR',
'PMA_SAVEDIR',
'PMA_SSL',
'PMA_SSL_VERIFY',
'PMA_SSL_CA',
'PMA_SSL_KEY',
'PMA_SSL_CERT',
'PMA_SSLS',
'PMA_SSL_VERIFIES',
'PMA_SSL_CAS',
'PMA_SSL_KEYS',
'PMA_SSL_CERTS'
];

foreach ($vars as $var) {
Expand All @@ -55,6 +66,47 @@
$cfg['PmaAbsoluteUri'] = trim($_ENV['PMA_ABSOLUTE_URI']);
}

if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
$_ENV['PMA_SSL_CA'] = SSL_DIR . '/pma-ssl-ca.pem';
}

/* Decode and save the SSL key from base64 */
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
$_ENV['PMA_SSL_KEY'] = SSL_DIR . '/pma-ssl-key.key';
}

/* Decode and save the SSL certificate from base64 */
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
if (!is_dir(SSL_DIR)) {
mkdir(SSL_DIR, 0755, true);
}
file_put_contents(SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
$_ENV['PMA_SSL_CERT'] = SSL_DIR . '/pma-ssl-cert.pem';
}

/* Decode and save multiple SSL CA certificates from base64 */
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
}

/* Decode and save multiple SSL keys from base64 */
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
}

/* Decode and save multiple SSL certificates from base64 */
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
}

/* Figure out hosts */

/* Fallback to default linked */
Expand All @@ -66,11 +118,19 @@
$verbose = [$_ENV['PMA_VERBOSE']];
$ports = [$_ENV['PMA_PORT']];
$ssls = [$_ENV['PMA_SSL']];
$ssl_verifies = [$_ENV['PMA_SSL_VERIFY']];
$ssl_cas = [$_ENV['PMA_SSL_CA']];
$ssl_keys = [$_ENV['PMA_SSL_KEY']];
$ssl_certs = [$_ENV['PMA_SSL_CERT']];
} elseif (! empty($_ENV['PMA_HOSTS'])) {
$hosts = array_map('trim', explode(',', $_ENV['PMA_HOSTS']));
$verbose = array_map('trim', explode(',', $_ENV['PMA_VERBOSES']));
$ports = array_map('trim', explode(',', $_ENV['PMA_PORTS']));
$ssls = array_map('trim', explode(',', $_ENV['PMA_SSLS']));
$ssl_verifies = array_map('trim', explode(',', $_ENV['PMA_SSL_VERIFIES']));
$ssl_cas = array_map('trim', explode(',', $_ENV['PMA_SSL_CAS']));
$ssl_keys = array_map('trim', explode(',', $_ENV['PMA_SSL_KEYS']));
$ssl_certs = array_map('trim', explode(',', $_ENV['PMA_SSL_CERTS']));
}

if (! empty($_ENV['PMA_SOCKET'])) {
Expand All @@ -84,6 +144,18 @@
if (isset($ssls[$i - 1]) && $ssls[$i - 1] === '1') {
$cfg['Servers'][$i]['ssl'] = $ssls[$i - 1];
}
if (isset($ssl_verifies[$i - 1]) && $ssl_verifies[$i - 1] === '1') {
$cfg['Servers'][$i]['ssl_verify'] = $ssl_verifies[$i - 1];
}
if (isset($ssl_cas[$i - 1])) {
$cfg['Servers'][$i]['ssl_ca'] = $ssl_cas[$i - 1];
}
if (isset($ssl_keys[$i - 1])) {
$cfg['Servers'][$i]['ssl_key'] = $ssl_keys[$i - 1];
}
if (isset($ssl_certs[$i - 1])) {
$cfg['Servers'][$i]['ssl_cert'] = $ssl_certs[$i - 1];
}
$cfg['Servers'][$i]['host'] = $hosts[$i - 1];
if (isset($verbose[$i - 1])) {
$cfg['Servers'][$i]['verbose'] = $verbose[$i - 1];
Expand Down
1 change: 0 additions & 1 deletion fpm-alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ if [ ! -z "${PMA_USER_CONFIG_BASE64}" ]; then
echo "${PMA_USER_CONFIG_BASE64}" | base64 -d > /etc/phpmyadmin/config.user.inc.php
fi


get_docker_secret() {
local env_var="${1}"
local env_var_file="${env_var}_FILE"
Expand Down
43 changes: 43 additions & 0 deletions fpm-alpine/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class SslFileGenerationException extends Exception {}

define('OUTPUT_DIR', '/etc/phpmyadmin/ssl');

/**
* Helper function to decode and save multiple SSL files from base64.
*
* @param string $base64_string The base64 encoded string containing multiple SSL files separated by commas.
* If no commas are present, the entire string is treated as a single file.
* @param string $prefix The prefix to use for the generated SSL file names.
* @param string $extension The file extension to use for the generated SSL files.
* @return string A comma-separated list of paths to the generated SSL files.
*/
function decodeAndSaveSslFiles($base64_string, $prefix, $extension) {
// Ensure the output directory exists
if (!is_dir(OUTPUT_DIR)) {
mkdir(OUTPUT_DIR, 0755, true);
}

// Split the base64 string into an array of files
$files = strpos($base64_string, ',') !== false ? explode(',', $base64_string) : [$base64_string];
$counter = 1;
$ssl_files = [];

// Process each file
foreach ($files as $file) {
$output_file = OUTPUT_DIR . "/pma-ssl-$prefix-$counter.$extension";

// Write the decoded file to the output directory
if (file_put_contents($output_file, base64_decode($file)) === false) {
throw new SslFileGenerationException("Failed to write to $output_file");
}

// Add the output file path to the list
$ssl_files[] = $output_file;
$counter++;
}

// Return a comma-separated list of the generated file paths
return implode(',', $ssl_files);
}
1 change: 1 addition & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ RUN set -ex; \

# Copy configuration
COPY config.inc.php /etc/phpmyadmin/config.inc.php
COPY helpers.php /etc/phpmyadmin/helpers.php
RUN chown www-data:www-data -R /etc/phpmyadmin/

# Copy main script
Expand Down
Loading