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

Refactor env vars from file #1942

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
32 changes: 10 additions & 22 deletions .config/autoconfig.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
<?php

require_once "util.php";
Copy link
Contributor Author

@aentwist aentwist Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with the autoloading for this. Feedback welcome

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html#multiple-config-php-file

Furthermore, Nextcloud accepts some parameters from NC_ prefixed environment variables. This behaviour is already complex enough. I'd not add any custom code inclusions in the config directory.

The reason why all database parameters should be configured is because the automated setup only works if all of them are specified:

elif [ -n "${POSTGRES_DB+x}" ] && [ -n "${POSTGRES_USER+x}" ] && [ -n "${POSTGRES_PASSWORD+x}" ] && [ -n "${POSTGRES_HOST+x}" ]; then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the first part, are you suggesting that I try to have getFileEnv included by renaming the file to x.config.php? Or simply forget the whole idea?


As for the database part -

In the entrypoint script those values can independently be from file or not.

docker/docker-entrypoint.sh

Lines 162 to 164 in 405e815

file_env POSTGRES_DB
file_env POSTGRES_PASSWORD
file_env POSTGRES_USER

The improvement here isn't that we allow some to go missing, it is that this PR just so happens to allow the same thing in the PHP. That is,

current behavior (as described in the README section removed by this PR)

  • must have postgres_db, _user, _password, _host OR
  • postgres_db_file, _user_file, _password_file, _host

updated behavior

  • must have one of postgres_db or postgres_db_file AND
  • _user or user_file AND
  • _password or _password_file AND
  • _host

Just noting that any potential confusion from this (e.g. documented by #1148 (comment)) is additionally avoided by this PR.


$autoconfig_enabled = false;

if (getenv('SQLITE_DATABASE')) {
$AUTOCONFIG['dbtype'] = 'sqlite';
$AUTOCONFIG['dbname'] = getenv('SQLITE_DATABASE');
$autoconfig_enabled = true;
} elseif (getenv('MYSQL_DATABASE_FILE') && getenv('MYSQL_USER_FILE') && getenv('MYSQL_PASSWORD_FILE') && getenv('MYSQL_HOST')) {
$AUTOCONFIG['dbtype'] = 'mysql';
$AUTOCONFIG['dbname'] = trim(file_get_contents(getenv('MYSQL_DATABASE_FILE')));
$AUTOCONFIG['dbuser'] = trim(file_get_contents(getenv('MYSQL_USER_FILE')));
$AUTOCONFIG['dbpass'] = trim(file_get_contents(getenv('MYSQL_PASSWORD_FILE')));
$AUTOCONFIG['dbhost'] = getenv('MYSQL_HOST');
$autoconfig_enabled = true;
} elseif (getenv('MYSQL_DATABASE') && getenv('MYSQL_USER') && getenv('MYSQL_PASSWORD') && getenv('MYSQL_HOST')) {
} elseif (getFileEnv('MYSQL_DATABASE') && getFileEnv('MYSQL_USER') && getFileEnv('MYSQL_PASSWORD') && getenv('MYSQL_HOST')) {
$AUTOCONFIG['dbtype'] = 'mysql';
$AUTOCONFIG['dbname'] = getenv('MYSQL_DATABASE');
$AUTOCONFIG['dbuser'] = getenv('MYSQL_USER');
$AUTOCONFIG['dbpass'] = getenv('MYSQL_PASSWORD');
$AUTOCONFIG['dbname'] = getFileEnv('MYSQL_DATABASE');
$AUTOCONFIG['dbuser'] = getFileEnv('MYSQL_USER');
$AUTOCONFIG['dbpass'] = getFileEnv('MYSQL_PASSWORD');
$AUTOCONFIG['dbhost'] = getenv('MYSQL_HOST');
$autoconfig_enabled = true;
} elseif (getenv('POSTGRES_DB_FILE') && getenv('POSTGRES_USER_FILE') && getenv('POSTGRES_PASSWORD_FILE') && getenv('POSTGRES_HOST')) {
$AUTOCONFIG['dbtype'] = 'pgsql';
$AUTOCONFIG['dbname'] = trim(file_get_contents(getenv('POSTGRES_DB_FILE')));
$AUTOCONFIG['dbuser'] = trim(file_get_contents(getenv('POSTGRES_USER_FILE')));
$AUTOCONFIG['dbpass'] = trim(file_get_contents(getenv('POSTGRES_PASSWORD_FILE')));
$AUTOCONFIG['dbhost'] = getenv('POSTGRES_HOST');
$autoconfig_enabled = true;
} elseif (getenv('POSTGRES_DB') && getenv('POSTGRES_USER') && getenv('POSTGRES_PASSWORD') && getenv('POSTGRES_HOST')) {
} elseif (getFileEnv('POSTGRES_DB') && getFileEnv('POSTGRES_USER') && getFileEnv('POSTGRES_PASSWORD') && getenv('POSTGRES_HOST')) {
$AUTOCONFIG['dbtype'] = 'pgsql';
$AUTOCONFIG['dbname'] = getenv('POSTGRES_DB');
$AUTOCONFIG['dbuser'] = getenv('POSTGRES_USER');
$AUTOCONFIG['dbpass'] = getenv('POSTGRES_PASSWORD');
$AUTOCONFIG['dbname'] = getFileEnv('POSTGRES_DB');
$AUTOCONFIG['dbuser'] = getFileEnv('POSTGRES_USER');
$AUTOCONFIG['dbpass'] = getFileEnv('POSTGRES_PASSWORD');
$AUTOCONFIG['dbhost'] = getenv('POSTGRES_HOST');
$autoconfig_enabled = true;
}
Expand Down
12 changes: 4 additions & 8 deletions .config/smtp.config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

require_once "util.php";

if (getenv('SMTP_HOST') && getenv('MAIL_FROM_ADDRESS') && getenv('MAIL_DOMAIN')) {
$CONFIG = array (
'mail_smtpmode' => 'smtp',
Expand All @@ -8,15 +11,8 @@
'mail_smtpauth' => getenv('SMTP_NAME') && (getenv('SMTP_PASSWORD') || (getenv('SMTP_PASSWORD_FILE') && file_exists(getenv('SMTP_PASSWORD_FILE')))),
'mail_smtpauthtype' => getenv('SMTP_AUTHTYPE') ?: 'LOGIN',
'mail_smtpname' => getenv('SMTP_NAME') ?: '',
'mail_smtppassword' => getFileEnv('SMTP_PASSWORD', ''),
'mail_from_address' => getenv('MAIL_FROM_ADDRESS'),
'mail_domain' => getenv('MAIL_DOMAIN'),
);

if (getenv('SMTP_PASSWORD_FILE') && file_exists(getenv('SMTP_PASSWORD_FILE'))) {
$CONFIG['mail_smtppassword'] = trim(file_get_contents(getenv('SMTP_PASSWORD_FILE')));
} elseif (getenv('SMTP_PASSWORD')) {
$CONFIG['mail_smtppassword'] = getenv('SMTP_PASSWORD');
} else {
$CONFIG['mail_smtppassword'] = '';
}
}
27 changes: 27 additions & 0 deletions .config/util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* Gets the value of an environment variable or the contents of the file
* at the path specified by its value if it exists with a suffix of "_FILE"
*/
function getFileEnv(string $envVarName, ?string $defaultValue): ?string {
$FILE_ENV_VAR_SUFFIX = "_FILE";

$fileEnvVarName = "$envVarName$FILE_ENV_VAR_SUFFIX";
$filename = getenv($fileEnvVarName);
$envVarValue = getenv($envVarName);

$configValue = null;
if ($filename && file_exists($filename)) {
$configValue = trim(file_get_contents($filename));
} else if ($envVarValue) {
$configValue = $envVarValue;
} else if ($defaultValue) {
$configValue = $defaultValue;
}
return $configValue;
}

?>
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ secrets:

Currently, this is only supported for `NEXTCLOUD_ADMIN_PASSWORD`, `NEXTCLOUD_ADMIN_USER`, `MYSQL_DATABASE`, `MYSQL_PASSWORD`, `MYSQL_USER`, `POSTGRES_DB`, `POSTGRES_PASSWORD`, `POSTGRES_USER`, `REDIS_HOST_PASSWORD`, `SMTP_PASSWORD`, `OBJECTSTORE_S3_KEY`, and `OBJECTSTORE_S3_SECRET`.

If you set any group of values (i.e. all of `MYSQL_DATABASE_FILE`, `MYSQL_USER_FILE`, `MYSQL_PASSWORD_FILE`, `MYSQL_HOST`), the script will not use the corresponding group of environment variables (`MYSQL_DATABASE`, `MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_HOST`).

# Make your Nextcloud available from the internet
Until here, your Nextcloud is just available from your docker host. If you want your Nextcloud available from the internet adding SSL encryption is mandatory.

Expand Down